Merge branch 'master' of ansgar.seatribe.se:/usr/local/src/wrw
[wrw.git] / wrw / sp / cons.py
index 9df4725..d8b30a8 100644 (file)
@@ -1,48 +1,52 @@
+import sys, collections
 import xml.dom.minidom
 
 class node(object):
-    def __str__(self):
-        doc = xml.dom.minidom.Document()
-        return self.__todom__(doc).toxml()
+    pass
 
-class text(node, unicode):
+class text(node, str):
     def __todom__(self, doc):
         return doc.createTextNode(self)
 
-class raw(node, unicode):
+class raw(node, str):
     def __todom__(self, doc):
         raise Exception("Cannot convert raw code to DOM objects")
 
 class element(node):
     def __init__(self, ns, name, ctx):
         self.ns = ns
-        self.name = unicode(name)
+        self.name = str(name)
         self.ctx = ctx
         self.attrs = {}
         self.children = []
 
     def __call__(self, *children, **attrs):
         for child in children:
-            self.children.append(self.ctx.nodefrom(child))
-        for k, v in attrs.iteritems():
-            self.attrs[unicode(k)] = unicode(v)
+            self.ctx.addchild(self, child)
+        for k, v in attrs.items():
+            self.ctx.addattr(self, k, v)
         return self
 
     def __todom__(self, doc):
         el = doc.createElementNS(self.ns, self.name)
-        for k, v in self.attrs.iteritems():
+        for k, v in self.attrs.items():
             el.setAttribute(k, v)
         for child in self.children:
             el.appendChild(child.__todom__(doc))
         return el
 
+    def __str__(self):
+        doc = xml.dom.minidom.Document()
+        return self.__todom__(doc).toxml()
+
 class context(object):
+    charset = (sys.getfilesystemencoding() or "ascii")
+
     def __init__(self):
         self.nodeconv = {}
-        self.nodeconv[str] = lambda ob: text(ob, "utf-8")
-        self.nodeconv[unicode] = text
+        self.nodeconv[bytes] = lambda ob: text(ob, self.charset)
+        self.nodeconv[str] = text
         self.nodeconv[int] = text
-        self.nodeconv[long] = text
         self.nodeconv[float] = text
 
     def nodefrom(self, ob):
@@ -52,10 +56,26 @@ class context(object):
             return ob.__tonode__()
         if type(ob) in self.nodeconv:
             return self.nodeconv[type(ob)](ob)
-        raise Exception("No node conversion known for %s objects" % str(type(ob)))
+        return None
+
+    def addchild(self, node, child):
+        if child is None:
+            return
+        new = self.nodefrom(child)
+        if new is not None:
+            node.children.append(new)
+        elif isinstance(child, collections.Iterable):
+            for ch in child:
+                self.addchild(node, ch)
+        else:
+            raise Exception("No node conversion known for %s objects" % str(type(child)))
+
+    def addattr(self, node, k, v):
+        if v is not None:
+            node.attrs[str(k)] = str(v)
 
 class constructor(object):
-    def __init__(self, ns, elcls = element, ctx=None):
+    def __init__(self, ns, elcls=element, ctx=None):
         self._ns = ns
         self._elcls = elcls
         if ctx is None: ctx = context()
@@ -63,3 +83,9 @@ class constructor(object):
 
     def __getattr__(self, name):
         return self._elcls(self._ns, name, self._ctx)
+
+class doctype(node):
+    def __init__(self, rootname, pubid, dtdid):
+        self.rootname = rootname
+        self.pubid = pubid
+        self.dtdid = dtdid