Skip to content

copier_template_extensions ¤

Copier Template-Extensions package.

Special Jinja2 extension for Copier that allows to load extensions using file paths relative to the template root instead of Python dotted paths.

Classes:

ContextHook ¤

ContextHook(
    extension_self: Extension, environment: Environment
)

Bases: Extension

Extension allowing to modify the Copier context.

Parameters:

  • environment ¤

    (Environment) –

    The Jinja environment.

Methods:

  • hook –

    Abstract hook. Does nothing.

Attributes:

  • update (bool) –

    Deprecated attribute to indicate whether the context should be updated with the value returned by hook.

Source code in src/copier_template_extensions/_internal/context.py
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
def __init__(extension_self: Extension, environment: Environment) -> None:  # noqa: N805
    """Initialize the object.

    Arguments:
        environment: The Jinja environment.
    """
    super().__init__(environment)  # ty:ignore[invalid-super-argument]

    class ContextClass(environment.context_class):  # ty:ignore[unsupported-base]
        def __init__(
            self,
            env: Environment,
            parent: dict[str, Any],
            name: str | None,
            blocks: dict[str, Callable],
            globals: MutableMapping[str, Any] | None = None,  # noqa: A002
        ):
            if extension_self.update is not _sentinel:  # ty:ignore[unresolved-attribute]
                warnings.warn(
                    "The `update` attribute of `ContextHook` subclasses is deprecated. "
                    "The `hook` method should now always modify the `context` in place.",
                    DeprecationWarning,
                    stacklevel=1,
                )
            if "_copier_conf" in parent and (context := extension_self.hook(parent)) is not None:  # ty:ignore[unresolved-attribute]
                parent.update(context)
                warnings.warn(
                    "Returning a dict from the `hook` method is deprecated. "
                    "It should now always modify the `context` in place.",
                    DeprecationWarning,
                    stacklevel=1,
                )

            super().__init__(env, parent, name, blocks, globals)

    environment.context_class = ContextClass

update class-attribute instance-attribute ¤

update: bool = _sentinel

Deprecated attribute to indicate whether the context should be updated with the value returned by hook.

hook ¤

hook(context: dict[str, Any]) -> dict[str, Any] | None

Abstract hook. Does nothing.

Override this method to either return a new context dictionary that will be used to update the original one, or modify the context object in-place.

Parameters:

Raises:

  • NotImplementedError –

    This method must be overridden in a subclass, and instead return either the same context instance modified, or new context instance (dictionary).

Source code in src/copier_template_extensions/_internal/context.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def hook(self, context: dict[str, Any]) -> dict[str, Any] | None:
    """Abstract hook. Does nothing.

    Override this method to either return
    a new context dictionary that will be used
    to update the original one,
    or modify the context object in-place.

    Arguments:
        context: The context to modify.

    Raises:
        NotImplementedError: This method must be overridden in a subclass,
            and instead return either the same context instance modified,
            or new context instance (dictionary).
    """
    raise NotImplementedError

TemplateExtensionLoader ¤

TemplateExtensionLoader(environment: Environment)

Bases: Extension

Extension allowing to load other extensions using relative file paths.

Parameters:

  • environment ¤

    (Environment) –

    The Jinja environment.

Source code in src/copier_template_extensions/_internal/loader.py
40
41
42
43
44
45
46
47
48
def __init__(self, environment: Environment):
    """Initialize the object.

    Arguments:
        environment: The Jinja environment.
    """
    super().__init__(environment)
    # patch jinja's extension loading mechanism
    jinja_env_module.import_string = self._patched_import_string  # ty:ignore[invalid-assignment]