2 __all__
= ['BaseRepresenter', 'SafeRepresenter', 'Representer',
10 datetime_available
= True
12 datetime_available
= False
17 from sets
import Set
as set
21 class RepresenterError(YAMLError
):
24 class BaseRepresenter
:
26 yaml_representers
= {}
27 yaml_multi_representers
= {}
30 self
.represented_objects
= {}
32 def represent(self
, data
):
33 node
= self
.represent_data(data
)
35 self
.represented_objects
= {}
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
)
47 def get_classobj_bases(self
, cls
):
49 for base
in cls
.__bases
__:
50 bases
.extend(self
.get_classobj_bases(base
))
53 def represent_data(self
, data
):
54 if self
.ignore_aliases(data
):
58 if alias_key
is not None:
59 if alias_key
in self
.represented_objects
:
60 node
= self
.represented_objects
[alias_key
]
62 raise RepresenterError("recursive objects are not allowed: %r" % data
)
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
)
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
)
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
)
81 node
= ScalarNode(None, unicode(data
))
82 if alias_key
is not None:
83 self
.represented_objects
[alias_key
] = 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):
104 for item
in sequence
:
105 node_item
= self
.represent_data(item
)
106 if not (isinstance(node_item
, ScalarNode
) and not node_item
.style
):
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):
115 if hasattr(mapping
, 'keys'):
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
):
123 if not (isinstance(node_value
, ScalarNode
) and not node_value
.style
):
125 value
[node_key
] = node_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
):
133 if not (isinstance(node_value
, ScalarNode
) and not node_value
.style
):
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
):
143 class SafeRepresenter(BaseRepresenter
):
145 def ignore_aliases(self
, data
):
146 if data
in [None, ()]:
148 if isinstance(data
, (str, unicode, bool, int, float)):
151 def represent_none(self
, data
):
152 return self
.represent_scalar(u
'tag:yaml.org,2002:null',
155 def represent_str(self
, data
):
159 data
= unicode(data
, 'ascii')
160 tag
= u
'tag:yaml.org,2002:str'
161 except UnicodeDecodeError:
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'
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
):
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
:
195 elif repr_data
== self
.repr_neg_inf
:
197 elif repr_data
== self
.repr_nan
:
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))
207 if not isinstance(item
, tuple) or len(item
) != 2:
211 return self
.represent_sequence(u
'tag:yaml.org,2002:seq', data
)
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
):
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
)
236 value
+= u
'.' + unicode(data
.microsecond
/1000000.0).split(u
'.')[1]
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
__()
245 state
= data
.__dict
__.copy()
246 if isinstance(state
, dict):
247 state
= state
.items()
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
):
302 data
= unicode(data
, 'ascii')
303 tag
= u
'tag:yaml.org,2002:str'
304 except UnicodeDecodeError:
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'
312 return self
.represent_scalar(tag
, data
, style
=style
)
314 def represent_unicode(self
, data
):
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
):
331 data
= u
'%r' % data
.real
332 elif data
.real
== 0.0:
333 data
= u
'%rj' % data
.imag
335 data
= u
'%r+%rj' % (data
.real
, data
.imag
)
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.
370 class_name
= u
'%s.%s' % (cls
.__module
__, cls
.__name
__)
373 if hasattr(data
, '__getinitargs__'):
374 args
= list(data
.__getinitargs
__())
375 if hasattr(data
, '__getstate__'):
376 state
= data
.__getstate
__()
378 state
= data
.__dict
__
379 if args
is None and isinstance(state
, dict):
380 state
= state
.items()
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
]
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 state
= state
.items()
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
)
450 if state
or not isinstance(state
, dict):
451 value
['state'] = state
453 value
['listitems'] = listitems
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
)