916f214cb693c9c79215611117e2e3970b59234c
[platform/upstream/gst-common.git] / mangle-tmpl.py
1 # -*- Mode: Python -*-
2 # vi:si:et:sw=4:sts=4:ts=4
3
4 """
5 use the output from gst-xmlinspect.py to mangle tmpl/*.sgml and
6 insert/overwrite Short Description and Long Description
7 """
8
9 import glob
10 import re
11 import sys
12 import os
13 import gst
14
15 class Tmpl:
16     def __init__(self, filename):
17         self.filename = filename
18         self._sectionids = []
19         self._sections = {}
20
21     def read(self):
22         """
23         Read and parse the sections from the given file.
24         """
25         lines = open(self.filename).readlines()
26         matcher = re.compile("<!-- ##### SECTION (\S+) ##### -->\n")
27         id = None
28
29         for line in lines:
30             match = matcher.search(line)
31             if match:
32                 id = match.expand("\\1")
33                 self._sectionids.append(id)
34                 self._sections[id] = []
35             else:
36                 if not id:
37                     sys.stderr.write(
38                         "WARNING: line before a SECTION header: %s" % line)
39                 else:
40                     self._sections[id].append(line)
41
42     def set_section(self, id, content):
43         """
44         Replace the given section id with the given content.
45         """
46         self._sections[id] = content
47
48     def output(self):
49         """
50         Return the output of the current template in the tmpl/*.sgml format.
51         """
52         lines = []
53         for id in self._sectionids:
54             lines.append("<!-- ##### SECTION %s ##### -->\n" % id)
55             for line in self._sections[id]:
56                 lines.append(line)
57
58         return "".join(lines)
59
60     def write(self):
61         """
62         Write out the template file again, backing up the previous one.
63         """
64         target = self.filename + ".mangle.bak"
65         os.rename(self.filename, target)
66         handle = open(self.filename, "w")
67         handle.write(self.output())
68         handle.close()
69
70         
71 def main():
72     if len(sys.argv) > 1 and sys.argv[1]:
73         os.chdir(sys.argv[1])
74
75     elements = {}
76     all = gst.registry_pool_plugin_list()
77     for plugin in all:
78         for feature in plugin.get_feature_list():
79             if isinstance(feature, gst.ElementFactory):
80                 elements[feature.get_name()] = feature
81                 
82     for file in glob.glob("element-*.sgml"):
83         base = os.path.basename(file)
84         element = base[len("element-"):-len(".sgml")]
85         tmpl = Tmpl(file)
86         tmpl.read()
87         if element in elements.keys():
88             feature = elements[element]
89             description = feature.get_description()
90             tmpl.set_section("Short_Description", "%s\n\n" % description)
91         tmpl.set_section("Long_Description",
92             '<include xmlns="http://www.w3.org/2003/XInclude" href="element-' +
93             element + '-details.xml" />\n<para>\n\n</para>\n')
94         tmpl.write()
95
96 main()