Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field_class.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
5 from bt2 import native_bt, object, utils
6 import collections.abc
7 from bt2 import field_path as bt2_field_path
8 from bt2 import integer_range_set as bt2_integer_range_set
9 from bt2 import value as bt2_value
10 import bt2
11
12
13 def _create_field_class_from_ptr_and_get_ref_template(type_map, ptr):
14 typeid = native_bt.field_class_get_type(ptr)
15 return type_map[typeid]._create_from_ptr_and_get_ref(ptr)
16
17
18 def _create_field_class_from_ptr_and_get_ref(ptr):
19 return _create_field_class_from_ptr_and_get_ref_template(
20 _FIELD_CLASS_TYPE_TO_OBJ, ptr
21 )
22
23
24 def _create_field_class_from_const_ptr_and_get_ref(ptr):
25 return _create_field_class_from_ptr_and_get_ref_template(
26 _FIELD_CLASS_TYPE_TO_CONST_OBJ, ptr
27 )
28
29
30 class IntegerDisplayBase:
31 BINARY = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
32 OCTAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
33 DECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
34 HEXADECIMAL = native_bt.FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
35
36
37 class _FieldClassConst(object._SharedObject):
38 _get_ref = staticmethod(native_bt.field_class_get_ref)
39 _put_ref = staticmethod(native_bt.field_class_put_ref)
40 _borrow_user_attributes_ptr = staticmethod(
41 native_bt.field_class_borrow_user_attributes_const
42 )
43 _create_value_from_ptr_and_get_ref = staticmethod(
44 bt2_value._create_from_const_ptr_and_get_ref
45 )
46
47 def _check_create_status(self, ptr):
48 if ptr is None:
49 raise bt2._MemoryError(
50 'cannot create {} field class object'.format(self._NAME.lower())
51 )
52
53 @property
54 def user_attributes(self):
55 ptr = self._borrow_user_attributes_ptr(self._ptr)
56 assert ptr is not None
57 return self._create_value_from_ptr_and_get_ref(ptr)
58
59
60 class _FieldClass(_FieldClassConst):
61 _borrow_user_attributes_ptr = staticmethod(
62 native_bt.field_class_borrow_user_attributes
63 )
64 _create_value_from_ptr_and_get_ref = staticmethod(
65 bt2_value._create_from_ptr_and_get_ref
66 )
67
68 def _user_attributes(self, user_attributes):
69 value = bt2_value.create_value(user_attributes)
70 utils._check_type(value, bt2_value.MapValue)
71 native_bt.field_class_set_user_attributes(self._ptr, value._ptr)
72
73 _user_attributes = property(fset=_user_attributes)
74
75
76 class _BoolFieldClassConst(_FieldClassConst):
77 _NAME = 'Const boolean'
78
79
80 class _BoolFieldClass(_BoolFieldClassConst, _FieldClass):
81 _NAME = 'Boolean'
82
83
84 class _BitArrayFieldClassConst(_FieldClassConst):
85 _NAME = 'Const bit array'
86
87 @property
88 def length(self):
89 length = native_bt.field_class_bit_array_get_length(self._ptr)
90 assert length >= 1
91 return length
92
93
94 class _BitArrayFieldClass(_BitArrayFieldClassConst, _FieldClass):
95 _NAME = 'Bit array'
96
97
98 class _IntegerFieldClassConst(_FieldClassConst):
99 @property
100 def field_value_range(self):
101 size = native_bt.field_class_integer_get_field_value_range(self._ptr)
102 assert size >= 1
103 return size
104
105 @property
106 def preferred_display_base(self):
107 base = native_bt.field_class_integer_get_preferred_display_base(self._ptr)
108 assert base >= 0
109 return base
110
111
112 class _IntegerFieldClass(_FieldClass, _IntegerFieldClassConst):
113 def _field_value_range(self, size):
114 if size < 1 or size > 64:
115 raise ValueError("Value is outside valid range [1, 64] ({})".format(size))
116 native_bt.field_class_integer_set_field_value_range(self._ptr, size)
117
118 _field_value_range = property(fset=_field_value_range)
119
120 def _preferred_display_base(self, base):
121 utils._check_uint64(base)
122
123 if base not in (
124 IntegerDisplayBase.BINARY,
125 IntegerDisplayBase.OCTAL,
126 IntegerDisplayBase.DECIMAL,
127 IntegerDisplayBase.HEXADECIMAL,
128 ):
129 raise ValueError("Display base is not a valid IntegerDisplayBase value")
130
131 native_bt.field_class_integer_set_preferred_display_base(self._ptr, base)
132
133 _preferred_display_base = property(fset=_preferred_display_base)
134
135
136 class _UnsignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst):
137 _NAME = 'Const unsigned integer'
138
139
140 class _UnsignedIntegerFieldClass(
141 _UnsignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass
142 ):
143 _NAME = 'Unsigned integer'
144
145
146 class _SignedIntegerFieldClassConst(_IntegerFieldClassConst, _FieldClassConst):
147 _NAME = 'Const signed integer'
148
149
150 class _SignedIntegerFieldClass(
151 _SignedIntegerFieldClassConst, _IntegerFieldClass, _FieldClass
152 ):
153 _NAME = 'Signed integer'
154
155
156 class _RealFieldClassConst(_FieldClassConst):
157 pass
158
159
160 class _SinglePrecisionRealFieldClassConst(_RealFieldClassConst):
161 _NAME = 'Const single-precision real'
162
163
164 class _DoublePrecisionRealFieldClassConst(_RealFieldClassConst):
165 _NAME = 'Const double-precision real'
166
167
168 class _RealFieldClass(_FieldClass, _RealFieldClassConst):
169 pass
170
171
172 class _SinglePrecisionRealFieldClass(_RealFieldClass):
173 _NAME = 'Single-precision real'
174
175
176 class _DoublePrecisionRealFieldClass(_RealFieldClass):
177 _NAME = 'Double-precision real'
178
179
180 # an enumeration field class mapping does not have a reference count, so
181 # we copy the properties here to avoid eventual memory access errors.
182 class _EnumerationFieldClassMapping:
183 def __init__(self, mapping_ptr):
184 base_mapping_ptr = self._as_enumeration_field_class_mapping_ptr(mapping_ptr)
185 self._label = native_bt.field_class_enumeration_mapping_get_label(
186 base_mapping_ptr
187 )
188 assert self._label is not None
189 ranges_ptr = self._mapping_borrow_ranges_ptr(mapping_ptr)
190 assert ranges_ptr is not None
191 self._ranges = self._range_set_pycls._create_from_ptr_and_get_ref(ranges_ptr)
192
193 @property
194 def label(self):
195 return self._label
196
197 @property
198 def ranges(self):
199 return self._ranges
200
201
202 class _UnsignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping):
203 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
204 _as_enumeration_field_class_mapping_ptr = staticmethod(
205 native_bt.field_class_enumeration_unsigned_mapping_as_mapping_const
206 )
207 _mapping_borrow_ranges_ptr = staticmethod(
208 native_bt.field_class_enumeration_unsigned_mapping_borrow_ranges_const
209 )
210
211
212 class _SignedEnumerationFieldClassMappingConst(_EnumerationFieldClassMapping):
213 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
214 _as_enumeration_field_class_mapping_ptr = staticmethod(
215 native_bt.field_class_enumeration_signed_mapping_as_mapping_const
216 )
217 _mapping_borrow_ranges_ptr = staticmethod(
218 native_bt.field_class_enumeration_signed_mapping_borrow_ranges_const
219 )
220
221
222 class _EnumerationFieldClassConst(_IntegerFieldClassConst, collections.abc.Mapping):
223 def __len__(self):
224 count = native_bt.field_class_enumeration_get_mapping_count(self._ptr)
225 assert count >= 0
226 return count
227
228 def mappings_for_value(self, value):
229 self._check_int_type(value)
230
231 status, labels = self._get_mapping_labels_for_value(self._ptr, value)
232 utils._handle_func_status(
233 status, 'cannot get mapping labels for value {}'.format(value)
234 )
235 return [self[label] for label in labels]
236
237 def __iter__(self):
238 for idx in range(len(self)):
239 mapping_ptr = self._borrow_mapping_ptr_by_index(self._ptr, idx)
240 yield self._mapping_pycls(mapping_ptr).label
241
242 def __getitem__(self, label):
243 utils._check_str(label)
244 mapping_ptr = self._borrow_mapping_ptr_by_label(self._ptr, label)
245
246 if mapping_ptr is None:
247 raise KeyError(label)
248
249 return self._mapping_pycls(mapping_ptr)
250
251
252 class _EnumerationFieldClass(_EnumerationFieldClassConst, _IntegerFieldClass):
253 def add_mapping(self, label, ranges):
254 utils._check_str(label)
255 utils._check_type(ranges, self._range_set_pycls)
256
257 if label in self:
258 raise ValueError("duplicate mapping label '{}'".format(label))
259
260 status = self._add_mapping(self._ptr, label, ranges._ptr)
261 utils._handle_func_status(
262 status, 'cannot add mapping to enumeration field class object'
263 )
264
265 def __iadd__(self, mappings):
266 for label, ranges in mappings:
267 self.add_mapping(label, ranges)
268
269 return self
270
271
272 class _UnsignedEnumerationFieldClassConst(
273 _EnumerationFieldClassConst, _UnsignedIntegerFieldClassConst
274 ):
275 _NAME = 'Const unsigned enumeration'
276 _borrow_mapping_ptr_by_label = staticmethod(
277 native_bt.field_class_enumeration_unsigned_borrow_mapping_by_label_const
278 )
279 _borrow_mapping_ptr_by_index = staticmethod(
280 native_bt.field_class_enumeration_unsigned_borrow_mapping_by_index_const
281 )
282 _mapping_pycls = property(lambda _: _UnsignedEnumerationFieldClassMappingConst)
283 _get_mapping_labels_for_value = staticmethod(
284 native_bt.field_class_enumeration_unsigned_get_mapping_labels_for_value
285 )
286 _check_int_type = staticmethod(utils._check_uint64)
287
288
289 class _UnsignedEnumerationFieldClass(
290 _UnsignedEnumerationFieldClassConst,
291 _EnumerationFieldClass,
292 _UnsignedIntegerFieldClass,
293 ):
294 _NAME = 'Unsigned enumeration'
295 _range_set_pycls = bt2_integer_range_set.UnsignedIntegerRangeSet
296 _add_mapping = staticmethod(native_bt.field_class_enumeration_unsigned_add_mapping)
297
298
299 class _SignedEnumerationFieldClassConst(
300 _EnumerationFieldClassConst, _SignedIntegerFieldClassConst
301 ):
302 _NAME = 'Const signed enumeration'
303 _borrow_mapping_ptr_by_label = staticmethod(
304 native_bt.field_class_enumeration_signed_borrow_mapping_by_label_const
305 )
306 _borrow_mapping_ptr_by_index = staticmethod(
307 native_bt.field_class_enumeration_signed_borrow_mapping_by_index_const
308 )
309 _mapping_pycls = property(lambda _: _SignedEnumerationFieldClassMappingConst)
310 _get_mapping_labels_for_value = staticmethod(
311 native_bt.field_class_enumeration_signed_get_mapping_labels_for_value
312 )
313 _check_int_type = staticmethod(utils._check_int64)
314
315
316 class _SignedEnumerationFieldClass(
317 _SignedEnumerationFieldClassConst, _EnumerationFieldClass, _SignedIntegerFieldClass
318 ):
319 _NAME = 'Signed enumeration'
320 _range_set_pycls = bt2_integer_range_set.SignedIntegerRangeSet
321 _add_mapping = staticmethod(native_bt.field_class_enumeration_signed_add_mapping)
322
323
324 class _StringFieldClassConst(_FieldClassConst):
325 _NAME = 'Const string'
326
327
328 class _StringFieldClass(_StringFieldClassConst, _FieldClass):
329 _NAME = 'String'
330
331
332 class _StructureFieldClassMemberConst:
333 _create_field_class_from_ptr_and_get_ref = staticmethod(
334 _create_field_class_from_const_ptr_and_get_ref
335 )
336 _borrow_field_class_ptr = staticmethod(
337 native_bt.field_class_structure_member_borrow_field_class_const
338 )
339 _borrow_user_attributes_ptr = staticmethod(
340 native_bt.field_class_structure_member_borrow_user_attributes_const
341 )
342 _create_value_from_ptr_and_get_ref = staticmethod(
343 bt2_value._create_from_const_ptr_and_get_ref
344 )
345
346 def __init__(self, owning_struct_fc, member_ptr):
347 # this field class owns the member; keeping it here maintains
348 # the member alive as members are not shared objects
349 self._owning_struct_fc = owning_struct_fc
350 self._ptr = member_ptr
351
352 @property
353 def name(self):
354 name = native_bt.field_class_structure_member_get_name(self._ptr)
355 assert name is not None
356 return name
357
358 @property
359 def field_class(self):
360 fc_ptr = self._borrow_field_class_ptr(self._ptr)
361 assert fc_ptr is not None
362 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
363
364 @property
365 def user_attributes(self):
366 ptr = self._borrow_user_attributes_ptr(self._ptr)
367 assert ptr is not None
368 return self._create_value_from_ptr_and_get_ref(ptr)
369
370
371 class _StructureFieldClassMember(_StructureFieldClassMemberConst):
372 _borrow_field_class_ptr = staticmethod(
373 native_bt.field_class_structure_member_borrow_field_class
374 )
375 _borrow_user_attributes_ptr = staticmethod(
376 native_bt.field_class_structure_member_borrow_user_attributes
377 )
378 _create_field_class_from_ptr_and_get_ref = staticmethod(
379 _create_field_class_from_ptr_and_get_ref
380 )
381 _create_value_from_ptr_and_get_ref = staticmethod(
382 bt2_value._create_from_ptr_and_get_ref
383 )
384
385 def _user_attributes(self, user_attributes):
386 value = bt2_value.create_value(user_attributes)
387 utils._check_type(value, bt2_value.MapValue)
388 native_bt.field_class_structure_member_set_user_attributes(
389 self._ptr, value._ptr
390 )
391
392 _user_attributes = property(fset=_user_attributes)
393
394
395 class _StructureFieldClassConst(_FieldClassConst, collections.abc.Mapping):
396 _NAME = 'Const structure'
397 _borrow_member_ptr_by_index = staticmethod(
398 native_bt.field_class_structure_borrow_member_by_index_const
399 )
400 _borrow_member_ptr_by_name = staticmethod(
401 native_bt.field_class_structure_borrow_member_by_name_const
402 )
403 _structure_member_field_class_pycls = property(
404 lambda _: _StructureFieldClassMemberConst
405 )
406
407 def __len__(self):
408 count = native_bt.field_class_structure_get_member_count(self._ptr)
409 assert count >= 0
410 return count
411
412 def __getitem__(self, key):
413 if not isinstance(key, str):
414 raise TypeError(
415 "key must be a 'str' object, got '{}'".format(key.__class__.__name__)
416 )
417
418 member_ptr = self._borrow_member_ptr_by_name(self._ptr, key)
419
420 if member_ptr is None:
421 raise KeyError(key)
422
423 return self._structure_member_field_class_pycls(self, member_ptr)
424
425 def __iter__(self):
426 for idx in range(len(self)):
427 member_ptr = self._borrow_member_ptr_by_index(self._ptr, idx)
428 assert member_ptr is not None
429 yield native_bt.field_class_structure_member_get_name(member_ptr)
430
431 def member_at_index(self, index):
432 utils._check_uint64(index)
433
434 if index >= len(self):
435 raise IndexError
436
437 member_ptr = self._borrow_member_ptr_by_index(self._ptr, index)
438 assert member_ptr is not None
439 return self._structure_member_field_class_pycls(self, member_ptr)
440
441
442 class _StructureFieldClass(_StructureFieldClassConst, _FieldClass):
443 _NAME = 'Structure'
444 _borrow_member_by_index = staticmethod(
445 native_bt.field_class_structure_borrow_member_by_index
446 )
447 _borrow_member_ptr_by_name = staticmethod(
448 native_bt.field_class_structure_borrow_member_by_name
449 )
450 _structure_member_field_class_pycls = property(lambda _: _StructureFieldClassMember)
451
452 def append_member(self, name, field_class, user_attributes=None):
453 utils._check_str(name)
454 utils._check_type(field_class, _FieldClass)
455
456 if name in self:
457 raise ValueError("duplicate member name '{}'".format(name))
458
459 user_attributes_value = None
460
461 if user_attributes is not None:
462 # check now that user attributes are valid
463 user_attributes_value = bt2.create_value(user_attributes)
464
465 status = native_bt.field_class_structure_append_member(
466 self._ptr, name, field_class._ptr
467 )
468 utils._handle_func_status(
469 status, 'cannot append member to structure field class object'
470 )
471
472 if user_attributes is not None:
473 self[name]._user_attributes = user_attributes_value
474
475 def __iadd__(self, members):
476 for name, field_class in members:
477 self.append_member(name, field_class)
478
479 return self
480
481
482 class _OptionFieldClassConst(_FieldClassConst):
483 _NAME = 'Const option'
484 _create_field_class_from_ptr_and_get_ref = staticmethod(
485 _create_field_class_from_const_ptr_and_get_ref
486 )
487 _borrow_field_class_ptr = staticmethod(
488 native_bt.field_class_option_borrow_field_class_const
489 )
490
491 @property
492 def field_class(self):
493 elem_fc_ptr = self._borrow_field_class_ptr(self._ptr)
494 return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr)
495
496
497 class _OptionWithSelectorFieldClassConst(_OptionFieldClassConst):
498 _NAME = 'Const option (with selector)'
499
500 @property
501 def selector_field_path(self):
502 ptr = native_bt.field_class_option_with_selector_field_borrow_selector_field_path_const(
503 self._ptr
504 )
505 if ptr is None:
506 return
507
508 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
509
510
511 class _OptionWithBoolSelectorFieldClassConst(_OptionWithSelectorFieldClassConst):
512 _NAME = 'Const option (with boolean selector)'
513
514 @property
515 def selector_is_reversed(self):
516 return bool(
517 native_bt.field_class_option_with_selector_field_bool_selector_is_reversed(
518 self._ptr
519 )
520 )
521
522
523 class _OptionWithIntegerSelectorFieldClassConst(_OptionWithSelectorFieldClassConst):
524 _NAME = 'Const option (with integer selector)'
525
526 @property
527 def ranges(self):
528 range_set_ptr = self._borrow_selector_ranges_ptr(self._ptr)
529 assert range_set_ptr is not None
530 return self._range_set_pycls._create_from_ptr_and_get_ref(range_set_ptr)
531
532
533 class _OptionWithUnsignedIntegerSelectorFieldClassConst(
534 _OptionWithIntegerSelectorFieldClassConst
535 ):
536 _NAME = 'Const option (with unsigned integer selector)'
537 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
538 _borrow_selector_ranges_ptr = staticmethod(
539 native_bt.field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const
540 )
541
542
543 class _OptionWithSignedIntegerSelectorFieldClassConst(
544 _OptionWithIntegerSelectorFieldClassConst
545 ):
546 _NAME = 'Const option (with signed integer selector)'
547 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
548 _borrow_selector_ranges_ptr = staticmethod(
549 native_bt.field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const
550 )
551
552
553 class _OptionFieldClass(_OptionFieldClassConst, _FieldClass):
554 _NAME = 'Option'
555 _borrow_field_class_ptr = staticmethod(
556 native_bt.field_class_option_borrow_field_class
557 )
558 _create_field_class_from_ptr_and_get_ref = staticmethod(
559 _create_field_class_from_ptr_and_get_ref
560 )
561
562
563 class _OptionWithSelectorFieldClass(
564 _OptionWithSelectorFieldClassConst, _OptionFieldClass
565 ):
566 _NAME = 'Option (with selector)'
567
568
569 class _OptionWithBoolSelectorFieldClass(
570 _OptionWithBoolSelectorFieldClassConst, _OptionWithSelectorFieldClass
571 ):
572 _NAME = 'Option (with boolean selector)'
573
574 def _selector_is_reversed(self, selector_is_reversed):
575 utils._check_bool(selector_is_reversed)
576 native_bt.field_class_option_with_selector_field_bool_set_selector_is_reversed(
577 self._ptr, selector_is_reversed
578 )
579
580 _selector_is_reversed = property(fset=_selector_is_reversed)
581
582
583 class _OptionWithIntegerSelectorFieldClass(
584 _OptionWithIntegerSelectorFieldClassConst, _OptionWithSelectorFieldClass
585 ):
586 _NAME = 'Option (with integer selector)'
587
588
589 class _OptionWithUnsignedIntegerSelectorFieldClass(
590 _OptionWithUnsignedIntegerSelectorFieldClassConst,
591 _OptionWithIntegerSelectorFieldClass,
592 ):
593 _NAME = 'Option (with unsigned integer selector)'
594
595
596 class _OptionWithSignedIntegerSelectorFieldClass(
597 _OptionWithSignedIntegerSelectorFieldClassConst,
598 _OptionWithIntegerSelectorFieldClass,
599 ):
600 _NAME = 'Option (with signed integer selector)'
601
602
603 class _VariantFieldClassOptionConst:
604 _create_field_class_from_ptr_and_get_ref = staticmethod(
605 _create_field_class_from_const_ptr_and_get_ref
606 )
607 _borrow_field_class_ptr = staticmethod(
608 native_bt.field_class_variant_option_borrow_field_class_const
609 )
610 _borrow_user_attributes_ptr = staticmethod(
611 native_bt.field_class_variant_option_borrow_user_attributes_const
612 )
613 _create_value_from_ptr_and_get_ref = staticmethod(
614 bt2_value._create_from_const_ptr_and_get_ref
615 )
616
617 def __init__(self, owning_var_fc, option_ptr):
618 # this field class owns the option; keeping it here maintains
619 # the option alive as options are not shared objects
620 self._owning_var_fc = owning_var_fc
621 self._ptr = option_ptr
622
623 @property
624 def name(self):
625 name = native_bt.field_class_variant_option_get_name(self._ptr)
626 assert name is not None
627 return name
628
629 @property
630 def field_class(self):
631 fc_ptr = self._borrow_field_class_ptr(self._ptr)
632 assert fc_ptr is not None
633 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
634
635 @property
636 def user_attributes(self):
637 ptr = self._borrow_user_attributes_ptr(self._ptr)
638 assert ptr is not None
639 return self._create_value_from_ptr_and_get_ref(ptr)
640
641
642 class _VariantFieldClassOption(_VariantFieldClassOptionConst):
643 _create_field_class_from_ptr_and_get_ref = staticmethod(
644 _create_field_class_from_ptr_and_get_ref
645 )
646 _borrow_field_class_ptr = staticmethod(
647 native_bt.field_class_variant_option_borrow_field_class
648 )
649 _borrow_user_attributes_ptr = staticmethod(
650 native_bt.field_class_variant_option_borrow_user_attributes
651 )
652 _create_value_from_ptr_and_get_ref = staticmethod(
653 bt2_value._create_from_ptr_and_get_ref
654 )
655
656 def _user_attributes(self, user_attributes):
657 value = bt2_value.create_value(user_attributes)
658 utils._check_type(value, bt2_value.MapValue)
659 native_bt.field_class_variant_option_set_user_attributes(self._ptr, value._ptr)
660
661 _user_attributes = property(fset=_user_attributes)
662
663
664 class _VariantFieldClassWithIntegerSelectorOptionConst(_VariantFieldClassOptionConst):
665 def __init__(self, owning_var_fc, spec_opt_ptr):
666 self._spec_ptr = spec_opt_ptr
667 super().__init__(owning_var_fc, self._as_option_ptr(spec_opt_ptr))
668
669 @property
670 def ranges(self):
671 range_set_ptr = self._borrow_ranges_ptr(self._spec_ptr)
672 assert range_set_ptr is not None
673 return self._range_set_pycls._create_from_ptr_and_get_ref(range_set_ptr)
674
675
676 class _VariantFieldClassWithIntegerSelectorOption(
677 _VariantFieldClassWithIntegerSelectorOptionConst, _VariantFieldClassOption
678 ):
679 pass
680
681
682 class _VariantFieldClassWithSignedIntegerSelectorOptionConst(
683 _VariantFieldClassWithIntegerSelectorOptionConst
684 ):
685 _as_option_ptr = staticmethod(
686 native_bt.field_class_variant_with_selector_field_integer_signed_option_as_option_const
687 )
688 _borrow_ranges_ptr = staticmethod(
689 native_bt.field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const
690 )
691 _range_set_pycls = bt2_integer_range_set._SignedIntegerRangeSetConst
692
693
694 class _VariantFieldClassWithSignedIntegerSelectorOption(
695 _VariantFieldClassWithSignedIntegerSelectorOptionConst,
696 _VariantFieldClassWithIntegerSelectorOption,
697 ):
698 pass
699
700
701 class _VariantFieldClassWithUnsignedIntegerSelectorOptionConst(
702 _VariantFieldClassWithIntegerSelectorOptionConst
703 ):
704 _as_option_ptr = staticmethod(
705 native_bt.field_class_variant_with_selector_field_integer_unsigned_option_as_option_const
706 )
707 _borrow_ranges_ptr = staticmethod(
708 native_bt.field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const
709 )
710 _range_set_pycls = bt2_integer_range_set._UnsignedIntegerRangeSetConst
711
712
713 class _VariantFieldClassWithUnsignedIntegerSelectorOption(
714 _VariantFieldClassWithUnsignedIntegerSelectorOptionConst,
715 _VariantFieldClassWithIntegerSelectorOption,
716 ):
717 pass
718
719
720 class _VariantFieldClassConst(_FieldClassConst, collections.abc.Mapping):
721 _NAME = 'Const variant'
722 _borrow_option_ptr_by_name = staticmethod(
723 native_bt.field_class_variant_borrow_option_by_name_const
724 )
725 _borrow_option_ptr_by_index = staticmethod(
726 native_bt.field_class_variant_borrow_option_by_index_const
727 )
728 _variant_option_pycls = _VariantFieldClassOptionConst
729
730 @staticmethod
731 def _as_option_ptr(opt_ptr):
732 return opt_ptr
733
734 def _create_option_from_ptr(self, opt_ptr):
735 return self._variant_option_pycls(self, opt_ptr)
736
737 def __len__(self):
738 count = native_bt.field_class_variant_get_option_count(self._ptr)
739 assert count >= 0
740 return count
741
742 def __getitem__(self, key):
743 if not isinstance(key, str):
744 raise TypeError(
745 "key must be a 'str' object, got '{}'".format(key.__class__.__name__)
746 )
747
748 opt_ptr = self._borrow_option_ptr_by_name(self._ptr, key)
749
750 if opt_ptr is None:
751 raise KeyError(key)
752
753 return self._create_option_from_ptr(opt_ptr)
754
755 def __iter__(self):
756 for idx in range(len(self)):
757 opt_ptr = self._borrow_option_ptr_by_index(self._ptr, idx)
758 assert opt_ptr is not None
759 base_opt_ptr = self._as_option_ptr(opt_ptr)
760 yield native_bt.field_class_variant_option_get_name(base_opt_ptr)
761
762 def option_at_index(self, index):
763 utils._check_uint64(index)
764
765 if index >= len(self):
766 raise IndexError
767
768 opt_ptr = self._borrow_option_ptr_by_index(self._ptr, index)
769 assert opt_ptr is not None
770 return self._create_option_from_ptr(opt_ptr)
771
772
773 class _VariantFieldClass(_VariantFieldClassConst, _FieldClass, collections.abc.Mapping):
774 _NAME = 'Variant'
775 _borrow_option_ptr_by_name = staticmethod(
776 native_bt.field_class_variant_borrow_option_by_name
777 )
778 _borrow_option_ptr_by_index = staticmethod(
779 native_bt.field_class_variant_borrow_option_by_index
780 )
781 _variant_option_pycls = _VariantFieldClassOption
782
783
784 class _VariantFieldClassWithoutSelectorConst(_VariantFieldClassConst):
785 _NAME = 'Const variant (without selector)'
786
787
788 class _VariantFieldClassWithoutSelector(
789 _VariantFieldClassWithoutSelectorConst, _VariantFieldClass
790 ):
791 _NAME = 'Variant (without selector)'
792
793 def append_option(self, name, field_class, user_attributes=None):
794 utils._check_str(name)
795 utils._check_type(field_class, _FieldClass)
796
797 if name in self:
798 raise ValueError("duplicate option name '{}'".format(name))
799
800 user_attributes_value = None
801
802 if user_attributes is not None:
803 # check now that user attributes are valid
804 user_attributes_value = bt2.create_value(user_attributes)
805
806 status = native_bt.field_class_variant_without_selector_append_option(
807 self._ptr, name, field_class._ptr
808 )
809 utils._handle_func_status(
810 status, 'cannot append option to variant field class object'
811 )
812
813 if user_attributes is not None:
814 self[name]._user_attributes = user_attributes_value
815
816 def __iadd__(self, options):
817 for name, field_class in options:
818 self.append_option(name, field_class)
819
820 return self
821
822
823 class _VariantFieldClassWithIntegerSelectorConst(_VariantFieldClassConst):
824 _NAME = 'Const variant (with selector)'
825
826 @property
827 def selector_field_path(self):
828 ptr = native_bt.field_class_variant_with_selector_field_borrow_selector_field_path_const(
829 self._ptr
830 )
831
832 if ptr is None:
833 return
834
835 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
836
837
838 class _VariantFieldClassWithIntegerSelector(
839 _VariantFieldClassWithIntegerSelectorConst, _VariantFieldClass
840 ):
841 _NAME = 'Variant (with selector)'
842
843 def append_option(self, name, field_class, ranges, user_attributes=None):
844 utils._check_str(name)
845 utils._check_type(field_class, _FieldClass)
846 utils._check_type(ranges, self._variant_option_pycls._range_set_pycls)
847
848 if name in self:
849 raise ValueError("duplicate option name '{}'".format(name))
850
851 if len(ranges) == 0:
852 raise ValueError('range set is empty')
853
854 user_attributes_value = None
855
856 if user_attributes is not None:
857 # check now that user attributes are valid
858 user_attributes_value = bt2.create_value(user_attributes)
859
860 # TODO: check overlaps (precondition of self._append_option())
861
862 status = self._append_option(self._ptr, name, field_class._ptr, ranges._ptr)
863 utils._handle_func_status(
864 status, 'cannot append option to variant field class object'
865 )
866
867 if user_attributes is not None:
868 self[name]._user_attributes = user_attributes_value
869
870 def __iadd__(self, options):
871 for name, field_class, ranges in options:
872 self.append_option(name, field_class, ranges)
873
874 return self
875
876
877 class _VariantFieldClassWithUnsignedIntegerSelectorConst(
878 _VariantFieldClassWithIntegerSelectorConst
879 ):
880 _NAME = 'Const variant (with unsigned integer selector)'
881 _borrow_option_ptr_by_name = staticmethod(
882 native_bt.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const
883 )
884 _borrow_option_ptr_by_index = staticmethod(
885 native_bt.field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const
886 )
887 _variant_option_pycls = _VariantFieldClassWithUnsignedIntegerSelectorOptionConst
888 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
889
890
891 class _VariantFieldClassWithUnsignedIntegerSelector(
892 _VariantFieldClassWithUnsignedIntegerSelectorConst,
893 _VariantFieldClassWithIntegerSelector,
894 ):
895 _NAME = 'Variant (with unsigned integer selector)'
896 _variant_option_pycls = _VariantFieldClassWithUnsignedIntegerSelectorOption
897 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
898 _append_option = staticmethod(
899 native_bt.field_class_variant_with_selector_field_integer_unsigned_append_option
900 )
901
902
903 class _VariantFieldClassWithSignedIntegerSelectorConst(
904 _VariantFieldClassWithIntegerSelectorConst
905 ):
906 _NAME = 'Const variant (with signed integer selector)'
907 _borrow_option_ptr_by_name = staticmethod(
908 native_bt.field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const
909 )
910 _borrow_option_ptr_by_index = staticmethod(
911 native_bt.field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const
912 )
913 _variant_option_pycls = _VariantFieldClassWithSignedIntegerSelectorOptionConst
914 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
915
916
917 class _VariantFieldClassWithSignedIntegerSelector(
918 _VariantFieldClassWithSignedIntegerSelectorConst,
919 _VariantFieldClassWithIntegerSelector,
920 ):
921 _NAME = 'Variant (with signed integer selector)'
922 _variant_option_pycls = _VariantFieldClassWithSignedIntegerSelectorOption
923 _as_option_ptr = staticmethod(_variant_option_pycls._as_option_ptr)
924 _append_option = staticmethod(
925 native_bt.field_class_variant_with_selector_field_integer_signed_append_option
926 )
927
928
929 class _ArrayFieldClassConst(_FieldClassConst):
930 _create_field_class_from_ptr_and_get_ref = staticmethod(
931 _create_field_class_from_const_ptr_and_get_ref
932 )
933 _borrow_element_field_class = staticmethod(
934 native_bt.field_class_array_borrow_element_field_class_const
935 )
936
937 @property
938 def element_field_class(self):
939 elem_fc_ptr = self._borrow_element_field_class(self._ptr)
940 return self._create_field_class_from_ptr_and_get_ref(elem_fc_ptr)
941
942
943 class _ArrayFieldClass(_ArrayFieldClassConst, _FieldClass):
944 _create_field_class_from_ptr_and_get_ref = staticmethod(
945 _create_field_class_from_ptr_and_get_ref
946 )
947 _borrow_element_field_class = staticmethod(
948 native_bt.field_class_array_borrow_element_field_class
949 )
950
951
952 class _StaticArrayFieldClassConst(_ArrayFieldClassConst):
953 _NAME = 'Const static array'
954
955 @property
956 def length(self):
957 return native_bt.field_class_array_static_get_length(self._ptr)
958
959
960 class _StaticArrayFieldClass(_StaticArrayFieldClassConst, _ArrayFieldClass):
961 _NAME = 'Static array'
962
963
964 class _DynamicArrayFieldClassConst(_ArrayFieldClassConst):
965 _NAME = 'Const dynamic array'
966
967
968 class _DynamicArrayWithLengthFieldFieldClassConst(_DynamicArrayFieldClassConst):
969 _NAME = 'Const dynamic array (with length field)'
970
971 @property
972 def length_field_path(self):
973 ptr = native_bt.field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
974 self._ptr
975 )
976 if ptr is None:
977 return
978
979 return bt2_field_path._FieldPathConst._create_from_ptr_and_get_ref(ptr)
980
981
982 class _DynamicArrayFieldClass(_DynamicArrayFieldClassConst, _ArrayFieldClass):
983 _NAME = 'Dynamic array'
984
985
986 class _DynamicArrayWithLengthFieldFieldClass(
987 _DynamicArrayWithLengthFieldFieldClassConst, _DynamicArrayFieldClass
988 ):
989 _NAME = 'Dynamic array (with length field)'
990
991
992 _FIELD_CLASS_TYPE_TO_CONST_OBJ = {
993 native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClassConst,
994 native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClassConst,
995 native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClassConst,
996 native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClassConst,
997 native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealFieldClassConst,
998 native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealFieldClassConst,
999 native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClassConst,
1000 native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClassConst,
1001 native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClassConst,
1002 native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClassConst,
1003 native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClassConst,
1004 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayFieldClassConst,
1005 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayWithLengthFieldFieldClassConst,
1006 native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionFieldClassConst,
1007 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionWithBoolSelectorFieldClassConst,
1008 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionWithUnsignedIntegerSelectorFieldClassConst,
1009 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionWithSignedIntegerSelectorFieldClassConst,
1010 native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantFieldClassWithoutSelectorConst,
1011 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithUnsignedIntegerSelectorConst,
1012 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithSignedIntegerSelectorConst,
1013 }
1014
1015 _FIELD_CLASS_TYPE_TO_OBJ = {
1016 native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClass,
1017 native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClass,
1018 native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass,
1019 native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass,
1020 native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealFieldClass,
1021 native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealFieldClass,
1022 native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldClass,
1023 native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldClass,
1024 native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldClass,
1025 native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldClass,
1026 native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldClass,
1027 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayFieldClass,
1028 native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayWithLengthFieldFieldClass,
1029 native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionFieldClass,
1030 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionWithBoolSelectorFieldClass,
1031 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionWithUnsignedIntegerSelectorFieldClass,
1032 native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionWithSignedIntegerSelectorFieldClass,
1033 native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantFieldClassWithoutSelector,
1034 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithUnsignedIntegerSelector,
1035 native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldClassWithSignedIntegerSelector,
1036 }
This page took 0.086272 seconds and 4 git commands to generate.