7 __all__ = [
'BaseRepresenter',
'SafeRepresenter',
'Representer',
18 from sets
import Set
as set
20 import sys, copy_reg, types
27 yaml_representers = {}
28 yaml_multi_representers = {}
30 def __init__(self, default_style=None, default_flow_style=None):
46 for base
in cls.__bases__:
62 self.object_keeper.append(data)
63 data_types =
type(data).__mro__
64 if type(data)
is types.InstanceType:
69 for data_type
in data_types:
85 if not 'yaml_representers' in cls.__dict__:
86 cls.yaml_representers = cls.yaml_representers.copy()
87 cls.yaml_representers[data_type] = representer
88 add_representer = classmethod(add_representer)
91 if not 'yaml_multi_representers' in cls.__dict__:
92 cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
93 cls.yaml_multi_representers[data_type] = representer
94 add_multi_representer = classmethod(add_multi_representer)
110 for item
in sequence:
112 if not (isinstance(node_item, ScalarNode)
and not node_item.style):
114 value.append(node_item)
115 if flow_style
is None:
119 node.flow_style = best_style
124 node =
MappingNode(tag, value, flow_style=flow_style)
128 if hasattr(mapping,
'items'):
129 mapping = mapping.items()
131 for item_key, item_value
in mapping:
134 if not (isinstance(node_key, ScalarNode)
and not node_key.style):
136 if not (isinstance(node_value, ScalarNode)
and not node_value.style):
138 value.append((node_key, node_value))
139 if flow_style
is None:
143 node.flow_style = best_style
152 if data
in [
None, ()]:
154 if isinstance(data, (str, unicode, bool, int, float)):
166 tag =
u'tag:yaml.org,2002:str'
167 except UnicodeDecodeError:
170 tag =
u'tag:yaml.org,2002:str'
171 except UnicodeDecodeError:
172 data = data.encode(
'base64')
173 tag =
u'tag:yaml.org,2002:binary'
194 while repr(inf_value) != repr(inf_value*inf_value):
195 inf_value *= inf_value
198 if data != data
or (data == 0.0
and data == 1.0):
205 value =
unicode(repr(data)).lower()
213 if u'.' not in value
and u'e' in value:
214 value = value.replace(
u'e',
u'.0e', 1)
242 value =
unicode(data.isoformat())
246 value =
unicode(data.isoformat(
' '))
250 if hasattr(data,
'__getstate__'):
251 state = data.__getstate__()
253 state = data.__dict__.copy()
259 SafeRepresenter.add_representer(
type(
None),
260 SafeRepresenter.represent_none)
262 SafeRepresenter.add_representer(str,
263 SafeRepresenter.represent_str)
265 SafeRepresenter.add_representer(unicode,
266 SafeRepresenter.represent_unicode)
268 SafeRepresenter.add_representer(bool,
269 SafeRepresenter.represent_bool)
271 SafeRepresenter.add_representer(int,
272 SafeRepresenter.represent_int)
274 SafeRepresenter.add_representer(long,
275 SafeRepresenter.represent_long)
277 SafeRepresenter.add_representer(float,
278 SafeRepresenter.represent_float)
280 SafeRepresenter.add_representer(list,
281 SafeRepresenter.represent_list)
283 SafeRepresenter.add_representer(tuple,
284 SafeRepresenter.represent_list)
286 SafeRepresenter.add_representer(dict,
287 SafeRepresenter.represent_dict)
289 SafeRepresenter.add_representer(set,
290 SafeRepresenter.represent_set)
292 SafeRepresenter.add_representer(datetime.date,
293 SafeRepresenter.represent_date)
294 SafeRepresenter.add_representer(datetime.datetime,
295 SafeRepresenter.represent_datetime)
297 SafeRepresenter.add_representer(
None,
298 SafeRepresenter.represent_undefined)
307 tag =
u'tag:yaml.org,2002:str'
308 except UnicodeDecodeError:
311 tag =
u'tag:yaml.org,2002:python/str'
312 except UnicodeDecodeError:
313 data = data.encode(
'base64')
314 tag =
u'tag:yaml.org,2002:binary'
322 tag =
u'tag:yaml.org,2002:python/unicode'
323 except UnicodeEncodeError:
324 tag =
u'tag:yaml.org,2002:str'
328 tag =
u'tag:yaml.org,2002:int'
329 if int(data)
is not data:
330 tag =
u'tag:yaml.org,2002:python/long'
335 data =
u'%r' % data.real
336 elif data.real == 0.0:
337 data =
u'%rj' % data.imag
339 data =
u'%r+%rj' % (data.real, data.imag)
341 data =
u'%r%rj' % (data.real, data.imag)
348 name =
u'%s.%s' % (data.__module__, data.__name__)
353 u'tag:yaml.org,2002:python/module:'+data.__name__,
u'')
374 class_name =
u'%s.%s' % (cls.__module__, cls.__name__)
377 if hasattr(data,
'__getinitargs__'):
378 args = list(data.__getinitargs__())
379 if hasattr(data,
'__getstate__'):
380 state = data.__getstate__()
382 state = data.__dict__
383 if args
is None and isinstance(state, dict):
385 u'tag:yaml.org,2002:python/object:'+class_name, state)
386 if isinstance(state, dict)
and not state:
388 u'tag:yaml.org,2002:python/object/new:'+class_name, args)
392 value[
'state'] = state
394 u'tag:yaml.org,2002:python/object/new:'+class_name, value)
414 if cls
in copy_reg.dispatch_table:
415 reduce = copy_reg.dispatch_table[cls](data)
416 elif hasattr(data,
'__reduce_ex__'):
417 reduce = data.__reduce_ex__(2)
418 elif hasattr(data,
'__reduce__'):
419 reduce = data.__reduce__()
422 reduce = (list(reduce)+[
None]*5)[:5]
423 function, args, state, listitems, dictitems = reduce
427 if listitems
is not None:
428 listitems = list(listitems)
429 if dictitems
is not None:
430 dictitems = dict(dictitems)
431 if function.__name__ ==
'__newobj__':
434 tag =
u'tag:yaml.org,2002:python/object/new:'
437 tag =
u'tag:yaml.org,2002:python/object/apply:'
439 function_name =
u'%s.%s' % (function.__module__, function.__name__)
440 if not args
and not listitems
and not dictitems \
441 and isinstance(state, dict)
and newobj:
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:
450 if state
or not isinstance(state, dict):
451 value[
'state'] = state
453 value[
'listitems'] = listitems
455 value[
'dictitems'] = dictitems
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(types.ClassType,
477 Representer.represent_name)
479 Representer.add_representer(types.FunctionType,
480 Representer.represent_name)
482 Representer.add_representer(types.BuiltinFunctionType,
483 Representer.represent_name)
485 Representer.add_representer(types.ModuleType,
486 Representer.represent_module)
488 Representer.add_multi_representer(types.InstanceType,
489 Representer.represent_instance)
491 Representer.add_multi_representer(object,
492 Representer.represent_object)
def represent_yaml_object
dictionary yaml_representers
dictionary yaml_multi_representers
tuple add_multi_representer