Changeset 20 for trunk/lib/syck/dumpers.py
- Timestamp:
- 08/12/05 16:15:19 (8 years ago)
- File:
-
- 1 edited
-
trunk/lib/syck/dumpers.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/syck/dumpers.py
r19 r20 10 10 'emit', 'dump', 'emit_documents', 'dump_documents'] 11 11 12 INF = 1e300000 13 NEGINF = -INF 14 NAN = INF/INF 15 16 12 17 class GenericDumper(_syck.Emitter): 13 18 … … 16 21 17 22 def _convert(self, object, object_to_node): 18 if id(object) in object_to_node :23 if id(object) in object_to_node and self.allow_aliases(object): 19 24 return object_to_node[id(object)] 20 25 node = self.represent(object) … … 29 34 node.value[self._convert(key, object_to_node)] = \ 30 35 self._convert(value, object_to_node) 36 # # Workaround against a Syck bug: 37 # if node.kind == 'scalar' and node.style not in ['1quote', '2quote'] \ 38 # and node.value and node.value[-1] in [' ', '\t']: 39 # node.style = '2quote' 31 40 return node 32 41 … … 39 48 return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") 40 49 50 def allow_aliases(self, object): 51 return True 52 41 53 class Dumper(GenericDumper): 42 54 55 def __init__(self, *args, **kwds): 56 super(Dumper, self).__init__(*args, **kwds) 57 43 58 def represent(self, object): 59 for object_type in type(object).__mro__: 60 if object_type.__module__ == '__builtin__': 61 name = object_type.__name__ 62 else: 63 name = '%s.%s' % (object_type.__module__, object_type.__name__) 64 method = 'represent_%s' % name.replace('.', '_') 65 if hasattr(self, method): 66 return getattr(self, method)(object) 44 67 return super(Dumper, self).represent(object) 68 69 def represent_object(self, object): 70 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:str") 71 72 def represent_NoneType(self, object): 73 return _syck.Scalar('~', tag="tag:yaml.org,2002:null") 74 75 def represent_bool(self, object): 76 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:bool") 77 78 def represent_str(self, object): 79 return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") 80 81 def represent_list(self, object): 82 return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq") 83 84 def represent_dict(self, object): 85 return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") 86 87 def represent_int(self, object): 88 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 89 90 def represent_int(self, object): 91 return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") 92 93 def represent_float(self, object): 94 value = repr(object) 95 if value == repr(INF): 96 value = '.inf' 97 elif value == repr(NEGINF): 98 value = '-.inf' 99 elif value == repr(NAN): 100 value = '.nan' 101 return _syck.Scalar(value, tag="tag:yaml.org,2002:float") 102 103 def represent_sets_Set(self, object): 104 return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") 105 106 def represent_datetime_datetime(self, object): 107 return _syck.Scalar(object.isoformat(), tag="tag:yaml.org,2002:timestamp") 108 109 def allow_aliases(self, object): 110 if object is None or type(object) in [int, bool, float]: 111 return False 112 if type(object) is str and (not object or object.isalnum()): 113 return False 114 return True 45 115 46 116 def emit(node, output=None, **parameters):
Note: See TracChangeset
for help on using the changeset viewer.
