E4XML/XMLHandlerBase.py

changeset 45
9a18f4dbb493
parent 44
fe5cd20cb0eb
equal deleted inserted replaced
44:fe5cd20cb0eb 45:9a18f4dbb493
6 """ 6 """
7 Module implementing a base class for all of eric5s XML handlers. 7 Module implementing a base class for all of eric5s XML handlers.
8 """ 8 """
9 9
10 import sys 10 import sys
11 ##from types import UnicodeType 11 import pickle
12 import pickle as pickle 12 import base64
13 13
14 from xml.sax.handler import ContentHandler 14 from xml.sax.handler import ContentHandler
15 15
16 class XMLHandlerBase(ContentHandler): 16 class XMLHandlerBase(ContentHandler):
17 """ 17 """
21 """ 21 """
22 Constructor 22 Constructor
23 """ 23 """
24 self.startDocumentSpecific = None 24 self.startDocumentSpecific = None
25 25
26 # TODO: add support for bytes, bytearray, set, frozenset
27 self.elements = { 26 self.elements = {
28 'none' : (self.defaultStartElement, self.endNone), 27 'none' : (self.defaultStartElement, self.endNone),
29 'int' : (self.defaultStartElement, self.endInt), 28 'int' : (self.defaultStartElement, self.endInt),
30 'float' : (self.defaultStartElement, self.endFloat), 29 'float' : (self.defaultStartElement, self.endFloat),
31 'complex' : (self.defaultStartElement, self.endComplex), 30 'complex' : (self.defaultStartElement, self.endComplex),
49 self._marker = '__MARKER__' 48 self._marker = '__MARKER__'
50 49
51 self.NEWPARA = chr(0x2029) 50 self.NEWPARA = chr(0x2029)
52 self.NEWLINE = chr(0x2028) 51 self.NEWLINE = chr(0x2028)
53 52
54 def utf8_to_code(self, text):
55 """
56 Public method to convert a string to unicode and encode it for XML.
57
58 @param text the text to encode (string)
59 """
60 # TODO: convert calls to this method to not use it anymore
61 ## if not isinstance(text, UnicodeType):
62 ## text = str(text, "utf-8")
63 return text
64
65 def unescape(self, text, attribute = False): 53 def unescape(self, text, attribute = False):
66 """ 54 """
67 Public method used to unescape certain characters. 55 Public method used to unescape certain characters.
68 56
69 @param text the text to unescape (string) 57 @param text the text to unescape (string)
182 170
183 def endString(self): 171 def endString(self):
184 """ 172 """
185 Handler method for the "string" end tag. 173 Handler method for the "string" end tag.
186 """ 174 """
187 s = str(self.utf8_to_code(self.unescape(self.buffer))) 175 s = str(self.unescape(self.buffer))
188 self.stack.append(s) 176 self.stack.append(s)
189 177
190 def endBytes(self): 178 def endBytes(self):
191 """ 179 """
192 Handler method for the "bytes" end tag. 180 Handler method for the "bytes" end tag.
198 """ 186 """
199 Handler method for the "bytearray" end tag. 187 Handler method for the "bytearray" end tag.
200 """ 188 """
201 by = bytearray([int(b) for b in self.buffer.strip().split(",")]) 189 by = bytearray([int(b) for b in self.buffer.strip().split(",")])
202 self.stack.append(by) 190 self.stack.append(by)
203 ## def endUnicode(self): 191
204 ## """
205 ## Handler method for the "unicode" end tag.
206 ## """
207 ## u = str(self.utf8_to_code(self.unescape(self.buffer)))
208 ## self.stack.append(u)
209 ##
210 def startList(self, attrs): 192 def startList(self, attrs):
211 """ 193 """
212 Handler method for the "list" start tag. 194 Handler method for the "list" start tag.
213 195
214 @param attrs list of tag attributes 196 @param attrs list of tag attributes
314 296
315 def endPickle(self): 297 def endPickle(self):
316 """ 298 """
317 Handler method for the "pickle" end tag. 299 Handler method for the "pickle" end tag.
318 """ 300 """
319 pic = self.utf8_to_code(self.buffer).decode(self.pickleEnc) 301 pic = base64.b64decode(self.buffer.encode("ASCII"))
320 self.stack.append(pickle.loads(pic)) 302 self.stack.append(pickle.loads(pic))

eric ide

mercurial