Skip to contentSkip to conversation

qapi: Top-level type reference for command definitions

PATCH Superseded Kevin Wolf 1 participant 4 messages Jun 21, 2013

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

v2 cover qapi: Top-level type reference for command definitions

v2:
- Updated documentation in patch 3

Kevin Wolf (3):
qapi.py: Move common code to evaluate()
qapi.py: Allow top-level type reference for command definitions
qapi-schema: Use BlockdevSnapshot type for blockdev-snapshot-sync

 qapi-schema.json | 14 ++------------
 scripts/qapi.py  | 43 +++++++++++++++++++++++++++++--------------
 2 files changed, 31 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.1.4
author
#1 lore raw

PATCH v2 1/3 qapi.py: Move common code to evaluate()

Don't duplicate more code than is really necessary.

---
 scripts/qapi.py | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
scripts/qapi.py +10 -14
index 02ad668..3a64769 100644
@@ -76,12 +76,18 @@ def parse(tokens):
         return tokens[0], tokens[1:]
 
 def evaluate(string):
-    return parse(map(lambda x: x, tokenize(string)))[0]
+    expr_eval = parse(map(lambda x: x, tokenize(string)))[0]
+
+    if expr_eval.has_key('enum'):
+        add_enum(expr_eval['enum'])
+    elif expr_eval.has_key('union'):
+        add_enum('%sKind' % expr_eval['union'])
+
+    return expr_eval
 
 def parse_schema(fp):
     exprs = []
     expr = ''
-    expr_eval = None
 
     for line in fp:
         if line.startswith('#') or line == '\n':
@@ -90,23 +96,13 @@ def parse_schema(fp):
         if line.startswith(' '):
             expr += line
         elif expr:
-            expr_eval = evaluate(expr)
-            if expr_eval.has_key('enum'):
-                add_enum(expr_eval['enum'])
-            elif expr_eval.has_key('union'):
-                add_enum('%sKind' % expr_eval['union'])
-            exprs.append(expr_eval)
+            exprs.append(evaluate(expr))
             expr = line
         else:
             expr += line
 
     if expr:
-        expr_eval = evaluate(expr)
-        if expr_eval.has_key('enum'):
-            add_enum(expr_eval['enum'])
-        elif expr_eval.has_key('union'):
-            add_enum('%sKind' % expr_eval['union'])
-        exprs.append(expr_eval)
+        exprs.append(evaluate(expr))
 
     return exprs
 
Signature · 1 line
1.8.1.4
author
#2 lore raw

PATCH v2 2/3 qapi.py: Allow top-level type reference for command definitions

In reply to Kevin Wolf

If 'data' for a command definition isn't a dict, but a string, it is taken as a (struct) type name and the fields of this struct are directly used as parameters.

This is useful for transactionable commands that can use the same type definition for both the transaction action and the arguments of the standalone command.

---
 scripts/qapi.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qapi.py +19 -0
index 3a64769..e151659 100644
@@ -82,6 +82,8 @@ def evaluate(string):
         add_enum(expr_eval['enum'])
     elif expr_eval.has_key('union'):
         add_enum('%sKind' % expr_eval['union'])
+    elif expr_eval.has_key('type'):
+        add_struct(expr_eval)
 
     return expr_eval
 
@@ -107,6 +109,11 @@ def parse_schema(fp):
     return exprs
 
 def parse_args(typeinfo):
+    if isinstance(typeinfo, basestring):
+        struct = find_struct(typeinfo)
+        assert struct != None
+        typeinfo = struct['data']
+
     for member in typeinfo:
         argname = member
         argentry = typeinfo[member]
@@ -176,6 +183,18 @@ def type_name(name):
     return name
 
 enum_types = []
+struct_types = []
+
+def add_struct(definition):
+    global struct_types
+    struct_types.append(definition)
+
+def find_struct(name):
+    global struct_types
+    for struct in struct_types:
+        if struct['type'] == name:
+            return struct
+    return None
 
 def add_enum(name):
     global enum_types
Signature · 1 line
1.8.1.4
author
#3 lore raw

PATCH v2 3/3 qapi-schema: Use BlockdevSnapshot type for blockdev-snapshot-sync

In reply to Kevin Wolf

We don't have to duplicate the definition any more now that we may refer to a type instead.

---
 qapi-schema.json | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
qapi-schema.json +2 -12
index a80ee40..54fbeef 100644
@@ -1652,16 +1652,7 @@
 #
 # Generates a synchronous snapshot of a block device.
 #
-# @device:  the name of the device to generate the snapshot from.
-#
-# @snapshot-file: the target of the new image. If the file exists, or if it
-#                 is a device, the snapshot will be created in the existing
-#                 file/device. If does not exist, a new file will be created.
-#
-# @format: #optional the format of the snapshot image, default is 'qcow2'.
-#
-# @mode: #optional whether and how QEMU should create a new image, default is
-#        'absolute-paths'.
+# For the arguments, see the documentation of BlockdevSnapshot.
 #
 # Returns: nothing on success
 #          If @device is not a valid block device, DeviceNotFound
@@ -1669,8 +1660,7 @@
 # Since 0.14.0
 ##
 { 'command': 'blockdev-snapshot-sync',
-  'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str',
-            '*mode': 'NewImageMode'} }
+  'data': 'BlockdevSnapshot' }
 
 ##
 # @human-monitor-command:
Signature · 1 line
1.8.1.4