Received: from eggs.gnu.org ([2001:4830:134:3::10]:47754) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WCx00-0004hj-Cu for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:58:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WCwzu-0005GP-6H for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:58:36 -0500 Received: from roura.ac.upc.edu ([147.83.33.10]:44580 helo=roura.ac.upc.es) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WCwzt-0005GD-R6 for qemu-devel@nongnu.org; Mon, 10 Feb 2014 14:58:30 -0500 From: =?utf-8?b?TGx1w61z?= Vilanova Date: Mon, 10 Feb 2014 20:58:28 +0100 Message-Id: <20140210195828.3495.73534.stgit@fimbulvetr.bsc.es> In-Reply-To: <20140210195816.3495.74530.stgit@fimbulvetr.bsc.es> References: <20140210195816.3495.74530.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 2/2] 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 --- scripts/qapi.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 9fc6fb2..80fb594 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -11,9 +11,13 @@ # 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 sys =20 +include_cre =3D re.compile("include\(\"([^\"]*)\"\)") + builtin_types =3D [ 'str', 'int', 'number', 'bool', 'int8', 'int16', 'int32', 'int64', @@ -54,8 +58,9 @@ class QAPISchemaError(Exception): =20 class QAPISchema: =20 - def __init__(self, fp): + def __init__(self, fp, input_dir): self.fp =3D fp + 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' @@ -100,6 +105,20 @@ 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)) + if not os.path.isfile(include_path): + raise QAPISchemaError( + self, + 'Non-existing included file "%s"' % include_= path) + include_schema =3D QAPISchema(open(include_path), + self.input_dir) + 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 @@ -159,8 +178,9 @@ class QAPISchema: return expr =20 def parse_schema(input_path): + input_dir =3D os.path.dirname(input_path) try: - schema =3D QAPISchema(open(input_path, "r")) + schema =3D QAPISchema(open(input_path, "r"), input_dir) except QAPISchemaError, e: print >>sys.stderr, e exit(1)