Skip to content

RewriteLoader

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
  97041652965376["rewriteloader.RewriteLoader"]
  97041656291584["loaders.LoaderMixin"]
  128674497047040["builtins.object"]
  97041655470432["loaders.BaseLoader"]
  97041656291584 --> 97041652965376
  128674497047040 --> 97041656291584
  97041655470432 --> 97041652965376
  128674497047040 --> 97041655470432

🛈 DocStrings

Bases: LoaderMixin, BaseLoader

A loader which modifies templates based on a callable.

Can get chained like a PrefixLoader. The path passed to the callable can be used to check whether given template should be modified.

Source code in src/jinjarope/rewriteloader.py
class RewriteLoader(loaders_.LoaderMixin, jinja2.BaseLoader):
    """A loader which modifies templates based on a callable.

    Can get chained like a PrefixLoader.
    The path passed to the callable can be used to check whether given template
    should be modified.
    """

    ID = "rewrite"

    def __init__(self, loader: jinja2.BaseLoader, rewrite_fn: Callable[[str, str], str]) -> None:
        """Instanciate the RewriteLoader.

        Args:
            loader: The loader to rewrite / modify the templates from
            rewrite_fn: Callable to modify templates.
                        It gets called with two arguments (path and template text)
                        and should return a (possibly modified) template text
        """
        self.loader = loader
        self.rewrite_fn = rewrite_fn

    def __repr__(self) -> str:
        return utils.get_repr(self, self.loader, self.rewrite_fn)

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, RewriteLoader):
            return NotImplemented
        return self.loader == other.loader and self.rewrite_fn == other.rewrite_fn

    def __hash__(self) -> int:
        return hash(self.loader) + hash(self.rewrite_fn)

    def get_source(
        self,
        environment: jinja2.Environment,
        template: str,
    ) -> tuple[str, str, Callable[[], bool] | None]:
        src: str | None
        src, filename, uptodate = self.loader.get_source(environment, template)
        old_src = src
        assert filename is not None
        path = pathlib.Path(filename).as_posix()
        src = self.rewrite_fn(path, src)
        return src or old_src, filename, uptodate

__init__

__init__(loader: BaseLoader, rewrite_fn: Callable[[str, str], str]) -> None

Instanciate the RewriteLoader.

Parameters:

Name Type Description Default
loader BaseLoader

The loader to rewrite / modify the templates from

required
rewrite_fn Callable[[str, str], str]

Callable to modify templates. It gets called with two arguments (path and template text) and should return a (possibly modified) template text

required
Source code in src/jinjarope/rewriteloader.py
def __init__(self, loader: jinja2.BaseLoader, rewrite_fn: Callable[[str, str], str]) -> None:
    """Instanciate the RewriteLoader.

    Args:
        loader: The loader to rewrite / modify the templates from
        rewrite_fn: Callable to modify templates.
                    It gets called with two arguments (path and template text)
                    and should return a (possibly modified) template text
    """
    self.loader = loader
    self.rewrite_fn = rewrite_fn

:fa-brands-github: Show source on GitHub