* [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell
@ 2012-08-15 10:33 Daniel P. Berrange
2012-08-15 11:20 ` Peter Maydell
2012-08-15 17:20 ` Luiz Capitulino
0 siblings, 2 replies; 3+ messages in thread
From: Daniel P. Berrange @ 2012-08-15 10:33 UTC (permalink / raw)
To: qemu-devel; +Cc: Luiz Capitulino
From: "Daniel P. Berrange" <berrange@redhat.com>
Add a '-p' arg to the QMP/qmp-shell test program, which uses
the python pprint module to pretty-print the dictionary
returned from a command
$ qmp-shell -p /tmp/qemu
Welcome to the QMP low-level shell!
Connected to QEMU 1.1.50
(QEMU) query-cpus
{ u'return': [ { u'CPU': 0,
u'current': True,
u'halted': True,
u'pc': 1048556,
u'thread_id': 7108}]}
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
QMP/qmp-shell | 46 +++++++++++++++++++++++++++++++++-------------
1 file changed, 33 insertions(+), 13 deletions(-)
diff --git a/QMP/qmp-shell b/QMP/qmp-shell
index 42dabc8..24b665c 100755
--- a/QMP/qmp-shell
+++ b/QMP/qmp-shell
@@ -33,6 +33,7 @@
import qmp
import readline
import sys
+import pprint
class QMPCompleter(list):
def complete(self, text, state):
@@ -52,10 +53,11 @@ class QMPShellBadPort(QMPShellError):
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
# _execute_cmd()). Let's design a better one.
class QMPShell(qmp.QEMUMonitorProtocol):
- def __init__(self, address):
+ def __init__(self, address, pp=None):
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
self._greeting = None
self._completer = None
+ self._pp = pp
def __get_address(self, arg):
"""
@@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
if resp is None:
print 'Disconnected'
return False
- print resp
+
+ if self._pp is not None:
+ self._pp.pprint(resp)
+ else:
+ print resp
return True
def connect(self):
@@ -222,22 +228,36 @@ def die(msg):
def fail_cmdline(option=None):
if option:
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
- sys.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n')
+ sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
sys.exit(1)
def main():
addr = ''
+ qemu = None
+ hmp = False
+ pp = None
+
try:
- if len(sys.argv) == 2:
- qemu = QMPShell(sys.argv[1])
- addr = sys.argv[1]
- elif len(sys.argv) == 3:
- if sys.argv[1] != '-H':
- fail_cmdline(sys.argv[1])
- qemu = HMPShell(sys.argv[2])
- addr = sys.argv[2]
- else:
- fail_cmdline()
+ for arg in sys.argv[1:]:
+ if arg == "-H":
+ if qemu is not None:
+ fail_cmdline(arg)
+ hmp = True
+ elif arg == "-p":
+ if pp is not None:
+ fail_cmdline(arg)
+ pp = pprint.PrettyPrinter(indent=4)
+ else:
+ if qemu is not None:
+ fail_cmdline(arg)
+ if hmp:
+ qemu = HMPShell(arg)
+ else:
+ qemu = QMPShell(arg, pp)
+ addr = arg
+
+ if qemu is None:
+ fail_cmdline()
except QMPShellBadPort:
die('bad port number in command-line')
--
1.7.11.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell
2012-08-15 10:33 [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell Daniel P. Berrange
@ 2012-08-15 11:20 ` Peter Maydell
2012-08-15 17:20 ` Luiz Capitulino
1 sibling, 0 replies; 3+ messages in thread
From: Peter Maydell @ 2012-08-15 11:20 UTC (permalink / raw)
To: Daniel P. Berrange; +Cc: qemu-devel, Luiz Capitulino
On 15 August 2012 11:33, Daniel P. Berrange <berrange@redhat.com> wrote:
> class QMPShell(qmp.QEMUMonitorProtocol):
> - def __init__(self, address):
> + def __init__(self, address, pp=None):
> qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
> self._greeting = None
> self._completer = None
> + self._pp = pp
>
> def __get_address(self, arg):
> """
> @@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
> if resp is None:
> print 'Disconnected'
> return False
> - print resp
> +
> + if self._pp is not None:
> + self._pp.pprint(resp)
> + else:
> + print resp
If not pretty printing, you could initialise self._pp to a dummy object
with a pprint method that just does "print", which would let you avoid
the conditional here and just always do "self._pp.pprint(resp)".
Dunno whether that's worth the effort -- probably depends whether
there are likely to be other places in the code in future that want
to pretty-print an object.
-- PMM
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell
2012-08-15 10:33 [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell Daniel P. Berrange
2012-08-15 11:20 ` Peter Maydell
@ 2012-08-15 17:20 ` Luiz Capitulino
1 sibling, 0 replies; 3+ messages in thread
From: Luiz Capitulino @ 2012-08-15 17:20 UTC (permalink / raw)
To: Daniel P. Berrange; +Cc: qemu-devel
On Wed, 15 Aug 2012 11:33:47 +0100
"Daniel P. Berrange" <berrange@redhat.com> wrote:
> From: "Daniel P. Berrange" <berrange@redhat.com>
>
> Add a '-p' arg to the QMP/qmp-shell test program, which uses
> the python pprint module to pretty-print the dictionary
> returned from a command
>
> $ qmp-shell -p /tmp/qemu
> Welcome to the QMP low-level shell!
> Connected to QEMU 1.1.50
>
> (QEMU) query-cpus
> { u'return': [ { u'CPU': 0,
> u'current': True,
> u'halted': True,
> u'pc': 1048556,
> u'thread_id': 7108}]}
>
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Applied to the qmp branch, thanks.
> ---
> QMP/qmp-shell | 46 +++++++++++++++++++++++++++++++++-------------
> 1 file changed, 33 insertions(+), 13 deletions(-)
>
> diff --git a/QMP/qmp-shell b/QMP/qmp-shell
> index 42dabc8..24b665c 100755
> --- a/QMP/qmp-shell
> +++ b/QMP/qmp-shell
> @@ -33,6 +33,7 @@
> import qmp
> import readline
> import sys
> +import pprint
>
> class QMPCompleter(list):
> def complete(self, text, state):
> @@ -52,10 +53,11 @@ class QMPShellBadPort(QMPShellError):
> # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
> # _execute_cmd()). Let's design a better one.
> class QMPShell(qmp.QEMUMonitorProtocol):
> - def __init__(self, address):
> + def __init__(self, address, pp=None):
> qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
> self._greeting = None
> self._completer = None
> + self._pp = pp
>
> def __get_address(self, arg):
> """
> @@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
> if resp is None:
> print 'Disconnected'
> return False
> - print resp
> +
> + if self._pp is not None:
> + self._pp.pprint(resp)
> + else:
> + print resp
> return True
>
> def connect(self):
> @@ -222,22 +228,36 @@ def die(msg):
> def fail_cmdline(option=None):
> if option:
> sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
> - sys.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n')
> + sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
> sys.exit(1)
>
> def main():
> addr = ''
> + qemu = None
> + hmp = False
> + pp = None
> +
> try:
> - if len(sys.argv) == 2:
> - qemu = QMPShell(sys.argv[1])
> - addr = sys.argv[1]
> - elif len(sys.argv) == 3:
> - if sys.argv[1] != '-H':
> - fail_cmdline(sys.argv[1])
> - qemu = HMPShell(sys.argv[2])
> - addr = sys.argv[2]
> - else:
> - fail_cmdline()
> + for arg in sys.argv[1:]:
> + if arg == "-H":
> + if qemu is not None:
> + fail_cmdline(arg)
> + hmp = True
> + elif arg == "-p":
> + if pp is not None:
> + fail_cmdline(arg)
> + pp = pprint.PrettyPrinter(indent=4)
> + else:
> + if qemu is not None:
> + fail_cmdline(arg)
> + if hmp:
> + qemu = HMPShell(arg)
> + else:
> + qemu = QMPShell(arg, pp)
> + addr = arg
> +
> + if qemu is None:
> + fail_cmdline()
> except QMPShellBadPort:
> die('bad port number in command-line')
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-08-15 17:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-15 10:33 [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell Daniel P. Berrange
2012-08-15 11:20 ` Peter Maydell
2012-08-15 17:20 ` Luiz Capitulino
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).