Merge branch 'master' into python3
[wrw.git] / wrw / sp / cons.py
1 import xml.dom.minidom
2
3 class node(object):
4     def __str__(self):
5         doc = xml.dom.minidom.Document()
6         return self.__todom__(doc).toxml()
7
8 class text(node, str):
9     def __todom__(self, doc):
10         return doc.createTextNode(self)
11
12 class element(node):
13     def __init__(self, ns, name, ctx):
14         self.ns = ns
15         self.name = str(name)
16         self.ctx = ctx
17         self.attrs = {}
18         self.children = []
19
20     def __call__(self, *children, **attrs):
21         for child in children:
22             self.children.append(self.ctx.nodefrom(child))
23         for k, v in attrs.items():
24             self.attrs[str(k)] = str(v)
25         return self
26
27     def __todom__(self, doc):
28         el = doc.createElementNS(self.ns, self.name)
29         for k, v in self.attrs.items():
30             el.setAttribute(k, v)
31         for child in self.children:
32             el.appendChild(child.__todom__(doc))
33         return el
34
35 class context(object):
36     def __init__(self):
37         self.nodeconv = {}
38         self.nodeconv[bytes] = lambda ob: text(ob, "utf-8")
39         self.nodeconv[str] = text
40         self.nodeconv[int] = text
41         self.nodeconv[float] = text
42
43     def nodefrom(self, ob):
44         if isinstance(ob, node):
45             return ob
46         if hasattr(ob, "__tonode__"):
47             return ob.__tonode__()
48         if type(ob) in self.nodeconv:
49             return self.nodeconv[type(ob)](ob)
50         raise Exception("No node conversion known for %s objects" % str(type(ob)))
51
52 class constructor(object):
53     def __init__(self, ns, elcls = element, ctx=None):
54         self._ns = ns
55         self._elcls = elcls
56         if ctx is None: ctx = context()
57         self._ctx = ctx
58
59     def __getattr__(self, name):
60         return self._elcls(self._ns, name, self._ctx)