Skip to contentSkip to conversation

QMP: allow JSON dict arguments in qmp-shell

PATCH Applied Stefan Hajnoczi 7 participants 12 messages Jan 29, 2014 – Feb 12, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 QMP: allow JSON dict arguments in qmp-shell

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---
 scripts/qmp/qmp-shell | 3 +++
 1 file changed, 3 insertions(+)
scripts/qmp/qmp-shell +3 -0
index d6b420f..d374b35 100755
@@ -31,6 +31,7 @@
 # (QEMU)
 
 import qmp
+import json
 import readline
 import sys
 import pprint
@@ -107,6 +108,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = True
                 elif opt[1] == 'false':
                     value = False
+                elif opt[1].startswith('{'):
+                    value = json.loads(opt[1])
                 else:
                     value = opt[1]
             qmpcmd['arguments'][opt[0]] = value
Signature · 1 line
1.8.5.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Stefan Hajnoczi

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Quoted 7 lines

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---
 scripts/qmp/qmp-shell | 3 +++
 1 file changed, 3 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by Benoit Canet on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 44 lines · Stefan Hajnoczi

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---
 scripts/qmp/qmp-shell | 3 +++
 1 file changed, 3 insertions(+)
scripts/qmp/qmp-shell +3 -0
index d6b420f..d374b35 100755
@@ -31,6 +31,7 @@
 # (QEMU)
 
 import qmp
+import json
 import readline
 import sys
 import pprint
@@ -107,6 +108,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = True
                 elif opt[1] == 'false':
                     value = False
+                elif opt[1].startswith('{'):
+                    value = json.loads(opt[1])
                 else:
                     value = opt[1]
             qmpcmd['arguments'][opt[0]] = value
Signature · 1 line
1.8.5.3

This patch is very usefull.

#3 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · On Wed, 29 Jan 2014 12:17:31 +0100 Stefan Hajnoczi <[email protected]> wrote:

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

Quoted 6 lines

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

Applied to the qmp branch, thanks.

Quoted 25 lines
---
 scripts/qmp/qmp-shell | 3 +++
 1 file changed, 3 insertions(+)
scripts/qmp/qmp-shell +3 -0
index d6b420f..d374b35 100755
@@ -31,6 +31,7 @@
 # (QEMU)
 
 import qmp
+import json
 import readline
 import sys
 import pprint
@@ -107,6 +108,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = True
                 elif opt[1] == 'false':
                     value = False
+                elif opt[1].startswith('{'):
+                    value = json.loads(opt[1])
                 else:
                     value = opt[1]
             qmpcmd['arguments'][opt[0]] = value
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

Quoted 7 lines

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---

While this is definitely useful, just wondering if it is more friendly to allow passing options as command line options do:

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

Thanks,
Fam

Quoted 28 lines
 scripts/qmp/qmp-shell | 3 +++
 1 file changed, 3 insertions(+)
scripts/qmp/qmp-shell +3 -0
index d6b420f..d374b35 100755
@@ -31,6 +31,7 @@
 # (QEMU)
 
 import qmp
+import json
 import readline
 import sys
 import pprint
@@ -107,6 +108,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = True
                 elif opt[1] == 'false':
                     value = False
+                elif opt[1].startswith('{'):
+                    value = json.loads(opt[1])
                 else:
                     value = opt[1]
             qmpcmd['arguments'][opt[0]] = value
Signature · 1 line
1.8.5.3
#5 lore raw

Am 11.02.2014 um 02:39 hat Fam Zheng geschrieben:

Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

Quoted 7 lines
Quoted 2 lines

---

While this is definitely useful, just wondering if it is more friendly to allow passing options as command line options do:

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

In fact, I have been considering the brace syntax even for the command line, because the dot syntax gets a bit too verbose when you have a lot of nested dicts. The reason why I didn't do it is that it would introduce more special characters.

Quoted 1 line

From a usability POV, it might be best to have both.

Kevin

#6 lore raw
Quoted 22 lines · Kevin Wolf

Am 11.02.2014 um 02:39 hat Fam Zheng geschrieben:

Quoted 21 lines · Stefan Hajnoczi
Quoted 9 lines

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

Quoted 7 lines

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---

While this is definitely useful, just wondering if it is more friendly to allow passing options as command line options do:

Quoted 6 lines
Quoted 1 line

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

In fact, I have been considering the brace syntax even for the command line, because the dot syntax gets a bit too verbose when you have a lot of nested dicts. The reason why I didn't do it is that it would introduce more special characters.

