Changeset 207 for pysyck/trunk/lib/syck/dumpers.py
- Timestamp:
- 07/11/06 07:34:01 (7 years ago)
- File:
-
- 1 edited
-
pysyck/trunk/lib/syck/dumpers.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pysyck/trunk/lib/syck/dumpers.py
r49 r207 10 10 except ImportError: 11 11 import StringIO 12 13 import copy_reg 12 14 13 15 __all__ = ['GenericDumper', 'Dumper', … … 198 200 tag="tag:python.yaml.org,2002:new:"+class_name) 199 201 200 def represent_object(self, object): # Do you understand this? I don't.202 def represent_object(self, object): # Borrowed from PyYAML. 201 203 cls = type(object) 202 class_name = '%s.%s' % (cls.__module__, cls.__name__) 203 args = () 204 state = {} 205 if cls.__reduce__ is type.__reduce__: 206 if hasattr(object, '__reduce_ex__'): 207 reduce = object.__reduce_ex__(2) 208 args = reduce[1][1:] 209 else: 210 reduce = object.__reduce__() 211 if len(reduce) > 2: 212 state = reduce[2] 213 if state is None: 214 state = {} 215 if not args and isinstance(state, dict): 216 return _syck.Map(state.copy(), 217 tag="tag:python.yaml.org,2002:object:"+class_name) 218 if not state and isinstance(state, dict): 219 return _syck.Seq(list(args), 220 tag="tag:python.yaml.org,2002:new:"+class_name) 221 value = {} 222 if args: 223 value['args'] = list(args) 224 if state or not isinstance(state, dict): 225 value['state'] = state 226 return _syck.Map(value, 227 tag="tag:python.yaml.org,2002:new:"+class_name) 228 else: 204 if cls in copy_reg.dispatch_table: 205 reduce = copy_reg.dispatch_table[cls](object) 206 elif hasattr(object, '__reduce_ex__'): 207 reduce = object.__reduce_ex__(2) 208 elif hasattr(object, '__reduce__'): 229 209 reduce = object.__reduce__() 230 cls = reduce[0] 231 class_name = '%s.%s' % (cls.__module__, cls.__name__) 232 args = reduce[1] 233 state = None 234 if len(reduce) > 2: 235 state = reduce[2] 236 if state is None: 237 state = {} 238 if not state and isinstance(state, dict): 239 return _syck.Seq(list(args), 240 tag="tag:python.yaml.org,2002:apply:"+class_name) 241 value = {} 242 if args: 243 value['args'] = list(args) 244 if state or not isinstance(state, dict): 245 value['state'] = state 246 return _syck.Map(value, 247 tag="tag:python.yaml.org,2002:apply:"+class_name) 210 else: 211 raise RuntimeError("cannot dump object: %r" % object) 212 reduce = (list(reduce)+[None]*3)[:3] 213 function, args, state = reduce 214 args = list(args) 215 if state is None: 216 state = {} 217 if function.__name__ == '__newobj__': 218 function = args[0] 219 args = args[1:] 220 tag = 'tag:python.yaml.org,2002:new:' 221 newobj = True 222 else: 223 tag = 'tag:python.yaml.org,2002:apply:' 224 newobj = False 225 function_name = '%s.%s' % (function.__module__, function.__name__) 226 if not args and isinstance(state, dict) and newobj: 227 return _syck.Map(state.copy(), 228 'tag:python.yaml.org,2002:object:'+function_name) 229 if isinstance(state, dict) and not state: 230 return _syck.Seq(args, tag+function_name) 231 value = {} 232 if args: 233 value['args'] = args 234 if state or not isinstance(state, dict): 235 value['state'] = state 236 return _syck.Map(value, tag+function_name) 248 237 249 238 def represent__syck_Node(self, object):
Note: See TracChangeset
for help on using the changeset viewer.
