Skip to content

NestedDictLoader

Sub classes

Name Children Inherits
TemplateFileLoader
jinjarope.configloaders
A jinja loader for loading templates from config files.

    Base classes

    Name Children Inherits
    LoaderMixin
    jinjarope.loaders
    Loader mixin which allows to OR loaders into a choice loader.
    BaseLoader
    jinja2.loaders
    Baseclass for all loaders. Subclass this and override `get_source` to

    ⋔ Inheritance diagram

    graph TD
      94173637836640["configloaders.NestedDictLoader"]
      94173637890544["loaders.LoaderMixin"]
      139866058043616["builtins.object"]
      94173636407456["loaders.BaseLoader"]
      94173637890544 --> 94173637836640
      139866058043616 --> 94173637890544
      94173636407456 --> 94173637836640
      139866058043616 --> 94173636407456

    🛈 DocStrings

    Bases: LoaderMixin, BaseLoader

    A jinja loader for loading templates from nested dicts.

    This loader allows to access templates from nested dicts. Can be used to load templates defined with markup like TOML.

    Examples:

    [example]
    template = "{{ something }}"
    
    content = tomllib.load(toml_file)
    loader = NestedDictLoader(content)
    env = Environment(loader=loader)
    env.get_template("example/template")
    

    Source code in src/jinjarope/configloaders.py
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    class NestedDictLoader(loaders.LoaderMixin, jinja2.BaseLoader):
        """A jinja loader for loading templates from nested dicts.
    
        This loader allows to access templates from nested dicts.
        Can be used to load templates defined with markup like TOML.
    
        Examples:
            ``` toml
            [example]
            template = "{{ something }}"
            ```
            ``` py
            content = tomllib.load(toml_file)
            loader = NestedDictLoader(content)
            env = Environment(loader=loader)
            env.get_template("example/template")
            ```
        """
    
        ID = "nested_dict"
    
        def __init__(self, mapping: NestedMapping):
            """Constructor.
    
            Args:
                mapping: A nested dict containing templates
            """
            super().__init__()
            self._data = mapping
    
        def __repr__(self):
            return utils.get_repr(self, mapping=self._data)
    
        def list_templates(self) -> list[str]:
            return list(iterfilters.flatten_dict(self._data).keys())
    
        def get_source(
            self,
            environment: jinja2.Environment,
            template: str,
        ) -> tuple[str, str | None, Callable[[], bool] | None]:
            data: Any = self._data
            try:
                for part in template.split("/"):
                    data = data[part]
                assert isinstance(data, str)
            except (AssertionError, KeyError) as e:
                raise jinja2.TemplateNotFound(template) from e
            return data, None, lambda: True  # type: ignore[return-value]
    

    __init__

    __init__(mapping: NestedMapping)
    

    Constructor.

    Parameters:

    Name Type Description Default
    mapping NestedMapping

    A nested dict containing templates

    required
    Source code in src/jinjarope/configloaders.py
    39
    40
    41
    42
    43
    44
    45
    46
    def __init__(self, mapping: NestedMapping):
        """Constructor.
    
        Args:
            mapping: A nested dict containing templates
        """
        super().__init__()
        self._data = mapping
    

    Show source on GitHub