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 DynamicArrayFieldType(_ArrayFieldType
):
226 def __init__(self
, length_field_type
: UnsignedIntegerFieldType
, element_field_type
: _FieldType
):
227 super().__init
__(element_field_type
)
228 self
._length
_field
_type
= length_field_type
229 self
._length
_ft
_member
_name
: Optional
[str] = None
232 def length_field_type(self
):
233 return self
._length
_field
_type
236 def size_is_dynamic(self
):
240 class StructureFieldTypeMember
:
241 def __init__(self
, field_type
: _FieldType
):
242 self
._field
_type
= field_type
245 def field_type(self
) -> _FieldType
:
246 return self
._field
_type
249 _StructFtMembers
= Mapping
[str, StructureFieldTypeMember
]
252 class StructureFieldTypeMembers(collections
.abc
.Mapping
):
253 def __init__(self
, members
: _StructFtMembers
):
254 self
._members
= collections
.OrderedDict()
256 for name
, member
in members
.items():
257 assert type(member
) is StructureFieldTypeMember
258 self
._members
[name
] = member
260 def __getitem__(self
, key
: str) -> StructureFieldTypeMember
:
261 return self
._members
[key
]
263 def __iter__(self
) -> Iterator
[str]:
264 return iter(self
._members
)
266 def __len__(self
) -> int:
267 return len(self
._members
)
270 class StructureFieldType(_FieldType
):
271 def __init__(self
, minimum_alignment
: Alignment
= Alignment(1),
272 members
: Optional
[_StructFtMembers
] = None):
273 self
._minimum
_alignment
= minimum_alignment
274 self
._members
= StructureFieldTypeMembers({})
276 if members
is not None:
277 self
._members
= StructureFieldTypeMembers(members
)
279 self
._set
_alignment
()
280 self
._set
_dyn
_array
_ft
_length
_ft
_member
_names
()
282 def _set_alignment(self
):
283 self
._alignment
: Alignment
= self
._minimum
_alignment
285 for member
in self
._members
.values():
286 if member
.field_type
.alignment
> self
._alignment
:
287 self
._alignment
= member
.field_type
.alignment
289 def _set_dyn_array_ft_length_ft_member_names(self
):
290 for member
in self
._members
.values():
291 if type(member
.field_type
) is DynamicArrayFieldType
:
292 # Find length field type member name within the same
293 # structure field type members.
294 for len_name
, len_member
in self
._members
.items():
295 if member
.field_type
.length_field_type
is len_member
.field_type
:
296 member
.field_type
._length
_ft
_member
_name
= len_name
297 len_member
.field_type
._is
_len
= True
300 if member
.field_type
.alignment
> self
._alignment
:
301 self
._alignment
= member
.field_type
.alignment
304 def minimum_alignment(self
) -> Alignment
:
305 return self
._minimum
_alignment
308 def alignment(self
) -> Alignment
:
309 return self
._alignment
312 def size_is_dynamic(self
):
313 return any([member
.field_type
.size_is_dynamic
for member
in self
.members
.values()])
316 def members(self
) -> StructureFieldTypeMembers
:
323 def __eq__(self
, other
: Any
) -> bool:
324 if type(other
) is not type(self
):
327 return self
._name
== other
._name
329 def __lt__(self
, other
: '_UniqueByName'):
330 assert type(self
) is type(other
)
331 return self
._name
< other
._name
333 def __hash__(self
) -> int:
334 return hash(self
._name
)
337 _OptFt
= Optional
[_FieldType
]
338 _OptStructFt
= Optional
[StructureFieldType
]
339 LogLevel
= typing
.NewType('LogLevel', int)
342 class EventType(_UniqueByName
):
343 def __init__(self
, name
: str, log_level
: Optional
[LogLevel
] = None,
344 specific_context_field_type
: _OptStructFt
= None, payload_field_type
: _OptStructFt
= None):
345 self
._id
: Optional
[Id
] = None
347 self
._log
_level
= log_level
348 self
._specific
_context
_field
_type
= specific_context_field_type
349 self
._payload
_field
_type
= payload_field_type
352 def id(self
) -> Optional
[Id
]:
356 def name(self
) -> str:
360 def log_level(self
) -> Optional
[LogLevel
]:
361 return self
._log
_level
364 def specific_context_field_type(self
) -> _OptStructFt
:
365 return self
._specific
_context
_field
_type
368 def payload_field_type(self
) -> _OptStructFt
:
369 return self
._payload
_field
_type
372 class ClockTypeOffset
:
373 def __init__(self
, seconds
: int = 0, cycles
: Count
= Count(0)):
374 self
._seconds
= seconds
375 self
._cycles
= cycles
378 def seconds(self
) -> int:
382 def cycles(self
) -> Count
:
386 _OptUuid
= Optional
[uuidp
.UUID
]
389 class ClockType(_UniqueByName
):
390 def __init__(self
, name
: str, frequency
: Count
= Count(int(1e9
)), uuid
: _OptUuid
= None,
391 description
: _OptStr
= None, precision
: Count
= Count(0),
392 offset
: Optional
[ClockTypeOffset
] = None, origin_is_unix_epoch
: bool = False):
394 self
._frequency
= frequency
396 self
._description
= description
397 self
._precision
= precision
398 self
._offset
= ClockTypeOffset()
400 if offset
is not None:
401 self
._offset
= offset
403 self
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
406 def name(self
) -> str:
410 def frequency(self
) -> Count
:
411 return self
._frequency
414 def uuid(self
) -> _OptUuid
:
418 def description(self
) -> _OptStr
:
419 return self
._description
422 def precision(self
) -> Count
:
423 return self
._precision
426 def offset(self
) -> ClockTypeOffset
:
430 def origin_is_unix_epoch(self
) -> bool:
431 return self
._origin
_is
_unix
_epoch
434 DEFAULT_FIELD_TYPE
= 'default'
435 _DefaultableUIntFt
= Union
[str, UnsignedIntegerFieldType
]
436 _OptDefaultableUIntFt
= Optional
[_DefaultableUIntFt
]
437 _OptUIntFt
= Optional
[UnsignedIntegerFieldType
]
440 class StreamTypePacketFeatures
:
441 def __init__(self
, total_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
442 content_size_field_type
: _DefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
443 beginning_time_field_type
: _OptDefaultableUIntFt
= None,
444 end_time_field_type
: _OptDefaultableUIntFt
= None,
445 discarded_events_counter_field_type
: _OptDefaultableUIntFt
= None):
446 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
447 if user_ft
== DEFAULT_FIELD_TYPE
:
448 return UnsignedIntegerFieldType(64)
450 return typing
.cast(_OptUIntFt
, user_ft
)
452 self
._total
_size
_field
_type
= get_ft(total_size_field_type
)
453 self
._content
_size
_field
_type
= get_ft(content_size_field_type
)
454 self
._beginning
_time
_field
_type
= get_ft(beginning_time_field_type
)
455 self
._end
_time
_field
_type
= get_ft(end_time_field_type
)
456 self
._discarded
_events
_counter
_field
_type
= get_ft(discarded_events_counter_field_type
)
459 def total_size_field_type(self
) -> _OptUIntFt
:
460 return self
._total
_size
_field
_type
463 def content_size_field_type(self
) -> _OptUIntFt
:
464 return self
._content
_size
_field
_type
467 def beginning_time_field_type(self
) -> _OptUIntFt
:
468 return self
._beginning
_time
_field
_type
471 def end_time_field_type(self
) -> _OptUIntFt
:
472 return self
._end
_time
_field
_type
475 def discarded_events_counter_field_type(self
) -> _OptUIntFt
:
476 return self
._discarded
_events
_counter
_field
_type
479 class StreamTypeEventFeatures
:
480 def __init__(self
, type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
481 time_field_type
: _OptDefaultableUIntFt
= None):
482 def get_ft(user_ft
: _OptDefaultableUIntFt
) -> _OptUIntFt
:
483 if user_ft
== DEFAULT_FIELD_TYPE
:
484 return UnsignedIntegerFieldType(64)
486 return typing
.cast(_OptUIntFt
, user_ft
)
488 self
._type
_id
_field
_type
= get_ft(type_id_field_type
)
489 self
._time
_field
_type
= get_ft(time_field_type
)
492 def type_id_field_type(self
) -> _OptUIntFt
:
493 return self
._type
_id
_field
_type
496 def time_field_type(self
) -> _OptUIntFt
:
497 return self
._time
_field
_type
500 class StreamTypeFeatures
:
501 def __init__(self
, packet_features
: Optional
[StreamTypePacketFeatures
] = None,
502 event_features
: Optional
[StreamTypeEventFeatures
] = None):
503 if packet_features
is None:
504 self
._packet
_features
= StreamTypePacketFeatures()
506 self
._packet
_features
= packet_features
508 if event_features
is None:
509 self
._event
_features
= StreamTypeEventFeatures()
511 self
._event
_features
= event_features
514 def packet_features(self
) -> StreamTypePacketFeatures
:
515 return self
._packet
_features
518 def event_features(self
) -> StreamTypeEventFeatures
:
519 return self
._event
_features
522 class StreamType(_UniqueByName
):
523 def __init__(self
, name
: str, event_types
: Set
[EventType
],
524 default_clock_type
: Optional
[ClockType
] = None,
525 features
: Optional
[StreamTypeFeatures
] = None,
526 packet_context_field_type_extra_members
: Optional
[_StructFtMembers
] = None,
527 event_common_context_field_type
: _OptStructFt
= None):
528 self
._id
: Optional
[Id
] = None
530 self
._default
_clock
_type
= default_clock_type
531 self
._event
_common
_context
_field
_type
= event_common_context_field_type
532 self
._event
_types
= frozenset(event_types
)
535 for index
, ev_type
in enumerate(sorted(self
._event
_types
, key
=lambda evt
: evt
.name
)):
536 assert ev_type
._id
is None
537 ev_type
._id
= Id(index
)
539 self
._set
_features
(features
)
540 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers({})
542 if packet_context_field_type_extra_members
is not None:
543 self
._packet
_context
_field
_type
_extra
_members
= StructureFieldTypeMembers(packet_context_field_type_extra_members
)
545 self
._set
_pkt
_ctx
_ft
()
546 self
._set
_ev
_header
_ft
()
548 def _set_features(self
, features
: Optional
[StreamTypeFeatures
]):
549 if features
is not None:
550 self
._features
= features
554 pkt_beginning_time_ft
= None
555 pkt_end_time_ft
= None
557 if self
._default
_clock
_type
is not None:
558 # Automatic time field types because the stream type has a
559 # default clock type.
560 ev_time_ft
= DEFAULT_FIELD_TYPE
561 pkt_beginning_time_ft
= DEFAULT_FIELD_TYPE
562 pkt_end_time_ft
= DEFAULT_FIELD_TYPE
564 self
._features
= StreamTypeFeatures(StreamTypePacketFeatures(beginning_time_field_type
=pkt_beginning_time_ft
,
565 end_time_field_type
=pkt_end_time_ft
),
566 StreamTypeEventFeatures(time_field_type
=ev_time_ft
))
568 def _set_ft_mapped_clk_type_name(self
, ft
: Optional
[UnsignedIntegerFieldType
]):
572 if self
._default
_clock
_type
is not None:
573 assert isinstance(ft
, UnsignedIntegerFieldType
)
574 ft
._mapped
_clk
_type
_name
= self
._default
_clock
_type
.name
576 def _set_pkt_ctx_ft(self
):
579 def add_member_if_exists(name
: str, ft
: _FieldType
, set_mapped_clk_type_name
: bool = False):
583 if set_mapped_clk_type_name
:
584 self
._set
_ft
_mapped
_clk
_type
_name
(typing
.cast(UnsignedIntegerFieldType
, ft
))
586 members
[name
] = StructureFieldTypeMember(ft
)
588 members
= collections
.OrderedDict([
591 StructureFieldTypeMember(self
._features
.packet_features
.total_size_field_type
)
595 StructureFieldTypeMember(self
._features
.packet_features
.content_size_field_type
)
599 add_member_if_exists('timestamp_begin',
600 self
._features
.packet_features
.beginning_time_field_type
, True)
601 add_member_if_exists('timestamp_end', self
._features
.packet_features
.end_time_field_type
,
603 add_member_if_exists('events_discarded',
604 self
._features
.packet_features
.discarded_events_counter_field_type
)
606 if self
._packet
_context
_field
_type
_extra
_members
is not None:
607 for name
, field_type
in self
._packet
_context
_field
_type
_extra
_members
.items():
608 assert name
not in members
609 members
[name
] = field_type
611 self
._pkt
_ctx
_ft
= StructureFieldType(8, members
)
613 def _set_ev_header_ft(self
):
614 members
= collections
.OrderedDict()
616 if self
._features
.event_features
.type_id_field_type
is not None:
617 members
['id'] = StructureFieldTypeMember(self
._features
.event_features
.type_id_field_type
)
619 if self
._features
.event_features
.time_field_type
is not None:
620 ft
= self
._features
.event_features
.time_field_type
621 self
._set
_ft
_mapped
_clk
_type
_name
(ft
)
622 members
['timestamp'] = StructureFieldTypeMember(ft
)
624 self
._ev
_header
_ft
= StructureFieldType(8, members
)
627 def id(self
) -> Optional
[Id
]:
631 def name(self
) -> str:
635 def default_clock_type(self
) -> Optional
[ClockType
]:
636 return self
._default
_clock
_type
639 def features(self
) -> StreamTypeFeatures
:
640 return self
._features
643 def packet_context_field_type_extra_members(self
) -> StructureFieldTypeMembers
:
644 return self
._packet
_context
_field
_type
_extra
_members
647 def event_common_context_field_type(self
) -> _OptStructFt
:
648 return self
._event
_common
_context
_field
_type
651 def event_types(self
) -> FrozenSet
[EventType
]:
652 return self
._event
_types
655 _OptUuidFt
= Optional
[Union
[str, StaticArrayFieldType
]]
658 class TraceTypeFeatures
:
659 def __init__(self
, magic_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
,
660 uuid_field_type
: _OptUuidFt
= None,
661 stream_type_id_field_type
: _OptDefaultableUIntFt
= DEFAULT_FIELD_TYPE
):
662 def get_field_type(user_ft
: Optional
[Union
[str, _FieldType
]],
663 create_default_ft
: Callable
[[], _FieldType
]) -> _OptFt
:
664 if user_ft
== DEFAULT_FIELD_TYPE
:
665 return create_default_ft()
667 return typing
.cast(_OptFt
, user_ft
)
669 def create_default_magic_ft():
670 return UnsignedIntegerFieldType(32)
672 def create_default_uuid_ft():
673 return StaticArrayFieldType(Count(16), UnsignedIntegerFieldType(8))
675 def create_default_stream_type_id_ft():
676 return UnsignedIntegerFieldType(64)
678 self
._magic
_field
_type
= typing
.cast(_OptUIntFt
, get_field_type(magic_field_type
, create_default_magic_ft
))
679 self
._uuid
_field
_type
= typing
.cast(Optional
[StaticArrayFieldType
],
680 get_field_type(uuid_field_type
, create_default_uuid_ft
))
681 self
._stream
_type
_id
_field
_type
= typing
.cast(_OptUIntFt
,
682 get_field_type(stream_type_id_field_type
,
683 create_default_stream_type_id_ft
))
686 def magic_field_type(self
) -> _OptUIntFt
:
687 return self
._magic
_field
_type
690 def uuid_field_type(self
) -> Optional
[StaticArrayFieldType
]:
691 return self
._uuid
_field
_type
694 def stream_type_id_field_type(self
) -> _OptUIntFt
:
695 return self
._stream
_type
_id
_field
_type
699 def __init__(self
, stream_types
: Set
[StreamType
], uuid
: _OptUuid
= None,
700 features
: Optional
[TraceTypeFeatures
] = None):
701 self
._stream
_types
= frozenset(stream_types
)
704 for index
, stream_type
in enumerate(sorted(self
._stream
_types
, key
=lambda st
: st
.name
)):
705 assert stream_type
._id
is None
706 stream_type
._id
= Id(index
)
709 self
._set
_features
(features
)
710 self
._set
_pkt
_header
_ft
()
712 def _set_features(self
, features
: Optional
[TraceTypeFeatures
]):
713 if features
is not None:
714 self
._features
= features
717 # automatic UUID field type because the trace type has a UUID
718 uuid_ft
= None if self
._uuid
is None else DEFAULT_FIELD_TYPE
719 self
._features
= TraceTypeFeatures(uuid_field_type
=uuid_ft
)
721 def _set_pkt_header_ft(self
):
722 members
= collections
.OrderedDict()
724 def add_member_if_exists(name
: str, ft
: _OptFt
):
728 members
[name
] = StructureFieldTypeMember(ft
)
730 add_member_if_exists('magic', self
._features
.magic_field_type
)
731 add_member_if_exists('uuid', self
._features
.uuid_field_type
)
732 add_member_if_exists('stream_id', self
._features
.stream_type_id_field_type
)
733 self
._pkt
_header
_ft
= StructureFieldType(8, members
)
736 def uuid(self
) -> _OptUuid
:
740 def stream_types(self
) -> FrozenSet
[StreamType
]:
741 return self
._stream
_types
743 def stream_type(self
, name
: str) -> Optional
[StreamType
]:
744 for cand_stream_type
in self
._stream
_types
:
745 if cand_stream_type
.name
== name
:
746 return cand_stream_type
751 def features(self
) -> TraceTypeFeatures
:
752 return self
._features
755 def clock_types(self
) -> Set
[ClockType
]:
758 for stream_type
in self
._stream
_types
:
759 if stream_type
.default_clock_type
is not None:
760 clk_types
.add(stream_type
.default_clock_type
)
765 _EnvEntry
= Union
[str, int]
766 _EnvEntries
= Mapping
[str, _EnvEntry
]
769 class TraceEnvironment(collections
.abc
.Mapping
):
770 def __init__(self
, environment
: _EnvEntries
):
771 self
._env
= {name
: value
for name
, value
in environment
.items()}
773 def __getitem__(self
, key
: str) -> _EnvEntry
:
774 return self
._env
[key
]
776 def __iter__(self
) -> Iterator
[str]:
777 return iter(self
._env
)
779 def __len__(self
) -> int:
780 return len(self
._env
)
784 def __init__(self
, type: TraceType
, environment
: Optional
[_EnvEntries
] = None):
786 self
._set
_env
(environment
)
788 def _set_env(self
, environment
: Optional
[_EnvEntries
]):
789 init_env
= collections
.OrderedDict([
791 ('tracer_name', 'barectf'),
792 ('tracer_major', barectf_version
.__major
_version
__),
793 ('tracer_minor', barectf_version
.__minor
_version
__),
794 ('tracer_patch', barectf_version
.__patch
_version
__),
795 ('barectf_gen_date', str(datetime
.datetime
.now().isoformat())),
798 if environment
is None:
801 init_env
.update(environment
)
802 self
._env
= TraceEnvironment(typing
.cast(_EnvEntries
, init_env
))
805 def type(self
) -> TraceType
:
809 def environment(self
) -> TraceEnvironment
:
813 _ClkTypeCTypes
= Mapping
[ClockType
, str]
816 class ClockTypeCTypes(collections
.abc
.Mapping
):
817 def __init__(self
, c_types
: _ClkTypeCTypes
):
818 self
._c
_types
= {clk_type
: c_type
for clk_type
, c_type
in c_types
.items()}
820 def __getitem__(self
, key
: ClockType
) -> str:
821 return self
._c
_types
[key
]
823 def __iter__(self
) -> Iterator
[ClockType
]:
824 return iter(self
._c
_types
)
826 def __len__(self
) -> int:
827 return len(self
._c
_types
)
830 class ConfigurationCodeGenerationHeaderOptions
:
831 def __init__(self
, identifier_prefix_definition
: bool = False,
832 default_stream_type_name_definition
: bool = False):
833 self
._identifier
_prefix
_definition
= identifier_prefix_definition
834 self
._default
_stream
_type
_name
_definition
= default_stream_type_name_definition
837 def identifier_prefix_definition(self
) -> bool:
838 return self
._identifier
_prefix
_definition
841 def default_stream_type_name_definition(self
) -> bool:
842 return self
._default
_stream
_type
_name
_definition
845 class ConfigurationCodeGenerationOptions
:
846 def __init__(self
, identifier_prefix
: str = 'barectf_', file_name_prefix
: str = 'barectf',
847 default_stream_type
: Optional
[StreamType
] = None,
848 header_options
: Optional
[ConfigurationCodeGenerationHeaderOptions
] = None,
849 clock_type_c_types
: Optional
[_ClkTypeCTypes
] = None):
850 self
._identifier
_prefix
= identifier_prefix
851 self
._file
_name
_prefix
= file_name_prefix
852 self
._default
_stream
_type
= default_stream_type
854 self
._header
_options
= ConfigurationCodeGenerationHeaderOptions()
856 if header_options
is not None:
857 self
._header
_options
= header_options
859 self
._clock
_type
_c
_types
= ClockTypeCTypes({})
861 if clock_type_c_types
is not None:
862 self
._clock
_type
_c
_types
= ClockTypeCTypes(clock_type_c_types
)
865 def identifier_prefix(self
) -> str:
866 return self
._identifier
_prefix
869 def file_name_prefix(self
) -> str:
870 return self
._file
_name
_prefix
873 def default_stream_type(self
) -> Optional
[StreamType
]:
874 return self
._default
_stream
_type
877 def header_options(self
) -> ConfigurationCodeGenerationHeaderOptions
:
878 return self
._header
_options
881 def clock_type_c_types(self
) -> ClockTypeCTypes
:
882 return self
._clock
_type
_c
_types
885 class ConfigurationOptions
:
887 code_generation_options
: Optional
[ConfigurationCodeGenerationOptions
] = None):
888 self
._code
_generation
_options
= ConfigurationCodeGenerationOptions()
890 if code_generation_options
is not None:
891 self
._code
_generation
_options
= code_generation_options
894 def code_generation_options(self
) -> ConfigurationCodeGenerationOptions
:
895 return self
._code
_generation
_options
899 def __init__(self
, trace
: Trace
, target_byte_order
: ByteOrder
,
900 options
: Optional
[ConfigurationOptions
] = None):
902 self
._options
= ConfigurationOptions()
903 self
._target
_byte
_order
= target_byte_order
905 if options
is not None:
906 self
._options
= options
908 clk_type_c_types
= self
._options
.code_generation_options
.clock_type_c_types
910 for stream_type
in trace
.type.stream_types
:
911 def_clk_type
= stream_type
.default_clock_type
913 if def_clk_type
is None:
916 if def_clk_type
not in clk_type_c_types
:
917 clk_type_c_types
._c
_types
[def_clk_type
] = 'uint32_t'
920 def trace(self
) -> Trace
:
924 def target_byte_order(self
):
925 return self
._target
_byte
_order
928 def options(self
) -> ConfigurationOptions
: