23 """ |
23 """ |
24 self.startDocumentSpecific = None |
24 self.startDocumentSpecific = None |
25 |
25 |
26 # TODO: add support for bytes, bytearray, set, frozenset |
26 # TODO: add support for bytes, bytearray, set, frozenset |
27 self.elements = { |
27 self.elements = { |
28 'none' : (self.defaultStartElement, self.endNone), |
28 'none' : (self.defaultStartElement, self.endNone), |
29 'int' : (self.defaultStartElement, self.endInt), |
29 'int' : (self.defaultStartElement, self.endInt), |
30 'float' : (self.defaultStartElement, self.endFloat), |
30 'float' : (self.defaultStartElement, self.endFloat), |
31 'complex' : (self.defaultStartElement, self.endComplex), |
31 'complex' : (self.defaultStartElement, self.endComplex), |
32 'bool' : (self.defaultStartElement, self.endBool), |
32 'bool' : (self.defaultStartElement, self.endBool), |
33 'string' : (self.defaultStartElement, self.endString), |
33 'string' : (self.defaultStartElement, self.endString), |
34 'tuple' : (self.startTuple, self.endTuple), |
34 'bytes' : (self.defaultStartElement, self.endBytes), |
35 'list' : (self.startList, self.endList), |
35 'bytearray' : (self.defaultStartElement, self.endBytearray), |
36 'dict' : (self.startDictionary, self.endDictionary), |
36 'tuple' : (self.startTuple, self.endTuple), |
37 'pickle' : (self.startPickle, self.endPickle), |
37 'list' : (self.startList, self.endList), |
|
38 'dict' : (self.startDictionary, self.endDictionary), |
|
39 'set' : (self.startSet, self.endSet), |
|
40 'frozenset' : (self.startFrozenset, self.endFrozenset), |
|
41 'pickle' : (self.startPickle, self.endPickle), |
38 # for backward compatibility |
42 # for backward compatibility |
39 'long' : (self.defaultStartElement, self.endInt), |
43 'long' : (self.defaultStartElement, self.endInt), |
40 'unicode' : (self.defaultStartElement, self.endString), |
44 'unicode' : (self.defaultStartElement, self.endString), |
41 } |
45 } |
42 |
46 |
43 self.buffer = "" |
47 self.buffer = "" |
44 self.stack = [] |
48 self.stack = [] |
45 self._marker = '__MARKER__' |
49 self._marker = '__MARKER__' |
181 Handler method for the "string" end tag. |
185 Handler method for the "string" end tag. |
182 """ |
186 """ |
183 s = str(self.utf8_to_code(self.unescape(self.buffer))) |
187 s = str(self.utf8_to_code(self.unescape(self.buffer))) |
184 self.stack.append(s) |
188 self.stack.append(s) |
185 |
189 |
|
190 def endBytes(self): |
|
191 """ |
|
192 Handler method for the "bytes" end tag. |
|
193 """ |
|
194 by = bytes([int(b) for b in self.buffer.strip().split(",")]) |
|
195 self.stack.append(by) |
|
196 |
|
197 def endBytearray(self): |
|
198 """ |
|
199 Handler method for the "bytearray" end tag. |
|
200 """ |
|
201 by = bytearray([int(b) for b in self.buffer.strip().split(",")]) |
|
202 self.stack.append(by) |
186 ## def endUnicode(self): |
203 ## def endUnicode(self): |
187 ## """ |
204 ## """ |
188 ## Handler method for the "unicode" end tag. |
205 ## Handler method for the "unicode" end tag. |
189 ## """ |
206 ## """ |
190 ## u = str(self.utf8_to_code(self.unescape(self.buffer))) |
207 ## u = str(self.utf8_to_code(self.unescape(self.buffer))) |
195 Handler method for the "list" start tag. |
212 Handler method for the "list" start tag. |
196 |
213 |
197 @param attrs list of tag attributes |
214 @param attrs list of tag attributes |
198 """ |
215 """ |
199 self.stack.append(self._marker) |
216 self.stack.append(self._marker) |
200 self.stack.append([]) |
|
201 |
217 |
202 def endList(self): |
218 def endList(self): |
203 """ |
219 """ |
204 Handler method for the "list" end tag. |
220 Handler method for the "list" end tag. |
205 """ |
221 """ |
206 for i in range(len(self.stack) - 1, -1, -1): |
222 for i in range(len(self.stack) - 1, -1, -1): |
207 if self.stack[i] is self._marker: |
223 if self.stack[i] is self._marker: |
208 break |
224 break |
209 assert i != -1 |
225 assert i != -1 |
210 l = self.stack[i + 1] |
226 l = self.stack[i + 1:len(self.stack)] |
211 l[:] = self.stack[i + 2:len(self.stack)] |
|
212 self.stack[i:] = [l] |
227 self.stack[i:] = [l] |
213 |
228 |
214 def startTuple(self, attrs): |
229 def startTuple(self, attrs): |
215 """ |
230 """ |
216 Handler method for the "tuple" start tag. |
231 Handler method for the "tuple" start tag. |
235 Handler method for the "dictionary" start tag. |
250 Handler method for the "dictionary" start tag. |
236 |
251 |
237 @param attrs list of tag attributes |
252 @param attrs list of tag attributes |
238 """ |
253 """ |
239 self.stack.append(self._marker) |
254 self.stack.append(self._marker) |
240 self.stack.append({}) |
|
241 |
255 |
242 def endDictionary(self): |
256 def endDictionary(self): |
243 """ |
257 """ |
244 Handler method for the "dictionary" end tag. |
258 Handler method for the "dictionary" end tag. |
245 """ |
259 """ |
246 for i in range(len(self.stack) - 1, -1, -1): |
260 for i in range(len(self.stack) - 1, -1, -1): |
247 if self.stack[i] is self._marker: |
261 if self.stack[i] is self._marker: |
248 break |
262 break |
249 assert i != -1 |
263 assert i != -1 |
250 d = self.stack[i + 1] |
264 d = {} |
251 for j in range(i + 2, len(self.stack), 2): |
265 for j in range(i + 1, len(self.stack), 2): |
252 d[self.stack[j]] = self.stack[j + 1] |
266 d[self.stack[j]] = self.stack[j + 1] |
253 self.stack[i:] = [d] |
267 self.stack[i:] = [d] |
254 |
268 |
|
269 def startSet(self, attrs): |
|
270 """ |
|
271 Handler method for the "set" start tag. |
|
272 |
|
273 @param attrs list of tag attributes |
|
274 """ |
|
275 self.stack.append(self._marker) |
|
276 |
|
277 def endSet(self): |
|
278 """ |
|
279 Handler method for the "set" end tag. |
|
280 """ |
|
281 for i in range(len(self.stack) - 1, -1, -1): |
|
282 if self.stack[i] is self._marker: |
|
283 break |
|
284 assert i != -1 |
|
285 s = set(self.stack[i + 1:len(self.stack)]) |
|
286 self.stack[i:] = [s] |
|
287 |
|
288 def startFrozenset(self, attrs): |
|
289 """ |
|
290 Handler method for the "frozenset" start tag. |
|
291 |
|
292 @param attrs list of tag attributes |
|
293 """ |
|
294 self.stack.append(self._marker) |
|
295 |
|
296 def endFrozenset(self): |
|
297 """ |
|
298 Handler method for the "frozenset" end tag. |
|
299 """ |
|
300 for i in range(len(self.stack) - 1, -1, -1): |
|
301 if self.stack[i] is self._marker: |
|
302 break |
|
303 assert i != -1 |
|
304 f = frozenset(self.stack[i + 1:len(self.stack)]) |
|
305 self.stack[i:] = [f] |
|
306 |
255 def startPickle(self, attrs): |
307 def startPickle(self, attrs): |
256 """ |
308 """ |
257 Handler method for the "pickle" start tag. |
309 Handler method for the "pickle" start tag. |
258 |
310 |
259 @param attrs list of tag attributes |
311 @param attrs list of tag attributes |