qapi: De-duplicate entity documentation generation code

QAPISchemaGenDocVisitor.visit_command() duplicates texi_entity() for
its boxed arguments case.  The previous commit added another copy in
.visit_event().

Replace texi_entity() by texi_type() and texi_msg().  Use texi_msg()
for the boxed arguments case as well.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20191024110237.30963-8-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2019-10-24 13:02:25 +02:00
parent b621a26040
commit a4bd91d3f3

View File

@ -12,7 +12,7 @@ from qapi.gen import QAPIGenDoc, QAPISchemaVisitor
MSG_FMT = """ MSG_FMT = """
@deftypefn {type} {{}} {name} @deftypefn {type} {{}} {name}
{body} {body}{members}{features}{sections}
@end deftypefn @end deftypefn
""".format """.format
@ -20,7 +20,7 @@ MSG_FMT = """
TYPE_FMT = """ TYPE_FMT = """
@deftp {{{type}}} {name} @deftp {{{type}}} {name}
{body} {body}{members}{features}{sections}
@end deftp @end deftp
""".format """.format
@ -149,7 +149,8 @@ def texi_member(member, desc, suffix):
suffix, desc, texi_if(member.ifcond, prefix='@*')) suffix, desc, texi_if(member.ifcond, prefix='@*'))
def texi_members(doc, what, base, variants, member_func): def texi_members(doc, what, base=None, variants=None,
member_func=texi_member):
"""Format the table of members""" """Format the table of members"""
items = '' items = ''
for section in doc.args.values(): for section in doc.args.values():
@ -182,6 +183,13 @@ def texi_members(doc, what, base, variants, member_func):
return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items) return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
def texi_arguments(doc, boxed_arg_type):
if boxed_arg_type:
return ('\n@b{Arguments:} the members of @code{%s}\n'
% boxed_arg_type.name)
return texi_members(doc, 'Arguments')
def texi_features(doc): def texi_features(doc):
"""Format the table of features""" """Format the table of features"""
items = '' items = ''
@ -208,12 +216,22 @@ def texi_sections(doc, ifcond):
return body return body
def texi_entity(doc, what, ifcond, base=None, variants=None, def texi_type(typ, doc, ifcond, members):
member_func=texi_member): return TYPE_FMT(type=typ,
return (texi_body(doc) name=doc.symbol,
+ texi_members(doc, what, base, variants, member_func) body=texi_body(doc),
+ texi_features(doc) members=members,
+ texi_sections(doc, ifcond)) features=texi_features(doc),
sections=texi_sections(doc, ifcond))
def texi_msg(typ, doc, ifcond, members):
return MSG_FMT(type=typ,
name=doc.symbol,
body=texi_body(doc),
members=members,
features=texi_features(doc),
sections=texi_sections(doc, ifcond))
class QAPISchemaGenDocVisitor(QAPISchemaVisitor): class QAPISchemaGenDocVisitor(QAPISchemaVisitor):
@ -227,9 +245,8 @@ class QAPISchemaGenDocVisitor(QAPISchemaVisitor):
def visit_enum_type(self, name, info, ifcond, members, prefix): def visit_enum_type(self, name, info, ifcond, members, prefix):
doc = self.cur_doc doc = self.cur_doc
self._gen.add(TYPE_FMT(type='Enum', self._gen.add(texi_type('Enum', doc, ifcond,
name=doc.symbol, texi_members(doc, 'Values',
body=texi_entity(doc, 'Values', ifcond,
member_func=texi_enum_value))) member_func=texi_enum_value)))
def visit_object_type(self, name, info, ifcond, base, members, variants, def visit_object_type(self, name, info, ifcond, base, members, variants,
@ -237,46 +254,27 @@ class QAPISchemaGenDocVisitor(QAPISchemaVisitor):
doc = self.cur_doc doc = self.cur_doc
if base and base.is_implicit(): if base and base.is_implicit():
base = None base = None
self._gen.add(TYPE_FMT(type='Object', self._gen.add(texi_type('Object', doc, ifcond,
name=doc.symbol, texi_members(doc, 'Members', base, variants)))
body=texi_entity(doc, 'Members', ifcond,
base, variants)))
def visit_alternate_type(self, name, info, ifcond, variants): def visit_alternate_type(self, name, info, ifcond, variants):
doc = self.cur_doc doc = self.cur_doc
self._gen.add(TYPE_FMT(type='Alternate', self._gen.add(texi_type('Alternate', doc, ifcond,
name=doc.symbol, texi_members(doc, 'Members')))
body=texi_entity(doc, 'Members', ifcond)))
def visit_command(self, name, info, ifcond, arg_type, ret_type, gen, def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
success_response, boxed, allow_oob, allow_preconfig, success_response, boxed, allow_oob, allow_preconfig,
features): features):
doc = self.cur_doc doc = self.cur_doc
if boxed: self._gen.add(texi_msg('Command', doc, ifcond,
body = texi_body(doc) texi_arguments(doc,
body += ('\n@b{Arguments:} the members of @code{%s}\n' arg_type if boxed else None)))
% arg_type.name)
body += texi_features(doc)
body += texi_sections(doc, ifcond)
else:
body = texi_entity(doc, 'Arguments', ifcond)
self._gen.add(MSG_FMT(type='Command',
name=doc.symbol,
body=body))
def visit_event(self, name, info, ifcond, arg_type, boxed): def visit_event(self, name, info, ifcond, arg_type, boxed):
doc = self.cur_doc doc = self.cur_doc
if boxed: self._gen.add(texi_msg('Event', doc, ifcond,
body = texi_body(doc) texi_arguments(doc,
body += ('\n@b{Arguments:} the members of @code{%s}\n' arg_type if boxed else None)))
% arg_type.name)
body += texi_features(doc)
body += texi_sections(doc, ifcond)
else:
body = texi_entity(doc, 'Arguments', ifcond)
self._gen.add(MSG_FMT(type='Event',
name=doc.symbol,
body=body))
def symbol(self, doc, entity): def symbol(self, doc, entity):
if self._gen._body: if self._gen._body: