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
.version
as barectf_version
25 from typing
import Optional
, Any
, FrozenSet
, Mapping
, Iterator
, Set
, Union
, Callable
27 from barectf
.typing
import Count
, Alignment
, _OptStr
, Id
28 import collections
.abc
36 class ByteOrder(enum
.Enum
):
43 def alignment(self
) -> Alignment
:
44 raise NotImplementedError
47 def size_is_dynamic(self
):
51 class _BitArrayFieldType(_FieldType
):
52 def __init__(self
, size
: Count
, alignment
: Alignment
= Alignment(1)):
54 self
._alignment
= alignment
57 def size(self
) -> Count
:
61 def alignment(self
) -> Alignment
:
62 return self
._alignment
65 class DisplayBase(enum
.Enum
):
72 class _IntegerFieldType(_BitArrayFieldType
):
73 def __init__(self
, size
: Count
, alignment
: Optional
[Alignment
] = None,
74 preferred_display_base
: DisplayBase
= DisplayBase
.DECIMAL
):
76 alignment
= Alignment(8 if size
% 8 == 0 else 1)
78 super().__init
__(size
, alignment
)
79 self
._preferred
_display
_base
= preferred_display_base
82 def preferred_display_base(self
) -> DisplayBase
:
83 return self
._preferred
_display
_base
86 class UnsignedIntegerFieldType(_IntegerFieldType
):
87 def __init__(self
, *args
, **kwargs
):
88 super().__init
__(*args
, **kwargs
)
89 self
._mapped
_clk
_type
_name
= None
93 class SignedIntegerFieldType(_IntegerFieldType
):
97 class EnumerationFieldTypeMappingRange
:
98 def __init__(self
, lower
: int, upper
: int):
103 def lower(self
) -> int:
107 def upper(self
) -> int:
110 def __eq__(self
, other
: Any
) -> bool:
111 if type(other
) is not type(self
):
114 return (self
._lower
, self
._upper
) == (other
._lower
, other
._upper
)
116 def __hash__(self
) -> int:
117 return hash((self
._lower
, self
._upper
))
119 def contains(self
, value
: int) -> bool:
120 return self
._lower
<= value
<= self
._upper
123 class EnumerationFieldTypeMapping
:
124 def __init__(self
, ranges
: Set
[EnumerationFieldTypeMappingRange
]):
125 self
._ranges
= frozenset(ranges
)
128 def ranges(self
) -> FrozenSet
[EnumerationFieldTypeMappingRange
]:
131 def ranges_contain_value(self
, value
: int) -> bool:
132 return any([rg
.contains(value
) for rg
in self
._ranges
])
135 _EnumFtMappings
= Mapping
[str, EnumerationFieldTypeMapping
]
138 class EnumerationFieldTypeMappings(collections
.abc
.Mapping
):
139 def __init__(self
, mappings
: _EnumFtMappings
):
140 self
._mappings
= {label
: mapping
for label
, mapping
in mappings
.items()}
142 def __getitem__(self
, key
: str) -> EnumerationFieldTypeMapping
:
143 return self
._mappings
[key
]
145 def __iter__(self
) -> Iterator
[str]:
146 return iter(self
._mappings
)
148 def __len__(self
) -> int:
149 return len(self
._mappings
)
152 class _EnumerationFieldType(_IntegerFieldType
):
153 def __init__(self
, size
: Count
, alignment
: Optional
[Alignment
] = None,
154 preferred_display_base
: DisplayBase
= DisplayBase
.DECIMAL
,
155 mappings
: Optional
[_EnumFtMappings
] = None):
156 super().__init
__(size
, alignment
, preferred_display_base
)
157 self
._mappings
= EnumerationFieldTypeMappings({})
159 if mappings
is not None:
160 self
._mappings
= EnumerationFieldTypeMappings(mappings
)
163 def mappings(self
) -> EnumerationFieldTypeMappings
:
164 return self
._mappings
166 def labels_for_value(self
, value
: int) -> Set
[str]:
169 for label
, mapping
in self
._mappings
.items():
170 if mapping
.ranges_contain_value(value
):
176 class UnsignedEnumerationFieldType(_EnumerationFieldType
, UnsignedIntegerFieldType
):
180 class SignedEnumerationFieldType(_EnumerationFieldType
, SignedIntegerFieldType
):
184 class RealFieldType(_BitArrayFieldType
):
188 class StringFieldType(_FieldType
):
190 def alignment(self
) -> Alignment
:
194 def size_is_dynamic(self
):
198 class _ArrayFieldType(_FieldType
):
199 def __init__(self
, element_field_type
: _FieldType
):
200 self
._element
_field
_type
= element_field_type
203 def element_field_type(self
) -> _FieldType
:
204 return self
._element
_field
_type
207 def alignment(self
) -> Alignment
:
208 return self
._element
_field
_type
.alignment
211 def size_is_dynamic(self
):
212 return self
._element
_field
_type
.size_is_dynamic
215 class StaticArrayFieldType(_ArrayFieldType
):
216 def __init__(self
, length
: Count
, element_field_type
: _FieldType
):
217 super().__init
__(element_field_type
)
218 self
._length
= length
221 def length(self
) -> Count
:
225 class StructureFieldTypeMember
:
226 def __init__(self
, field_type
: _FieldType
):
227 self
._field
_type
= field_type
230 def field_type(self
) -> _FieldType
:
231 return self
._field
_type
234 _StructFtMembers
= Mapping
[str, StructureFieldTypeMember
]
237 class StructureFieldTypeMembers(collections
.abc
.Mapping
):
238 def __init__(self
, members
: _StructFtMembers
):
239 self
._members
= collections
.OrderedDict()
241 for name
, member
in members
.items():
242 assert type(member
) is StructureFieldTypeMember
243 self
._members
[name
] = member
245 def __getitem__(self
, key
: str) -> StructureFieldTypeMember
:
246 return self
._members
[key
]
248 def __iter__(self
) -> Iterator
[str]:
249 return iter(self
._members
)
251 def __len__(self
) -> int:
252 return len(self
._members
)
255 class StructureFieldType(_FieldType
):
256 def __init__(self
, minimum_alignment
: Alignment
= Alignment(1),
257 members
: Optional
[_StructFtMembers
] = None):
258 self
._minimum
_alignment
= minimum_alignment
259 self
._members
= StructureFieldTypeMembers({})
261 if members
is not None:
262 self
._members
= StructureFieldTypeMembers(members
)
264 self
._set
_alignment
()
266 def _set_alignment(self
):
267 self
._alignment
: Alignment
= self
._minimum
_alignment
269 for member
in self
._members
.values():
270 if member
.field_type
.alignment
> self
._alignment
:
271 self
._alignment
= member
.field_type
.alignment
274 def minimum_alignment(self
) -> Alignment
:
275 return self
._minimum
_alignment
278 def alignment(self
) -> Alignment
:
279 return self
._alignment
282 def size_is_dynamic(self
):
283 return any([member
.field_type
.size_is_dynamic
for member
in self
.members
.values()])
286 def members(self
) -> StructureFieldTypeMembers
:
293 def __eq__(self
, other
: Any
) -> bool:
294 if type(other
) is not type(self
):
297 return self
._name
== other
._name
299 def __lt__(self
, other
: '_UniqueByName'):
300 assert type(self
) is type(other
)
301 return self
._name
< other
._name
303 def __hash__(self
) -> int:
304 return hash(self
._name
)
307 _OptFt
= Optional
[_FieldType
]
308 _OptStructFt
= Optional
[StructureFieldType
]
309 LogLevel
= typing
.NewType('LogLevel', int)
312 class EventType(_UniqueByName
):
313 def __init__(self
, name
: str, log_level
: Optional
[LogLevel
] = None,
314 specific_context_field_type
: _OptStructFt
= None, payload_field_type
: _OptStructFt
= None):
315 self
._id
: Optional
[Id
] = None
317 self
._log
_level
= log_level
318 self
._specific
_context
_field
_type
= specific_context_field_type
319 self
._payload
_field
_type
= payload_field_type
322 def id(self
) -> Optional
[Id
]:
326 def name(self
) -> str:
330 def log_level(self
) -> Optional
[LogLevel
]:
331 return self
._log
_level
334 def specific_context_field_type(self
) -> _OptStructFt
:
335 return self
._specific
_context
_field
_type
338 def payload_field_type(self
) -> _OptStructFt
:
339 return self
._payload
_field
_type
342 class ClockTypeOffset
:
343 def __init__(self
, seconds
: int = 0, cycles
: Count
= Count(0)):
344 self
._seconds
= seconds
345 self
._cycles
= cycles
348 def seconds(self
) -> int:
352 def cycles(self
) -> Count
:
356 _OptUuid
= Optional
[uuidp
.UUID
]
359 class ClockType(_UniqueByName
):
360 def __init__(self
, name
: str, frequency
: Count
= Count(int(1e9
)), uuid
: _OptUuid
= None,
361 description
: _OptStr
= None, precision
: Count
= Count(0),
362 offset
: Optional
[ClockTypeOffset
] = None, origin_is_unix_epoch
: bool = False):
364 self
._frequency
= frequency
366 self
._description
= description
367 self
._precision
= precision
368 self
._offset
= ClockTypeOffset()
370 if offset
is not None:
371 self
._offset
= offset
373 self
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
376 def name(self
) -> str:
380 def frequency(self
) -> Count
:
381 return self
._frequency
384 def uuid(self
) -> _OptUuid
:
388 def description(self
) -> _OptStr
:
389 return self
._description
392 def precision(self
) -> Count
:
393 return self
._precision
396 def offset(self
) -> ClockTypeOffset
:
400 def origin_is_unix_epoch(self
) -> bool:
401 return self
._origin
_is
_unix
_epoch
404 DEFAULT_FIELD_TYPE
= 'default'
405 _DefaultableUIntFt
= Union
[str, UnsignedIntegerFieldType
]
406 _OptDefaultableUIntFt
= Optional
[_DefaultableUIntFt
]
407 _OptUIntFt
= Optional
[UnsignedIntegerFieldType
]
410 class StreamTypePacketFeatures
:
411 def __init__(self
, total_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
412 content_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
413 beginning_time_field_type
: _OptDefaultableUIntFt
= None,
414 end_time_field_type
: _OptDefaultableUIntFt
= None,
415 discarded_events_counter_field_type
: _OptDefaultableUIntFt
= None):
416 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
417 if user_ft
== DEFAULT_FIELD_TYPE
:
418 return UnsignedIntegerFieldType(64)
420 return typing
.cast(_OptUIntFt
, user_ft
)
422 self
._total
_size
_field
_type
= get_ft(total_size_field_type
)
423 self
._content
_size
_field
_type
= get_ft(content_size_field_type
)
424 self
._beginning
_time
_field
_type
= get_ft(beginning_time_field_type
)
425 self
._end
_time
_field
_type
= get_ft(end_time_field_type
)
426 self
._discarded
_events
_counter
_field
_type
= get_ft(discarded_events_counter_field_type
)
429 def total_size_field_type(self
) -> _OptUIntFt
:
430 return self
._total
_size
_field
_type
433 def content_size_field_type(self
) -> _OptUIntFt
:
434 return self
._content
_size
_field
_type
437 def beginning_time_field_type(self
) -> _OptUIntFt
:
438 return self
._beginning
_time
_field
_type
441 def end_time_field_type(self
) -> _OptUIntFt
:
442 return self
._end
_time
_field
_type
445 def discarded_events_counter_field_type(self
) -> _OptUIntFt
:
446 return self
._discarded
_events
_counter
_field
_type
449 class StreamTypeEventFeatures
:
450 def __init__(self
, type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
451 time_field_type
: _OptDefaultableUIntFt
= None):
452 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
453 if user_ft
== DEFAULT_FIELD_TYPE
:
454 return UnsignedIntegerFieldType(64)
456 return typing
.cast(_OptUIntFt
, user_ft
)
458 self
._type
_id
_field
_type
= get_ft(type_id_field_type
)
459 self
._time
_field
_type
= get_ft(time_field_type
)
462 def type_id_field_type(self
) -> _OptUIntFt
:
463 return self
._type
_id
_field
_type
466 def time_field_type(self
) -> _OptUIntFt
:
467 return self
._time
_field
_type
470 class StreamTypeFeatures
:
471 def __init__(self
, packet_features
: Optional
[StreamTypePacketFeatures
] = None,
472 event_features
: Optional
[StreamTypeEventFeatures
] = None):
473 if packet_features
is None:
474 self
._packet
_features
= StreamTypePacketFeatures()
476 self
._packet
_features
= packet_features
478 if event_features
is None:
479 self
._event
_features
= StreamTypeEventFeatures()
481 self
._event
_features
= event_features
484 def packet_features(self
) -> StreamTypePacketFeatures
:
485 return self
._packet
_features
488 def event_features(self
) -> StreamTypeEventFeatures
:
489 return self
._event
_features
492 class StreamType(_UniqueByName
):
493 def __init__(self
, name
: str, event_types
: Set
[EventType
],
494 default_clock_type
: Optional
[ClockType
] = None,
495 features
: Optional
[StreamTypeFeatures
] = None,
496 packet_context_field_type_extra_members
: Optional
[_StructFtMembers
] = None,
497 event_common_context_field_type
: _OptStructFt
= None):
498 self
._id
: Optional
[Id
] = None
500 self
._default
_clock
_type
= default_clock_type
501 self
._event
_common
_context
_field
_type
= event_common_context_field_type
502 self
._event
_types
= frozenset(event_types
)
505 for index
, ev_type
in enumerate(sorted(self
._event
_types
, key
=lambda evt
: evt
.name
)):
506 assert ev_type
._id
is None
507 ev_type
._id
= Id(index
)
509 self
._set
_features
(features
)
510 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers({})
512 if packet_context_field_type_extra_members
is not None:
513 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers(packet_context_field_type_extra_members
)
515 self
._set
_pkt
_ctx
_ft
()
516 self
._set
_ev
_header
_ft
()
518 def _set_features(self
, features
: Optional
[StreamTypeFeatures
]):
519 if features
is not None:
520 self
._features
= features
524 pkt_beginning_time_ft
= None
525 pkt_end_time_ft
= None
527 if self
._default
_clock
_type
is not None:
528 # Automatic time field types because the stream type has a
529 # default clock type.
530 ev_time_ft
= DEFAULT_FIELD_TYPE
531 pkt_beginning_time_ft
= DEFAULT_FIELD_TYPE
532 pkt_end_time_ft
= DEFAULT_FIELD_TYPE
534 self
._features
= StreamTypeFeatures(StreamTypePacketFeatures(beginning_time_field_type
=pkt_beginning_time_ft
,
535 end_time_field_type
=pkt_end_time_ft
),
536 StreamTypeEventFeatures(time_field_type
=ev_time_ft
))
538 def _set_ft_mapped_clk_type_name(self
, ft
: Optional
[UnsignedIntegerFieldType
]):
542 if self
._default
_clock
_type
is not None:
543 assert isinstance(ft
, UnsignedIntegerFieldType
)
544 ft
._mapped
_clk
_type
_name
= self
._default
_clock
_type
.name
546 def _set_pkt_ctx_ft(self
):
549 def add_member_if_exists(name
: str, ft
: _FieldType
, set_mapped_clk_type_name
: bool = False):
553 if set_mapped_clk_type_name
:
554 self
._set
_ft
_mapped
_clk
_type
_name
(typing
.cast(UnsignedIntegerFieldType
, ft
))
556 members
[name
] = StructureFieldTypeMember(ft
)
558 members
= collections
.OrderedDict([
561 StructureFieldTypeMember(self
._features
.packet_features
.total_size_field_type
)
565 StructureFieldTypeMember(self
._features
.packet_features
.content_size_field_type
)
569 add_member_if_exists('timestamp_begin',
570 self
._features
.packet_features
.beginning_time_field_type
, True)
571 add_member_if_exists('timestamp_end', self
._features
.packet_features
.end_time_field_type
,
573 add_member_if_exists('events_discarded',
574 self
._features
.packet_features
.discarded_events_counter_field_type
)
576 if self
._packet
_context
_field
_type
_extra
_members
is not None:
577 for name
, field_type
in self
._packet
_context
_field
_type
_extra
_members
.items():
578 assert name
not in members
579 members
[name
] = field_type
581 self
._pkt
_ctx
_ft
= StructureFieldType(8, members
)
583 def _set_ev_header_ft(self
):
584 members
= collections
.OrderedDict()
586 if self
._features
.event_features
.type_id_field_type
is not None:
587 members
['id'] = StructureFieldTypeMember(self
._features
.event_features
.type_id_field_type
)
589 if self
._features
.event_features
.time_field_type
is not None:
590 ft
= self
._features
.event_features
.time_field_type
591 self
._set
_ft
_mapped
_clk
_type
_name
(ft
)
592 members
['timestamp'] = StructureFieldTypeMember(ft
)
594 self
._ev
_header
_ft
= StructureFieldType(8, members
)
597 def id(self
) -> Optional
[Id
]:
601 def name(self
) -> str:
605 def default_clock_type(self
) -> Optional
[ClockType
]:
606 return self
._default
_clock
_type
609 def features(self
) -> StreamTypeFeatures
:
610 return self
._features
613 def packet_context_field_type_extra_members(self
) -> StructureFieldTypeMembers
:
614 return self
._packet
_context
_field
_type
_extra
_members
617 def event_common_context_field_type(self
) -> _OptStructFt
:
618 return self
._event
_common
_context
_field
_type
621 def event_types(self
) -> FrozenSet
[EventType
]:
622 return self
._event
_types
625 _OptUuidFt
= Optional
[Union
[str, StaticArrayFieldType
]]
628 class TraceTypeFeatures
:
629 def __init__(self
, magic_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
630 uuid_field_type
: _OptUuidFt
= None,
631 stream_type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
):
632 def get_field_type(user_ft
: Optional
[Union
[str, _FieldType
]],
633 create_default_ft
: Callable
[[], _FieldType
]) -> _OptFt
:
634 if user_ft
== DEFAULT_FIELD_TYPE
:
635 return create_default_ft()
637 return typing
.cast(_OptFt
, user_ft
)
639 def create_default_magic_ft():
640 return UnsignedIntegerFieldType(32)
642 def create_default_uuid_ft():
643 return StaticArrayFieldType(Count(16), UnsignedIntegerFieldType(8))
645 def create_default_stream_type_id_ft():
646 return UnsignedIntegerFieldType(64)
648 self
._magic
_field
_type
= typing
.cast(_OptUIntFt
, get_field_type(magic_field_type
, create_default_magic_ft
))
649 self
._uuid
_field
_type
= typing
.cast(Optional
[StaticArrayFieldType
],
650 get_field_type(uuid_field_type
, create_default_uuid_ft
))
651 self
._stream
_type
_id
_field
_type
= typing
.cast(_OptUIntFt
,
652 get_field_type(stream_type_id_field_type
,
653 create_default_stream_type_id_ft
))
656 def magic_field_type(self
) -> _OptUIntFt
:
657 return self
._magic
_field
_type
660 def uuid_field_type(self
) -> Optional
[StaticArrayFieldType
]:
661 return self
._uuid
_field
_type
664 def stream_type_id_field_type(self
) -> _OptUIntFt
:
665 return self
._stream
_type
_id
_field
_type
669 def __init__(self
, stream_types
: Set
[StreamType
], uuid
: _OptUuid
= None,
670 features
: Optional
[TraceTypeFeatures
] = None):
671 self
._stream
_types
= frozenset(stream_types
)
674 for index
, stream_type
in enumerate(sorted(self
._stream
_types
, key
=lambda st
: st
.name
)):
675 assert stream_type
._id
is None
676 stream_type
._id
= Id(index
)
679 self
._set
_features
(features
)
680 self
._set
_pkt
_header
_ft
()
682 def _set_features(self
, features
: Optional
[TraceTypeFeatures
]):
683 if features
is not None:
684 self
._features
= features
687 # automatic UUID field type because the trace type has a UUID
688 uuid_ft
= None if self
._uuid
is None else DEFAULT_FIELD_TYPE
689 self
._features
= TraceTypeFeatures(uuid_field_type
=uuid_ft
)
691 def _set_pkt_header_ft(self
):
692 members
= collections
.OrderedDict()
694 def add_member_if_exists(name
: str, ft
: _OptFt
):
698 members
[name
] = StructureFieldTypeMember(ft
)
700 add_member_if_exists('magic', self
._features
.magic_field_type
)
701 add_member_if_exists('uuid', self
._features
.uuid_field_type
)
702 add_member_if_exists('stream_id', self
._features
.stream_type_id_field_type
)
703 self
._pkt
_header
_ft
= StructureFieldType(8, members
)
706 def uuid(self
) -> _OptUuid
:
710 def stream_types(self
) -> FrozenSet
[StreamType
]:
711 return self
._stream
_types
713 def stream_type(self
, name
: str) -> Optional
[StreamType
]:
714 for cand_stream_type
in self
._stream
_types
:
715 if cand_stream_type
.name
== name
:
716 return cand_stream_type
721 def features(self
) -> TraceTypeFeatures
:
722 return self
._features
725 def clock_types(self
) -> Set
[ClockType
]:
728 for stream_type
in self
._stream
_types
:
729 if stream_type
.default_clock_type
is not None:
730 clk_types
.add(stream_type
.default_clock_type
)
735 _EnvEntry
= Union
[str, int]
736 _EnvEntries
= Mapping
[str, _EnvEntry
]
739 class TraceEnvironment(collections
.abc
.Mapping
):
740 def __init__(self
, environment
: _EnvEntries
):
741 self
._env
= {name
: value
for name
, value
in environment
.items()}
743 def __getitem__(self
, key
: str) -> _EnvEntry
:
744 return self
._env
[key
]
746 def __iter__(self
) -> Iterator
[str]:
747 return iter(self
._env
)
749 def __len__(self
) -> int:
750 return len(self
._env
)
754 def __init__(self
, type: TraceType
, environment
: Optional
[_EnvEntries
] = None):
756 self
._set
_env
(environment
)
758 def _set_env(self
, environment
: Optional
[_EnvEntries
]):
759 init_env
= collections
.OrderedDict([
761 ('tracer_name', 'barectf'),
762 ('tracer_major', barectf_version
.__major
_version
__),
763 ('tracer_minor', barectf_version
.__minor
_version
__),
764 ('tracer_patch', barectf_version
.__patch
_version
__),
765 ('barectf_gen_date', str(datetime
.datetime
.now().isoformat())),
768 if environment
is None:
771 init_env
.update(environment
)
772 self
._env
= TraceEnvironment(typing
.cast(_EnvEntries
, init_env
))
775 def type(self
) -> TraceType
:
779 def environment(self
) -> TraceEnvironment
:
783 _ClkTypeCTypes
= Mapping
[ClockType
, str]
786 class ClockTypeCTypes(collections
.abc
.Mapping
):
787 def __init__(self
, c_types
: _ClkTypeCTypes
):
788 self
._c
_types
= {clk_type
: c_type
for clk_type
, c_type
in c_types
.items()}
790 def __getitem__(self
, key
: ClockType
) -> str:
791 return self
._c
_types
[key
]
793 def __iter__(self
) -> Iterator
[ClockType
]:
794 return iter(self
._c
_types
)
796 def __len__(self
) -> int:
797 return len(self
._c
_types
)
800 class ConfigurationCodeGenerationHeaderOptions
:
801 def __init__(self
, identifier_prefix_definition
: bool = False,
802 default_stream_type_name_definition
: bool = False):
803 self
._identifier
_prefix
_definition
= identifier_prefix_definition
804 self
._default
_stream
_type
_name
_definition
= default_stream_type_name_definition
807 def identifier_prefix_definition(self
) -> bool:
808 return self
._identifier
_prefix
_definition
811 def default_stream_type_name_definition(self
) -> bool:
812 return self
._default
_stream
_type
_name
_definition
815 class ConfigurationCodeGenerationOptions
:
816 def __init__(self
, identifier_prefix
: str = 'barectf_', file_name_prefix
: str = 'barectf',
817 default_stream_type
: Optional
[StreamType
] = None,
818 header_options
: Optional
[ConfigurationCodeGenerationHeaderOptions
] = None,
819 clock_type_c_types
: Optional
[_ClkTypeCTypes
] = None):
820 self
._identifier
_prefix
= identifier_prefix
821 self
._file
_name
_prefix
= file_name_prefix
822 self
._default
_stream
_type
= default_stream_type
824 self
._header
_options
= ConfigurationCodeGenerationHeaderOptions()
826 if header_options
is not None:
827 self
._header
_options
= header_options
829 self
._clock
_type
_c
_types
= ClockTypeCTypes({})
831 if clock_type_c_types
is not None:
832 self
._clock
_type
_c
_types
= ClockTypeCTypes(clock_type_c_types
)
835 def identifier_prefix(self
) -> str:
836 return self
._identifier
_prefix
839 def file_name_prefix(self
) -> str:
840 return self
._file
_name
_prefix
843 def default_stream_type(self
) -> Optional
[StreamType
]:
844 return self
._default
_stream
_type
847 def header_options(self
) -> ConfigurationCodeGenerationHeaderOptions
:
848 return self
._header
_options
851 def clock_type_c_types(self
) -> ClockTypeCTypes
:
852 return self
._clock
_type
_c
_types
855 class ConfigurationOptions
:
857 code_generation_options
: Optional
[ConfigurationCodeGenerationOptions
] = None):
858 self
._code
_generation
_options
= ConfigurationCodeGenerationOptions()
860 if code_generation_options
is not None:
861 self
._code
_generation
_options
= code_generation_options
864 def code_generation_options(self
) -> ConfigurationCodeGenerationOptions
:
865 return self
._code
_generation
_options
869 def __init__(self
, trace
: Trace
, target_byte_order
: ByteOrder
,
870 options
: Optional
[ConfigurationOptions
] = None):
872 self
._options
= ConfigurationOptions()
873 self
._target
_byte
_order
= target_byte_order
875 if options
is not None:
876 self
._options
= options
878 clk_type_c_types
= self
._options
.code_generation_options
.clock_type_c_types
880 for stream_type
in trace
.type.stream_types
:
881 def_clk_type
= stream_type
.default_clock_type
883 if def_clk_type
is None:
886 if def_clk_type
not in clk_type_c_types
:
887 clk_type_c_types
._c
_types
[def_clk_type
] = 'uint32_t'
890 def trace(self
) -> Trace
:
894 def target_byte_order(self
):
895 return self
._target
_byte
_order
898 def options(self
) -> ConfigurationOptions
: