Commit | Line | Data |
---|---|---|
e5aa0be3 PP |
1 | # The MIT License (MIT) |
2 | # | |
4a90140d | 3 | # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com> |
e5aa0be3 | 4 | # |
1378f213 PP |
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: | |
e5aa0be3 | 12 | # |
1378f213 PP |
13 | # The above copyright notice and this permission notice shall be |
14 | # included in all copies or substantial portions of the Software. | |
e5aa0be3 | 15 | # |
1378f213 PP |
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. | |
e5aa0be3 | 23 | |
4810b707 | 24 | import barectf.config as barectf_config |
de49021e | 25 | import barectf.template as barectf_template |
be9f12dc | 26 | from typing import List, Optional, Union |
de49021e | 27 | import typing |
e5aa0be3 PP |
28 | |
29 | ||
de49021e PP |
30 | def _filt_disp_base_int(disp_base: barectf_config.DisplayBase) -> int: |
31 | return { | |
32 | barectf_config.DisplayBase.BINARY: 2, | |
33 | barectf_config.DisplayBase.OCTAL: 8, | |
34 | barectf_config.DisplayBase.DECIMAL: 10, | |
35 | barectf_config.DisplayBase.HEXADECIMAL: 16, | |
36 | }[disp_base] | |
e5aa0be3 | 37 | |
4810b707 | 38 | |
de49021e | 39 | def _filt_int_ft_str(ft: barectf_config._FieldType) -> str: |
e902a26f PP |
40 | return _INT_FT_TEMPL.render(ft=ft, |
41 | is_signed=isinstance(ft, barectf_config.SignedIntegerFieldType)) | |
4810b707 | 42 | |
4810b707 | 43 | |
de49021e | 44 | def _gen_enum_ft(ft: barectf_config._FieldType) -> str: |
e902a26f | 45 | return _ENUM_FT_TEMPL.render(ft=ft) |
4810b707 | 46 | |
e5aa0be3 | 47 | |
de49021e | 48 | def _gen_real_ft(ft: barectf_config._FieldType) -> str: |
e902a26f | 49 | return _REAL_FT_TEMPL.render(ft=ft) |
e5aa0be3 | 50 | |
e5aa0be3 | 51 | |
de49021e | 52 | def _gen_str_ft(ft: barectf_config._FieldType) -> str: |
e902a26f | 53 | return _STR_FT_TEMPL.render(ft=ft) |
e5aa0be3 PP |
54 | |
55 | ||
67ec4028 | 56 | def _filt_ft_lengths(ft: barectf_config._FieldType) -> List[Union[str, int]]: |
be9f12dc | 57 | lengths: List[Union[str, int]] = [] |
e5aa0be3 | 58 | |
be9f12dc PP |
59 | while isinstance(ft, barectf_config._ArrayFieldType): |
60 | if type(ft) is barectf_config.StaticArrayFieldType: | |
61 | ft = typing.cast(barectf_config.StaticArrayFieldType, ft) | |
62 | lengths.append(ft.length) | |
63 | else: | |
64 | assert type(ft) is barectf_config.DynamicArrayFieldType | |
65 | ft = typing.cast(barectf_config.DynamicArrayFieldType, ft) | |
67ec4028 | 66 | lengths.append(typing.cast(str, ft._length_ft_member_name)) |
be9f12dc PP |
67 | |
68 | ft = ft.element_field_type | |
69 | ||
70 | return lengths | |
71 | ||
72 | ||
73 | def _filt_deepest_ft(ft: barectf_config._FieldType) -> barectf_config._FieldType: | |
74 | while isinstance(ft, barectf_config._ArrayFieldType): | |
de49021e | 75 | ft = ft.element_field_type |
e5aa0be3 | 76 | |
be9f12dc | 77 | return ft |
e5aa0be3 | 78 | |
e5aa0be3 | 79 | |
de49021e | 80 | def _gen_struct_ft(ft: barectf_config._FieldType) -> str: |
be9f12dc | 81 | return _STRUCT_FT_TEMPL.render(ft=ft) |
e5aa0be3 | 82 | |
e5aa0be3 | 83 | |
de49021e PP |
84 | _FT_CLS_TO_GEN_FT_FUNC = { |
85 | barectf_config.UnsignedIntegerFieldType: _filt_int_ft_str, | |
86 | barectf_config.SignedIntegerFieldType: _filt_int_ft_str, | |
4810b707 PP |
87 | barectf_config.UnsignedEnumerationFieldType: _gen_enum_ft, |
88 | barectf_config.SignedEnumerationFieldType: _gen_enum_ft, | |
89 | barectf_config.RealFieldType: _gen_real_ft, | |
de49021e | 90 | barectf_config.StringFieldType: _gen_str_ft, |
4810b707 | 91 | barectf_config.StructureFieldType: _gen_struct_ft, |
e5aa0be3 PP |
92 | } |
93 | ||
94 | ||
de49021e PP |
95 | def _filt_ft_str(ft: barectf_config._FieldType) -> str: |
96 | return _FT_CLS_TO_GEN_FT_FUNC[type(ft)](ft) | |
e5aa0be3 | 97 | |
e5aa0be3 | 98 | |
de49021e | 99 | _TEMPL_FILTERS = { |
de49021e PP |
100 | 'disp_base_int': _filt_disp_base_int, |
101 | 'int_ft_str': _filt_int_ft_str, | |
102 | 'ft_str': _filt_ft_str, | |
be9f12dc PP |
103 | 'ft_lengths': _filt_ft_lengths, |
104 | 'deepest_ft': _filt_deepest_ft, | |
de49021e | 105 | } |
e5aa0be3 | 106 | |
e5aa0be3 | 107 | |
e902a26f PP |
108 | def _create_template(name: str, is_file_template: bool = False, |
109 | cfg: Optional[barectf_config.Configuration] = None) -> barectf_template._Template: | |
d6483c83 | 110 | return barectf_template._Template(f'metadata/{name}', is_file_template, cfg, |
e902a26f | 111 | typing.cast(barectf_template._Filters, _TEMPL_FILTERS)) |
e5aa0be3 | 112 | |
e5aa0be3 | 113 | |
d6483c83 PP |
114 | _ENUM_FT_TEMPL = _create_template('enum-ft.j2') |
115 | _INT_FT_TEMPL = _create_template('int-ft.j2') | |
116 | _REAL_FT_TEMPL = _create_template('real-ft.j2') | |
117 | _STR_FT_TEMPL = _create_template('str-ft.j2') | |
118 | _STRUCT_FT_TEMPL = _create_template('struct-ft.j2') | |
e5aa0be3 | 119 | |
e5aa0be3 | 120 | |
de49021e | 121 | def _from_config(cfg: barectf_config.Configuration) -> str: |
e902a26f | 122 | return _create_template('metadata.j2', True, cfg).render() |