1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
30 def _create_from_ptr(ptr
):
31 typeid
= native_bt
.field_type_get_type_id(ptr
)
32 return _TYPE_ID_TO_OBJ
[typeid
]._create
_from
_ptr
(ptr
)
35 class _FieldType(object._Object
, metaclass
=abc
.ABCMeta
):
36 def __init__(self
, ptr
):
39 def __eq__(self
, other
):
40 if not isinstance(other
, self
.__class
__):
41 # not comparing apples to apples
44 if self
.addr
== other
.addr
:
47 ret
= native_bt
.field_type_compare(self
._ptr
, other
._ptr
)
48 utils
._handle
_ret
(ret
, "cannot compare field types")
51 def _check_create_status(self
, ptr
):
53 raise bt2
.CreationError('cannot create {} field type object'.format(self
._NAME
.lower()))
56 ptr
= native_bt
.field_type_copy(self
._ptr
)
57 utils
._handle
_ptr
(ptr
, 'cannot copy {} field type object'.format(self
._NAME
.lower()))
58 return _create_from_ptr(ptr
)
60 def __deepcopy__(self
, memo
):
65 def __call__(self
, value
=None):
66 field_ptr
= native_bt
.field_create(self
._ptr
)
69 raise bt2
.CreationError('cannot create {} field object'.format(self
._NAME
.lower()))
71 field
= bt2
.fields
._create
_from
_ptr
(field_ptr
)
74 if not isinstance(field
, (bt2
.fields
._IntegerField
, bt2
.fields
._FloatingPointNumberField
, bt2
.fields
._StringField
)):
75 raise bt2
.Error('cannot assign an initial value to a {} field object'.format(field
._NAME
))
83 NATIVE
= native_bt
.BYTE_ORDER_NATIVE
84 LITTLE_ENDIAN
= native_bt
.BYTE_ORDER_LITTLE_ENDIAN
85 BIG_ENDIAN
= native_bt
.BYTE_ORDER_BIG_ENDIAN
86 NETWORK
= native_bt
.BYTE_ORDER_NETWORK
90 NONE
= native_bt
.STRING_ENCODING_NONE
91 UTF8
= native_bt
.STRING_ENCODING_UTF8
92 ASCII
= native_bt
.STRING_ENCODING_ASCII
96 BINARY
= native_bt
.INTEGER_BASE_BINARY
97 OCTAL
= native_bt
.INTEGER_BASE_OCTAL
98 DECIMAL
= native_bt
.INTEGER_BASE_DECIMAL
99 HEXADECIMAL
= native_bt
.INTEGER_BASE_HEXADECIMAL
102 class _AlignmentProp
:
105 alignment
= native_bt
.field_type_get_alignment(self
._ptr
)
106 assert(alignment
>= 0)
110 def alignment(self
, alignment
):
111 utils
._check
_alignment
(alignment
)
112 ret
= native_bt
.field_type_set_alignment(self
._ptr
, alignment
)
113 utils
._handle
_ret
(ret
, "cannot set field type object's alignment")
116 class _ByteOrderProp
:
118 def byte_order(self
):
119 bo
= native_bt
.field_type_get_byte_order(self
._ptr
)
124 def byte_order(self
, byte_order
):
125 utils
._check
_int
(byte_order
)
126 ret
= native_bt
.field_type_set_byte_order(self
._ptr
, byte_order
)
127 utils
._handle
_ret
(ret
, "cannot set field type object's byte order")
130 class IntegerFieldType(_FieldType
, _AlignmentProp
, _ByteOrderProp
):
133 def __init__(self
, size
, alignment
=None, byte_order
=None, is_signed
=None,
134 base
=None, encoding
=None, mapped_clock_class
=None):
135 utils
._check
_uint
64(size
)
138 raise ValueError('size is 0 bits')
140 ptr
= native_bt
.field_type_integer_create(size
)
141 self
._check
_create
_status
(ptr
)
142 super().__init
__(ptr
)
144 if alignment
is not None:
145 self
.alignment
= alignment
147 if byte_order
is not None:
148 self
.byte_order
= byte_order
150 if is_signed
is not None:
151 self
.is_signed
= is_signed
156 if encoding
is not None:
157 self
.encoding
= encoding
159 if mapped_clock_class
is not None:
160 self
.mapped_clock_class
= mapped_clock_class
164 size
= native_bt
.field_type_integer_get_size(self
._ptr
)
170 is_signed
= native_bt
.field_type_integer_is_signed(self
._ptr
)
171 assert(is_signed
>= 0)
175 def is_signed(self
, is_signed
):
176 utils
._check
_bool
(is_signed
)
177 ret
= native_bt
.field_type_integer_set_is_signed(self
._ptr
, int(is_signed
))
178 utils
._handle
_ret
(ret
, "cannot set integer field type object's signedness")
182 base
= native_bt
.field_type_integer_get_base(self
._ptr
)
187 def base(self
, base
):
188 utils
._check
_int
(base
)
189 ret
= native_bt
.field_type_integer_set_base(self
._ptr
, base
)
190 utils
._handle
_ret
(ret
, "cannot set integer field type object's base")
194 encoding
= native_bt
.field_type_integer_get_encoding(self
._ptr
)
195 assert(encoding
>= 0)
199 def encoding(self
, encoding
):
200 utils
._check
_int
(encoding
)
201 ret
= native_bt
.field_type_integer_set_encoding(self
._ptr
, encoding
)
202 utils
._handle
_ret
(ret
, "cannot set integer field type object's encoding")
205 def mapped_clock_class(self
):
206 ptr
= native_bt
.field_type_integer_get_mapped_clock_class(self
._ptr
)
211 return bt2
.ClockClass
._create
_from
_ptr
(ptr
)
213 @mapped_clock_class.setter
214 def mapped_clock_class(self
, clock_class
):
215 utils
._check
_type
(clock_class
, bt2
.ClockClass
)
216 ret
= native_bt
.field_type_integer_set_mapped_clock_class(self
._ptr
, clock_class
._ptr
)
217 utils
._handle
_ret
(ret
, "cannot set integer field type object's mapped clock class")
220 class FloatingPointNumberFieldType(_FieldType
, _AlignmentProp
, _ByteOrderProp
):
221 _NAME
= 'Floating point number'
223 def __init__(self
, alignment
=None, byte_order
=None, exponent_size
=None,
225 ptr
= native_bt
.field_type_floating_point_create()
226 self
._check
_create
_status
(ptr
)
227 super().__init
__(ptr
)
229 if alignment
is not None:
230 self
.alignment
= alignment
232 if byte_order
is not None:
233 self
.byte_order
= byte_order
235 if exponent_size
is not None:
236 self
.exponent_size
= exponent_size
238 if mantissa_size
is not None:
239 self
.mantissa_size
= mantissa_size
242 def exponent_size(self
):
243 exp_size
= native_bt
.field_type_floating_point_get_exponent_digits(self
._ptr
)
244 assert(exp_size
>= 0)
247 @exponent_size.setter
248 def exponent_size(self
, exponent_size
):
249 utils
._check
_uint
64(exponent_size
)
250 ret
= native_bt
.field_type_floating_point_set_exponent_digits(self
._ptr
, exponent_size
)
251 utils
._handle
_ret
(ret
, "cannot set floating point number field type object's exponent size")
254 def mantissa_size(self
):
255 mant_size
= native_bt
.field_type_floating_point_get_mantissa_digits(self
._ptr
)
256 assert(mant_size
>= 0)
259 @mantissa_size.setter
260 def mantissa_size(self
, mantissa_size
):
261 utils
._check
_uint
64(mantissa_size
)
262 ret
= native_bt
.field_type_floating_point_set_mantissa_digits(self
._ptr
, mantissa_size
)
263 utils
._handle
_ret
(ret
, "cannot set floating point number field type object's mantissa size")
266 class _EnumerationFieldTypeMapping
:
267 def __init__(self
, name
, lower
, upper
):
284 def __eq__(self
, other
):
285 if type(other
) is not self
.__class
__:
288 return (self
.name
, self
.lower
, self
.upper
) == (other
.name
, other
.lower
, other
.upper
)
291 class _EnumerationFieldTypeMappingIterator(object._Object
,
292 collections
.abc
.Iterator
):
293 def __init__(self
, iter_ptr
, is_signed
):
294 super().__init
__(iter_ptr
)
295 self
._is
_signed
= is_signed
296 self
._done
= (iter_ptr
is None)
302 ret
= native_bt
.field_type_enumeration_mapping_iterator_next(self
._ptr
)
308 ret
, name
, lower
, upper
= native_bt
.field_type_enumeration_mapping_iterator_get_signed(self
._ptr
)
310 ret
, name
, lower
, upper
= native_bt
.field_type_enumeration_mapping_iterator_get_unsigned(self
._ptr
)
313 mapping
= _EnumerationFieldTypeMapping(name
, lower
, upper
)
318 class EnumerationFieldType(IntegerFieldType
, collections
.abc
.Sequence
):
319 _NAME
= 'Enumeration'
321 def __init__(self
, int_field_type
=None, size
=None, alignment
=None,
322 byte_order
=None, is_signed
=None, base
=None, encoding
=None,
323 mapped_clock_class
=None):
324 if int_field_type
is None:
325 int_field_type
= IntegerFieldType(size
=size
, alignment
=alignment
,
326 byte_order
=byte_order
,
327 is_signed
=is_signed
, base
=base
,
329 mapped_clock_class
=mapped_clock_class
)
331 utils
._check
_type
(int_field_type
, IntegerFieldType
)
332 ptr
= native_bt
.field_type_enumeration_create(int_field_type
._ptr
)
333 self
._check
_create
_status
(ptr
)
334 _FieldType
.__init
__(self
, ptr
)
337 def integer_field_type(self
):
338 ptr
= native_bt
.field_type_enumeration_get_container_type(self
._ptr
)
340 return _create_from_ptr(ptr
)
344 return self
.integer_field_type
.size
348 return self
.integer_field_type
.alignment
351 def alignment(self
, alignment
):
352 self
.integer_field_type
.alignment
= alignment
355 def byte_order(self
):
356 return self
.integer_field_type
.byte_order
359 def byte_order(self
, byte_order
):
360 self
.integer_field_type
.byte_order
= byte_order
364 return self
.integer_field_type
.is_signed
367 def is_signed(self
, is_signed
):
368 self
.integer_field_type
.is_signed
= is_signed
372 return self
.integer_field_type
.base
375 def base(self
, base
):
376 self
.integer_field_type
.base
= base
380 return self
.integer_field_type
.encoding
383 def encoding(self
, encoding
):
384 self
.integer_field_type
.encoding
= encoding
387 def mapped_clock_class(self
):
388 return self
.integer_field_type
.mapped_clock_class
390 @mapped_clock_class.setter
391 def mapped_clock_class(self
, mapped_clock_class
):
392 self
.integer_field_type
.mapped_clock_class
= mapped_clock_class
395 count
= native_bt
.field_type_enumeration_get_mapping_count(self
._ptr
)
399 def __getitem__(self
, index
):
400 utils
._check
_uint
64(index
)
402 if index
>= len(self
):
406 get_fn
= native_bt
.field_type_enumeration_get_mapping_signed
408 get_fn
= native_bt
.field_type_enumeration_get_mapping_unsigned
410 ret
, name
, lower
, upper
= get_fn(self
._ptr
, index
)
412 return _EnumerationFieldTypeMapping(name
, lower
, upper
)
414 def _get_mapping_iter(self
, iter_ptr
):
415 return _EnumerationFieldTypeMappingIterator(iter_ptr
, self
.is_signed
)
417 def mappings_by_name(self
, name
):
418 utils
._check
_str
(name
)
419 iter_ptr
= native_bt
.field_type_enumeration_find_mappings_by_name(self
._ptr
, name
)
420 print('iter_ptr', iter_ptr
)
421 return self
._get
_mapping
_iter
(iter_ptr
)
423 def mappings_by_value(self
, value
):
425 utils
._check
_int
64(value
)
426 iter_ptr
= native_bt
.field_type_enumeration_find_mappings_by_signed_value(self
._ptr
, value
)
428 utils
._check
_uint
64(value
)
429 iter_ptr
= native_bt
.field_type_enumeration_find_mappings_by_unsigned_value(self
._ptr
, value
)
431 return self
._get
_mapping
_iter
(iter_ptr
)
433 def append_mapping(self
, name
, lower
, upper
=None):
434 utils
._check
_str
(name
)
440 add_fn
= native_bt
.field_type_enumeration_add_mapping_signed
441 utils
._check
_int
64(lower
)
442 utils
._check
_int
64(upper
)
444 add_fn
= native_bt
.field_type_enumeration_add_mapping_unsigned
445 utils
._check
_uint
64(lower
)
446 utils
._check
_uint
64(upper
)
448 ret
= add_fn(self
._ptr
, name
, lower
, upper
)
449 utils
._handle
_ret
(ret
, "cannot add mapping to enumeration field type object")
451 def __iadd__(self
, mappings
):
452 for mapping
in mappings
:
453 self
.append_mapping(mapping
.name
, mapping
.lower
, mapping
.upper
)
458 class StringFieldType(_FieldType
):
461 def __init__(self
, encoding
=None):
462 ptr
= native_bt
.field_type_string_create()
463 self
._check
_create
_status
(ptr
)
464 super().__init
__(ptr
)
466 if encoding
is not None:
467 self
.encoding
= encoding
471 encoding
= native_bt
.field_type_string_get_encoding(self
._ptr
)
472 assert(encoding
>= 0)
476 def encoding(self
, encoding
):
477 utils
._check
_int
(encoding
)
478 ret
= native_bt
.field_type_string_set_encoding(self
._ptr
, encoding
)
479 utils
._handle
_ret
(ret
, "cannot set string field type object's encoding")
482 class _FieldContainer(collections
.abc
.Mapping
):
484 count
= self
._count
()
488 def __getitem__(self
, key
):
489 if not isinstance(key
, str):
490 raise TypeError("'{}' is not a 'str' object".format(key
.__class
__.__name
__))
492 ptr
= self
._get
_field
_by
_name
(key
)
497 return _create_from_ptr(ptr
)
500 return self
._ITER
_CLS
(self
)
502 def append_field(self
, name
, field_type
):
503 utils
._check
_str
(name
)
504 utils
._check
_type
(field_type
, _FieldType
)
505 ret
= self
._add
_field
(field_type
._ptr
, name
)
506 utils
._handle
_ret
(ret
, "cannot add field to {} field type object".format(self
._NAME
.lower()))
508 def __iadd__(self
, fields
):
509 for name
, field_type
in fields
.items():
510 self
.append_field(name
, field_type
)
514 def at_index(self
, index
):
515 utils
._check
_uint
64(index
)
516 return self
._at
(index
)
519 class _StructureFieldTypeFieldIterator(collections
.abc
.Iterator
):
520 def __init__(self
, struct_field_type
):
521 self
._struct
_field
_type
= struct_field_type
525 if self
._at
== len(self
._struct
_field
_type
):
528 get_ft_by_index
= native_bt
.field_type_structure_get_field_by_index
529 ret
, name
, field_type_ptr
= get_ft_by_index(self
._struct
_field
_type
._ptr
,
532 native_bt
.put(field_type_ptr
)
537 class StructureFieldType(_FieldType
, _FieldContainer
, _AlignmentProp
):
539 _ITER_CLS
= _StructureFieldTypeFieldIterator
541 def __init__(self
, min_alignment
=None):
542 ptr
= native_bt
.field_type_structure_create()
543 self
._check
_create
_status
(ptr
)
544 super().__init
__(ptr
)
546 if min_alignment
is not None:
547 self
.min_alignment
= min_alignment
550 return native_bt
.field_type_structure_get_field_count(self
._ptr
)
552 def _get_field_by_name(self
, key
):
553 return native_bt
.field_type_structure_get_field_type_by_name(self
._ptr
, key
)
555 def _add_field(self
, ptr
, name
):
556 return native_bt
.field_type_structure_add_field(self
._ptr
, ptr
,
559 def _at(self
, index
):
560 if index
< 0 or index
>= len(self
):
563 ret
, name
, field_type_ptr
= native_bt
.field_type_structure_get_field_by_index(self
._ptr
, index
)
565 return _create_from_ptr(field_type_ptr
)
568 StructureFieldType
.min_alignment
= property(fset
=StructureFieldType
.alignment
.fset
)
569 StructureFieldType
.alignment
= property(fget
=StructureFieldType
.alignment
.fget
)
572 class _VariantFieldTypeFieldIterator(collections
.abc
.Iterator
):
573 def __init__(self
, variant_field_type
):
574 self
._variant
_field
_type
= variant_field_type
578 if self
._at
== len(self
._variant
_field
_type
):
581 ret
, name
, field_type_ptr
= native_bt
.field_type_variant_get_field_by_index(self
._variant
_field
_type
._ptr
,
584 native_bt
.put(field_type_ptr
)
589 class VariantFieldType(_FieldType
, _FieldContainer
, _AlignmentProp
):
591 _ITER_CLS
= _VariantFieldTypeFieldIterator
593 def __init__(self
, tag_name
, tag_field_type
=None):
594 utils
._check
_str
(tag_name
)
596 if tag_field_type
is None:
599 utils
._check
_type
(tag_field_type
, EnumerationFieldType
)
600 tag_ft_ptr
= tag_field_type
._ptr
602 ptr
= native_bt
.field_type_variant_create(tag_ft_ptr
,
604 self
._check
_create
_status
(ptr
)
605 super().__init
__(ptr
)
609 tag_name
= native_bt
.field_type_variant_get_tag_name(self
._ptr
)
610 assert(tag_name
is not None)
614 def tag_name(self
, tag_name
):
615 utils
._check
_str
(tag_name
)
616 ret
= native_bt
.field_type_variant_set_tag_name(self
._ptr
, tag_name
)
617 utils
._handle
_ret
(ret
, "cannot set variant field type object's tag name")
620 def tag_field_type(self
):
621 ft_ptr
= native_bt
.field_type_variant_get_tag_type(self
._ptr
)
626 return _create_from_ptr(ft_ptr
)
629 return native_bt
.field_type_variant_get_field_count(self
._ptr
)
631 def _get_field_by_name(self
, key
):
632 return native_bt
.field_type_variant_get_field_type_by_name(self
._ptr
, key
)
634 def _add_field(self
, ptr
, name
):
635 return native_bt
.field_type_variant_add_field(self
._ptr
, ptr
, name
)
637 def _at(self
, index
):
638 if index
< 0 or index
>= len(self
):
641 ret
, name
, field_type_ptr
= native_bt
.field_type_variant_get_field_by_index(self
._ptr
, index
)
643 return _create_from_ptr(field_type_ptr
)
646 class ArrayFieldType(_FieldType
):
649 def __init__(self
, element_field_type
, length
):
650 utils
._check
_type
(element_field_type
, _FieldType
)
651 utils
._check
_uint
64(length
)
652 ptr
= native_bt
.field_type_array_create(element_field_type
._ptr
, length
)
653 self
._check
_create
_status
(ptr
)
654 super().__init
__(ptr
)
658 length
= native_bt
.field_type_array_get_length(self
._ptr
)
663 def element_field_type(self
):
664 ptr
= native_bt
.field_type_array_get_element_type(self
._ptr
)
666 return _create_from_ptr(ptr
)
669 class SequenceFieldType(_FieldType
):
672 def __init__(self
, element_field_type
, length_name
):
673 utils
._check
_type
(element_field_type
, _FieldType
)
674 utils
._check
_str
(length_name
)
675 ptr
= native_bt
.field_type_sequence_create(element_field_type
._ptr
,
677 self
._check
_create
_status
(ptr
)
678 super().__init
__(ptr
)
681 def length_name(self
):
682 length_name
= native_bt
.field_type_sequence_get_length_field_name(self
._ptr
)
683 assert(length_name
is not None)
687 def element_field_type(self
):
688 ptr
= native_bt
.field_type_sequence_get_element_type(self
._ptr
)
690 return _create_from_ptr(ptr
)
694 native_bt
.FIELD_TYPE_ID_INTEGER
: IntegerFieldType
,
695 native_bt
.FIELD_TYPE_ID_FLOAT
: FloatingPointNumberFieldType
,
696 native_bt
.FIELD_TYPE_ID_ENUM
: EnumerationFieldType
,
697 native_bt
.FIELD_TYPE_ID_STRING
: StringFieldType
,
698 native_bt
.FIELD_TYPE_ID_STRUCT
: StructureFieldType
,
699 native_bt
.FIELD_TYPE_ID_ARRAY
: ArrayFieldType
,
700 native_bt
.FIELD_TYPE_ID_SEQUENCE
: SequenceFieldType
,
701 native_bt
.FIELD_TYPE_ID_VARIANT
: VariantFieldType
,