def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' % node.id, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError, exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
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
python类constructor()的实例源码
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' % node.id, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
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
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' % node.id, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
node.start_mark, 'found unacceptable key (%s)' % exc,
key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
if key in mapping:
raise KeyError('There are two same keys "'+node+'" in the yaml file.')
mapping[key] = value
return mapping
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(
None
, None
, 'expected a mapping node, but found {}'.format(node.id)
, node.start_mark
)
mapping = collections.OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as ex:
raise yaml.constructor.ConstructorError(
'while constructing a mapping'
, node.start_mark
, 'found unacceptable key `{}`'.format(ex)
, key_node.start_mark
)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
def construct_specsparameters(loader, node, spectype):
spec_class = {x.__name__: x for x in Spec.__subclasses__()}.get(spectype, None)
if not spec_class:
yaml_error(loader, yaml.constructor.ConstructorError(
None, None,
"Unrecognized Spec class %s" % spectype,
node.start_mark))
return
if not isinstance(node, yaml.MappingNode):
yaml_error(loader, yaml.constructor.ConstructorError(
None, None,
"expected a mapping node, but found %s" % node.id,
node.start_mark))
return
specs = OrderedDict()
for spec_key_node, spec_value_node in node.value:
try:
spec_key = str(loader.construct_scalar(spec_key_node))
except yaml.MarkedYAMLError, e:
yaml_error(loader, e)
specs[spec_key] = construct_spec(spec_class, loader, spec_value_node)
return specs
def _get_loader(self):
"""
Get a loader that uses an IgnoreCaseDict for
complex objects.
:return yaml.Loader: The loader object.
"""
# this class was copied from
# https://github.com/fmenabe/python-yamlordereddictloader/blob/master/yamlordereddictloader.py
# and adapted to use IgnoreCaseDict
class Loader(yaml.Loader):
def __init__(self, *args, **kwargs):
yaml.Loader.__init__(self, *args, **kwargs)
self.add_constructor(
'tag:yaml.org,2002:map', type(self).construct_yaml_map)
self.add_constructor(
'tag:yaml.org,2002:omap', type(self).construct_yaml_map)
def construct_yaml_map(self, node):
data = IgnoreCaseDict()
yield data
value = self.construct_mapping(node)
data.update(value)
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(
None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark)
mapping = IgnoreCaseDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as err:
raise yaml.constructor.ConstructorError(
'while constructing a mapping', node.start_mark,
'found unacceptable key (%s)' % err, key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
return Loader