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 <ldoktor@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20170818142613.32394-7-ldoktor@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
This commit is contained in:
Lukáš Doktor 2017-08-18 16:26:09 +02:00 committed by Eduardo Habkost
parent a004e249f0
commit 9d47f6de10

View file

@ -13,19 +13,30 @@ import errno
import socket import socket
import sys import sys
class QMPError(Exception): class QMPError(Exception):
pass pass
class QMPConnectError(QMPError): class QMPConnectError(QMPError):
pass pass
class QMPCapabilitiesError(QMPError): class QMPCapabilitiesError(QMPError):
pass pass
class QMPTimeoutError(QMPError): class QMPTimeoutError(QMPError):
pass pass
class QEMUMonitorProtocol: class QEMUMonitorProtocol:
#: Socket's error class
error = socket.error
#: Socket's timeout
timeout = socket.timeout
def __init__(self, address, server=False, debug=False): def __init__(self, address, server=False, debug=False):
""" """
Create a QEMUMonitorProtocol class. Create a QEMUMonitorProtocol class.
@ -42,6 +53,7 @@ class QEMUMonitorProtocol:
self.__address = address self.__address = address
self._debug = debug self._debug = debug
self.__sock = self.__get_sock() self.__sock = self.__get_sock()
self.__sockfile = None
if server: if server:
self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.__sock.bind(self.__address) self.__sock.bind(self.__address)
@ -56,7 +68,7 @@ class QEMUMonitorProtocol:
def __negotiate_capabilities(self): def __negotiate_capabilities(self):
greeting = self.__json_read() 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 raise QMPConnectError
# Greeting seems ok, negotiate capabilities # Greeting seems ok, negotiate capabilities
resp = self.cmd('qmp_capabilities') resp = self.cmd('qmp_capabilities')
@ -78,8 +90,6 @@ class QEMUMonitorProtocol:
continue continue
return resp return resp
error = socket.error
def __get_events(self, wait=False): def __get_events(self, wait=False):
""" """
Check for new events in the stream and cache them in __events. 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 @raise QMPTimeoutError: If a timeout float is provided and the timeout
period elapses. period elapses.
@raise QMPConnectError: If wait is True but no events could be retrieved @raise QMPConnectError: If wait is True but no events could be
or if some other error occurred. retrieved or if some other error occurred.
""" """
# Check for new events regardless and pull them into the cache: # Check for new events regardless and pull them into the cache:
@ -183,6 +193,9 @@ class QEMUMonitorProtocol:
return self.cmd_obj(qmp_cmd) return self.cmd_obj(qmp_cmd)
def command(self, cmd, **kwds): def command(self, cmd, **kwds):
"""
Build and send a QMP command to the monitor, report errors if any
"""
ret = self.cmd(cmd, kwds) ret = self.cmd(cmd, kwds)
if ret.has_key('error'): if ret.has_key('error'):
raise Exception(ret['error']['desc']) raise Exception(ret['error']['desc'])
@ -190,15 +203,15 @@ class QEMUMonitorProtocol:
def pull_event(self, wait=False): 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 (bool): block until an event is available.
@param wait (float): If wait is a float, treat it as a timeout value. @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 @raise QMPTimeoutError: If a timeout float is provided and the timeout
period elapses. period elapses.
@raise QMPConnectError: If wait is True but no events could be retrieved @raise QMPConnectError: If wait is True but no events could be
or if some other error occurred. retrieved or if some other error occurred.
@return The first available QMP event, or None. @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 @raise QMPTimeoutError: If a timeout float is provided and the timeout
period elapses. period elapses.
@raise QMPConnectError: If wait is True but no events could be retrieved @raise QMPConnectError: If wait is True but no events could be
or if some other error occurred. retrieved or if some other error occurred.
@return The list of available QMP events. @return The list of available QMP events.
""" """
@ -235,8 +248,6 @@ class QEMUMonitorProtocol:
self.__sock.close() self.__sock.close()
self.__sockfile.close() self.__sockfile.close()
timeout = socket.timeout
def settimeout(self, timeout): def settimeout(self, timeout):
self.__sock.settimeout(timeout) self.__sock.settimeout(timeout)