Received: from eggs.gnu.org ([2001:4830:134:3::10]:55576) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WUhhZ-0002Zm-UT for qemu-devel@nongnu.org; Mon, 31 Mar 2014 15:17:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WUhhT-0003ld-JY for qemu-devel@nongnu.org; Mon, 31 Mar 2014 15:16:57 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:34488) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WUhhS-0003lO-D2 for qemu-devel@nongnu.org; Mon, 31 Mar 2014 15:16:51 -0400 From: =?utf-8?b?TGx1w61z?= Vilanova Date: Mon, 31 Mar 2014 21:16:48 +0200 Message-Id: <20140331191648.21034.9744.stgit@fimbulvetr.bsc.es> In-Reply-To: <20140331191631.21034.25433.stgit@fimbulvetr.bsc.es> References: <20140331191631.21034.25433.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v6 3/4] 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: =?UTF-8?q?Beno=C3=AEt=20Canet?= , Markus Armbruster , Luiz Capitulino Signed-off-by: Llu=C3=ADs Vilanova --- docs/qapi-code-gen.txt | 11 +++++++++++ scripts/qapi.py | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt index 824f6e5..70b4eeb 100644 --- a/docs/qapi-code-gen.txt +++ b/docs/qapi-code-gen.txt @@ -40,6 +40,17 @@ 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 + +=3D=3D=3D Includes =3D=3D=3D + +The QAPI schema definitions can be modularized using the 'include' direc= tive: + + { 'include': 'path/to/file.json'} + +The directive is evaluated recursively, and include paths are relative t= o the +file using the directive. + + =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 3a38e27..9f73426 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -11,6 +11,8 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. =20 +import os +import re from ordereddict import OrderedDict import os import sys @@ -62,8 +64,11 @@ class QAPIExprError(Exception): =20 class QAPISchema: =20 - def __init__(self, fp): + def __init__(self, fp, included=3D[]): self.fp =3D fp + input_path =3D os.path.abspath(fp.name) + self.included =3D included + [input_path] + self.input_dir =3D os.path.dirname(input_path) self.src =3D fp.read() if self.src =3D=3D '' or self.src[-1] !=3D '\n': self.src +=3D '\n' @@ -75,9 +80,33 @@ class QAPISchema: =20 while self.tok !=3D None: expr_info =3D {'fp': fp, 'line': self.line} - expr_elem =3D {'expr': self.get_expr(False), - 'info': expr_info} - self.exprs.append(expr_elem) + expr =3D self.get_expr(False) + if isinstance(expr, dict) and "include" in expr: + if len(expr) !=3D 1: + raise QAPIExprError(expr_info, "Invalid 'include' di= rective") + include_path =3D expr["include"] + if not isinstance(include_path, str): + raise QAPIExprError(expr_info, + 'Expected a file path (string), = got: %s' + % include_path) + if not os.path.isabs(include_path): + include_path =3D os.path.join(self.input_dir, includ= e_path) + if not os.path.isfile(include_path): + raise QAPIExprError( + expr_info, + 'Non-existing included file "%s"' % + include_path) + if include_path in self.included: + raise QAPIExprError(expr_info, "Infinite inclusion l= oop: %s" + % " -> ".join(self.included + + [include_path])) + exprs_include =3D QAPISchema(open(include_path, "r"), + self.included) + self.exprs.extend(exprs_include.exprs) + else: + expr_elem =3D {'expr': expr, + 'info': expr_info} + self.exprs.append(expr_elem) =20 def accept(self): while True: @@ -267,7 +296,7 @@ def check_exprs(schema): def parse_schema(input_path): try: schema =3D QAPISchema(open(input_path, "r")) - except QAPISchemaError, e: + except (QAPISchemaError, QAPIExprError), e: print >>sys.stderr, e exit(1) =20