@@ -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.
+import os
+import re
from ordereddict import OrderedDict
import sys
+include_cre = re.compile("include\(\"([^\"]*)\"\)")
+
builtin_types = [
'str', 'int', 'number', 'bool',
'int8', 'int16', 'int32', 'int64',
@@ -54,8 +58,9 @@ class QAPISchemaError(Exception):
class QAPISchema:
- def __init__(self, fp):
+ def __init__(self, fp, input_dir):
self.fp = fp
+ self.input_dir = input_dir
self.src = fp.read()
if self.src == '' or self.src[-1] != '\n':
self.src += '\n'
@@ -100,6 +105,20 @@ 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))
+ if not os.path.isfile(include_path):
+ raise QAPISchemaError(
+ self,
+ 'Non-existing included file "%s"' % include_path)
+ include_schema = QAPISchema(open(include_path),
+ self.input_dir)
+ self.exprs += include_schema.exprs
+ self.cursor += include_match.span()[1] - 1
elif not self.tok.isspace():
raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
@@ -159,8 +178,9 @@ class QAPISchema:
return expr
def parse_schema(input_path):
+ input_dir = os.path.dirname(input_path)
try:
- schema = QAPISchema(open(input_path, "r"))
+ schema = QAPISchema(open(input_path, "r"), input_dir)
except QAPISchemaError, e:
print >>sys.stderr, e
exit(1)
Adds the "include(...)" primitive to the syntax of QAPI schema files. Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu> --- scripts/qapi.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)