Mercurial > mez_xml
comparison tools.py @ 0:3679d2d8443a
Import from CVS and goto mez_xml-0.4
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 13 Feb 2008 22:33:51 +0800 |
parents | |
children | 1727286834b5 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3679d2d8443a |
---|---|
1 | |
2 class scope(object): | |
3 def __init__(self, temp, func, name): | |
4 super(scope, self).__init__() | |
5 self.temp = temp | |
6 self.name = name | |
7 self.func = func | |
8 pass | |
9 | |
10 def __enter__(self): | |
11 temp = self.temp | |
12 name = self.name | |
13 top = temp._nss[-1] | |
14 if top.has_key(name): | |
15 if isinstance(top[name], dict): | |
16 top[name] = [top[name]] | |
17 pass | |
18 if isinstance(top[name], list): | |
19 ntop = {} | |
20 top[name].append(ntop) | |
21 pass | |
22 pass | |
23 else: | |
24 ntop = top[name] = {} | |
25 pass | |
26 temp._nss.append(ntop) | |
27 return temp | |
28 | |
29 def __exit__(self, exc_type, exc_value, traceback): | |
30 self.temp._nss.pop() | |
31 pass | |
32 | |
33 def __call__(self, *args, **kws): | |
34 return self.func(self.temp, *args, **kws) | |
35 | |
36 def set_var(self, var): | |
37 if not isinstance(var, (dict, list, tuple)): | |
38 raise TypeError, 'var should be type of dict, list or tuple' | |
39 ntop = self.temp._nss[-1] | |
40 ntop[self.name] = var | |
41 pass | |
42 pass | |
43 | |
44 class scope_deco(object): | |
45 def __init__(self, name, func): | |
46 super(scope_deco, self).__init__() | |
47 self.func = func | |
48 self.name = name | |
49 pass | |
50 | |
51 def __get__(self, temp, owner): | |
52 return scope(temp, self.func, self.name) | |
53 | |
54 def __set__(self, temp, value): | |
55 top = temp._nss[-1] | |
56 top[self.name] = value | |
57 pass | |
58 pass | |
59 | |
60 class new_web_meta(type): | |
61 def __init__(clz, name, bases, dict): | |
62 super(new_web_meta, clz).__init__(name, bases, dict) | |
63 for name in dict.keys(): | |
64 if not name.startswith('_'): | |
65 func = dict[name] | |
66 setattr(clz, name, scope_deco(name, func)) | |
67 pass | |
68 pass | |
69 pass | |
70 | |
71 class nez_web(object): | |
72 __metaclass__ = new_web_meta | |
73 | |
74 def __init__(self): | |
75 super(nez_web, self).__init__() | |
76 self.safe = False | |
77 pass | |
78 | |
79 def __enter__(self): | |
80 self._rootns = {} | |
81 self._nss = [self._rootns] | |
82 self._committed = False | |
83 self._ready = False | |
84 pass | |
85 | |
86 def __exit__(self, exc_type, exc_value, traceback): | |
87 if self._committed: | |
88 self._root({}, self._rootns) | |
89 pass | |
90 self._ready = True | |
91 pass | |
92 | |
93 def _feed_subtree(self, temp, pdata, cdata): | |
94 if isinstance(cdata, dict): | |
95 data = dict(pdata) | |
96 data.update(cdata) | |
97 temp(data) | |
98 elif isinstance(cdata, list): | |
99 for d in cdata: | |
100 data = dict(pdata) | |
101 data.update(d) | |
102 temp(data) | |
103 pass | |
104 pass | |
105 else: | |
106 if self.safe: | |
107 pass | |
108 self.ofo.write(cdata) | |
109 pass | |
110 pass | |
111 | |
112 def _esc_param(self, data): | |
113 import types | |
114 if not (isinstance(data, types.StringTypes) and self.safe): | |
115 return str(data) | |
116 data = data.replace('&', '&') | |
117 data = data.replace('<', '<') | |
118 return str(data.replace('"', '')) | |
119 | |
120 def _esc_comm(self, data): | |
121 import types | |
122 if not (isinstance(data, types.StringTypes) and self.safe): | |
123 return str(data) | |
124 return str(data.replace('--', '-d;')) | |
125 | |
126 def _esc_text(self, data): | |
127 import types | |
128 if not (isinstance(data, types.StringTypes) and self.safe): | |
129 return str(data) | |
130 return str(data.replace('&', '&').replace('<', '<').replace('>', '>')) | |
131 | |
132 def _esc_cdata(self, data): | |
133 import types | |
134 if not (isinstance(data, types.StringTypes) and self.safe): | |
135 return str(data) | |
136 return str(data.replace(']]>', ']]>')) | |
137 | |
138 def commit(self): | |
139 assert not self._committed, '%s was committed more than one time.' % (repr(self),) | |
140 if self._ready: | |
141 self._root({}, self._rootns) | |
142 pass | |
143 self._committed = True | |
144 pass | |
145 | |
146 def gen_doc(self, data): | |
147 self._root({}, data) | |
148 pass | |
149 pass | |
150 |