Skip to contentSkip to conversation

QMP: Allow dot separated dict path arguments in qmp-shell

PATCH Applied Fam Zheng 4 participants 5 messages Feb 12, 2014 – Feb 27, 2014

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH v3 1/1 QMP: Allow dot separated dict path arguments in qmp-shell

As another convinience to allow using commands that expect a dict as argument, this patch adds support for foo.bar=value syntax, similar to command line argument style:

(QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

---
v3: Fix error message wording. (Eric)
v2: Fix variable usage and improved error check and report.

Applies on top of Stefan's patch

[PATCH] QMP: allow JSON dict arguments in qmp-shell

---
 scripts/qmp/qmp-shell | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
scripts/qmp/qmp-shell +18 -2
index d374b35..e0e848b 100755
@@ -112,13 +112,29 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = json.loads(opt[1])
                 else:
                     value = opt[1]
-            qmpcmd['arguments'][opt[0]] = value
+            optpath = opt[0].split('.')
+            parent = qmpcmd['arguments']
+            curpath = []
+            for p in optpath[:-1]:
+                curpath.append(p)
+                d = parent.get(p, {})
+                if type(d) is not dict:
+                    raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
+                parent[p] = d
+                parent = d
+            if optpath[-1] in parent:
+                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])
+            parent[optpath[-1]] = value
         return qmpcmd
 
     def _execute_cmd(self, cmdline):
         try:
             qmpcmd = self.__build_cmd(cmdline)
-        except:
+        except Exception, e:
+            print 'Error while parsing command line: %s' % e
             print 'command format: <command-name> ',
             print '[arg-name1=arg1] ... [arg-nameN=argN]'
             return True
Signature · 1 line
1.8.5.4
Reviewed-by on v3 1/1
#1 lore raw
Quoted 1 line · Fam Zheng

As another convinience to allow using commands that expect a dict as

You missed this, even though I pointed it out in v2:

s/convinience/convenience/

Quoted 8 lines

argument, this patch adds support for foo.bar=value syntax, similar to command line argument style:

(QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

---

My python is weak, so take this with a grain of salt:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 6 lines · Eric Blake
Quoted 1 line · Fam Zheng

As another convinience to allow using commands that expect a dict as

You missed this, even though I pointed it out in v2:

s/convinience/convenience/

Oops, I'm sorry for that. Luiz, would you fix this when applying?

Thanks,
Fam

Quoted 17 lines
Quoted 9 lines

argument, this patch adds support for foo.bar=value syntax, similar to command line argument style:

(QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

---

My python is weak, so take this with a grain of salt:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v3 1/1
#3 lore raw
In reply to Fam Zheng
Quoted 12 lines · Fam Zheng

As another convinience to allow using commands that expect a dict as argument, this patch adds support for foo.bar=value syntax, similar to command line argument style:

(QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

---
v3: Fix error message wording. (Eric)
v2: Fix variable usage and improved error check and report.

Quoted 8 lines

Applies on top of Stefan's patch

[PATCH] QMP: allow JSON dict arguments in qmp-shell

---
 scripts/qmp/qmp-shell | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
#4 lore raw
In reply to Fam Zheng
Quoted 7 lines · On Wed, 12 Feb 2014 11:05:13 +0800 Fam Zheng <[email protected]> wrote:

As another convinience to allow using commands that expect a dict as argument, this patch adds support for foo.bar=value syntax, similar to command line argument style:

(QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

Applied to the qmp branch, thanks.

Quoted 50 lines

---
v3: Fix error message wording. (Eric)
v2: Fix variable usage and improved error check and report.

Applies on top of Stefan's patch

[PATCH] QMP: allow JSON dict arguments in qmp-shell

---
 scripts/qmp/qmp-shell | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
scripts/qmp/qmp-shell +18 -2
index d374b35..e0e848b 100755
@@ -112,13 +112,29 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = json.loads(opt[1])
                 else:
                     value = opt[1]
-            qmpcmd['arguments'][opt[0]] = value
+            optpath = opt[0].split('.')
+            parent = qmpcmd['arguments']
+            curpath = []
+            for p in optpath[:-1]:
+                curpath.append(p)
+                d = parent.get(p, {})
+                if type(d) is not dict:
+                    raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
+                parent[p] = d
+                parent = d
+            if optpath[-1] in parent:
+                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])
+            parent[optpath[-1]] = value
         return qmpcmd
 
     def _execute_cmd(self, cmdline):
         try:
             qmpcmd = self.__build_cmd(cmdline)
-        except:
+        except Exception, e:
+            print 'Error while parsing command line: %s' % e
             print 'command format: <command-name> ',
             print '[arg-name1=arg1] ... [arg-nameN=argN]'
             return True