| [55] | 1 | |
|---|
| [133] | 2 | __all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor', |
|---|
| 3 | 'ConstructorError'] |
|---|
| [57] | 4 | |
|---|
| [55] | 5 | from error import * |
|---|
| 6 | from nodes import * |
|---|
| 7 | |
|---|
| [225] | 8 | import datetime |
|---|
| [55] | 9 | |
|---|
| 10 | try: |
|---|
| 11 | set |
|---|
| 12 | except NameError: |
|---|
| 13 | from sets import Set as set |
|---|
| 14 | |
|---|
| [235] | 15 | import binascii, re, sys, types |
|---|
| [55] | 16 | |
|---|
| 17 | class ConstructorError(MarkedYAMLError): |
|---|
| 18 | pass |
|---|
| 19 | |
|---|
| [222] | 20 | class BaseConstructor(object): |
|---|
| [55] | 21 | |
|---|
| [136] | 22 | yaml_constructors = {} |
|---|
| 23 | yaml_multi_constructors = {} |
|---|
| 24 | |
|---|
| 25 | def __init__(self): |
|---|
| [55] | 26 | self.constructed_objects = {} |
|---|
| [142] | 27 | self.recursive_objects = {} |
|---|
| [222] | 28 | self.state_generators = [] |
|---|
| 29 | self.deep_construct = False |
|---|
| [55] | 30 | |
|---|
| [136] | 31 | def check_data(self): |
|---|
| [55] | 32 | # If there are more documents available? |
|---|
| [136] | 33 | return self.check_node() |
|---|
| [55] | 34 | |
|---|
| [136] | 35 | def get_data(self): |
|---|
| [55] | 36 | # Construct and return the next document. |
|---|
| [136] | 37 | if self.check_node(): |
|---|
| 38 | return self.construct_document(self.get_node()) |
|---|
| [55] | 39 | |
|---|
| [258] | 40 | def get_single_data(self): |
|---|
| 41 | # Ensure that the stream contains a single document and construct it. |
|---|
| 42 | node = self.get_single_node() |
|---|
| 43 | if node is not None: |
|---|
| 44 | return self.construct_document(node) |
|---|
| 45 | return None |
|---|
| 46 | |
|---|
| [55] | 47 | def construct_document(self, node): |
|---|
| [136] | 48 | data = self.construct_object(node) |
|---|
| [222] | 49 | while self.state_generators: |
|---|
| 50 | state_generators = self.state_generators |
|---|
| 51 | self.state_generators = [] |
|---|
| 52 | for generator in state_generators: |
|---|
| 53 | for dummy in generator: |
|---|
| 54 | pass |
|---|
| [55] | 55 | self.constructed_objects = {} |
|---|
| [142] | 56 | self.recursive_objects = {} |
|---|
| [222] | 57 | self.deep_construct = False |
|---|
| [136] | 58 | return data |
|---|
| [55] | 59 | |
|---|
| [222] | 60 | def construct_object(self, node, deep=False): |
|---|
| [377] | 61 | if node in self.constructed_objects: |
|---|
| 62 | return self.constructed_objects[node] |
|---|
| [222] | 63 | if deep: |
|---|
| 64 | old_deep = self.deep_construct |
|---|
| 65 | self.deep_construct = True |
|---|
| [142] | 66 | if node in self.recursive_objects: |
|---|
| 67 | raise ConstructorError(None, None, |
|---|
| [222] | 68 | "found unconstructable recursive node", node.start_mark) |
|---|
| [142] | 69 | self.recursive_objects[node] = None |
|---|
| [136] | 70 | constructor = None |
|---|
| [222] | 71 | tag_suffix = None |
|---|
| [55] | 72 | if node.tag in self.yaml_constructors: |
|---|
| [222] | 73 | constructor = self.yaml_constructors[node.tag] |
|---|
| [136] | 74 | else: |
|---|
| 75 | for tag_prefix in self.yaml_multi_constructors: |
|---|
| 76 | if node.tag.startswith(tag_prefix): |
|---|
| 77 | tag_suffix = node.tag[len(tag_prefix):] |
|---|
| [222] | 78 | constructor = self.yaml_multi_constructors[tag_prefix] |
|---|
| [139] | 79 | break |
|---|
| [136] | 80 | else: |
|---|
| 81 | if None in self.yaml_multi_constructors: |
|---|
| [222] | 82 | tag_suffix = node.tag |
|---|
| 83 | constructor = self.yaml_multi_constructors[None] |
|---|
| [136] | 84 | elif None in self.yaml_constructors: |
|---|
| [222] | 85 | constructor = self.yaml_constructors[None] |
|---|
| [136] | 86 | elif isinstance(node, ScalarNode): |
|---|
| [222] | 87 | constructor = self.__class__.construct_scalar |
|---|
| [136] | 88 | elif isinstance(node, SequenceNode): |
|---|
| [222] | 89 | constructor = self.__class__.construct_sequence |
|---|
| [136] | 90 | elif isinstance(node, MappingNode): |
|---|
| [222] | 91 | constructor = self.__class__.construct_mapping |
|---|
| 92 | if tag_suffix is None: |
|---|
| 93 | data = constructor(self, node) |
|---|
| 94 | else: |
|---|
| 95 | data = constructor(self, tag_suffix, node) |
|---|
| [235] | 96 | if isinstance(data, types.GeneratorType): |
|---|
| [222] | 97 | generator = data |
|---|
| 98 | data = generator.next() |
|---|
| 99 | if self.deep_construct: |
|---|
| 100 | for dummy in generator: |
|---|
| 101 | pass |
|---|
| 102 | else: |
|---|
| 103 | self.state_generators.append(generator) |
|---|
| [136] | 104 | self.constructed_objects[node] = data |
|---|
| [142] | 105 | del self.recursive_objects[node] |
|---|
| [222] | 106 | if deep: |
|---|
| 107 | self.deep_construct = old_deep |
|---|
| [136] | 108 | return data |
|---|
| [55] | 109 | |
|---|
| 110 | def construct_scalar(self, node): |
|---|
| 111 | if not isinstance(node, ScalarNode): |
|---|
| 112 | raise ConstructorError(None, None, |
|---|
| 113 | "expected a scalar node, but found %s" % node.id, |
|---|
| [116] | 114 | node.start_mark) |
|---|
| [55] | 115 | return node.value |
|---|
| 116 | |
|---|
| [222] | 117 | def construct_sequence(self, node, deep=False): |
|---|
| [55] | 118 | if not isinstance(node, SequenceNode): |
|---|
| 119 | raise ConstructorError(None, None, |
|---|
| 120 | "expected a sequence node, but found %s" % node.id, |
|---|
| [116] | 121 | node.start_mark) |
|---|
| [222] | 122 | return [self.construct_object(child, deep=deep) |
|---|
| 123 | for child in node.value] |
|---|
| [55] | 124 | |
|---|
| [222] | 125 | def construct_mapping(self, node, deep=False): |
|---|
| [55] | 126 | if not isinstance(node, MappingNode): |
|---|
| 127 | raise ConstructorError(None, None, |
|---|
| 128 | "expected a mapping node, but found %s" % node.id, |
|---|
| [116] | 129 | node.start_mark) |
|---|
| [55] | 130 | mapping = {} |
|---|
| [222] | 131 | for key_node, value_node in node.value: |
|---|
| 132 | key = self.construct_object(key_node, deep=deep) |
|---|
| 133 | try: |
|---|
| 134 | hash(key) |
|---|
| 135 | except TypeError, exc: |
|---|
| 136 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| 137 | "found unacceptable key (%s)" % exc, key_node.start_mark) |
|---|
| 138 | value = self.construct_object(value_node, deep=deep) |
|---|
| 139 | mapping[key] = value |
|---|
| [55] | 140 | return mapping |
|---|
| 141 | |
|---|
| [222] | 142 | def construct_pairs(self, node, deep=False): |
|---|
| [55] | 143 | if not isinstance(node, MappingNode): |
|---|
| 144 | raise ConstructorError(None, None, |
|---|
| 145 | "expected a mapping node, but found %s" % node.id, |
|---|
| [116] | 146 | node.start_mark) |
|---|
| [55] | 147 | pairs = [] |
|---|
| [222] | 148 | for key_node, value_node in node.value: |
|---|
| 149 | key = self.construct_object(key_node, deep=deep) |
|---|
| 150 | value = self.construct_object(value_node, deep=deep) |
|---|
| [55] | 151 | pairs.append((key, value)) |
|---|
| 152 | return pairs |
|---|
| 153 | |
|---|
| 154 | def add_constructor(cls, tag, constructor): |
|---|
| 155 | if not 'yaml_constructors' in cls.__dict__: |
|---|
| 156 | cls.yaml_constructors = cls.yaml_constructors.copy() |
|---|
| 157 | cls.yaml_constructors[tag] = constructor |
|---|
| 158 | add_constructor = classmethod(add_constructor) |
|---|
| 159 | |
|---|
| [136] | 160 | def add_multi_constructor(cls, tag_prefix, multi_constructor): |
|---|
| 161 | if not 'yaml_multi_constructors' in cls.__dict__: |
|---|
| 162 | cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() |
|---|
| 163 | cls.yaml_multi_constructors[tag_prefix] = multi_constructor |
|---|
| 164 | add_multi_constructor = classmethod(add_multi_constructor) |
|---|
| [55] | 165 | |
|---|
| [133] | 166 | class SafeConstructor(BaseConstructor): |
|---|
| [55] | 167 | |
|---|
| [222] | 168 | def construct_scalar(self, node): |
|---|
| 169 | if isinstance(node, MappingNode): |
|---|
| 170 | for key_node, value_node in node.value: |
|---|
| 171 | if key_node.tag == u'tag:yaml.org,2002:value': |
|---|
| 172 | return self.construct_scalar(value_node) |
|---|
| 173 | return BaseConstructor.construct_scalar(self, node) |
|---|
| 174 | |
|---|
| 175 | def flatten_mapping(self, node): |
|---|
| 176 | merge = [] |
|---|
| 177 | index = 0 |
|---|
| 178 | while index < len(node.value): |
|---|
| 179 | key_node, value_node = node.value[index] |
|---|
| 180 | if key_node.tag == u'tag:yaml.org,2002:merge': |
|---|
| 181 | del node.value[index] |
|---|
| 182 | if isinstance(value_node, MappingNode): |
|---|
| 183 | self.flatten_mapping(value_node) |
|---|
| 184 | merge.extend(value_node.value) |
|---|
| 185 | elif isinstance(value_node, SequenceNode): |
|---|
| 186 | submerge = [] |
|---|
| 187 | for subnode in value_node.value: |
|---|
| 188 | if not isinstance(subnode, MappingNode): |
|---|
| 189 | raise ConstructorError("while constructing a mapping", |
|---|
| 190 | node.start_mark, |
|---|
| 191 | "expected a mapping for merging, but found %s" |
|---|
| 192 | % subnode.id, subnode.start_mark) |
|---|
| 193 | self.flatten_mapping(subnode) |
|---|
| 194 | submerge.append(subnode.value) |
|---|
| 195 | submerge.reverse() |
|---|
| 196 | for value in submerge: |
|---|
| 197 | merge.extend(value) |
|---|
| 198 | else: |
|---|
| 199 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| 200 | "expected a mapping or list of mappings for merging, but found %s" |
|---|
| 201 | % value_node.id, value_node.start_mark) |
|---|
| 202 | elif key_node.tag == u'tag:yaml.org,2002:value': |
|---|
| 203 | key_node.tag = u'tag:yaml.org,2002:str' |
|---|
| 204 | index += 1 |
|---|
| 205 | else: |
|---|
| 206 | index += 1 |
|---|
| 207 | if merge: |
|---|
| 208 | node.value = merge + node.value |
|---|
| 209 | |
|---|
| 210 | def construct_mapping(self, node, deep=False): |
|---|
| 211 | if isinstance(node, MappingNode): |
|---|
| 212 | self.flatten_mapping(node) |
|---|
| 213 | return BaseConstructor.construct_mapping(self, node, deep=deep) |
|---|
| 214 | |
|---|
| [55] | 215 | def construct_yaml_null(self, node): |
|---|
| 216 | self.construct_scalar(node) |
|---|
| 217 | return None |
|---|
| 218 | |
|---|
| 219 | bool_values = { |
|---|
| 220 | u'yes': True, |
|---|
| 221 | u'no': False, |
|---|
| 222 | u'true': True, |
|---|
| 223 | u'false': False, |
|---|
| 224 | u'on': True, |
|---|
| 225 | u'off': False, |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | def construct_yaml_bool(self, node): |
|---|
| 229 | value = self.construct_scalar(node) |
|---|
| 230 | return self.bool_values[value.lower()] |
|---|
| 231 | |
|---|
| 232 | def construct_yaml_int(self, node): |
|---|
| 233 | value = str(self.construct_scalar(node)) |
|---|
| 234 | value = value.replace('_', '') |
|---|
| 235 | sign = +1 |
|---|
| 236 | if value[0] == '-': |
|---|
| 237 | sign = -1 |
|---|
| 238 | if value[0] in '+-': |
|---|
| 239 | value = value[1:] |
|---|
| 240 | if value == '0': |
|---|
| 241 | return 0 |
|---|
| 242 | elif value.startswith('0b'): |
|---|
| 243 | return sign*int(value[2:], 2) |
|---|
| 244 | elif value.startswith('0x'): |
|---|
| 245 | return sign*int(value[2:], 16) |
|---|
| 246 | elif value[0] == '0': |
|---|
| 247 | return sign*int(value, 8) |
|---|
| 248 | elif ':' in value: |
|---|
| 249 | digits = [int(part) for part in value.split(':')] |
|---|
| 250 | digits.reverse() |
|---|
| 251 | base = 1 |
|---|
| 252 | value = 0 |
|---|
| 253 | for digit in digits: |
|---|
| 254 | value += digit*base |
|---|
| 255 | base *= 60 |
|---|
| 256 | return sign*value |
|---|
| 257 | else: |
|---|
| 258 | return sign*int(value) |
|---|
| 259 | |
|---|
| [168] | 260 | inf_value = 1e300 |
|---|
| [173] | 261 | while inf_value != inf_value*inf_value: |
|---|
| [168] | 262 | inf_value *= inf_value |
|---|
| [173] | 263 | nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99). |
|---|
| [55] | 264 | |
|---|
| 265 | def construct_yaml_float(self, node): |
|---|
| 266 | value = str(self.construct_scalar(node)) |
|---|
| [175] | 267 | value = value.replace('_', '').lower() |
|---|
| [55] | 268 | sign = +1 |
|---|
| 269 | if value[0] == '-': |
|---|
| [58] | 270 | sign = -1 |
|---|
| [55] | 271 | if value[0] in '+-': |
|---|
| 272 | value = value[1:] |
|---|
| [175] | 273 | if value == '.inf': |
|---|
| [55] | 274 | return sign*self.inf_value |
|---|
| [175] | 275 | elif value == '.nan': |
|---|
| [55] | 276 | return self.nan_value |
|---|
| 277 | elif ':' in value: |
|---|
| 278 | digits = [float(part) for part in value.split(':')] |
|---|
| 279 | digits.reverse() |
|---|
| 280 | base = 1 |
|---|
| 281 | value = 0.0 |
|---|
| 282 | for digit in digits: |
|---|
| 283 | value += digit*base |
|---|
| 284 | base *= 60 |
|---|
| 285 | return sign*value |
|---|
| 286 | else: |
|---|
| [170] | 287 | return sign*float(value) |
|---|
| [55] | 288 | |
|---|
| 289 | def construct_yaml_binary(self, node): |
|---|
| 290 | value = self.construct_scalar(node) |
|---|
| 291 | try: |
|---|
| 292 | return str(value).decode('base64') |
|---|
| 293 | except (binascii.Error, UnicodeEncodeError), exc: |
|---|
| 294 | raise ConstructorError(None, None, |
|---|
| [116] | 295 | "failed to decode base64 data: %s" % exc, node.start_mark) |
|---|
| [55] | 296 | |
|---|
| [56] | 297 | timestamp_regexp = re.compile( |
|---|
| 298 | ur'''^(?P<year>[0-9][0-9][0-9][0-9]) |
|---|
| 299 | -(?P<month>[0-9][0-9]?) |
|---|
| 300 | -(?P<day>[0-9][0-9]?) |
|---|
| [58] | 301 | (?:(?:[Tt]|[ \t]+) |
|---|
| [56] | 302 | (?P<hour>[0-9][0-9]?) |
|---|
| 303 | :(?P<minute>[0-9][0-9]) |
|---|
| 304 | :(?P<second>[0-9][0-9]) |
|---|
| [234] | 305 | (?:\.(?P<fraction>[0-9]*))? |
|---|
| [225] | 306 | (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?) |
|---|
| 307 | (?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X) |
|---|
| [56] | 308 | |
|---|
| 309 | def construct_yaml_timestamp(self, node): |
|---|
| 310 | value = self.construct_scalar(node) |
|---|
| [58] | 311 | match = self.timestamp_regexp.match(node.value) |
|---|
| [56] | 312 | values = match.groupdict() |
|---|
| [225] | 313 | year = int(values['year']) |
|---|
| 314 | month = int(values['month']) |
|---|
| 315 | day = int(values['day']) |
|---|
| 316 | if not values['hour']: |
|---|
| 317 | return datetime.date(year, month, day) |
|---|
| 318 | hour = int(values['hour']) |
|---|
| 319 | minute = int(values['minute']) |
|---|
| 320 | second = int(values['second']) |
|---|
| 321 | fraction = 0 |
|---|
| 322 | if values['fraction']: |
|---|
| [269] | 323 | fraction = values['fraction'][:6] |
|---|
| 324 | while len(fraction) < 6: |
|---|
| 325 | fraction += '0' |
|---|
| 326 | fraction = int(fraction) |
|---|
| [225] | 327 | delta = None |
|---|
| 328 | if values['tz_sign']: |
|---|
| 329 | tz_hour = int(values['tz_hour']) |
|---|
| 330 | tz_minute = int(values['tz_minute'] or 0) |
|---|
| 331 | delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) |
|---|
| 332 | if values['tz_sign'] == '-': |
|---|
| 333 | delta = -delta |
|---|
| 334 | data = datetime.datetime(year, month, day, hour, minute, second, fraction) |
|---|
| 335 | if delta: |
|---|
| 336 | data -= delta |
|---|
| 337 | return data |
|---|
| [56] | 338 | |
|---|
| 339 | def construct_yaml_omap(self, node): |
|---|
| 340 | # Note: we do not check for duplicate keys, because it's too |
|---|
| 341 | # CPU-expensive. |
|---|
| [222] | 342 | omap = [] |
|---|
| 343 | yield omap |
|---|
| [56] | 344 | if not isinstance(node, SequenceNode): |
|---|
| [116] | 345 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| 346 | "expected a sequence, but found %s" % node.id, node.start_mark) |
|---|
| [56] | 347 | for subnode in node.value: |
|---|
| 348 | if not isinstance(subnode, MappingNode): |
|---|
| [116] | 349 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| [56] | 350 | "expected a mapping of length 1, but found %s" % subnode.id, |
|---|
| [116] | 351 | subnode.start_mark) |
|---|
| [58] | 352 | if len(subnode.value) != 1: |
|---|
| [116] | 353 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| [58] | 354 | "expected a single mapping item, but found %d items" % len(subnode.value), |
|---|
| [116] | 355 | subnode.start_mark) |
|---|
| [222] | 356 | key_node, value_node = subnode.value[0] |
|---|
| [58] | 357 | key = self.construct_object(key_node) |
|---|
| [222] | 358 | value = self.construct_object(value_node) |
|---|
| [58] | 359 | omap.append((key, value)) |
|---|
| [56] | 360 | |
|---|
| 361 | def construct_yaml_pairs(self, node): |
|---|
| 362 | # Note: the same code as `construct_yaml_omap`. |
|---|
| [222] | 363 | pairs = [] |
|---|
| 364 | yield pairs |
|---|
| [56] | 365 | if not isinstance(node, SequenceNode): |
|---|
| [116] | 366 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| 367 | "expected a sequence, but found %s" % node.id, node.start_mark) |
|---|
| [56] | 368 | for subnode in node.value: |
|---|
| 369 | if not isinstance(subnode, MappingNode): |
|---|
| [116] | 370 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| [56] | 371 | "expected a mapping of length 1, but found %s" % subnode.id, |
|---|
| [116] | 372 | subnode.start_mark) |
|---|
| [58] | 373 | if len(subnode.value) != 1: |
|---|
| [116] | 374 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| [58] | 375 | "expected a single mapping item, but found %d items" % len(subnode.value), |
|---|
| [116] | 376 | subnode.start_mark) |
|---|
| [222] | 377 | key_node, value_node = subnode.value[0] |
|---|
| [58] | 378 | key = self.construct_object(key_node) |
|---|
| [222] | 379 | value = self.construct_object(value_node) |
|---|
| [58] | 380 | pairs.append((key, value)) |
|---|
| [56] | 381 | |
|---|
| 382 | def construct_yaml_set(self, node): |
|---|
| [222] | 383 | data = set() |
|---|
| 384 | yield data |
|---|
| [56] | 385 | value = self.construct_mapping(node) |
|---|
| [222] | 386 | data.update(value) |
|---|
| [56] | 387 | |
|---|
| [55] | 388 | def construct_yaml_str(self, node): |
|---|
| 389 | value = self.construct_scalar(node) |
|---|
| 390 | try: |
|---|
| [251] | 391 | return value.encode('ascii') |
|---|
| [55] | 392 | except UnicodeEncodeError: |
|---|
| 393 | return value |
|---|
| 394 | |
|---|
| [56] | 395 | def construct_yaml_seq(self, node): |
|---|
| [222] | 396 | data = [] |
|---|
| 397 | yield data |
|---|
| 398 | data.extend(self.construct_sequence(node)) |
|---|
| [56] | 399 | |
|---|
| 400 | def construct_yaml_map(self, node): |
|---|
| [222] | 401 | data = {} |
|---|
| 402 | yield data |
|---|
| 403 | value = self.construct_mapping(node) |
|---|
| 404 | data.update(value) |
|---|
| [56] | 405 | |
|---|
| [136] | 406 | def construct_yaml_object(self, node, cls): |
|---|
| 407 | data = cls.__new__(cls) |
|---|
| [222] | 408 | yield data |
|---|
| [136] | 409 | if hasattr(data, '__setstate__'): |
|---|
| [222] | 410 | state = self.construct_mapping(node, deep=True) |
|---|
| [139] | 411 | data.__setstate__(state) |
|---|
| [136] | 412 | else: |
|---|
| [222] | 413 | state = self.construct_mapping(node) |
|---|
| [139] | 414 | data.__dict__.update(state) |
|---|
| [136] | 415 | |
|---|
| [57] | 416 | def construct_undefined(self, node): |
|---|
| 417 | raise ConstructorError(None, None, |
|---|
| 418 | "could not determine a constructor for the tag %r" % node.tag.encode('utf-8'), |
|---|
| [116] | 419 | node.start_mark) |
|---|
| [57] | 420 | |
|---|
| [133] | 421 | SafeConstructor.add_constructor( |
|---|
| [55] | 422 | u'tag:yaml.org,2002:null', |
|---|
| [133] | 423 | SafeConstructor.construct_yaml_null) |
|---|
| [55] | 424 | |
|---|
| [133] | 425 | SafeConstructor.add_constructor( |
|---|
| [55] | 426 | u'tag:yaml.org,2002:bool', |
|---|
| [133] | 427 | SafeConstructor.construct_yaml_bool) |
|---|
| [55] | 428 | |
|---|
| [133] | 429 | SafeConstructor.add_constructor( |
|---|
| [55] | 430 | u'tag:yaml.org,2002:int', |
|---|
| [133] | 431 | SafeConstructor.construct_yaml_int) |
|---|
| [55] | 432 | |
|---|
| [133] | 433 | SafeConstructor.add_constructor( |
|---|
| [55] | 434 | u'tag:yaml.org,2002:float', |
|---|
| [133] | 435 | SafeConstructor.construct_yaml_float) |
|---|
| [55] | 436 | |
|---|
| [133] | 437 | SafeConstructor.add_constructor( |
|---|
| [58] | 438 | u'tag:yaml.org,2002:binary', |
|---|
| [133] | 439 | SafeConstructor.construct_yaml_binary) |
|---|
| [56] | 440 | |
|---|
| [225] | 441 | SafeConstructor.add_constructor( |
|---|
| 442 | u'tag:yaml.org,2002:timestamp', |
|---|
| 443 | SafeConstructor.construct_yaml_timestamp) |
|---|
| [58] | 444 | |
|---|
| [133] | 445 | SafeConstructor.add_constructor( |
|---|
| [56] | 446 | u'tag:yaml.org,2002:omap', |
|---|
| [133] | 447 | SafeConstructor.construct_yaml_omap) |
|---|
| [56] | 448 | |
|---|
| [133] | 449 | SafeConstructor.add_constructor( |
|---|
| [56] | 450 | u'tag:yaml.org,2002:pairs', |
|---|
| [133] | 451 | SafeConstructor.construct_yaml_pairs) |
|---|
| [56] | 452 | |
|---|
| [133] | 453 | SafeConstructor.add_constructor( |
|---|
| [56] | 454 | u'tag:yaml.org,2002:set', |
|---|
| [133] | 455 | SafeConstructor.construct_yaml_set) |
|---|
| [56] | 456 | |
|---|
| [133] | 457 | SafeConstructor.add_constructor( |
|---|
| [55] | 458 | u'tag:yaml.org,2002:str', |
|---|
| [133] | 459 | SafeConstructor.construct_yaml_str) |
|---|
| [55] | 460 | |
|---|
| [133] | 461 | SafeConstructor.add_constructor( |
|---|
| [56] | 462 | u'tag:yaml.org,2002:seq', |
|---|
| [133] | 463 | SafeConstructor.construct_yaml_seq) |
|---|
| [56] | 464 | |
|---|
| [133] | 465 | SafeConstructor.add_constructor( |
|---|
| [56] | 466 | u'tag:yaml.org,2002:map', |
|---|
| [133] | 467 | SafeConstructor.construct_yaml_map) |
|---|
| [56] | 468 | |
|---|
| [133] | 469 | SafeConstructor.add_constructor(None, |
|---|
| 470 | SafeConstructor.construct_undefined) |
|---|
| [57] | 471 | |
|---|
| [133] | 472 | class Constructor(SafeConstructor): |
|---|
| [55] | 473 | |
|---|
| [139] | 474 | def construct_python_str(self, node): |
|---|
| 475 | return self.construct_scalar(node).encode('utf-8') |
|---|
| 476 | |
|---|
| 477 | def construct_python_unicode(self, node): |
|---|
| 478 | return self.construct_scalar(node) |
|---|
| 479 | |
|---|
| 480 | def construct_python_long(self, node): |
|---|
| 481 | return long(self.construct_yaml_int(node)) |
|---|
| 482 | |
|---|
| 483 | def construct_python_complex(self, node): |
|---|
| 484 | return complex(self.construct_scalar(node)) |
|---|
| 485 | |
|---|
| 486 | def construct_python_tuple(self, node): |
|---|
| [222] | 487 | return tuple(self.construct_sequence(node)) |
|---|
| [139] | 488 | |
|---|
| 489 | def find_python_module(self, name, mark): |
|---|
| 490 | if not name: |
|---|
| 491 | raise ConstructorError("while constructing a Python module", mark, |
|---|
| 492 | "expected non-empty name appended to the tag", mark) |
|---|
| 493 | try: |
|---|
| 494 | __import__(name) |
|---|
| 495 | except ImportError, exc: |
|---|
| 496 | raise ConstructorError("while constructing a Python module", mark, |
|---|
| 497 | "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) |
|---|
| 498 | return sys.modules[name] |
|---|
| 499 | |
|---|
| 500 | def find_python_name(self, name, mark): |
|---|
| 501 | if not name: |
|---|
| 502 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 503 | "expected non-empty name appended to the tag", mark) |
|---|
| 504 | if u'.' in name: |
|---|
| [146] | 505 | # Python 2.4 only |
|---|
| 506 | #module_name, object_name = name.rsplit('.', 1) |
|---|
| 507 | items = name.split('.') |
|---|
| 508 | object_name = items.pop() |
|---|
| 509 | module_name = '.'.join(items) |
|---|
| [139] | 510 | else: |
|---|
| 511 | module_name = '__builtin__' |
|---|
| 512 | object_name = name |
|---|
| 513 | try: |
|---|
| 514 | __import__(module_name) |
|---|
| 515 | except ImportError, exc: |
|---|
| 516 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 517 | "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) |
|---|
| 518 | module = sys.modules[module_name] |
|---|
| 519 | if not hasattr(module, object_name): |
|---|
| 520 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 521 | "cannot find %r in the module %r" % (object_name.encode('utf-8'), |
|---|
| 522 | module.__name__), mark) |
|---|
| 523 | return getattr(module, object_name) |
|---|
| 524 | |
|---|
| 525 | def construct_python_name(self, suffix, node): |
|---|
| 526 | value = self.construct_scalar(node) |
|---|
| 527 | if value: |
|---|
| 528 | raise ConstructorError("while constructing a Python name", node.start_mark, |
|---|
| 529 | "expected the empty value, but found %r" % value.encode('utf-8'), |
|---|
| 530 | node.start_mark) |
|---|
| 531 | return self.find_python_name(suffix, node.start_mark) |
|---|
| 532 | |
|---|
| 533 | def construct_python_module(self, suffix, node): |
|---|
| 534 | value = self.construct_scalar(node) |
|---|
| 535 | if value: |
|---|
| 536 | raise ConstructorError("while constructing a Python module", node.start_mark, |
|---|
| 537 | "expected the empty value, but found %r" % value.encode('utf-8'), |
|---|
| 538 | node.start_mark) |
|---|
| 539 | return self.find_python_module(suffix, node.start_mark) |
|---|
| 540 | |
|---|
| [147] | 541 | class classobj: pass |
|---|
| 542 | |
|---|
| 543 | def make_python_instance(self, suffix, node, |
|---|
| 544 | args=None, kwds=None, newobj=False): |
|---|
| 545 | if not args: |
|---|
| 546 | args = [] |
|---|
| 547 | if not kwds: |
|---|
| 548 | kwds = {} |
|---|
| 549 | cls = self.find_python_name(suffix, node.start_mark) |
|---|
| 550 | if newobj and isinstance(cls, type(self.classobj)) \ |
|---|
| 551 | and not args and not kwds: |
|---|
| 552 | instance = self.classobj() |
|---|
| 553 | instance.__class__ = cls |
|---|
| 554 | return instance |
|---|
| 555 | elif newobj and isinstance(cls, type): |
|---|
| 556 | return cls.__new__(cls, *args, **kwds) |
|---|
| 557 | else: |
|---|
| 558 | return cls(*args, **kwds) |
|---|
| 559 | |
|---|
| 560 | def set_python_instance_state(self, instance, state): |
|---|
| 561 | if hasattr(instance, '__setstate__'): |
|---|
| 562 | instance.__setstate__(state) |
|---|
| 563 | else: |
|---|
| 564 | slotstate = {} |
|---|
| 565 | if isinstance(state, tuple) and len(state) == 2: |
|---|
| 566 | state, slotstate = state |
|---|
| 567 | if hasattr(instance, '__dict__'): |
|---|
| 568 | instance.__dict__.update(state) |
|---|
| 569 | elif state: |
|---|
| 570 | slotstate.update(state) |
|---|
| 571 | for key, value in slotstate.items(): |
|---|
| 572 | setattr(object, key, value) |
|---|
| 573 | |
|---|
| 574 | def construct_python_object(self, suffix, node): |
|---|
| 575 | # Format: |
|---|
| 576 | # !!python/object:module.name { ... state ... } |
|---|
| 577 | instance = self.make_python_instance(suffix, node, newobj=True) |
|---|
| [222] | 578 | yield instance |
|---|
| 579 | deep = hasattr(instance, '__setstate__') |
|---|
| 580 | state = self.construct_mapping(node, deep=deep) |
|---|
| [147] | 581 | self.set_python_instance_state(instance, state) |
|---|
| 582 | |
|---|
| 583 | def construct_python_object_apply(self, suffix, node, newobj=False): |
|---|
| 584 | # Format: |
|---|
| 585 | # !!python/object/apply # (or !!python/object/new) |
|---|
| 586 | # args: [ ... arguments ... ] |
|---|
| 587 | # kwds: { ... keywords ... } |
|---|
| 588 | # state: ... state ... |
|---|
| 589 | # listitems: [ ... listitems ... ] |
|---|
| 590 | # dictitems: { ... dictitems ... } |
|---|
| 591 | # or short format: |
|---|
| 592 | # !!python/object/apply [ ... arguments ... ] |
|---|
| 593 | # The difference between !!python/object/apply and !!python/object/new |
|---|
| 594 | # is how an object is created, check make_python_instance for details. |
|---|
| 595 | if isinstance(node, SequenceNode): |
|---|
| [222] | 596 | args = self.construct_sequence(node, deep=True) |
|---|
| [147] | 597 | kwds = {} |
|---|
| 598 | state = {} |
|---|
| 599 | listitems = [] |
|---|
| 600 | dictitems = {} |
|---|
| 601 | else: |
|---|
| [222] | 602 | value = self.construct_mapping(node, deep=True) |
|---|
| [147] | 603 | args = value.get('args', []) |
|---|
| 604 | kwds = value.get('kwds', {}) |
|---|
| 605 | state = value.get('state', {}) |
|---|
| 606 | listitems = value.get('listitems', []) |
|---|
| 607 | dictitems = value.get('dictitems', {}) |
|---|
| 608 | instance = self.make_python_instance(suffix, node, args, kwds, newobj) |
|---|
| 609 | if state: |
|---|
| 610 | self.set_python_instance_state(instance, state) |
|---|
| 611 | if listitems: |
|---|
| 612 | instance.extend(listitems) |
|---|
| 613 | if dictitems: |
|---|
| 614 | for key in dictitems: |
|---|
| 615 | instance[key] = dictitems[key] |
|---|
| 616 | return instance |
|---|
| 617 | |
|---|
| 618 | def construct_python_object_new(self, suffix, node): |
|---|
| 619 | return self.construct_python_object_apply(suffix, node, newobj=True) |
|---|
| 620 | |
|---|
| [139] | 621 | Constructor.add_constructor( |
|---|
| 622 | u'tag:yaml.org,2002:python/none', |
|---|
| 623 | Constructor.construct_yaml_null) |
|---|
| 624 | |
|---|
| 625 | Constructor.add_constructor( |
|---|
| 626 | u'tag:yaml.org,2002:python/bool', |
|---|
| 627 | Constructor.construct_yaml_bool) |
|---|
| 628 | |
|---|
| 629 | Constructor.add_constructor( |
|---|
| 630 | u'tag:yaml.org,2002:python/str', |
|---|
| 631 | Constructor.construct_python_str) |
|---|
| 632 | |
|---|
| 633 | Constructor.add_constructor( |
|---|
| 634 | u'tag:yaml.org,2002:python/unicode', |
|---|
| 635 | Constructor.construct_python_unicode) |
|---|
| 636 | |
|---|
| 637 | Constructor.add_constructor( |
|---|
| 638 | u'tag:yaml.org,2002:python/int', |
|---|
| 639 | Constructor.construct_yaml_int) |
|---|
| 640 | |
|---|
| 641 | Constructor.add_constructor( |
|---|
| 642 | u'tag:yaml.org,2002:python/long', |
|---|
| 643 | Constructor.construct_python_long) |
|---|
| 644 | |
|---|
| 645 | Constructor.add_constructor( |
|---|
| 646 | u'tag:yaml.org,2002:python/float', |
|---|
| 647 | Constructor.construct_yaml_float) |
|---|
| 648 | |
|---|
| 649 | Constructor.add_constructor( |
|---|
| 650 | u'tag:yaml.org,2002:python/complex', |
|---|
| 651 | Constructor.construct_python_complex) |
|---|
| 652 | |
|---|
| 653 | Constructor.add_constructor( |
|---|
| 654 | u'tag:yaml.org,2002:python/list', |
|---|
| 655 | Constructor.construct_yaml_seq) |
|---|
| 656 | |
|---|
| 657 | Constructor.add_constructor( |
|---|
| 658 | u'tag:yaml.org,2002:python/tuple', |
|---|
| 659 | Constructor.construct_python_tuple) |
|---|
| 660 | |
|---|
| 661 | Constructor.add_constructor( |
|---|
| 662 | u'tag:yaml.org,2002:python/dict', |
|---|
| 663 | Constructor.construct_yaml_map) |
|---|
| 664 | |
|---|
| 665 | Constructor.add_multi_constructor( |
|---|
| 666 | u'tag:yaml.org,2002:python/name:', |
|---|
| 667 | Constructor.construct_python_name) |
|---|
| 668 | |
|---|
| 669 | Constructor.add_multi_constructor( |
|---|
| 670 | u'tag:yaml.org,2002:python/module:', |
|---|
| 671 | Constructor.construct_python_module) |
|---|
| 672 | |
|---|
| [147] | 673 | Constructor.add_multi_constructor( |
|---|
| 674 | u'tag:yaml.org,2002:python/object:', |
|---|
| 675 | Constructor.construct_python_object) |
|---|
| 676 | |
|---|
| 677 | Constructor.add_multi_constructor( |
|---|
| 678 | u'tag:yaml.org,2002:python/object/apply:', |
|---|
| 679 | Constructor.construct_python_object_apply) |
|---|
| 680 | |
|---|
| 681 | Constructor.add_multi_constructor( |
|---|
| 682 | u'tag:yaml.org,2002:python/object/new:', |
|---|
| 683 | Constructor.construct_python_object_new) |
|---|
| 684 | |
|---|