import errno
import os
import re
-import sys
from contextlib import contextmanager
from qapi.common import *
from qapi.schema import QAPISchemaVisitor
-class QAPIGen(object):
+class QAPIGen:
def __init__(self, fname):
self.fname = fname
return ''
def write(self, output_dir):
+ # Include paths starting with ../ are used to reuse modules of the main
+ # schema in specialised schemas. Don't overwrite the files that are
+ # already generated for the main schema.
+ if self.fname.startswith('../'):
+ return
pathname = os.path.join(output_dir, self.fname)
- dir = os.path.dirname(pathname)
- if dir:
+ odir = os.path.dirname(pathname)
+ if odir:
try:
- os.makedirs(dir)
+ os.makedirs(odir)
except os.error as e:
if e.errno != errno.EEXIST:
raise
fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
- if sys.version_info[0] >= 3:
- f = open(fd, 'r+', encoding='utf-8')
- else:
- f = os.fdopen(fd, 'r+')
+ f = open(fd, 'r+', encoding='utf-8')
text = self.get_content()
oldtext = f.read(len(text) + 1)
if text != oldtext:
class QAPIGenCCode(QAPIGen):
def __init__(self, fname):
- QAPIGen.__init__(self, fname)
+ super().__init__(fname)
self._start_if = None
def start_if(self, ifcond):
def get_content(self):
assert self._start_if is None
- return QAPIGen.get_content(self)
+ return super().get_content()
class QAPIGenC(QAPIGenCCode):
def __init__(self, fname, blurb, pydoc):
- QAPIGenCCode.__init__(self, fname)
+ super().__init__(fname)
self._blurb = blurb
self._copyright = '\n * '.join(re.findall(r'^Copyright .*', pydoc,
re.MULTILINE))
class QAPIGenH(QAPIGenC):
def _top(self):
- return QAPIGenC._top(self) + guardstart(self.fname)
+ return super()._top() + guardstart(self.fname)
def _bottom(self):
return guardend(self.fname)
class QAPIGenDoc(QAPIGen):
def _top(self):
- return (QAPIGen._top(self)
+ return (super()._top()
+ '@c AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n')
class QAPISchemaModularCVisitor(QAPISchemaVisitor):
- def __init__(self, prefix, what, blurb, pydoc):
+ def __init__(self, prefix, what, user_blurb, builtin_blurb, pydoc):
self._prefix = prefix
self._what = what
- self._blurb = blurb
+ self._user_blurb = user_blurb
+ self._builtin_blurb = builtin_blurb
self._pydoc = pydoc
self._genc = None
self._genh = None
genc = QAPIGenC(basename + '.c', blurb, self._pydoc)
genh = QAPIGenH(basename + '.h', blurb, self._pydoc)
self._module[name] = (genc, genh)
- self._set_module(name)
+ self._genc, self._genh = self._module[name]
def _add_user_module(self, name, blurb):
assert self._is_user_module(name)
def _add_system_module(self, name, blurb):
self._add_module(name and './' + name, blurb)
- def _set_module(self, name):
- self._genc, self._genh = self._module[name]
-
def write(self, output_dir, opt_builtins=False):
for name in self._module:
if self._is_builtin_module(name) and not opt_builtins:
genc.write(output_dir)
genh.write(output_dir)
+ def _begin_system_module(self, name):
+ pass
+
def _begin_user_module(self, name):
pass
def visit_module(self, name):
- if name in self._module:
- self._set_module(name)
- elif self._is_builtin_module(name):
- # The built-in module has not been created. No code may
- # be generated.
- self._genc = None
- self._genh = None
+ if name is None:
+ if self._builtin_blurb:
+ self._add_system_module(None, self._builtin_blurb)
+ self._begin_system_module(name)
+ else:
+ # The built-in module has not been created. No code may
+ # be generated.
+ self._genc = None
+ self._genh = None
else:
- self._add_user_module(name, self._blurb)
+ self._add_user_module(name, self._user_blurb)
self._begin_user_module(name)
def visit_include(self, name, info):