5 def process_file(filename, obj, clean = True):
6 doc = xml.dom.minidom.parse(filename)
7 process(doc, obj, clean)
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 while not node.firstChild is None:
18 node.removeChild(node.firstChild)
20 if isinstance(obj, xml.dom.minidom.Document):
21 obj = obj.documentElement
23 newobj = obj.cloneNode(True)
24 node.appendChild(newobj)
26 process(newobj, {}, clean)
27 elif isinstance(obj, dict): # substitute
28 for child in node.childNodes:
31 if child.nodeType == xml.dom.Node.ELEMENT_NODE:
34 attrs = child.attributes
37 for i in range(attrs.length):
39 if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id":
42 attrs_to_remove.append(attr.name)
43 if attr.name.startswith("xmlns:") and attr.value == "http://template.sesse.net/" and clean:
44 attrs_to_remove.append(attr.name)
46 for a in attrs_to_remove:
47 if child.hasAttribute(a):
48 child.removeAttribute(a)
51 # check all substitutions to see if we found anything
53 for key in obj.keys():
54 if key.startswith(child.tagName + "/"):
55 child.setAttribute(key.split("/")[1], obj[key])
56 elif (not id is None) and key.startswith("#" + id + "/"):
57 child.setAttribute(key.split("/")[1], obj[key])
60 if key == child.localName or ((not id is None) and key == "#" + id):
61 process(child, obj[key], clean)
65 process(child, obj, clean)
66 elif isinstance(obj, list): # repeat
67 doc = _get_document_element(node)
68 frag = doc.createElement("temporary-fragment") # ugh
70 while not node.firstChild is None:
71 child = node.firstChild
72 node.removeChild(child)
73 frag.appendChild(child)
76 newnode = frag.cloneNode(True)
77 node.appendChild(newnode)
78 process(newnode, instance, clean)
82 # remove all the <fragment> tags
84 children_to_remove = []
85 for child in node.childNodes:
86 if isinstance(child, xml.dom.minidom.Element) and child.tagName == 'temporary-fragment':
87 while not child.firstChild is None:
88 child2 = child.firstChild
89 child.removeChild(child2)
90 node.appendChild(child2)
91 children_to_remove.append(child)
93 for child in children_to_remove:
94 node.removeChild(child)
99 def alternate(tag, array, *elems):
102 ref[tag] = elems[i % len(elems)]
108 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.namespaceURI == "http://template.sesse.net/":
109 # as this is a dummy node, we want to remove it and move everything further up
110 # after we've done any required replacements
111 doc = _get_document_element(node)
112 parent = node.parentNode
114 while not node.firstChild is None:
115 child = node.firstChild
116 node.removeChild(child)
117 parent.insertBefore(child, node)
119 parent.removeChild(node)
122 def _get_document_element(node):
123 if node.parentNode is None:
126 return _get_document_element(node.parentNode)