Add dynamic array tracing tests
[deliverable/barectf.git] / barectf / template.py
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
52 _Filter = Callable[..., Any]
53 _Filters = Mapping[str, _Filter]
54 _Test = Callable[..., bool]
55 _Tests = Mapping[str, _Test]
56
57
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 })
69
70 env.filters['indent_tab'] = _filt_indent_tab
71 env.filters['escape_dq'] = _filt_escape_dq
72
73 if filters is not None:
74 env.filters.update(filters)
75
76 if tests is not None:
77 env.tests.update(tests)
78
79 self._templ = env.get_template(name)
80 self._is_file_template = is_file_template
81
82 def render(self, **kwargs) -> str:
83 text = self._templ.render(**kwargs)
84
85 if self._is_file_template:
86 text = text.strip() + '\n'
87
88 return text
This page took 0.031337 seconds and 4 git commands to generate.