qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v4 2/3] qapi: Add a primitive to include other files


From: Lluís Vilanova
Subject: [Qemu-devel] [PATCH v4 2/3] qapi: Add a primitive to include other files from a QAPI schema file
Date: Fri, 28 Feb 2014 16:53:22 +0100
User-agent: StGit/0.16

Adds the "include(...)" primitive to the syntax of QAPI schema files.

Signed-off-by: Lluís Vilanova <address@hidden>
---
 docs/qapi-code-gen.txt |    8 ++++++++
 scripts/qapi.py        |   36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
index 2e9f036..e007807 100644
--- a/docs/qapi-code-gen.txt
+++ b/docs/qapi-code-gen.txt
@@ -40,6 +40,14 @@ enumeration types and union types.
 Generally speaking, types definitions should always use CamelCase for the type
 names. Command names should be all lower case with words separated by a hyphen.
 
+The QAPI schema definitions can be modularized using the 'include' directive:
+
+ include("sub-system/qapi.json")
+
+All paths are interpreted as relative to the initial input file passed to the
+QAPI parsing scripts.
+
+
 === Complex types ===
 
 A complex type is a dictionary containing a single key whose value is a
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 59c2b9b..eddbf25 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -11,10 +11,14 @@
 # This work is licensed under the terms of the GNU GPLv2.
 # See the COPYING.LIB file in the top-level directory.
 
+import os
+import re
 from ordereddict import OrderedDict
 import os
 import sys
 
+include_cre = re.compile("include\(\"([^\"]*)\"\)")
+
 builtin_types = [
     'str', 'int', 'number', 'bool',
     'int8', 'int16', 'int32', 'int64',
@@ -57,12 +61,16 @@ class QAPISchemaError(Exception):
 
 class QAPISchema:
 
-    def __init__(self, fp, error_base=None):
+    def __init__(self, fp, input_dir, error_base=None, included=[]):
         self.fp = fp
         if error_base is None:
             self.error_base = os.getcwd()
         else:
             self.error_base = error_base
+        input_path = os.path.abspath(fp.name)
+        input_path = os.path.relpath(input_path, self.error_base)
+        self.included = included + [input_path]
+        self.input_dir = input_dir
         self.src = fp.read()
         if self.src == '' or self.src[-1] != '\n':
             self.src += '\n'
@@ -107,6 +115,29 @@ class QAPISchema:
                 if self.cursor == len(self.src):
                     self.tok = None
                     return
+            elif self.tok == 'i':
+                include_src = self.src[self.cursor-1:]
+                include_match = include_cre.match(include_src)
+                if include_match is not None:
+                    include_path = os.path.join(self.input_dir,
+                                                include_match.group(1))
+                    include_path = os.path.abspath(include_path)
+                    if not os.path.isfile(include_path):
+                        raise QAPISchemaError(
+                            self,
+                            'Non-existing included file "%s"' %
+                            include_match.group(1))
+                    include_path_rel = os.path.relpath(include_path,
+                                                       self.error_base)
+                    if include_path_rel in self.included:
+                        raise QAPISchemaError(self, "Infinite inclusion loop: 
%s"
+                                              % " -> ".join(self.included +
+                                                            
[include_path_rel]))
+                    include_schema = QAPISchema(open(include_path, "r"),
+                                                self.input_dir, 
self.error_base,
+                                                self.included)
+                    self.exprs += include_schema.exprs
+                    self.cursor += include_match.span()[1] - 1
             elif not self.tok.isspace():
                 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
 
@@ -166,8 +197,9 @@ class QAPISchema:
         return expr
 
 def parse_schema(input_path, error_base=None):
+    input_dir = os.path.dirname(input_path)
     try:
-        schema = QAPISchema(open(input_path, "r"), error_base)
+        schema = QAPISchema(open(input_path, "r"), input_dir, error_base)
     except QAPISchemaError, e:
         print >>sys.stderr, e
         exit(1)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]