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

1"""The subpackage containing the builtin templates.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from urllib.parse import urlparse 

7 

8from jinja2 import Environment, Template 

9 

10TEMPLATES_PATH = Path(__file__).parent 

11JINJA_ENV = Environment() # noqa: S701 

12 

13 

14def _filter_is_url(value: str) -> bool: 

15 return bool(urlparse(value).scheme) 

16 

17 

18def configure_env(env: Environment) -> None: 

19 """Configure the Jinja environment. 

20 

21 Parameters: 

22 env: The environment to configure. 

23 """ 

24 env.filters.update({"is_url": _filter_is_url}) 

25 

26 

27def get_custom_template(path: str | Path) -> Template: 

28 """Get a custom template instance. 

29 

30 Arguments: 

31 path: Path to the custom template. 

32 

33 Returns: 

34 The Jinja template. 

35 """ 

36 return JINJA_ENV.from_string(Path(path).read_text()) 

37 

38 

39def get_template(name: str) -> Template: 

40 """Get a builtin template instance. 

41 

42 Arguments: 

43 name: The template name. 

44 

45 Returns: 

46 The Jinja template. 

47 """ 

48 return JINJA_ENV.from_string(TEMPLATES_PATH.joinpath(f"{name}.md.jinja").read_text()) 

49 

50 

51configure_env(JINJA_ENV)