qmp-shell: fix pretty printing of JSON responses

Pretty printing of JSON responses is important to be able to understand
large responses from query commands in particular. Unfortunately this
was broken during the addition of the verbose flag in

  commit 1ceca07e48
  Author: John Snow <jsnow@redhat.com>
  Date:   Wed Apr 29 15:14:04 2015 -0400

    scripts: qmp-shell: Add verbose flag

This is because that change turned the python data structure into a
formatted JSON string before the pretty print was given it. So we're
just pretty printing a string, which is a no-op.

The original pretty printer would output python objects.

(QEMU) query-chardev
{   u'return': [   {   u'filename': u'vc',
                       u'frontend-open': False,
                       u'label': u'parallel0'},
                   {   u'filename': u'vc',
                       u'frontend-open': True,
                       u'label': u'serial0'},
                   {   u'filename': u'unix:/tmp/qemp,server',
                       u'frontend-open': True,
                       u'label': u'compat_monitor0'}]}

This fixes the problem by switching to outputting pretty formatted JSON
text instead. This has the added benefit that the pretty printed output
is now valid JSON text. Due to the way the verbose flag was handled, the
pretty printing now applies to the command sent, as well as its response:

(QEMU) query-chardev
{
    "execute": "query-chardev",
    "arguments": {}
}
{
    "return": [
        {
            "frontend-open": false,
            "label": "parallel0",
            "filename": "vc"
        },
        {
            "frontend-open": true,
            "label": "serial0",
            "filename": "vc"
        },
        {
            "frontend-open": true,
            "label": "compat_monitor0",
            "filename": "unix:/tmp/qmp,server"
        }
    ]
}

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-Id: <1456224706-1591-1-git-send-email-berrange@redhat.com>
Tested-by: Kashyap Chamarthy <kchamart@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
[Bonus fix: multiple -p now work]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-02-23 10:51:46 +00:00 committed by Markus Armbruster
parent 3c0f12df65
commit e55250c6cb

View File

@ -70,7 +70,6 @@ import json
import ast import ast
import readline import readline
import sys import sys
import pprint
class QMPCompleter(list): class QMPCompleter(list):
def complete(self, text, state): def complete(self, text, state):
@ -103,11 +102,11 @@ class FuzzyJSON(ast.NodeTransformer):
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
# _execute_cmd()). Let's design a better one. # _execute_cmd()). Let's design a better one.
class QMPShell(qmp.QEMUMonitorProtocol): class QMPShell(qmp.QEMUMonitorProtocol):
def __init__(self, address, pp=None): def __init__(self, address, pretty=False):
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
self._greeting = None self._greeting = None
self._completer = None self._completer = None
self._pp = pp self._pretty = pretty
self._transmode = False self._transmode = False
self._actions = list() self._actions = list()
@ -231,11 +230,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
return qmpcmd return qmpcmd
def _print(self, qmp): def _print(self, qmp):
jsobj = json.dumps(qmp) indent = None
if self._pp is not None: if self._pretty:
self._pp.pprint(jsobj) indent = 4
else: jsobj = json.dumps(qmp, indent=indent)
print str(jsobj) print str(jsobj)
def _execute_cmd(self, cmdline): def _execute_cmd(self, cmdline):
try: try:
@ -377,7 +376,7 @@ def main():
addr = '' addr = ''
qemu = None qemu = None
hmp = False hmp = False
pp = None pretty = False
verbose = False verbose = False
try: try:
@ -387,9 +386,7 @@ def main():
fail_cmdline(arg) fail_cmdline(arg)
hmp = True hmp = True
elif arg == "-p": elif arg == "-p":
if pp is not None: pretty = True
fail_cmdline(arg)
pp = pprint.PrettyPrinter(indent=4)
elif arg == "-v": elif arg == "-v":
verbose = True verbose = True
else: else:
@ -398,7 +395,7 @@ def main():
if hmp: if hmp:
qemu = HMPShell(arg) qemu = HMPShell(arg)
else: else:
qemu = QMPShell(arg, pp) qemu = QMPShell(arg, pretty)
addr = arg addr = arg
if qemu is None: if qemu is None: