python/aqmp: take QMPBadPortError and parse_address from qemu.qmp

Shift these definitions over from the qmp package to the async qmp
package.

(Licensing: this is a lateral move, from GPLv2 (only) to GPLv2 (only))

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Beraldo Leal <bleal@redhat.com>
Message-id: 20220330172812.3427355-3-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
This commit is contained in:
John Snow 2022-03-30 13:28:05 -04:00
parent 335e7d410e
commit 9fcd3930e0
3 changed files with 27 additions and 32 deletions

View file

@ -35,9 +35,8 @@ from pygments import token as Token
import urwid import urwid
import urwid_readline import urwid_readline
from qemu.qmp import QEMUMonitorProtocol, QMPBadPortError
from .error import ProtocolError from .error import ProtocolError
from .legacy import QEMUMonitorProtocol, QMPBadPortError
from .message import DeserializationError, Message, UnexpectedTypeError from .message import DeserializationError, Message, UnexpectedTypeError
from .protocol import ConnectError, Runstate from .protocol import ConnectError, Runstate
from .qmp_client import ExecInterruptedError, QMPClient from .qmp_client import ExecInterruptedError, QMPClient

View file

@ -33,9 +33,6 @@ from .protocol import Runstate, SocketAddrT
from .qmp_client import QMPClient from .qmp_client import QMPClient
# (Temporarily) Re-export QMPBadPortError
QMPBadPortError = qemu.qmp.QMPBadPortError
#: QMPMessage is an entire QMP message of any kind. #: QMPMessage is an entire QMP message of any kind.
QMPMessage = Dict[str, Any] QMPMessage = Dict[str, Any]
@ -56,6 +53,12 @@ QMPObject = Dict[str, object]
# pylint: disable=missing-docstring # pylint: disable=missing-docstring
class QMPBadPortError(QMPError):
"""
Unable to parse socket address: Port was non-numerical.
"""
class QEMUMonitorProtocol(qemu.qmp.QEMUMonitorProtocol): class QEMUMonitorProtocol(qemu.qmp.QEMUMonitorProtocol):
def __init__(self, address: SocketAddrT, def __init__(self, address: SocketAddrT,
server: bool = False, server: bool = False,
@ -86,7 +89,26 @@ class QEMUMonitorProtocol(qemu.qmp.QEMUMonitorProtocol):
return None return None
# __enter__ and __exit__ need no changes # __enter__ and __exit__ need no changes
# parse_address needs no changes
@classmethod
def parse_address(cls, address: str) -> SocketAddrT:
"""
Parse a string into a QMP address.
Figure out if the argument is in the port:host form.
If it's not, it's probably a file path.
"""
components = address.split(':')
if len(components) == 2:
try:
port = int(components[1])
except ValueError:
msg = f"Bad port: '{components[1]}' in '{address}'."
raise QMPBadPortError(msg) from None
return (components[0], port)
# Treat as filepath.
return address
def connect(self, negotiate: bool = True) -> Optional[QMPMessage]: def connect(self, negotiate: bool = True) -> Optional[QMPMessage]:
self._aqmp.await_greeting = negotiate self._aqmp.await_greeting = negotiate

View file

@ -102,12 +102,6 @@ class QMPResponseError(QMPError):
self.reply = reply self.reply = reply
class QMPBadPortError(QMPError):
"""
Unable to parse socket address: Port was non-numerical.
"""
class QEMUMonitorProtocol: class QEMUMonitorProtocol:
""" """
Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then
@ -237,26 +231,6 @@ class QEMUMonitorProtocol:
# Implement context manager exit function. # Implement context manager exit function.
self.close() self.close()
@classmethod
def parse_address(cls, address: str) -> SocketAddrT:
"""
Parse a string into a QMP address.
Figure out if the argument is in the port:host form.
If it's not, it's probably a file path.
"""
components = address.split(':')
if len(components) == 2:
try:
port = int(components[1])
except ValueError:
msg = f"Bad port: '{components[1]}' in '{address}'."
raise QMPBadPortError(msg) from None
return (components[0], port)
# Treat as filepath.
return address
def connect(self, negotiate: bool = True) -> Optional[QMPMessage]: def connect(self, negotiate: bool = True) -> Optional[QMPMessage]:
""" """
Connect to the QMP Monitor and perform capabilities negotiation. Connect to the QMP Monitor and perform capabilities negotiation.