| [55] | 1 | |
|---|
| [133] | 2 | __all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor', |
|---|
| 3 | 'ConstructorError'] |
|---|
| [57] | 4 | |
|---|
| [55] | 5 | from error import * |
|---|
| 6 | from nodes import * |
|---|
| [136] | 7 | from composer import * |
|---|
| [55] | 8 | |
|---|
| 9 | try: |
|---|
| 10 | import datetime |
|---|
| 11 | datetime_available = True |
|---|
| 12 | except ImportError: |
|---|
| 13 | datetime_available = False |
|---|
| 14 | |
|---|
| 15 | try: |
|---|
| 16 | set |
|---|
| 17 | except NameError: |
|---|
| 18 | from sets import Set as set |
|---|
| 19 | |
|---|
| [139] | 20 | import binascii, re, sys |
|---|
| [55] | 21 | |
|---|
| 22 | class ConstructorError(MarkedYAMLError): |
|---|
| 23 | pass |
|---|
| 24 | |
|---|
| [136] | 25 | class BaseConstructor(Composer): |
|---|
| [55] | 26 | |
|---|
| [136] | 27 | yaml_constructors = {} |
|---|
| 28 | yaml_multi_constructors = {} |
|---|
| 29 | |
|---|
| 30 | def __init__(self): |
|---|
| [55] | 31 | self.constructed_objects = {} |
|---|
| 32 | |
|---|
| [136] | 33 | def check_data(self): |
|---|
| [55] | 34 | # If there are more documents available? |
|---|
| [136] | 35 | return self.check_node() |
|---|
| [55] | 36 | |
|---|
| [136] | 37 | def get_data(self): |
|---|
| [55] | 38 | # Construct and return the next document. |
|---|
| [136] | 39 | if self.check_node(): |
|---|
| 40 | return self.construct_document(self.get_node()) |
|---|
| [55] | 41 | |
|---|
| 42 | def __iter__(self): |
|---|
| 43 | # Iterator protocol. |
|---|
| [136] | 44 | while self.check_node(): |
|---|
| 45 | yield self.construct_document(self.get_node()) |
|---|
| [55] | 46 | |
|---|
| 47 | def construct_document(self, node): |
|---|
| [136] | 48 | data = self.construct_object(node) |
|---|
| [55] | 49 | self.constructed_objects = {} |
|---|
| [136] | 50 | return data |
|---|
| [55] | 51 | |
|---|
| 52 | def construct_object(self, node): |
|---|
| 53 | if node in self.constructed_objects: |
|---|
| 54 | return self.constructed_objects[node] |
|---|
| [136] | 55 | constructor = None |
|---|
| [55] | 56 | if node.tag in self.yaml_constructors: |
|---|
| [136] | 57 | constructor = lambda node: self.yaml_constructors[node.tag](self, node) |
|---|
| 58 | else: |
|---|
| 59 | for tag_prefix in self.yaml_multi_constructors: |
|---|
| 60 | if node.tag.startswith(tag_prefix): |
|---|
| 61 | tag_suffix = node.tag[len(tag_prefix):] |
|---|
| 62 | constructor = lambda node: \ |
|---|
| 63 | self.yaml_multi_constructors[tag_prefix](self, tag_suffix, node) |
|---|
| [139] | 64 | break |
|---|
| [136] | 65 | else: |
|---|
| 66 | if None in self.yaml_multi_constructors: |
|---|
| 67 | constructor = lambda node: \ |
|---|
| 68 | self.yaml_multi_constructors[None](self, node.tag, node) |
|---|
| 69 | elif None in self.yaml_constructors: |
|---|
| 70 | constructor = lambda node: \ |
|---|
| 71 | self.yaml_constructors[None](self, node) |
|---|
| 72 | elif isinstance(node, ScalarNode): |
|---|
| 73 | constructor = self.construct_scalar |
|---|
| 74 | elif isinstance(node, SequenceNode): |
|---|
| 75 | constructor = self.construct_sequence |
|---|
| 76 | elif isinstance(node, MappingNode): |
|---|
| 77 | constructor = self.construct_mapping |
|---|
| [139] | 78 | else: |
|---|
| 79 | print node.tag |
|---|
| [136] | 80 | data = constructor(node) |
|---|
| 81 | self.constructed_objects[node] = data |
|---|
| 82 | return data |
|---|
| [55] | 83 | |
|---|
| 84 | def construct_scalar(self, node): |
|---|
| 85 | if not isinstance(node, ScalarNode): |
|---|
| 86 | if isinstance(node, MappingNode): |
|---|
| 87 | for key_node in node.value: |
|---|
| 88 | if key_node.tag == u'tag:yaml.org,2002:value': |
|---|
| 89 | return self.construct_scalar(node.value[key_node]) |
|---|
| 90 | raise ConstructorError(None, None, |
|---|
| 91 | "expected a scalar node, but found %s" % node.id, |
|---|
| [116] | 92 | node.start_mark) |
|---|
| [55] | 93 | return node.value |
|---|
| 94 | |
|---|
| 95 | def construct_sequence(self, node): |
|---|
| 96 | if not isinstance(node, SequenceNode): |
|---|
| 97 | raise ConstructorError(None, None, |
|---|
| 98 | "expected a sequence node, but found %s" % node.id, |
|---|
| [116] | 99 | node.start_mark) |
|---|
| [55] | 100 | return [self.construct_object(child) for child in node.value] |
|---|
| 101 | |
|---|
| 102 | def construct_mapping(self, node): |
|---|
| 103 | if not isinstance(node, MappingNode): |
|---|
| 104 | raise ConstructorError(None, None, |
|---|
| 105 | "expected a mapping node, but found %s" % node.id, |
|---|
| [116] | 106 | node.start_mark) |
|---|
| [55] | 107 | mapping = {} |
|---|
| [56] | 108 | merge = None |
|---|
| [55] | 109 | for key_node in node.value: |
|---|
| [56] | 110 | if key_node.tag == u'tag:yaml.org,2002:merge': |
|---|
| 111 | if merge is not None: |
|---|
| [116] | 112 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| 113 | "found duplicate merge key", key_node.start_mark) |
|---|
| [58] | 114 | value_node = node.value[key_node] |
|---|
| 115 | if isinstance(value_node, MappingNode): |
|---|
| 116 | merge = [self.construct_mapping(value_node)] |
|---|
| 117 | elif isinstance(value_node, SequenceNode): |
|---|
| 118 | merge = [] |
|---|
| 119 | for subnode in value_node.value: |
|---|
| 120 | if not isinstance(subnode, MappingNode): |
|---|
| 121 | raise ConstructorError("while constructing a mapping", |
|---|
| [116] | 122 | node.start_mark, |
|---|
| [58] | 123 | "expected a mapping for merging, but found %s" |
|---|
| [116] | 124 | % subnode.id, subnode.start_mark) |
|---|
| [58] | 125 | merge.append(self.construct_mapping(subnode)) |
|---|
| 126 | merge.reverse() |
|---|
| 127 | else: |
|---|
| [116] | 128 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| [58] | 129 | "expected a mapping or list of mappings for merging, but found %s" |
|---|
| [116] | 130 | % value_node.id, value_node.start_mark) |
|---|
| [56] | 131 | elif key_node.tag == u'tag:yaml.org,2002:value': |
|---|
| 132 | if '=' in mapping: |
|---|
| [116] | 133 | raise ConstructorError("while construction a mapping", node.start_mark, |
|---|
| 134 | "found duplicate value key", key_node.start_mark) |
|---|
| [56] | 135 | value = self.construct_object(node.value[key_node]) |
|---|
| 136 | mapping['='] = value |
|---|
| 137 | else: |
|---|
| 138 | key = self.construct_object(key_node) |
|---|
| 139 | try: |
|---|
| 140 | duplicate_key = key in mapping |
|---|
| 141 | except TypeError, exc: |
|---|
| [116] | 142 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| 143 | "found unacceptable key (%s)" % exc, key_node.start_mark) |
|---|
| [56] | 144 | if duplicate_key: |
|---|
| [116] | 145 | raise ConstructorError("while constructing a mapping", node.start_mark, |
|---|
| 146 | "found duplicate key", key_node.start_mark) |
|---|
| [56] | 147 | value = self.construct_object(node.value[key_node]) |
|---|
| 148 | mapping[key] = value |
|---|
| 149 | if merge is not None: |
|---|
| 150 | merge.append(mapping) |
|---|
| 151 | mapping = {} |
|---|
| 152 | for submapping in merge: |
|---|
| 153 | mapping.update(submapping) |
|---|
| [55] | 154 | return mapping |
|---|
| 155 | |
|---|
| 156 | def construct_pairs(self, node): |
|---|
| 157 | if not isinstance(node, MappingNode): |
|---|
| 158 | raise ConstructorError(None, None, |
|---|
| 159 | "expected a mapping node, but found %s" % node.id, |
|---|
| [116] | 160 | node.start_mark) |
|---|
| [55] | 161 | pairs = [] |
|---|
| 162 | for key_node in node.value: |
|---|
| 163 | key = self.construct_object(key_node) |
|---|
| 164 | value = self.construct_object(node.value[key_node]) |
|---|
| 165 | pairs.append((key, value)) |
|---|
| 166 | return pairs |
|---|
| 167 | |
|---|
| 168 | def add_constructor(cls, tag, constructor): |
|---|
| 169 | if not 'yaml_constructors' in cls.__dict__: |
|---|
| 170 | cls.yaml_constructors = cls.yaml_constructors.copy() |
|---|
| 171 | cls.yaml_constructors[tag] = constructor |
|---|
| 172 | add_constructor = classmethod(add_constructor) |
|---|
| 173 | |
|---|
| [136] | 174 | def add_multi_constructor(cls, tag_prefix, multi_constructor): |
|---|
| 175 | if not 'yaml_multi_constructors' in cls.__dict__: |
|---|
| 176 | cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() |
|---|
| 177 | cls.yaml_multi_constructors[tag_prefix] = multi_constructor |
|---|
| 178 | add_multi_constructor = classmethod(add_multi_constructor) |
|---|
| [55] | 179 | |
|---|
| [133] | 180 | class SafeConstructor(BaseConstructor): |
|---|
| [55] | 181 | |
|---|
| 182 | def construct_yaml_null(self, node): |
|---|
| 183 | self.construct_scalar(node) |
|---|
| 184 | return None |
|---|
| 185 | |
|---|
| 186 | bool_values = { |
|---|
| 187 | u'yes': True, |
|---|
| 188 | u'no': False, |
|---|
| 189 | u'true': True, |
|---|
| 190 | u'false': False, |
|---|
| 191 | u'on': True, |
|---|
| 192 | u'off': False, |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | def construct_yaml_bool(self, node): |
|---|
| 196 | value = self.construct_scalar(node) |
|---|
| 197 | return self.bool_values[value.lower()] |
|---|
| 198 | |
|---|
| 199 | def construct_yaml_int(self, node): |
|---|
| 200 | value = str(self.construct_scalar(node)) |
|---|
| 201 | value = value.replace('_', '') |
|---|
| 202 | sign = +1 |
|---|
| 203 | if value[0] == '-': |
|---|
| 204 | sign = -1 |
|---|
| 205 | if value[0] in '+-': |
|---|
| 206 | value = value[1:] |
|---|
| 207 | if value == '0': |
|---|
| 208 | return 0 |
|---|
| 209 | elif value.startswith('0b'): |
|---|
| 210 | return sign*int(value[2:], 2) |
|---|
| 211 | elif value.startswith('0x'): |
|---|
| 212 | return sign*int(value[2:], 16) |
|---|
| 213 | elif value[0] == '0': |
|---|
| 214 | return sign*int(value, 8) |
|---|
| 215 | elif ':' in value: |
|---|
| 216 | digits = [int(part) for part in value.split(':')] |
|---|
| 217 | digits.reverse() |
|---|
| 218 | base = 1 |
|---|
| 219 | value = 0 |
|---|
| 220 | for digit in digits: |
|---|
| 221 | value += digit*base |
|---|
| 222 | base *= 60 |
|---|
| 223 | return sign*value |
|---|
| 224 | else: |
|---|
| 225 | return sign*int(value) |
|---|
| 226 | |
|---|
| 227 | inf_value = 1e300000 |
|---|
| 228 | nan_value = inf_value/inf_value |
|---|
| 229 | |
|---|
| 230 | def construct_yaml_float(self, node): |
|---|
| 231 | value = str(self.construct_scalar(node)) |
|---|
| 232 | value = value.replace('_', '') |
|---|
| 233 | sign = +1 |
|---|
| 234 | if value[0] == '-': |
|---|
| [58] | 235 | sign = -1 |
|---|
| [55] | 236 | if value[0] in '+-': |
|---|
| 237 | value = value[1:] |
|---|
| 238 | if value.lower() == '.inf': |
|---|
| 239 | return sign*self.inf_value |
|---|
| 240 | elif value.lower() == '.nan': |
|---|
| 241 | return self.nan_value |
|---|
| 242 | elif ':' in value: |
|---|
| 243 | digits = [float(part) for part in value.split(':')] |
|---|
| 244 | digits.reverse() |
|---|
| 245 | base = 1 |
|---|
| 246 | value = 0.0 |
|---|
| 247 | for digit in digits: |
|---|
| 248 | value += digit*base |
|---|
| 249 | base *= 60 |
|---|
| 250 | return sign*value |
|---|
| 251 | else: |
|---|
| 252 | return float(value) |
|---|
| 253 | |
|---|
| 254 | def construct_yaml_binary(self, node): |
|---|
| 255 | value = self.construct_scalar(node) |
|---|
| 256 | try: |
|---|
| 257 | return str(value).decode('base64') |
|---|
| 258 | except (binascii.Error, UnicodeEncodeError), exc: |
|---|
| 259 | raise ConstructorError(None, None, |
|---|
| [116] | 260 | "failed to decode base64 data: %s" % exc, node.start_mark) |
|---|
| [55] | 261 | |
|---|
| [56] | 262 | timestamp_regexp = re.compile( |
|---|
| 263 | ur'''^(?P<year>[0-9][0-9][0-9][0-9]) |
|---|
| 264 | -(?P<month>[0-9][0-9]?) |
|---|
| 265 | -(?P<day>[0-9][0-9]?) |
|---|
| [58] | 266 | (?:(?:[Tt]|[ \t]+) |
|---|
| [56] | 267 | (?P<hour>[0-9][0-9]?) |
|---|
| 268 | :(?P<minute>[0-9][0-9]) |
|---|
| 269 | :(?P<second>[0-9][0-9]) |
|---|
| 270 | (?:\.(?P<fraction>[0-9]*))? |
|---|
| 271 | (?:[ \t]*(?:Z|(?P<tz_hour>[-+][0-9][0-9]?) |
|---|
| [58] | 272 | (?::(?P<tz_minute>[0-9][0-9])?)?))?)?$''', re.X) |
|---|
| [56] | 273 | |
|---|
| 274 | def construct_yaml_timestamp(self, node): |
|---|
| 275 | value = self.construct_scalar(node) |
|---|
| [58] | 276 | match = self.timestamp_regexp.match(node.value) |
|---|
| [56] | 277 | values = match.groupdict() |
|---|
| 278 | for key in values: |
|---|
| 279 | if values[key]: |
|---|
| 280 | values[key] = int(values[key]) |
|---|
| 281 | else: |
|---|
| 282 | values[key] = 0 |
|---|
| 283 | fraction = values['fraction'] |
|---|
| [58] | 284 | if fraction: |
|---|
| [56] | 285 | while 10*fraction < 1000000: |
|---|
| 286 | fraction *= 10 |
|---|
| 287 | values['fraction'] = fraction |
|---|
| 288 | stamp = datetime.datetime(values['year'], values['month'], values['day'], |
|---|
| 289 | values['hour'], values['minute'], values['second'], values['fraction']) |
|---|
| 290 | diff = datetime.timedelta(hours=values['tz_hour'], minutes=values['tz_minute']) |
|---|
| 291 | return stamp-diff |
|---|
| 292 | |
|---|
| 293 | def construct_yaml_omap(self, node): |
|---|
| 294 | # Note: we do not check for duplicate keys, because it's too |
|---|
| 295 | # CPU-expensive. |
|---|
| 296 | if not isinstance(node, SequenceNode): |
|---|
| [116] | 297 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| 298 | "expected a sequence, but found %s" % node.id, node.start_mark) |
|---|
| [56] | 299 | omap = [] |
|---|
| 300 | for subnode in node.value: |
|---|
| 301 | if not isinstance(subnode, MappingNode): |
|---|
| [116] | 302 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| [56] | 303 | "expected a mapping of length 1, but found %s" % subnode.id, |
|---|
| [116] | 304 | subnode.start_mark) |
|---|
| [58] | 305 | if len(subnode.value) != 1: |
|---|
| [116] | 306 | raise ConstructorError("while constructing an ordered map", node.start_mark, |
|---|
| [58] | 307 | "expected a single mapping item, but found %d items" % len(subnode.value), |
|---|
| [116] | 308 | subnode.start_mark) |
|---|
| [58] | 309 | key_node = subnode.value.keys()[0] |
|---|
| 310 | key = self.construct_object(key_node) |
|---|
| 311 | value = self.construct_object(subnode.value[key_node]) |
|---|
| 312 | omap.append((key, value)) |
|---|
| 313 | return omap |
|---|
| [56] | 314 | |
|---|
| 315 | def construct_yaml_pairs(self, node): |
|---|
| 316 | # Note: the same code as `construct_yaml_omap`. |
|---|
| 317 | if not isinstance(node, SequenceNode): |
|---|
| [116] | 318 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| 319 | "expected a sequence, but found %s" % node.id, node.start_mark) |
|---|
| [58] | 320 | pairs = [] |
|---|
| [56] | 321 | for subnode in node.value: |
|---|
| 322 | if not isinstance(subnode, MappingNode): |
|---|
| [116] | 323 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| [56] | 324 | "expected a mapping of length 1, but found %s" % subnode.id, |
|---|
| [116] | 325 | subnode.start_mark) |
|---|
| [58] | 326 | if len(subnode.value) != 1: |
|---|
| [116] | 327 | raise ConstructorError("while constructing pairs", node.start_mark, |
|---|
| [58] | 328 | "expected a single mapping item, but found %d items" % len(subnode.value), |
|---|
| [116] | 329 | subnode.start_mark) |
|---|
| [58] | 330 | key_node = subnode.value.keys()[0] |
|---|
| 331 | key = self.construct_object(key_node) |
|---|
| 332 | value = self.construct_object(subnode.value[key_node]) |
|---|
| 333 | pairs.append((key, value)) |
|---|
| 334 | return pairs |
|---|
| [56] | 335 | |
|---|
| 336 | def construct_yaml_set(self, node): |
|---|
| 337 | value = self.construct_mapping(node) |
|---|
| 338 | return set(value) |
|---|
| 339 | |
|---|
| [55] | 340 | def construct_yaml_str(self, node): |
|---|
| 341 | value = self.construct_scalar(node) |
|---|
| 342 | try: |
|---|
| 343 | return str(value) |
|---|
| 344 | except UnicodeEncodeError: |
|---|
| 345 | return value |
|---|
| 346 | |
|---|
| [56] | 347 | def construct_yaml_seq(self, node): |
|---|
| 348 | return self.construct_sequence(node) |
|---|
| 349 | |
|---|
| 350 | def construct_yaml_map(self, node): |
|---|
| 351 | return self.construct_mapping(node) |
|---|
| 352 | |
|---|
| [136] | 353 | def construct_yaml_object(self, node, cls): |
|---|
| [139] | 354 | state = self.construct_mapping(node) |
|---|
| [136] | 355 | data = cls.__new__(cls) |
|---|
| 356 | if hasattr(data, '__setstate__'): |
|---|
| [139] | 357 | data.__setstate__(state) |
|---|
| [136] | 358 | else: |
|---|
| [139] | 359 | data.__dict__.update(state) |
|---|
| [136] | 360 | return data |
|---|
| 361 | |
|---|
| [57] | 362 | def construct_undefined(self, node): |
|---|
| 363 | raise ConstructorError(None, None, |
|---|
| 364 | "could not determine a constructor for the tag %r" % node.tag.encode('utf-8'), |
|---|
| [116] | 365 | node.start_mark) |
|---|
| [57] | 366 | |
|---|
| [133] | 367 | SafeConstructor.add_constructor( |
|---|
| [55] | 368 | u'tag:yaml.org,2002:null', |
|---|
| [133] | 369 | SafeConstructor.construct_yaml_null) |
|---|
| [55] | 370 | |
|---|
| [133] | 371 | SafeConstructor.add_constructor( |
|---|
| [55] | 372 | u'tag:yaml.org,2002:bool', |
|---|
| [133] | 373 | SafeConstructor.construct_yaml_bool) |
|---|
| [55] | 374 | |
|---|
| [133] | 375 | SafeConstructor.add_constructor( |
|---|
| [55] | 376 | u'tag:yaml.org,2002:int', |
|---|
| [133] | 377 | SafeConstructor.construct_yaml_int) |
|---|
| [55] | 378 | |
|---|
| [133] | 379 | SafeConstructor.add_constructor( |
|---|
| [55] | 380 | u'tag:yaml.org,2002:float', |
|---|
| [133] | 381 | SafeConstructor.construct_yaml_float) |
|---|
| [55] | 382 | |
|---|
| [133] | 383 | SafeConstructor.add_constructor( |
|---|
| [58] | 384 | u'tag:yaml.org,2002:binary', |
|---|
| [133] | 385 | SafeConstructor.construct_yaml_binary) |
|---|
| [56] | 386 | |
|---|
| [58] | 387 | if datetime_available: |
|---|
| [133] | 388 | SafeConstructor.add_constructor( |
|---|
| [58] | 389 | u'tag:yaml.org,2002:timestamp', |
|---|
| [133] | 390 | SafeConstructor.construct_yaml_timestamp) |
|---|
| [58] | 391 | |
|---|
| [133] | 392 | SafeConstructor.add_constructor( |
|---|
| [56] | 393 | u'tag:yaml.org,2002:omap', |
|---|
| [133] | 394 | SafeConstructor.construct_yaml_omap) |
|---|
| [56] | 395 | |
|---|
| [133] | 396 | SafeConstructor.add_constructor( |
|---|
| [56] | 397 | u'tag:yaml.org,2002:pairs', |
|---|
| [133] | 398 | SafeConstructor.construct_yaml_pairs) |
|---|
| [56] | 399 | |
|---|
| [133] | 400 | SafeConstructor.add_constructor( |
|---|
| [56] | 401 | u'tag:yaml.org,2002:set', |
|---|
| [133] | 402 | SafeConstructor.construct_yaml_set) |
|---|
| [56] | 403 | |
|---|
| [133] | 404 | SafeConstructor.add_constructor( |
|---|
| [55] | 405 | u'tag:yaml.org,2002:str', |
|---|
| [133] | 406 | SafeConstructor.construct_yaml_str) |
|---|
| [55] | 407 | |
|---|
| [133] | 408 | SafeConstructor.add_constructor( |
|---|
| [56] | 409 | u'tag:yaml.org,2002:seq', |
|---|
| [133] | 410 | SafeConstructor.construct_yaml_seq) |
|---|
| [56] | 411 | |
|---|
| [133] | 412 | SafeConstructor.add_constructor( |
|---|
| [56] | 413 | u'tag:yaml.org,2002:map', |
|---|
| [133] | 414 | SafeConstructor.construct_yaml_map) |
|---|
| [56] | 415 | |
|---|
| [133] | 416 | SafeConstructor.add_constructor(None, |
|---|
| 417 | SafeConstructor.construct_undefined) |
|---|
| [57] | 418 | |
|---|
| [133] | 419 | class Constructor(SafeConstructor): |
|---|
| [55] | 420 | |
|---|
| [139] | 421 | def construct_python_str(self, node): |
|---|
| 422 | return self.construct_scalar(node).encode('utf-8') |
|---|
| 423 | |
|---|
| 424 | def construct_python_unicode(self, node): |
|---|
| 425 | return self.construct_scalar(node) |
|---|
| 426 | |
|---|
| 427 | def construct_python_long(self, node): |
|---|
| 428 | return long(self.construct_yaml_int(node)) |
|---|
| 429 | |
|---|
| 430 | def construct_python_complex(self, node): |
|---|
| 431 | return complex(self.construct_scalar(node)) |
|---|
| 432 | |
|---|
| 433 | def construct_python_tuple(self, node): |
|---|
| 434 | return tuple(self.construct_yaml_seq(node)) |
|---|
| 435 | |
|---|
| 436 | def find_python_module(self, name, mark): |
|---|
| 437 | if not name: |
|---|
| 438 | raise ConstructorError("while constructing a Python module", mark, |
|---|
| 439 | "expected non-empty name appended to the tag", mark) |
|---|
| 440 | try: |
|---|
| 441 | __import__(name) |
|---|
| 442 | except ImportError, exc: |
|---|
| 443 | raise ConstructorError("while constructing a Python module", mark, |
|---|
| 444 | "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) |
|---|
| 445 | return sys.modules[name] |
|---|
| 446 | |
|---|
| 447 | def find_python_name(self, name, mark): |
|---|
| 448 | if not name: |
|---|
| 449 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 450 | "expected non-empty name appended to the tag", mark) |
|---|
| 451 | if u'.' in name: |
|---|
| 452 | module_name, object_name = name.rsplit('.', 1) |
|---|
| 453 | else: |
|---|
| 454 | module_name = '__builtin__' |
|---|
| 455 | object_name = name |
|---|
| 456 | try: |
|---|
| 457 | __import__(module_name) |
|---|
| 458 | except ImportError, exc: |
|---|
| 459 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 460 | "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) |
|---|
| 461 | module = sys.modules[module_name] |
|---|
| 462 | if not hasattr(module, object_name): |
|---|
| 463 | raise ConstructorError("while constructing a Python object", mark, |
|---|
| 464 | "cannot find %r in the module %r" % (object_name.encode('utf-8'), |
|---|
| 465 | module.__name__), mark) |
|---|
| 466 | return getattr(module, object_name) |
|---|
| 467 | |
|---|
| 468 | def construct_python_name(self, suffix, node): |
|---|
| 469 | value = self.construct_scalar(node) |
|---|
| 470 | if value: |
|---|
| 471 | raise ConstructorError("while constructing a Python name", node.start_mark, |
|---|
| 472 | "expected the empty value, but found %r" % value.encode('utf-8'), |
|---|
| 473 | node.start_mark) |
|---|
| 474 | return self.find_python_name(suffix, node.start_mark) |
|---|
| 475 | |
|---|
| 476 | def construct_python_module(self, suffix, node): |
|---|
| 477 | value = self.construct_scalar(node) |
|---|
| 478 | if value: |
|---|
| 479 | raise ConstructorError("while constructing a Python module", node.start_mark, |
|---|
| 480 | "expected the empty value, but found %r" % value.encode('utf-8'), |
|---|
| 481 | node.start_mark) |
|---|
| 482 | return self.find_python_module(suffix, node.start_mark) |
|---|
| 483 | |
|---|
| 484 | Constructor.add_constructor( |
|---|
| 485 | u'tag:yaml.org,2002:python/none', |
|---|
| 486 | Constructor.construct_yaml_null) |
|---|
| 487 | |
|---|
| 488 | Constructor.add_constructor( |
|---|
| 489 | u'tag:yaml.org,2002:python/bool', |
|---|
| 490 | Constructor.construct_yaml_bool) |
|---|
| 491 | |
|---|
| 492 | Constructor.add_constructor( |
|---|
| 493 | u'tag:yaml.org,2002:python/str', |
|---|
| 494 | Constructor.construct_python_str) |
|---|
| 495 | |
|---|
| 496 | Constructor.add_constructor( |
|---|
| 497 | u'tag:yaml.org,2002:python/unicode', |
|---|
| 498 | Constructor.construct_python_unicode) |
|---|
| 499 | |
|---|
| 500 | Constructor.add_constructor( |
|---|
| 501 | u'tag:yaml.org,2002:python/int', |
|---|
| 502 | Constructor.construct_yaml_int) |
|---|
| 503 | |
|---|
| 504 | Constructor.add_constructor( |
|---|
| 505 | u'tag:yaml.org,2002:python/long', |
|---|
| 506 | Constructor.construct_python_long) |
|---|
| 507 | |
|---|
| 508 | Constructor.add_constructor( |
|---|
| 509 | u'tag:yaml.org,2002:python/float', |
|---|
| 510 | Constructor.construct_yaml_float) |
|---|
| 511 | |
|---|
| 512 | Constructor.add_constructor( |
|---|
| 513 | u'tag:yaml.org,2002:python/complex', |
|---|
| 514 | Constructor.construct_python_complex) |
|---|
| 515 | |
|---|
| 516 | Constructor.add_constructor( |
|---|
| 517 | u'tag:yaml.org,2002:python/list', |
|---|
| 518 | Constructor.construct_yaml_seq) |
|---|
| 519 | |
|---|
| 520 | Constructor.add_constructor( |
|---|
| 521 | u'tag:yaml.org,2002:python/tuple', |
|---|
| 522 | Constructor.construct_python_tuple) |
|---|
| 523 | |
|---|
| 524 | Constructor.add_constructor( |
|---|
| 525 | u'tag:yaml.org,2002:python/dict', |
|---|
| 526 | Constructor.construct_yaml_map) |
|---|
| 527 | |
|---|
| 528 | Constructor.add_multi_constructor( |
|---|
| 529 | u'tag:yaml.org,2002:python/name:', |
|---|
| 530 | Constructor.construct_python_name) |
|---|
| 531 | |
|---|
| 532 | Constructor.add_multi_constructor( |
|---|
| 533 | u'tag:yaml.org,2002:python/module:', |
|---|
| 534 | Constructor.construct_python_module) |
|---|
| 535 | |
|---|