本文整理汇总了Python中yaml.constructor.ConstructorError方法的典型用法代码示例。如果您正苦于以下问题:Python constructor.ConstructorError方法的具体用法?Python constructor.ConstructorError怎么用?Python constructor.ConstructorError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml.constructor
的用法示例。
在下文中一共展示了constructor.ConstructorError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unsorted
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def test_unsorted(self):
source = SplitYamlProvider(
'test', join(dirname(__file__), 'config/split'))
zone = Zone('unordered.', [])
with self.assertRaises(ConstructorError):
source.populate(zone)
zone = Zone('unordered.', [])
source = SplitYamlProvider(
'test', join(dirname(__file__), 'config/split'),
enforce_order=False)
# no exception
source.populate(zone)
self.assertEqual(2, len(zone.records))
示例2: test_load_duplicate_keys_top
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def test_load_duplicate_keys_top() -> None:
from yaml.constructor import ConstructorError
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
content = """
a:
b: 1
a:
b: 2
"""
fp.write(content.encode("utf-8"))
with pytest.raises(ConstructorError):
OmegaConf.load(fp.name)
finally:
os.unlink(fp.name)
示例3: test_load_duplicate_keys_sub
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def test_load_duplicate_keys_sub() -> None:
from yaml.constructor import ConstructorError
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
content = """
a:
b: 1
c: 2
b: 3
"""
fp.write(content.encode("utf-8"))
with pytest.raises(ConstructorError):
OmegaConf.load(fp.name)
finally:
os.unlink(fp.name)
示例4: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def construct_mapping(self, node, deep=False):
if not isinstance(node, yaml.MappingNode):
raise ConstructorError(None, None,
"expected a mapping node, but found %s" % node.id,
node.start_mark)
key_set = set()
for key_node, value_node in node.value:
if not isinstance(key_node, yaml.ScalarNode):
continue
k = key_node.value
if k in key_set:
raise ConstructorError(
"while constructing a mapping", node.start_mark,
"found duplicate key", key_node.start_mark)
key_set.add(k)
return super(DuplicateKeyCheckLoader, self).construct_mapping(node, deep)
示例5: load
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def load(self, config_path):
"""Load a CFNgin config into a context object.
Args:
config_path (str): Valid path to a CFNgin config file.
Returns:
:class:`runway.cfngin.context.Context`
"""
LOGGER.debug('%s: loading...', os.path.basename(config_path))
try:
config = self._get_config(config_path)
return self._get_context(config, config_path)
except ConstructorError as err:
if err.problem.startswith('could not determine a constructor '
'for the tag \'!'):
LOGGER.error('"%s" appears to be a CloudFormation template, '
'but is located in the top level of a module '
'alongside the CloudFormation config files (i.e. '
'the file or files indicating the stack names & '
'parameters). Please move the template to a '
'subdirectory.', config_path)
sys.exit(1)
raise
示例6: test_raise_constructor_error_on_keyword_duplicate_key
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def test_raise_constructor_error_on_keyword_duplicate_key(self):
"""Some keys should never have a duplicate sibling.
For example we treat `class_path` as a special "keyword" and
disallow dupes.
"""
yaml_config = """
namespace: prod
stacks:
- name: vpc
class_path: blueprints.VPC
class_path: blueprints.Fake
"""
with self.assertRaises(ConstructorError):
parse(yaml_config)
示例7: test_raise_construct_error_on_duplicate_stack_name_dict
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def test_raise_construct_error_on_duplicate_stack_name_dict(self):
"""Some mappings should never have a duplicate children.
For example we treat `stacks` as a special mapping and disallow
dupe children keys.
"""
yaml_config = """
namespace: prod
stacks:
my_vpc:
class_path: blueprints.VPC1
my_vpc:
class_path: blueprints.VPC2
"""
with self.assertRaises(ConstructorError):
parse(yaml_config)
示例8: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
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)
mapping = {}
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as exc:
raise ConstructorError("while constructing a mapping", node.start_mark,
"found unacceptable key (%s)" % exc, key_node.start_mark)
# check for duplicate keys
if key in mapping:
raise ConstructorError("while constructing a mapping", node.start_mark,
"found duplicate key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
示例9: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
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)
# UNITY: dict has to be ordered to reproduce nested maps, also save flow style
mapping = OrderedFlowDict()
mapping.set_flow_style(node.flow_style)
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
if not isinstance(key, collections.abc.Hashable):
raise ConstructorError("while constructing a mapping", node.start_mark,
"found unhashable key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
示例10: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def construct_mapping(self, node, deep=False):
"""Check for duplicate YAML keys."""
try:
key_value_pairs = [
(self.construct_object(key_node, deep=deep),
self.construct_object(value_node, deep=deep))
for key_node, value_node in node.value]
except TypeError as err:
raise ConstructorError('invalid key type: %s' % err)
mapping = {}
for key, value in key_value_pairs:
try:
if key in mapping:
raise ConstructorError('duplicate key: %s' % key)
except TypeError:
raise ConstructorError('unhashable key: %s' % key)
mapping[key] = value
return mapping
示例11: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def construct_mapping(self, node, deep=False):
if isinstance(node, MappingNode):
self.flatten_mapping(node)
if not isinstance(node, MappingNode):
raise 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)
if not isinstance(key, Hashable):
raise ConstructorError("while constructing a mapping", node.start_mark,
"found unhashable key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
示例12: testIfTableIsAMap
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def testIfTableIsAMap(self):
from yaml.constructor import ConstructorError
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
# Credit https://gist.github.com/pypt/94d747fe5180851196eb
def no_duplicates_constructor(loader, node, deep=False):
"""Check for duplicate keys."""
mapping = {}
for key_node, value_node in node.value:
key = loader.construct_object(key_node, deep=deep)
value = loader.construct_object(value_node, deep=deep)
if key in mapping:
raise ConstructorError(
"while constructing a mapping", node.start_mark,
"found duplicate key (%s)" % key, key_node.start_mark)
mapping[key] = value
return loader.construct_mapping(node, deep)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor)
file_path = '../translationtable/GLOBAL_TERMS.yaml'
if os.path.exists(os.path.join(os.path.dirname(__file__), file_path)):
tt_file = open(os.path.join(os.path.dirname(__file__), file_path), 'r')
try:
translation_table = yaml.safe_load(tt_file)
except yaml.constructor.ConstructorError as e:
tt_file.close()
self.assertTrue(False)
print(e)
tt_file.close()
示例13: _construct
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def _construct(self, node):
self.flatten_mapping(node)
ret = self.construct_pairs(node)
keys = [d[0] for d in ret]
keys_sorted = sorted(keys, key=_natsort_key)
for key in keys:
expected = keys_sorted.pop(0)
if key != expected:
raise ConstructorError(None, None, 'keys out of order: '
'expected {} got {} at {}'
.format(expected, key, node.start_mark))
return dict(ret)
示例14: construct_mapping
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def construct_mapping(self, node, deep=False):
if isinstance(node, MappingNode):
self.flatten_mapping(node)
mapping = FrozenDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
if not isinstance(key, Hashable):
raise ConstructorError(
"while constructing a mapping", node.start_mark,
"found unhashable key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
mapping.freeze()
return mapping
示例15: NoDuplicatesConstructor
# 需要导入模块: from yaml import constructor [as 别名]
# 或者: from yaml.constructor import ConstructorError [as 别名]
def NoDuplicatesConstructor(loader, node, deep=False):
"""Check for duplicate keys."""
mapping = {}
for key_node, value_node in node.value:
key = loader.construct_object(key_node, deep=deep)
value = loader.construct_object(value_node, deep=deep)
if key in mapping:
raise ConstructorError('while constructing a mapping', node.start_mark,
'found duplicate key (%s)' % key,
key_node.start_mark)
mapping[key] = value
return loader.construct_mapping(node, deep)