Python yaml.ScalarNode() Examples
The following are 30
code examples of yaml.ScalarNode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
yaml
, or try the search function
.
Example #1
Source File: utils.py From tacker with Apache License 2.0 | 6 votes |
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
Example #2
Source File: yaml_utils.py From rekall with GNU General Public License v2.0 | 6 votes |
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='')
Example #3
Source File: commands.py From cloud-custodian with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: cfn_yaml.py From checkov with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: config.py From insteon-mqtt with GNU General Public License v3.0 | 6 votes |
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) #===========================================================================
Example #6
Source File: yaml_loader.py From aws-cfn-template-flip with Apache License 2.0 | 6 votes |
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)), ))
Example #7
Source File: translate_template.py From tacker with Apache License 2.0 | 6 votes |
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
Example #8
Source File: representers.py From hokusai with MIT License | 6 votes |
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
Example #9
Source File: yaml_odict.py From airspyone_firmware with GNU General Public License v2.0 | 6 votes |
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
Example #10
Source File: specific_function.py From nxs-backup with GNU General Public License v3.0 | 6 votes |
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
Example #11
Source File: base_entity.py From v20-python with MIT License | 6 votes |
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
Example #12
Source File: util.py From forge with Apache License 2.0 | 5 votes |
def unicode_representer(dumper, uni): node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni) return node
Example #13
Source File: test_tools.py From aws-cfn-template-flip with Apache License 2.0 | 5 votes |
def test_represent_scalar(): """ When invoking represent_scalar apply style if value has \n :return: ScalarNode """ source = """z: first m: !Sub - The cake is a ${CakeType} - CakeType: lie a: !Ref last """ yaml_dumper = CfnYamlDumper(six.StringIO(source)) resp = yaml_dumper.represent_scalar('Key', 'value\n') print(resp) assert isinstance(resp, ScalarNode)
Example #14
Source File: meta.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #15
Source File: config.py From insteon-mqtt with GNU General Public License v3.0 | 5 votes |
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) #-----------------------------------------------------------------------
Example #16
Source File: ihateyaml.py From ansible-cmdb with GNU General Public License v3.0 | 5 votes |
def handle_tag(self, node_name, node): # I just *know* there are gonna be problems with simply returning a # Scalar, but I don't give a fuck at this point. if node_name == "vault": new_node = yaml.ScalarNode(node_name, 'ENCRYPTED CONTENTS REDACTED') else: new_node = yaml.ScalarNode(node_name, node.value) return self.construct_scalar(new_node) # Fugly!
Example #17
Source File: prettystate.py From nmstate with GNU Lesser General Public License v2.1 | 5 votes |
def represent_unicode(_, data): """ Represent unicode as regular string Source: https://stackoverflow.com/questions/1950306/pyyaml-dumping-without-tags """ return yaml.ScalarNode( tag="tag:yaml.org,2002:str", value=data.encode("utf-8") )
Example #18
Source File: utfyaml.py From pykit with MIT License | 5 votes |
def represent_mapping(dumper, mapping, flow_style=None): # override to dump map with item in any types value = [] tag = u'tag:yaml.org,2002:map' node = MappingNode(tag, value, flow_style=flow_style) if dumper.alias_key is not None: dumper.represented_objects[dumper.alias_key] = node # there is the change # ex: [u'hello', '中国'].sort() raises an error # if hasattr(mapping, 'items'): # mapping = mapping.items() # mapping.sort() best_style = True for item_key, item_value in mapping.items(): node_key = dumper.represent_data(item_key) node_value = dumper.represent_data(item_value) if not (isinstance(node_key, ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dumper.default_flow_style is not None: node.flow_style = dumper.default_flow_style else: node.flow_style = best_style return node
Example #19
Source File: file_loader.py From cfn-sphere with Apache License 2.0 | 5 votes |
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)}
Example #20
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_raises_exception_on_unknown_tag(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = "myResource" node_mock = Mock(spec=yaml.ScalarNode) with self.assertRaises(CfnSphereException): FileLoader.handle_yaml_constructors(loader_mock, "!anyTag", node_mock)
Example #21
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_ref(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = "myResource" node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!ref", node_mock) self.assertEqual({'Ref': "myResource"}, response)
Example #22
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_sub(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = ["string", {"key": "value"}] node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!sub", node_mock) self.assertEqual({'Fn::Sub': ["string", {"key": "value"}]}, response)
Example #23
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_split(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = ["delimiter", "string"] node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!split", node_mock) self.assertEqual({'Fn::Split': ["delimiter", "string"]}, response)
Example #24
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_join(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = ["delimiter", ["a", "b"]] node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!join", node_mock) self.assertEqual({'Fn::Join': ["delimiter", ["a", "b"]]}, response)
Example #25
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_import_value(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = "sharedValue" node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!ImportValue", node_mock) self.assertEqual({'Fn::ImportValue': "sharedValue"}, response)
Example #26
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_get_azs(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = "region" node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!GetAZs", node_mock) self.assertEqual({'Fn::GetAZs': "region"}, response)
Example #27
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_getatt(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = "logicalNameOfResource.attributeName" node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!GetAtt", node_mock) self.assertEqual({'Fn::GetAtt': ["logicalNameOfResource", "attributeName"]}, response)
Example #28
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_find_in_map(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = ["MapName", "TopLevelKey", "SecondLevelKey"] node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!FindInMap", node_mock) self.assertEqual({'Fn::FindInMap': ["MapName", "TopLevelKey", "SecondLevelKey"]}, response)
Example #29
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
def test_handle_yaml_constructors_converts_not(self): loader_mock = Mock() loader_mock.construct_scalar.return_value = ["myCondition"] node_mock = Mock(spec=yaml.ScalarNode) response = FileLoader.handle_yaml_constructors(loader_mock, "!not", node_mock) self.assertEqual({'Fn::Not': ["myCondition"]}, response)
Example #30
Source File: file_loader_tests.py From cfn-sphere with Apache License 2.0 | 5 votes |
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)