1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
14 def _create_from_ptr_template(ptr
, object_map
):
18 # bt_value_null is translated to None. However, we are given a reference
19 # to it that we are not going to manage anymore, since we don't create a
20 # Python wrapper for it. Therefore put that reference immediately.
21 if ptr
== native_bt
.value_null
:
25 typeid
= native_bt
.value_get_type(ptr
)
26 return object_map
[typeid
]._create
_from
_ptr
(ptr
)
29 def _create_from_ptr(ptr
):
30 return _create_from_ptr_template(ptr
, _TYPE_TO_OBJ
)
33 def _create_from_const_ptr(ptr
):
34 return _create_from_ptr_template(ptr
, _TYPE_TO_CONST_OBJ
)
37 def _create_from_ptr_and_get_ref_template(ptr
, object_map
):
38 if ptr
is None or ptr
== native_bt
.value_null
:
41 typeid
= native_bt
.value_get_type(ptr
)
42 return object_map
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
45 def _create_from_ptr_and_get_ref(ptr
):
46 return _create_from_ptr_and_get_ref_template(ptr
, _TYPE_TO_OBJ
)
49 def _create_from_const_ptr_and_get_ref(ptr
):
50 return _create_from_ptr_and_get_ref_template(ptr
, _TYPE_TO_CONST_OBJ
)
53 def create_value(value
):
58 if isinstance(value
, _Value
):
61 if isinstance(value
, bool):
62 return BoolValue(value
)
64 if isinstance(value
, numbers
.Integral
):
65 return SignedIntegerValue(value
)
67 if isinstance(value
, numbers
.Real
):
68 return RealValue(value
)
70 if isinstance(value
, str):
71 return StringValue(value
)
73 if isinstance(value
, collections
.abc
.Sequence
):
74 return ArrayValue(value
)
76 if isinstance(value
, collections
.abc
.Mapping
):
77 return MapValue(value
)
80 "cannot create value object from '{}' object".format(value
.__class
__.__name
__)
84 class _ValueConst(object._SharedObject
, metaclass
=abc
.ABCMeta
):
87 native_bt
.value_get_ref(ptr
)
91 native_bt
.value_put_ref(ptr
)
93 _create_value_from_ptr
= staticmethod(_create_from_const_ptr
)
94 _create_value_from_ptr_and_get_ref
= staticmethod(
95 _create_from_const_ptr_and_get_ref
98 def __ne__(self
, other
):
99 return not (self
== other
)
101 def _check_create_status(self
, ptr
):
103 raise bt2
._MemoryError(
104 "cannot create {} value object".format(self
._NAME
.lower())
108 class _Value(_ValueConst
):
109 _create_value_from_ptr
= staticmethod(_create_from_ptr
)
110 _create_value_from_ptr_and_get_ref
= staticmethod(_create_from_ptr_and_get_ref
)
113 @functools.total_ordering
114 class _NumericValueConst(_ValueConst
):
116 def _extract_value(other
):
117 if isinstance(other
, _BoolValueConst
) or isinstance(other
, bool):
120 if isinstance(other
, numbers
.Integral
):
123 if isinstance(other
, numbers
.Real
):
126 if isinstance(other
, numbers
.Complex
):
127 return complex(other
)
130 "'{}' object is not a number object".format(other
.__class
__.__name
__)
134 return int(self
._value
)
137 return float(self
._value
)
140 return repr(self
._value
)
142 def __lt__(self
, other
):
143 return self
._value
< self
._extract
_value
(other
)
145 def __eq__(self
, other
):
147 return self
._value
== self
._extract
_value
(other
)
151 def __rmod__(self
, other
):
152 return self
._extract
_value
(other
) % self
._value
154 def __mod__(self
, other
):
155 return self
._value
% self
._extract
_value
(other
)
157 def __rfloordiv__(self
, other
):
158 return self
._extract
_value
(other
) // self
._value
160 def __floordiv__(self
, other
):
161 return self
._value
// self
._extract
_value
(other
)
163 def __round__(self
, ndigits
=None):
165 return round(self
._value
)
167 return round(self
._value
, ndigits
)
170 return math
.ceil(self
._value
)
173 return math
.floor(self
._value
)
176 return int(self
._value
)
179 return abs(self
._value
)
181 def __add__(self
, other
):
182 return self
._value
+ self
._extract
_value
(other
)
184 def __radd__(self
, other
):
185 return self
.__add
__(other
)
193 def __mul__(self
, other
):
194 return self
._value
* self
._extract
_value
(other
)
196 def __rmul__(self
, other
):
197 return self
.__mul
__(other
)
199 def __truediv__(self
, other
):
200 return self
._value
/ self
._extract
_value
(other
)
202 def __rtruediv__(self
, other
):
203 return self
._extract
_value
(other
) / self
._value
205 def __pow__(self
, exponent
):
206 return self
._value
** self
._extract
_value
(exponent
)
208 def __rpow__(self
, base
):
209 return self
._extract
_value
(base
) ** self
._value
212 class _NumericValue(_NumericValueConst
, _Value
):
216 class _IntegralValueConst(_NumericValueConst
, numbers
.Integral
):
217 def __lshift__(self
, other
):
218 return self
._value
<< self
._extract
_value
(other
)
220 def __rlshift__(self
, other
):
221 return self
._extract
_value
(other
) << self
._value
223 def __rshift__(self
, other
):
224 return self
._value
>> self
._extract
_value
(other
)
226 def __rrshift__(self
, other
):
227 return self
._extract
_value
(other
) >> self
._value
229 def __and__(self
, other
):
230 return self
._value
& self
._extract
_value
(other
)
232 def __rand__(self
, other
):
233 return self
._extract
_value
(other
) & self
._value
235 def __xor__(self
, other
):
236 return self
._value ^ self
._extract
_value
(other
)
238 def __rxor__(self
, other
):
239 return self
._extract
_value
(other
) ^ self
._value
241 def __or__(self
, other
):
242 return self
._value | self
._extract
_value
(other
)
244 def __ror__(self
, other
):
245 return self
._extract
_value
(other
) | self
._value
247 def __invert__(self
):
251 class _IntegralValue(_IntegralValueConst
, _NumericValue
):
255 class _BoolValueConst(_IntegralValueConst
):
256 _NAME
= "Const boolean"
262 return repr(self
._value
)
266 value
= native_bt
.value_bool_get(self
._ptr
)
270 class BoolValue(_BoolValueConst
, _IntegralValue
):
273 def __init__(self
, value
=None):
275 ptr
= native_bt
.value_bool_create()
277 ptr
= native_bt
.value_bool_create_init(self
._value
_to
_bool
(value
))
279 self
._check
_create
_status
(ptr
)
280 super().__init
__(ptr
)
283 def _value_to_bool(cls
, value
):
284 if isinstance(value
, _BoolValueConst
):
287 if not isinstance(value
, bool):
289 "'{}' object is not a 'bool', 'BoolValue', or '_BoolValueConst' object".format(
296 def _set_value(self
, value
):
297 native_bt
.value_bool_set(self
._ptr
, self
._value
_to
_bool
(value
))
299 value
= property(fset
=_set_value
)
302 class _IntegerValueConst(_IntegralValueConst
):
305 return self
._get
_value
(self
._ptr
)
308 class _IntegerValue(_IntegerValueConst
, _IntegralValue
):
309 def __init__(self
, value
=None):
311 ptr
= self
._create
_default
_value
()
313 ptr
= self
._create
_value
(self
._value
_to
_int
(value
))
315 self
._check
_create
_status
(ptr
)
316 super().__init
__(ptr
)
319 def _value_to_int(cls
, value
):
320 if not isinstance(value
, numbers
.Integral
):
321 raise TypeError("expecting an integral number object")
324 cls
._check
_int
_range
(value
)
327 def _prop_set_value(self
, value
):
328 self
._set
_value
(self
._ptr
, self
._value
_to
_int
(value
))
330 value
= property(fset
=_prop_set_value
)
333 class _UnsignedIntegerValueConst(_IntegerValueConst
):
334 _NAME
= "Const unsigned integer"
335 _get_value
= staticmethod(native_bt
.value_integer_unsigned_get
)
338 class UnsignedIntegerValue(_UnsignedIntegerValueConst
, _IntegerValue
):
339 _NAME
= "Unsigned integer"
340 _check_int_range
= staticmethod(utils
._check
_uint
64)
341 _create_default_value
= staticmethod(native_bt
.value_integer_unsigned_create
)
342 _create_value
= staticmethod(native_bt
.value_integer_unsigned_create_init
)
343 _set_value
= staticmethod(native_bt
.value_integer_unsigned_set
)
346 class _SignedIntegerValueConst(_IntegerValueConst
):
347 _NAME
= "Const signed integer"
348 _get_value
= staticmethod(native_bt
.value_integer_signed_get
)
351 class SignedIntegerValue(_SignedIntegerValueConst
, _IntegerValue
):
352 _NAME
= "Signed integer"
353 _check_int_range
= staticmethod(utils
._check
_int
64)
354 _create_default_value
= staticmethod(native_bt
.value_integer_signed_create
)
355 _create_value
= staticmethod(native_bt
.value_integer_signed_create_init
)
356 _set_value
= staticmethod(native_bt
.value_integer_signed_set
)
359 class _RealValueConst(_NumericValueConst
, numbers
.Real
):
360 _NAME
= "Const real number"
364 return native_bt
.value_real_get(self
._ptr
)
367 class RealValue(_RealValueConst
, _NumericValue
):
368 _NAME
= "Real number"
370 def __init__(self
, value
=None):
372 ptr
= native_bt
.value_real_create()
374 value
= self
._value
_to
_float
(value
)
375 ptr
= native_bt
.value_real_create_init(value
)
377 self
._check
_create
_status
(ptr
)
378 super().__init
__(ptr
)
381 def _value_to_float(cls
, value
):
382 if not isinstance(value
, numbers
.Real
):
383 raise TypeError("expecting a real number object")
387 def _set_value(self
, value
):
388 native_bt
.value_real_set(self
._ptr
, self
._value
_to
_float
(value
))
390 value
= property(fset
=_set_value
)
393 @functools.total_ordering
394 class _StringValueConst(collections
.abc
.Sequence
, _Value
):
395 _NAME
= "Const string"
398 def _value_to_str(cls
, value
):
399 if isinstance(value
, _StringValueConst
):
402 utils
._check
_str
(value
)
407 return native_bt
.value_string_get(self
._ptr
)
409 def __eq__(self
, other
):
411 return self
._value
== self
._value
_to
_str
(other
)
415 def __lt__(self
, other
):
416 return self
._value
< self
._value
_to
_str
(other
)
419 return bool(self
._value
)
422 return repr(self
._value
)
427 def __getitem__(self
, index
):
428 return self
._value
[index
]
431 return len(self
._value
)
433 def __contains__(self
, item
):
434 return self
._value
_to
_str
(item
) in self
._value
437 class StringValue(_StringValueConst
, _Value
):
440 def __init__(self
, value
=None):
442 ptr
= native_bt
.value_string_create()
444 ptr
= native_bt
.value_string_create_init(self
._value
_to
_str
(value
))
446 self
._check
_create
_status
(ptr
)
447 super().__init
__(ptr
)
449 def _set_value(self
, value
):
450 status
= native_bt
.value_string_set(self
._ptr
, self
._value
_to
_str
(value
))
451 utils
._handle
_func
_status
(status
)
453 value
= property(fset
=_set_value
)
455 def __iadd__(self
, value
):
456 curvalue
= self
._value
457 curvalue
+= self
._value
_to
_str
(value
)
458 self
.value
= curvalue
462 class _ContainerConst
:
464 return len(self
) != 0
467 class _Container(_ContainerConst
):
468 def __delitem__(self
, index
):
469 raise NotImplementedError
472 class _ArrayValueConst(_ContainerConst
, collections
.abc
.Sequence
, _ValueConst
):
473 _NAME
= "Const array"
474 _borrow_element_by_index
= staticmethod(
475 native_bt
.value_array_borrow_element_by_index_const
479 def __eq__(self
, other
):
480 if not isinstance(other
, collections
.abc
.Sequence
):
483 if len(self
) != len(other
):
487 for self_elem
, other_elem
in zip(self
, other
):
488 if self_elem
!= other_elem
:
494 size
= native_bt
.value_array_get_length(self
._ptr
)
498 def _check_index(self
, index
):
499 # TODO: support slices also
500 if not isinstance(index
, numbers
.Integral
):
502 "'{}' object is not an integral number object: invalid index".format(
503 index
.__class
__.__name
__
509 if index
< 0 or index
>= len(self
):
510 raise IndexError("array value object index is out of range")
512 def __getitem__(self
, index
):
513 self
._check
_index
(index
)
514 ptr
= self
._borrow
_element
_by
_index
(self
._ptr
, index
)
516 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
519 return "[{}]".format(", ".join([repr(v
) for v
in self
]))
522 class ArrayValue(_ArrayValueConst
, _Container
, collections
.abc
.MutableSequence
, _Value
):
524 _borrow_element_by_index
= staticmethod(
525 native_bt
.value_array_borrow_element_by_index
528 def __init__(self
, value
=None):
529 ptr
= native_bt
.value_array_create()
530 self
._check
_create
_status
(ptr
)
531 super().__init
__(ptr
)
533 # Python will raise a TypeError if there's anything wrong with
534 # the iterable protocol.
535 if value
is not None:
539 def __setitem__(self
, index
, value
):
540 self
._check
_index
(index
)
541 value
= create_value(value
)
544 ptr
= native_bt
.value_null
548 status
= native_bt
.value_array_set_element_by_index(self
._ptr
, index
, ptr
)
549 utils
._handle
_func
_status
(status
)
551 def append(self
, value
):
552 value
= create_value(value
)
555 ptr
= native_bt
.value_null
559 status
= native_bt
.value_array_append_element(self
._ptr
, ptr
)
560 utils
._handle
_func
_status
(status
)
562 def __iadd__(self
, iterable
):
563 # Python will raise a TypeError if there's anything wrong with
564 # the iterable protocol.
565 for elem
in iterable
:
570 def insert(self
, value
):
571 raise NotImplementedError
574 class _MapValueKeyIterator(collections
.abc
.Iterator
):
575 def __init__(self
, map_obj
):
576 self
._map
_obj
= map_obj
578 keys_ptr
= native_bt
.value_map_get_keys(map_obj
._ptr
)
581 raise RuntimeError("unexpected error: cannot get map value object keys")
583 self
._keys
= _create_from_ptr(keys_ptr
)
586 if self
._at
== len(self
._map
_obj
):
589 key
= self
._keys
[self
._at
]
594 class _MapValueConst(_ContainerConst
, collections
.abc
.Mapping
, _ValueConst
):
596 _borrow_entry_value_ptr
= staticmethod(native_bt
.value_map_borrow_entry_value_const
)
598 def __ne__(self
, other
):
599 return _Value
.__ne
__(self
, other
)
601 def __eq__(self
, other
):
602 if not isinstance(other
, collections
.abc
.Mapping
):
605 if len(self
) != len(other
):
609 for self_key
in self
:
610 if self_key
not in other
:
613 if self
[self_key
] != other
[self_key
]:
619 size
= native_bt
.value_map_get_size(self
._ptr
)
623 def __contains__(self
, key
):
624 self
._check
_key
_type
(key
)
625 return native_bt
.value_map_has_entry(self
._ptr
, key
)
627 def _check_key_type(self
, key
):
628 utils
._check
_str
(key
)
630 def _check_key(self
, key
):
634 def __getitem__(self
, key
):
636 ptr
= self
._borrow
_entry
_value
_ptr
(self
._ptr
, key
)
638 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
641 return _MapValueKeyIterator(self
)
644 items
= ["{}: {}".format(repr(k
), repr(v
)) for k
, v
in self
.items()]
645 return "{{{}}}".format(", ".join(items
))
648 class MapValue(_MapValueConst
, _Container
, collections
.abc
.MutableMapping
, _Value
):
650 _borrow_entry_value_ptr
= staticmethod(native_bt
.value_map_borrow_entry_value
)
652 def __init__(self
, value
=None):
653 ptr
= native_bt
.value_map_create()
654 self
._check
_create
_status
(ptr
)
655 super().__init
__(ptr
)
657 # Python will raise a TypeError if there's anything wrong with
658 # the iterable/mapping protocol.
659 if value
is not None:
660 for key
, elem
in value
.items():
663 def __setitem__(self
, key
, value
):
664 self
._check
_key
_type
(key
)
665 value
= create_value(value
)
668 ptr
= native_bt
.value_null
672 status
= native_bt
.value_map_insert_entry(self
._ptr
, key
, ptr
)
673 utils
._handle
_func
_status
(status
)
677 native_bt
.VALUE_TYPE_BOOL
: BoolValue
,
678 native_bt
.VALUE_TYPE_UNSIGNED_INTEGER
: UnsignedIntegerValue
,
679 native_bt
.VALUE_TYPE_SIGNED_INTEGER
: SignedIntegerValue
,
680 native_bt
.VALUE_TYPE_REAL
: RealValue
,
681 native_bt
.VALUE_TYPE_STRING
: StringValue
,
682 native_bt
.VALUE_TYPE_ARRAY
: ArrayValue
,
683 native_bt
.VALUE_TYPE_MAP
: MapValue
,
686 _TYPE_TO_CONST_OBJ
= {
687 native_bt
.VALUE_TYPE_BOOL
: _BoolValueConst
,
688 native_bt
.VALUE_TYPE_UNSIGNED_INTEGER
: _UnsignedIntegerValueConst
,
689 native_bt
.VALUE_TYPE_SIGNED_INTEGER
: _SignedIntegerValueConst
,
690 native_bt
.VALUE_TYPE_REAL
: _RealValueConst
,
691 native_bt
.VALUE_TYPE_STRING
: _StringValueConst
,
692 native_bt
.VALUE_TYPE_ARRAY
: _ArrayValueConst
,
693 native_bt
.VALUE_TYPE_MAP
: _MapValueConst
,