comparison orpg/xmltramp.py @ 23:551cd440acce traipse_dev

Final Dev build of the Day. Should reduce RAM usage more.
author sirebral
date Sat, 25 Jul 2009 21:05:18 -0500
parents 4385a7d0efd1
children c88a794d9a22
comparison
equal deleted inserted replaced
22:88cea66228d6 23:551cd440acce
35 else: self._dNS = None 35 else: self._dNS = None
36 36
37 def __repr__(self, recursive=0, multiline=0, inprefixes=None): 37 def __repr__(self, recursive=0, multiline=0, inprefixes=None):
38 def qname(name, inprefixes): 38 def qname(name, inprefixes):
39 if islst(name): 39 if islst(name):
40 if inprefixes[name[0]] is not None: 40 if inprefixes[name[0]] is not None: return inprefixes[name[0]]+':'+name[1]
41 return inprefixes[name[0]]+':'+name[1] 41 else: return name[1]
42 else: 42 else: return name
43 return name[1]
44 else:
45 return name
46 43
47 def arep(a, inprefixes, addns=1): 44 def arep(a, inprefixes, addns=1):
48 out = '' 45 out = ''
49 for p in self._prefixes.keys(): 46 for p in self._prefixes.keys():
50 if not p in inprefixes.keys(): 47 if not p in inprefixes.keys():
65 out += ' />' 62 out += ' />'
66 return out 63 return out
67 out += '>' 64 out += '>'
68 if recursive: 65 if recursive:
69 content = 0 66 content = 0
70 for x in self._dir: 67 for x in self._dir: if isinstance(x, Element): content = 1
71 if isinstance(x, Element): content = 1
72 pad = '\n' + ('\t' * recursive) 68 pad = '\n' + ('\t' * recursive)
73 for x in self._dir: 69 for x in self._dir:
74 if multiline and content: out += pad 70 if multiline and content: out += pad
75 if isstr(x): out += quote(x) 71 if isstr(x): out += quote(x)
76 elif isinstance(x, Element): 72 elif isinstance(x, Element): out += x.__repr__(recursive+1, multiline, inprefixes.copy())
77 out += x.__repr__(recursive+1, multiline, inprefixes.copy()) 73 else: raise TypeError, "I wasn't expecting "+`x`+"."
78 else:
79 raise TypeError, "I wasn't expecting "+`x`+"."
80 if multiline and content: out += '\n' + ('\t' * (recursive-1)) 74 if multiline and content: out += '\n' + ('\t' * (recursive-1))
81 else: 75 else: if self._dir: out += '...'
82 if self._dir: out += '...'
83 out += '</'+qname(self._name, inprefixes)+'>' 76 out += '</'+qname(self._name, inprefixes)+'>'
84 return out 77 return out
85 78
86 def __unicode__(self): 79 def __unicode__(self):
87 text = '' 80 text = ''
88 for x in self._dir: 81 for x in self._dir: text += unicode(x)
89 text += unicode(x)
90 return ' '.join(text.split()) 82 return ' '.join(text.split())
91 83
92 def __str__(self): 84 def __str__(self):
93 return self.__unicode__().encode('utf-8') 85 return self.__unicode__().encode('utf-8')
94 86
168 160
169 def __call__(self, *_pos, **_set): 161 def __call__(self, *_pos, **_set):
170 if _set: 162 if _set:
171 for k in _set.keys(): self._attrs[k] = _set[k] 163 for k in _set.keys(): self._attrs[k] = _set[k]
172 if len(_pos) > 1: 164 if len(_pos) > 1:
173 for i in range(0, len(_pos), 2): 165 for i in range(0, len(_pos), 2): self._attrs[_pos[i]] = _pos[i+1]
174 self._attrs[_pos[i]] = _pos[i+1] 166 if len(_pos) == 1 is not None: return self._attrs[_pos[0]]
175 if len(_pos) == 1 is not None: 167 if len(_pos) == 0: return self._attrs
176 return self._attrs[_pos[0]]
177 if len(_pos) == 0:
178 return self._attrs
179 168
180 def __len__(self): return len(self._dir) 169 def __len__(self): return len(self._dir)
181 170
182 class Namespace: 171 class Namespace:
183 def __init__(self, uri): self.__uri = uri 172 def __init__(self, uri): self.__uri = uri
212 201
213 def endElementNS(self, name, qname): 202 def endElementNS(self, name, qname):
214 ch = self.ch; self.ch = '' 203 ch = self.ch; self.ch = ''
215 if ch and not ch.isspace(): self.stack[-1]._dir.append(ch) 204 if ch and not ch.isspace(): self.stack[-1]._dir.append(ch)
216 element = self.stack.pop() 205 element = self.stack.pop()
217 if self.stack: 206 if self.stack: self.stack[-1]._dir.append(element)
218 self.stack[-1]._dir.append(element) 207 else: self.result = element
219 else:
220 self.result = element
221 208
222 from xml.sax import make_parser 209 from xml.sax import make_parser
223 from xml.sax.handler import feature_namespaces 210 from xml.sax.handler import feature_namespaces
224 211
225 def seed(fileobj): 212 def seed(fileobj):