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_field_class_from_ptr_and_get_ref(ptr
):
31 typeid
= native_bt
.field_class_get_type(ptr
)
32 return _FIELD_CLASS_TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
35 class _FieldClass(object._SharedObject
, metaclass
=abc
.ABCMeta
):
36 _get_ref
= staticmethod(native_bt
.field_class_get_ref
)
37 _put_ref
= staticmethod(native_bt
.field_class_put_ref
)
39 def __init__(self
, ptr
):
42 def __eq__(self
, other
):
43 if not isinstance(other
, self
.__class
__):
44 # not comparing apples to apples
47 if self
.addr
== other
.addr
:
50 ret
= native_bt
.field_class_compare(self
._ptr
, other
._ptr
)
51 utils
._handle
_ret
(ret
, "cannot compare field classes")
54 def _check_create_status(self
, ptr
):
56 raise bt2
.CreationError('cannot create {} field class object'.format(self
._NAME
.lower()))
59 ptr
= native_bt
.field_class_copy(self
._ptr
)
60 utils
._handle
_ptr
(ptr
, 'cannot copy {} field class object'.format(self
._NAME
.lower()))
61 return _create_from_ptr(ptr
)
63 def __deepcopy__(self
, memo
):
68 def __call__(self
, value
=None):
69 field_ptr
= native_bt
.field_create(self
._ptr
)
72 raise bt2
.CreationError('cannot create {} field object'.format(self
._NAME
.lower()))
74 field
= bt2
.field
._create
_from
_ptr
(field_ptr
)
77 if not isinstance(field
, (bt2
.field
._IntegerField
, bt2
.field
._FloatingPointNumberField
, bt2
.field
._StringField
)):
78 raise bt2
.Error('cannot assign an initial value to a {} field object'.format(field
._NAME
))
88 alignment
= native_bt
.field_class_get_alignment(self
._ptr
)
89 assert(alignment
>= 0)
93 def alignment(self
, alignment
):
94 utils
._check
_alignment
(alignment
)
95 ret
= native_bt
.field_class_set_alignment(self
._ptr
, alignment
)
96 utils
._handle
_ret
(ret
, "cannot set field class object's alignment")
101 def byte_order(self
):
102 bo
= native_bt
.field_class_get_byte_order(self
._ptr
)
107 def byte_order(self
, byte_order
):
108 utils
._check
_int
(byte_order
)
109 ret
= native_bt
.field_class_set_byte_order(self
._ptr
, byte_order
)
110 utils
._handle
_ret
(ret
, "cannot set field class object's byte order")
113 class IntegerFieldClass(_FieldClass
, _AlignmentProp
, _ByteOrderProp
):
116 def __init__(self
, size
, alignment
=None, byte_order
=None, is_signed
=None,
117 base
=None, encoding
=None, mapped_clock_class
=None):
118 utils
._check
_uint
64(size
)
121 raise ValueError('size is 0 bits')
123 ptr
= native_bt
.field_class_integer_create(size
)
124 self
._check
_create
_status
(ptr
)
125 super().__init
__(ptr
)
127 if alignment
is not None:
128 self
.alignment
= alignment
130 if byte_order
is not None:
131 self
.byte_order
= byte_order
133 if is_signed
is not None:
134 self
.is_signed
= is_signed
139 if encoding
is not None:
140 self
.encoding
= encoding
142 if mapped_clock_class
is not None:
143 self
.mapped_clock_class
= mapped_clock_class
147 size
= native_bt
.field_class_integer_get_size(self
._ptr
)
153 is_signed
= native_bt
.field_class_integer_is_signed(self
._ptr
)
154 assert(is_signed
>= 0)
158 def is_signed(self
, is_signed
):
159 utils
._check
_bool
(is_signed
)
160 ret
= native_bt
.field_class_integer_set_is_signed(self
._ptr
, int(is_signed
))
161 utils
._handle
_ret
(ret
, "cannot set integer field class object's signedness")
165 base
= native_bt
.field_class_integer_get_base(self
._ptr
)
170 def base(self
, base
):
171 utils
._check
_int
(base
)
172 ret
= native_bt
.field_class_integer_set_base(self
._ptr
, base
)
173 utils
._handle
_ret
(ret
, "cannot set integer field class object's base")
177 encoding
= native_bt
.field_class_integer_get_encoding(self
._ptr
)
178 assert(encoding
>= 0)
182 def encoding(self
, encoding
):
183 utils
._check
_int
(encoding
)
184 ret
= native_bt
.field_class_integer_set_encoding(self
._ptr
, encoding
)
185 utils
._handle
_ret
(ret
, "cannot set integer field class object's encoding")
188 def mapped_clock_class(self
):
189 ptr
= native_bt
.field_class_integer_get_mapped_clock_class(self
._ptr
)
194 return bt2
.ClockClass
._create
_from
_ptr
(ptr
)
196 @mapped_clock_class.setter
197 def mapped_clock_class(self
, clock_class
):
198 utils
._check
_type
(clock_class
, bt2
.ClockClass
)
199 ret
= native_bt
.field_class_integer_set_mapped_clock_class(self
._ptr
, clock_class
._ptr
)
200 utils
._handle
_ret
(ret
, "cannot set integer field class object's mapped clock class")
203 class FloatingPointNumberFieldClass(_FieldClass
, _AlignmentProp
, _ByteOrderProp
):
204 _NAME
= 'Floating point number'
206 def __init__(self
, alignment
=None, byte_order
=None, exponent_size
=None,
208 ptr
= native_bt
.field_class_floating_point_create()
209 self
._check
_create
_status
(ptr
)
210 super().__init
__(ptr
)
212 if alignment
is not None:
213 self
.alignment
= alignment
215 if byte_order
is not None:
216 self
.byte_order
= byte_order
218 if exponent_size
is not None:
219 self
.exponent_size
= exponent_size
221 if mantissa_size
is not None:
222 self
.mantissa_size
= mantissa_size
225 def exponent_size(self
):
226 exp_size
= native_bt
.field_class_floating_point_get_exponent_digits(self
._ptr
)
227 assert(exp_size
>= 0)
230 @exponent_size.setter
231 def exponent_size(self
, exponent_size
):
232 utils
._check
_uint
64(exponent_size
)
233 ret
= native_bt
.field_class_floating_point_set_exponent_digits(self
._ptr
, exponent_size
)
234 utils
._handle
_ret
(ret
, "cannot set floating point number field class object's exponent size")
237 def mantissa_size(self
):
238 mant_size
= native_bt
.field_class_floating_point_get_mantissa_digits(self
._ptr
)
239 assert(mant_size
>= 0)
242 @mantissa_size.setter
243 def mantissa_size(self
, mantissa_size
):
244 utils
._check
_uint
64(mantissa_size
)
245 ret
= native_bt
.field_class_floating_point_set_mantissa_digits(self
._ptr
, mantissa_size
)
246 utils
._handle
_ret
(ret
, "cannot set floating point number field class object's mantissa size")
249 class _EnumerationFieldClassMapping
:
250 def __init__(self
, name
, lower
, upper
):
267 def __eq__(self
, other
):
268 if type(other
) is not self
.__class
__:
271 return (self
.name
, self
.lower
, self
.upper
) == (other
.name
, other
.lower
, other
.upper
)
274 class _EnumerationFieldClassMappingIterator(object._SharedObject
,
275 collections
.abc
.Iterator
):
276 def __init__(self
, iter_ptr
, is_signed
):
277 super().__init
__(iter_ptr
)
278 self
._is
_signed
= is_signed
279 self
._done
= (iter_ptr
is None)
285 ret
= native_bt
.field_class_enumeration_mapping_iterator_next(self
._ptr
)
291 ret
, name
, lower
, upper
= native_bt
.field_class_enumeration_mapping_iterator_get_signed(self
._ptr
)
293 ret
, name
, lower
, upper
= native_bt
.field_class_enumeration_mapping_iterator_get_unsigned(self
._ptr
)
296 mapping
= _EnumerationFieldClassMapping(name
, lower
, upper
)
301 class EnumerationFieldClass(IntegerFieldClass
, collections
.abc
.Sequence
):
302 _NAME
= 'Enumeration'
304 def __init__(self
, int_field_class
=None, size
=None, alignment
=None,
305 byte_order
=None, is_signed
=None, base
=None, encoding
=None,
306 mapped_clock_class
=None):
307 if int_field_class
is None:
308 int_field_class
= IntegerFieldClass(size
=size
, alignment
=alignment
,
309 byte_order
=byte_order
,
310 is_signed
=is_signed
, base
=base
,
312 mapped_clock_class
=mapped_clock_class
)
314 utils
._check
_type
(int_field_class
, IntegerFieldClass
)
315 ptr
= native_bt
.field_class_enumeration_create(int_field_class
._ptr
)
316 self
._check
_create
_status
(ptr
)
317 _FieldClass
.__init
__(self
, ptr
)
320 def integer_field_class(self
):
321 ptr
= native_bt
.field_class_enumeration_get_container_type(self
._ptr
)
323 return _create_from_ptr(ptr
)
327 return self
.integer_field_class
.size
331 return self
.integer_field_class
.alignment
334 def alignment(self
, alignment
):
335 self
.integer_field_class
.alignment
= alignment
338 def byte_order(self
):
339 return self
.integer_field_class
.byte_order
342 def byte_order(self
, byte_order
):
343 self
.integer_field_class
.byte_order
= byte_order
347 return self
.integer_field_class
.is_signed
350 def is_signed(self
, is_signed
):
351 self
.integer_field_class
.is_signed
= is_signed
355 return self
.integer_field_class
.base
358 def base(self
, base
):
359 self
.integer_field_class
.base
= base
363 return self
.integer_field_class
.encoding
366 def encoding(self
, encoding
):
367 self
.integer_field_class
.encoding
= encoding
370 def mapped_clock_class(self
):
371 return self
.integer_field_class
.mapped_clock_class
373 @mapped_clock_class.setter
374 def mapped_clock_class(self
, mapped_clock_class
):
375 self
.integer_field_class
.mapped_clock_class
= mapped_clock_class
378 count
= native_bt
.field_class_enumeration_get_mapping_count(self
._ptr
)
382 def __getitem__(self
, index
):
383 utils
._check
_uint
64(index
)
385 if index
>= len(self
):
389 get_fn
= native_bt
.field_class_enumeration_get_mapping_signed
391 get_fn
= native_bt
.field_class_enumeration_get_mapping_unsigned
393 ret
, name
, lower
, upper
= get_fn(self
._ptr
, index
)
395 return _EnumerationFieldClassMapping(name
, lower
, upper
)
397 def _get_mapping_iter(self
, iter_ptr
):
398 return _EnumerationFieldClassMappingIterator(iter_ptr
, self
.is_signed
)
400 def mappings_by_name(self
, name
):
401 utils
._check
_str
(name
)
402 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_name(self
._ptr
, name
)
403 print('iter_ptr', iter_ptr
)
404 return self
._get
_mapping
_iter
(iter_ptr
)
406 def mappings_by_value(self
, value
):
408 utils
._check
_int
64(value
)
409 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_signed_value(self
._ptr
, value
)
411 utils
._check
_uint
64(value
)
412 iter_ptr
= native_bt
.field_class_enumeration_find_mappings_by_unsigned_value(self
._ptr
, value
)
414 return self
._get
_mapping
_iter
(iter_ptr
)
416 def add_mapping(self
, name
, lower
, upper
=None):
417 utils
._check
_str
(name
)
423 add_fn
= native_bt
.field_class_enumeration_add_mapping_signed
424 utils
._check
_int
64(lower
)
425 utils
._check
_int
64(upper
)
427 add_fn
= native_bt
.field_class_enumeration_add_mapping_unsigned
428 utils
._check
_uint
64(lower
)
429 utils
._check
_uint
64(upper
)
431 ret
= add_fn(self
._ptr
, name
, lower
, upper
)
432 utils
._handle
_ret
(ret
, "cannot add mapping to enumeration field class object")
434 def __iadd__(self
, mappings
):
435 for mapping
in mappings
:
436 self
.add_mapping(mapping
.name
, mapping
.lower
, mapping
.upper
)
441 class StringFieldClass(_FieldClass
):
444 def __init__(self
, encoding
=None):
445 ptr
= native_bt
.field_class_string_create()
446 self
._check
_create
_status
(ptr
)
447 super().__init
__(ptr
)
449 if encoding
is not None:
450 self
.encoding
= encoding
454 encoding
= native_bt
.field_class_string_get_encoding(self
._ptr
)
455 assert(encoding
>= 0)
459 def encoding(self
, encoding
):
460 utils
._check
_int
(encoding
)
461 ret
= native_bt
.field_class_string_set_encoding(self
._ptr
, encoding
)
462 utils
._handle
_ret
(ret
, "cannot set string field class object's encoding")
465 class _FieldContainer(collections
.abc
.Mapping
):
467 count
= self
._count
()
471 def __getitem__(self
, key
):
472 if not isinstance(key
, str):
473 raise TypeError("'{}' is not a 'str' object".format(key
.__class
__.__name
__))
475 ptr
= self
._get
_field
_by
_name
(key
)
480 return _create_from_ptr(ptr
)
483 return self
._ITER
_CLS
(self
)
485 def append_field(self
, name
, field_class
):
486 utils
._check
_str
(name
)
487 utils
._check
_type
(field_class
, _FieldClass
)
488 ret
= self
._add
_field
(field_class
._ptr
, name
)
489 utils
._handle
_ret
(ret
, "cannot add field to {} field class object".format(self
._NAME
.lower()))
491 def __iadd__(self
, fields
):
492 for name
, field_class
in fields
.items():
493 self
.append_field(name
, field_class
)
497 def at_index(self
, index
):
498 utils
._check
_uint
64(index
)
499 return self
._at
(index
)
502 class _StructureFieldClassFieldIterator(collections
.abc
.Iterator
):
503 def __init__(self
, struct_field_class
):
504 self
._struct
_field
_class
= struct_field_class
508 if self
._at
== len(self
._struct
_field
_class
):
511 get_fc_by_index
= native_bt
.field_class_structure_get_field_by_index
512 ret
, name
, field_class_ptr
= get_fc_by_index(self
._struct
_field
_class
._ptr
,
515 native_bt
.put(field_class_ptr
)
520 class _StructureFieldClass(_FieldClass
, _FieldContainer
, _AlignmentProp
):
522 _ITER_CLS
= _StructureFieldClassFieldIterator
524 def __init__(self
, min_alignment
=None):
525 ptr
= native_bt
.field_class_structure_create()
526 self
._check
_create
_status
(ptr
)
527 super().__init
__(ptr
)
529 if min_alignment
is not None:
530 self
.min_alignment
= min_alignment
533 return native_bt
.field_class_structure_get_field_count(self
._ptr
)
535 def _get_field_by_name(self
, key
):
536 return native_bt
.field_class_structure_get_field_class_by_name(self
._ptr
, key
)
538 def _add_field(self
, ptr
, name
):
539 return native_bt
.field_class_structure_add_field(self
._ptr
, ptr
,
542 def _at(self
, index
):
543 if index
< 0 or index
>= len(self
):
546 ret
, name
, field_class_ptr
= native_bt
.field_class_structure_get_field_by_index(self
._ptr
, index
)
548 return _create_from_ptr(field_class_ptr
)
551 _StructureFieldClass
.min_alignment
= property(fset
=_StructureFieldClass
.alignment
.fset
)
552 _StructureFieldClass
.alignment
= property(fget
=_StructureFieldClass
.alignment
.fget
)
555 class _VariantFieldClassFieldIterator(collections
.abc
.Iterator
):
556 def __init__(self
, variant_field_class
):
557 self
._variant
_field
_class
= variant_field_class
561 if self
._at
== len(self
._variant
_field
_class
):
564 ret
, name
, field_class_ptr
= native_bt
.field_class_variant_get_field_by_index(self
._variant
_field
_class
._ptr
,
567 native_bt
.put(field_class_ptr
)
572 class VariantFieldClass(_FieldClass
, _FieldContainer
, _AlignmentProp
):
574 _ITER_CLS
= _VariantFieldClassFieldIterator
576 def __init__(self
, tag_name
, tag_field_class
=None):
577 utils
._check
_str
(tag_name
)
579 if tag_field_class
is None:
582 utils
._check
_type
(tag_field_class
, EnumerationFieldClass
)
583 tag_fc_ptr
= tag_field_class
._ptr
585 ptr
= native_bt
.field_class_variant_create(tag_fc_ptr
,
587 self
._check
_create
_status
(ptr
)
588 super().__init
__(ptr
)
592 tag_name
= native_bt
.field_class_variant_get_tag_name(self
._ptr
)
593 assert(tag_name
is not None)
597 def tag_name(self
, tag_name
):
598 utils
._check
_str
(tag_name
)
599 ret
= native_bt
.field_class_variant_set_tag_name(self
._ptr
, tag_name
)
600 utils
._handle
_ret
(ret
, "cannot set variant field class object's tag name")
603 def tag_field_class(self
):
604 fc_ptr
= native_bt
.field_class_variant_get_tag_type(self
._ptr
)
609 return _create_from_ptr(fc_ptr
)
612 return native_bt
.field_class_variant_get_field_count(self
._ptr
)
614 def _get_field_by_name(self
, key
):
615 return native_bt
.field_class_variant_get_field_class_by_name(self
._ptr
, key
)
617 def _add_field(self
, ptr
, name
):
618 return native_bt
.field_class_variant_add_field(self
._ptr
, ptr
, name
)
620 def _at(self
, index
):
621 if index
< 0 or index
>= len(self
):
624 ret
, name
, field_class_ptr
= native_bt
.field_class_variant_get_field_by_index(self
._ptr
, index
)
626 return _create_from_ptr(field_class_ptr
)
629 class ArrayFieldClass(_FieldClass
):
632 def __init__(self
, element_field_class
, length
):
633 utils
._check
_type
(element_field_class
, _FieldClass
)
634 utils
._check
_uint
64(length
)
635 ptr
= native_bt
.field_class_array_create(element_field_class
._ptr
, length
)
636 self
._check
_create
_status
(ptr
)
637 super().__init
__(ptr
)
641 length
= native_bt
.field_class_array_get_length(self
._ptr
)
646 def element_field_class(self
):
647 ptr
= native_bt
.field_class_array_get_element_type(self
._ptr
)
649 return _create_from_ptr(ptr
)
652 class SequenceFieldClass(_FieldClass
):
655 def __init__(self
, element_field_class
, length_name
):
656 utils
._check
_type
(element_field_class
, _FieldClass
)
657 utils
._check
_str
(length_name
)
658 ptr
= native_bt
.field_class_sequence_create(element_field_class
._ptr
,
660 self
._check
_create
_status
(ptr
)
661 super().__init
__(ptr
)
664 def length_name(self
):
665 length_name
= native_bt
.field_class_sequence_get_length_field_name(self
._ptr
)
666 assert(length_name
is not None)
670 def element_field_class(self
):
671 ptr
= native_bt
.field_class_sequence_get_element_type(self
._ptr
)
673 return _create_from_ptr(ptr
)
676 _FIELD_CLASS_TYPE_TO_OBJ
= {
677 native_bt
.FIELD_CLASS_TYPE_STRUCTURE
: _StructureFieldClass
,