Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / common / 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 # FIXME: right now it uses pygst and scans on its own;
10 # we really should use inspect/*.xml instead since the result of
11 # gst-xmlinspect.py is committed by the docs maintainer, who can be
12 # expected to have pygst, but this step should be done for every docs build,
13 # so no pygst allowed
14
15 # read in inspect/*.xml
16 # for every tmpl/element-(name).xml: mangle with details from element
17
18 import glob
19 import re
20 import sys
21 import os
22
23 class Tmpl:
24     def __init__(self, filename):
25         self.filename = filename
26         self._sectionids = []
27         self._sections = {}
28
29     def read(self):
30         """
31         Read and parse the sections from the given file.
32         """
33         lines = open(self.filename).readlines()
34         matcher = re.compile("<!-- ##### SECTION (\S+) ##### -->\n")
35         id = None
36
37         for line in lines:
38             match = matcher.search(line)
39             if match:
40                 id = match.expand("\\1")
41                 self._sectionids.append(id)
42                 self._sections[id] = []
43             else:
44                 if not id:
45                     sys.stderr.write(
46                         "WARNING: line before a SECTION header: %s" % line)
47                 else:
48                     self._sections[id].append(line)
49
50     def get_section(self, id):
51         """
52         Get the content from the given section.
53         """
54         return self._sections[id]
55
56     def set_section(self, id, content):
57         """
58         Replace the given section id with the given content.
59         """
60         self._sections[id] = content
61
62     def output(self):
63         """
64         Return the output of the current template in the tmpl/*.sgml format.
65         """
66         lines = []
67         for id in self._sectionids:
68             lines.append("<!-- ##### SECTION %s ##### -->\n" % id)
69             for line in self._sections[id]:
70                 lines.append(line)
71
72         return "".join(lines)
73
74     def write(self, backup=False):
75         """
76         Write out the template file again, backing up the previous one.
77         """
78         if backup:
79             target = self.filename + ".mangle.bak"
80             os.rename(self.filename, target)
81
82         handle = open(self.filename, "w")
83         handle.write(self.output())
84         handle.close()
85
86 import xml.dom.minidom
87
88 def get_elements(file):
89     elements = {}
90     doc = xml.dom.minidom.parse(file)
91
92     elem = None
93     for e in doc.childNodes:
94         if e.nodeType == e.ELEMENT_NODE and e.localName == 'plugin':
95             elem = e
96             break
97     if elem == None:
98         return None
99
100     elem2 = None
101     for e in elem.childNodes:
102         if e.nodeType == e.ELEMENT_NODE and e.localName == 'elements':
103             elem2 = e
104             break
105     if elem2 == None:
106         return None
107
108     elem = elem2
109
110     for e in elem.childNodes:
111         if e.nodeType == e.ELEMENT_NODE and e.localName == 'element':
112             name = None
113             description = None
114
115             for e2 in e.childNodes:
116                 if e2.nodeType == e2.ELEMENT_NODE and e2.localName == 'name':
117                     name = e2.childNodes[0].nodeValue.encode("UTF-8")
118                 elif e2.nodeType == e2.ELEMENT_NODE and e2.localName == 'description':
119                     if e2.childNodes:
120                       description = e2.childNodes[0].nodeValue.encode("UTF-8")
121                     else:
122                       description = 'No description'
123
124             if name != None and description != None:
125                 elements[name] = {'description': description}
126
127     return elements
128
129 def main():
130     if not len(sys.argv) == 3:
131         sys.stderr.write('Please specify the inspect/ dir and the tmpl/ dir')
132         sys.exit(1)
133
134     inspectdir = sys.argv[1]
135     tmpldir = sys.argv[2]
136
137     # parse all .xml files; build map of element name -> short desc
138     #for file in glob.glob("inspect/plugin-*.xml"):
139     elements = {}
140     for file in glob.glob("%s/plugin-*.xml" % inspectdir):
141         elements.update(get_elements(file))
142
143     for file in glob.glob("%s/element-*.sgml" % tmpldir):
144         base = os.path.basename(file)
145         element = base[len("element-"):-len(".sgml")]
146         tmpl = Tmpl(file)
147         tmpl.read()
148         if element in elements.keys():
149             description = elements[element]['description']
150             tmpl.set_section("Short_Description", "%s\n\n" % description)
151
152         # put in an include if not yet there
153         line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
154             'element-' + element + '-details.xml">' + \
155             '<fallback xmlns="http://www.w3.org/2003/XInclude" />' + \
156             '</include>\n'
157         section = tmpl.get_section("Long_Description")
158         if not section[0]  == line:
159             section.insert(0, line)
160         tmpl.set_section("Long_Description", section)
161         tmpl.write()
162
163 main()