QAPIGenC and QAPIGenH in particular depend on fname being defined, but we have a usage of QAPIGenCCode that isn't intended to be associated with a particular file.
No problem, move the write method down to the class that actually needs it, and keep QAPIGenCCode more abstract. Signed-off-by: John Snow <js...@redhat.com> --- Possibly un-needed in concert with a forthcoming patch by Markus, but I didn't have it in my hands at time of publishing. Signed-off-by: John Snow <js...@redhat.com> --- scripts/qapi/commands.py | 2 +- scripts/qapi/gen.py | 54 ++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py index 71744f48a35..b346676d15a 100644 --- a/scripts/qapi/commands.py +++ b/scripts/qapi/commands.py @@ -258,7 +258,7 @@ def __init__(self, prefix: str): super().__init__( prefix, 'qapi-commands', ' * Schema-defined QAPI/QMP commands', None, __doc__) - self._regy = QAPIGenCCode(None) + self._regy = QAPIGenCCode() self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {} def _begin_user_module(self, name: str) -> None: diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py index 12ea98fb61e..2dd99635e74 100644 --- a/scripts/qapi/gen.py +++ b/scripts/qapi/gen.py @@ -36,8 +36,7 @@ class QAPIGen: - def __init__(self, fname: Optional[str]): - self.fname = fname + def __init__(self) -> None: self._preamble = '' self._body = '' @@ -58,28 +57,6 @@ def _bottom(self) -> str: # pylint: disable=no-self-use return '' - def write(self, output_dir: str) -> None: - # 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) - odir = os.path.dirname(pathname) - - if odir: - os.makedirs(odir, exist_ok=True) - - # use os.open for O_CREAT to create and read a non-existant file - fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666) - with os.fdopen(fd, 'r+', encoding='utf-8') as fp: - text = self.get_content() - oldtext = fp.read(len(text) + 1) - if text != oldtext: - fp.seek(0) - fp.truncate(0) - fp.write(text) - def _wrap_ifcond(ifcond: List[str], before: str, after: str) -> str: if before == after: @@ -121,8 +98,8 @@ def build_params(arg_type: Optional[QAPISchemaObjectType], class QAPIGenCCode(QAPIGen): - def __init__(self, fname: Optional[str]): - super().__init__(fname) + def __init__(self) -> None: + super().__init__() self._start_if: Optional[Tuple[List[str], str, str]] = None def start_if(self, ifcond: List[str]) -> None: @@ -147,11 +124,34 @@ def get_content(self) -> str: class QAPIGenC(QAPIGenCCode): def __init__(self, fname: str, blurb: str, pydoc: str): - super().__init__(fname) + super().__init__() + self.fname = fname self._blurb = blurb self._copyright = '\n * '.join(re.findall(r'^Copyright .*', pydoc, re.MULTILINE)) + def write(self, output_dir: str) -> None: + # 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) + odir = os.path.dirname(pathname) + + if odir: + os.makedirs(odir, exist_ok=True) + + # use os.open for O_CREAT to create and read a non-existant file + fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666) + with os.fdopen(fd, 'r+', encoding='utf-8') as fp: + text = self.get_content() + oldtext = fp.read(len(text) + 1) + if text != oldtext: + fp.seek(0) + fp.truncate(0) + fp.write(text) + def _top(self) -> str: return mcgen(''' /* AUTOMATICALLY GENERATED, DO NOT MODIFY */ -- 2.26.2