6 if sys.version_info[0] > 2:
12 def process_file(filename, obj, clean = True):
13 doc = xml.dom.minidom.parse(filename)
14 process(doc, obj, clean)
17 def process(node, obj, clean = True):
18 if isinstance(obj, string): # overwrite
19 while not node.firstChild is None:
20 node.removeChild(node.firstChild)
21 doc = _get_document_element(node)
22 node.appendChild(doc.createTextNode(obj))
23 elif isinstance(obj, xml.dom.Node): # overwrite
24 while not node.firstChild is None:
25 node.removeChild(node.firstChild)
27 if isinstance(obj, xml.dom.minidom.Document):
28 obj = obj.documentElement
30 newobj = obj.cloneNode(True)
31 node.appendChild(newobj)
33 process(newobj, {}, clean)
34 elif isinstance(obj, dict): # substitute
35 for child in node.childNodes:
38 if child.nodeType == xml.dom.Node.ELEMENT_NODE:
41 attrs = child.attributes
44 for i in range(attrs.length):
46 if attr.namespaceURI == "http://template.sesse.net/" and attr.localName == "id":
49 attrs_to_remove.append(attr.name)
50 if attr.name.startswith("xmlns:") and attr.value == "http://template.sesse.net/" and clean:
51 attrs_to_remove.append(attr.name)
53 for a in attrs_to_remove:
54 if child.hasAttribute(a):
55 child.removeAttribute(a)
58 # check all substitutions to see if we found anything
60 for key in obj.keys():
61 if key.startswith(child.tagName + "/"):
62 child.setAttribute(key.split("/")[1], obj[key])
63 elif (not id is None) and key.startswith("#" + id + "/"):
64 child.setAttribute(key.split("/")[1], obj[key])
67 if key == child.localName or ((not id is None) and key == "#" + id):
68 process(child, obj[key], clean)
72 process(child, obj, clean)
73 elif hasattr(obj, '__iter__'): # repeat
74 doc = _get_document_element(node)
75 frag = doc.createElement("temporary-fragment") # ugh
77 while not node.firstChild is None:
78 child = node.firstChild
79 node.removeChild(child)
80 frag.appendChild(child)
83 if instance is not None:
84 newnode = frag.cloneNode(True)
85 node.appendChild(newnode)
86 process(newnode, instance, clean)
90 # remove all the <fragment> tags
92 children_to_remove = []
93 for child in node.childNodes:
94 if isinstance(child, xml.dom.minidom.Element) and child.tagName == 'temporary-fragment':
95 while not child.firstChild is None:
96 child2 = child.firstChild
97 child.removeChild(child2)
98 node.appendChild(child2)
99 children_to_remove.append(child)
101 for child in children_to_remove:
102 node.removeChild(child)
107 def alternate(tag, array, *elems):
111 ref[tag] = elems[i % len(elems)]
117 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.namespaceURI == "http://template.sesse.net/":
118 # as this is a dummy node, we want to remove it and move everything further up
119 # after we've done any required replacements
120 doc = _get_document_element(node)
121 parent = node.parentNode
123 while not node.firstChild is None:
124 child = node.firstChild
125 node.removeChild(child)
126 parent.insertBefore(child, node)
128 parent.removeChild(node)
131 def _get_document_element(node):
132 if node.parentNode is None:
135 return _get_document_element(node.parentNode)