gstreamer/bindings/python/codegen/docgen.py

767 lines
28 KiB
Python
Raw Normal View History

2011-06-07 00:26:20 +00:00
#!/usr/bin/env python
2011-06-07 19:20:46 +00:00
import getopt
import os
import re
import sys
2011-06-07 00:26:20 +00:00
import definitions
2011-06-07 19:20:46 +00:00
import defsparser
2011-06-07 00:26:20 +00:00
import docextract
2011-06-07 19:20:46 +00:00
import override
2011-06-07 00:26:20 +00:00
class Node:
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def __init__(self, name, interfaces=[]):
self.name = name
self.interfaces = interfaces
self.subclasses = []
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def add_child(self, node):
self.subclasses.append(node)
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def build_object_tree(parser):
# reorder objects so that parent classes come first ...
objects = parser.objects[:]
pos = 0
while pos < len(objects):
parent = objects[pos].parent
for i in range(pos+1, len(objects)):
if objects[i].c_name == parent:
objects.insert(i+1, objects[pos])
del objects[pos]
break
else:
pos = pos + 1
root = Node(None)
2011-06-07 19:20:46 +00:00
nodes = {None: root}
2011-06-07 00:26:20 +00:00
for obj_def in objects:
2011-06-07 19:20:46 +00:00
parent_name = obj_def.parent
if parent_name == 'GObject':
parent_name = None
parent_node = nodes[parent_name]
2011-06-07 00:26:20 +00:00
node = Node(obj_def.c_name, obj_def.implements)
parent_node.add_child(node)
nodes[node.name] = node
if parser.interfaces:
interfaces = Node('gobject.GInterface')
root.add_child(interfaces)
nodes[interfaces.name] = interfaces
for obj_def in parser.interfaces:
node = Node(obj_def.c_name)
interfaces.add_child(node)
nodes[node.name] = node
if parser.boxes:
boxed = Node('gobject.GBoxed')
root.add_child(boxed)
nodes[boxed.name] = boxed
for obj_def in parser.boxes:
node = Node(obj_def.c_name)
boxed.add_child(node)
nodes[node.name] = node
if parser.pointers:
pointers = Node('gobject.GPointer')
root.add_child(pointers)
nodes[pointers.name] = pointers
for obj_def in parser.pointers:
node = Node(obj_def.c_name)
pointers.add_child(node)
nodes[node.name] = node
return root
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
class DocWriter:
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def __init__(self):
2011-06-07 19:20:46 +00:00
self._fp = None
2011-06-07 00:26:20 +00:00
# parse the defs file
self.parser = defsparser.DefsParser(())
self.overrides = override.Overrides()
self.classmap = {}
self.docs = {}
def add_sourcedirs(self, source_dirs):
self.docs = docextract.extract(source_dirs, self.docs)
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def add_tmpldirs(self, tmpl_dirs):
self.docs = docextract.extract_tmpl(tmpl_dirs, self.docs)
def add_docs(self, defs_file, overrides_file, module_name):
'''parse information about a given defs file'''
self.parser.filename = defs_file
self.parser.startParsing(defs_file)
if overrides_file:
self.overrides.handle_file(overrides_file)
2011-06-07 19:20:46 +00:00
for obj in (self.parser.objects + self.parser.interfaces +
self.parser.boxes + self.parser.pointers):
if not obj.c_name in self.classmap:
self.classmap[obj.c_name] = '%s.%s' % (
module_name, obj.name)
2011-06-07 00:26:20 +00:00
def pyname(self, name):
return self.classmap.get(name, name)
2011-06-07 19:20:46 +00:00
def _compare(self, obja, objb):
2011-06-07 00:26:20 +00:00
return cmp(self.pyname(obja.c_name), self.pyname(objb.c_name))
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def output_docs(self, output_prefix):
2011-06-07 19:20:46 +00:00
files = {}
2011-06-07 00:26:20 +00:00
# class hierarchy
hierarchy = build_object_tree(self.parser)
filename = self.create_filename('hierarchy', output_prefix)
2011-06-07 19:20:46 +00:00
self._fp = open(filename, 'w')
self.write_full_hierarchy(hierarchy)
self._fp.close()
obj_defs = (self.parser.objects + self.parser.interfaces +
self.parser.boxes + self.parser.pointers)
obj_defs.sort(self._compare)
2011-06-07 00:26:20 +00:00
for obj_def in obj_defs:
filename = self.create_filename(obj_def.c_name, output_prefix)
2011-06-07 19:20:46 +00:00
self._fp = open(filename, 'w')
2011-06-07 00:26:20 +00:00
if isinstance(obj_def, definitions.ObjectDef):
2011-06-07 19:20:46 +00:00
self.output_object_docs(obj_def)
2011-06-07 00:26:20 +00:00
elif isinstance(obj_def, definitions.InterfaceDef):
2011-06-07 19:20:46 +00:00
self.output_interface_docs(obj_def)
2011-06-07 00:26:20 +00:00
elif isinstance(obj_def, definitions.BoxedDef):
2011-06-07 19:20:46 +00:00
self.output_boxed_docs(obj_def)
2011-06-07 00:26:20 +00:00
elif isinstance(obj_def, definitions.PointerDef):
2011-06-07 19:20:46 +00:00
self.output_boxed_docs(obj_def)
self._fp.close()
files[os.path.basename(filename)] = obj_def
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
if not files:
return
output_filename = self.create_toc_filename(output_prefix)
self._fp = open(output_filename, 'w')
self.output_toc(files)
self._fp.close()
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def output_object_docs(self, obj_def):
self.write_class_header(obj_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
self.write_heading('Synopsis')
self.write_synopsis(obj_def)
self.close_section()
2011-06-07 00:26:20 +00:00
# construct the inheritence hierarchy ...
2011-06-07 19:20:46 +00:00
ancestry = [(obj_def.c_name, obj_def.implements)]
2011-06-07 00:26:20 +00:00
try:
parent = obj_def.parent
while parent != None:
if parent == 'GObject':
ancestry.append(('GObject', []))
parent = None
else:
parent_def = self.parser.find_object(parent)
ancestry.append((parent_def.c_name, parent_def.implements))
parent = parent_def.parent
except ValueError:
pass
ancestry.reverse()
2011-06-07 19:20:46 +00:00
self.write_heading('Ancestry')
self.write_hierarchy(obj_def.c_name, ancestry)
self.close_section()
2011-06-07 00:26:20 +00:00
constructor = self.parser.find_constructor(obj_def, self.overrides)
if constructor:
2011-06-07 19:20:46 +00:00
self.write_heading('Constructor')
2011-06-07 00:26:20 +00:00
self.write_constructor(constructor,
2011-06-07 19:20:46 +00:00
self.docs.get(constructor.c_name, None))
self.close_section()
2011-06-07 00:26:20 +00:00
methods = self.parser.find_methods(obj_def)
methods = filter(lambda meth, self=self:
not self.overrides.is_ignored(meth.c_name), methods)
if methods:
2011-06-07 19:20:46 +00:00
self.write_heading('Methods')
2011-06-07 00:26:20 +00:00
for method in methods:
2011-06-07 19:20:46 +00:00
self.write_method(method, self.docs.get(method.c_name, None))
self.close_section()
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
self.write_class_footer(obj_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def get_methods_for_object(self, obj_def):
methods = []
for method in self.parser.find_methods(obj_def):
if not self.overrides.is_ignored(method.c_name):
methods.append(method)
return methods
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def output_interface_docs(self, int_def):
self.write_class_header(int_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
self.write_heading('Synopsis')
self.write_synopsis(int_def)
self.close_section()
methods = self.get_methods_for_object(int_def)
2011-06-07 00:26:20 +00:00
if methods:
2011-06-07 19:20:46 +00:00
self.write_heading('Methods')
2011-06-07 00:26:20 +00:00
for method in methods:
2011-06-07 19:20:46 +00:00
self.write_method(method, self.docs.get(method.c_name, None))
self.close_section()
self.write_class_footer(int_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def output_boxed_docs(self, box_def):
self.write_class_header(box_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
self.write_heading('Synopsis')
self.write_synopsis(box_def)
self.close_section()
2011-06-07 00:26:20 +00:00
constructor = self.parser.find_constructor(box_def, self.overrides)
if constructor:
2011-06-07 19:20:46 +00:00
self.write_heading('Constructor')
2011-06-07 00:26:20 +00:00
self.write_constructor(constructor,
2011-06-07 19:20:46 +00:00
self.docs.get(constructor.c_name, None))
self.close_section()
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
methods = self.get_methods_for_object(box_def)
2011-06-07 00:26:20 +00:00
if methods:
2011-06-07 19:20:46 +00:00
self.write_heading('Methods')
2011-06-07 00:26:20 +00:00
for method in methods:
2011-06-07 19:20:46 +00:00
self.write_method(method, self.docs.get(method.c_name, None))
self.close_section()
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
self.write_class_footer(box_def.c_name)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def output_toc(self, files):
self._fp.write('TOC\n\n')
for filename in sorted(files):
obj_def = files[filename]
self._fp.write(obj_def.c_name + ' - ' + filename + '\n')
2011-06-07 00:26:20 +00:00
# override the following to create a more complex output format
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def create_filename(self, obj_name, output_prefix):
'''Create output filename for this particular object'''
2011-06-07 19:20:46 +00:00
return output_prefix + '-' + obj_name.lower() + '.txt'
2011-06-07 00:26:20 +00:00
def create_toc_filename(self, output_prefix):
return self.create_filename(self, 'docs', output_prefix)
2011-06-07 19:20:46 +00:00
def write_full_hierarchy(self, hierarchy):
def handle_node(node, indent=''):
2011-06-07 00:26:20 +00:00
for child in node.subclasses:
2011-06-07 19:20:46 +00:00
self._fp.write(indent + node.name)
2011-06-07 00:26:20 +00:00
if node.interfaces:
2011-06-07 19:20:46 +00:00
self._fp.write(' (implements ')
self._fp.write(', '.join(node.interfaces))
self._fp.write(')\n')
2011-06-07 00:26:20 +00:00
else:
2011-06-07 19:20:46 +00:00
self._fp.write('\n')
handle_node(child, indent + ' ')
handle_node(hierarchy)
def serialize_params(self, func_def):
params = []
for param in func_def.params:
params.append(param[1])
return ', '.join(params)
2011-06-07 00:26:20 +00:00
# these need to handle default args ...
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def create_constructor_prototype(self, func_def):
2011-06-07 19:20:46 +00:00
return '%s(%s)' % (func_def.is_constructor_of,
self.serialize_params(func_def))
2011-06-07 00:26:20 +00:00
def create_function_prototype(self, func_def):
2011-06-07 19:20:46 +00:00
return '%s(%s)' % (func_def.name,
self.serialize_params(func_def))
2011-06-07 00:26:20 +00:00
def create_method_prototype(self, meth_def):
2011-06-07 19:20:46 +00:00
return '%s.%s(%s)' % (meth_def.of_object,
meth_def.name,
self.serialize_params(meth_def))
def write_class_header(self, obj_name):
self._fp.write('Class %s\n' % obj_name)
self._fp.write('======%s\n\n' % ('=' * len(obj_name)))
def write_class_footer(self, obj_name):
2011-06-07 00:26:20 +00:00
pass
2011-06-07 19:20:46 +00:00
def write_heading(self, text):
self._fp.write('\n' + text + '\n' + ('-' * len(text)) + '\n')
def close_section(self):
2011-06-07 00:26:20 +00:00
pass
2011-06-07 19:20:46 +00:00
def write_synopsis(self, obj_def):
self._fp.write('class %s' % obj_def.c_name)
2011-06-07 00:26:20 +00:00
if isinstance(obj_def, definitions.ObjectDef):
bases = []
2011-06-07 19:20:46 +00:00
if obj_def.parent:
bases.append(obj_def.parent)
2011-06-07 00:26:20 +00:00
bases = bases = obj_def.implements
if bases:
2011-06-07 19:20:46 +00:00
self._fp.write('(%s)' % ', '.join(bases, ))
self._fp.write(':\n')
2011-06-07 00:26:20 +00:00
constructor = self.parser.find_constructor(obj_def, self.overrides)
if constructor:
prototype = self.create_constructor_prototype(constructor)
2011-06-07 19:20:46 +00:00
self._fp.write(' def %s\n' % prototype)
for method in self.get_methods_for_object(obj_def):
prototype = self.create_method_prototype(method)
self._fp.write(' def %s\n' % prototype)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def write_hierarchy(self, obj_name, ancestry):
2011-06-07 00:26:20 +00:00
indent = ''
for name, interfaces in ancestry:
2011-06-07 19:20:46 +00:00
self._fp.write(indent + '+-- ' + name)
2011-06-07 00:26:20 +00:00
if interfaces:
2011-06-07 19:20:46 +00:00
self._fp.write(' (implements ')
self._fp.write(', '.join(interfaces))
self._fp.write(')\n')
2011-06-07 00:26:20 +00:00
else:
2011-06-07 19:20:46 +00:00
self._fp.write('\n')
2011-06-07 00:26:20 +00:00
indent = indent + ' '
2011-06-07 19:20:46 +00:00
self._fp.write('\n')
def write_constructor(self, func_def, func_doc):
2011-06-07 00:26:20 +00:00
prototype = self.create_constructor_prototype(func_def)
2011-06-07 19:20:46 +00:00
self._fp.write(prototype + '\n\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in func_def.params:
2011-06-07 19:20:46 +00:00
self.write_parameter(name, func_doc)
self.write_return_value(func_def, func_doc)
2011-06-07 00:26:20 +00:00
if func_doc and func_doc.description:
2011-06-07 19:20:46 +00:00
self._fp.write(func_doc.description)
self._fp.write('\n\n\n')
def write_method(self, meth_def, func_doc):
2011-06-07 00:26:20 +00:00
prototype = self.create_method_prototype(meth_def)
2011-06-07 19:20:46 +00:00
self._fp.write(prototype + '\n\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in meth_def.params:
2011-06-07 19:20:46 +00:00
self.write_parameter(name, func_doc)
self.write_return_value(meth_def, func_doc)
if func_doc and func_doc.description:
self._fp.write('\n')
self._fp.write(func_doc.description)
self._fp.write('\n\n')
def write_parameter(self, param_name, func_doc):
if func_doc:
descr = func_doc.get_param_description(param_name)
else:
descr = 'a ' + type
self._fp.write(' ' + param_name + ': ' + descr + '\n')
def write_return_value(self, meth_def, func_doc):
2011-06-07 00:26:20 +00:00
if meth_def.ret and meth_def.ret != 'none':
if func_doc and func_doc.ret:
descr = func_doc.ret
else:
descr = 'a ' + meth_def.ret
2011-06-07 19:20:46 +00:00
self._fp.write(' Returns: ' + descr + '\n')
CLASS_HEADER_TEMPLATE = """<refentry id="%(entryid)s">
<refmeta>
<refentrytitle>%(name)s</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>%(miscinfo)s</refmiscinfo>
</refmeta>
<refnamediv>
<refname>%(name)s</refname><refpurpose></refpurpose>
</refnamediv>
"""
VARIABLE_TEMPLATE = """<varlistentry>
<term><parameter>%(parameter)s</parameter>&nbsp;:</term>
<listitem><simpara>%(description)s</simpara></listitem>
</varlistentry>
"""
DOCBOOK_HEADER = """<?xml version="1.0" standalone="no"?>
<!DOCTYPE synopsis PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
"""
2011-06-07 00:26:20 +00:00
class DocbookDocWriter(DocWriter):
2011-06-07 19:20:46 +00:00
def __init__(self):
2011-06-07 00:26:20 +00:00
DocWriter.__init__(self)
2011-06-07 19:20:46 +00:00
self._function_pat = re.compile(r'(\w+)\s*\(\)')
self._parameter_pat = re.compile(r'\@(\w+)')
self._constant_pat = re.compile(r'\%(-?\w+)')
self._symbol_pat = re.compile(r'#([\w-]+)')
self._transtable = ['-'] * 256
# make string -> reference translation func
for digit in '0123456789':
self._transtable[ord(digit)] = digit
for letter in 'abcdefghijklmnopqrstuvwxyz':
self._transtable[ord(letter)] = letter
self._transtable[ord(letter.upper())] = letter
self._transtable = ''.join(self._transtable)
2011-06-07 00:26:20 +00:00
def create_filename(self, obj_name, output_prefix):
'''Create output filename for this particular object'''
2011-06-07 19:20:46 +00:00
stem = output_prefix + '-' + obj_name.lower()
return stem + '.xml'
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def create_toc_filename(self, output_prefix):
return self.create_filename('classes', output_prefix)
2011-06-07 00:26:20 +00:00
def make_class_ref(self, obj_name):
2011-06-07 19:20:46 +00:00
return 'class-' + obj_name.translate(self._transtable)
2011-06-07 00:26:20 +00:00
def make_method_ref(self, meth_def):
2011-06-07 19:20:46 +00:00
return 'method-%s--%s' % (
meth_def.of_object.translate(self._transtable),
meth_def.name.translate(self._transtable))
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def _format_function(self, match):
2011-06-07 00:26:20 +00:00
info = self.parser.c_name.get(match.group(1), None)
if info:
if isinstance(info, defsparser.FunctionDef):
2011-06-07 19:20:46 +00:00
return self._format_funcdef(info)
2011-06-07 00:26:20 +00:00
if isinstance(info, defsparser.MethodDef):
2011-06-07 19:20:46 +00:00
return self._format_method(info)
2011-06-07 00:26:20 +00:00
# fall through through
2011-06-07 19:20:46 +00:00
return '<function>%s()</function>' % (match.group(1), )
def _format_funcdef(self, info):
if info.is_constructor_of is not None:
# should have a link here
return '<methodname>%s()</methodname>' % (
self.pyname(info.is_constructor_of), )
else:
return '<function>%s()</function>' % (info.name, )
def _format_param(self, match):
return '<parameter>%s</parameterdliteral>' % (match.group(1), )
def _format_const(self, match):
return '<literal>%s</literal>' % (match.group(1), )
def _format_method(self, info):
return ('<link linkend="%s">'
'<methodname>%s.%s</methodname>'
'</link>') % (self.make_method_ref(info),
self.pyname(info.of_object),
info.name)
def _format_object(self, info):
return ('<link linkend="%s">'
'<classname>%s</classname>'
'</link>') % (self.make_class_ref(info.c_name),
self.pyname(info.c_name))
def _format_symbol(self, match):
2011-06-07 00:26:20 +00:00
info = self.parser.c_name.get(match.group(1), None)
if info:
if isinstance(info, defsparser.FunctionDef):
2011-06-07 19:20:46 +00:00
return self._format_funcdef(info)
elif isinstance(info, defsparser.MethodDef):
return self._format_method(info)
elif isinstance(info, (defsparser.ObjectDef,
defsparser.InterfaceDef,
defsparser.BoxedDef,
defsparser.PointerDef)):
return self._format_object(info)
2011-06-07 00:26:20 +00:00
# fall through through
2011-06-07 19:20:46 +00:00
return '<literal>%s</literal>' % (match.group(1), )
2011-06-07 00:26:20 +00:00
def reformat_text(self, text, singleline=0):
# replace special strings ...
2011-06-07 19:20:46 +00:00
text = self._function_pat.sub(self._format_function, text)
text = self._parameter_pat.sub(self._format_param, text)
text = self._constant_pat.sub(self._format_const, text)
text = self._symbol_pat.sub(self._format_symbol, text)
2011-06-07 00:26:20 +00:00
# don't bother with <para> expansion for single line text.
2011-06-07 19:20:46 +00:00
if singleline:
return text
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
lines = text.strip().split('\n')
2011-06-07 00:26:20 +00:00
for index in range(len(lines)):
2011-06-07 19:20:46 +00:00
if lines[index].strip() == '':
2011-06-07 00:26:20 +00:00
lines[index] = '</para>\n<para>'
continue
2011-06-07 19:20:46 +00:00
return '<para>%s</para>' % ('\n'.join(lines), )
2011-06-07 00:26:20 +00:00
# write out hierarchy
2011-06-07 19:20:46 +00:00
def write_full_hierarchy(self, hierarchy):
def handle_node(node, indent=''):
2011-06-07 00:26:20 +00:00
if node.name:
2011-06-07 19:20:46 +00:00
self._fp.write('%s<link linkend="%s">%s</link>' %
2011-06-07 00:26:20 +00:00
(indent, self.make_class_ref(node.name),
self.pyname(node.name)))
if node.interfaces:
2011-06-07 19:20:46 +00:00
self._fp.write(' (implements ')
2011-06-07 00:26:20 +00:00
for i in range(len(node.interfaces)):
2011-06-07 19:20:46 +00:00
self._fp.write('<link linkend="%s">%s</link>' %
2011-06-07 00:26:20 +00:00
(self.make_class_ref(node.interfaces[i]),
self.pyname(node.interfaces[i])))
if i != len(node.interfaces) - 1:
2011-06-07 19:20:46 +00:00
self._fp.write(', ')
self._fp.write(')\n')
2011-06-07 00:26:20 +00:00
else:
2011-06-07 19:20:46 +00:00
self._fp.write('\n')
2011-06-07 00:26:20 +00:00
indent = indent + ' '
2011-06-07 19:20:46 +00:00
node.subclasses.sort(lambda a, b:
2011-06-07 00:26:20 +00:00
cmp(self.pyname(a.name), self.pyname(b.name)))
for child in node.subclasses:
2011-06-07 19:20:46 +00:00
handle_node(child, indent)
self._fp.write(DOCBOOK_HEADER)
self._fp.write('<synopsis>')
handle_node(hierarchy)
self._fp.write('</synopsis>\n')
2011-06-07 00:26:20 +00:00
# these need to handle default args ...
2011-06-07 19:20:46 +00:00
2011-06-07 00:26:20 +00:00
def create_constructor_prototype(self, func_def):
2011-06-07 19:20:46 +00:00
xml = ['<constructorsynopsis language="python">\n']
xml.append(' <methodname>__init__</methodname>\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in func_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <methodparam><parameter>')
xml.append(name)
xml.append('</parameter>')
2011-06-07 00:26:20 +00:00
if dflt:
2011-06-07 19:20:46 +00:00
xml.append('<initializer>')
xml.append(dflt)
xml.append('</initializer>')
xml.append('</methodparam>\n')
2011-06-07 00:26:20 +00:00
if not func_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <methodparam></methodparam>')
xml.append(' </constructorsynopsis>')
return ''.join(xml)
2011-06-07 00:26:20 +00:00
def create_function_prototype(self, func_def):
2011-06-07 19:20:46 +00:00
xml = ['<funcsynopsis language="python">\n <funcprototype>\n']
xml.append(' <funcdef><function>')
xml.append(func_def.name)
xml.append('</function></funcdef>\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in func_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <paramdef><parameter>')
xml.append(name)
xml.append('</parameter>')
2011-06-07 00:26:20 +00:00
if dflt:
2011-06-07 19:20:46 +00:00
xml.append('<initializer>')
xml.append(dflt)
xml.append('</initializer>')
xml.append('</paramdef>\n')
2011-06-07 00:26:20 +00:00
if not func_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <paramdef></paramdef')
xml.append(' </funcprototype>\n </funcsynopsis>')
return ''.join(xml)
2011-06-07 00:26:20 +00:00
def create_method_prototype(self, meth_def, addlink=0):
2011-06-07 19:20:46 +00:00
xml = ['<methodsynopsis language="python">\n']
xml.append(' <methodname>')
2011-06-07 00:26:20 +00:00
if addlink:
2011-06-07 19:20:46 +00:00
xml.append('<link linkend="%s">' % self.make_method_ref(meth_def))
xml.append(self.pyname(meth_def.name))
2011-06-07 00:26:20 +00:00
if addlink:
2011-06-07 19:20:46 +00:00
xml.append('</link>')
xml.append('</methodname>\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in meth_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <methodparam><parameter>')
xml.append(name)
xml.append('</parameter>')
2011-06-07 00:26:20 +00:00
if dflt:
2011-06-07 19:20:46 +00:00
xml.append('<initializer>')
xml.append(dflt)
xml.append('</initializer>')
xml.append('</methodparam>\n')
2011-06-07 00:26:20 +00:00
if not meth_def.params:
2011-06-07 19:20:46 +00:00
xml.append(' <methodparam></methodparam>')
xml.append(' </methodsynopsis>')
return ''.join(xml)
def write_class_header(self, obj_name):
self._fp.write(DOCBOOK_HEADER)
self._fp.write(CLASS_HEADER_TEMPLATE % dict(
entryid=self.make_class_ref(obj_name),
name=self.pyname(obj_name),
miscinfo="PyGTK Docs"))
def write_class_footer(self, obj_name):
self._fp.write('</refentry>\n')
def write_heading(self, text):
self._fp.write(' <refsect1>\n')
self._fp.write(' <title>' + text + '</title>\n\n')
def close_section(self):
self._fp.write(' </refsect1>\n')
def write_synopsis(self, obj_def):
self._fp.write('<classsynopsis language="python">\n')
self._fp.write(' <ooclass><classname>%s</classname></ooclass>\n'
2011-06-07 00:26:20 +00:00
% self.pyname(obj_def.c_name))
if isinstance(obj_def, definitions.ObjectDef):
if obj_def.parent:
2011-06-07 19:20:46 +00:00
self._fp.write(' <ooclass><classname><link linkend="%s">%s'
2011-06-07 00:26:20 +00:00
'</link></classname></ooclass>\n'
% (self.make_class_ref(obj_def.parent),
self.pyname(obj_def.parent)))
for base in obj_def.implements:
2011-06-07 19:20:46 +00:00
self._fp.write(' <ooclass><classname><link linkend="%s">%s'
2011-06-07 00:26:20 +00:00
'</link></classname></ooclass>\n'
% (self.make_class_ref(base), self.pyname(base)))
elif isinstance(obj_def, definitions.InterfaceDef):
2011-06-07 19:20:46 +00:00
self._fp.write(' <ooclass><classname>gobject.GInterface'
2011-06-07 00:26:20 +00:00
'</classname></ooclass>\n')
elif isinstance(obj_def, definitions.BoxedDef):
2011-06-07 19:20:46 +00:00
self._fp.write(' <ooclass><classname>gobject.GBoxed'
2011-06-07 00:26:20 +00:00
'</classname></ooclass>\n')
elif isinstance(obj_def, definitions.PointerDef):
2011-06-07 19:20:46 +00:00
self._fp.write(' <ooclass><classname>gobject.GPointer'
2011-06-07 00:26:20 +00:00
'</classname></ooclass>\n')
constructor = self.parser.find_constructor(obj_def, self.overrides)
if constructor:
2011-06-07 19:20:46 +00:00
self._fp.write(
'%s\n' % self.create_constructor_prototype(constructor))
for method in self.get_methods_for_object(obj_def):
self._fp.write(
'%s\n' % self.create_method_prototype(method, addlink=1))
self._fp.write('</classsynopsis>\n\n')
def write_hierarchy(self, obj_name, ancestry):
self._fp.write('<synopsis>')
2011-06-07 00:26:20 +00:00
indent = ''
for name, interfaces in ancestry:
2011-06-07 19:20:46 +00:00
self._fp.write(
'%s+-- <link linkend="%s">%s</link>' %
(indent, self.make_class_ref(name), self.pyname(name)))
2011-06-07 00:26:20 +00:00
if interfaces:
2011-06-07 19:20:46 +00:00
self._fp.write(' (implements ')
2011-06-07 00:26:20 +00:00
for i in range(len(interfaces)):
2011-06-07 19:20:46 +00:00
self._fp.write('<link linkend="%s">%s</link>' %
2011-06-07 00:26:20 +00:00
(self.make_class_ref(interfaces[i]),
self.pyname(interfaces[i])))
if i != len(interfaces) - 1:
2011-06-07 19:20:46 +00:00
self._fp.write(', ')
self._fp.write(')\n')
2011-06-07 00:26:20 +00:00
else:
2011-06-07 19:20:46 +00:00
self._fp.write('\n')
2011-06-07 00:26:20 +00:00
indent = indent + ' '
2011-06-07 19:20:46 +00:00
self._fp.write('</synopsis>\n\n')
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
def write_params(self, params, ret, func_doc):
2011-06-07 00:26:20 +00:00
if not params and (not ret or ret == 'none'):
return
2011-06-07 19:20:46 +00:00
self._fp.write(' <variablelist>\n')
2011-06-07 00:26:20 +00:00
for type, name, dflt, null in params:
if func_doc:
2011-06-07 19:20:46 +00:00
descr = func_doc.get_param_description(name).strip()
2011-06-07 00:26:20 +00:00
else:
descr = 'a ' + type
2011-06-07 19:20:46 +00:00
self._fp.write(VARIABLE_TEMPLATE % dict(
parameter=name,
description=self.reformat_text(descr, singleline=1)))
2011-06-07 00:26:20 +00:00
if ret and ret != 'none':
if func_doc and func_doc.ret:
2011-06-07 19:20:46 +00:00
descr = func_doc.ret.strip()
2011-06-07 00:26:20 +00:00
else:
descr = 'a ' + ret
2011-06-07 19:20:46 +00:00
self._fp.write(VARIABLE_TEMPLATE % dict(
parameter='Returns',
description=self.reformat_text(descr, singleline=1)))
self._fp.write(' </variablelist>\n')
def write_constructor(self, func_def, func_doc):
2011-06-07 00:26:20 +00:00
prototype = self.create_constructor_prototype(func_def)
2011-06-07 19:20:46 +00:00
self._fp.write('<programlisting>%s</programlisting>\n' % prototype)
self.write_params(func_def.params, func_def.ret, func_doc)
2011-06-07 00:26:20 +00:00
if func_doc and func_doc.description:
2011-06-07 19:20:46 +00:00
self._fp.write(self.reformat_text(func_doc.description))
self._fp.write('\n\n\n')
def write_method(self, meth_def, func_doc):
self._fp.write(' <refsect2 id="%s">\n' % (
self.make_method_ref(meth_def), ))
self._fp.write(' <title>%s.%s</title>\n\n' % (
self.pyname(meth_def.of_object),
meth_def.name))
2011-06-07 00:26:20 +00:00
prototype = self.create_method_prototype(meth_def)
2011-06-07 19:20:46 +00:00
self._fp.write('<programlisting>%s</programlisting>\n' % prototype)
self.write_params(meth_def.params, meth_def.ret, func_doc)
2011-06-07 00:26:20 +00:00
if func_doc and func_doc.description:
2011-06-07 19:20:46 +00:00
self._fp.write(self.reformat_text(func_doc.description))
self._fp.write(' </refsect2>\n\n\n')
2011-06-07 00:26:20 +00:00
def output_toc(self, files, fp=sys.stdout):
2011-06-07 19:20:46 +00:00
self._fp.write(DOCBOOK_HEADER)
2011-06-07 00:26:20 +00:00
2011-06-07 19:20:46 +00:00
#self._fp.write('<reference id="class-reference">\n')
#self._fp.write(' <title>Class Documentation</title>\n')
#for filename, obj_def in files:
# self._fp.write('&' +
# obj_def.c_name.translate(self._transtable) + ';\n')
#self._fp.write('</reference>\n')
self._fp.write('<reference id="class-reference" '
'xmlns:xi="http://www.w3.org/2001/XInclude">\n')
self._fp.write(' <title>Class Reference</title>\n')
for filename in sorted(files):
self._fp.write(' <xi:include href="%s"/>\n' % filename)
self._fp.write('</reference>\n')
def main(args):
2011-06-07 00:26:20 +00:00
try:
2011-06-07 19:20:46 +00:00
opts, args = getopt.getopt(args[1:], "d:s:o:",
2011-06-07 00:26:20 +00:00
["defs-file=", "override=", "source-dir=",
"output-prefix="])
except getopt.error, e:
sys.stderr.write('docgen.py: %s\n' % e)
sys.stderr.write(
'usage: docgen.py -d file.defs [-s /src/dir] [-o output-prefix]\n')
2011-06-07 19:20:46 +00:00
return 1
2011-06-07 00:26:20 +00:00
defs_file = None
overrides_file = None
source_dirs = []
output_prefix = 'docs'
for opt, arg in opts:
if opt in ('-d', '--defs-file'):
defs_file = arg
2011-06-07 19:20:46 +00:00
if opt in ('--override', ):
2011-06-07 00:26:20 +00:00
overrides_file = arg
elif opt in ('-s', '--source-dir'):
source_dirs.append(arg)
elif opt in ('-o', '--output-prefix'):
output_prefix = arg
if len(args) != 0 or not defs_file:
sys.stderr.write(
'usage: docgen.py -d file.defs [-s /src/dir] [-o output-prefix]\n')
2011-06-07 19:20:46 +00:00
return 1
2011-06-07 00:26:20 +00:00
d = DocbookDocWriter()
d.add_sourcedirs(source_dirs)
2011-06-07 19:20:46 +00:00
d.add_docs(defs_file, overrides_file, 'gio')
2011-06-07 00:26:20 +00:00
d.output_docs(output_prefix)
2011-06-07 19:20:46 +00:00
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))