E4XML/XMLHandlerBase.py

changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
6 """ 6 """
7 Module implementing a base class for all of eric4s XML handlers. 7 Module implementing a base class for all of eric4s XML handlers.
8 """ 8 """
9 9
10 import sys 10 import sys
11 from types import UnicodeType 11 ##from types import UnicodeType
12 try: 12 import pickle as pickle
13 import cPickle as pickle
14 except ImportError:
15 import pickle
16 13
17 from xml.sax.handler import ContentHandler 14 from xml.sax.handler import ContentHandler
18 15
19 class XMLHandlerBase(ContentHandler): 16 class XMLHandlerBase(ContentHandler):
20 """ 17 """
43 40
44 self.buffer = "" 41 self.buffer = ""
45 self.stack = [] 42 self.stack = []
46 self._marker = '__MARKER__' 43 self._marker = '__MARKER__'
47 44
48 self.NEWPARA = unichr(0x2029) 45 self.NEWPARA = chr(0x2029)
49 self.NEWLINE = unichr(0x2028) 46 self.NEWLINE = chr(0x2028)
50 47
51 def utf8_to_code(self, text): 48 def utf8_to_code(self, text):
52 """ 49 """
53 Public method to convert a string to unicode and encode it for XML. 50 Public method to convert a string to unicode and encode it for XML.
54 51
55 @param text the text to encode (string) 52 @param text the text to encode (string)
56 """ 53 """
57 if type(text) is not UnicodeType: 54 # TODO: convert calls to this method to not use it anymore
58 text = unicode(text, "utf-8") 55 ## if not isinstance(text, UnicodeType):
56 ## text = str(text, "utf-8")
59 return text 57 return text
60 58
61 def unescape(self, text, attribute = False): 59 def unescape(self, text, attribute = False):
62 """ 60 """
63 Public method used to unescape certain characters. 61 Public method used to unescape certain characters.
156 154
157 def endLong(self): 155 def endLong(self):
158 """ 156 """
159 Handler method for the "long" end tag. 157 Handler method for the "long" end tag.
160 """ 158 """
161 self.stack.append(long(self.buffer.strip())) 159 self.stack.append(int(self.buffer.strip()))
162 160
163 def endBool(self): 161 def endBool(self):
164 """ 162 """
165 Handler method for the "bool" end tag. 163 Handler method for the "bool" end tag.
166 """ 164 """
191 189
192 def endUnicode(self): 190 def endUnicode(self):
193 """ 191 """
194 Handler method for the "unicode" end tag. 192 Handler method for the "unicode" end tag.
195 """ 193 """
196 u = unicode(self.utf8_to_code(self.unescape(self.buffer))) 194 u = str(self.utf8_to_code(self.unescape(self.buffer)))
197 self.stack.append(u) 195 self.stack.append(u)
198 196
199 def startList(self, attrs): 197 def startList(self, attrs):
200 """ 198 """
201 Handler method for the "list" start tag. 199 Handler method for the "list" start tag.

eric ide

mercurial