Changes in trunk/lib/syck/dumpers.py [25:20]
- File:
-
- 1 edited
-
trunk/lib/syck/dumpers.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/syck/dumpers.py
r25 r20 1 """2 syck.dumpers is a high-level wrapper for the Syck YAML emitter.3 Do not use it directly, use the module 'syck' instead.4 """5 1 6 2 import _syck … … 14 10 'emit', 'dump', 'emit_documents', 'dump_documents'] 15 11 12 INF = 1e300000 13 NEGINF = -INF 14 NAN = INF/INF 15 16 16 17 class GenericDumper(_syck.Emitter): 17 """18 GenericDumper dumps native Python objects into YAML documents.19 """20 18 21 19 def dump(self, object): 22 """Dumps the given Python object as a YAML document."""23 20 self.emit(self._convert(object, {})) 24 21 25 22 def _convert(self, object, object_to_node): 26 23 if id(object) in object_to_node and self.allow_aliases(object): 27 return object_to_node[id(object)] [1]24 return object_to_node[id(object)] 28 25 node = self.represent(object) 29 object_to_node[id(object)] = object,node26 object_to_node[id(object)] = node 30 27 if node.kind == 'seq': 31 for index in range(len(node.value)): 32 item = node.value[index] 28 for index, item in enumerate(node.value): 33 29 node.value[index] = self._convert(item, object_to_node) 34 30 elif node.kind == 'map': 35 if isinstance(node.value, dict): 36 for key in node.value.keys(): 37 value = node.value[key] 38 del node.value[key] 39 node.value[self._convert(key, object_to_node)] = \ 40 self._convert(value, object_to_node) 41 elif isinstance(node.value, list): 42 for index in range(len(node.value)): 43 key, value = node.value[index] 44 node.value[index] = (self._convert(key, object_to_node), 45 self._convert(value, object_to_node)) 31 for key in node.value.keys(): 32 value = node.value[key] 33 del node.value[key] 34 node.value[self._convert(key, object_to_node)] = \ 35 self._convert(value, object_to_node) 46 36 # # Workaround against a Syck bug: 47 37 # if node.kind == 'scalar' and node.style not in ['1quote', '2quote'] \ … … 51 41 52 42 def represent(self, object): 53 """Represents the given Python object as a 'Node'."""54 43 if isinstance(object, dict): 55 44 return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") … … 60 49 61 50 def allow_aliases(self, object): 62 """Checks whether the given object can be aliased."""63 51 return True 64 52 65 53 class Dumper(GenericDumper): 66 """67 Dumper dumps native Python objects into YAML documents.68 """69 54 70 INF = 1e300000 71 inf_value = repr(INF) 72 neginf_value = repr(-INF) 73 nan_value = repr(INF/INF) 55 def __init__(self, *args, **kwds): 56 super(Dumper, self).__init__(*args, **kwds) 74 57 75 def find_representer(self, object): 76 """ 77 For the given object, find a method that can represent it as a 'Node' 78 object. 79 80 If the type of the object has the form 'package.module.type', 81 find_representer() returns the method 'represent_package_module_type'. 82 If this method does not exist, it checks the base types. 83 """ 58 def represent(self, object): 84 59 for object_type in type(object).__mro__: 85 60 if object_type.__module__ == '__builtin__': … … 87 62 else: 88 63 name = '%s.%s' % (object_type.__module__, object_type.__name__) 89 method = 'represent_ ' +name.replace('.', '_')64 method = 'represent_%s' % name.replace('.', '_') 90 65 if hasattr(self, method): 91 return getattr(self, method) 92 93 def represent(self, object): 94 """Represents the given Python object as a 'Node'.""" 95 representer = self.find_representer(object) 96 if representer: 97 return representer(object) 98 else: 99 return super(Dumper, self).represent(object) 66 return getattr(self, method)(object) 67 return super(Dumper, self).represent(object) 100 68 101 69 def represent_object(self, object): … … 120 88 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 121 89 90 def represent_int(self, object): 91 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 92 122 93 def represent_float(self, object): 123 94 value = repr(object) 124 if value == self.inf_value:95 if value == repr(INF): 125 96 value = '.inf' 126 elif value == self.neginf_value:97 elif value == repr(NEGINF): 127 98 value = '-.inf' 128 elif value == self.nan_value:99 elif value == repr(NAN): 129 100 value = '.nan' 130 101 return _syck.Scalar(value, tag="tag:yaml.org,2002:float") … … 132 103 def represent_sets_Set(self, object): 133 104 return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") 134 represent_set = represent_sets_Set135 105 136 106 def represent_datetime_datetime(self, object): 137 107 return _syck.Scalar(object.isoformat(), tag="tag:yaml.org,2002:timestamp") 138 108 139 def represent_long(self, object):140 return _syck.Scalar(repr(object), tag="tag:python.yaml.org,2002:long")141 142 def represent_unicode(self, object):143 return _syck.Scalar(object.encode('utf-8'), tag="tag:python.yaml.org,2002:unicode")144 145 def represent_tuple(self, object):146 return _syck.Seq(list(object), tag="tag:python.yaml.org,2002:tuple")147 148 def represent_type(self, object):149 name = '%s.%s' % (object.__module__, object.__name__)150 return _syck.Scalar('', tag="tag:python.yaml.org,2002:name:"+name)151 represent_classobj = represent_type152 represent_class = represent_type153 # TODO: Python 2.2 does not provide the module name of a function154 represent_function = represent_type155 represent_builtin_function_or_method = represent_type156 157 def represent_instance(self, object):158 cls = object.__class__159 class_name = '%s.%s' % (cls.__module__, cls.__name__)160 args = ()161 state = {}162 if hasattr(object, '__getinitargs__'):163 args = object.__getinitargs__()164 if hasattr(object, '__getstate__'):165 state = object.__getstate__()166 elif not hasattr(object, '__getinitargs__'):167 state = object.__dict__.copy()168 if not args and isinstance(state, dict):169 return _syck.Map(state.copy(),170 tag="tag:python.yaml.org,2002:object:"+class_name)171 value = {}172 if args:173 value['args'] = list(args)174 if state or not isinstance(state, dict):175 value['state'] = state176 return _syck.Map(value,177 tag="tag:python.yaml.org,2002:new:"+class_name)178 179 def represent_object(self, object): # Do you understand this? I don't.180 cls = type(object)181 class_name = '%s.%s' % (cls.__module__, cls.__name__)182 args = ()183 state = {}184 if cls.__reduce__ is type.__reduce__:185 if hasattr(object, '__reduce_ex__'):186 reduce = object.__reduce_ex__(2)187 args = reduce[1][1:]188 else:189 reduce = object.__reduce__()190 if len(reduce) > 2:191 state = reduce[2]192 if state is None:193 state = {}194 if not args and isinstance(state, dict):195 return _syck.Map(state.copy(),196 tag="tag:python.yaml.org,2002:object:"+class_name)197 if not state and isinstance(state, dict):198 return _syck.Seq(list(args),199 tag="tag:python.yaml.org,2002:new:"+class_name)200 value = {}201 if args:202 value['args'] = list(args)203 if state or not isinstance(state, dict):204 value['state'] = state205 return _syck.Map(value,206 tag="tag:python.yaml.org,2002:new:"+class_name)207 else:208 reduce = object.__reduce__()209 cls = reduce[0]210 class_name = '%s.%s' % (cls.__module__, cls.__name__)211 args = reduce[1]212 state = None213 if len(reduce) > 2:214 state = reduce[2]215 if state is None:216 state = {}217 if not state and isinstance(state, dict):218 return _syck.Seq(list(args),219 tag="tag:python.yaml.org,2002:apply:"+class_name)220 value = {}221 if args:222 value['args'] = list(args)223 if state or not isinstance(state, dict):224 value['state'] = state225 return _syck.Map(value,226 tag="tag:python.yaml.org,2002:apply:"+class_name)227 228 def represent__syck_Node(self, object):229 object_type = type(object)230 type_name = '%s.%s' % (object_type.__module__, object_type.__name__)231 state = []232 if hasattr(object_type, '__slotnames__'):233 for name in object_type.__slotnames__:234 value = getattr(object, name)235 if value:236 state.append((name, value))237 return _syck.Map(state,238 tag="tag:python.yaml.org,2002:object:"+type_name)239 240 109 def allow_aliases(self, object): 241 """Checks whether the given object can be aliased."""242 110 if object is None or type(object) in [int, bool, float]: 243 111 return False 244 112 if type(object) is str and (not object or object.isalnum()): 245 113 return False 246 if type(object) is tuple and not object:247 return False248 114 return True 249 115 250 def emit(node, output=None, Dumper=Dumper, **parameters): 251 """ 252 Emits the given node to the output. 253 254 If output is None, returns the produced YAML document. 255 """ 116 def emit(node, output=None, **parameters): 256 117 if output is None: 257 118 dumper = Dumper(StringIO.StringIO(), **parameters) … … 262 123 return dumper.output.getvalue() 263 124 264 def dump(object, output=None, Dumper=Dumper, **parameters): 265 """ 266 Dumps the given object to the output. 267 268 If output is None, returns the produced YAML document. 269 """ 125 def dump(object, output=None, **parameters): 270 126 if output is None: 271 127 dumper = Dumper(StringIO.StringIO(), **parameters) … … 276 132 return dumper.output.getvalue() 277 133 278 def emit_documents(nodes, output=None, Dumper=Dumper, **parameters): 279 """ 280 Emits the list of nodes to the output. 281 282 If output is None, returns the produced YAML document. 283 """ 134 def emit_documents(nodes, output=None, **parameters): 284 135 if output is None: 285 136 dumper = Dumper(StringIO.StringIO(), **parameters) … … 291 142 return dumper.output.getvalue() 292 143 293 def dump_documents(objects, output=None, Dumper=Dumper, **parameters): 294 """ 295 Dumps the list of objects to the output. 296 297 If output is None, returns the produced YAML document. 298 """ 144 def dump_documents(objects, output=None, **parameters): 299 145 if output is None: 300 146 dumper = Dumper(StringIO.StringIO(), **parameters)
Note: See TracChangeset
for help on using the changeset viewer.
