Skip to content

Commit 52cfa3a

Browse files
DaRaschFizzadar
authored andcommitted
ops.files.template: allow custom jinja2 template loaders
1 parent f8557e4 commit 52cfa3a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/pyinfra/api/util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import IO, TYPE_CHECKING, Any, Callable, Dict, List, Optional, Type, Union
1111

1212
import click
13-
from jinja2 import Environment, FileSystemLoader, StrictUndefined
13+
from jinja2 import Environment, FileSystemLoader, StrictUndefined, Template
1414
from paramiko import SSHException
1515
from typeguard import TypeCheckError, check_type
1616

@@ -26,7 +26,7 @@
2626
BLOCKSIZE = 65536
2727

2828
# Caches
29-
TEMPLATES: Dict[Any, Any] = {}
29+
TEMPLATES: Dict[str, Template] = {}
3030
FILE_SHAS: Dict[Any, Any] = {}
3131

3232
PYINFRA_INSTALL_DIR = path.normpath(path.join(path.dirname(__file__), ".."))
@@ -139,7 +139,9 @@ def get_operation_order_from_stack(state: "State"):
139139
return line_numbers
140140

141141

142-
def get_template(filename_or_io: str | IO, jinja_env_kwargs: dict[str, Any] | None = None):
142+
def get_template(
143+
filename_or_io: str | IO, jinja_env_kwargs: dict[str, Any] | None = None
144+
) -> Template:
143145
"""
144146
Gets a jinja2 ``Template`` object for the input filename or string, with caching
145147
based on the filename of the template, or the SHA1 of the input string.
@@ -155,10 +157,11 @@ def get_template(filename_or_io: str | IO, jinja_env_kwargs: dict[str, Any] | No
155157
with file_data as file_io:
156158
template_string = file_io.read()
157159

160+
default_loader = FileSystemLoader(getcwd())
158161
template = Environment(
159162
undefined=StrictUndefined,
160163
keep_trailing_newline=True,
161-
loader=FileSystemLoader(getcwd()),
164+
loader=jinja_env_kwargs.pop("loader", default_loader),
162165
**jinja_env_kwargs,
163166
).from_string(template_string)
164167

src/pyinfra/operations/files.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,14 @@ def template(
11561156
if not set explicitly.
11571157
11581158
Notes:
1159-
Common convention is to store templates in a "templates" directory and
1160-
have a filename suffix with '.j2' (for jinja2).
1159+
Common convention is to store templates in a "templates" directory and
1160+
have a filename suffix with '.j2' (for jinja2).
11611161
1162-
For information on the template syntax, see
1163-
`the jinja2 docs <https://jinja.palletsprojects.com>`_.
1162+
The default template lookup directory (used with jinjas ``extends``, ``import`` and
1163+
``include`` statements) is the current working directory.
1164+
1165+
For information on the template syntax, see
1166+
`the jinja2 docs <https://jinja.palletsprojects.com>`_.
11641167
11651168
**Examples:**
11661169

0 commit comments

Comments
 (0)