]> git.sesse.net Git - xml-template/blob - python/xmltemplate.py
Fix a possible latent overwrite bug in the Python version.
[xml-template] / python / xmltemplate.py
1 #! /usr/bin/python
2 import re
3 import xml.dom.minidom
4
5 def process_file(filename, obj, clean = True):
6         doc = xml.dom.minidom.parse(filename)
7         process(doc, obj, clean)
8         return doc
9
10 def process(node, obj, clean = True):
11         if isinstance(obj, str):                        # overwrite
12                 while not node.firstChild is None:
13                         node.removeChild(node.firstChild)
14                 doc = _get_document_element(node)
15                 node.appendChild(doc.createTextNode(obj))
16         elif isinstance(obj, xml.dom.Node):             # overwrite
17                 pass
18         elif isinstance(obj, dict):                     # substitute
19                 for child in node.childNodes:
20                         processed = False
21
22                         if child.nodeType == xml.dom.Node.ELEMENT_NODE:
23                                 id = None
24
25                                 attrs = child.attributes
26                                 if not attrs is None:
27                                         for i in range(attrs.length):
28                                                 attr = attrs.item(0)
29                                                 if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id":
30                                                         id = attr.value
31                                                         if clean:
32                                                                 child.removeAttribute(attr.name)
33
34                                 # check all substitutions to see if we found anything
35                                 # appropriate
36                                 for key in obj.keys():
37                                         if key.startswith(child.tagName + "/"):
38                                                 child.setAttribute(key.split("/")[1], obj[key])
39                                         elif (not id is None) and key.startswith("#" + id + "/"):
40                                                 child.setAttribute(key.split("/")[1], obj[key])
41
42                                         if not processed:
43                                                 if key == child.localName or ((not id is None) and key == "#" + id):
44                                                         process(child, obj[key], clean)
45                                                         processed = True
46
47                         if not processed:
48                                 process(child, obj, clean)
49         elif isinstance(obj, list):                     # repeat
50                 doc = _get_document_element(node)
51                 frag = doc.createElement("temporary-fragment")     # ugh
52
53                 while not node.firstChild is None:
54                         child = node.firstChild
55                         node.removeChild(child)
56                         frag.appendChild(child)
57
58                 for instance in obj:
59                         newnode = frag.cloneNode(True)
60                         node.appendChild(newnode)
61                         process(newnode, instance, clean)
62                         if clean:
63                                 _clean(newnode)
64
65                 # remove all the <fragment> tags
66
67                 children_to_remove = []
68                 for child in node.childNodes:
69                         if isinstance(child, xml.dom.minidom.Element) and child.tagName == 'temporary-fragment':
70                                 while not child.firstChild is None:
71                                         child2 = child.firstChild
72                                         child.removeChild(child2)
73                                         node.appendChild(child2)
74                                 children_to_remove.append(child)
75
76                 for child in children_to_remove:
77                         node.removeChild(child)
78
79         if clean:
80                 _clean(node)
81
82 def alternate(tag, array, *elems):
83         return array
84
85 def _clean(node):
86         if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.namespaceURI == "http://template.sesse.net/":
87                 # as this is a dummy node, we want to remove it and move everything further up
88                 # after we've done any required replacements
89                 doc = _get_document_element(node)
90                 parent = node.parentNode
91
92                 while not node.firstChild is None:
93                         child = node.firstChild
94                         node.removeChild(child)
95                         parent.insertBefore(child, node)
96
97                 parent.removeChild(node)
98
99 # ugh
100 def _get_document_element(node):
101         if node.parentNode is None:
102                 return node
103         else:
104                 return _get_document_element(node.parentNode)