Add dynamic array tracing tests
[deliverable/barectf.git] / barectf / tsdl182gen.py
index c11dac27869381eb8fd304ab910210380c81f7ac..5de785d3522a0e036ea5647a24f91fa65379a730 100644 (file)
 
 import barectf.config as barectf_config
 import barectf.template as barectf_template
-from typing import List, Optional
+from typing import List, Optional, Union
 import typing
 
 
-def _filt_bo_str(bo: barectf_config.ByteOrder) -> str:
-    return {
-        barectf_config.ByteOrder.LITTLE_ENDIAN: 'le',
-        barectf_config.ByteOrder.BIG_ENDIAN: 'be',
-    }[bo]
-
-
 def _filt_disp_base_int(disp_base: barectf_config.DisplayBase) -> int:
     return {
         barectf_config.DisplayBase.BINARY: 2,
@@ -60,19 +53,32 @@ def _gen_str_ft(ft: barectf_config._FieldType) -> str:
     return _STR_FT_TEMPL.render(ft=ft)
 
 
-def _ft_chain(ft: barectf_config._FieldType) -> List[barectf_config._FieldType]:
-    chain: List[barectf_config._FieldType] = []
+def _filt_ft_lengths(ft: barectf_config._FieldType) -> List[Union[str, int]]:
+    lengths: List[Union[str, int]] = []
+
+    while isinstance(ft, barectf_config._ArrayFieldType):
+        if type(ft) is barectf_config.StaticArrayFieldType:
+            ft = typing.cast(barectf_config.StaticArrayFieldType, ft)
+            lengths.append(ft.length)
+        else:
+            assert type(ft) is barectf_config.DynamicArrayFieldType
+            ft = typing.cast(barectf_config.DynamicArrayFieldType, ft)
+            lengths.append(typing.cast(str, ft._length_ft_member_name))
+
+        ft = ft.element_field_type
+
+    return lengths
+
 
-    while isinstance(ft, barectf_config.StaticArrayFieldType):
-        chain.append(ft)
+def _filt_deepest_ft(ft: barectf_config._FieldType) -> barectf_config._FieldType:
+    while isinstance(ft, barectf_config._ArrayFieldType):
         ft = ft.element_field_type
 
-    chain.append(ft)
-    return chain
+    return ft
 
 
 def _gen_struct_ft(ft: barectf_config._FieldType) -> str:
-    return _STRUCT_FT_TEMPL.render(ft=ft, ft_chain=_ft_chain)
+    return _STRUCT_FT_TEMPL.render(ft=ft)
 
 
 _FT_CLS_TO_GEN_FT_FUNC = {
@@ -91,24 +97,25 @@ def _filt_ft_str(ft: barectf_config._FieldType) -> str:
 
 
 _TEMPL_FILTERS = {
-    'bo_str': _filt_bo_str,
     'disp_base_int': _filt_disp_base_int,
     'int_ft_str': _filt_int_ft_str,
     'ft_str': _filt_ft_str,
+    'ft_lengths': _filt_ft_lengths,
+    'deepest_ft': _filt_deepest_ft,
 }
 
 
 def _create_template(name: str, is_file_template: bool = False,
                      cfg: Optional[barectf_config.Configuration] = None) -> barectf_template._Template:
-    return barectf_template._Template(name, is_file_template, cfg,
+    return barectf_template._Template(f'metadata/{name}', is_file_template, cfg,
                                       typing.cast(barectf_template._Filters, _TEMPL_FILTERS))
 
 
-_ENUM_FT_TEMPL = _create_template('metadata-enum-ft.j2')
-_INT_FT_TEMPL = _create_template('metadata-int-ft.j2')
-_REAL_FT_TEMPL = _create_template('metadata-real-ft.j2')
-_STR_FT_TEMPL = _create_template('metadata-str-ft.j2')
-_STRUCT_FT_TEMPL = _create_template('metadata-struct-ft.j2')
+_ENUM_FT_TEMPL = _create_template('enum-ft.j2')
+_INT_FT_TEMPL = _create_template('int-ft.j2')
+_REAL_FT_TEMPL = _create_template('real-ft.j2')
+_STR_FT_TEMPL = _create_template('str-ft.j2')
+_STRUCT_FT_TEMPL = _create_template('struct-ft.j2')
 
 
 def _from_config(cfg: barectf_config.Configuration) -> str:
This page took 0.032836 seconds and 4 git commands to generate.