blob: 9dca41af7a572794c2895e8fcd15668918624606 [file] [log] [blame]
Kirill Simonovcc316a42006-04-11 00:34:16 +00001
2__all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
3 'RepresenterError']
4
5from error import *
6from nodes import *
Kirill Simonovcc316a42006-04-11 00:34:16 +00007
Kirill Simonov29413ea2006-08-16 18:22:38 +00008import datetime
Kirill Simonovcc316a42006-04-11 00:34:16 +00009
Kirill Simonov21483f22006-12-08 15:36:53 +000010import sys, copy_reg, types
Kirill Simonov19e134b2006-04-18 14:35:28 +000011
Kirill Simonovcc316a42006-04-11 00:34:16 +000012class RepresenterError(YAMLError):
13 pass
14
Kirill Simonov8b083c62006-08-03 16:07:29 +000015class BaseRepresenter(object):
Kirill Simonovcc316a42006-04-11 00:34:16 +000016
Kirill Simonov6a97abb2006-04-15 23:54:52 +000017 yaml_representers = {}
Kirill Simonovc87ce162006-04-22 20:40:43 +000018 yaml_multi_representers = {}
Kirill Simonovcc316a42006-04-11 00:34:16 +000019
Tina Müller507a4642019-02-23 22:24:40 +010020 def __init__(self, default_style=None, default_flow_style=False, sort_keys=True):
Kirill Simonov80ba4502006-05-04 10:46:11 +000021 self.default_style = default_style
22 self.default_flow_style = default_flow_style
Tina Müller07c88c62018-03-16 23:25:44 +010023 self.sort_keys = sort_keys
Kirill Simonovcc316a42006-04-11 00:34:16 +000024 self.represented_objects = {}
Kirill Simonov8b083c62006-08-03 16:07:29 +000025 self.object_keeper = []
26 self.alias_key = None
Kirill Simonovcc316a42006-04-11 00:34:16 +000027
Kirill Simonov6a97abb2006-04-15 23:54:52 +000028 def represent(self, data):
Kirill Simonovc87ce162006-04-22 20:40:43 +000029 node = self.represent_data(data)
Kirill Simonov6a97abb2006-04-15 23:54:52 +000030 self.serialize(node)
Kirill Simonovcc316a42006-04-11 00:34:16 +000031 self.represented_objects = {}
Kirill Simonov8b083c62006-08-03 16:07:29 +000032 self.object_keeper = []
33 self.alias_key = None
Kirill Simonovcc316a42006-04-11 00:34:16 +000034
Kirill Simonov19e134b2006-04-18 14:35:28 +000035 def get_classobj_bases(self, cls):
36 bases = [cls]
37 for base in cls.__bases__:
38 bases.extend(self.get_classobj_bases(base))
39 return bases
40
Kirill Simonovc87ce162006-04-22 20:40:43 +000041 def represent_data(self, data):
Kirill Simonov6a97abb2006-04-15 23:54:52 +000042 if self.ignore_aliases(data):
Kirill Simonov8b083c62006-08-03 16:07:29 +000043 self.alias_key = None
Kirill Simonovcc316a42006-04-11 00:34:16 +000044 else:
Kirill Simonov8b083c62006-08-03 16:07:29 +000045 self.alias_key = id(data)
46 if self.alias_key is not None:
47 if self.alias_key in self.represented_objects:
48 node = self.represented_objects[self.alias_key]
49 #if node is None:
50 # raise RepresenterError("recursive objects are not allowed: %r" % data)
Kirill Simonovcc316a42006-04-11 00:34:16 +000051 return node
Kirill Simonov8b083c62006-08-03 16:07:29 +000052 #self.represented_objects[alias_key] = None
53 self.object_keeper.append(data)
Kirill Simonov19e134b2006-04-18 14:35:28 +000054 data_types = type(data).__mro__
Kirill Simonov21483f22006-12-08 15:36:53 +000055 if type(data) is types.InstanceType:
Kirill Simonov80ed3f12006-04-18 19:33:16 +000056 data_types = self.get_classobj_bases(data.__class__)+list(data_types)
Kirill Simonovc87ce162006-04-22 20:40:43 +000057 if data_types[0] in self.yaml_representers:
58 node = self.yaml_representers[data_types[0]](self, data)
Kirill Simonovcc316a42006-04-11 00:34:16 +000059 else:
Kirill Simonovc87ce162006-04-22 20:40:43 +000060 for data_type in data_types:
61 if data_type in self.yaml_multi_representers:
62 node = self.yaml_multi_representers[data_type](self, data)
63 break
Kirill Simonovcc316a42006-04-11 00:34:16 +000064 else:
Kirill Simonovc87ce162006-04-22 20:40:43 +000065 if None in self.yaml_multi_representers:
66 node = self.yaml_multi_representers[None](self, data)
67 elif None in self.yaml_representers:
68 node = self.yaml_representers[None](self, data)
69 else:
70 node = ScalarNode(None, unicode(data))
Kirill Simonov8b083c62006-08-03 16:07:29 +000071 #if alias_key is not None:
72 # self.represented_objects[alias_key] = node
Kirill Simonovcc316a42006-04-11 00:34:16 +000073 return node
74
Kirill Simonov6a97abb2006-04-15 23:54:52 +000075 def add_representer(cls, data_type, representer):
Kirill Simonovcc316a42006-04-11 00:34:16 +000076 if not 'yaml_representers' in cls.__dict__:
77 cls.yaml_representers = cls.yaml_representers.copy()
Kirill Simonov6a97abb2006-04-15 23:54:52 +000078 cls.yaml_representers[data_type] = representer
Kirill Simonovcc316a42006-04-11 00:34:16 +000079 add_representer = classmethod(add_representer)
80
Kirill Simonovc87ce162006-04-22 20:40:43 +000081 def add_multi_representer(cls, data_type, representer):
82 if not 'yaml_multi_representers' in cls.__dict__:
83 cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
84 cls.yaml_multi_representers[data_type] = representer
85 add_multi_representer = classmethod(add_multi_representer)
86
Kirill Simonovcc316a42006-04-11 00:34:16 +000087 def represent_scalar(self, tag, value, style=None):
Kirill Simonov80ba4502006-05-04 10:46:11 +000088 if style is None:
89 style = self.default_style
Kirill Simonov8b083c62006-08-03 16:07:29 +000090 node = ScalarNode(tag, value, style=style)
91 if self.alias_key is not None:
92 self.represented_objects[self.alias_key] = node
93 return node
Kirill Simonovcc316a42006-04-11 00:34:16 +000094
95 def represent_sequence(self, tag, sequence, flow_style=None):
Kirill Simonovcc316a42006-04-11 00:34:16 +000096 value = []
Kirill Simonov8b083c62006-08-03 16:07:29 +000097 node = SequenceNode(tag, value, flow_style=flow_style)
98 if self.alias_key is not None:
99 self.represented_objects[self.alias_key] = node
100 best_style = True
Kirill Simonovcc316a42006-04-11 00:34:16 +0000101 for item in sequence:
Kirill Simonovc87ce162006-04-22 20:40:43 +0000102 node_item = self.represent_data(item)
103 if not (isinstance(node_item, ScalarNode) and not node_item.style):
104 best_style = False
Kirill Simonov8b083c62006-08-03 16:07:29 +0000105 value.append(node_item)
Kirill Simonovc87ce162006-04-22 20:40:43 +0000106 if flow_style is None:
Kirill Simonov8b083c62006-08-03 16:07:29 +0000107 if self.default_flow_style is not None:
108 node.flow_style = self.default_flow_style
109 else:
110 node.flow_style = best_style
111 return node
Kirill Simonovcc316a42006-04-11 00:34:16 +0000112
113 def represent_mapping(self, tag, mapping, flow_style=None):
Kirill Simonov8b083c62006-08-03 16:07:29 +0000114 value = []
115 node = MappingNode(tag, value, flow_style=flow_style)
116 if self.alias_key is not None:
117 self.represented_objects[self.alias_key] = node
Kirill Simonovc87ce162006-04-22 20:40:43 +0000118 best_style = True
Kirill Simonov8b083c62006-08-03 16:07:29 +0000119 if hasattr(mapping, 'items'):
120 mapping = mapping.items()
Tina Müller07c88c62018-03-16 23:25:44 +0100121 if self.sort_keys:
122 mapping.sort()
Kirill Simonov8b083c62006-08-03 16:07:29 +0000123 for item_key, item_value in mapping:
124 node_key = self.represent_data(item_key)
125 node_value = self.represent_data(item_value)
126 if not (isinstance(node_key, ScalarNode) and not node_key.style):
127 best_style = False
128 if not (isinstance(node_value, ScalarNode) and not node_value.style):
129 best_style = False
130 value.append((node_key, node_value))
Kirill Simonovc87ce162006-04-22 20:40:43 +0000131 if flow_style is None:
Kirill Simonov8b083c62006-08-03 16:07:29 +0000132 if self.default_flow_style is not None:
133 node.flow_style = self.default_flow_style
134 else:
135 node.flow_style = best_style
136 return node
Kirill Simonovcc316a42006-04-11 00:34:16 +0000137
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000138 def ignore_aliases(self, data):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000139 return False
140
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000141class SafeRepresenter(BaseRepresenter):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000142
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000143 def ignore_aliases(self, data):
Kirill Simonovf10d92f2016-08-25 16:27:19 -0500144 if data is None:
145 return True
146 if isinstance(data, tuple) and data == ():
Kirill Simonovcc316a42006-04-11 00:34:16 +0000147 return True
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000148 if isinstance(data, (str, unicode, bool, int, float)):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000149 return True
150
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000151 def represent_none(self, data):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000152 return self.represent_scalar(u'tag:yaml.org,2002:null',
153 u'null')
154
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000155 def represent_str(self, data):
Kirill Simonov19e134b2006-04-18 14:35:28 +0000156 tag = None
157 style = None
Kirill Simonovcc316a42006-04-11 00:34:16 +0000158 try:
Kirill Simonov19e134b2006-04-18 14:35:28 +0000159 data = unicode(data, 'ascii')
160 tag = u'tag:yaml.org,2002:str'
Kirill Simonovfcf5d612006-04-12 22:26:41 +0000161 except UnicodeDecodeError:
162 try:
Kirill Simonov19e134b2006-04-18 14:35:28 +0000163 data = unicode(data, 'utf-8')
164 tag = u'tag:yaml.org,2002:str'
Kirill Simonovfcf5d612006-04-12 22:26:41 +0000165 except UnicodeDecodeError:
Kirill Simonov19e134b2006-04-18 14:35:28 +0000166 data = data.encode('base64')
167 tag = u'tag:yaml.org,2002:binary'
168 style = '|'
169 return self.represent_scalar(tag, data, style=style)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000170
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000171 def represent_unicode(self, data):
172 return self.represent_scalar(u'tag:yaml.org,2002:str', data)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000173
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000174 def represent_bool(self, data):
175 if data:
Kirill Simonovcc316a42006-04-11 00:34:16 +0000176 value = u'true'
177 else:
178 value = u'false'
179 return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
180
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000181 def represent_int(self, data):
182 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
Kirill Simonovcc316a42006-04-11 00:34:16 +0000183
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000184 def represent_long(self, data):
185 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
Kirill Simonovcc316a42006-04-11 00:34:16 +0000186
Kirill Simonov810977b2006-05-15 18:43:58 +0000187 inf_value = 1e300
188 while repr(inf_value) != repr(inf_value*inf_value):
189 inf_value *= inf_value
Kirill Simonovcc316a42006-04-11 00:34:16 +0000190
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000191 def represent_float(self, data):
Kirill Simonov500659d2006-05-22 19:49:54 +0000192 if data != data or (data == 0.0 and data == 1.0):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000193 value = u'.nan'
Kirill Simonov500659d2006-05-22 19:49:54 +0000194 elif data == self.inf_value:
195 value = u'.inf'
196 elif data == -self.inf_value:
197 value = u'-.inf'
Kirill Simonovcc316a42006-04-11 00:34:16 +0000198 else:
Kirill Simonov96fcba32007-03-22 16:13:32 +0000199 value = unicode(repr(data)).lower()
200 # Note that in some cases `repr(data)` represents a float number
201 # without the decimal parts. For instance:
202 # >>> repr(1e17)
203 # '1e17'
204 # Unfortunately, this is not a valid float representation according
205 # to the definition of the `!!float` tag. We fix this by adding
206 # '.0' before the 'e' symbol.
207 if u'.' not in value and u'e' in value:
208 value = value.replace(u'e', u'.0e', 1)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000209 return self.represent_scalar(u'tag:yaml.org,2002:float', value)
210
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000211 def represent_list(self, data):
Kirill Simonov8b083c62006-08-03 16:07:29 +0000212 #pairs = (len(data) > 0 and isinstance(data, list))
213 #if pairs:
214 # for item in data:
215 # if not isinstance(item, tuple) or len(item) != 2:
216 # pairs = False
217 # break
218 #if not pairs:
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000219 return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
Kirill Simonov8b083c62006-08-03 16:07:29 +0000220 #value = []
221 #for item_key, item_value in data:
222 # value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
223 # [(item_key, item_value)]))
224 #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000225
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000226 def represent_dict(self, data):
227 return self.represent_mapping(u'tag:yaml.org,2002:map', data)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000228
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000229 def represent_set(self, data):
Kirill Simonovcc316a42006-04-11 00:34:16 +0000230 value = {}
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000231 for key in data:
Kirill Simonovcc316a42006-04-11 00:34:16 +0000232 value[key] = None
233 return self.represent_mapping(u'tag:yaml.org,2002:set', value)
234
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000235 def represent_date(self, data):
Kirill Simonov29413ea2006-08-16 18:22:38 +0000236 value = unicode(data.isoformat())
Kirill Simonovcc316a42006-04-11 00:34:16 +0000237 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
238
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000239 def represent_datetime(self, data):
Kirill Simonov29413ea2006-08-16 18:22:38 +0000240 value = unicode(data.isoformat(' '))
Kirill Simonovcc316a42006-04-11 00:34:16 +0000241 return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
242
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000243 def represent_yaml_object(self, tag, data, cls, flow_style=None):
244 if hasattr(data, '__getstate__'):
245 state = data.__getstate__()
246 else:
247 state = data.__dict__.copy()
Kirill Simonov19e134b2006-04-18 14:35:28 +0000248 return self.represent_mapping(tag, state, flow_style=flow_style)
Kirill Simonov6a97abb2006-04-15 23:54:52 +0000249
250 def represent_undefined(self, data):
Timofei Bondarevef744d82017-03-01 00:45:09 -0500251 raise RepresenterError("cannot represent an object", data)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000252
253SafeRepresenter.add_representer(type(None),
254 SafeRepresenter.represent_none)
255
256SafeRepresenter.add_representer(str,
257 SafeRepresenter.represent_str)
258
259SafeRepresenter.add_representer(unicode,
260 SafeRepresenter.represent_unicode)
261
262SafeRepresenter.add_representer(bool,
263 SafeRepresenter.represent_bool)
264
265SafeRepresenter.add_representer(int,
266 SafeRepresenter.represent_int)
267
268SafeRepresenter.add_representer(long,
269 SafeRepresenter.represent_long)
270
271SafeRepresenter.add_representer(float,
272 SafeRepresenter.represent_float)
273
274SafeRepresenter.add_representer(list,
275 SafeRepresenter.represent_list)
276
Kirill Simonov19e134b2006-04-18 14:35:28 +0000277SafeRepresenter.add_representer(tuple,
278 SafeRepresenter.represent_list)
279
Kirill Simonovcc316a42006-04-11 00:34:16 +0000280SafeRepresenter.add_representer(dict,
281 SafeRepresenter.represent_dict)
282
283SafeRepresenter.add_representer(set,
284 SafeRepresenter.represent_set)
285
Kirill Simonov29413ea2006-08-16 18:22:38 +0000286SafeRepresenter.add_representer(datetime.date,
287 SafeRepresenter.represent_date)
Kirill Simonov235ab982008-12-29 17:24:05 +0000288
Kirill Simonov29413ea2006-08-16 18:22:38 +0000289SafeRepresenter.add_representer(datetime.datetime,
290 SafeRepresenter.represent_datetime)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000291
292SafeRepresenter.add_representer(None,
293 SafeRepresenter.represent_undefined)
294
295class Representer(SafeRepresenter):
Kirill Simonovc87ce162006-04-22 20:40:43 +0000296
Kirill Simonov19e134b2006-04-18 14:35:28 +0000297 def represent_str(self, data):
298 tag = None
299 style = None
300 try:
301 data = unicode(data, 'ascii')
302 tag = u'tag:yaml.org,2002:str'
303 except UnicodeDecodeError:
304 try:
305 data = unicode(data, 'utf-8')
306 tag = u'tag:yaml.org,2002:python/str'
307 except UnicodeDecodeError:
308 data = data.encode('base64')
309 tag = u'tag:yaml.org,2002:binary'
310 style = '|'
311 return self.represent_scalar(tag, data, style=style)
312
313 def represent_unicode(self, data):
314 tag = None
315 try:
316 data.encode('ascii')
317 tag = u'tag:yaml.org,2002:python/unicode'
318 except UnicodeEncodeError:
319 tag = u'tag:yaml.org,2002:str'
320 return self.represent_scalar(tag, data)
321
322 def represent_long(self, data):
323 tag = u'tag:yaml.org,2002:int'
324 if int(data) is not data:
325 tag = u'tag:yaml.org,2002:python/long'
326 return self.represent_scalar(tag, unicode(data))
327
328 def represent_complex(self, data):
Kirill Simonov80ed3f12006-04-18 19:33:16 +0000329 if data.imag == 0.0:
330 data = u'%r' % data.real
331 elif data.real == 0.0:
332 data = u'%rj' % data.imag
333 elif data.imag > 0:
Kirill Simonov19e134b2006-04-18 14:35:28 +0000334 data = u'%r+%rj' % (data.real, data.imag)
335 else:
Kirill Simonov80ed3f12006-04-18 19:33:16 +0000336 data = u'%r%rj' % (data.real, data.imag)
Kirill Simonov19e134b2006-04-18 14:35:28 +0000337 return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
338
339 def represent_tuple(self, data):
340 return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
341
342 def represent_name(self, data):
343 name = u'%s.%s' % (data.__module__, data.__name__)
344 return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'')
345
346 def represent_module(self, data):
347 return self.represent_scalar(
348 u'tag:yaml.org,2002:python/module:'+data.__name__, u'')
349
Kirill Simonovc87ce162006-04-22 20:40:43 +0000350 def represent_instance(self, data):
351 # For instances of classic classes, we use __getinitargs__ and
352 # __getstate__ to serialize the data.
353
354 # If data.__getinitargs__ exists, the object must be reconstructed by
355 # calling cls(**args), where args is a tuple returned by
356 # __getinitargs__. Otherwise, the cls.__init__ method should never be
357 # called and the class instance is created by instantiating a trivial
358 # class and assigning to the instance's __class__ variable.
359
360 # If data.__getstate__ exists, it returns the state of the object.
361 # Otherwise, the state of the object is data.__dict__.
362
363 # We produce either a !!python/object or !!python/object/new node.
364 # If data.__getinitargs__ does not exist and state is a dictionary, we
365 # produce a !!python/object node . Otherwise we produce a
366 # !!python/object/new node.
367
368 cls = data.__class__
369 class_name = u'%s.%s' % (cls.__module__, cls.__name__)
370 args = None
371 state = None
372 if hasattr(data, '__getinitargs__'):
373 args = list(data.__getinitargs__())
374 if hasattr(data, '__getstate__'):
375 state = data.__getstate__()
376 else:
377 state = data.__dict__
378 if args is None and isinstance(state, dict):
379 return self.represent_mapping(
380 u'tag:yaml.org,2002:python/object:'+class_name, state)
381 if isinstance(state, dict) and not state:
382 return self.represent_sequence(
383 u'tag:yaml.org,2002:python/object/new:'+class_name, args)
384 value = {}
385 if args:
386 value['args'] = args
387 value['state'] = state
388 return self.represent_mapping(
389 u'tag:yaml.org,2002:python/object/new:'+class_name, value)
390
391 def represent_object(self, data):
392 # We use __reduce__ API to save the data. data.__reduce__ returns
393 # a tuple of length 2-5:
394 # (function, args, state, listitems, dictitems)
395
396 # For reconstructing, we calls function(*args), then set its state,
397 # listitems, and dictitems if they are not None.
398
399 # A special case is when function.__name__ == '__newobj__'. In this
400 # case we create the object with args[0].__new__(*args).
401
402 # Another special case is when __reduce__ returns a string - we don't
403 # support it.
404
405 # We produce a !!python/object, !!python/object/new or
406 # !!python/object/apply node.
407
408 cls = type(data)
409 if cls in copy_reg.dispatch_table:
Kirill Simonov056fe5c2006-07-11 11:32:39 +0000410 reduce = copy_reg.dispatch_table[cls](data)
Kirill Simonovc87ce162006-04-22 20:40:43 +0000411 elif hasattr(data, '__reduce_ex__'):
412 reduce = data.__reduce_ex__(2)
413 elif hasattr(data, '__reduce__'):
414 reduce = data.__reduce__()
415 else:
Timofei Bondarevef744d82017-03-01 00:45:09 -0500416 raise RepresenterError("cannot represent an object", data)
Kirill Simonovc87ce162006-04-22 20:40:43 +0000417 reduce = (list(reduce)+[None]*5)[:5]
418 function, args, state, listitems, dictitems = reduce
419 args = list(args)
420 if state is None:
421 state = {}
422 if listitems is not None:
423 listitems = list(listitems)
424 if dictitems is not None:
425 dictitems = dict(dictitems)
426 if function.__name__ == '__newobj__':
427 function = args[0]
428 args = args[1:]
429 tag = u'tag:yaml.org,2002:python/object/new:'
430 newobj = True
431 else:
432 tag = u'tag:yaml.org,2002:python/object/apply:'
433 newobj = False
434 function_name = u'%s.%s' % (function.__module__, function.__name__)
435 if not args and not listitems and not dictitems \
436 and isinstance(state, dict) and newobj:
437 return self.represent_mapping(
438 u'tag:yaml.org,2002:python/object:'+function_name, state)
439 if not listitems and not dictitems \
440 and isinstance(state, dict) and not state:
441 return self.represent_sequence(tag+function_name, args)
442 value = {}
443 if args:
444 value['args'] = args
445 if state or not isinstance(state, dict):
446 value['state'] = state
447 if listitems:
448 value['listitems'] = listitems
449 if dictitems:
450 value['dictitems'] = dictitems
451 return self.represent_mapping(tag+function_name, value)
452
Kirill Simonov19e134b2006-04-18 14:35:28 +0000453Representer.add_representer(str,
454 Representer.represent_str)
455
456Representer.add_representer(unicode,
457 Representer.represent_unicode)
458
459Representer.add_representer(long,
460 Representer.represent_long)
461
462Representer.add_representer(complex,
463 Representer.represent_complex)
464
465Representer.add_representer(tuple,
466 Representer.represent_tuple)
467
468Representer.add_representer(type,
469 Representer.represent_name)
470
Kirill Simonov21483f22006-12-08 15:36:53 +0000471Representer.add_representer(types.ClassType,
Kirill Simonov19e134b2006-04-18 14:35:28 +0000472 Representer.represent_name)
473
Kirill Simonov21483f22006-12-08 15:36:53 +0000474Representer.add_representer(types.FunctionType,
Kirill Simonov19e134b2006-04-18 14:35:28 +0000475 Representer.represent_name)
476
Kirill Simonov21483f22006-12-08 15:36:53 +0000477Representer.add_representer(types.BuiltinFunctionType,
Kirill Simonov19e134b2006-04-18 14:35:28 +0000478 Representer.represent_name)
479
Kirill Simonov21483f22006-12-08 15:36:53 +0000480Representer.add_representer(types.ModuleType,
Kirill Simonov19e134b2006-04-18 14:35:28 +0000481 Representer.represent_module)
Kirill Simonovcc316a42006-04-11 00:34:16 +0000482
Kirill Simonov21483f22006-12-08 15:36:53 +0000483Representer.add_multi_representer(types.InstanceType,
Kirill Simonovc87ce162006-04-22 20:40:43 +0000484 Representer.represent_instance)
485
486Representer.add_multi_representer(object,
487 Representer.represent_object)
488