adding support files for plugins documentation
[platform/upstream/gst-common.git] / gst-xmlinspect.py
1 # -*- Mode: Python -*-
2 # vi:si:et:sw=4:sts=4:ts=4
3
4 """
5 examine all plugins and elements and output xml documentation for them
6 used as part of the plugin documentation build
7 """
8
9 import sys
10 import os
11 import gst
12
13 INDENT_SIZE = 2
14
15 # all templates
16 ELEMENT_TEMPLATE = """<element>
17   <name>%(name)s</name>
18   <longname>%(longname)s</longname>
19   <class>%(class)s</class>
20   <description>%(description)s</description>
21   <author>%(author)s</author>
22 </element>"""
23
24 PLUGIN_TEMPLATE = """<plugin>
25   <name>%(name)s</name>
26   <description>%(description)s</description>
27   <filename>%(filename)s</filename>
28   <basename>%(basename)s</basename>
29   <version>%(version)s</version>
30   <license>%(license)s</license>
31   <package>%(package)s</package>
32   <origin>%(origin)s</origin>
33   <elements>
34 %(elements)s
35   </elements>
36 </plugin>"""
37
38 def xmlencode(line):
39     """
40     Replace &, <, and >
41     """
42     line = "&amp;".join(line.split("&"))
43     line = "&lt;".join(line.split("<"))
44     line = "&gt;".join(line.split(">"))
45     return line
46
47 def get_offset(indent):
48     return " " * INDENT_SIZE * indent
49
50 def output_element_factory(elf, indent=0):
51     print  "ELEMENT", elf.get_name()
52     d = {
53         'name':        xmlencode(elf.get_name()),
54         'longname':    xmlencode(elf.get_longname()),
55         'class':       xmlencode(elf.get_klass()),
56         'description': xmlencode(elf.get_description()),
57         'author':      xmlencode(elf.get_author()),
58     }
59     block = ELEMENT_TEMPLATE % d
60     
61     offset = get_offset(indent)
62     return offset + ("\n" + offset).join(block.split("\n"))
63
64
65 def output_plugin(plugin, indent=0):
66     print "PLUGIN", plugin.get_name()
67     version = ".".join([str(i) for i in plugin.get_version()])
68     
69     elements = []
70     for feature in plugin.get_feature_list():
71         if isinstance(feature, gst.ElementFactory):
72             elements.append(output_element_factory(feature, indent + 2))
73         
74     filename = plugin.get_filename()
75     basename = filename
76     if basename:
77         basename = os.path.basename(basename)
78     d = {
79         'name':        xmlencode(plugin.get_name()),
80         'description': xmlencode(plugin.get_description()),
81         'filename':    filename,
82         'basename':    basename,
83         'version':     version,
84         'license':     xmlencode(plugin.get_license()),
85         'package':     xmlencode(plugin.get_package()),
86         'origin':      xmlencode(plugin.get_origin()),
87         'elements': "\n".join(elements),
88     }
89     block = PLUGIN_TEMPLATE % d
90     
91     offset = get_offset(indent)
92     return offset + ("\n" + offset).join(block.split("\n"))
93
94 def main():
95     if sys.argv[1]:
96         os.chdir(sys.argv[1])
97
98     all = gst.registry_pool_plugin_list()
99     for plugin in all:
100         filename = "plugin-%s.xml" % plugin.get_name()
101         handle = open(filename, "w")
102         handle.write(output_plugin(plugin))
103         handle.close()
104
105 main()