1e30000 => 1e300000
[pyyaml/python3.git] / lib / yaml / representer.py
blob685580ef0d75d85c44701e203cc61c33cea3844f
2 __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
3 'RepresenterError']
5 from error import *
6 from nodes import *
8 try:
9 import datetime
10 datetime_available = True
11 except ImportError:
12 datetime_available = False
14 try:
15 set
16 except NameError:
17 from sets import Set as set
19 import sys, copy_reg
21 class RepresenterError(YAMLError):
22 pass
24 class BaseRepresenter:
26 yaml_representers = {}
27 yaml_multi_representers = {}
29 def __init__(self):
30 self.represented_objects = {}
32 def represent(self, data):
33 node = self.represent_data(data)
34 self.serialize(node)
35 self.represented_objects = {}
37 class C: pass
38 c = C()
39 def f(): pass
40 classobj_type = type(C)
41 instance_type = type(c)
42 function_type = type(f)
43 builtin_function_type = type(abs)
44 module_type = type(sys)
45 del C, c, f
47 def get_classobj_bases(self, cls):
48 bases = [cls]
49 for base in cls.__bases__:
50 bases.extend(self.get_classobj_bases(base))
51 return bases
53 def represent_data(self, data):
54 if self.ignore_aliases(data):
55 alias_key = None
56 else:
57 alias_key = id(data)
58 if alias_key is not None:
59 if alias_key in self.represented_objects:
60 node = self.represented_objects[alias_key]
61 if node is None:
62 raise RepresenterError("recursive objects are not allowed: %r" % data)
63 return node
64 self.represented_objects[alias_key] = None
65 data_types = type(data).__mro__
66 if type(data) is self.instance_type:
67 data_types = self.get_classobj_bases(data.__class__)+list(data_types)
68 if data_types[0] in self.yaml_representers:
69 node = self.yaml_representers[data_types[0]](self, data)
70 else:
71 for data_type in data_types:
72 if data_type in self.yaml_multi_representers:
73 node = self.yaml_multi_representers[data_type](self, data)
74 break
75 else:
76 if None in self.yaml_multi_representers:
77 node = self.yaml_multi_representers[None](self, data)
78 elif None in self.yaml_representers:
79 node = self.yaml_representers[None](self, data)
80 else:
81 node = ScalarNode(None, unicode(data))
82 if alias_key is not None:
83 self.represented_objects[alias_key] = node
84 return node
86 def add_representer(cls, data_type, representer):
87 if not 'yaml_representers' in cls.__dict__:
88 cls.yaml_representers = cls.yaml_representers.copy()
89 cls.yaml_representers[data_type] = representer
90 add_representer = classmethod(add_representer)
92 def add_multi_representer(cls, data_type, representer):
93 if not 'yaml_multi_representers' in cls.__dict__:
94 cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
95 cls.yaml_multi_representers[data_type] = representer
96 add_multi_representer = classmethod(add_multi_representer)
98 def represent_scalar(self, tag, value, style=None):
99 return ScalarNode(tag, value, style=style)
101 def represent_sequence(self, tag, sequence, flow_style=None):
102 best_style = True
103 value = []
104 for item in sequence:
105 node_item = self.represent_data(item)
106 if not (isinstance(node_item, ScalarNode) and not node_item.style):
107 best_style = False
108 value.append(self.represent_data(item))
109 if flow_style is None:
110 flow_style = best_style
111 return SequenceNode(tag, value, flow_style=flow_style)
113 def represent_mapping(self, tag, mapping, flow_style=None):
114 best_style = True
115 if hasattr(mapping, 'keys'):
116 value = {}
117 for item_key in mapping.keys():
118 item_value = mapping[item_key]
119 node_key = self.represent_data(item_key)
120 node_value = self.represent_data(item_value)
121 if not (isinstance(node_key, ScalarNode) and not node_key.style):
122 best_style = False
123 if not (isinstance(node_value, ScalarNode) and not node_value.style):
124 best_style = False
125 value[node_key] = node_value
126 else:
127 value = []
128 for item_key, item_value in mapping:
129 node_key = self.represent_data(item_key)
130 node_value = self.represent_data(item_value)
131 if not (isinstance(node_key, ScalarNode) and not node_key.style):
132 best_style = False
133 if not (isinstance(node_value, ScalarNode) and not node_value.style):
134 best_style = False
135 value.append((node_key, node_value))
136 if flow_style is None:
137 flow_style = best_style
138 return MappingNode(tag, value, flow_style=flow_style)
140 def ignore_aliases(self, data):
141 return False
143 class SafeRepresenter(BaseRepresenter):
145 def ignore_aliases(self, data):
146 if data in [None, ()]:
147 return True
148 if isinstance(data, (str, unicode, bool, int, float)):
149 return True
151 def represent_none(self, data):
152 return self.represent_scalar(u'tag:yaml.org,2002:null',
153 u'null')
155 def represent_str(self, data):
156 tag = None
157 style = None
158 try:
159 data = unicode(data, 'ascii')
160 tag = u'tag:yaml.org,2002:str'
161 except UnicodeDecodeError:
162 try:
163 data = unicode(data, 'utf-8')
164 tag = u'tag:yaml.org,2002:str'
165 except UnicodeDecodeError:
166 data = data.encode('base64')
167 tag = u'tag:yaml.org,2002:binary'
168 style = '|'
169 return self.represent_scalar(tag, data, style=style)
171 def represent_unicode(self, data):
172 return self.represent_scalar(u'tag:yaml.org,2002:str', data)
174 def represent_bool(self, data):
175 if data:
176 value = u'true'
177 else:
178 value = u'false'
179 return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
181 def represent_int(self, data):
182 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
184 def represent_long(self, data):
185 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
187 repr_pos_inf = repr(1e300000)
188 repr_neg_inf = repr(-1e300000)
189 repr_nan = repr(1e300000/1e300000)
191 def represent_float(self, data):
192 repr_data = repr(data)
193 if repr_data == self.repr_pos_inf:
194 value = u'.inf'
195 elif repr_data == self.repr_neg_inf:
196 value = u'-.inf'
197 elif repr_data == self.repr_nan:
198 value = u'.nan'
199 else:
200 value = unicode(repr_data)
201 return self.represent_scalar(u'tag:yaml.org,2002:float', value)
203 def represent_list(self, data):
204 pairs = (len(data) > 0 and isinstance(data, list))
205 if pairs:
206 for item in data:
207 if not isinstance(item, tuple) or len(item) != 2:
208 pairs = False
209 break
210 if not pairs:
211 return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
212 value = []
213 for item_key, item_value in data:
214 value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
215 [(item_key, item_value)]))
216 return SequenceNode(u'tag:yaml.org,2002:pairs', value)
218 def represent_dict(self, data):
219 return self.represent_mapping(u'tag:yaml.org,2002:map', data)
221 def represent_set(self, data):
222 value = {}
223 for key in data:
224 value[key] = None
225 return self.represent_mapping(u'tag:yaml.org,2002:set', value)
227 def represent_date(self, data):
228 value = u'%04d-%02d-%02d' % (data.year, data.month, data.day)
229 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
231 def represent_datetime(self, data):
232 value = u'%04d-%02d-%02d %02d:%02d:%02d' \
233 % (data.year, data.month, data.day,
234 data.hour, data.minute, data.second)
235 if data.microsecond:
236 value += u'.' + unicode(data.microsecond/1000000.0).split(u'.')[1]
237 if data.utcoffset():
238 value += unicode(data.utcoffset())
239 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
241 def represent_yaml_object(self, tag, data, cls, flow_style=None):
242 if hasattr(data, '__getstate__'):
243 state = data.__getstate__()
244 else:
245 state = data.__dict__.copy()
246 if isinstance(state, dict):
247 state = state.items()
248 state.sort()
249 return self.represent_mapping(tag, state, flow_style=flow_style)
251 def represent_undefined(self, data):
252 raise RepresenterError("cannot represent an object: %s" % data)
254 SafeRepresenter.add_representer(type(None),
255 SafeRepresenter.represent_none)
257 SafeRepresenter.add_representer(str,
258 SafeRepresenter.represent_str)
260 SafeRepresenter.add_representer(unicode,
261 SafeRepresenter.represent_unicode)
263 SafeRepresenter.add_representer(bool,
264 SafeRepresenter.represent_bool)
266 SafeRepresenter.add_representer(int,
267 SafeRepresenter.represent_int)
269 SafeRepresenter.add_representer(long,
270 SafeRepresenter.represent_long)
272 SafeRepresenter.add_representer(float,
273 SafeRepresenter.represent_float)
275 SafeRepresenter.add_representer(list,
276 SafeRepresenter.represent_list)
278 SafeRepresenter.add_representer(tuple,
279 SafeRepresenter.represent_list)
281 SafeRepresenter.add_representer(dict,
282 SafeRepresenter.represent_dict)
284 SafeRepresenter.add_representer(set,
285 SafeRepresenter.represent_set)
287 if datetime_available:
288 SafeRepresenter.add_representer(datetime.date,
289 SafeRepresenter.represent_date)
290 SafeRepresenter.add_representer(datetime.datetime,
291 SafeRepresenter.represent_datetime)
293 SafeRepresenter.add_representer(None,
294 SafeRepresenter.represent_undefined)
296 class Representer(SafeRepresenter):
298 def represent_str(self, data):
299 tag = None
300 style = None
301 try:
302 data = unicode(data, 'ascii')
303 tag = u'tag:yaml.org,2002:str'
304 except UnicodeDecodeError:
305 try:
306 data = unicode(data, 'utf-8')
307 tag = u'tag:yaml.org,2002:python/str'
308 except UnicodeDecodeError:
309 data = data.encode('base64')
310 tag = u'tag:yaml.org,2002:binary'
311 style = '|'
312 return self.represent_scalar(tag, data, style=style)
314 def represent_unicode(self, data):
315 tag = None
316 try:
317 data.encode('ascii')
318 tag = u'tag:yaml.org,2002:python/unicode'
319 except UnicodeEncodeError:
320 tag = u'tag:yaml.org,2002:str'
321 return self.represent_scalar(tag, data)
323 def represent_long(self, data):
324 tag = u'tag:yaml.org,2002:int'
325 if int(data) is not data:
326 tag = u'tag:yaml.org,2002:python/long'
327 return self.represent_scalar(tag, unicode(data))
329 def represent_complex(self, data):
330 if data.imag == 0.0:
331 data = u'%r' % data.real
332 elif data.real == 0.0:
333 data = u'%rj' % data.imag
334 elif data.imag > 0:
335 data = u'%r+%rj' % (data.real, data.imag)
336 else:
337 data = u'%r%rj' % (data.real, data.imag)
338 return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
340 def represent_tuple(self, data):
341 return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
343 def represent_name(self, data):
344 name = u'%s.%s' % (data.__module__, data.__name__)
345 return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'')
347 def represent_module(self, data):
348 return self.represent_scalar(
349 u'tag:yaml.org,2002:python/module:'+data.__name__, u'')
351 def represent_instance(self, data):
352 # For instances of classic classes, we use __getinitargs__ and
353 # __getstate__ to serialize the data.
355 # If data.__getinitargs__ exists, the object must be reconstructed by
356 # calling cls(**args), where args is a tuple returned by
357 # __getinitargs__. Otherwise, the cls.__init__ method should never be
358 # called and the class instance is created by instantiating a trivial
359 # class and assigning to the instance's __class__ variable.
361 # If data.__getstate__ exists, it returns the state of the object.
362 # Otherwise, the state of the object is data.__dict__.
364 # We produce either a !!python/object or !!python/object/new node.
365 # If data.__getinitargs__ does not exist and state is a dictionary, we
366 # produce a !!python/object node . Otherwise we produce a
367 # !!python/object/new node.
369 cls = data.__class__
370 class_name = u'%s.%s' % (cls.__module__, cls.__name__)
371 args = None
372 state = None
373 if hasattr(data, '__getinitargs__'):
374 args = list(data.__getinitargs__())
375 if hasattr(data, '__getstate__'):
376 state = data.__getstate__()
377 else:
378 state = data.__dict__
379 if args is None and isinstance(state, dict):
380 state = state.items()
381 state.sort()
382 return self.represent_mapping(
383 u'tag:yaml.org,2002:python/object:'+class_name, state)
384 if isinstance(state, dict) and not state:
385 return self.represent_sequence(
386 u'tag:yaml.org,2002:python/object/new:'+class_name, args)
387 value = {}
388 if args:
389 value['args'] = args
390 value['state'] = state
391 return self.represent_mapping(
392 u'tag:yaml.org,2002:python/object/new:'+class_name, value)
394 def represent_object(self, data):
395 # We use __reduce__ API to save the data. data.__reduce__ returns
396 # a tuple of length 2-5:
397 # (function, args, state, listitems, dictitems)
399 # For reconstructing, we calls function(*args), then set its state,
400 # listitems, and dictitems if they are not None.
402 # A special case is when function.__name__ == '__newobj__'. In this
403 # case we create the object with args[0].__new__(*args).
405 # Another special case is when __reduce__ returns a string - we don't
406 # support it.
408 # We produce a !!python/object, !!python/object/new or
409 # !!python/object/apply node.
411 cls = type(data)
412 if cls in copy_reg.dispatch_table:
413 reduce = copy_reg.dispatch_table[cls]
414 elif hasattr(data, '__reduce_ex__'):
415 reduce = data.__reduce_ex__(2)
416 elif hasattr(data, '__reduce__'):
417 reduce = data.__reduce__()
418 else:
419 raise RepresenterError("cannot represent object: %r" % data)
420 reduce = (list(reduce)+[None]*5)[:5]
421 function, args, state, listitems, dictitems = reduce
422 args = list(args)
423 if state is None:
424 state = {}
425 if listitems is not None:
426 listitems = list(listitems)
427 if dictitems is not None:
428 dictitems = dict(dictitems)
429 if function.__name__ == '__newobj__':
430 function = args[0]
431 args = args[1:]
432 tag = u'tag:yaml.org,2002:python/object/new:'
433 newobj = True
434 else:
435 tag = u'tag:yaml.org,2002:python/object/apply:'
436 newobj = False
437 function_name = u'%s.%s' % (function.__module__, function.__name__)
438 if not args and not listitems and not dictitems \
439 and isinstance(state, dict) and newobj:
440 state = state.items()
441 state.sort()
442 return self.represent_mapping(
443 u'tag:yaml.org,2002:python/object:'+function_name, state)
444 if not listitems and not dictitems \
445 and isinstance(state, dict) and not state:
446 return self.represent_sequence(tag+function_name, args)
447 value = {}
448 if args:
449 value['args'] = args
450 if state or not isinstance(state, dict):
451 value['state'] = state
452 if listitems:
453 value['listitems'] = listitems
454 if dictitems:
455 value['dictitems'] = dictitems
456 return self.represent_mapping(tag+function_name, value)
458 Representer.add_representer(str,
459 Representer.represent_str)
461 Representer.add_representer(unicode,
462 Representer.represent_unicode)
464 Representer.add_representer(long,
465 Representer.represent_long)
467 Representer.add_representer(complex,
468 Representer.represent_complex)
470 Representer.add_representer(tuple,
471 Representer.represent_tuple)
473 Representer.add_representer(type,
474 Representer.represent_name)
476 Representer.add_representer(Representer.classobj_type,
477 Representer.represent_name)
479 Representer.add_representer(Representer.function_type,
480 Representer.represent_name)
482 Representer.add_representer(Representer.builtin_function_type,
483 Representer.represent_name)
485 Representer.add_representer(Representer.module_type,
486 Representer.represent_module)
488 Representer.add_multi_representer(Representer.instance_type,
489 Representer.represent_instance)
491 Representer.add_multi_representer(object,
492 Representer.represent_object)