diff options
Diffstat (limited to 'tablib/packages/yaml/constructor.py')
| -rw-r--r-- | tablib/packages/yaml/constructor.py | 195 |
1 files changed, 101 insertions, 94 deletions
diff --git a/tablib/packages/yaml/constructor.py b/tablib/packages/yaml/constructor.py index 420c434..d44c7bd 100644 --- a/tablib/packages/yaml/constructor.py +++ b/tablib/packages/yaml/constructor.py @@ -1,6 +1,5 @@ - __all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor', - 'ConstructorError'] + 'ConstructorError'] from error import * from nodes import * @@ -12,13 +11,17 @@ try: except NameError: from sets import Set as set -import binascii, re, sys, types +import binascii +import re +import sys +import types + class ConstructorError(MarkedYAMLError): pass -class BaseConstructor(object): +class BaseConstructor(object): yaml_constructors = {} yaml_multi_constructors = {} @@ -65,7 +68,7 @@ class BaseConstructor(object): return self.constructed_objects[node] if node in self.recursive_objects: raise ConstructorError(None, None, - "found unconstructable recursive node", node.start_mark) + "found unconstructable recursive node", node.start_mark) self.recursive_objects[node] = None constructor = None tag_suffix = None @@ -110,23 +113,23 @@ class BaseConstructor(object): def construct_scalar(self, node): if not isinstance(node, ScalarNode): raise ConstructorError(None, None, - "expected a scalar node, but found %s" % node.id, - node.start_mark) + "expected a scalar node, but found %s" % node.id, + node.start_mark) return node.value def construct_sequence(self, node, deep=False): if not isinstance(node, SequenceNode): raise ConstructorError(None, None, - "expected a sequence node, but found %s" % node.id, - node.start_mark) + "expected a sequence node, but found %s" % node.id, + node.start_mark) return [self.construct_object(child, deep=deep) for child in node.value] def construct_mapping(self, node, deep=False): if not isinstance(node, MappingNode): raise ConstructorError(None, None, - "expected a mapping node, but found %s" % node.id, - node.start_mark) + "expected a mapping node, but found %s" % node.id, + node.start_mark) mapping = {} for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) @@ -134,7 +137,7 @@ class BaseConstructor(object): hash(key) except TypeError, exc: raise ConstructorError("while constructing a mapping", node.start_mark, - "found unacceptable key (%s)" % exc, key_node.start_mark) + "found unacceptable key (%s)" % exc, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping @@ -142,8 +145,8 @@ class BaseConstructor(object): def construct_pairs(self, node, deep=False): if not isinstance(node, MappingNode): raise ConstructorError(None, None, - "expected a mapping node, but found %s" % node.id, - node.start_mark) + "expected a mapping node, but found %s" % node.id, + node.start_mark) pairs = [] for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) @@ -155,16 +158,18 @@ class BaseConstructor(object): if not 'yaml_constructors' in cls.__dict__: cls.yaml_constructors = cls.yaml_constructors.copy() cls.yaml_constructors[tag] = constructor + add_constructor = classmethod(add_constructor) def add_multi_constructor(cls, tag_prefix, multi_constructor): if not 'yaml_multi_constructors' in cls.__dict__: cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() cls.yaml_multi_constructors[tag_prefix] = multi_constructor + add_multi_constructor = classmethod(add_multi_constructor) -class SafeConstructor(BaseConstructor): +class SafeConstructor(BaseConstructor): def construct_scalar(self, node): if isinstance(node, MappingNode): for key_node, value_node in node.value: @@ -187,9 +192,9 @@ class SafeConstructor(BaseConstructor): for subnode in value_node.value: if not isinstance(subnode, MappingNode): raise ConstructorError("while constructing a mapping", - node.start_mark, - "expected a mapping for merging, but found %s" - % subnode.id, subnode.start_mark) + node.start_mark, + "expected a mapping for merging, but found %s" + % subnode.id, subnode.start_mark) self.flatten_mapping(subnode) submerge.append(subnode.value) submerge.reverse() @@ -197,8 +202,8 @@ class SafeConstructor(BaseConstructor): merge.extend(value) else: raise ConstructorError("while constructing a mapping", node.start_mark, - "expected a mapping or list of mappings for merging, but found %s" - % value_node.id, value_node.start_mark) + "expected a mapping or list of mappings for merging, but found %s" + % value_node.id, value_node.start_mark) elif key_node.tag == u'tag:yaml.org,2002:value': key_node.tag = u'tag:yaml.org,2002:str' index += 1 @@ -217,12 +222,12 @@ class SafeConstructor(BaseConstructor): return None bool_values = { - u'yes': True, - u'no': False, - u'true': True, - u'false': False, - u'on': True, - u'off': False, + u'yes': True, + u'no': False, + u'true': True, + u'false': False, + u'on': True, + u'off': False, } def construct_yaml_bool(self, node): @@ -240,27 +245,27 @@ class SafeConstructor(BaseConstructor): if value == '0': return 0 elif value.startswith('0b'): - return sign*int(value[2:], 2) + return sign * int(value[2:], 2) elif value.startswith('0x'): - return sign*int(value[2:], 16) + return sign * int(value[2:], 16) elif value[0] == '0': - return sign*int(value, 8) + return sign * int(value, 8) elif ':' in value: digits = [int(part) for part in value.split(':')] digits.reverse() base = 1 value = 0 for digit in digits: - value += digit*base + value += digit * base base *= 60 - return sign*value + return sign * value else: - return sign*int(value) + return sign * int(value) inf_value = 1e300 - while inf_value != inf_value*inf_value: + while inf_value != inf_value * inf_value: inf_value *= inf_value - nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99). + nan_value = -inf_value / inf_value # Trying to make a quiet NaN (like C99). def construct_yaml_float(self, node): value = str(self.construct_scalar(node)) @@ -271,7 +276,7 @@ class SafeConstructor(BaseConstructor): if value[0] in '+-': value = value[1:] if value == '.inf': - return sign*self.inf_value + return sign * self.inf_value elif value == '.nan': return self.nan_value elif ':' in value: @@ -280,11 +285,11 @@ class SafeConstructor(BaseConstructor): base = 1 value = 0.0 for digit in digits: - value += digit*base + value += digit * base base *= 60 - return sign*value + return sign * value else: - return sign*float(value) + return sign * float(value) def construct_yaml_binary(self, node): value = self.construct_scalar(node) @@ -292,10 +297,10 @@ class SafeConstructor(BaseConstructor): return str(value).decode('base64') except (binascii.Error, UnicodeEncodeError), exc: raise ConstructorError(None, None, - "failed to decode base64 data: %s" % exc, node.start_mark) + "failed to decode base64 data: %s" % exc, node.start_mark) timestamp_regexp = re.compile( - ur'''^(?P<year>[0-9][0-9][0-9][0-9]) + ur'''^(?P<year>[0-9][0-9][0-9][0-9]) -(?P<month>[0-9][0-9]?) -(?P<day>[0-9][0-9]?) (?:(?:[Tt]|[ \t]+) @@ -343,16 +348,16 @@ class SafeConstructor(BaseConstructor): yield omap if not isinstance(node, SequenceNode): raise ConstructorError("while constructing an ordered map", node.start_mark, - "expected a sequence, but found %s" % node.id, node.start_mark) + "expected a sequence, but found %s" % node.id, node.start_mark) for subnode in node.value: if not isinstance(subnode, MappingNode): raise ConstructorError("while constructing an ordered map", node.start_mark, - "expected a mapping of length 1, but found %s" % subnode.id, - subnode.start_mark) + "expected a mapping of length 1, but found %s" % subnode.id, + subnode.start_mark) if len(subnode.value) != 1: raise ConstructorError("while constructing an ordered map", node.start_mark, - "expected a single mapping item, but found %d items" % len(subnode.value), - subnode.start_mark) + "expected a single mapping item, but found %d items" % len(subnode.value), + subnode.start_mark) key_node, value_node = subnode.value[0] key = self.construct_object(key_node) value = self.construct_object(value_node) @@ -364,16 +369,16 @@ class SafeConstructor(BaseConstructor): yield pairs if not isinstance(node, SequenceNode): raise ConstructorError("while constructing pairs", node.start_mark, - "expected a sequence, but found %s" % node.id, node.start_mark) + "expected a sequence, but found %s" % node.id, node.start_mark) for subnode in node.value: if not isinstance(subnode, MappingNode): raise ConstructorError("while constructing pairs", node.start_mark, - "expected a mapping of length 1, but found %s" % subnode.id, - subnode.start_mark) + "expected a mapping of length 1, but found %s" % subnode.id, + subnode.start_mark) if len(subnode.value) != 1: raise ConstructorError("while constructing pairs", node.start_mark, - "expected a single mapping item, but found %d items" % len(subnode.value), - subnode.start_mark) + "expected a single mapping item, but found %d items" % len(subnode.value), + subnode.start_mark) key_node, value_node = subnode.value[0] key = self.construct_object(key_node) value = self.construct_object(value_node) @@ -415,62 +420,63 @@ class SafeConstructor(BaseConstructor): def construct_undefined(self, node): raise ConstructorError(None, None, - "could not determine a constructor for the tag %r" % node.tag.encode('utf-8'), - node.start_mark) + "could not determine a constructor for the tag %r" % node.tag.encode('utf-8'), + node.start_mark) + SafeConstructor.add_constructor( - u'tag:yaml.org,2002:null', - SafeConstructor.construct_yaml_null) + u'tag:yaml.org,2002:null', + SafeConstructor.construct_yaml_null) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:bool', - SafeConstructor.construct_yaml_bool) + u'tag:yaml.org,2002:bool', + SafeConstructor.construct_yaml_bool) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:int', - SafeConstructor.construct_yaml_int) + u'tag:yaml.org,2002:int', + SafeConstructor.construct_yaml_int) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:float', - SafeConstructor.construct_yaml_float) + u'tag:yaml.org,2002:float', + SafeConstructor.construct_yaml_float) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:binary', - SafeConstructor.construct_yaml_binary) + u'tag:yaml.org,2002:binary', + SafeConstructor.construct_yaml_binary) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:timestamp', - SafeConstructor.construct_yaml_timestamp) + u'tag:yaml.org,2002:timestamp', + SafeConstructor.construct_yaml_timestamp) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:omap', - SafeConstructor.construct_yaml_omap) + u'tag:yaml.org,2002:omap', + SafeConstructor.construct_yaml_omap) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:pairs', - SafeConstructor.construct_yaml_pairs) + u'tag:yaml.org,2002:pairs', + SafeConstructor.construct_yaml_pairs) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:set', - SafeConstructor.construct_yaml_set) + u'tag:yaml.org,2002:set', + SafeConstructor.construct_yaml_set) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:str', - SafeConstructor.construct_yaml_str) + u'tag:yaml.org,2002:str', + SafeConstructor.construct_yaml_str) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:seq', - SafeConstructor.construct_yaml_seq) + u'tag:yaml.org,2002:seq', + SafeConstructor.construct_yaml_seq) SafeConstructor.add_constructor( - u'tag:yaml.org,2002:map', - SafeConstructor.construct_yaml_map) + u'tag:yaml.org,2002:map', + SafeConstructor.construct_yaml_map) SafeConstructor.add_constructor(None, - SafeConstructor.construct_undefined) + SafeConstructor.construct_undefined) -class Constructor(SafeConstructor): +class Constructor(SafeConstructor): def construct_python_str(self, node): return self.construct_scalar(node).encode('utf-8') @@ -481,7 +487,7 @@ class Constructor(SafeConstructor): return long(self.construct_yaml_int(node)) def construct_python_complex(self, node): - return complex(self.construct_scalar(node)) + return complex(self.construct_scalar(node)) def construct_python_tuple(self, node): return tuple(self.construct_sequence(node)) @@ -489,21 +495,21 @@ class Constructor(SafeConstructor): def find_python_module(self, name, mark): if not name: raise ConstructorError("while constructing a Python module", mark, - "expected non-empty name appended to the tag", mark) + "expected non-empty name appended to the tag", mark) try: __import__(name) except ImportError, exc: raise ConstructorError("while constructing a Python module", mark, - "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) + "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) return sys.modules[name] def find_python_name(self, name, mark): if not name: raise ConstructorError("while constructing a Python object", mark, - "expected non-empty name appended to the tag", mark) + "expected non-empty name appended to the tag", mark) if u'.' in name: # Python 2.4 only - #module_name, object_name = name.rsplit('.', 1) + # module_name, object_name = name.rsplit('.', 1) items = name.split('.') object_name = items.pop() module_name = '.'.join(items) @@ -514,40 +520,41 @@ class Constructor(SafeConstructor): __import__(module_name) except ImportError, exc: raise ConstructorError("while constructing a Python object", mark, - "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) + "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) module = sys.modules[module_name] if not hasattr(module, object_name): raise ConstructorError("while constructing a Python object", mark, - "cannot find %r in the module %r" % (object_name.encode('utf-8'), - module.__name__), mark) + "cannot find %r in the module %r" % (object_name.encode('utf-8'), + module.__name__), mark) return getattr(module, object_name) def construct_python_name(self, suffix, node): value = self.construct_scalar(node) if value: raise ConstructorError("while constructing a Python name", node.start_mark, - "expected the empty value, but found %r" % value.encode('utf-8'), - node.start_mark) + "expected the empty value, but found %r" % value.encode('utf-8'), + node.start_mark) return self.find_python_name(suffix, node.start_mark) def construct_python_module(self, suffix, node): value = self.construct_scalar(node) if value: raise ConstructorError("while constructing a Python module", node.start_mark, - "expected the empty value, but found %r" % value.encode('utf-8'), - node.start_mark) + "expected the empty value, but found %r" % value.encode('utf-8'), + node.start_mark) return self.find_python_module(suffix, node.start_mark) - class classobj: pass + class classobj: + pass def make_python_instance(self, suffix, node, - args=None, kwds=None, newobj=False): + args=None, kwds=None, newobj=False): if not args: args = [] if not kwds: kwds = {} cls = self.find_python_name(suffix, node.start_mark) - if newobj and isinstance(cls, type(self.classobj)) \ + if newobj and isinstance(cls, type(self.classobj)) \ and not args and not kwds: instance = self.classobj() instance.__class__ = cls @@ -618,6 +625,7 @@ class Constructor(SafeConstructor): def construct_python_object_new(self, suffix, node): return self.construct_python_object_apply(suffix, node, newobj=True) + Constructor.add_constructor( u'tag:yaml.org,2002:python/none', Constructor.construct_yaml_null) @@ -681,4 +689,3 @@ Constructor.add_multi_constructor( Constructor.add_multi_constructor( u'tag:yaml.org,2002:python/object/new:', Constructor.construct_python_object_new) - |
