5 doc = xml.dom.minidom.Document()
6 return self.__todom__(doc).toxml()
9 def __todom__(self, doc):
10 return doc.createTextNode(self)
13 def __todom__(self, doc):
14 raise Exception("Cannot convert raw code to DOM objects")
17 def __init__(self, ns, name, ctx):
24 def __call__(self, *children, **attrs):
25 for child in children:
26 self.children.append(self.ctx.nodefrom(child))
27 for k, v in attrs.items():
28 self.attrs[str(k)] = str(v)
31 def __todom__(self, doc):
32 el = doc.createElementNS(self.ns, self.name)
33 for k, v in self.attrs.items():
35 for child in self.children:
36 el.appendChild(child.__todom__(doc))
39 class context(object):
42 self.nodeconv[bytes] = lambda ob: text(ob, "utf-8")
43 self.nodeconv[str] = text
44 self.nodeconv[int] = text
45 self.nodeconv[float] = text
47 def nodefrom(self, ob):
48 if isinstance(ob, node):
50 if hasattr(ob, "__tonode__"):
51 return ob.__tonode__()
52 if type(ob) in self.nodeconv:
53 return self.nodeconv[type(ob)](ob)
54 raise Exception("No node conversion known for %s objects" % str(type(ob)))
56 class constructor(object):
57 def __init__(self, ns, elcls = element, ctx=None):
60 if ctx is None: ctx = context()
63 def __getattr__(self, name):
64 return self._elcls(self._ns, name, self._ctx)