-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from Perfexionists/jinja-fix
Fix jinja2 issue with editable build
- Loading branch information
Showing
10 changed files
with
85 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Functions for working with templates | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
# Standard Imports | ||
from pathlib import Path | ||
from typing import Callable, Any, Optional, Mapping | ||
|
||
# Third-Party Imports | ||
from jinja2 import Environment, FileSystemLoader, Template | ||
|
||
# Perun Imports | ||
|
||
|
||
def get_template( | ||
template_name: str, filters: Optional[Mapping[str, Callable[[str], str]]] = None | ||
) -> Template: | ||
"""Loads jinja2 template from the templates directory | ||
Note: there are some issues with using PackageLoader of Jinja2 in combination | ||
with meson.build; when using the editable install (make dev) it seems that it | ||
nondeterministically sets the wrong path resulting into errors. It works fine | ||
for classic installation (make install). | ||
Hence, we wrapped it in FileSystemLoader with absolute paths. | ||
:return: loaded template from perun/templates directory | ||
""" | ||
# Note: we keep the autoescape=false, since we kindof believe we are not trying to fuck us up | ||
path = Path(__file__).parent | ||
env = Environment(loader=FileSystemLoader(path)) | ||
for filter_name, filter_func in (filters or {}).items(): | ||
env.filters[filter_name] = filter_func | ||
return env.get_template(template_name) | ||
|
||
|
||
def get_environment(**kwargs: Any) -> Environment: | ||
"""Returns Jinja2 environment for working with template | ||
:return: jinja environment | ||
""" | ||
path = Path(__file__).parent | ||
return Environment(loader=FileSystemLoader(path), **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters