本文整理汇总了Python中yaml.SequenceNode方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.SequenceNode方法的具体用法?Python yaml.SequenceNode怎么用?Python yaml.SequenceNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.SequenceNode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: multi_constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def multi_constructor(loader, tag_suffix, node):
"""
Deal with !Ref style function format
"""
if tag_suffix not in UNCONVERTED_SUFFIXES:
tag_suffix = '{}{}'.format(FN_PREFIX, tag_suffix)
constructor = None
if tag_suffix == 'Fn::GetAtt':
constructor = construct_getatt
elif isinstance(node, ScalarNode):
constructor = loader.construct_scalar
elif isinstance(node, SequenceNode):
constructor = loader.construct_sequence
elif isinstance(node, MappingNode):
constructor = loader.construct_mapping
else:
raise 'Bad tag: !{}'.format(tag_suffix)
return dict_node({tag_suffix: constructor(node)}, node.start_mark, node.end_mark)
示例2: multi_constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def multi_constructor(loader, tag_suffix, node):
"""
Deal with !Ref style function format
"""
if tag_suffix not in UNCONVERTED_SUFFIXES:
tag_suffix = "{}{}".format(FN_PREFIX, tag_suffix)
constructor = None
if tag_suffix == "Fn::GetAtt":
constructor = construct_getatt
elif isinstance(node, yaml.ScalarNode):
constructor = loader.construct_scalar
elif isinstance(node, yaml.SequenceNode):
constructor = loader.construct_sequence
elif isinstance(node, yaml.MappingNode):
constructor = loader.construct_mapping
else:
raise Exception("Bad tag: !{}".format(tag_suffix))
return ODict((
(tag_suffix, constructor(node)),
))
示例3: repr_pairs
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def repr_pairs(dump, tag, sequence, flow_style=None):
"""This is the same code as BaseRepresenter.represent_sequence(),
but the value passed to dump.represent_data() in the loop is a
dictionary instead of a tuple."""
value = []
node = yaml.SequenceNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
for (key, val) in sequence:
item = dump.represent_data({key: val})
if not (isinstance(item, yaml.ScalarNode) and not item.style):
best_style = False
value.append(item)
if flow_style is None:
if dump.default_flow_style is not None:
node.flow_style = dump.default_flow_style
else:
node.flow_style = best_style
return node
示例4: include
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def include(self, node):
if isinstance(node, yaml.ScalarNode):
return self.extractFile(self.construct_scalar(node))
elif isinstance(node, yaml.SequenceNode):
result = []
for i in self.construct_sequence(node):
i = general_function.get_absolute_path(i, self._root)
for j in general_files_func.get_ofs(i):
result += self.extractFile(j)
return result
elif isinstance(node, yaml.MappingNode):
result = {}
for k,v in self.construct_mapping(node).iteritems():
result[k] = self.extractFile(v)
return result
else:
print ('Error:: unrecognised node type in !include statement')
raise yaml.constructor.ConstructorError
示例5: handle_yaml_constructors
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def handle_yaml_constructors(loader, suffix, node):
"""
Constructor method for PyYaml to handle cfn intrinsic functions specified as yaml tags
"""
function_mapping = {
"!and": ("Fn::And", lambda x: x),
"!base64": ("Fn::Base64", lambda x: x),
"!condition": ("Condition", lambda x: x),
"!equals": ("Fn::Equals", lambda x: x),
"!findinmap": ("Fn::FindInMap", lambda x: x),
"!getatt": ("Fn::GetAtt", lambda x: str(x).split(".", 1)),
"!getazs": ("Fn::GetAZs", lambda x: x),
"!if": ("Fn::If", lambda x: x),
"!importvalue": ("Fn::ImportValue", lambda x: x),
"!join": ("Fn::Join", lambda x: [x[0], x[1]]),
"!not": ("Fn::Not", lambda x: x),
"!or": ("Fn::Or", lambda x: x),
"!ref": ("Ref", lambda x: x),
"!select": ("Fn::Select", lambda x: x),
"!split": ("Fn::Split", lambda x: x),
"!sub": ("Fn::Sub", lambda x: x),
}
try:
function, value_transformer = function_mapping[str(suffix).lower()]
except KeyError as key:
raise CfnSphereException(
"Unsupported cfn intrinsic function tag found: {0}".format(key))
if isinstance(node, yaml.ScalarNode):
value = loader.construct_scalar(node)
elif isinstance(node, yaml.SequenceNode):
value = loader.construct_sequence(node)
elif isinstance(node, yaml.MappingNode):
value = loader.construct_mapping(node)
else:
raise CfnSphereException(
"Invalid yaml node found while handling cfn intrinsic function tags")
return {function: value_transformer(value)}
示例6: construct_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def construct_odict(self, node):
omap = OrderedDict()
yield omap
if not isinstance(node, yaml.SequenceNode):
raise yaml.constructor.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, yaml.MappingNode):
raise yaml.constructor.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 yaml.constructor.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[key] = value
示例7: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def load(name: str, value: Any, *allowed: Tag) -> SequenceView:
if isinstance(value, str):
value = StringIO(value)
value.name = name
result = view(SequenceNode(Tag.SEQUENCE.value, list(compose_all(value))), ViewMode.PYTHON)
for r in view(result, ViewMode.NODE):
if r.tag not in allowed:
raise ValueError("expecting %s, got %s" % (", ".join(t.name for t in allowed),
r.node.tag))
return result
示例8: include
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def include(self, node):
"""!include file command. Supports:
foo: !include file.yaml
foo: !include [file1.yaml, file2.yaml]
Args:
node: The YAML node to load.
"""
# input is a single file to load.
if isinstance(node, yaml.ScalarNode):
include_file = self.construct_scalar(node)
return self._load_file(include_file)
# input is a list of files to load.
elif isinstance(node, yaml.SequenceNode):
result = []
for include_file in self.construct_sequence(node):
result += self._load_file(include_file)
return result
else:
msg = ("Error: unrecognized node type in !include statement: %s"
% str(node))
raise yaml.constructor.ConstructorError(msg)
#-----------------------------------------------------------------------
示例9: _repr_pairs
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def _repr_pairs(dump, tag, sequence, flow_style=None):
"""
This is the same code as BaseRepresenter.represent_sequence(),
but the value passed to dump.represent_data() in the loop is a
dictionary instead of a tuple.
Source: https://gist.github.com/weaver/317164
License: Unspecified
"""
import yaml
value = []
node = yaml.SequenceNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
for (key, val) in sequence:
item = dump.represent_data({key: val})
if not (isinstance(item, yaml.ScalarNode) and not item.style):
best_style = False
value.append(item)
if flow_style is None:
if dump.default_flow_style is not None:
node.flow_style = dump.default_flow_style
else:
node.flow_style = best_style
return node
示例10: construct_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def construct_odict(load, node):
"""This is the same as SafeConstructor.construct_yaml_omap(),
except the data type is changed to OrderedDict() and setitem is
used instead of append in the loop.
>>> yaml.load('''
... !!omap
... - foo: bar
... - mumble: quux
... - baz: gorp
... ''')
OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
>>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''')
OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
"""
omap = OrderedDict()
yield omap
if not isinstance(node, yaml.SequenceNode):
raise yaml.constructor.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, yaml.MappingNode):
raise yaml.constructor.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 yaml.constructor.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 = load.construct_object(key_node)
value = load.construct_object(value_node)
omap[key] = value
示例11: _construct_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SequenceNode [as 别名]
def _construct_odict(load, node):
"""
Construct OrderedDict from !!omap in yaml safe load.
Source: https://gist.github.com/weaver/317164
License: Unspecified
This is the same as SafeConstructor.construct_yaml_omap(),
except the data type is changed to OrderedDict() and setitem is
used instead of append in the loop
Examples
--------
::
>>> yaml.load(''' # doctest: +SKIP
... !!omap
... - foo: bar
... - mumble: quux
... - baz: gorp
... ''')
OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
>>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''') # doctest: +SKIP
OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
"""
import yaml
omap = OrderedDict()
yield omap
if not isinstance(node, yaml.SequenceNode):
raise yaml.constructor.ConstructorError(
"while constructing an ordered map", node.start_mark,
f"expected a sequence, but found {node.id}", node.start_mark)
for subnode in node.value:
if not isinstance(subnode, yaml.MappingNode):
raise yaml.constructor.ConstructorError(
"while constructing an ordered map", node.start_mark,
f"expected a mapping of length 1, but found {subnode.id}",
subnode.start_mark)
if len(subnode.value) != 1:
raise yaml.constructor.ConstructorError(
"while constructing an ordered map", node.start_mark,
"expected a single mapping item, but found {} items".format(len(subnode.value)),
subnode.start_mark)
key_node, value_node = subnode.value[0]
key = load.construct_object(key_node)
value = load.construct_object(value_node)
omap[key] = value