comparison orpg/pulldom.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
comparison
equal deleted inserted replaced
22:88cea66228d6 23:551cd440acce
1 import minidom 1 import minidom
2 import xml.sax,xml.sax.handler 2 import xml.sax,xml.sax.handler
3 import types 3 import types
4 4
5 try: 5 try: from cStringIO import StringIO
6 from cStringIO import StringIO 6 except ImportError: from StringIO import StringIO
7 except ImportError: 7
8 from StringIO import StringIO 8 try: _StringTypes = [types.StringType, types.UnicodeType]
9 9 except AttributeError: _StringTypes = [types.StringType]
10 try:
11 _StringTypes = [types.StringType, types.UnicodeType]
12 except AttributeError:
13 _StringTypes = [types.StringType]
14 10
15 START_ELEMENT = "START_ELEMENT" 11 START_ELEMENT = "START_ELEMENT"
16 END_ELEMENT = "END_ELEMENT" 12 END_ELEMENT = "END_ELEMENT"
17 COMMENT = "COMMENT" 13 COMMENT = "COMMENT"
18 START_DOCUMENT = "START_DOCUMENT" 14 START_DOCUMENT = "START_DOCUMENT"
41 uri,localname = name 37 uri,localname = name
42 if uri: 38 if uri:
43 # When using namespaces, the reader may or may not 39 # When using namespaces, the reader may or may not
44 # provide us with the original name. If not, create 40 # provide us with the original name. If not, create
45 # *a* valid tagName from the current context. 41 # *a* valid tagName from the current context.
46 if tagName is None: 42 if tagName is None: tagName = self._current_context[uri] + ":" + localname
47 tagName = self._current_context[uri] + ":" + localname
48 node = self.document.createElementNS(uri, tagName) 43 node = self.document.createElementNS(uri, tagName)
49 else: 44 else:
50 # When the tagname is not prefixed, it just appears as 45 # When the tagname is not prefixed, it just appears as
51 # localname 46 # localname
52 node = self.document.createElement(localname) 47 node = self.document.createElement(localname)
53 for aname,value in attrs.items(): 48 for aname,value in attrs.items():
54 a_uri, a_localname = aname 49 a_uri, a_localname = aname
55 if a_uri: 50 if a_uri:
56 qname = self._current_context[a_uri] + ":" + a_localname 51 qname = self._current_context[a_uri] + ":" + a_localname
57 attr = self.document.createAttributeNS(a_uri, qname) 52 attr = self.document.createAttributeNS(a_uri, qname)
58 else: 53 else: attr = self.document.createAttribute(a_localname)
59 attr = self.document.createAttribute(a_localname)
60 attr.value = value 54 attr.value = value
61 node.setAttributeNode(attr) 55 node.setAttributeNode(attr)
62 parent = self.curNode 56 parent = self.curNode
63 node.parentNode = parent 57 node.parentNode = parent
64 self.curNode = node 58 self.curNode = node
132 #self.events.append((START_DOCUMENT, node)) 126 #self.events.append((START_DOCUMENT, node))
133 127
134 def endDocument(self): 128 def endDocument(self):
135 assert not self.curNode.parentNode 129 assert not self.curNode.parentNode
136 for node in self.curNode.childNodes: 130 for node in self.curNode.childNodes:
137 if node.nodeType == node.ELEMENT_NODE: 131 if node.nodeType == node.ELEMENT_NODE: self.document.documentElement = node
138 self.document.documentElement = node
139 #if not self.document.documentElement: 132 #if not self.document.documentElement:
140 # raise Error, "No document element" 133 # raise Error, "No document element"
141 self.lastEvent[1] = [(END_DOCUMENT, node), None] 134 self.lastEvent[1] = [(END_DOCUMENT, node), None]
142 #self.events.append((END_DOCUMENT, self.curNode)) 135 #self.events.append((END_DOCUMENT, self.curNode))
143 136
162 self.parser.setFeature(xml.sax.handler.feature_namespaces,1) 155 self.parser.setFeature(xml.sax.handler.feature_namespaces,1)
163 self.parser.setContentHandler(self.pulldom) 156 self.parser.setContentHandler(self.pulldom)
164 157
165 def __getitem__(self, pos): 158 def __getitem__(self, pos):
166 rc = self.getEvent() 159 rc = self.getEvent()
167 if rc: 160 if rc: return rc
168 return rc
169 raise IndexError 161 raise IndexError
170 162
171 def expandNode(self, node): 163 def expandNode(self, node):
172 event = self.getEvent() 164 event = self.getEvent()
173 while event: 165 while event:
174 token, cur_node = event 166 token, cur_node = event
175 if cur_node is node: 167 if cur_node is node: return
176 return 168 if token != END_ELEMENT: cur_node.parentNode.appendChild(cur_node)
177 if token != END_ELEMENT:
178 cur_node.parentNode.appendChild(cur_node)
179 event = self.getEvent() 169 event = self.getEvent()
180 170
181 def getEvent(self): 171 def getEvent(self):
182 if not self.pulldom.firstEvent[1]: 172 if not self.pulldom.firstEvent[1]: self.pulldom.lastEvent = self.pulldom.firstEvent
183 self.pulldom.lastEvent = self.pulldom.firstEvent
184 while not self.pulldom.firstEvent[1]: 173 while not self.pulldom.firstEvent[1]:
185 buf=self.stream.read(self.bufsize) 174 buf=self.stream.read(self.bufsize)
186 if not buf: 175 if not buf:
187 #FIXME: why doesn't Expat close work? 176 #FIXME: why doesn't Expat close work?
188 #self.parser.close() 177 #self.parser.close()
218 node.parentNode.appendChild(node) 207 node.parentNode.appendChild(node)
219 208
220 default_bufsize = (2 ** 14) - 20 209 default_bufsize = (2 ** 14) - 20
221 210
222 def parse(stream_or_string, parser=None, bufsize=default_bufsize): 211 def parse(stream_or_string, parser=None, bufsize=default_bufsize):
223 if type(stream_or_string) in _StringTypes: 212 if type(stream_or_string) in _StringTypes: stream = open(stream_or_string)
224 stream = open(stream_or_string) 213 else: stream = stream_or_string
225 else: 214 if not parser: parser = xml.sax.make_parser()
226 stream = stream_or_string
227 if not parser:
228 parser = xml.sax.make_parser()
229 return DOMEventStream(stream, parser, bufsize) 215 return DOMEventStream(stream, parser, bufsize)
230 216
231 def parseString(string, parser=None): 217 def parseString(string, parser=None):
232 bufsize = len(string) 218 bufsize = len(string)
233 buf = StringIO(string) 219 buf = StringIO(string)
234 if not parser: 220 if not parser: parser = xml.sax.make_parser()
235 parser = xml.sax.make_parser()
236 return DOMEventStream(buf, parser, bufsize) 221 return DOMEventStream(buf, parser, bufsize)