require 0.9 gst-python
[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):
70         """
71         Write out the template file again, backing up the previous one.
72         """
73         target = self.filename + ".mangle.bak"
74         os.rename(self.filename, target)
75         handle = open(self.filename, "w")
76         handle.write(self.output())
77         handle.close()
78
79         
80 def main():
81     if len(sys.argv) > 1 and sys.argv[1]:
82         os.chdir(sys.argv[1])
83
84     elements = {}
85     all = gst.registry_pool_plugin_list()
86     for plugin in all:
87         for feature in plugin.get_feature_list():
88             if isinstance(feature, gst.ElementFactory):
89                 elements[feature.get_name()] = feature
90                 
91     for file in glob.glob("element-*.sgml"):
92         base = os.path.basename(file)
93         element = base[len("element-"):-len(".sgml")]
94         tmpl = Tmpl(file)
95         tmpl.read()
96         if element in elements.keys():
97             feature = elements[element]
98             description = feature.get_description()
99             tmpl.set_section("Short_Description", "%s\n\n" % description)
100
101         # put in an include if not yet there
102         line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
103             'element-' + element + '-details.xml" />\n'
104         section = tmpl.get_section("Long_Description")
105         if not section[0]  == line:
106             section.insert(0, line)
107         tmpl.set_section("Long_Description", section)
108         tmpl.write()
109
110 main()