Skip to content

PackageLoader

Base classes

Name Children Inherits
LoaderMixin
jinjarope.loaders
Loader mixin which allows to OR loaders into a choice loader.
PackageLoader
jinja2.loaders
Load templates from a directory in a Python package.

⋔ Inheritance diagram

graph TD
  97041655344464["loaders.PackageLoader"]
  97041656291584["loaders.LoaderMixin"]
  128674497047040["builtins.object"]
  97041654427456["loaders.PackageLoader"]
  97041655470432["loaders.BaseLoader"]
  97041656291584 --> 97041655344464
  128674497047040 --> 97041656291584
  97041654427456 --> 97041655344464
  97041655470432 --> 97041654427456
  128674497047040 --> 97041655470432

🛈 DocStrings

Bases: LoaderMixin, PackageLoader

A loader for loading templates from a package.

Source code in src/jinjarope/loaders.py
class PackageLoader(LoaderMixin, jinja2.PackageLoader):
    """A loader for loading templates from a package."""

    ID = "package"

    def __init__(
        self,
        package: str | types.ModuleType,
        package_path: str | None = None,
        encoding: str = "utf-8",
    ) -> None:
        """Instanciate a PackageLoader.

        Compared to the jinja2 equivalent, this loader also supports
        `ModuleType`s and dotted module paths for the `package` argument.

        Args:
            package: The python package to create a loader for
            package_path: If given, use the given path as the root.
            encoding: The encoding to use for loading templates
        """
        if isinstance(package, types.ModuleType):
            package = package.__name__
        parts = package.split(".")
        path = "/".join(parts[1:])
        if package_path:
            path = (pathlib.Path(path) / package_path).as_posix()
        super().__init__(parts[0], path, encoding)

    def __repr__(self) -> str:
        return utils.get_repr(
            self,
            package_name=self.package_name,
            package_path=self.package_path,
        )

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, PackageLoader):
            return NotImplemented
        return self.package_name == other.package_name and self.package_path == other.package_path

    def __hash__(self) -> int:
        return hash(self.package_name) + hash(self.package_path)

__init__

__init__(
    package: str | ModuleType, package_path: str | None = None, encoding: str = "utf-8"
) -> None

Instanciate a PackageLoader.

Compared to the jinja2 equivalent, this loader also supports ModuleTypes and dotted module paths for the package argument.

Parameters:

Name Type Description Default
package str | ModuleType

The python package to create a loader for

required
package_path str | None

If given, use the given path as the root.

None
encoding str

The encoding to use for loading templates

'utf-8'
Source code in src/jinjarope/loaders.py
def __init__(
    self,
    package: str | types.ModuleType,
    package_path: str | None = None,
    encoding: str = "utf-8",
) -> None:
    """Instanciate a PackageLoader.

    Compared to the jinja2 equivalent, this loader also supports
    `ModuleType`s and dotted module paths for the `package` argument.

    Args:
        package: The python package to create a loader for
        package_path: If given, use the given path as the root.
        encoding: The encoding to use for loading templates
    """
    if isinstance(package, types.ModuleType):
        package = package.__name__
    parts = package.split(".")
    path = "/".join(parts[1:])
    if package_path:
        path = (pathlib.Path(path) / package_path).as_posix()
    super().__init__(parts[0], path, encoding)

:fa-brands-github: Show source on GitHub