Coverage for src/git_changelog/templates/__init__.py: 93.33%
15 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-02 00:26 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-02 00:26 +0200
1"""The subpackage containing the builtin templates."""
3from __future__ import annotations
5from pathlib import Path
6from urllib.parse import urlparse
8from jinja2 import Environment, Template
10TEMPLATES_PATH = Path(__file__).parent
11JINJA_ENV = Environment() # noqa: S701
14def _filter_is_url(value: str) -> bool:
15 return bool(urlparse(value).scheme)
18def configure_env(env: Environment) -> None:
19 """Configure the Jinja environment.
21 Parameters:
22 env: The environment to configure.
23 """
24 env.filters.update({"is_url": _filter_is_url})
27def get_custom_template(path: str | Path) -> Template:
28 """Get a custom template instance.
30 Arguments:
31 path: Path to the custom template.
33 Returns:
34 The Jinja template.
35 """
36 return JINJA_ENV.from_string(Path(path).read_text())
39def get_template(name: str) -> Template:
40 """Get a builtin template instance.
42 Arguments:
43 name: The template name.
45 Returns:
46 The Jinja template.
47 """
48 return JINJA_ENV.from_string(TEMPLATES_PATH.joinpath(f"{name}.md.jinja").read_text())
51configure_env(JINJA_ENV)