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
30 def _filt_bo_str(bo
: barectf_config
.ByteOrder
) -> str:
32 barectf_config
.ByteOrder
.LITTLE_ENDIAN
: 'le',
33 barectf_config
.ByteOrder
.BIG_ENDIAN
: 'be',
37 def _filt_disp_base_int(disp_base
: barectf_config
.DisplayBase
) -> int:
39 barectf_config
.DisplayBase
.BINARY
: 2,
40 barectf_config
.DisplayBase
.OCTAL
: 8,
41 barectf_config
.DisplayBase
.DECIMAL
: 10,
42 barectf_config
.DisplayBase
.HEXADECIMAL
: 16,
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
))
51 def _gen_enum_ft(ft
: barectf_config
._FieldType
) -> str:
52 return _ENUM_FT_TEMPL
.render(ft
=ft
)
55 def _gen_real_ft(ft
: barectf_config
._FieldType
) -> str:
56 return _REAL_FT_TEMPL
.render(ft
=ft
)
59 def _gen_str_ft(ft
: barectf_config
._FieldType
) -> str:
60 return _STR_FT_TEMPL
.render(ft
=ft
)
63 def _ft_chain(ft
: barectf_config
._FieldType
) -> List
[barectf_config
._FieldType
]:
64 chain
: List
[barectf_config
._FieldType
] = []
66 while isinstance(ft
, barectf_config
.StaticArrayFieldType
):
68 ft
= ft
.element_field_type
74 def _gen_struct_ft(ft
: barectf_config
._FieldType
) -> str:
75 return _STRUCT_FT_TEMPL
.render(ft
=ft
, ft_chain
=_ft_chain
)
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
,
89 def _filt_ft_str(ft
: barectf_config
._FieldType
) -> str:
90 return _FT_CLS_TO_GEN_FT_FUNC
[type(ft
)](ft
)
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
,
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
(name
, is_file_template
, cfg
,
104 typing
.cast(barectf_template
._Filters
, _TEMPL_FILTERS
))
107 _ENUM_FT_TEMPL
= _create_template('metadata-enum-ft.j2')
108 _INT_FT_TEMPL
= _create_template('metadata-int-ft.j2')
109 _REAL_FT_TEMPL
= _create_template('metadata-real-ft.j2')
110 _STR_FT_TEMPL
= _create_template('metadata-str-ft.j2')
111 _STRUCT_FT_TEMPL
= _create_template('metadata-struct-ft.j2')
114 def _from_config(cfg
: barectf_config
.Configuration
) -> str:
115 return _create_template('metadata.j2', True, cfg
).render()