Exemplo n.º 1
0
def render(  # pylint: disable=too-many-branches
    source, _saltenv, _sls, argline=None, **kwargs
):
    if not isinstance(source, dict):
        raise TypeError(f"Source must be a dict, not {type(source)}")

    selector = "grain"
    default = None
    key = "id"

    if argline:
        for arg in shlex.split(argline):
            try:
                (option, value) = arg.split("=", 2)
            except ValueError:
                option, value = arg, None

            if option in VALID_SELECTORS:
                if not value:
                    raise TemplateError(f"Selector {option!r} needs a value")
                selector = option
                key = value

            elif option == "default":
                if not value:
                    raise TemplateError(f"Option {option!r} needs a value")
                default = value

            else:
                raise TemplateError(f"Unknown option {option!r}")

    if selector == "grain":
        value = traverse_dict_and_list(__grains__, key, default)

    elif selector == "pillar":
        context = kwargs.get("context", {})
        if "pillar" in context:
            value = traverse_dict_and_list(context["pillar"], key, default)
        else:
            value = traverse_dict_and_list(__pillar__, key, default)

    if not value:
        LOG.debug("Skipping blank filter value: %r", value)
        return {}

    # Matching only works on strings
    value = str(value)

    for pattern in source:
        if fnmatch(value, pattern):
            return source[pattern]

    LOG.debug("No pattern matched value: %r", value)

    return {}
Exemplo n.º 2
0
def jinja_raise(msg):
    raise TemplateError(msg)