Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
#!/usr/bin/env python3

import os
import yaml

class Loader(yaml.SafeLoader):
    """
        YAML Loader with '!include' and '!join' constructors
    """
    def __init__(self, stream):
        """
            :param self: Loader instance
            :param stream: YAML formatted file
        """
        self._root = os.path.split(stream.name)[0]
        super(Loader, self).__init__(stream)

    def include(self, node):
        """
            :param self: Loader instance
            :param node: A node from YAML file
            :return: the content of the file after the !include tag in th value of the given node
        """
        file_name = os.path.join(self._root, self.construct_scalar(node))
        with open(file_name, 'r') as file:
            return yaml.load(file, Loader)

    def join(self, node):
        """
            :param self: Loader instance
            :param node: A node from YAML file
            :return: concatenation of the values in the list after the !join tag in the value of the given node
        """
        seq = self.construct_sequence(node)
        return ''.join([str(i) for i in seq])

#adding the constructors to YAML loader so it can parse '!include' tag
Loader.add_constructor('!include', Loader.include)
#adding the constructors to YAML loader so it can parse '!join' tag
Loader.add_constructor('!join', Loader.join)

def load(yaml_path):
    """
        :param yaml_path: path to YAML file 
        :return: pased dictionary from the YAML file with '!include' and '!join' constructors
    """
    with open(yaml_path,"r") as stream:
        pyyaml_dict = yaml.load(stream, Loader)
    return pyyaml_dict