Skip to content

rewriteloader

Class info

Classes

Name Children Inherits
RewriteLoader
jinjarope.rewriteloader
A loader which modifies templates based on a callable.

    🛈 DocStrings

    RewriteLoader

    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
    15
    16
    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
    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]):
            """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):
            return utils.get_repr(self, self.loader, self.rewrite_fn)
    
        def __eq__(self, other):
            return (
                type(self) is type(other)
                and self.loader == other.loader
                and self.rewrite_fn == other.rewrite_fn
            )
    
        def __hash__(self):
            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])
    

    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    def __init__(self, loader: jinja2.BaseLoader, rewrite_fn: Callable[[str, str], str]):
        """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