Python yaml.resolver() Examples

The following are 4 code examples of yaml.resolver(). 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: parser.py    From aws-serverlessrepo-python with Apache License 2.0 6 votes vote down vote up
def parse_template(template_str):
    """
    Parse the SAM template.

    :param template_str: A packaged YAML or json CloudFormation template
    :type template_str: str
    :return: Dictionary with keys defined in the template
    :rtype: dict
    """
    try:
        # PyYAML doesn't support json as well as it should, so if the input
        # is actually just json it is better to parse it with the standard
        # json parser.
        return json.loads(template_str, object_pairs_hook=OrderedDict)
    except ValueError:
        yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor)
        yaml.SafeLoader.add_multi_constructor('!', intrinsics_multi_constructor)
        return yaml.safe_load(template_str) 
Example #2
Source File: utils.py    From excelcy with MIT License 6 votes vote down vote up
def yaml_load(file_path: str):
    import yaml
    from yaml.resolver import BaseResolver

    def ordered_load(stream, loader_cls):
        class OrderedLoader(loader_cls):
            pass

        def construct_mapping(loader, node):
            loader.flatten_mapping(node)
            return odict(loader.construct_pairs(node))

        OrderedLoader.add_constructor(BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
        return yaml.load(stream, OrderedLoader)

    return ordered_load(open(file_path, 'r'), yaml.SafeLoader) 
Example #3
Source File: util.py    From report-ng with GNU General Public License v2.0 5 votes vote down vote up
def yaml_load(stream, Loader=yaml.Loader, object_pairs_hook=UnsortableOrderedDict):
    class MyLoader(Loader):
        pass

    # use UnsortableOrderedDict
    MyLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                             lambda loader, node: object_pairs_hook(loader.construct_pairs(node)))
    # treat str as unicode

    def construct_yaml_str(self, node):
        return self.construct_scalar(node)

    MyLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    return yaml.load(stream, MyLoader) 
Example #4
Source File: yamlhelper.py    From bash-lambda-layer with MIT License 5 votes vote down vote up
def yaml_parse(yamlstr):
    """Parse a yaml string"""
    try:
        # PyYAML doesn't support json as well as it should, so if the input
        # is actually just json it is better to parse it with the standard
        # json parser.
        return json.loads(yamlstr, object_pairs_hook=OrderedDict)
    except ValueError:
        yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor)
        yaml.SafeLoader.add_multi_constructor(
            "!", intrinsics_multi_constructor)
        return yaml.safe_load(yamlstr)