2 __all__
= ['BaseRepresenter', 'SafeRepresenter', 'Representer',
13 from sets
import Set
as set
17 class RepresenterError(YAMLError
):
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
= []
32 def represent(self
, data
):
33 node
= self
.represent_data(data
)
35 self
.represented_objects
= {}
36 self
.object_keeper
= []
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
)
51 def get_classobj_bases(self
, cls
):
53 for base
in cls
.__bases
__:
54 bases
.extend(self
.get_classobj_bases(base
))
57 def represent_data(self
, data
):
58 if self
.ignore_aliases(data
):
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
]
66 # raise RepresenterError("recursive objects are not allowed: %r" % data)
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
)
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
)
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
)
86 node
= ScalarNode(None, unicode(data
))
87 #if alias_key is not None:
88 # self.represented_objects[alias_key] = 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):
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
111 def represent_sequence(self
, tag
, sequence
, flow_style
=None):
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
117 for item
in sequence
:
118 node_item
= self
.represent_data(item
)
119 if not (isinstance(node_item
, ScalarNode
) and not node_item
.style
):
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
126 node
.flow_style
= best_style
129 def represent_mapping(self
, tag
, mapping
, flow_style
=None):
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
135 if hasattr(mapping
, 'items'):
136 mapping
= mapping
.items()
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
):
143 if not (isinstance(node_value
, ScalarNode
) and not node_value
.style
):
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
150 node
.flow_style
= best_style
153 def ignore_aliases(self
, data
):
156 class SafeRepresenter(BaseRepresenter
):
158 def ignore_aliases(self
, data
):
159 if data
in [None, ()]:
161 if isinstance(data
, (str, unicode, bool, int, float)):
164 def represent_none(self
, data
):
165 return self
.represent_scalar(u
'tag:yaml.org,2002:null',
168 def represent_str(self
, data
):
172 data
= unicode(data
, 'ascii')
173 tag
= u
'tag:yaml.org,2002:str'
174 except UnicodeDecodeError:
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'
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
):
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
))
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):
207 elif data
== self
.inf_value
:
209 elif data
== -self
.inf_value
:
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))
219 # if not isinstance(item, tuple) or len(item) != 2:
223 return self
.represent_sequence(u
'tag:yaml.org,2002:seq', data
)
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
):
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
__()
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
):
304 data
= unicode(data
, 'ascii')
305 tag
= u
'tag:yaml.org,2002:str'
306 except UnicodeDecodeError:
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'
314 return self
.represent_scalar(tag
, data
, style
=style
)
316 def represent_unicode(self
, data
):
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
):
333 data
= u
'%r' % data
.real
334 elif data
.real
== 0.0:
335 data
= u
'%rj' % data
.imag
337 data
= u
'%r+%rj' % (data
.real
, data
.imag
)
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.
372 class_name
= u
'%s.%s' % (cls
.__module
__, cls
.__name
__)
375 if hasattr(data
, '__getinitargs__'):
376 args
= list(data
.__getinitargs
__())
377 if hasattr(data
, '__getstate__'):
378 state
= data
.__getstate
__()
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
)
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
408 # We produce a !!python/object, !!python/object/new or
409 # !!python/object/apply node.
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
__()
419 raise RepresenterError("cannot represent object: %r" % data
)
420 reduce = (list(reduce)+[None]*5)[:5]
421 function
, args
, state
, listitems
, dictitems
= reduce
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__':
432 tag
= u
'tag:yaml.org,2002:python/object/new:'
435 tag
= u
'tag:yaml.org,2002:python/object/apply:'
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
)
448 if state
or not isinstance(state
, dict):
449 value
['state'] = state
451 value
['listitems'] = listitems
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
)