]> git.sesse.net Git - xml-template/commitdiff
Write enough of the Python version to make it pass simple.
authorsgunderson@bigfoot.com <>
Wed, 6 Sep 2006 15:28:49 +0000 (17:28 +0200)
committersgunderson@bigfoot.com <>
Wed, 6 Sep 2006 15:28:49 +0000 (17:28 +0200)
python/xmltemplate.py

index 5922ba637652912cd924951ad3efb206085135cf..83a9e0efe0cd522f318911b1c01f5a6fcf05ba79 100644 (file)
@@ -1,13 +1,60 @@
 #! /usr/bin/python
+import re
 import xml.dom.minidom
 
-def process_file(filename, obj, clean = False):
+def process_file(filename, obj, clean = True):
        doc = xml.dom.minidom.parse(filename)
        process(doc, obj, clean)
        return doc
 
-def process(node, obj, clean):
-       pass
+def process(node, obj, clean = True):
+       if isinstance(obj, str):                        # overwrite
+               for child in node.childNodes:
+                       node.removeChild(child)
+               doc = _get_document_element(node)
+               node.appendChild(doc.createTextNode(obj))
+       elif isinstance(obj, xml.dom.Node):             # overwrite
+               pass
+       elif isinstance(obj, dict):                     # substitute
+               for child in node.childNodes:
+                       processed = False
+
+                       if child.nodeType == xml.dom.Node.ELEMENT_NODE:
+                               id = None
+
+                               attrs = child.attributes
+                               if not attrs is None:
+                                       for i in range(attrs.length):
+                                               attr = attrs.item(0)
+                                               if attr.namespaceURI == "http://template.sesse.net/" or attr.name == "id":
+                                                       id = attr.value
+                                                       if clean:
+                                                               child.removeAttribute(attr.name)
+
+                               # check all substitutions to see if we found anything
+                               # appropriate
+                               for key in obj.keys():
+                                       if key.startswith(child.tagName + "/"):
+                                               child.setAttribute(key.split("/")[1], obj[key])
+                                       elif (not id is None) and key.startswith("#" + id + "/"):
+                                               child.setAttribute(key.split("/")[1], obj[key])
+
+                                       if not processed:
+                                               if key == child.tagName or ((not id is None) and key == "#" + id):
+                                                       process(child, obj[key], clean)
+                                                       processed = True
+
+                       if not processed:
+                               process(child, obj, clean)
+
+
 
 def alternate(tag, array, *elems):
        return array
+
+# ugh
+def _get_document_element(node):
+       if node.parentNode is None:
+               return node
+       else:
+               return _get_document_element(node.parentNode)