From 9d47f6de10e36c988e935069bf288d39bdcc1126 Mon Sep 17 00:00:00 2001 From: Lukáš Doktor Date: Fri, 18 Aug 2017 16:26:09 +0200 Subject: qmp.py: Couple of pylint/style fixes No actual code changes, just initializing attributes earlier to avoid AttributeError on early introspection, a few pylint/style fixes and docstring clarifications. Signed-off-by: Lukáš Doktor Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20170818142613.32394-7-ldoktor@redhat.com> Signed-off-by: Eduardo Habkost --- scripts/qmp/qmp.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'scripts/qmp/qmp.py') diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 62d3651967..782d1ac5df 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -13,19 +13,30 @@ import errno import socket import sys + class QMPError(Exception): pass + class QMPConnectError(QMPError): pass + class QMPCapabilitiesError(QMPError): pass + class QMPTimeoutError(QMPError): pass + class QEMUMonitorProtocol: + + #: Socket's error class + error = socket.error + #: Socket's timeout + timeout = socket.timeout + def __init__(self, address, server=False, debug=False): """ Create a QEMUMonitorProtocol class. @@ -42,6 +53,7 @@ class QEMUMonitorProtocol: self.__address = address self._debug = debug self.__sock = self.__get_sock() + self.__sockfile = None if server: self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__sock.bind(self.__address) @@ -56,7 +68,7 @@ class QEMUMonitorProtocol: def __negotiate_capabilities(self): greeting = self.__json_read() - if greeting is None or not greeting.has_key('QMP'): + if greeting is None or "QMP" not in greeting: raise QMPConnectError # Greeting seems ok, negotiate capabilities resp = self.cmd('qmp_capabilities') @@ -78,8 +90,6 @@ class QEMUMonitorProtocol: continue return resp - error = socket.error - def __get_events(self, wait=False): """ Check for new events in the stream and cache them in __events. @@ -89,8 +99,8 @@ class QEMUMonitorProtocol: @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. """ # Check for new events regardless and pull them into the cache: @@ -175,7 +185,7 @@ class QEMUMonitorProtocol: @param args: command arguments (dict) @param id: command id (dict, list, string or int) """ - qmp_cmd = { 'execute': name } + qmp_cmd = {'execute': name} if args: qmp_cmd['arguments'] = args if id: @@ -183,6 +193,9 @@ class QEMUMonitorProtocol: return self.cmd_obj(qmp_cmd) def command(self, cmd, **kwds): + """ + Build and send a QMP command to the monitor, report errors if any + """ ret = self.cmd(cmd, kwds) if ret.has_key('error'): raise Exception(ret['error']['desc']) @@ -190,15 +203,15 @@ class QEMUMonitorProtocol: def pull_event(self, wait=False): """ - Get and delete the first available QMP event. + Pulls a single event. @param wait (bool): block until an event is available. @param wait (float): If wait is a float, treat it as a timeout value. @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. @return The first available QMP event, or None. """ @@ -217,8 +230,8 @@ class QEMUMonitorProtocol: @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. @return The list of available QMP events. """ @@ -235,8 +248,6 @@ class QEMUMonitorProtocol: self.__sock.close() self.__sockfile.close() - timeout = socket.timeout - def settimeout(self, timeout): self.__sock.settimeout(timeout) -- cgit v1.2.3-55-g7522 From 3dd29b4133e4f6ad595bcd835fc0b3302a3304fe Mon Sep 17 00:00:00 2001 From: Lukáš Doktor Date: Fri, 18 Aug 2017 16:26:10 +0200 Subject: qmp.py: Use object-based class for QEMUMonitorProtocol There is no need to define QEMUMonitorProtocol as old-style class. Signed-off-by: Lukáš Doktor Reviewed-by: Eduardo Habkost Message-Id: <20170818142613.32394-8-ldoktor@redhat.com> Signed-off-by: Eduardo Habkost --- scripts/qmp/qmp-shell | 4 ++-- scripts/qmp/qmp.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'scripts/qmp/qmp.py') diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index 860ffb27f2..be449de621 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -106,7 +106,7 @@ class FuzzyJSON(ast.NodeTransformer): # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): def __init__(self, address, pretty=False): - qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) + super(QMPShell, self).__init__(self.__get_address(address)) self._greeting = None self._completer = None self._pretty = pretty @@ -281,7 +281,7 @@ class QMPShell(qmp.QEMUMonitorProtocol): return True def connect(self, negotiate): - self._greeting = qmp.QEMUMonitorProtocol.connect(self, negotiate) + self._greeting = super(QMPShell, self).connect(negotiate) self.__completer_setup() def show_banner(self, msg='Welcome to the QMP low-level shell!'): diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 782d1ac5df..95ff5cc39a 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -30,7 +30,7 @@ class QMPTimeoutError(QMPError): pass -class QEMUMonitorProtocol: +class QEMUMonitorProtocol(object): #: Socket's error class error = socket.error -- cgit v1.2.3-55-g7522 From 2cb05a3f3613f633b916b854cfaf04c663ccc953 Mon Sep 17 00:00:00 2001 From: Lukáš Doktor Date: Fri, 18 Aug 2017 16:26:11 +0200 Subject: qmp.py: Avoid "has_key" usage The "has_key" is deprecated in favor of "__in__" operator. Signed-off-by: Lukáš Doktor Reviewed-by: Eduardo Habkost Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20170818142613.32394-9-ldoktor@redhat.com> Signed-off-by: Eduardo Habkost --- scripts/qmp/qmp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts/qmp/qmp.py') diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 95ff5cc39a..f2f5a9b296 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -197,7 +197,7 @@ class QEMUMonitorProtocol(object): Build and send a QMP command to the monitor, report errors if any """ ret = self.cmd(cmd, kwds) - if ret.has_key('error'): + if "error" in ret: raise Exception(ret['error']['desc']) return ret['return'] -- cgit v1.2.3-55-g7522 From 7b6b9dbb3c4cb7eac4c847d601735da9b1d150ce Mon Sep 17 00:00:00 2001 From: Lukáš Doktor Date: Fri, 18 Aug 2017 16:26:12 +0200 Subject: qmp.py: Avoid overriding a builtin object The "id" is a builtin method to get object's identity and should not be overridden. This might bring some issues in case someone was directly calling "cmd(..., id=id)" but I haven't found such usage on brief search for "cmd\(.*id=". Signed-off-by: Lukáš Doktor Reviewed-by: Eduardo Habkost Message-Id: <20170818142613.32394-10-ldoktor@redhat.com> Signed-off-by: Eduardo Habkost --- scripts/qmp/qmp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'scripts/qmp/qmp.py') diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index f2f5a9b296..ef12e8a1a0 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -177,19 +177,19 @@ class QEMUMonitorProtocol(object): print >>sys.stderr, "QMP:<<< %s" % resp return resp - def cmd(self, name, args=None, id=None): + def cmd(self, name, args=None, cmd_id=None): """ Build a QMP command and send it to the QMP Monitor. @param name: command name (string) @param args: command arguments (dict) - @param id: command id (dict, list, string or int) + @param cmd_id: command id (dict, list, string or int) """ qmp_cmd = {'execute': name} if args: qmp_cmd['arguments'] = args - if id: - qmp_cmd['id'] = id + if cmd_id: + qmp_cmd['id'] = cmd_id return self.cmd_obj(qmp_cmd) def command(self, cmd, **kwds): -- cgit v1.2.3-55-g7522