[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 47/72] python/qmp: add parse_address classmethod
From: |
John Snow |
Subject: |
[PATCH v2 47/72] python/qmp: add parse_address classmethod |
Date: |
Tue, 3 Nov 2020 19:35:37 -0500 |
This takes the place of qmp-shell's __get_address function.
Signed-off-by: John Snow <jsnow@redhat.com>
---
python/qemu/qmp/__init__.py | 26 ++++++++++++++++++++++++++
scripts/qmp/qmp-shell | 27 ++-------------------------
2 files changed, 28 insertions(+), 25 deletions(-)
diff --git a/python/qemu/qmp/__init__.py b/python/qemu/qmp/__init__.py
index dd211e3f6511..a6e1a7b85775 100644
--- a/python/qemu/qmp/__init__.py
+++ b/python/qemu/qmp/__init__.py
@@ -97,6 +97,12 @@ def __init__(self, reply: QMPMessage):
self.reply = reply
+class QMPBadPortError(QMPError):
+ """
+ Unable to parse socket address: Port was non-numerical.
+ """
+
+
class QEMUMonitorProtocol:
"""
Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then
@@ -224,6 +230,26 @@ def __exit__(self,
# Implement context manager exit function.
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]:
"""
Connect to the QMP Monitor and perform capabilities negotiation.
diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell
index d5496aeac0bd..5a72b9d39502 100755
--- a/scripts/qmp/qmp-shell
+++ b/scripts/qmp/qmp-shell
@@ -92,10 +92,6 @@ class QMPShellError(Exception):
pass
-class QMPShellBadPort(QMPShellError):
- pass
-
-
class FuzzyJSON(ast.NodeTransformer):
"""
This extension of ast.NodeTransformer filters literal "true/false/null"
@@ -118,7 +114,7 @@ class FuzzyJSON(ast.NodeTransformer):
# _execute_cmd()). Let's design a better one.
class QMPShell(qmp.QEMUMonitorProtocol):
def __init__(self, address, pretty=False):
- super().__init__(self.__get_address(address))
+ super().__init__(self.parse_address(address))
self._greeting = None
self._completer = None
self._pretty = pretty
@@ -128,22 +124,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
'.qmp-shell_history')
self._verbose = False
- @classmethod
- def __get_address(cls, arg):
- """
- Figure out if the argument is in the port:host form, if it's not it's
- probably a file path.
- """
- addr = arg.split(':')
- if len(addr) == 2:
- try:
- port = int(addr[1])
- except ValueError as err:
- raise QMPShellBadPort from err
- return addr[0], port
- # socket path
- return arg
-
def _fill_completion(self):
cmds = self.cmd('query-commands')
if 'error' in cmds:
@@ -337,9 +317,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
return self._execute_cmd(cmdline)
- def set_verbosity(self, verbose):
- self._verbose = verbose
-
class HMPShell(QMPShell):
def __init__(self, address):
@@ -447,7 +424,7 @@ def main():
qemu = HMPShell(args.qmp_server)
else:
qemu = QMPShell(args.qmp_server, args.pretty)
- except QMPShellBadPort:
+ except qmp.QMPBadPortError:
parser.error(f"Bad port number: {args.qmp_server}")
return # pycharm doesn't know error() is noreturn
--
2.26.2
- [PATCH v2 36/72] scripts/qmp-shell: declare verbose in __init__, (continued)
- [PATCH v2 36/72] scripts/qmp-shell: declare verbose in __init__, John Snow, 2020/11/03
- [PATCH v2 39/72] scripts/qmp-shell: make QMPCompleter returns explicit, John Snow, 2020/11/03
- [PATCH v2 42/72] scripts/qmp-shell: explicitly chain exception context, John Snow, 2020/11/03
- [PATCH v2 37/72] scripts/qmp-shell: use triple-double-quote docstring style, John Snow, 2020/11/03
- [PATCH v2 43/72] scripts/qmp-shell: remove if-raise-else patterns, John Snow, 2020/11/03
- [PATCH v2 41/72] scripts/qmp-shell: fix shell history exception handling, John Snow, 2020/11/03
- [PATCH v2 44/72] scripts/qmp-shell: use isinstance() instead of type(), John Snow, 2020/11/03
- [PATCH v2 45/72] scripts/qmp-shell: use argparse, John Snow, 2020/11/03
- [PATCH v2 46/72] python/qmp: Fix type of SocketAddrT, John Snow, 2020/11/03
- [PATCH v2 38/72] scripts/qmp-shell: ignore visit_Name name, John Snow, 2020/11/03
- [PATCH v2 47/72] python/qmp: add parse_address classmethod,
John Snow <=
- [PATCH v2 48/72] scripts/qmp-shell: Add pretty attribute to HMP shell, John Snow, 2020/11/03
- [PATCH v2 49/72] scripts/qmp-shell: Make verbose a public attribute, John Snow, 2020/11/03
- [PATCH v2 50/72] scripts/qmp-shell: move get_prompt() to prompt property, John Snow, 2020/11/03
- [PATCH v2 51/72] scripts/qmp-shell: remove prompt argument from read_exec_command, John Snow, 2020/11/03
- [PATCH v2 52/72] scripts/qmp-shell: move the REPL functionality into QMPShell, John Snow, 2020/11/03
- [PATCH v2 53/72] scripts/qmp-shell: Fix "FuzzyJSON" parser, John Snow, 2020/11/03
- [PATCH v2 54/72] scripts/qmp-shell: refactor QMPCompleter, John Snow, 2020/11/03
- [PATCH v2 56/72] python/qmp: add QMPObject type alias, John Snow, 2020/11/03
- [PATCH v2 59/72] scripts/qmp-shell: unprivatize 'pretty' property, John Snow, 2020/11/03
- [PATCH v2 58/72] scripts/qmp-shell: Accept SocketAddrT instead of string, John Snow, 2020/11/03