Commit | Line | Data |
---|---|---|
de49021e PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining | |
6 | # a copy of this software and associated documentation files (the | |
7 | # "Software"), to deal in the Software without restriction, including | |
8 | # without limitation the rights to use, copy, modify, merge, publish, | |
9 | # distribute, sublicense, and/or sell copies of the Software, and to | |
10 | # permit persons to whom the Software is furnished to do so, subject to | |
11 | # the following conditions: | |
12 | # | |
13 | # The above copyright notice and this permission notice shall be | |
14 | # included in all copies or substantial portions of the Software. | |
15 | # | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
19 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
20 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
21 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
22 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
23 | ||
24 | import jinja2 | |
25 | import barectf.config as barectf_config | |
26 | import barectf.version as barectf_version | |
27 | from barectf.typing import Count | |
28 | from typing import Callable, Optional, Any, Mapping | |
29 | ||
30 | ||
31 | def _filt_indent_tab(text: str, count: Count = Count(1), indent_first: bool = False) -> str: | |
32 | in_lines = text.split('\n') | |
33 | out_lines = [] | |
34 | tab = '\t' | |
35 | ||
36 | for index, in_line in enumerate(in_lines): | |
37 | in_line = in_line.rstrip() | |
38 | ||
39 | if len(in_line) == 0 or (index == 0 and not indent_first): | |
40 | out_lines.append(in_line) | |
41 | continue | |
42 | ||
43 | out_lines.append(f'{tab * count}{in_line}') | |
44 | ||
45 | return '\n'.join(out_lines) | |
46 | ||
47 | ||
48 | def _filt_escape_dq(text: str) -> str: | |
49 | return text.replace('\\', '\\\\').replace('"', '\\"') | |
50 | ||
51 | ||
1624d186 | 52 | _Filter = Callable[..., Any] |
de49021e | 53 | _Filters = Mapping[str, _Filter] |
1624d186 | 54 | _Test = Callable[..., bool] |
de49021e PP |
55 | _Tests = Mapping[str, _Test] |
56 | ||
57 | ||
e902a26f PP |
58 | class _Template: |
59 | def __init__(self, name: str, is_file_template: bool = False, | |
60 | cfg: Optional[barectf_config.Configuration] = None, | |
61 | filters: Optional[_Filters] = None, tests: Optional[_Tests] = None): | |
62 | env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True, | |
63 | loader=jinja2.PackageLoader('barectf', 'templates')) | |
64 | env.globals.update({ | |
65 | 'cfg': cfg, | |
66 | 'barectf_config': barectf_config, | |
67 | 'barectf_version': barectf_version, | |
68 | }) | |
de49021e | 69 | |
e902a26f PP |
70 | env.filters['indent_tab'] = _filt_indent_tab |
71 | env.filters['escape_dq'] = _filt_escape_dq | |
de49021e | 72 | |
e902a26f PP |
73 | if filters is not None: |
74 | env.filters.update(filters) | |
de49021e | 75 | |
e902a26f PP |
76 | if tests is not None: |
77 | env.tests.update(tests) | |
de49021e | 78 | |
e902a26f PP |
79 | self._templ = env.get_template(name) |
80 | self._is_file_template = is_file_template | |
de49021e | 81 | |
e902a26f PP |
82 | def render(self, **kwargs) -> str: |
83 | text = self._templ.render(**kwargs) | |
de49021e | 84 | |
e902a26f PP |
85 | if self._is_file_template: |
86 | text = text.strip() + '\n' | |
de49021e | 87 | |
e902a26f | 88 | return text |