Пример #1
0
def add_constructor(tag,
                    object_constructor,
                    Loader=None,
                    constructor=Constructor):
    # type: (Any, Any, Any, Any) -> None
    """
    Add an object constructor for the given tag.
    object_onstructor is a function that accepts a Loader instance
    and a node object and produces the corresponding Python object.
    """
    if Loader is None:
        constructor.add_constructor(tag, object_constructor)
    else:
        if hasattr(Loader, 'add_constructor'):
            Loader.add_constructor(tag, object_constructor)
            return
        if issubclass(Loader, BaseLoader):
            BaseConstructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, SafeLoader):
            SafeConstructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, Loader):
            Constructor.add_constructor(tag, object_constructor)
        elif issubclass(Loader, RoundTripLoader):
            RoundTripConstructor.add_constructor(tag, object_constructor)
        else:
            raise NotImplementedError
Пример #2
0
def load_yaml(fname: str) -> JSON_TYPE:
    """Load a YAML file."""
    from ruamel.yaml import YAML
    from ruamel.yaml.constructor import RoundTripConstructor
    from ruamel.yaml.error import YAMLError

    RoundTripConstructor.add_constructor(None, _yaml_unsupported)

    yaml = YAML(typ='rt')

    try:
        with open(fname, encoding='utf-8') as conf_file:
            # If configuration file is empty YAML returns None
            # We convert that to an empty dict
            return yaml.load(conf_file) or OrderedDict()
    except YAMLError as exc:
        _LOGGER.error("YAML error in %s: %s", fname, exc)
        raise HomeAssistantError(exc)
    except UnicodeDecodeError as exc:
        _LOGGER.error("Unable to read file %s: %s", fname, exc)
        raise HomeAssistantError(exc)
    global jinja
    template = jinja.get_template(path)
    yaml = YAML(typ='rt')
    yaml.preserve_quotes = True
    yaml.Constructor = RoundTripConstructor
    return yaml.load(template.render(args) + '\n')


def include_statement(loader, node):
    fn, *args = node.value.split(' ', 1)
    if args:
        args = json.loads(args[0])
    return process_file(fn, args)


RoundTripConstructor.add_constructor("!include", include_statement)


def file_statement(loader, node):
    path = node.value
    timestamp = time.time()
    if '?' in path:
        return '{}&{}'.format(path, str(timestamp))
    else:
        return '{}?{}'.format(path, str(timestamp))


RoundTripConstructor.add_constructor("!file", file_statement)


def main():
Пример #4
0
def construct_expression(self, node):
    if isinstance(node, MapExpressionNode):
        return CommentedMapExpression(node.value)

    assert isinstance(node, yaml.nodes.ScalarNode), (type(node), node)

    v = node.value
    if isinstance(v, str):
        v = exp.parse(v)

    assert isinstance(v, exp.Expression), (type(v), v)

    return v


RoundTripConstructor.add_constructor(u'tag:github.com,2020:expression', construct_expression)


# ==============================================================


class CommentedMapExpression(yaml.comments.CommentedMap):
    def __init__(self, a0, *args, **kw):
        yaml.comments.CommentedMap.__init__(self, [], *args, **kw)

        if isinstance(a0, str):
            a0 = exp.parse(a0)

        self.exp_value = a0

    def __repr__(self):