template.py: define a `_Template` class instead of two functions
[deliverable/barectf.git] / barectf / tsdl182gen.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2015-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 barectf.config as barectf_config
25 import barectf.template as barectf_template
26 import jinja2
27 from typing import List, Optional
28 import typing
29
30
31 def _filt_bo_str(bo: barectf_config.ByteOrder) -> str:
32 return {
33 barectf_config.ByteOrder.LITTLE_ENDIAN: 'le',
34 barectf_config.ByteOrder.BIG_ENDIAN: 'be',
35 }[bo]
36
37
38 def _filt_disp_base_int(disp_base: barectf_config.DisplayBase) -> int:
39 return {
40 barectf_config.DisplayBase.BINARY: 2,
41 barectf_config.DisplayBase.OCTAL: 8,
42 barectf_config.DisplayBase.DECIMAL: 10,
43 barectf_config.DisplayBase.HEXADECIMAL: 16,
44 }[disp_base]
45
46
47 def _filt_int_ft_str(ft: barectf_config._FieldType) -> str:
48 return _INT_FT_TEMPL.render(ft=ft,
49 is_signed=isinstance(ft, barectf_config.SignedIntegerFieldType))
50
51
52 def _gen_enum_ft(ft: barectf_config._FieldType) -> str:
53 return _ENUM_FT_TEMPL.render(ft=ft)
54
55
56 def _gen_real_ft(ft: barectf_config._FieldType) -> str:
57 return _REAL_FT_TEMPL.render(ft=ft)
58
59
60 def _gen_str_ft(ft: barectf_config._FieldType) -> str:
61 return _STR_FT_TEMPL.render(ft=ft)
62
63
64 def _ft_chain(ft: barectf_config._FieldType) -> List[barectf_config._FieldType]:
65 chain: List[barectf_config._FieldType] = []
66
67 while isinstance(ft, barectf_config.StaticArrayFieldType):
68 chain.append(ft)
69 ft = ft.element_field_type
70
71 chain.append(ft)
72 return chain
73
74
75 def _gen_struct_ft(ft: barectf_config._FieldType) -> str:
76 return _STRUCT_FT_TEMPL.render(ft=ft, ft_chain=_ft_chain)
77
78
79 _FT_CLS_TO_GEN_FT_FUNC = {
80 barectf_config.UnsignedIntegerFieldType: _filt_int_ft_str,
81 barectf_config.SignedIntegerFieldType: _filt_int_ft_str,
82 barectf_config.UnsignedEnumerationFieldType: _gen_enum_ft,
83 barectf_config.SignedEnumerationFieldType: _gen_enum_ft,
84 barectf_config.RealFieldType: _gen_real_ft,
85 barectf_config.StringFieldType: _gen_str_ft,
86 barectf_config.StructureFieldType: _gen_struct_ft,
87 }
88
89
90 def _filt_ft_str(ft: barectf_config._FieldType) -> str:
91 return _FT_CLS_TO_GEN_FT_FUNC[type(ft)](ft)
92
93
94 _TEMPL_FILTERS = {
95 'bo_str': _filt_bo_str,
96 'disp_base_int': _filt_disp_base_int,
97 'int_ft_str': _filt_int_ft_str,
98 'ft_str': _filt_ft_str,
99 }
100
101
102 def _create_template(name: str, is_file_template: bool = False,
103 cfg: Optional[barectf_config.Configuration] = None) -> barectf_template._Template:
104 return barectf_template._Template(name, is_file_template, cfg,
105 typing.cast(barectf_template._Filters, _TEMPL_FILTERS))
106
107
108 _ENUM_FT_TEMPL = _create_template('metadata-enum-ft.j2')
109 _INT_FT_TEMPL = _create_template('metadata-int-ft.j2')
110 _REAL_FT_TEMPL = _create_template('metadata-real-ft.j2')
111 _STR_FT_TEMPL = _create_template('metadata-str-ft.j2')
112 _STRUCT_FT_TEMPL = _create_template('metadata-struct-ft.j2')
113
114
115 def _from_config(cfg: barectf_config.Configuration) -> str:
116 return _create_template('metadata.j2', True, cfg).render()
This page took 0.06716 seconds and 5 git commands to generate.