summaryrefslogtreecommitdiff
path: root/tools/xincludator.py
diff options
context:
space:
mode:
authorOlivier CrĂȘte <olivier.crete@collabora.co.uk>2010-08-15 09:21:23 -0400
committerOlivier CrĂȘte <olivier.crete@collabora.co.uk>2010-10-31 14:46:10 -0400
commit0847b44e37bdfc01ea937cdc542ecd880d503c14 (patch)
tree3e726beaf1af4d0b587fda1c86d69cec8e0d758b /tools/xincludator.py
parent0c471fe55ac4bbedcc4a1acccb0cc2477a7279f1 (diff)
downloadtelepathy-farstream-0847b44e37bdfc01ea937cdc542ecd880d503c14.tar.gz
Import code generator from tp-glib
Diffstat (limited to 'tools/xincludator.py')
-rw-r--r--tools/xincludator.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/tools/xincludator.py b/tools/xincludator.py
new file mode 100644
index 0000000..63e106a
--- /dev/null
+++ b/tools/xincludator.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+
+from sys import argv, stdout, stderr
+import codecs, locale
+import os
+import xml.dom.minidom
+
+stdout = codecs.getwriter('utf-8')(stdout)
+
+NS_XI = 'http://www.w3.org/2001/XInclude'
+
+def xincludate(dom, base, dropns = []):
+ remove_attrs = []
+ for i in xrange(dom.documentElement.attributes.length):
+ attr = dom.documentElement.attributes.item(i)
+ if attr.prefix == 'xmlns':
+ if attr.localName in dropns:
+ remove_attrs.append(attr)
+ else:
+ dropns.append(attr.localName)
+ for attr in remove_attrs:
+ dom.documentElement.removeAttributeNode(attr)
+ for include in dom.getElementsByTagNameNS(NS_XI, 'include'):
+ href = include.getAttribute('href')
+ # FIXME: assumes Unixy paths
+ filename = os.path.join(os.path.dirname(base), href)
+ subdom = xml.dom.minidom.parse(filename)
+ xincludate(subdom, filename, dropns)
+ if './' in href:
+ subdom.documentElement.setAttribute('xml:base', href)
+ include.parentNode.replaceChild(subdom.documentElement, include)
+
+if __name__ == '__main__':
+ argv = argv[1:]
+ dom = xml.dom.minidom.parse(argv[0])
+ xincludate(dom, argv[0])
+ xml = dom.toxml()
+ stdout.write(xml)
+ stdout.write('\n')