| 1 | |
|---|
| 2 | import _syck |
|---|
| 3 | |
|---|
| 4 | try: |
|---|
| 5 | import cStringIO as StringIO |
|---|
| 6 | except ImportError: |
|---|
| 7 | import StringIO |
|---|
| 8 | |
|---|
| 9 | __all__ = ['GenericDumper', 'Dumper', |
|---|
| 10 | 'emit', 'dump', 'emit_documents', 'dump_documents'] |
|---|
| 11 | |
|---|
| 12 | INF = 1e300000 |
|---|
| 13 | NEGINF = -INF |
|---|
| 14 | NAN = INF/INF |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class GenericDumper(_syck.Emitter): |
|---|
| 18 | |
|---|
| 19 | def dump(self, object): |
|---|
| 20 | self.emit(self._convert(object, {})) |
|---|
| 21 | |
|---|
| 22 | def _convert(self, object, object_to_node): |
|---|
| 23 | if id(object) in object_to_node and self.allow_aliases(object): |
|---|
| 24 | return object_to_node[id(object)] |
|---|
| 25 | node = self.represent(object) |
|---|
| 26 | object_to_node[id(object)] = node |
|---|
| 27 | if node.kind == 'seq': |
|---|
| 28 | for index, item in enumerate(node.value): |
|---|
| 29 | node.value[index] = self._convert(item, object_to_node) |
|---|
| 30 | elif node.kind == 'map': |
|---|
| 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) |
|---|
| 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' |
|---|
| 40 | return node |
|---|
| 41 | |
|---|
| 42 | def represent(self, object): |
|---|
| 43 | if isinstance(object, dict): |
|---|
| 44 | return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") |
|---|
| 45 | elif isinstance(object, list): |
|---|
| 46 | return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq") |
|---|
| 47 | else: |
|---|
| 48 | return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") |
|---|
| 49 | |
|---|
| 50 | def allow_aliases(self, object): |
|---|
| 51 | return True |
|---|
| 52 | |
|---|
| 53 | class Dumper(GenericDumper): |
|---|
| 54 | |
|---|
| 55 | def __init__(self, *args, **kwds): |
|---|
| 56 | super(Dumper, self).__init__(*args, **kwds) |
|---|
| 57 | |
|---|
| 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) |
|---|
| 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 |
|---|
| 115 | |
|---|
| 116 | def emit(node, output=None, **parameters): |
|---|
| 117 | if output is None: |
|---|
| 118 | dumper = Dumper(StringIO.StringIO(), **parameters) |
|---|
| 119 | else: |
|---|
| 120 | dumper = Dumper(output, **parameters) |
|---|
| 121 | dumper.emit(node) |
|---|
| 122 | if output is None: |
|---|
| 123 | return dumper.output.getvalue() |
|---|
| 124 | |
|---|
| 125 | def dump(object, output=None, **parameters): |
|---|
| 126 | if output is None: |
|---|
| 127 | dumper = Dumper(StringIO.StringIO(), **parameters) |
|---|
| 128 | else: |
|---|
| 129 | dumper = Dumper(output, **parameters) |
|---|
| 130 | dumper.dump(object) |
|---|
| 131 | if output is None: |
|---|
| 132 | return dumper.output.getvalue() |
|---|
| 133 | |
|---|
| 134 | def emit_documents(nodes, output=None, **parameters): |
|---|
| 135 | if output is None: |
|---|
| 136 | dumper = Dumper(StringIO.StringIO(), **parameters) |
|---|
| 137 | else: |
|---|
| 138 | dumper = Dumper(output, **parameters) |
|---|
| 139 | for node in nodes: |
|---|
| 140 | dumper.emit(node) |
|---|
| 141 | if output is None: |
|---|
| 142 | return dumper.output.getvalue() |
|---|
| 143 | |
|---|
| 144 | def dump_documents(objects, output=None, **parameters): |
|---|
| 145 | if output is None: |
|---|
| 146 | dumper = Dumper(StringIO.StringIO(), **parameters) |
|---|
| 147 | else: |
|---|
| 148 | dumper = Dumper(output, **parameters) |
|---|
| 149 | for object in objects: |
|---|
| 150 | dumper.dump(object) |
|---|
| 151 | if output is None: |
|---|
| 152 | return dumper.output.getvalue() |
|---|
| 153 | |
|---|
| 154 | |
|---|