Skip to content

FsSpecProtocolPathLoader

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
  94142655719984["fsspecloaders.FsSpecProtocolPathLoader"]
  94142653860608["loaders.LoaderMixin"]
  140199010283712["builtins.object"]
  94142650643376["loaders.BaseLoader"]
  94142653860608 --> 94142655719984
  140199010283712 --> 94142653860608
  94142650643376 --> 94142655719984
  140199010283712 --> 94142650643376

🛈 DocStrings

Bases: LoaderMixin, BaseLoader

A jinja loader for fsspec filesystems.

This loader allows to access templates from an fsspec protocol path, like "github://phil65:mknodes@main/README.md"

Examples:

loader = FsSpecProtocolPathLoader()
env = Environment(loader=loader)
env.get_template("github://phil65:mknodes@main/docs/icons.jinja").render()
Source code in src/jinjarope/fsspecloaders.py
17
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
class FsSpecProtocolPathLoader(loaders_.LoaderMixin, jinja2.BaseLoader):
    """A jinja loader for fsspec filesystems.

    This loader allows to access templates from an fsspec protocol path,
    like "github://phil65:mknodes@main/README.md"

    Examples:
        ``` py
        loader = FsSpecProtocolPathLoader()
        env = Environment(loader=loader)
        env.get_template("github://phil65:mknodes@main/docs/icons.jinja").render()
        ```
    """

    ID = "fsspec_protocol_path"

    def __eq__(self, other: object):
        return type(self) is type(other)

    def __hash__(self):
        return hash(type(self))

    def get_source(
        self,
        environment: jinja2.Environment | None,
        template: str,
    ) -> tuple[str, str, Callable[[], bool] | None]:
        try:
            src = envglobals.load_file_cached(template)
        except FileNotFoundError as e:
            raise jinja2.TemplateNotFound(template) from e
        path = pathlib.Path(template).as_posix()
        return src, path, lambda: True

    def list_templates(self) -> list[str]:
        return []

    def __contains__(self, path: str):
        try:
            self.get_source(None, path)
        except jinja2.TemplateNotFound:
            return False
        else:
            return True

    def __repr__(self):
        return utils.get_repr(self)

Show source on GitHub