mangle-tmpl.py: keep original Long_Description; only insert an include if it's not...
[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 get_section(self, id):
43         """
44         Get the content from the given section.
45         """
46         return self._sections[id]
47
48     def set_section(self, id, content):
49         """
50         Replace the given section id with the given content.
51         """
52         self._sections[id] = content
53
54     def output(self):
55         """
56         Return the output of the current template in the tmpl/*.sgml format.
57         """
58         lines = []
59         for id in self._sectionids:
60             lines.append("<!-- ##### SECTION %s ##### -->\n" % id)
61             for line in self._sections[id]:
62                 lines.append(line)
63
64         return "".join(lines)
65
66     def write(self):
67         """
68         Write out the template file again, backing up the previous one.
69         """
70         target = self.filename + ".mangle.bak"
71         os.rename(self.filename, target)
72         handle = open(self.filename, "w")
73         handle.write(self.output())
74         handle.close()
75
76         
77 def main():
78     if len(sys.argv) > 1 and sys.argv[1]:
79         os.chdir(sys.argv[1])
80
81     elements = {}
82     all = gst.registry_pool_plugin_list()
83     for plugin in all:
84         for feature in plugin.get_feature_list():
85             if isinstance(feature, gst.ElementFactory):
86                 elements[feature.get_name()] = feature
87                 
88     for file in glob.glob("element-*.sgml"):
89         base = os.path.basename(file)
90         element = base[len("element-"):-len(".sgml")]
91         tmpl = Tmpl(file)
92         tmpl.read()
93         if element in elements.keys():
94             feature = elements[element]
95             description = feature.get_description()
96             tmpl.set_section("Short_Description", "%s\n\n" % description)
97
98         # put in an include if not yet there
99         line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
100             'element-' + element + '-details.xml" />\n'
101         section = tmpl.get_section("Long_Description")
102         if not section[0]  == line:
103             section.insert(0, line)
104         tmpl.set_section("Long_Description", section)
105         tmpl.write()
106
107 main()