本文整理汇总了Python中yaml.ScalarNode方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.ScalarNode方法的具体用法?Python yaml.ScalarNode怎么用?Python yaml.ScalarNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.ScalarNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unicode_representer
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def unicode_representer(_, data):
has_wide_lines = False
for line in data.splitlines():
if len(line) > 80:
has_wide_lines = True
break
if has_wide_lines:
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='>')
if "\n" in data:
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='|')
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='')
示例2: construct_mapping
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [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)
示例3: multi_constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [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)
示例4: rel_path
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def rel_path(self, node):
"""Handles !rel_path file command. Supports:
scenes: !rel_path file.yaml
Allows the use of relative paths in the config.yaml file. Intended
for use with scenes.
Args:
node: The YAML node to load.
"""
# input is a single file to load.
if isinstance(node, yaml.ScalarNode):
filename = self.construct_scalar(node)
return os.path.join(self._base_dir, filename)
else:
msg = ("Error: unrecognized node type in !rel_path statement: %s"
% str(node))
raise yaml.constructor.ConstructorError(msg)
#===========================================================================
示例5: multi_constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [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)),
))
示例6: represent_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def represent_odict(dump, tag, mapping, flow_style=None):
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump.represent_data(item_key)
node_value = dump.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode)
and not node_value.style):
best_style = False
value.append((node_key, node_value))
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
示例7: represent_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def represent_odict(self, dump, tag, mapping, flow_style=None):
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump.represent_data(item_key)
node_value = dump.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode)
and not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode)
and not node_value.style):
best_style = False
value.append((node_key, node_value))
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
示例8: represent_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def represent_odict(dump, tag, mapping, flow_style=None):
"""Like BaseRepresenter.represent_mapping, but does not issue the sort().
"""
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump.represent_data(item_key)
node_value = dump.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
best_style = False
value.append((node_key, node_value))
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
示例9: repr_pairs
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [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
示例10: include
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [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
示例11: represent_odict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def represent_odict(dump, tag, mapping, flow_style=None):
"""
Like BaseRepresenter.represent_mapping, but does not issue the sort().
"""
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)
if dump.alias_key is not None:
dump.represented_objects[dump.alias_key] = node
best_style = True
if hasattr(mapping, 'items'):
mapping = mapping.items()
for item_key, item_value in mapping:
node_key = dump.represent_data(item_key)
node_value = dump.represent_data(item_value)
if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
best_style = False
if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
best_style = False
value.append((node_key, node_value))
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
示例12: test_handle_yaml_constructors_converts_base64
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def test_handle_yaml_constructors_converts_base64(self):
loader_mock = Mock()
loader_mock.construct_scalar.return_value = "myString"
node_mock = Mock(spec=yaml.ScalarNode)
response = FileLoader.handle_yaml_constructors(loader_mock, "!base64", node_mock)
self.assertEqual({'Fn::Base64': 'myString'}, response)
示例13: test_handle_yaml_constructors_converts_and
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def test_handle_yaml_constructors_converts_and(self):
loader_mock = Mock()
loader_mock.construct_scalar.return_value = ["myCondition", "myOtherCondition"]
node_mock = Mock(spec=yaml.ScalarNode)
response = FileLoader.handle_yaml_constructors(loader_mock, "!and", node_mock)
self.assertEqual({'Fn::And': ["myCondition", "myOtherCondition"]}, response)
示例14: test_handle_yaml_constructors_converts_equals
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def test_handle_yaml_constructors_converts_equals(self):
loader_mock = Mock()
loader_mock.construct_scalar.return_value = ["myValue", "myOtherValue"]
node_mock = Mock(spec=yaml.ScalarNode)
response = FileLoader.handle_yaml_constructors(loader_mock, "!equals", node_mock)
self.assertEqual({'Fn::Equals': ["myValue", "myOtherValue"]}, response)
示例15: test_handle_yaml_constructors_converts_if
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import ScalarNode [as 别名]
def test_handle_yaml_constructors_converts_if(self):
loader_mock = Mock()
loader_mock.construct_scalar.return_value = ["myCondition", "myOtherCondition"]
node_mock = Mock(spec=yaml.ScalarNode)
response = FileLoader.handle_yaml_constructors(loader_mock, "!if", node_mock)
self.assertEqual({'Fn::If': ["myCondition", "myOtherCondition"]}, response)