1 # The MIT License (MIT)
3 # Copyright (c) 2015-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.
24 import barectf
.config
as barectf_config
25 import barectf
.template
as barectf_template
26 from typing
import List
, Optional
, Union
30 def _filt_disp_base_int(disp_base
: barectf_config
.DisplayBase
) -> int:
32 barectf_config
.DisplayBase
.BINARY
: 2,
33 barectf_config
.DisplayBase
.OCTAL
: 8,
34 barectf_config
.DisplayBase
.DECIMAL
: 10,
35 barectf_config
.DisplayBase
.HEXADECIMAL
: 16,
39 def _filt_int_ft_str(ft
: barectf_config
._FieldType
) -> str:
40 return _INT_FT_TEMPL
.render(ft
=ft
,
41 is_signed
=isinstance(ft
, barectf_config
.SignedIntegerFieldType
))
44 def _gen_enum_ft(ft
: barectf_config
._FieldType
) -> str:
45 return _ENUM_FT_TEMPL
.render(ft
=ft
)
48 def _gen_real_ft(ft
: barectf_config
._FieldType
) -> str:
49 return _REAL_FT_TEMPL
.render(ft
=ft
)
52 def _gen_str_ft(ft
: barectf_config
._FieldType
) -> str:
53 return _STR_FT_TEMPL
.render(ft
=ft
)
56 def _filt_ft_lengths(ft
: barectf_config
._FieldType
) -> List
[Union
[str, int]]:
57 lengths
: List
[Union
[str, int]] = []
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
)
64 assert type(ft
) is barectf_config
.DynamicArrayFieldType
65 ft
= typing
.cast(barectf_config
.DynamicArrayFieldType
, ft
)
66 lengths
.append(typing
.cast(str, ft
._length
_ft
_member
_name
))
68 ft
= ft
.element_field_type
73 def _filt_deepest_ft(ft
: barectf_config
._FieldType
) -> barectf_config
._FieldType
:
74 while isinstance(ft
, barectf_config
._ArrayFieldType
):
75 ft
= ft
.element_field_type
80 def _gen_struct_ft(ft
: barectf_config
._FieldType
) -> str:
81 return _STRUCT_FT_TEMPL
.render(ft
=ft
)
84 _FT_CLS_TO_GEN_FT_FUNC
= {
85 barectf_config
.UnsignedIntegerFieldType
: _filt_int_ft_str
,
86 barectf_config
.SignedIntegerFieldType
: _filt_int_ft_str
,
87 barectf_config
.UnsignedEnumerationFieldType
: _gen_enum_ft
,
88 barectf_config
.SignedEnumerationFieldType
: _gen_enum_ft
,
89 barectf_config
.RealFieldType
: _gen_real_ft
,
90 barectf_config
.StringFieldType
: _gen_str_ft
,
91 barectf_config
.StructureFieldType
: _gen_struct_ft
,
95 def _filt_ft_str(ft
: barectf_config
._FieldType
) -> str:
96 return _FT_CLS_TO_GEN_FT_FUNC
[type(ft
)](ft
)
100 'disp_base_int': _filt_disp_base_int
,
101 'int_ft_str': _filt_int_ft_str
,
102 'ft_str': _filt_ft_str
,
103 'ft_lengths': _filt_ft_lengths
,
104 'deepest_ft': _filt_deepest_ft
,
108 def _create_template(name
: str, is_file_template
: bool = False,
109 cfg
: Optional
[barectf_config
.Configuration
] = None) -> barectf_template
._Template
:
110 return barectf_template
._Template
(f
'metadata/{name}', is_file_template
, cfg
,
111 typing
.cast(barectf_template
._Filters
, _TEMPL_FILTERS
))
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')
121 def _from_config(cfg
: barectf_config
.Configuration
) -> str:
122 return _create_template('metadata.j2', True, cfg
).render()