Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 1 | |
| 2 | __all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer', |
| 3 | 'RepresenterError'] |
| 4 | |
| 5 | from error import * |
| 6 | from nodes import * |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 7 | |
Kirill Simonov | 29413ea | 2006-08-16 18:22:38 +0000 | [diff] [blame] | 8 | import datetime |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 9 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 10 | import sys, copy_reg, types |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 11 | |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 12 | class RepresenterError(YAMLError): |
| 13 | pass |
| 14 | |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 15 | class BaseRepresenter(object): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 16 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 17 | yaml_representers = {} |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 18 | yaml_multi_representers = {} |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 19 | |
Tina Müller | 507a464 | 2019-02-23 22:24:40 +0100 | [diff] [blame] | 20 | def __init__(self, default_style=None, default_flow_style=False, sort_keys=True): |
Kirill Simonov | 80ba450 | 2006-05-04 10:46:11 +0000 | [diff] [blame] | 21 | self.default_style = default_style |
| 22 | self.default_flow_style = default_flow_style |
Tina Müller | 07c88c6 | 2018-03-16 23:25:44 +0100 | [diff] [blame] | 23 | self.sort_keys = sort_keys |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 24 | self.represented_objects = {} |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 25 | self.object_keeper = [] |
| 26 | self.alias_key = None |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 27 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 28 | def represent(self, data): |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 29 | node = self.represent_data(data) |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 30 | self.serialize(node) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 31 | self.represented_objects = {} |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 32 | self.object_keeper = [] |
| 33 | self.alias_key = None |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 34 | |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 35 | def get_classobj_bases(self, cls): |
| 36 | bases = [cls] |
| 37 | for base in cls.__bases__: |
| 38 | bases.extend(self.get_classobj_bases(base)) |
| 39 | return bases |
| 40 | |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 41 | def represent_data(self, data): |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 42 | if self.ignore_aliases(data): |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 43 | self.alias_key = None |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 44 | else: |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 45 | self.alias_key = id(data) |
| 46 | if self.alias_key is not None: |
| 47 | if self.alias_key in self.represented_objects: |
| 48 | node = self.represented_objects[self.alias_key] |
| 49 | #if node is None: |
| 50 | # raise RepresenterError("recursive objects are not allowed: %r" % data) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 51 | return node |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 52 | #self.represented_objects[alias_key] = None |
| 53 | self.object_keeper.append(data) |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 54 | data_types = type(data).__mro__ |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 55 | if type(data) is types.InstanceType: |
Kirill Simonov | 80ed3f1 | 2006-04-18 19:33:16 +0000 | [diff] [blame] | 56 | data_types = self.get_classobj_bases(data.__class__)+list(data_types) |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 57 | if data_types[0] in self.yaml_representers: |
| 58 | node = self.yaml_representers[data_types[0]](self, data) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 59 | else: |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 60 | for data_type in data_types: |
| 61 | if data_type in self.yaml_multi_representers: |
| 62 | node = self.yaml_multi_representers[data_type](self, data) |
| 63 | break |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 64 | else: |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 65 | if None in self.yaml_multi_representers: |
| 66 | node = self.yaml_multi_representers[None](self, data) |
| 67 | elif None in self.yaml_representers: |
| 68 | node = self.yaml_representers[None](self, data) |
| 69 | else: |
| 70 | node = ScalarNode(None, unicode(data)) |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 71 | #if alias_key is not None: |
| 72 | # self.represented_objects[alias_key] = node |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 73 | return node |
| 74 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 75 | def add_representer(cls, data_type, representer): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 76 | if not 'yaml_representers' in cls.__dict__: |
| 77 | cls.yaml_representers = cls.yaml_representers.copy() |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 78 | cls.yaml_representers[data_type] = representer |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 79 | add_representer = classmethod(add_representer) |
| 80 | |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 81 | def add_multi_representer(cls, data_type, representer): |
| 82 | if not 'yaml_multi_representers' in cls.__dict__: |
| 83 | cls.yaml_multi_representers = cls.yaml_multi_representers.copy() |
| 84 | cls.yaml_multi_representers[data_type] = representer |
| 85 | add_multi_representer = classmethod(add_multi_representer) |
| 86 | |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 87 | def represent_scalar(self, tag, value, style=None): |
Kirill Simonov | 80ba450 | 2006-05-04 10:46:11 +0000 | [diff] [blame] | 88 | if style is None: |
| 89 | style = self.default_style |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 90 | node = ScalarNode(tag, value, style=style) |
| 91 | if self.alias_key is not None: |
| 92 | self.represented_objects[self.alias_key] = node |
| 93 | return node |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 94 | |
| 95 | def represent_sequence(self, tag, sequence, flow_style=None): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 96 | value = [] |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 97 | node = SequenceNode(tag, value, flow_style=flow_style) |
| 98 | if self.alias_key is not None: |
| 99 | self.represented_objects[self.alias_key] = node |
| 100 | best_style = True |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 101 | for item in sequence: |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 102 | node_item = self.represent_data(item) |
| 103 | if not (isinstance(node_item, ScalarNode) and not node_item.style): |
| 104 | best_style = False |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 105 | value.append(node_item) |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 106 | if flow_style is None: |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 107 | if self.default_flow_style is not None: |
| 108 | node.flow_style = self.default_flow_style |
| 109 | else: |
| 110 | node.flow_style = best_style |
| 111 | return node |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 112 | |
| 113 | def represent_mapping(self, tag, mapping, flow_style=None): |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 114 | value = [] |
| 115 | node = MappingNode(tag, value, flow_style=flow_style) |
| 116 | if self.alias_key is not None: |
| 117 | self.represented_objects[self.alias_key] = node |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 118 | best_style = True |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 119 | if hasattr(mapping, 'items'): |
| 120 | mapping = mapping.items() |
Tina Müller | 07c88c6 | 2018-03-16 23:25:44 +0100 | [diff] [blame] | 121 | if self.sort_keys: |
| 122 | mapping.sort() |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 123 | for item_key, item_value in mapping: |
| 124 | node_key = self.represent_data(item_key) |
| 125 | node_value = self.represent_data(item_value) |
| 126 | if not (isinstance(node_key, ScalarNode) and not node_key.style): |
| 127 | best_style = False |
| 128 | if not (isinstance(node_value, ScalarNode) and not node_value.style): |
| 129 | best_style = False |
| 130 | value.append((node_key, node_value)) |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 131 | if flow_style is None: |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 132 | if self.default_flow_style is not None: |
| 133 | node.flow_style = self.default_flow_style |
| 134 | else: |
| 135 | node.flow_style = best_style |
| 136 | return node |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 137 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 138 | def ignore_aliases(self, data): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 139 | return False |
| 140 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 141 | class SafeRepresenter(BaseRepresenter): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 142 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 143 | def ignore_aliases(self, data): |
Kirill Simonov | f10d92f | 2016-08-25 16:27:19 -0500 | [diff] [blame] | 144 | if data is None: |
| 145 | return True |
| 146 | if isinstance(data, tuple) and data == (): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 147 | return True |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 148 | if isinstance(data, (str, unicode, bool, int, float)): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 149 | return True |
| 150 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 151 | def represent_none(self, data): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 152 | return self.represent_scalar(u'tag:yaml.org,2002:null', |
| 153 | u'null') |
| 154 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 155 | def represent_str(self, data): |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 156 | tag = None |
| 157 | style = None |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 158 | try: |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 159 | data = unicode(data, 'ascii') |
| 160 | tag = u'tag:yaml.org,2002:str' |
Kirill Simonov | fcf5d61 | 2006-04-12 22:26:41 +0000 | [diff] [blame] | 161 | except UnicodeDecodeError: |
| 162 | try: |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 163 | data = unicode(data, 'utf-8') |
| 164 | tag = u'tag:yaml.org,2002:str' |
Kirill Simonov | fcf5d61 | 2006-04-12 22:26:41 +0000 | [diff] [blame] | 165 | except UnicodeDecodeError: |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 166 | data = data.encode('base64') |
| 167 | tag = u'tag:yaml.org,2002:binary' |
| 168 | style = '|' |
| 169 | return self.represent_scalar(tag, data, style=style) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 170 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 171 | def represent_unicode(self, data): |
| 172 | return self.represent_scalar(u'tag:yaml.org,2002:str', data) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 173 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 174 | def represent_bool(self, data): |
| 175 | if data: |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 176 | value = u'true' |
| 177 | else: |
| 178 | value = u'false' |
| 179 | return self.represent_scalar(u'tag:yaml.org,2002:bool', value) |
| 180 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 181 | def represent_int(self, data): |
| 182 | return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 183 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 184 | def represent_long(self, data): |
| 185 | return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 186 | |
Kirill Simonov | 810977b | 2006-05-15 18:43:58 +0000 | [diff] [blame] | 187 | inf_value = 1e300 |
| 188 | while repr(inf_value) != repr(inf_value*inf_value): |
| 189 | inf_value *= inf_value |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 190 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 191 | def represent_float(self, data): |
Kirill Simonov | 500659d | 2006-05-22 19:49:54 +0000 | [diff] [blame] | 192 | if data != data or (data == 0.0 and data == 1.0): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 193 | value = u'.nan' |
Kirill Simonov | 500659d | 2006-05-22 19:49:54 +0000 | [diff] [blame] | 194 | elif data == self.inf_value: |
| 195 | value = u'.inf' |
| 196 | elif data == -self.inf_value: |
| 197 | value = u'-.inf' |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 198 | else: |
Kirill Simonov | 96fcba3 | 2007-03-22 16:13:32 +0000 | [diff] [blame] | 199 | value = unicode(repr(data)).lower() |
| 200 | # Note that in some cases `repr(data)` represents a float number |
| 201 | # without the decimal parts. For instance: |
| 202 | # >>> repr(1e17) |
| 203 | # '1e17' |
| 204 | # Unfortunately, this is not a valid float representation according |
| 205 | # to the definition of the `!!float` tag. We fix this by adding |
| 206 | # '.0' before the 'e' symbol. |
| 207 | if u'.' not in value and u'e' in value: |
| 208 | value = value.replace(u'e', u'.0e', 1) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 209 | return self.represent_scalar(u'tag:yaml.org,2002:float', value) |
| 210 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 211 | def represent_list(self, data): |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 212 | #pairs = (len(data) > 0 and isinstance(data, list)) |
| 213 | #if pairs: |
| 214 | # for item in data: |
| 215 | # if not isinstance(item, tuple) or len(item) != 2: |
| 216 | # pairs = False |
| 217 | # break |
| 218 | #if not pairs: |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 219 | return self.represent_sequence(u'tag:yaml.org,2002:seq', data) |
Kirill Simonov | 8b083c6 | 2006-08-03 16:07:29 +0000 | [diff] [blame] | 220 | #value = [] |
| 221 | #for item_key, item_value in data: |
| 222 | # value.append(self.represent_mapping(u'tag:yaml.org,2002:map', |
| 223 | # [(item_key, item_value)])) |
| 224 | #return SequenceNode(u'tag:yaml.org,2002:pairs', value) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 225 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 226 | def represent_dict(self, data): |
| 227 | return self.represent_mapping(u'tag:yaml.org,2002:map', data) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 228 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 229 | def represent_set(self, data): |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 230 | value = {} |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 231 | for key in data: |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 232 | value[key] = None |
| 233 | return self.represent_mapping(u'tag:yaml.org,2002:set', value) |
| 234 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 235 | def represent_date(self, data): |
Kirill Simonov | 29413ea | 2006-08-16 18:22:38 +0000 | [diff] [blame] | 236 | value = unicode(data.isoformat()) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 237 | return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) |
| 238 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 239 | def represent_datetime(self, data): |
Kirill Simonov | 29413ea | 2006-08-16 18:22:38 +0000 | [diff] [blame] | 240 | value = unicode(data.isoformat(' ')) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 241 | return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value) |
| 242 | |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 243 | def represent_yaml_object(self, tag, data, cls, flow_style=None): |
| 244 | if hasattr(data, '__getstate__'): |
| 245 | state = data.__getstate__() |
| 246 | else: |
| 247 | state = data.__dict__.copy() |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 248 | return self.represent_mapping(tag, state, flow_style=flow_style) |
Kirill Simonov | 6a97abb | 2006-04-15 23:54:52 +0000 | [diff] [blame] | 249 | |
| 250 | def represent_undefined(self, data): |
Timofei Bondarev | ef744d8 | 2017-03-01 00:45:09 -0500 | [diff] [blame] | 251 | raise RepresenterError("cannot represent an object", data) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 252 | |
| 253 | SafeRepresenter.add_representer(type(None), |
| 254 | SafeRepresenter.represent_none) |
| 255 | |
| 256 | SafeRepresenter.add_representer(str, |
| 257 | SafeRepresenter.represent_str) |
| 258 | |
| 259 | SafeRepresenter.add_representer(unicode, |
| 260 | SafeRepresenter.represent_unicode) |
| 261 | |
| 262 | SafeRepresenter.add_representer(bool, |
| 263 | SafeRepresenter.represent_bool) |
| 264 | |
| 265 | SafeRepresenter.add_representer(int, |
| 266 | SafeRepresenter.represent_int) |
| 267 | |
| 268 | SafeRepresenter.add_representer(long, |
| 269 | SafeRepresenter.represent_long) |
| 270 | |
| 271 | SafeRepresenter.add_representer(float, |
| 272 | SafeRepresenter.represent_float) |
| 273 | |
| 274 | SafeRepresenter.add_representer(list, |
| 275 | SafeRepresenter.represent_list) |
| 276 | |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 277 | SafeRepresenter.add_representer(tuple, |
| 278 | SafeRepresenter.represent_list) |
| 279 | |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 280 | SafeRepresenter.add_representer(dict, |
| 281 | SafeRepresenter.represent_dict) |
| 282 | |
| 283 | SafeRepresenter.add_representer(set, |
| 284 | SafeRepresenter.represent_set) |
| 285 | |
Kirill Simonov | 29413ea | 2006-08-16 18:22:38 +0000 | [diff] [blame] | 286 | SafeRepresenter.add_representer(datetime.date, |
| 287 | SafeRepresenter.represent_date) |
Kirill Simonov | 235ab98 | 2008-12-29 17:24:05 +0000 | [diff] [blame] | 288 | |
Kirill Simonov | 29413ea | 2006-08-16 18:22:38 +0000 | [diff] [blame] | 289 | SafeRepresenter.add_representer(datetime.datetime, |
| 290 | SafeRepresenter.represent_datetime) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 291 | |
| 292 | SafeRepresenter.add_representer(None, |
| 293 | SafeRepresenter.represent_undefined) |
| 294 | |
| 295 | class Representer(SafeRepresenter): |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 296 | |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 297 | def represent_str(self, data): |
| 298 | tag = None |
| 299 | style = None |
| 300 | try: |
| 301 | data = unicode(data, 'ascii') |
| 302 | tag = u'tag:yaml.org,2002:str' |
| 303 | except UnicodeDecodeError: |
| 304 | try: |
| 305 | data = unicode(data, 'utf-8') |
| 306 | tag = u'tag:yaml.org,2002:python/str' |
| 307 | except UnicodeDecodeError: |
| 308 | data = data.encode('base64') |
| 309 | tag = u'tag:yaml.org,2002:binary' |
| 310 | style = '|' |
| 311 | return self.represent_scalar(tag, data, style=style) |
| 312 | |
| 313 | def represent_unicode(self, data): |
| 314 | tag = None |
| 315 | try: |
| 316 | data.encode('ascii') |
| 317 | tag = u'tag:yaml.org,2002:python/unicode' |
| 318 | except UnicodeEncodeError: |
| 319 | tag = u'tag:yaml.org,2002:str' |
| 320 | return self.represent_scalar(tag, data) |
| 321 | |
| 322 | def represent_long(self, data): |
| 323 | tag = u'tag:yaml.org,2002:int' |
| 324 | if int(data) is not data: |
| 325 | tag = u'tag:yaml.org,2002:python/long' |
| 326 | return self.represent_scalar(tag, unicode(data)) |
| 327 | |
| 328 | def represent_complex(self, data): |
Kirill Simonov | 80ed3f1 | 2006-04-18 19:33:16 +0000 | [diff] [blame] | 329 | if data.imag == 0.0: |
| 330 | data = u'%r' % data.real |
| 331 | elif data.real == 0.0: |
| 332 | data = u'%rj' % data.imag |
| 333 | elif data.imag > 0: |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 334 | data = u'%r+%rj' % (data.real, data.imag) |
| 335 | else: |
Kirill Simonov | 80ed3f1 | 2006-04-18 19:33:16 +0000 | [diff] [blame] | 336 | data = u'%r%rj' % (data.real, data.imag) |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 337 | return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data) |
| 338 | |
| 339 | def represent_tuple(self, data): |
| 340 | return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data) |
| 341 | |
| 342 | def represent_name(self, data): |
| 343 | name = u'%s.%s' % (data.__module__, data.__name__) |
| 344 | return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'') |
| 345 | |
| 346 | def represent_module(self, data): |
| 347 | return self.represent_scalar( |
| 348 | u'tag:yaml.org,2002:python/module:'+data.__name__, u'') |
| 349 | |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 350 | def represent_instance(self, data): |
| 351 | # For instances of classic classes, we use __getinitargs__ and |
| 352 | # __getstate__ to serialize the data. |
| 353 | |
| 354 | # If data.__getinitargs__ exists, the object must be reconstructed by |
| 355 | # calling cls(**args), where args is a tuple returned by |
| 356 | # __getinitargs__. Otherwise, the cls.__init__ method should never be |
| 357 | # called and the class instance is created by instantiating a trivial |
| 358 | # class and assigning to the instance's __class__ variable. |
| 359 | |
| 360 | # If data.__getstate__ exists, it returns the state of the object. |
| 361 | # Otherwise, the state of the object is data.__dict__. |
| 362 | |
| 363 | # We produce either a !!python/object or !!python/object/new node. |
| 364 | # If data.__getinitargs__ does not exist and state is a dictionary, we |
| 365 | # produce a !!python/object node . Otherwise we produce a |
| 366 | # !!python/object/new node. |
| 367 | |
| 368 | cls = data.__class__ |
| 369 | class_name = u'%s.%s' % (cls.__module__, cls.__name__) |
| 370 | args = None |
| 371 | state = None |
| 372 | if hasattr(data, '__getinitargs__'): |
| 373 | args = list(data.__getinitargs__()) |
| 374 | if hasattr(data, '__getstate__'): |
| 375 | state = data.__getstate__() |
| 376 | else: |
| 377 | state = data.__dict__ |
| 378 | if args is None and isinstance(state, dict): |
| 379 | return self.represent_mapping( |
| 380 | u'tag:yaml.org,2002:python/object:'+class_name, state) |
| 381 | if isinstance(state, dict) and not state: |
| 382 | return self.represent_sequence( |
| 383 | u'tag:yaml.org,2002:python/object/new:'+class_name, args) |
| 384 | value = {} |
| 385 | if args: |
| 386 | value['args'] = args |
| 387 | value['state'] = state |
| 388 | return self.represent_mapping( |
| 389 | u'tag:yaml.org,2002:python/object/new:'+class_name, value) |
| 390 | |
| 391 | def represent_object(self, data): |
| 392 | # We use __reduce__ API to save the data. data.__reduce__ returns |
| 393 | # a tuple of length 2-5: |
| 394 | # (function, args, state, listitems, dictitems) |
| 395 | |
| 396 | # For reconstructing, we calls function(*args), then set its state, |
| 397 | # listitems, and dictitems if they are not None. |
| 398 | |
| 399 | # A special case is when function.__name__ == '__newobj__'. In this |
| 400 | # case we create the object with args[0].__new__(*args). |
| 401 | |
| 402 | # Another special case is when __reduce__ returns a string - we don't |
| 403 | # support it. |
| 404 | |
| 405 | # We produce a !!python/object, !!python/object/new or |
| 406 | # !!python/object/apply node. |
| 407 | |
| 408 | cls = type(data) |
| 409 | if cls in copy_reg.dispatch_table: |
Kirill Simonov | 056fe5c | 2006-07-11 11:32:39 +0000 | [diff] [blame] | 410 | reduce = copy_reg.dispatch_table[cls](data) |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 411 | elif hasattr(data, '__reduce_ex__'): |
| 412 | reduce = data.__reduce_ex__(2) |
| 413 | elif hasattr(data, '__reduce__'): |
| 414 | reduce = data.__reduce__() |
| 415 | else: |
Timofei Bondarev | ef744d8 | 2017-03-01 00:45:09 -0500 | [diff] [blame] | 416 | raise RepresenterError("cannot represent an object", data) |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 417 | reduce = (list(reduce)+[None]*5)[:5] |
| 418 | function, args, state, listitems, dictitems = reduce |
| 419 | args = list(args) |
| 420 | if state is None: |
| 421 | state = {} |
| 422 | if listitems is not None: |
| 423 | listitems = list(listitems) |
| 424 | if dictitems is not None: |
| 425 | dictitems = dict(dictitems) |
| 426 | if function.__name__ == '__newobj__': |
| 427 | function = args[0] |
| 428 | args = args[1:] |
| 429 | tag = u'tag:yaml.org,2002:python/object/new:' |
| 430 | newobj = True |
| 431 | else: |
| 432 | tag = u'tag:yaml.org,2002:python/object/apply:' |
| 433 | newobj = False |
| 434 | function_name = u'%s.%s' % (function.__module__, function.__name__) |
| 435 | if not args and not listitems and not dictitems \ |
| 436 | and isinstance(state, dict) and newobj: |
| 437 | return self.represent_mapping( |
| 438 | u'tag:yaml.org,2002:python/object:'+function_name, state) |
| 439 | if not listitems and not dictitems \ |
| 440 | and isinstance(state, dict) and not state: |
| 441 | return self.represent_sequence(tag+function_name, args) |
| 442 | value = {} |
| 443 | if args: |
| 444 | value['args'] = args |
| 445 | if state or not isinstance(state, dict): |
| 446 | value['state'] = state |
| 447 | if listitems: |
| 448 | value['listitems'] = listitems |
| 449 | if dictitems: |
| 450 | value['dictitems'] = dictitems |
| 451 | return self.represent_mapping(tag+function_name, value) |
| 452 | |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 453 | Representer.add_representer(str, |
| 454 | Representer.represent_str) |
| 455 | |
| 456 | Representer.add_representer(unicode, |
| 457 | Representer.represent_unicode) |
| 458 | |
| 459 | Representer.add_representer(long, |
| 460 | Representer.represent_long) |
| 461 | |
| 462 | Representer.add_representer(complex, |
| 463 | Representer.represent_complex) |
| 464 | |
| 465 | Representer.add_representer(tuple, |
| 466 | Representer.represent_tuple) |
| 467 | |
| 468 | Representer.add_representer(type, |
| 469 | Representer.represent_name) |
| 470 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 471 | Representer.add_representer(types.ClassType, |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 472 | Representer.represent_name) |
| 473 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 474 | Representer.add_representer(types.FunctionType, |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 475 | Representer.represent_name) |
| 476 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 477 | Representer.add_representer(types.BuiltinFunctionType, |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 478 | Representer.represent_name) |
| 479 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 480 | Representer.add_representer(types.ModuleType, |
Kirill Simonov | 19e134b | 2006-04-18 14:35:28 +0000 | [diff] [blame] | 481 | Representer.represent_module) |
Kirill Simonov | cc316a4 | 2006-04-11 00:34:16 +0000 | [diff] [blame] | 482 | |
Kirill Simonov | 21483f2 | 2006-12-08 15:36:53 +0000 | [diff] [blame] | 483 | Representer.add_multi_representer(types.InstanceType, |
Kirill Simonov | c87ce16 | 2006-04-22 20:40:43 +0000 | [diff] [blame] | 484 | Representer.represent_instance) |
| 485 | |
| 486 | Representer.add_multi_representer(object, |
| 487 | Representer.represent_object) |
| 488 | |