comparison emeraldtree/SimpleXMLWriter.py @ 47:e647f30cc08e

remove backwards compat code for python < 2.4
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 03 Aug 2008 20:59:44 +0200
parents 34be1290ac0b
children
comparison
equal deleted inserted replaced
46:c228aba7258c 47:e647f30cc08e
85 # 85 #
86 # w.close(html) 86 # w.close(html)
87 # </pre> 87 # </pre>
88 ## 88 ##
89 89
90 import re, sys, string 90 import re
91 91
92 try: 92 def encode(s, encoding):
93 unicode("") 93 return s.encode(encoding)
94 except NameError: 94
95 def encode(s, encoding): 95 def encode_entity(text, pattern=re.compile(ur'[&<>"\u0080-\uffff]+')):
96 # 1.5.2: application must use the right encoding
97 return s
98 _escape = re.compile(r"[&<>\"\x80-\xff]+") # 1.5.2
99 else:
100 def encode(s, encoding):
101 return s.encode(encoding)
102 _escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
103
104 def encode_entity(text, pattern=_escape):
105 # map reserved and non-ascii characters to numerical entities 96 # map reserved and non-ascii characters to numerical entities
106 def escape_entities(m): 97 def escape_entities(m):
107 out = [] 98 out = []
108 for char in m.group(): 99 for char in m.group():
109 out.append("&#%d;" % ord(char)) 100 out.append("&#%d;" % ord(char))
110 return string.join(out, "") 101 return "".join(out)
111 return encode(pattern.sub(escape_entities, text), "ascii") 102 return encode(pattern.sub(escape_entities, text), "ascii")
112
113 del _escape
114 103
115 # 104 #
116 # the following functions assume an ascii-compatible encoding 105 # the following functions assume an ascii-compatible encoding
117 # (or "utf-16") 106 # (or "utf-16")
118 107
119 def escape_cdata(s, encoding=None, replace=string.replace): 108 def escape_cdata(s, encoding=None):
120 s = replace(s, "&", "&amp;") 109 s = s.replace("&", "&amp;")
121 s = replace(s, "<", "&lt;") 110 s = s.replace("<", "&lt;")
122 s = replace(s, ">", "&gt;") 111 s = s.replace(">", "&gt;")
123 if encoding: 112 if encoding:
124 try: 113 try:
125 return encode(s, encoding) 114 return encode(s, encoding)
126 except UnicodeError: 115 except UnicodeError:
127 return encode_entity(s) 116 return encode_entity(s)
128 return s 117 return s
129 118
130 def escape_attrib(s, encoding=None, replace=string.replace): 119 def escape_attrib(s, encoding=None):
131 s = replace(s, "&", "&amp;") 120 s = s.replace("&", "&amp;")
132 s = replace(s, "'", "&apos;") 121 s = s.replace("'", "&apos;")
133 s = replace(s, "\"", "&quot;") 122 s = s.replace('"', "&quot;")
134 s = replace(s, "<", "&lt;") 123 s = s.replace("<", "&lt;")
135 s = replace(s, ">", "&gt;") 124 s = s.replace(">", "&gt;")
136 if encoding: 125 if encoding:
137 try: 126 try:
138 return encode(s, encoding) 127 return encode(s, encoding)
139 except UnicodeError: 128 except UnicodeError:
140 return encode_entity(s) 129 return encode_entity(s)
164 # flush internal buffers 153 # flush internal buffers
165 if self.__open: 154 if self.__open:
166 self.__write(">") 155 self.__write(">")
167 self.__open = 0 156 self.__open = 0
168 if self.__data: 157 if self.__data:
169 data = string.join(self.__data, "") 158 data = "".join(self.__data)
170 self.__write(escape_cdata(data, self.__encoding)) 159 self.__write(escape_cdata(data, self.__encoding))
171 self.__data = [] 160 self.__data = []
172 161
173 ## 162 ##
174 # Writes an XML declaration. 163 # Writes an XML declaration.