Changeset 25 for trunk/lib/syck/dumpers.py
- Timestamp:
- 08/25/05 01:30:06 (8 years ago)
- File:
-
- 1 edited
-
trunk/lib/syck/dumpers.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/syck/dumpers.py
r24 r25 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 """ 1 5 2 6 import _syck … … 10 14 'emit', 'dump', 'emit_documents', 'dump_documents'] 11 15 12 INF = 1e30000013 NEGINF = -INF14 NAN = INF/INF15 16 17 16 class GenericDumper(_syck.Emitter): 17 """ 18 GenericDumper dumps native Python objects into YAML documents. 19 """ 18 20 19 21 def dump(self, object): 22 """Dumps the given Python object as a YAML document.""" 20 23 self.emit(self._convert(object, {})) 21 24 … … 30 33 node.value[index] = self._convert(item, object_to_node) 31 34 elif node.kind == 'map': 32 for key in node.value.keys(): 33 value = node.value[key] 34 del node.value[key] 35 node.value[self._convert(key, object_to_node)] = \ 36 self._convert(value, object_to_node) 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)) 37 46 # # Workaround against a Syck bug: 38 47 # if node.kind == 'scalar' and node.style not in ['1quote', '2quote'] \ … … 42 51 43 52 def represent(self, object): 53 """Represents the given Python object as a 'Node'.""" 44 54 if isinstance(object, dict): 45 55 return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") … … 50 60 51 61 def allow_aliases(self, object): 62 """Checks whether the given object can be aliased.""" 52 63 return True 53 64 54 65 class Dumper(GenericDumper): 55 56 def __init__(self, *args, **kwds): 57 super(Dumper, self).__init__(*args, **kwds) 66 """ 67 Dumper dumps native Python objects into YAML documents. 68 """ 69 70 INF = 1e300000 71 inf_value = repr(INF) 72 neginf_value = repr(-INF) 73 nan_value = repr(INF/INF) 58 74 59 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 """ 60 84 for object_type in type(object).__mro__: 61 85 if object_type.__module__ == '__builtin__': … … 68 92 69 93 def represent(self, object): 94 """Represents the given Python object as a 'Node'.""" 70 95 representer = self.find_representer(object) 71 96 if representer: … … 97 122 def represent_float(self, object): 98 123 value = repr(object) 99 if value == repr(INF):124 if value == self.inf_value: 100 125 value = '.inf' 101 elif value == repr(NEGINF):126 elif value == self.neginf_value: 102 127 value = '-.inf' 103 elif value == repr(NAN):128 elif value == self.nan_value: 104 129 value = '.nan' 105 130 return _syck.Scalar(value, tag="tag:yaml.org,2002:float") … … 107 132 def represent_sets_Set(self, object): 108 133 return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") 134 represent_set = represent_sets_Set 109 135 110 136 def represent_datetime_datetime(self, object): … … 121 147 122 148 def represent_type(self, object): 123 # TODO: Python 2.2 does not provide the module name of a function124 149 name = '%s.%s' % (object.__module__, object.__name__) 125 150 return _syck.Scalar('', tag="tag:python.yaml.org,2002:name:"+name) 126 151 represent_classobj = represent_type 127 152 represent_class = represent_type 153 # TODO: Python 2.2 does not provide the module name of a function 128 154 represent_function = represent_type 129 155 represent_builtin_function_or_method = represent_type … … 200 226 tag="tag:python.yaml.org,2002:apply:"+class_name) 201 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 202 240 def allow_aliases(self, object): 241 """Checks whether the given object can be aliased.""" 203 242 if object is None or type(object) in [int, bool, float]: 204 243 return False … … 210 249 211 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 """ 212 256 if output is None: 213 257 dumper = Dumper(StringIO.StringIO(), **parameters) … … 219 263 220 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 """ 221 270 if output is None: 222 271 dumper = Dumper(StringIO.StringIO(), **parameters) … … 228 277 229 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 """ 230 284 if output is None: 231 285 dumper = Dumper(StringIO.StringIO(), **parameters) … … 238 292 239 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 """ 240 299 if output is None: 241 300 dumper = Dumper(StringIO.StringIO(), **parameters)
Note: See TracChangeset
for help on using the changeset viewer.
