don't backup by default; helps fixing distcheck
[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
14 import pygst
15 pygst.require('0.9')
16 import gst
17
18 class Tmpl:
19     def __init__(self, filename):
20         self.filename = filename
21         self._sectionids = []
22         self._sections = {}
23
24     def read(self):
25         """
26         Read and parse the sections from the given file.
27         """
28         lines = open(self.filename).readlines()
29         matcher = re.compile("<!-- ##### SECTION (\S+) ##### -->\n")
30         id = None
31
32         for line in lines:
33             match = matcher.search(line)
34             if match:
35                 id = match.expand("\\1")
36                 self._sectionids.append(id)
37                 self._sections[id] = []
38             else:
39                 if not id:
40                     sys.stderr.write(
41                         "WARNING: line before a SECTION header: %s" % line)
42                 else:
43                     self._sections[id].append(line)
44
45     def get_section(self, id):
46         """
47         Get the content from the given section.
48         """
49         return self._sections[id]
50
51     def set_section(self, id, content):
52         """
53         Replace the given section id with the given content.
54         """
55         self._sections[id] = content
56
57     def output(self):
58         """
59         Return the output of the current template in the tmpl/*.sgml format.
60         """
61         lines = []
62         for id in self._sectionids:
63             lines.append("<!-- ##### SECTION %s ##### -->\n" % id)
64             for line in self._sections[id]:
65                 lines.append(line)
66
67         return "".join(lines)
68
69     def write(self, backup=False):
70         """
71         Write out the template file again, backing up the previous one.
72         """
73         if backup:
74             target = self.filename + ".mangle.bak"
75             os.rename(self.filename, target)
76
77         handle = open(self.filename, "w")
78         handle.write(self.output())
79         handle.close()
80         
81 def main():
82     if len(sys.argv) > 1 and sys.argv[1]:
83         os.chdir(sys.argv[1])
84
85     elements = {}
86     all = gst.registry_pool_plugin_list()
87     for plugin in all:
88         for feature in plugin.get_feature_list():
89             if isinstance(feature, gst.ElementFactory):
90                 elements[feature.get_name()] = feature
91                 
92     for file in glob.glob("element-*.sgml"):
93         base = os.path.basename(file)
94         element = base[len("element-"):-len(".sgml")]
95         tmpl = Tmpl(file)
96         tmpl.read()
97         if element in elements.keys():
98             feature = elements[element]
99             description = feature.get_description()
100             tmpl.set_section("Short_Description", "%s\n\n" % description)
101
102         # put in an include if not yet there
103         line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
104             'element-' + element + '-details.xml" />\n'
105         section = tmpl.get_section("Long_Description")
106         if not section[0]  == line:
107             section.insert(0, line)
108         tmpl.set_section("Long_Description", section)
109         tmpl.write()
110
111 main()