From 9d47f6de10e36c988e935069bf288d39bdcc1126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Fri, 18 Aug 2017 16:26:09 +0200 Subject: [PATCH] qmp.py: Couple of pylint/style fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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(-) 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 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 @@ def __init__(self, address, server=False, debug=False): 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 @@ def __get_sock(self): 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 @@ def __json_read(self, only_event=False): 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 @@ def __get_events(self, wait=False): @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 @@ def cmd(self, name, args=None, id=None): @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 @@ def cmd(self, name, args=None, id=None): 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 @@ def command(self, cmd, **kwds): 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 @@ def get_events(self, wait=False): @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 @@ def close(self): self.__sock.close() self.__sockfile.close() - timeout = socket.timeout - def settimeout(self, timeout): self.__sock.settimeout(timeout)