Always handle I/O errors in util.formparams.
[wrw.git] / wrw / sp / cons.py
... / ...
CommitLineData
1import sys
2import xml.dom.minidom
3
4class node(object):
5 pass
6
7class text(node, str):
8 def __todom__(self, doc):
9 return doc.createTextNode(self)
10
11class raw(node, str):
12 def __todom__(self, doc):
13 raise Exception("Cannot convert raw code to DOM objects")
14
15class element(node):
16 def __init__(self, ns, name, ctx):
17 self.ns = ns
18 self.name = str(name)
19 self.ctx = ctx
20 self.attrs = {}
21 self.children = []
22
23 def __call__(self, *children, **attrs):
24 for child in children:
25 self.ctx.addchild(self, child)
26 for k, v in attrs.items():
27 self.ctx.addattr(self, k, v)
28 return self
29
30 def __todom__(self, doc):
31 el = doc.createElementNS(self.ns, self.name)
32 for k, v in self.attrs.items():
33 el.setAttribute(k, v)
34 for child in self.children:
35 el.appendChild(child.__todom__(doc))
36 return el
37
38 def __str__(self):
39 doc = xml.dom.minidom.Document()
40 return self.__todom__(doc).toxml()
41
42class context(object):
43 charset = (sys.getfilesystemencoding() or "ascii")
44
45 def __init__(self):
46 self.nodeconv = {}
47 self.nodeconv[bytes] = lambda ob: text(ob, self.charset)
48 self.nodeconv[str] = text
49 self.nodeconv[int] = text
50 self.nodeconv[float] = text
51
52 def nodefrom(self, ob):
53 if isinstance(ob, node):
54 return ob
55 if hasattr(ob, "__tonode__"):
56 return ob.__tonode__()
57 if type(ob) in self.nodeconv:
58 return self.nodeconv[type(ob)](ob)
59 raise Exception("No node conversion known for %s objects" % str(type(ob)))
60
61 def addchild(self, node, child):
62 if child is None:
63 return
64 node.children.append(self.nodefrom(child))
65
66 def addattr(self, node, k, v):
67 if v is not None:
68 node.attrs[str(k)] = str(v)
69
70class constructor(object):
71 def __init__(self, ns, elcls=element, ctx=None):
72 self._ns = ns
73 self._elcls = elcls
74 if ctx is None: ctx = context()
75 self._ctx = ctx
76
77 def __getattr__(self, name):
78 return self._elcls(self._ns, name, self._ctx)
79
80class doctype(node):
81 def __init__(self, rootname, pubid, dtdid):
82 self.rootname = rootname
83 self.pubid = pubid
84 self.dtdid = dtdid