~ubuntu-branches/ubuntu/quantal/transmageddon/quantal

« back to all changes in this revision

Viewing changes to common/mangle-tmpl.py

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-10-14 08:28:43 UTC
  • Revision ID: james.westby@ubuntu.com-20091014082843-uxbyrcqydc13zrim
Tags: upstream-0.14
ImportĀ upstreamĀ versionĀ 0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 commited 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
                    description = e2.childNodes[0].nodeValue.encode("UTF-8")
 
120
 
 
121
            if name != None and description != None:
 
122
                elements[name] = {'description': description}
 
123
 
 
124
    return elements
 
125
        
 
126
def main():
 
127
    if not len(sys.argv) == 3:
 
128
        sys.stderr.write('Please specify the inspect/ dir and the tmpl/ dir')
 
129
        sys.exit(1)
 
130
 
 
131
    inspectdir = sys.argv[1]
 
132
    tmpldir = sys.argv[2]
 
133
 
 
134
    # parse all .xml files; build map of element name -> short desc
 
135
    #for file in glob.glob("inspect/plugin-*.xml"):
 
136
    elements = {}
 
137
    for file in glob.glob("%s/plugin-*.xml" % inspectdir):
 
138
        elements.update(get_elements(file))
 
139
 
 
140
    for file in glob.glob("%s/element-*.sgml" % tmpldir):
 
141
        base = os.path.basename(file)
 
142
        element = base[len("element-"):-len(".sgml")]
 
143
        tmpl = Tmpl(file)
 
144
        tmpl.read()
 
145
        if element in elements.keys():
 
146
            description = elements[element]['description']
 
147
            tmpl.set_section("Short_Description", "%s\n\n" % description)
 
148
 
 
149
        # put in an include if not yet there
 
150
        line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
 
151
            'element-' + element + '-details.xml" />\n'
 
152
        section = tmpl.get_section("Long_Description")
 
153
        if not section[0]  == line:
 
154
            section.insert(0, line)
 
155
        tmpl.set_section("Long_Description", section)
 
156
        tmpl.write()
 
157
 
 
158
main()