Changeset View
Changeset View
Standalone View
Standalone View
tools/xincludator.py
1 | #!/usr/bin/python | 1 | #!/usr/bin/python | ||
---|---|---|---|---|---|
2 | 2 | | |||
3 | import sys | ||||
3 | from sys import argv, stdout, stderr | 4 | from sys import argv, stdout, stderr | ||
4 | import codecs, locale | 5 | import codecs, locale | ||
5 | import os | 6 | import os | ||
6 | import xml.dom.minidom | 7 | import xml.dom.minidom | ||
7 | 8 | | |||
8 | stdout = codecs.getwriter('utf-8')(stdout) | 9 | if sys.version_info[0] < 3: | ||
10 | stdout = codecs.getwriter('utf-8')(stdout) | ||||
9 | 11 | | |||
10 | NS_XI = 'http://www.w3.org/2001/XInclude' | 12 | NS_XI = 'http://www.w3.org/2001/XInclude' | ||
11 | 13 | | |||
12 | def xincludate(dom, base, dropns = []): | 14 | def xincludate(dom, base, dropns = []): | ||
13 | remove_attrs = [] | 15 | remove_attrs = [] | ||
14 | for i in xrange(dom.documentElement.attributes.length): | 16 | for i in range(dom.documentElement.attributes.length): | ||
15 | attr = dom.documentElement.attributes.item(i) | 17 | attr = dom.documentElement.attributes.item(i) | ||
16 | if attr.prefix == 'xmlns': | 18 | if attr.prefix == 'xmlns': | ||
17 | if attr.localName in dropns: | 19 | if attr.localName in dropns: | ||
18 | remove_attrs.append(attr) | 20 | remove_attrs.append(attr) | ||
19 | else: | 21 | else: | ||
20 | dropns.append(attr.localName) | 22 | dropns.append(attr.localName) | ||
21 | for attr in remove_attrs: | 23 | for attr in remove_attrs: | ||
22 | dom.documentElement.removeAttributeNode(attr) | 24 | dom.documentElement.removeAttributeNode(attr) | ||
23 | for include in dom.getElementsByTagNameNS(NS_XI, 'include'): | 25 | for include in dom.getElementsByTagNameNS(NS_XI, 'include'): | ||
24 | href = include.getAttribute('href') | 26 | href = include.getAttribute('href') | ||
25 | # FIXME: assumes Unixy paths | 27 | # FIXME: assumes Unixy paths | ||
26 | filename = os.path.join(os.path.dirname(base), href) | 28 | filename = os.path.join(os.path.dirname(base), href) | ||
27 | subdom = xml.dom.minidom.parse(filename) | 29 | subdom = xml.dom.minidom.parse(filename) | ||
28 | xincludate(subdom, filename, dropns) | 30 | xincludate(subdom, filename, dropns) | ||
29 | if './' in href: | 31 | if './' in href: | ||
30 | subdom.documentElement.setAttribute('xml:base', href) | 32 | subdom.documentElement.setAttribute('xml:base', href) | ||
31 | include.parentNode.replaceChild(subdom.documentElement, include) | 33 | include.parentNode.replaceChild(subdom.documentElement, include) | ||
32 | 34 | | |||
33 | if __name__ == '__main__': | 35 | if __name__ == '__main__': | ||
34 | argv = argv[1:] | 36 | argv = argv[1:] | ||
35 | dom = xml.dom.minidom.parse(argv[0]) | 37 | dom = xml.dom.minidom.parse(argv[0]) | ||
36 | xincludate(dom, argv[0]) | 38 | xincludate(dom, argv[0]) | ||
37 | xml = dom.toxml() | 39 | | ||
38 | stdout.write(xml) | 40 | if sys.version_info[0] >= 3: | ||
39 | stdout.write('\n') | 41 | xml = dom.toxml('utf-8') | ||
42 | stdout.buffer.write(xml) | ||||
43 | stdout.buffer.write(b'\n') | ||||
44 | else: | ||||
45 | xml = dom.toxml() | ||||
46 | stdout.write(xml) | ||||
47 | stdout.write('\n') |