From 6092c3ecc4bdafee5bf07061be78a4a2cc5a5088 Mon Sep 17 00:00:00 2001 From: John Snow Date: Wed, 29 Apr 2015 15:14:02 -0400 Subject: scripts: qmp-shell: Expand support for QMP expressions This includes support for [] expressions, single-quotes in QMP expressions (which is not strictly a part of JSON), and the ability to use "True", "False" and "None" literals instead of JSON's equivalent true, false, and null literals. qmp-shell currently allows you to describe values as JSON expressions: key={"key":{"key2":"val"}} But it does not currently support arrays, which are needed for serializing and deserializing transactions: key=[{"type":"drive-backup","data":{...}}] qmp-shell also only currently accepts doubly quoted strings as-per JSON spec, but QMP allows single quotes. Lastly, python allows you to utilize "True" or "False" as boolean literals, but JSON expects "true" or "false". Expand qmp-shell to allow the user to type either, converting to the correct type. As a consequence of the above, the key=val parsing is also improved to give better error messages if a key=val token is not provided. CAVEAT: The parser is still extremely rudimentary and does not expect to find spaces in {} nor [] expressions. This patch does not improve this functionality. Signed-off-by: John Snow Reviewed-by: Eric Blake Tested-by: Kashyap Chamarthy Signed-off-by: Luiz Capitulino --- scripts/qmp/qmp-shell | 63 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 16 deletions(-) (limited to 'scripts/qmp') diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index a9632ecdd8..7f2c554b47 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -32,6 +32,7 @@ import qmp import json +import ast import readline import sys import pprint @@ -51,6 +52,19 @@ class QMPShellError(Exception): class QMPShellBadPort(QMPShellError): pass +class FuzzyJSON(ast.NodeTransformer): + '''This extension of ast.NodeTransformer filters literal "true/false/null" + values in an AST and replaces them by proper "True/False/None" values that + Python can properly evaluate.''' + def visit_Name(self, node): + if node.id == 'true': + node.id = 'True' + if node.id == 'false': + node.id = 'False' + if node.id == 'null': + node.id = 'None' + return node + # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): @@ -88,23 +102,40 @@ class QMPShell(qmp.QEMUMonitorProtocol): # clearing everything as it doesn't seem to matter readline.set_completer_delims('') - def __cli_expr(self, tokens, parent): - for arg in tokens: - opt = arg.split('=') + def __parse_value(self, val): + try: + return int(val) + except ValueError: + pass + + if val.lower() == 'true': + return True + if val.lower() == 'false': + return False + if val.startswith(('{', '[')): + # Try first as pure JSON: try: - if(len(opt) > 2): - opt[1] = '='.join(opt[1:]) - value = int(opt[1]) + return json.loads(val) except ValueError: - if opt[1] == 'true': - value = True - elif opt[1] == 'false': - value = False - elif opt[1].startswith('{'): - value = json.loads(opt[1]) - else: - value = opt[1] - optpath = opt[0].split('.') + pass + # Try once again as FuzzyJSON: + try: + st = ast.parse(val, mode='eval') + return ast.literal_eval(FuzzyJSON().visit(st)) + except SyntaxError: + pass + except ValueError: + pass + return val + + def __cli_expr(self, tokens, parent): + for arg in tokens: + (key, _, val) = arg.partition('=') + if not val: + raise QMPShellError("Expected a key=value pair, got '%s'" % arg) + + value = self.__parse_value(val) + optpath = key.split('.') curpath = [] for p in optpath[:-1]: curpath.append(p) @@ -117,7 +148,7 @@ class QMPShell(qmp.QEMUMonitorProtocol): if type(parent[optpath[-1]]) is dict: raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath)) else: - raise QMPShellError('Cannot set "%s" multiple times' % opt[0]) + raise QMPShellError('Cannot set "%s" multiple times' % key) parent[optpath[-1]] = value def __build_cmd(self, cmdline): -- cgit v1.2.1