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