1 # The MIT License (MIT)
3 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
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
31 def _filt_indent_tab(text
: str, count
: Count
= Count(1), indent_first
: bool = False) -> str:
32 in_lines
= text
.split('\n')
36 for index
, in_line
in enumerate(in_lines
):
37 in_line
= in_line
.rstrip()
39 if len(in_line
) == 0 or (index
== 0 and not indent_first
):
40 out_lines
.append(in_line
)
43 out_lines
.append(f
'{tab * count}{in_line}')
45 return '\n'.join(out_lines
)
48 def _filt_escape_dq(text
: str) -> str:
49 return text
.replace('\\', '\\\\').replace('"', '\\"')
52 _Filter
= Callable
[..., Any
]
53 _Filters
= Mapping
[str, _Filter
]
54 _Test
= Callable
[..., bool]
55 _Tests
= Mapping
[str, _Test
]
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'))
66 'barectf_config': barectf_config
,
67 'barectf_version': barectf_version
,
70 env
.filters
['indent_tab'] = _filt_indent_tab
71 env
.filters
['escape_dq'] = _filt_escape_dq
73 if filters
is not None:
74 env
.filters
.update(filters
)
77 env
.tests
.update(tests
)
79 self
._templ
= env
.get_template(name
)
80 self
._is
_file
_template
= is_file_template
82 def render(self
, **kwargs
) -> str:
83 text
= self
._templ
.render(**kwargs
)
85 if self
._is
_file
_template
:
86 text
= text
.strip() + '\n'
This page took 0.031337 seconds and 4 git commands to generate.