25 @param file open file (like) object for writing |
21 @param file open file (like) object for writing |
26 """ |
22 """ |
27 self.pf = file |
23 self.pf = file |
28 |
24 |
29 self.basics = { |
25 self.basics = { |
30 NoneType : self._write_none, |
26 type(None) : self._write_none, |
31 IntType : self._write_int, |
27 type(1) : self._write_int, |
32 LongType : self._write_long, |
28 type(1.1) : self._write_float, |
33 FloatType : self._write_float, |
29 type(1+1j) : self._write_complex, |
34 ComplexType : self._write_complex, |
30 type(True) : self._write_bool, |
35 BooleanType : self._write_bool, |
31 type("") : self._write_string, |
36 StringType : self._write_string, |
32 ## UnicodeType : self._write_unicode, # should be bytes |
37 UnicodeType : self._write_unicode, |
33 type((1,)) : self._write_tuple, |
38 TupleType : self._write_tuple, |
34 type([]) : self._write_list, |
39 ListType : self._write_list, |
35 type({}) : self._write_dictionary, |
40 DictType : self._write_dictionary, |
36 # TODO: add set |
41 } |
37 } |
42 |
38 |
43 self.NEWPARA = unichr(0x2029) |
39 self.NEWPARA = chr(0x2029) |
44 self.NEWLINE = unichr(0x2028) |
40 self.NEWLINE = chr(0x2028) |
45 |
41 |
46 def _write(self, s, newline = True): |
42 def _write(self, s, newline = True): |
47 """ |
43 """ |
48 Protected method used to do the real write operation. |
44 Protected method used to do the real write operation. |
49 |
45 |
50 @param s string to be written to the XML file |
46 @param s string to be written to the XML file |
51 @param newline flag indicating a linebreak |
47 @param newline flag indicating a linebreak |
52 """ |
48 """ |
53 self.pf.write("%s%s" % (s.encode('utf-8'), |
49 ## self.pf.write("%s%s" % (s.encode('utf-8'), |
|
50 self.pf.write("%s%s" % (s, |
54 newline and os.linesep or "")) |
51 newline and os.linesep or "")) |
55 |
52 |
56 def writeXML(self): |
53 def writeXML(self): |
57 """ |
54 """ |
58 Public method to write the XML to the file. |
55 Public method to write the XML to the file. |
161 @param value value to be dumped (string) |
158 @param value value to be dumped (string) |
162 @param indent indentation level for prettier output (integer) |
159 @param indent indentation level for prettier output (integer) |
163 """ |
160 """ |
164 self._write('%s<string>%s</string>' % (" " * indent, self.escape(value))) |
161 self._write('%s<string>%s</string>' % (" " * indent, self.escape(value))) |
165 |
162 |
|
163 # TODO: add method for writing bytes |
166 def _write_unicode(self, value, indent): |
164 def _write_unicode(self, value, indent): |
167 """ |
165 """ |
168 Protected method to dump an UnicodeType object. |
166 Protected method to dump an UnicodeType object. |
169 |
167 |
170 @param value value to be dumped (unicode) |
168 @param value value to be dumped (unicode) |
206 @param indent indentation level for prettier output (integer) |
204 @param indent indentation level for prettier output (integer) |
207 """ |
205 """ |
208 self._write('%s<dict>' % (" " * indent)) |
206 self._write('%s<dict>' % (" " * indent)) |
209 nindent1 = indent + 1 |
207 nindent1 = indent + 1 |
210 nindent2 = indent + 2 |
208 nindent2 = indent + 2 |
211 keys = value.keys() |
209 keys = sorted(list(value.keys())) |
212 keys.sort() |
|
213 for key in keys: |
210 for key in keys: |
214 self._write('%s<key>' % (" " * nindent1)) |
211 self._write('%s<key>' % (" " * nindent1)) |
215 self._writeBasics(key, nindent2) |
212 self._writeBasics(key, nindent2) |
216 self._write('%s</key>' % (" " * nindent1)) |
213 self._write('%s</key>' % (" " * nindent1)) |
217 self._write('%s<value>' % (" " * nindent1)) |
214 self._write('%s<value>' % (" " * nindent1)) |