qmp-shell: fix pretty printing of JSON responses
PATCH v2 1/1 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 1ceca07e48ead0dd2e41576c81d40e6a91cafefd
Author: John Snow <[email protected]>
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"
}
]
}--- scripts/qmp/qmp-shell | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
scripts/qmp/qmp-shell +10 -13
@@ -70,7 +70,6 @@ import json import ast import readline import sys -import pprint class QMPCompleter(list): 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 # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): - def __init__(self, address, pp=None): + def __init__(self, address, pretty=False): qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) self._greeting = None self._completer = None - self._pp = pp + self._pretty = pretty self._transmode = False self._actions = list()
@@ -231,11 +230,11 @@ class QMPShell(qmp.QEMUMonitorProtocol): return qmpcmd def _print(self, qmp): - jsobj = json.dumps(qmp) - if self._pp is not None: - self._pp.pprint(jsobj) - else: - print str(jsobj) + indent = None + if self._pretty: + indent = 4 + jsobj = json.dumps(qmp, indent=indent) + print str(jsobj) def _execute_cmd(self, cmdline): try:
@@ -377,7 +376,7 @@ def main(): addr = '' qemu = None hmp = False - pp = None + pretty = False verbose = False try:
@@ -387,9 +386,7 @@ def main(): fail_cmdline(arg) hmp = True elif arg == "-p": - if pp is not None: - fail_cmdline(arg) - pp = pprint.PrettyPrinter(indent=4) + pretty = True elif arg == "-v": verbose = True else:
@@ -398,7 +395,7 @@ def main(): if hmp: qemu = HMPShell(arg) else: - qemu = QMPShell(arg, pp) + qemu = QMPShell(arg, pretty) addr = arg if qemu is None:
Signature · 1 line
2.5.0
Quoted 18 lines · Daniel P. Berrange
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
1ceca07e48ead0dd2e41576c81d40e6a91cafefd
Author: John Snow <[email protected]>
Date: Wed Apr 29 15:14:04 2015 -0400scripts: 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',
Quoted 8 lines
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'}]}For me, without your patch (I was testing from 'v2.5.0-1307-g80b5d6b') the output doesn't even look like the above -- when qmp-shell -p /path/to/socket is invoked, and I type in any of the query commands, what I see is long unterminated JSON output.
Quoted 21 lines
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"
Quoted 8 lines
},
{
"frontend-open": true,
"label": "compat_monitor0",
"filename": "unix:/tmp/qmp,server"
}
]
}Works nice here. I ran for a bunch fo query-commands:
$ git describe pull-io-next-2016-02-16-1-221-ga043f9c
[Fire up QEMU with '-qmp unix:./qmp-sock,server'
$ ./qmp-shell -p /var/tmp/qmp-sock
[...]
(QEMU) query-named-block-nodes
{
"return": [
{
"bps_rd": 0,
"ro": false,
"backing_file_depth": 1,
"encrypted": false,
"image": {
"cluster-size": 65536,
"backing-image": {
"cluster-size": 65536,
"format": "qcow2",
"filename": "/export/tests//cirros-0.3.3.img",
"virtual-size": 41126400,
"dirty-flag": false,
"format-specific": {
"data": {
"compat": "0.10",
"refcount-bits": 16
},
"type": "qcow2"
},
"actual-size": 14446592
},
"format": "qcow2",
"full-backing-filename": "/export/tests//cirros-0.3.3.img",
"backing-filename-format": "qcow2",
"filename": "./ext-snap1.qcow2",
"dirty-flag": false,
"virtual-size": 41126400,
"backing-filename": "/export/tests//cirros-0.3.3.img",
"format-specific": {
"data": {
"compat": "1.1",
"refcount-bits": 16,
"corrupt": false,
"lazy-refcounts": false
},
"type": "qcow2"
},
"actual-size": 200704
},
[...]Quoted 4 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- scripts/qmp/qmp-shell | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
FWIW:
[...]
Signature · 1 line
/kashyap
Quoted 8 lines · Daniel P. Berrange
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
1ceca07e48ead0dd2e41576c81d40e6a91cafefd
Author: John Snow <[email protected]>
Date: Wed Apr 29 15:14:04 2015 -0400
Quoted 6 lines
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.
Mea Culpa.
Quoted 34 lines
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"
Quoted 9 lines
},
{
"frontend-open": true,
"label": "compat_monitor0",
"filename": "unix:/tmp/qmp,server"
}
]
}That's good news as far as I am concerned.
Quoted 18 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- scripts/qmp/qmp-shell | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)scripts/qmp/qmp-shell +0
-1@@ -70,7 +70,6 @@ import json import ast import readline import sys -import pprint class QMPCompleter(list): def complete(self, text, state):
scripts/qmp/qmp-shell class FuzzyJSON(ast.NodeTransformer): · line 103
Quoted 2 lines
scripts/qmp/qmp-shell +0
-0@@ -103,11 +102,11 @@ class FuzzyJSON(ast.NodeTransformer): # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # _execute_cmd()). Let's design a better one.
^ Heh ^
Quoted 38 lines
scripts/qmp/qmp-shell +8
-8class QMPShell(qmp.QEMUMonitorProtocol): - def __init__(self, address, pp=None): + def __init__(self, address, pretty=False): qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) self._greeting = None self._completer = None - self._pp = pp + self._pretty = pretty self._transmode = False self._actions = list()@@ -231,11 +230,11 @@ class QMPShell(qmp.QEMUMonitorProtocol): return qmpcmd def _print(self, qmp): - jsobj = json.dumps(qmp) - if self._pp is not None: - self._pp.pprint(jsobj) - else: - print str(jsobj) + indent = None + if self._pretty: + indent = 4 + jsobj = json.dumps(qmp, indent=indent) + print str(jsobj) def _execute_cmd(self, cmdline): try:@@ -377,7 +376,7 @@ def main(): addr = '' qemu = None hmp = False - pp = None + pretty = False verbose = False try:
scripts/qmp/qmp-shell def main(): · line 389
Quoted 7 lines
scripts/qmp/qmp-shell +1
-3@@ -387,9 +386,7 @@ def main(): fail_cmdline(arg) hmp = True elif arg == "-p": - if pp is not None: - fail_cmdline(arg) - pp = pprint.PrettyPrinter(indent=4) + pretty = True
We now accept any number of -p arguments. That's probably fine.
scripts/qmp/qmp-shell def main(): · line 401
Quoted 13 lines
scripts/qmp/qmp-shell +1
-1elif arg == "-v": verbose = True else:@@ -398,7 +395,7 @@ def main(): if hmp: qemu = HMPShell(arg) else: - qemu = QMPShell(arg, pp) + qemu = QMPShell(arg, pretty) addr = arg if qemu is None:
Markus Armbruster
AppliedQuoted 123 lines · John Snow
Quoted 8 lines · Daniel P. Berrange
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
1ceca07e48ead0dd2e41576c81d40e6a91cafefd
Author: John Snow <[email protected]>
Date: Wed Apr 29 15:14:04 2015 -0400Quoted 6 lines
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.
Mea Culpa.
Quoted 34 lines
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"Quoted 9 lines
}, { "frontend-open": true, "label": "compat_monitor0", "filename": "unix:/tmp/qmp,server" } ] }That's good news as far as I am concerned.
Quoted 18 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- scripts/qmp/qmp-shell | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)scripts/qmp/qmp-shell +0
-1@@ -70,7 +70,6 @@ import json import ast import readline import sys -import pprint class QMPCompleter(list): def complete(self, text, state):scripts/qmp/qmp-shell class FuzzyJSON(ast.NodeTransformer): · line 103
Quoted 2 lines
scripts/qmp/qmp-shell +0
-0@@ -103,11 +102,11 @@ class FuzzyJSON(ast.NodeTransformer): # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # _execute_cmd()). Let's design a better one.^ Heh ^
Quoted 36 lines
scripts/qmp/qmp-shell +8
-8class QMPShell(qmp.QEMUMonitorProtocol): - def __init__(self, address, pp=None): + def __init__(self, address, pretty=False): qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) self._greeting = None self._completer = None - self._pp = pp + self._pretty = pretty self._transmode = False self._actions = list()@@ -231,11 +230,11 @@ class QMPShell(qmp.QEMUMonitorProtocol): return qmpcmd def _print(self, qmp): - jsobj = json.dumps(qmp) - if self._pp is not None: - self._pp.pprint(jsobj) - else: - print str(jsobj) + indent = None + if self._pretty: + indent = 4 + jsobj = json.dumps(qmp, indent=indent) + print str(jsobj) def _execute_cmd(self, cmdline): try:@@ -377,7 +376,7 @@ def main(): addr = '' qemu = None hmp = False - pp = None + pretty = False verbose = False
Quoted 11 lines
scripts/qmp/qmp-shell def main(): · line 389
Quoted 9 lines
scripts/qmp/qmp-shell +1
-3try:@@ -387,9 +386,7 @@ def main(): fail_cmdline(arg) hmp = True elif arg == "-p": - if pp is not None: - fail_cmdline(arg) - pp = pprint.PrettyPrinter(indent=4) + pretty = TrueWe now accept any number of -p arguments. That's probably fine.
I'd call that a bug fix.
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +1
-1elif arg == "-v": verbose = True else:@@ -398,7 +395,7 @@ def main(): if hmp: qemu = HMPShell(arg) else: - qemu = QMPShell(arg, pp) + qemu = QMPShell(arg, pretty) addr = arg
Quoted 4 lines
scripts/qmp/qmp-shell def main(): · line 401
Quoted 2 lines
scripts/qmp/qmp-shell +0
-0if qemu is None:Reviewed-by John Snow <[email protected]>
Applied to my qapi-next branch, thanks!