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, basestring): # 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/" and attr.localName == "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 hasattr(obj, '__iter__'): # 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 if instance is not None:
77 newnode = frag.cloneNode(True)
78 node.appendChild(newnode)
79 process(newnode, instance, clean)
83 # remove all the <fragment> tags
85 children_to_remove = []
86 for child in node.childNodes:
87 if isinstance(child, xml.dom.minidom.Element) and child.tagName == 'temporary-fragment':
88 while not child.firstChild is None:
89 child2 = child.firstChild
90 child.removeChild(child2)
91 node.appendChild(child2)
92 children_to_remove.append(child)
94 for child in children_to_remove:
95 node.removeChild(child)
100 def alternate(tag, array, *elems):
104 ref[tag] = elems[i % len(elems)]
110 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.namespaceURI == "http://template.sesse.net/":
111 # as this is a dummy node, we want to remove it and move everything further up
112 # after we've done any required replacements
113 doc = _get_document_element(node)
114 parent = node.parentNode
116 while not node.firstChild is None:
117 child = node.firstChild
118 node.removeChild(child)
119 parent.insertBefore(child, node)
121 parent.removeChild(node)
124 def _get_document_element(node):
125 if node.parentNode is None:
128 return _get_document_element(node.parentNode)