| [133] | 1 | |
|---|
| 2 | __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer', |
|---|
| 3 | 'RepresenterError'] |
|---|
| 4 | |
|---|
| 5 | from error import * |
|---|
| 6 | from nodes import * |
|---|
| 7 | |
|---|
| [225] | 8 | import datetime |
|---|
| [133] | 9 | |
|---|
| 10 | try: |
|---|
| 11 | set |
|---|
| 12 | except NameError: |
|---|
| 13 | from sets import Set as set |
|---|
| 14 | |
|---|
| [147] | 15 | import sys, copy_reg |
|---|
| [139] | 16 | |
|---|
| [133] | 17 | class RepresenterError(YAMLError): |
|---|
| 18 | pass |
|---|
| 19 | |
|---|
| [222] | 20 | class BaseRepresenter(object): |
|---|
| [133] | 21 | |
|---|
| [136] | 22 | yaml_representers = {} |
|---|
| [147] | 23 | yaml_multi_representers = {} |
|---|
| [133] | 24 | |
|---|
| [152] | 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 |
|---|
| [133] | 28 | self.represented_objects = {} |
|---|
| [222] | 29 | self.object_keeper = [] |
|---|
| 30 | self.alias_key = None |
|---|
| [133] | 31 | |
|---|
| [136] | 32 | def represent(self, data): |
|---|
| [147] | 33 | node = self.represent_data(data) |
|---|
| [136] | 34 | self.serialize(node) |
|---|
| [133] | 35 | self.represented_objects = {} |
|---|
| [222] | 36 | self.object_keeper = [] |
|---|
| 37 | self.alias_key = None |
|---|
| [133] | 38 | |
|---|
| [139] | 39 | class C: pass |
|---|
| 40 | c = C() |
|---|
| 41 | def f(): pass |
|---|
| [222] | 42 | def g(): yield None |
|---|
| [139] | 43 | classobj_type = type(C) |
|---|
| 44 | instance_type = type(c) |
|---|
| 45 | function_type = type(f) |
|---|
| [222] | 46 | generator_type = type(g()) |
|---|
| [139] | 47 | builtin_function_type = type(abs) |
|---|
| 48 | module_type = type(sys) |
|---|
| [222] | 49 | del C, c, f, g |
|---|
| [139] | 50 | |
|---|
| 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 |
|---|
| 56 | |
|---|
| [147] | 57 | def represent_data(self, data): |
|---|
| [136] | 58 | if self.ignore_aliases(data): |
|---|
| [222] | 59 | self.alias_key = None |
|---|
| [133] | 60 | else: |
|---|
| [222] | 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) |
|---|
| [133] | 67 | return node |
|---|
| [222] | 68 | #self.represented_objects[alias_key] = None |
|---|
| 69 | self.object_keeper.append(data) |
|---|
| [139] | 70 | data_types = type(data).__mro__ |
|---|
| 71 | if type(data) is self.instance_type: |
|---|
| [143] | 72 | data_types = self.get_classobj_bases(data.__class__)+list(data_types) |
|---|
| [147] | 73 | if data_types[0] in self.yaml_representers: |
|---|
| 74 | node = self.yaml_representers[data_types[0]](self, data) |
|---|
| [133] | 75 | else: |
|---|
| [147] | 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 |
|---|
| [133] | 80 | else: |
|---|
| [147] | 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)) |
|---|
| [222] | 87 | #if alias_key is not None: |
|---|
| 88 | # self.represented_objects[alias_key] = node |
|---|
| [133] | 89 | return node |
|---|
| 90 | |
|---|
| [136] | 91 | def add_representer(cls, data_type, representer): |
|---|
| [133] | 92 | if not 'yaml_representers' in cls.__dict__: |
|---|
| 93 | cls.yaml_representers = cls.yaml_representers.copy() |
|---|
| [136] | 94 | cls.yaml_representers[data_type] = representer |
|---|
| [133] | 95 | add_representer = classmethod(add_representer) |
|---|
| 96 | |
|---|
| [147] | 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) |
|---|
| 102 | |
|---|
| [133] | 103 | def represent_scalar(self, tag, value, style=None): |
|---|
| [152] | 104 | if style is None: |
|---|
| 105 | style = self.default_style |
|---|
| [222] | 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 |
|---|
| [133] | 110 | |
|---|
| 111 | def represent_sequence(self, tag, sequence, flow_style=None): |
|---|
| [222] | 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 |
|---|
| [147] | 116 | best_style = True |
|---|
| [133] | 117 | for item in sequence: |
|---|
| [147] | 118 | node_item = self.represent_data(item) |
|---|
| 119 | if not (isinstance(node_item, ScalarNode) and not node_item.style): |
|---|
| 120 | best_style = False |
|---|
| [222] | 121 | value.append(node_item) |
|---|
| [147] | 122 | if flow_style is None: |
|---|
| [222] | 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 |
|---|
| [133] | 128 | |
|---|
| 129 | def represent_mapping(self, tag, mapping, flow_style=None): |
|---|
| [222] | 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 |
|---|
| [147] | 134 | best_style = True |
|---|
| [222] | 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)) |
|---|
| [147] | 146 | if flow_style is None: |
|---|
| [222] | 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 |
|---|
| [133] | 152 | |
|---|
| [136] | 153 | def ignore_aliases(self, data): |
|---|
| [133] | 154 | return False |
|---|
| 155 | |
|---|
| [136] | 156 | class SafeRepresenter(BaseRepresenter): |
|---|
| [133] | 157 | |
|---|
| [136] | 158 | def ignore_aliases(self, data): |
|---|
| 159 | if data in [None, ()]: |
|---|
| [133] | 160 | return True |
|---|
| [136] | 161 | if isinstance(data, (str, unicode, bool, int, float)): |
|---|
| [133] | 162 | return True |
|---|
| 163 | |
|---|
| [136] | 164 | def represent_none(self, data): |
|---|
| [133] | 165 | return self.represent_scalar(u'tag:yaml.org,2002:null', |
|---|
| 166 | u'null') |
|---|
| 167 | |
|---|
| [136] | 168 | def represent_str(self, data): |
|---|
| [139] | 169 | tag = None |
|---|
| 170 | style = None |
|---|
| [133] | 171 | try: |
|---|
| [139] | 172 | data = unicode(data, 'ascii') |
|---|
| 173 | tag = u'tag:yaml.org,2002:str' |
|---|
| [135] | 174 | except UnicodeDecodeError: |
|---|
| 175 | try: |
|---|
| [139] | 176 | data = unicode(data, 'utf-8') |
|---|
| 177 | tag = u'tag:yaml.org,2002:str' |
|---|
| [135] | 178 | except UnicodeDecodeError: |
|---|
| [139] | 179 | data = data.encode('base64') |
|---|
| 180 | tag = u'tag:yaml.org,2002:binary' |
|---|
| 181 | style = '|' |
|---|
| 182 | return self.represent_scalar(tag, data, style=style) |
|---|
| [133] | 183 | |
|---|
| [136] | 184 | def represent_unicode(self, data): |
|---|
| 185 | return self.represent_scalar(u'tag:yaml.org,2002:str', data) |
|---|
| [133] | 186 | |
|---|
| [136] | 187 | def represent_bool(self, data): |
|---|
| 188 | if data: |
|---|
| [133] | 189 | value = u'true' |
|---|
| 190 | else: |
|---|
| 191 | value = u'false' |
|---|
| 192 | return self.represent_scalar(u'tag:yaml.org,2002:bool', value) |
|---|
| 193 | |
|---|
| [136] | 194 | def represent_int(self, data): |
|---|
| 195 | return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) |
|---|
| [133] | 196 | |
|---|
| [136] | 197 | def represent_long(self, data): |
|---|
| 198 | return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) |
|---|
| [133] | 199 | |
|---|
| [168] | 200 | inf_value = 1e300 |
|---|
| 201 | while repr(inf_value) != repr(inf_value*inf_value): |
|---|
| 202 | inf_value *= inf_value |
|---|
| [133] | 203 | |
|---|
| [136] | 204 | def represent_float(self, data): |
|---|
| [173] | 205 | if data != data or (data == 0.0 and data == 1.0): |
|---|
| 206 | value = u'.nan' |
|---|
| 207 | elif data == self.inf_value: |
|---|
| [133] | 208 | value = u'.inf' |
|---|
| [173] | 209 | elif data == -self.inf_value: |
|---|
| [133] | 210 | value = u'-.inf' |
|---|
| 211 | else: |
|---|
| [173] | 212 | value = unicode(repr(data)) |
|---|
| [133] | 213 | return self.represent_scalar(u'tag:yaml.org,2002:float', value) |
|---|
| 214 | |
|---|
| [136] | 215 | def represent_list(self, data): |
|---|
| [222] | 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: |
|---|
| [136] | 223 | return self.represent_sequence(u'tag:yaml.org,2002:seq', data) |
|---|
| [222] | 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) |
|---|
| [133] | 229 | |
|---|
| [136] | 230 | def represent_dict(self, data): |
|---|
| 231 | return self.represent_mapping(u'tag:yaml.org,2002:map', data) |
|---|
| [133] | 232 | |
|---|
| [136] | 233 | def represent_set(self, data): |
|---|
| [133] | 234 | value = {} |
|---|
| [136] | 235 | for key in data: |
|---|
| [133] | 236 | value[key] = None |
|---|
| 237 | return self.represent_mapping(u'tag:yaml.org,2002:set', value) |
|---|
| 238 | |
|---|
| [136] | 239 | def represent_date(self, data): |
|---|
| [225] | 240 | value = unicode(data.isoformat()) |
|---|
| [133] | 241 | return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) |
|---|
| 242 | |
|---|
| [136] | 243 | def represent_datetime(self, data): |
|---|
| [225] | 244 | value = unicode(data.isoformat(' ')) |
|---|
| [133] | 245 | return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) |
|---|
| 246 | |
|---|
| [136] | 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() |
|---|
| [139] | 252 | return self.represent_mapping(tag, state, flow_style=flow_style) |
|---|
| [133] | 253 | |
|---|
| [136] | 254 | def represent_undefined(self, data): |
|---|
| 255 | raise RepresenterError("cannot represent an object: %s" % data) |
|---|
| 256 | |
|---|
| [133] | 257 | SafeRepresenter.add_representer(type(None), |
|---|
| 258 | SafeRepresenter.represent_none) |
|---|
| 259 | |
|---|
| 260 | SafeRepresenter.add_representer(str, |
|---|
| 261 | SafeRepresenter.represent_str) |
|---|
| 262 | |
|---|
| 263 | SafeRepresenter.add_representer(unicode, |
|---|
| 264 | SafeRepresenter.represent_unicode) |
|---|
| 265 | |
|---|
| 266 | SafeRepresenter.add_representer(bool, |
|---|
| 267 | SafeRepresenter.represent_bool) |
|---|
| 268 | |
|---|
| 269 | SafeRepresenter.add_representer(int, |
|---|
| 270 | SafeRepresenter.represent_int) |
|---|
| 271 | |
|---|
| 272 | SafeRepresenter.add_representer(long, |
|---|
| 273 | SafeRepresenter.represent_long) |
|---|
| 274 | |
|---|
| 275 | SafeRepresenter.add_representer(float, |
|---|
| 276 | SafeRepresenter.represent_float) |
|---|
| 277 | |
|---|
| 278 | SafeRepresenter.add_representer(list, |
|---|
| 279 | SafeRepresenter.represent_list) |
|---|
| 280 | |
|---|
| [139] | 281 | SafeRepresenter.add_representer(tuple, |
|---|
| 282 | SafeRepresenter.represent_list) |
|---|
| 283 | |
|---|
| [133] | 284 | SafeRepresenter.add_representer(dict, |
|---|
| 285 | SafeRepresenter.represent_dict) |
|---|
| 286 | |
|---|
| 287 | SafeRepresenter.add_representer(set, |
|---|
| 288 | SafeRepresenter.represent_set) |
|---|
| 289 | |
|---|
| [225] | 290 | SafeRepresenter.add_representer(datetime.date, |
|---|
| 291 | SafeRepresenter.represent_date) |
|---|
| 292 | SafeRepresenter.add_representer(datetime.datetime, |
|---|
| 293 | SafeRepresenter.represent_datetime) |
|---|
| [133] | 294 | |
|---|
| 295 | SafeRepresenter.add_representer(None, |
|---|
| 296 | SafeRepresenter.represent_undefined) |
|---|
| 297 | |
|---|
| 298 | class Representer(SafeRepresenter): |
|---|
| [147] | 299 | |
|---|
| [139] | 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) |
|---|
| [133] | 315 | |
|---|
| [139] | 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) |
|---|
| 324 | |
|---|
| 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)) |
|---|
| 330 | |
|---|
| 331 | def represent_complex(self, data): |
|---|
| [143] | 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: |
|---|
| [139] | 337 | data = u'%r+%rj' % (data.real, data.imag) |
|---|
| 338 | else: |
|---|
| [143] | 339 | data = u'%r%rj' % (data.real, data.imag) |
|---|
| [139] | 340 | return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data) |
|---|
| 341 | |
|---|
| 342 | def represent_tuple(self, data): |
|---|
| 343 | return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data) |
|---|
| 344 | |
|---|
| 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'') |
|---|
| 348 | |
|---|
| 349 | def represent_module(self, data): |
|---|
| 350 | return self.represent_scalar( |
|---|
| 351 | u'tag:yaml.org,2002:python/module:'+data.__name__, u'') |
|---|
| 352 | |
|---|
| [147] | 353 | def represent_instance(self, data): |
|---|
| 354 | # For instances of classic classes, we use __getinitargs__ and |
|---|
| 355 | # __getstate__ to serialize the data. |
|---|
| 356 | |
|---|
| 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. |
|---|
| 362 | |
|---|
| 363 | # If data.__getstate__ exists, it returns the state of the object. |
|---|
| 364 | # Otherwise, the state of the object is data.__dict__. |
|---|
| 365 | |
|---|
| 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. |
|---|
| 370 | |
|---|
| 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) |
|---|
| 393 | |
|---|
| 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) |
|---|
| 398 | |
|---|
| 399 | # For reconstructing, we calls function(*args), then set its state, |
|---|
| 400 | # listitems, and dictitems if they are not None. |
|---|
| 401 | |
|---|
| 402 | # A special case is when function.__name__ == '__newobj__'. In this |
|---|
| 403 | # case we create the object with args[0].__new__(*args). |
|---|
| 404 | |
|---|
| 405 | # Another special case is when __reduce__ returns a string - we don't |
|---|
| 406 | # support it. |
|---|
| 407 | |
|---|
| 408 | # We produce a !!python/object, !!python/object/new or |
|---|
| 409 | # !!python/object/apply node. |
|---|
| 410 | |
|---|
| 411 | cls = type(data) |
|---|
| 412 | if cls in copy_reg.dispatch_table: |
|---|
| [206] | 413 | reduce = copy_reg.dispatch_table[cls](data) |
|---|
| [147] | 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) |
|---|
| 455 | |
|---|
| [139] | 456 | Representer.add_representer(str, |
|---|
| 457 | Representer.represent_str) |
|---|
| 458 | |
|---|
| 459 | Representer.add_representer(unicode, |
|---|
| 460 | Representer.represent_unicode) |
|---|
| 461 | |
|---|
| 462 | Representer.add_representer(long, |
|---|
| 463 | Representer.represent_long) |
|---|
| 464 | |
|---|
| 465 | Representer.add_representer(complex, |
|---|
| 466 | Representer.represent_complex) |
|---|
| 467 | |
|---|
| 468 | Representer.add_representer(tuple, |
|---|
| 469 | Representer.represent_tuple) |
|---|
| 470 | |
|---|
| 471 | Representer.add_representer(type, |
|---|
| 472 | Representer.represent_name) |
|---|
| 473 | |
|---|
| 474 | Representer.add_representer(Representer.classobj_type, |
|---|
| 475 | Representer.represent_name) |
|---|
| 476 | |
|---|
| 477 | Representer.add_representer(Representer.function_type, |
|---|
| 478 | Representer.represent_name) |
|---|
| 479 | |
|---|
| 480 | Representer.add_representer(Representer.builtin_function_type, |
|---|
| 481 | Representer.represent_name) |
|---|
| 482 | |
|---|
| 483 | Representer.add_representer(Representer.module_type, |
|---|
| 484 | Representer.represent_module) |
|---|
| 485 | |
|---|
| [147] | 486 | Representer.add_multi_representer(Representer.instance_type, |
|---|
| 487 | Representer.represent_instance) |
|---|
| 488 | |
|---|
| 489 | Representer.add_multi_representer(object, |
|---|
| 490 | Representer.represent_object) |
|---|
| 491 | |
|---|