Skip to content

Commit

Permalink
qapi: De-duplicate entity documentation generation code
Browse files Browse the repository at this point in the history
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 <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Markus Armbruster committed Oct 29, 2019
1 parent b621a26 commit a4bd91d
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions scripts/qapi/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
MSG_FMT = """
@deftypefn {type} {{}} {name}
{body}
{body}{members}{features}{sections}
@end deftypefn
""".format

TYPE_FMT = """
@deftp {{{type}}} {name}
{body}
{body}{members}{features}{sections}
@end deftp
""".format
Expand Down Expand Up @@ -149,7 +149,8 @@ def texi_member(member, desc, suffix):
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"""
items = ''
for section in doc.args.values():
Expand Down Expand Up @@ -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)


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):
"""Format the table of features"""
items = ''
Expand All @@ -208,12 +216,22 @@ def texi_sections(doc, ifcond):
return body


def texi_entity(doc, what, ifcond, base=None, variants=None,
member_func=texi_member):
return (texi_body(doc)
+ texi_members(doc, what, base, variants, member_func)
+ texi_features(doc)
+ texi_sections(doc, ifcond))
def texi_type(typ, doc, ifcond, members):
return TYPE_FMT(type=typ,
name=doc.symbol,
body=texi_body(doc),
members=members,
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):
Expand All @@ -227,56 +245,36 @@ def write(self, output_dir):

def visit_enum_type(self, name, info, ifcond, members, prefix):
doc = self.cur_doc
self._gen.add(TYPE_FMT(type='Enum',
name=doc.symbol,
body=texi_entity(doc, 'Values', ifcond,
member_func=texi_enum_value)))
self._gen.add(texi_type('Enum', doc, ifcond,
texi_members(doc, 'Values',
member_func=texi_enum_value)))

def visit_object_type(self, name, info, ifcond, base, members, variants,
features):
doc = self.cur_doc
if base and base.is_implicit():
base = None
self._gen.add(TYPE_FMT(type='Object',
name=doc.symbol,
body=texi_entity(doc, 'Members', ifcond,
base, variants)))
self._gen.add(texi_type('Object', doc, ifcond,
texi_members(doc, 'Members', base, variants)))

def visit_alternate_type(self, name, info, ifcond, variants):
doc = self.cur_doc
self._gen.add(TYPE_FMT(type='Alternate',
name=doc.symbol,
body=texi_entity(doc, 'Members', ifcond)))
self._gen.add(texi_type('Alternate', doc, ifcond,
texi_members(doc, 'Members')))

def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
success_response, boxed, allow_oob, allow_preconfig,
features):
doc = self.cur_doc
if boxed:
body = texi_body(doc)
body += ('\n@b{Arguments:} the members of @code{%s}\n'
% 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))
self._gen.add(texi_msg('Command', doc, ifcond,
texi_arguments(doc,
arg_type if boxed else None)))

def visit_event(self, name, info, ifcond, arg_type, boxed):
doc = self.cur_doc
if boxed:
body = texi_body(doc)
body += ('\n@b{Arguments:} the members of @code{%s}\n'
% 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))
self._gen.add(texi_msg('Event', doc, ifcond,
texi_arguments(doc,
arg_type if boxed else None)))

def symbol(self, doc, entity):
if self._gen._body:
Expand Down

0 comments on commit a4bd91d

Please sign in to comment.