'ConstructorError']
# If there are more documents available?
# Construct and return the next document.
# Ensure that the stream contains a single document and construct it. return None
state_generators = self.state_generators self.state_generators = [] for generator in state_generators: for dummy in generator: pass
raise ConstructorError(None, None, "found unconstructable recursive node", node.start_mark) else: else: if None in self.yaml_multi_constructors: tag_suffix = node.tag constructor = self.yaml_multi_constructors[None] elif None in self.yaml_constructors: constructor = self.yaml_constructors[None] elif isinstance(node, ScalarNode): constructor = self.__class__.construct_scalar elif isinstance(node, SequenceNode): constructor = self.__class__.construct_sequence elif isinstance(node, MappingNode): constructor = self.__class__.construct_mapping else: pass else: self.state_generators.append(generator)
raise ConstructorError(None, None, "expected a scalar node, but found %s" % node.id, node.start_mark)
raise ConstructorError(None, None, "expected a sequence node, but found %s" % node.id, node.start_mark) for child in node.value]
raise ConstructorError(None, None, "expected a mapping node, but found %s" % node.id, node.start_mark) raise ConstructorError("while constructing a mapping", node.start_mark, "found unhashable key", key_node.start_mark)
raise ConstructorError(None, None, "expected a mapping node, but found %s" % node.id, node.start_mark)
def add_constructor(cls, tag, constructor):
def add_multi_constructor(cls, tag_prefix, multi_constructor):
for key_node, value_node in node.value: if key_node.tag == 'tag:yaml.org,2002:value': return self.construct_scalar(value_node)
del node.value[index] if isinstance(value_node, MappingNode): self.flatten_mapping(value_node) merge.extend(value_node.value) elif isinstance(value_node, SequenceNode): submerge = [] 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) self.flatten_mapping(subnode) submerge.append(subnode.value) submerge.reverse() for value in submerge: 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) key_node.tag = 'tag:yaml.org,2002:str' index += 1 else: node.value = merge + node.value
self.construct_scalar(node) return None
'yes': True, 'no': False, 'true': True, 'false': False, 'on': True, 'off': False, }
sign = -1 value = value[1:] return 0 return sign*int(value[2:], 2) return sign*int(value[2:], 16) return sign*int(value, 8) digits = [int(part) for part in value.split(':')] digits.reverse() base = 1 value = 0 for digit in digits: value += digit*base base *= 60 return sign*value else:
return sign*self.inf_value digits = [float(part) for part in value.split(':')] digits.reverse() base = 1 value = 0.0 for digit in digits: value += digit*base base *= 60 return sign*value else:
try: value = self.construct_scalar(node).encode('ascii') except UnicodeEncodeError as exc: raise ConstructorError(None, None, "failed to convert base64 data into ascii: %s" % exc, node.start_mark) try: if hasattr(base64, 'decodebytes'): return base64.decodebytes(value) else: return base64.decodestring(value) except binascii.Error as exc: raise ConstructorError(None, None, "failed to decode base64 data: %s" % exc, node.start_mark)
r'''^(?P<year>[0-9][0-9][0-9][0-9]) -(?P<month>[0-9][0-9]?) -(?P<day>[0-9][0-9]?) (?:(?:[Tt]|[ \t]+) (?P<hour>[0-9][0-9]?) :(?P<minute>[0-9][0-9]) :(?P<second>[0-9][0-9]) (?:\.(?P<fraction>[0-9]*))? (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?) (?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X)
return datetime.date(year, month, day) fraction += '0' tz_hour = int(values['tz_hour']) tz_minute = int(values['tz_minute'] or 0) delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) if values['tz_sign'] == '-': delta = -delta data -= delta
# Note: we do not check for duplicate keys, because it's too # CPU-expensive. omap = [] 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) 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) 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) key_node, value_node = subnode.value[0] key = self.construct_object(key_node) value = self.construct_object(value_node) omap.append((key, value))
# Note: the same code as `construct_yaml_omap`. pairs = [] 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) 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) 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) key_node, value_node = subnode.value[0] key = self.construct_object(key_node) value = self.construct_object(value_node) pairs.append((key, value))
data = set() yield data value = self.construct_mapping(node) data.update(value)
data = cls.__new__(cls) yield data if hasattr(data, '__setstate__'): state = self.construct_mapping(node, deep=True) data.__setstate__(state) else: state = self.construct_mapping(node) data.__dict__.update(state)
raise ConstructorError(None, None, "could not determine a constructor for the tag %r" % node.tag, node.start_mark)
'tag:yaml.org,2002:null', SafeConstructor.construct_yaml_null)
'tag:yaml.org,2002:bool', SafeConstructor.construct_yaml_bool)
'tag:yaml.org,2002:int', SafeConstructor.construct_yaml_int)
'tag:yaml.org,2002:float', SafeConstructor.construct_yaml_float)
'tag:yaml.org,2002:binary', SafeConstructor.construct_yaml_binary)
'tag:yaml.org,2002:timestamp', SafeConstructor.construct_yaml_timestamp)
'tag:yaml.org,2002:omap', SafeConstructor.construct_yaml_omap)
'tag:yaml.org,2002:pairs', SafeConstructor.construct_yaml_pairs)
'tag:yaml.org,2002:set', SafeConstructor.construct_yaml_set)
'tag:yaml.org,2002:str', SafeConstructor.construct_yaml_str)
'tag:yaml.org,2002:seq', SafeConstructor.construct_yaml_seq)
'tag:yaml.org,2002:map', SafeConstructor.construct_yaml_map)
SafeConstructor.construct_undefined)
return self.construct_scalar(node)
return self.construct_scalar(node)
try: value = self.construct_scalar(node).encode('ascii') except UnicodeEncodeError as exc: raise ConstructorError(None, None, "failed to convert base64 data into ascii: %s" % exc, node.start_mark) try: if hasattr(base64, 'decodebytes'): return base64.decodebytes(value) else: return base64.decodestring(value) except binascii.Error as exc: raise ConstructorError(None, None, "failed to decode base64 data: %s" % exc, node.start_mark)
return self.construct_yaml_int(node)
return complex(self.construct_scalar(node))
return tuple(self.construct_sequence(node))
if not name: raise ConstructorError("while constructing a Python module", mark, "expected non-empty name appended to the tag", mark) try: __import__(name) except ImportError as exc: raise ConstructorError("while constructing a Python module", mark, "cannot find module %r (%s)" % (name, exc), mark) return sys.modules[name]
if not name: raise ConstructorError("while constructing a Python object", mark, "expected non-empty name appended to the tag", mark) if '.' in name: module_name, object_name = name.rsplit('.', 1) else: module_name = 'builtins' object_name = name try: __import__(module_name) except ImportError as exc: raise ConstructorError("while constructing a Python object", mark, "cannot find module %r (%s)" % (module_name, 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, module.__name__), mark) return getattr(module, object_name)
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, node.start_mark) return self.find_python_name(suffix, node.start_mark)
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, node.start_mark) return self.find_python_module(suffix, node.start_mark)
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): return cls.__new__(cls, *args, **kwds) else: return cls(*args, **kwds)
if hasattr(instance, '__setstate__'): instance.__setstate__(state) else: slotstate = {} if isinstance(state, tuple) and len(state) == 2: state, slotstate = state if hasattr(instance, '__dict__'): instance.__dict__.update(state) elif state: slotstate.update(state) for key, value in slotstate.items(): setattr(object, key, value)
# Format: # !!python/object:module.name { ... state ... } instance = self.make_python_instance(suffix, node, newobj=True) yield instance deep = hasattr(instance, '__setstate__') state = self.construct_mapping(node, deep=deep) self.set_python_instance_state(instance, state)
# Format: # !!python/object/apply # (or !!python/object/new) # args: [ ... arguments ... ] # kwds: { ... keywords ... } # state: ... state ... # listitems: [ ... listitems ... ] # dictitems: { ... dictitems ... } # or short format: # !!python/object/apply [ ... arguments ... ] # The difference between !!python/object/apply and !!python/object/new # is how an object is created, check make_python_instance for details. if isinstance(node, SequenceNode): args = self.construct_sequence(node, deep=True) kwds = {} state = {} listitems = [] dictitems = {} else: value = self.construct_mapping(node, deep=True) args = value.get('args', []) kwds = value.get('kwds', {}) state = value.get('state', {}) listitems = value.get('listitems', []) dictitems = value.get('dictitems', {}) instance = self.make_python_instance(suffix, node, args, kwds, newobj) if state: self.set_python_instance_state(instance, state) if listitems: instance.extend(listitems) if dictitems: for key in dictitems: instance[key] = dictitems[key] return instance
return self.construct_python_object_apply(suffix, node, newobj=True)
'tag:yaml.org,2002:python/none', Constructor.construct_yaml_null)
'tag:yaml.org,2002:python/bool', Constructor.construct_yaml_bool)
'tag:yaml.org,2002:python/str', Constructor.construct_python_str)
'tag:yaml.org,2002:python/unicode', Constructor.construct_python_unicode)
'tag:yaml.org,2002:python/bytes', Constructor.construct_python_bytes)
'tag:yaml.org,2002:python/int', Constructor.construct_yaml_int)
'tag:yaml.org,2002:python/long', Constructor.construct_python_long)
'tag:yaml.org,2002:python/float', Constructor.construct_yaml_float)
'tag:yaml.org,2002:python/complex', Constructor.construct_python_complex)
'tag:yaml.org,2002:python/list', Constructor.construct_yaml_seq)
'tag:yaml.org,2002:python/tuple', Constructor.construct_python_tuple)
'tag:yaml.org,2002:python/dict', Constructor.construct_yaml_map)
'tag:yaml.org,2002:python/name:', Constructor.construct_python_name)
'tag:yaml.org,2002:python/module:', Constructor.construct_python_module)
'tag:yaml.org,2002:python/object:', Constructor.construct_python_object)
'tag:yaml.org,2002:python/object/apply:', Constructor.construct_python_object_apply)
'tag:yaml.org,2002:python/object/new:', Constructor.construct_python_object_new)
|