1 # QEMU Monitor Protocol Python class
3 # Copyright (C) 2009 Red Hat Inc.
6 # Luiz Capitulino <lcapitulino@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
13 class QMPError(Exception):
16 class QMPConnectError(QMPError
):
19 class QEMUMonitorProtocol
:
21 self
.sock
.connect(self
.filename
)
22 data
= self
.__json
_read
()
25 if not data
.has_key('QMP'):
27 return data
['QMP']['capabilities']
32 def send_raw(self
, line
):
33 self
.sock
.send(str(line
))
34 return self
.__json
_read
()
36 def send(self
, cmdline
):
37 cmd
= self
.__build
_cmd
(cmdline
)
39 resp
= self
.__json
_read
()
42 elif resp
.has_key('error'):
47 def __build_cmd(self
, cmdline
):
48 cmdargs
= cmdline
.split()
49 qmpcmd
= { 'execute': cmdargs
[0], 'arguments': {} }
50 for arg
in cmdargs
[1:]:
56 qmpcmd
['arguments'][opt
[0]] = value
59 def __json_send(self
, cmd
):
60 # XXX: We have to send any additional char, otherwise
61 # the Server won't read our input
62 self
.sock
.send(json
.dumps(cmd
) + ' ')
64 def __json_read(self
):
67 line
= json
.loads(self
.sockfile
.readline())
68 if not 'event' in line
:
73 def __init__(self
, filename
):
74 self
.filename
= filename
75 self
.sock
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
76 self
.sockfile
= self
.sock
.makefile()