[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 03/72] python/qmp: add parse_address classmethod
From: |
John Snow |
Subject: |
[PULL 03/72] python/qmp: add parse_address classmethod |
Date: |
Fri, 18 Jun 2021 19:03:46 -0400 |
This takes the place of qmp-shell's __get_address function. It also
allows other utilities to share the same parser and syntax for
specifying QMP locations.
Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 20210603003719.1321369-4-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
---
python/qemu/qmp/__init__.py | 26 ++++++++++++++++++++++++++
scripts/qmp/qmp-shell | 21 ++-------------------
2 files changed, 28 insertions(+), 19 deletions(-)
diff --git a/python/qemu/qmp/__init__.py b/python/qemu/qmp/__init__.py
index 5fb970f8a8..822c793c32 100644
--- a/python/qemu/qmp/__init__.py
+++ b/python/qemu/qmp/__init__.py
@@ -92,6 +92,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
@@ -219,6 +225,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 b4d06096ab..d5ae8a9b21 100755
--- a/scripts/qmp/qmp-shell
+++ b/scripts/qmp/qmp-shell
@@ -89,8 +89,6 @@ class QMPCompleter(list):
class QMPShellError(Exception):
pass
-class QMPShellBadPort(QMPShellError):
- pass
class FuzzyJSON(ast.NodeTransformer):
'''This extension of ast.NodeTransformer filters literal "true/false/null"
@@ -109,7 +107,7 @@ class FuzzyJSON(ast.NodeTransformer):
# _execute_cmd()). Let's design a better one.
class QMPShell(qmp.QEMUMonitorProtocol):
def __init__(self, address, pretty=False):
- super(QMPShell, self).__init__(self.__get_address(address))
+ super(QMPShell, self).__init__(self.parse_address(address))
self._greeting = None
self._completer = None
self._pretty = pretty
@@ -118,21 +116,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
self._histfile = os.path.join(os.path.expanduser('~'),
'.qmp-shell_history')
- def __get_address(self, 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:
- raise QMPShellBadPort
- return ( addr[0], port )
- # socket path
- return arg
-
def _fill_completion(self):
cmds = self.cmd('query-commands')
if 'error' in cmds:
@@ -437,7 +420,7 @@ def main():
if qemu is None:
fail_cmdline()
- except QMPShellBadPort:
+ except qmp.QMPBadPortError:
die('bad port number in command-line')
try:
--
2.31.1
- [PULL 17/72] scripts/qom-fuse: move to python/qemu/qmp/qom_fuse.py, (continued)
- [PULL 17/72] scripts/qom-fuse: move to python/qemu/qmp/qom_fuse.py, John Snow, 2021/06/18
- [PULL 01/72] python/pipenv: Update Pipfile.lock, John Snow, 2021/06/18
- [PULL 08/72] scripts/qom-fuse: apply flake8 rules, John Snow, 2021/06/18
- [PULL 15/72] scripts/qom-fuse: add static type hints, John Snow, 2021/06/18
- [PULL 11/72] scripts/qom-fuse: Add docstrings, John Snow, 2021/06/18
- [PULL 16/72] python: add optional FUSE dependencies, John Snow, 2021/06/18
- [PULL 30/72] scripts/qemu-ga-client: Add forwarder shim, John Snow, 2021/06/18
- [PULL 35/72] scripts/qmp-shell: fix connect method signature, John Snow, 2021/06/18
- [PULL 32/72] scripts/qmp-shell: Apply flake8 rules, John Snow, 2021/06/18
- [PULL 38/72] scripts/qmp-shell: Use python3-style super(), John Snow, 2021/06/18
- [PULL 03/72] python/qmp: add parse_address classmethod,
John Snow <=
- [PULL 04/72] python/qmp: Add qom script rewrites, John Snow, 2021/06/18
- [PULL 13/72] scripts/qom-fuse: use QOMCommand.qom_list(), John Snow, 2021/06/18
- [PULL 18/72] scripts/qom-fuse: add redirection shim to python/qemu/qmp/qom-fuse.py, John Snow, 2021/06/18
- [PULL 20/72] scripts/qemu-ga-client: apply isort rules, John Snow, 2021/06/18
- [PULL 22/72] scripts/qemu-ga-client: Fix exception handling, John Snow, 2021/06/18
- [PULL 27/72] scripts/qemu-ga-client: add mypy type hints, John Snow, 2021/06/18
- [PULL 26/72] python/qmp: Correct type of QMPReturnValue, John Snow, 2021/06/18
- [PULL 28/72] scripts/qemu-ga-client: move to python/qemu/qmp/qemu_ga_client.py, John Snow, 2021/06/18
- [PULL 36/72] scripts/qmp-shell: remove shadowed variable from _print(), John Snow, 2021/06/18
- [PULL 19/72] python/qmp: add fuse command to 'qom' tools, John Snow, 2021/06/18