Typing json syntax is harder than repeating the dict paths, so personally I still prefer dot syntax over braces, colons and double quotes.

Fam

#7 lore raw
In reply to Fam Zheng
Quoted 15 lines · Fam Zheng
Quoted 14 lines · Stefan Hajnoczi

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

Quoted 7 lines
Quoted 2 lines

---

While this is definitely useful, just wondering if it is more friendly to allow passing options as command line options do:

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

Yeah. The "spaces are not allowed" syntax is pretty horrid :)

#8 lore raw
Quoted 20 lines · Markus Armbruster
Quoted 18 lines · Fam Zheng
Quoted 16 lines · Stefan Hajnoczi

qmp-shell hides the QMP wire protocol JSON encoding from the user. Most of the time this is helpful and makes the command-line human-friendly.

Some QMP commands take a dict as an argument. In order to express this we need to revert back to JSON notation.

This patch allows JSON dict arguments in qmp-shell so commands like blockdev-add and nbd-server-start can be invoked:

(QEMU) blockdev-add options={"driver":"file","id":"drive1",...}

Note that spaces are not allowed since str.split() is used to break up the command-line arguments first.

---

Quoted 6 lines
Quoted 4 lines

While this is definitely useful, just wondering if it is more friendly to allow passing options as command line options do:

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

Yeah. The "spaces are not allowed" syntax is pretty horrid :)

It's not hard to loose the space restriction by making qmp-shell smarter to match "}" with "{", but the quote marks and colons are still troublesome.

Maybe I'll think differently when many levels of nesting becomes common. :)

Fam

#9 lore raw

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

In reply to Stefan Hajnoczi

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=...

--
Applies on top of Stefan's patch

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

---
 scripts/qmp/qmp-shell | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +8 -1
index d374b35..9c84551 100755
@@ -112,7 +112,14 @@ 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']
+            for p in optpath[:-1]:
+                if not p in parent:
+                    d = dict()
+                    parent[p] = d
+                parent = d
+            parent[optpath[-1]] = value
         return qmpcmd
 
     def _execute_cmd(self, cmdline):
Signature · 1 line
1.8.5.4
#10 lore raw

Re: [Qemu-devel] [PATCH] QMP: allow dot separated dict path arguments in qmp-shell

Quoted 6 lines · Fam Zheng
scripts/qmp/qmp-shell +0 -0
index d374b35..9c84551 100755
@@ -112,7 +112,14 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = json.loads(opt[1])

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 121

Quoted 10 lines
scripts/qmp/qmp-shell +7 -1
                 else:
                     value = opt[1]
-            qmpcmd['arguments'][opt[0]] = value
+            optpath = opt[0].split('.')
+            parent = qmpcmd['arguments']
+            for p in optpath[:-1]:
+                if not p in parent:
+                    d = dict()
+                    parent[p] = d
+                parent = d

d is a stale reference when the path component already exists (e.g. a.b.c=1 a.d=2). Since 'a' already exists when processing 'a.d' we'll the value of d will actually be the a.b dict!

I think you need the following instead:

for p in optpath[:-1]:
d = parent.get(p, {})
parent[p] = d
parent = d

#11 lore raw

Re: [Qemu-devel] [PATCH] QMP: allow dot separated dict path arguments in qmp-shell

Quoted 22 lines · Stefan Hajnoczi
Quoted 6 lines · Fam Zheng
scripts/qmp/qmp-shell +0 -0
index d374b35..9c84551 100755
@@ -112,7 +112,14 @@ class QMPShell(qmp.QEMUMonitorProtocol):
                     value = json.loads(opt[1])

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 121

Quoted 10 lines
scripts/qmp/qmp-shell +7 -1
                 else:
                     value = opt[1]
-            qmpcmd['arguments'][opt[0]] = value
+            optpath = opt[0].split('.')
+            parent = qmpcmd['arguments']
+            for p in optpath[:-1]:
+                if not p in parent:
+                    d = dict()
+                    parent[p] = d
+                parent = d

d is a stale reference when the path component already exists (e.g. a.b.c=1 a.d=2). Since 'a' already exists when processing 'a.d' we'll the value of d will actually be the a.b dict!

Quoted 6 lines

I think you need the following instead:

for p in optpath[:-1]:
d = parent.get(p, {})
parent[p] = d
parent = d

Yes, thanks. And we should check the contradictive case "foo=a foo.bar=b". Will send v2.

Fam