Hack qmp.py to support reading a JSON multi-line response
PATCH 1/1 Hack qmp.py to support reading a JSON multi-line response
From: "Daniel P. Berrange" <[email protected]>
The qmp-shell code assumes the JSON response is only on a single line. If the QEMU monitor is configured in "pretty print" mode the JSON response can be multi-line. The basic Python JSON APIs do not appear to support a streaming mode, so the simple hack here is to try parsing a line, and if it fails, then read another line, append it, and try parsing again. Keep reading lines until we can successfully parse
--- QMP/qmp.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
QMP/qmp.py +12 -4
@@ -61,10 +61,18 @@ class QEMUMonitorProtocol: def __json_read(self, only_event=False): while True: - data = self.__sockfile.readline() - if not data: - return - resp = json.loads(data) + data = "" + while True: + moredata = self.__sockfile.readline() + if not moredata: + return + data = data + moredata + try: + resp = json.loads(data) + break + except ValueError: + pass + if 'event' in resp: self.__events.append(resp) if not only_event:
Signature · 1 line
1.7.11.2
Quoted 29 lines · On Mon, 20 Aug 2012 15:01:37 +0100 "Daniel P. Berrange" <[email protected]>…
From: "Daniel P. Berrange" <[email protected]>
The qmp-shell code assumes the JSON response is only on a single line. If the QEMU monitor is configured in "pretty print" mode the JSON response can be multi-line. The basic Python JSON APIs do not appear to support a streaming mode, so the simple hack here is to try parsing a line, and if it fails, then read another line, append it, and try parsing again. Keep reading lines until we can successfully parse
Signed-off-by Daniel P. Berrange <[email protected]>--- QMP/qmp.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)QMP/qmp.py +2
-4@@ -61,10 +61,18 @@ class QEMUMonitorProtocol: def __json_read(self, only_event=False): while True: - data = self.__sockfile.readline() - if not data: - return - resp = json.loads(data) + data = "" + while True:
QMP/qmp.py class QEMUMonitorProtocol: · line 75
Quoted 10 lines
QMP/qmp.py +10
-0+ moredata = self.__sockfile.readline() + if not moredata: + return + data = data + moredata + try: + resp = json.loads(data) + break + except ValueError: + pass +
I'm reluctant about this, because it makes it impossible to detect bad json from qemu (not that qmp-shell handles this gracefully today).
At the same time I can't think of anything simpler than your hack. What about calling json.loads() when closing } matches the number of opening ones? QMP responses are always dictionaries.
Quoted 3 lines
QMP/qmp.py +0
-0if 'event' in resp: self.__events.append(resp) if not only_event:
Quoted 37 lines · Luiz Capitulino
Quoted 34 lines · On Mon, 20 Aug 2012 15:01:37 +0100 "Daniel P. Berrange" <[email protected]>…
From: "Daniel P. Berrange" <[email protected]>
The qmp-shell code assumes the JSON response is only on a single line. If the QEMU monitor is configured in "pretty print" mode the JSON response can be multi-line. The basic Python JSON APIs do not appear to support a streaming mode, so the simple hack here is to try parsing a line, and if it fails, then read another line, append it, and try parsing again. Keep reading lines until we can successfully parse
Signed-off-by Daniel P. Berrange <[email protected]>--- QMP/qmp.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)QMP/qmp.py +7
-4@@ -61,10 +61,18 @@ class QEMUMonitorProtocol: def __json_read(self, only_event=False): while True: - data = self.__sockfile.readline() - if not data: - return - resp = json.loads(data) + data = "" + while True: + moredata = self.__sockfile.readline() + if not moredata: + return + data = data + moredata + try:
Quoted 8 lines
QMP/qmp.py class QEMUMonitorProtocol: · line 75
Quoted 5 lines
QMP/qmp.py +5
-0+ resp = json.loads(data) + break + except ValueError: + pass +I'm reluctant about this, because it makes it impossible to detect bad json from qemu (not that qmp-shell handles this gracefully today).
Yeah, it is slightly unpleasant.
Quoted 3 lines
At the same time I can't think of anything simpler than your hack. What about calling
json.loads()when closing } matches the number of opening ones? QMP responses are always dictionaries.
How about making multi-line processing an optional feature, by adding a '-m' flag to qmp-shell. That way we still get error checking by default
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 49 lines · On Mon, 20 Aug 2012 19:25:02 +0100 "Daniel P. Berrange" <[email protected]>…
Quoted 37 lines · Luiz Capitulino
Quoted 34 lines · On Mon, 20 Aug 2012 15:01:37 +0100 "Daniel P. Berrange" <[email protected]>…
From: "Daniel P. Berrange" <[email protected]>
The qmp-shell code assumes the JSON response is only on a single line. If the QEMU monitor is configured in "pretty print" mode the JSON response can be multi-line. The basic Python JSON APIs do not appear to support a streaming mode, so the simple hack here is to try parsing a line, and if it fails, then read another line, append it, and try parsing again. Keep reading lines until we can successfully parse
Signed-off-by Daniel P. Berrange <[email protected]>--- QMP/qmp.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)QMP/qmp.py +7
-4@@ -61,10 +61,18 @@ class QEMUMonitorProtocol: def __json_read(self, only_event=False): while True: - data = self.__sockfile.readline() - if not data: - return - resp = json.loads(data) + data = "" + while True: + moredata = self.__sockfile.readline() + if not moredata: + return + data = data + moredata + try:Quoted 8 lines
QMP/qmp.py class QEMUMonitorProtocol: · line 75
Quoted 5 lines
QMP/qmp.py +5
-0+ resp = json.loads(data) + break + except ValueError: + pass +I'm reluctant about this, because it makes it impossible to detect bad json from qemu (not that qmp-shell handles this gracefully today).
Yeah, it is slightly unpleasant.
Quoted 7 lines
Quoted 3 lines
At the same time I can't think of anything simpler than your hack. What about calling
json.loads()when closing } matches the number of opening ones? QMP responses are always dictionaries.How about making multi-line processing an optional feature, by adding a '-m' flag to qmp-shell. That way we still get error checking by default
Good idea.
Quoted 3 lines
Daniel