-def texi_alternate(expr, doc):
- """Format an alternate to texi"""
- body = texi_body(doc)
- return TYPE_FMT(type="Alternate",
- name=doc.symbol,
- body=body)
-
-
-def texi_union(expr, doc):
- """Format a union to texi"""
- discriminator = expr.get("discriminator")
- if discriminator:
- union = "Flat Union"
- else:
- union = "Simple Union"
-
- body = texi_body(doc)
- return TYPE_FMT(type=union,
- name=doc.symbol,
- body=body)
-
-
-def texi_enum(expr, doc):
- """Format an enum to texi"""
- body = texi_body(doc, True)
- return TYPE_FMT(type="Enum",
- name=doc.symbol,
- body=body)
-
-
-def texi_struct(expr, doc):
- """Format a struct to texi"""
- body = texi_body(doc)
- return TYPE_FMT(type="Struct",
- name=doc.symbol,
- body=body)
-
-
-def texi_command(expr, doc):
- """Format a command to texi"""
- body = texi_body(doc)
- return MSG_FMT(type="Command",
- name=doc.symbol,
- body=body)
-
-
-def texi_event(expr, doc):
- """Format an event to texi"""
- body = texi_body(doc)
- return MSG_FMT(type="Event",
- name=doc.symbol,
- body=body)
-
-
-def texi_expr(expr, doc):
- """Format an expr to texi"""
- (kind, _) = expr.items()[0]
-
- fmt = {"command": texi_command,
- "struct": texi_struct,
- "enum": texi_enum,
- "union": texi_union,
- "alternate": texi_alternate,
- "event": texi_event}[kind]
-
- return fmt(expr, doc)
-
-
-def texi(docs):
- """Convert QAPI schema expressions to texi documentation"""
- res = []
- for doc in docs:
- expr = doc.expr
- if not expr:
- res.append(texi_body(doc))
- continue
- try:
- doc = texi_expr(expr, doc)
- res.append(doc)
- except:
- print >>sys.stderr, "error at @%s" % doc.info
- raise
-
- return '\n'.join(res)
+def texi_entity(doc, member_func=texi_member, show_undocumented=False):
+ return (texi_body(doc)
+ + texi_members(doc, member_func, show_undocumented)
+ + texi_sections(doc))
+
+
+class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
+ def __init__(self):
+ self.out = None
+ self.cur_doc = None
+
+ def visit_begin(self, schema):
+ self.out = ''
+
+ def visit_enum_type(self, name, info, values, prefix):
+ doc = self.cur_doc
+ if self.out:
+ self.out += '\n'
+ self.out += TYPE_FMT(type='Enum',
+ name=doc.symbol,
+ body=texi_entity(doc,
+ member_func=texi_enum_value,
+ show_undocumented=True))
+
+ def visit_object_type(self, name, info, base, members, variants):
+ doc = self.cur_doc
+ if not variants:
+ typ = 'Struct'
+ elif variants._tag_name: # TODO unclean member access
+ typ = 'Flat Union'
+ else:
+ typ = 'Simple Union'
+ if self.out:
+ self.out += '\n'
+ self.out += TYPE_FMT(type=typ,
+ name=doc.symbol,
+ body=texi_entity(doc))
+
+ def visit_alternate_type(self, name, info, variants):
+ doc = self.cur_doc
+ if self.out:
+ self.out += '\n'
+ self.out += TYPE_FMT(type='Alternate',
+ name=doc.symbol,
+ body=texi_entity(doc))
+
+ def visit_command(self, name, info, arg_type, ret_type,
+ gen, success_response, boxed):
+ doc = self.cur_doc
+ if self.out:
+ self.out += '\n'
+ self.out += MSG_FMT(type='Command',
+ name=doc.symbol,
+ body=texi_entity(doc))
+
+ def visit_event(self, name, info, arg_type, boxed):
+ doc = self.cur_doc
+ if self.out:
+ self.out += '\n'
+ self.out += MSG_FMT(type='Event',
+ name=doc.symbol,
+ body=texi_entity(doc))
+
+ def symbol(self, doc, entity):
+ self.cur_doc = doc
+ entity.visit(self)
+ self.cur_doc = None
+
+ def freeform(self, doc):
+ assert not doc.args
+ if self.out:
+ self.out += '\n'
+ self.out += texi_body(doc) + texi_sections(doc)
+
+
+def texi_schema(schema):
+ """Convert QAPI schema documentation to Texinfo"""
+ gen = QAPISchemaGenDocVisitor()
+ gen.visit_begin(schema)
+ for doc in schema.docs:
+ if doc.symbol:
+ gen.symbol(doc, schema.lookup_entity(doc.symbol))
+ else:
+ gen.freeform(doc)
+ return gen.out