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
  94892546069680["fsspecloaders.FsSpecProtocolPathLoader"]
  94892548325824["loaders.LoaderMixin"]
  140247609577664["builtins.object"]
  94892544482432["loaders.BaseLoader"]
  94892548325824 --> 94892546069680
  140247609577664 --> 94892548325824
  94892544482432 --> 94892546069680
  140247609577664 --> 94892544482432

🛈 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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

    async def get_source_async(
        self,
        environment: jinja2.Environment | None,
        template: str,
    ) -> tuple[str, str, Callable[[], bool] | None]:
        try:
            src = await get_template(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