Received: from eggs.gnu.org ([2001:4830:134:3::10]:42152) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDCwz-00022e-Ro for qemu-devel@nongnu.org; Tue, 11 Feb 2014 08:00:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WDCwt-00070s-Ak for qemu-devel@nongnu.org; Tue, 11 Feb 2014 08:00:33 -0500 Received: from roura.ac.upc.edu ([147.83.33.10]:46923 helo=roura.ac.upc.es) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WDCws-00070N-BQ for qemu-devel@nongnu.org; Tue, 11 Feb 2014 08:00:26 -0500 From: =?utf-8?b?TGx1w61z?= Vilanova Date: Tue, 11 Feb 2014 14:00:25 +0100 Message-Id: <20140211130025.12374.30326.stgit@fimbulvetr.bsc.es> In-Reply-To: <20140211130013.12374.17471.stgit@fimbulvetr.bsc.es> References: <20140211130013.12374.17471.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v3 2/3] qapi: Add a primitive to include other files from a QAPI schema file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Markus Armbruster , Luiz Capitulino Adds the "include(...)" primitive to the syntax of QAPI schema files. Signed-off-by: Llu=C3=ADs Vilanova --- 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 th= e type names. Command names should be all lower case with words separated by a = hyphen. =20 +The QAPI schema definitions can be modularized using the 'include' direc= tive: + + include("sub-system/qapi.json") + +All paths are interpreted as relative to the initial input file passed t= o the +QAPI parsing scripts. + + =3D=3D=3D Complex types =3D=3D=3D =20 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..a3e2a30 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. =20 +import os +import re from ordereddict import OrderedDict import os import sys =20 +include_cre =3D re.compile("include\(\"([^\"]*)\"\)") + builtin_types =3D [ 'str', 'int', 'number', 'bool', 'int8', 'int16', 'int32', 'int64', @@ -57,12 +61,16 @@ class QAPISchemaError(Exception): =20 class QAPISchema: =20 - def __init__(self, fp, error_base=3DNone): + def __init__(self, fp, input_dir, error_base=3DNone, included=3D[]): self.fp =3D fp if error_base is None: self.error_base =3D os.getcwd() else: self.error_base =3D error_base + input_path =3D os.path.abspath(fp.name) + input_path =3D os.path.relpath(input_path, self.error_base) + self.included =3D included + [input_path] + self.input_dir =3D input_dir self.src =3D fp.read() if self.src =3D=3D '' or self.src[-1] !=3D '\n': self.src +=3D '\n' @@ -107,6 +115,29 @@ class QAPISchema: if self.cursor =3D=3D len(self.src): self.tok =3D None return + elif self.tok =3D=3D 'i': + include_src =3D self.src[self.cursor-1:] + include_match =3D include_cre.match(include_src) + if include_match is not None: + include_path =3D os.path.join(self.input_dir, + include_match.group(1)) + include_path =3D 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 =3D os.path.relpath(include_path, + self.error_base) + included =3D self.included + [include_path_rel] + if include_path_rel in self.included: + raise QAPISchemaError(self, "Inifite inclusion l= oop: %s" + % " -> ".join(included)) + include_schema =3D QAPISchema(open(include_path, "r"= ), + self.input_dir, self.err= or_base, + included) + self.exprs +=3D include_schema.exprs + self.cursor +=3D include_match.span()[1] - 1 elif not self.tok.isspace(): raise QAPISchemaError(self, 'Stray "%s"' % self.tok) =20 @@ -166,8 +197,9 @@ class QAPISchema: return expr =20 def parse_schema(input_path, error_base=3DNone): + input_dir =3D os.path.dirname(input_path) try: - schema =3D QAPISchema(open(input_path, "r"), error_base) + schema =3D QAPISchema(open(input_path, "r"), input_dir, error_ba= se) except QAPISchemaError, e: print >>sys.stderr, e exit(1)