Fix a test to work under Python 2.3.
[pyyaml/python3.git] / lib / yaml / representer.py
blob3563a4c211ee63316a3a7c29e83dbb2212f55cec
2 __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
3 'RepresenterError']
5 from error import *
6 from nodes import *
8 import datetime
10 try:
11 set
12 except NameError:
13 from sets import Set as set
15 import sys, copy_reg
17 class RepresenterError(YAMLError):
18 pass
20 class BaseRepresenter(object):
22 yaml_representers = {}
23 yaml_multi_representers = {}
25 def __init__(self, default_style=None, default_flow_style=None):
26 self.default_style = default_style
27 self.default_flow_style = default_flow_style
28 self.represented_objects = {}
29 self.object_keeper = []
30 self.alias_key = None
32 def represent(self, data):
33 node = self.represent_data(data)
34 self.serialize(node)
35 self.represented_objects = {}
36 self.object_keeper = []
37 self.alias_key = None
39 class C: pass
40 c = C()
41 def f(): pass
42 def g(): yield None
43 classobj_type = type(C)
44 instance_type = type(c)
45 function_type = type(f)
46 generator_type = type(g())
47 builtin_function_type = type(abs)
48 module_type = type(sys)
49 del C, c, f, g
51 def get_classobj_bases(self, cls):
52 bases = [cls]
53 for base in cls.__bases__:
54 bases.extend(self.get_classobj_bases(base))
55 return bases
57 def represent_data(self, data):
58 if self.ignore_aliases(data):
59 self.alias_key = None
60 else:
61 self.alias_key = id(data)
62 if self.alias_key is not None:
63 if self.alias_key in self.represented_objects:
64 node = self.represented_objects[self.alias_key]
65 #if node is None:
66 # raise RepresenterError("recursive objects are not allowed: %r" % data)
67 return node
68 #self.represented_objects[alias_key] = None
69 self.object_keeper.append(data)
70 data_types = type(data).__mro__
71 if type(data) is self.instance_type:
72 data_types = self.get_classobj_bases(data.__class__)+list(data_types)
73 if data_types[0] in self.yaml_representers:
74 node = self.yaml_representers[data_types[0]](self, data)
75 else:
76 for data_type in data_types:
77 if data_type in self.yaml_multi_representers:
78 node = self.yaml_multi_representers[data_type](self, data)
79 break
80 else:
81 if None in self.yaml_multi_representers:
82 node = self.yaml_multi_representers[None](self, data)
83 elif None in self.yaml_representers:
84 node = self.yaml_representers[None](self, data)
85 else:
86 node = ScalarNode(None, unicode(data))
87 #if alias_key is not None:
88 # self.represented_objects[alias_key] = node
89 return node
91 def add_representer(cls, data_type, representer):
92 if not 'yaml_representers' in cls.__dict__:
93 cls.yaml_representers = cls.yaml_representers.copy()
94 cls.yaml_representers[data_type] = representer
95 add_representer = classmethod(add_representer)
97 def add_multi_representer(cls, data_type, representer):
98 if not 'yaml_multi_representers' in cls.__dict__:
99 cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
100 cls.yaml_multi_representers[data_type] = representer
101 add_multi_representer = classmethod(add_multi_representer)
103 def represent_scalar(self, tag, value, style=None):
104 if style is None:
105 style = self.default_style
106 node = ScalarNode(tag, value, style=style)
107 if self.alias_key is not None:
108 self.represented_objects[self.alias_key] = node
109 return node
111 def represent_sequence(self, tag, sequence, flow_style=None):
112 value = []
113 node = SequenceNode(tag, value, flow_style=flow_style)
114 if self.alias_key is not None:
115 self.represented_objects[self.alias_key] = node
116 best_style = True
117 for item in sequence:
118 node_item = self.represent_data(item)
119 if not (isinstance(node_item, ScalarNode) and not node_item.style):
120 best_style = False
121 value.append(node_item)
122 if flow_style is None:
123 if self.default_flow_style is not None:
124 node.flow_style = self.default_flow_style
125 else:
126 node.flow_style = best_style
127 return node
129 def represent_mapping(self, tag, mapping, flow_style=None):
130 value = []
131 node = MappingNode(tag, value, flow_style=flow_style)
132 if self.alias_key is not None:
133 self.represented_objects[self.alias_key] = node
134 best_style = True
135 if hasattr(mapping, 'items'):
136 mapping = mapping.items()
137 mapping.sort()
138 for item_key, item_value in mapping:
139 node_key = self.represent_data(item_key)
140 node_value = self.represent_data(item_value)
141 if not (isinstance(node_key, ScalarNode) and not node_key.style):
142 best_style = False
143 if not (isinstance(node_value, ScalarNode) and not node_value.style):
144 best_style = False
145 value.append((node_key, node_value))
146 if flow_style is None:
147 if self.default_flow_style is not None:
148 node.flow_style = self.default_flow_style
149 else:
150 node.flow_style = best_style
151 return node
153 def ignore_aliases(self, data):
154 return False
156 class SafeRepresenter(BaseRepresenter):
158 def ignore_aliases(self, data):
159 if data in [None, ()]:
160 return True
161 if isinstance(data, (str, unicode, bool, int, float)):
162 return True
164 def represent_none(self, data):
165 return self.represent_scalar(u'tag:yaml.org,2002:null',
166 u'null')
168 def represent_str(self, data):
169 tag = None
170 style = None
171 try:
172 data = unicode(data, 'ascii')
173 tag = u'tag:yaml.org,2002:str'
174 except UnicodeDecodeError:
175 try:
176 data = unicode(data, 'utf-8')
177 tag = u'tag:yaml.org,2002:str'
178 except UnicodeDecodeError:
179 data = data.encode('base64')
180 tag = u'tag:yaml.org,2002:binary'
181 style = '|'
182 return self.represent_scalar(tag, data, style=style)
184 def represent_unicode(self, data):
185 return self.represent_scalar(u'tag:yaml.org,2002:str', data)
187 def represent_bool(self, data):
188 if data:
189 value = u'true'
190 else:
191 value = u'false'
192 return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
194 def represent_int(self, data):
195 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
197 def represent_long(self, data):
198 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
200 inf_value = 1e300
201 while repr(inf_value) != repr(inf_value*inf_value):
202 inf_value *= inf_value
204 def represent_float(self, data):
205 if data != data or (data == 0.0 and data == 1.0):
206 value = u'.nan'
207 elif data == self.inf_value:
208 value = u'.inf'
209 elif data == -self.inf_value:
210 value = u'-.inf'
211 else:
212 value = unicode(repr(data))
213 return self.represent_scalar(u'tag:yaml.org,2002:float', value)
215 def represent_list(self, data):
216 #pairs = (len(data) > 0 and isinstance(data, list))
217 #if pairs:
218 # for item in data:
219 # if not isinstance(item, tuple) or len(item) != 2:
220 # pairs = False
221 # break
222 #if not pairs:
223 return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
224 #value = []
225 #for item_key, item_value in data:
226 # value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
227 # [(item_key, item_value)]))
228 #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
230 def represent_dict(self, data):
231 return self.represent_mapping(u'tag:yaml.org,2002:map', data)
233 def represent_set(self, data):
234 value = {}
235 for key in data:
236 value[key] = None
237 return self.represent_mapping(u'tag:yaml.org,2002:set', value)
239 def represent_date(self, data):
240 value = unicode(data.isoformat())
241 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
243 def represent_datetime(self, data):
244 value = unicode(data.isoformat(' '))
245 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
247 def represent_yaml_object(self, tag, data, cls, flow_style=None):
248 if hasattr(data, '__getstate__'):
249 state = data.__getstate__()
250 else:
251 state = data.__dict__.copy()
252 return self.represent_mapping(tag, state, flow_style=flow_style)
254 def represent_undefined(self, data):
255 raise RepresenterError("cannot represent an object: %s" % data)
257 SafeRepresenter.add_representer(type(None),
258 SafeRepresenter.represent_none)
260 SafeRepresenter.add_representer(str,
261 SafeRepresenter.represent_str)
263 SafeRepresenter.add_representer(unicode,
264 SafeRepresenter.represent_unicode)
266 SafeRepresenter.add_representer(bool,
267 SafeRepresenter.represent_bool)
269 SafeRepresenter.add_representer(int,
270 SafeRepresenter.represent_int)
272 SafeRepresenter.add_representer(long,
273 SafeRepresenter.represent_long)
275 SafeRepresenter.add_representer(float,
276 SafeRepresenter.represent_float)
278 SafeRepresenter.add_representer(list,
279 SafeRepresenter.represent_list)
281 SafeRepresenter.add_representer(tuple,
282 SafeRepresenter.represent_list)
284 SafeRepresenter.add_representer(dict,
285 SafeRepresenter.represent_dict)
287 SafeRepresenter.add_representer(set,
288 SafeRepresenter.represent_set)
290 SafeRepresenter.add_representer(datetime.date,
291 SafeRepresenter.represent_date)
292 SafeRepresenter.add_representer(datetime.datetime,
293 SafeRepresenter.represent_datetime)
295 SafeRepresenter.add_representer(None,
296 SafeRepresenter.represent_undefined)
298 class Representer(SafeRepresenter):
300 def represent_str(self, data):
301 tag = None
302 style = None
303 try:
304 data = unicode(data, 'ascii')
305 tag = u'tag:yaml.org,2002:str'
306 except UnicodeDecodeError:
307 try:
308 data = unicode(data, 'utf-8')
309 tag = u'tag:yaml.org,2002:python/str'
310 except UnicodeDecodeError:
311 data = data.encode('base64')
312 tag = u'tag:yaml.org,2002:binary'
313 style = '|'
314 return self.represent_scalar(tag, data, style=style)
316 def represent_unicode(self, data):
317 tag = None
318 try:
319 data.encode('ascii')
320 tag = u'tag:yaml.org,2002:python/unicode'
321 except UnicodeEncodeError:
322 tag = u'tag:yaml.org,2002:str'
323 return self.represent_scalar(tag, data)
325 def represent_long(self, data):
326 tag = u'tag:yaml.org,2002:int'
327 if int(data) is not data:
328 tag = u'tag:yaml.org,2002:python/long'
329 return self.represent_scalar(tag, unicode(data))
331 def represent_complex(self, data):
332 if data.imag == 0.0:
333 data = u'%r' % data.real
334 elif data.real == 0.0:
335 data = u'%rj' % data.imag
336 elif data.imag > 0:
337 data = u'%r+%rj' % (data.real, data.imag)
338 else:
339 data = u'%r%rj' % (data.real, data.imag)
340 return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
342 def represent_tuple(self, data):
343 return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
345 def represent_name(self, data):
346 name = u'%s.%s' % (data.__module__, data.__name__)
347 return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'')
349 def represent_module(self, data):
350 return self.represent_scalar(
351 u'tag:yaml.org,2002:python/module:'+data.__name__, u'')
353 def represent_instance(self, data):
354 # For instances of classic classes, we use __getinitargs__ and
355 # __getstate__ to serialize the data.
357 # If data.__getinitargs__ exists, the object must be reconstructed by
358 # calling cls(**args), where args is a tuple returned by
359 # __getinitargs__. Otherwise, the cls.__init__ method should never be
360 # called and the class instance is created by instantiating a trivial
361 # class and assigning to the instance's __class__ variable.
363 # If data.__getstate__ exists, it returns the state of the object.
364 # Otherwise, the state of the object is data.__dict__.
366 # We produce either a !!python/object or !!python/object/new node.
367 # If data.__getinitargs__ does not exist and state is a dictionary, we
368 # produce a !!python/object node . Otherwise we produce a
369 # !!python/object/new node.
371 cls = data.__class__
372 class_name = u'%s.%s' % (cls.__module__, cls.__name__)
373 args = None
374 state = None
375 if hasattr(data, '__getinitargs__'):
376 args = list(data.__getinitargs__())
377 if hasattr(data, '__getstate__'):
378 state = data.__getstate__()
379 else:
380 state = data.__dict__
381 if args is None and isinstance(state, dict):
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](data)
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 return self.represent_mapping(
441 u'tag:yaml.org,2002:python/object:'+function_name, state)
442 if not listitems and not dictitems \
443 and isinstance(state, dict) and not state:
444 return self.represent_sequence(tag+function_name, args)
445 value = {}
446 if args:
447 value['args'] = args
448 if state or not isinstance(state, dict):
449 value['state'] = state
450 if listitems:
451 value['listitems'] = listitems
452 if dictitems:
453 value['dictitems'] = dictitems
454 return self.represent_mapping(tag+function_name, value)
456 Representer.add_representer(str,
457 Representer.represent_str)
459 Representer.add_representer(unicode,
460 Representer.represent_unicode)
462 Representer.add_representer(long,
463 Representer.represent_long)
465 Representer.add_representer(complex,
466 Representer.represent_complex)
468 Representer.add_representer(tuple,
469 Representer.represent_tuple)
471 Representer.add_representer(type,
472 Representer.represent_name)
474 Representer.add_representer(Representer.classobj_type,
475 Representer.represent_name)
477 Representer.add_representer(Representer.function_type,
478 Representer.represent_name)
480 Representer.add_representer(Representer.builtin_function_type,
481 Representer.represent_name)
483 Representer.add_representer(Representer.module_type,
484 Representer.represent_module)
486 Representer.add_multi_representer(Representer.instance_type,
487 Representer.represent_instance)
489 Representer.add_multi_representer(object,
490 Representer.represent_object)