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
32 def _handle_status(status
, obj_name
):
36 raise RuntimeError('unexpected error')
39 def _create_from_ptr(ptr
):
40 if ptr
is None or ptr
== native_bt
.value_null
:
43 typeid
= native_bt
.value_get_type(ptr
)
44 return _TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
(ptr
)
47 def _create_from_ptr_and_get_ref(ptr
):
48 if ptr
is None or ptr
== native_bt
.value_null
:
51 typeid
= native_bt
.value_get_type(ptr
)
52 return _TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
55 def create_value(value
):
60 if isinstance(value
, _Value
):
63 if isinstance(value
, bool):
64 return BoolValue(value
)
66 if isinstance(value
, int):
67 return SignedIntegerValue(value
)
69 if isinstance(value
, float):
70 return RealValue(value
)
72 if isinstance(value
, str):
73 return StringValue(value
)
76 return MapValue(value
)
81 return ArrayValue(value
)
85 raise TypeError("cannot create value object from '{}' object".format(value
.__class
__.__name
__))
88 class _Value(object._SharedObject
, metaclass
=abc
.ABCMeta
):
89 _get_ref
= staticmethod(native_bt
.value_get_ref
)
90 _put_ref
= staticmethod(native_bt
.value_put_ref
)
92 def __ne__(self
, other
):
93 return not (self
== other
)
95 def _handle_status(self
, status
):
96 _handle_status(status
, self
._NAME
)
98 def _check_create_status(self
, ptr
):
100 raise bt2
.CreationError(
101 'cannot create {} value object'.format(self
._NAME
.lower()))
104 @functools.total_ordering
105 class _NumericValue(_Value
):
107 def _extract_value(other
):
108 if isinstance(other
, BoolValue
) or isinstance(other
, bool):
111 if isinstance(other
, numbers
.Integral
):
114 if isinstance(other
, numbers
.Real
):
117 if isinstance(other
, numbers
.Complex
):
118 return complex(other
)
120 raise TypeError("'{}' object is not a number object".format(other
.__class
__.__name
__))
123 return int(self
._value
)
126 return float(self
._value
)
129 return repr(self
._value
)
131 def __lt__(self
, other
):
132 return self
._value
< self
._extract
_value
(other
)
134 def __eq__(self
, other
):
136 return self
._value
== self
._extract
_value
(other
)
140 def __rmod__(self
, other
):
141 return self
._extract
_value
(other
) % self
._value
143 def __mod__(self
, other
):
144 return self
._value
% self
._extract
_value
(other
)
146 def __rfloordiv__(self
, other
):
147 return self
._extract
_value
(other
) // self
._value
149 def __floordiv__(self
, other
):
150 return self
._value
// self
._extract
_value
(other
)
152 def __round__(self
, ndigits
=None):
154 return round(self
._value
)
156 return round(self
._value
, ndigits
)
159 return math
.ceil(self
._value
)
162 return math
.floor(self
._value
)
165 return int(self
._value
)
168 return abs(self
._value
)
170 def __add__(self
, other
):
171 return self
._value
+ self
._extract
_value
(other
)
173 def __radd__(self
, other
):
174 return self
.__add
__(other
)
182 def __mul__(self
, other
):
183 return self
._value
* self
._extract
_value
(other
)
185 def __rmul__(self
, other
):
186 return self
.__mul
__(other
)
188 def __truediv__(self
, other
):
189 return self
._value
/ self
._extract
_value
(other
)
191 def __rtruediv__(self
, other
):
192 return self
._extract
_value
(other
) / self
._value
194 def __pow__(self
, exponent
):
195 return self
._value
** self
._extract
_value
(exponent
)
197 def __rpow__(self
, base
):
198 return self
._extract
_value
(base
) ** self
._value
200 def __iadd__(self
, other
):
201 self
.value
= self
+ other
204 def __isub__(self
, other
):
205 self
.value
= self
- other
208 def __imul__(self
, other
):
209 self
.value
= self
* other
212 def __itruediv__(self
, other
):
213 self
.value
= self
/ other
216 def __ifloordiv__(self
, other
):
217 self
.value
= self
// other
220 def __imod__(self
, other
):
221 self
.value
= self
% other
224 def __ipow__(self
, other
):
225 self
.value
= self
** other
229 class _IntegralValue(_NumericValue
, numbers
.Integral
):
230 def __lshift__(self
, other
):
231 return self
._value
<< self
._extract
_value
(other
)
233 def __rlshift__(self
, other
):
234 return self
._extract
_value
(other
) << self
._value
236 def __rshift__(self
, other
):
237 return self
._value
>> self
._extract
_value
(other
)
239 def __rrshift__(self
, other
):
240 return self
._extract
_value
(other
) >> self
._value
242 def __and__(self
, other
):
243 return self
._value
& self
._extract
_value
(other
)
245 def __rand__(self
, other
):
246 return self
._extract
_value
(other
) & self
._value
248 def __xor__(self
, other
):
249 return self
._value ^ self
._extract
_value
(other
)
251 def __rxor__(self
, other
):
252 return self
._extract
_value
(other
) ^ self
._value
254 def __or__(self
, other
):
255 return self
._value | self
._extract
_value
(other
)
257 def __ror__(self
, other
):
258 return self
._extract
_value
(other
) | self
._value
260 def __invert__(self
):
263 def __ilshift__(self
, other
):
264 self
.value
= self
<< other
267 def __irshift__(self
, other
):
268 self
.value
= self
>> other
271 def __iand__(self
, other
):
272 self
.value
= self
& other
275 def __ixor__(self
, other
):
276 self
.value
= self ^ other
279 def __ior__(self
, other
):
280 self
.value
= self | other
284 class _RealValue(_NumericValue
, numbers
.Real
):
288 class BoolValue(_IntegralValue
):
291 def __init__(self
, value
=None):
293 ptr
= native_bt
.value_bool_create()
295 ptr
= native_bt
.value_bool_create_init(self
._value
_to
_bool
(value
))
297 self
._check
_create
_status
(ptr
)
298 super().__init
__(ptr
)
304 return repr(self
._value
)
306 def _value_to_bool(self
, value
):
307 if isinstance(value
, BoolValue
):
310 if not isinstance(value
, bool):
311 raise TypeError("'{}' object is not a 'bool' or 'BoolValue' object".format(value
.__class
__))
317 value
= native_bt
.value_bool_get(self
._ptr
)
320 def _set_value(self
, value
):
321 native_bt
.value_bool_set(self
._ptr
, self
._value
_to
_bool
(value
))
323 value
= property(fset
=_set_value
)
326 class _IntegerValue(_IntegralValue
):
327 def __init__(self
, value
=None):
329 ptr
= self
._create
_default
_value
()
331 ptr
= self
._create
_value
(self
._value
_to
_int
(value
))
333 self
._check
_create
_status
(ptr
)
334 super().__init
__(ptr
)
336 def _value_to_int(self
, value
):
337 if not isinstance(value
, numbers
.Real
):
338 raise TypeError('expecting a number object')
341 self
._check
_int
_range
(value
)
346 return self
._get
_value
(self
._ptr
)
348 def _prop_set_value(self
, value
):
349 self
._set
_value
(self
._ptr
, self
._value
_to
_int
(value
))
351 value
= property(fset
=_prop_set_value
)
354 class UnsignedIntegerValue(_IntegerValue
):
355 _check_int_range
= staticmethod(utils
._check
_uint
64)
356 _create_default_value
= staticmethod(native_bt
.value_unsigned_integer_create
)
357 _create_value
= staticmethod(native_bt
.value_unsigned_integer_create_init
)
358 _set_value
= staticmethod(native_bt
.value_unsigned_integer_set
)
359 _get_value
= staticmethod(native_bt
.value_unsigned_integer_get
)
362 class SignedIntegerValue(_IntegerValue
):
363 _check_int_range
= staticmethod(utils
._check
_int
64)
364 _create_default_value
= staticmethod(native_bt
.value_signed_integer_create
)
365 _create_value
= staticmethod(native_bt
.value_signed_integer_create_init
)
366 _set_value
= staticmethod(native_bt
.value_signed_integer_set
)
367 _get_value
= staticmethod(native_bt
.value_signed_integer_get
)
370 class RealValue(_RealValue
):
371 _NAME
= 'Real number'
373 def __init__(self
, value
=None):
375 ptr
= native_bt
.value_real_create()
377 value
= self
._value
_to
_float
(value
)
378 ptr
= native_bt
.value_real_create_init(value
)
380 self
._check
_create
_status
(ptr
)
381 super().__init
__(ptr
)
383 def _value_to_float(self
, value
):
384 if not isinstance(value
, numbers
.Real
):
385 raise TypeError("expecting a real number object")
391 return native_bt
.value_real_get(self
._ptr
)
393 def _set_value(self
, value
):
394 native_bt
.value_real_set(self
._ptr
, self
._value
_to
_float
(value
))
396 value
= property(fset
=_set_value
)
399 @functools.total_ordering
400 class StringValue(collections
.abc
.Sequence
, _Value
):
403 def __init__(self
, value
=None):
405 ptr
= native_bt
.value_string_create()
407 ptr
= native_bt
.value_string_create_init(self
._value
_to
_str
(value
))
409 self
._check
_create
_status
(ptr
)
410 super().__init
__(ptr
)
412 def _value_to_str(self
, value
):
413 if isinstance(value
, self
.__class
__):
416 utils
._check
_str
(value
)
421 return native_bt
.value_string_get(self
._ptr
)
423 def _set_value(self
, value
):
424 status
= native_bt
.value_string_set(self
._ptr
, self
._value
_to
_str
(value
))
425 self
._handle
_status
(status
)
427 value
= property(fset
=_set_value
)
429 def __eq__(self
, other
):
431 return self
._value
== self
._value
_to
_str
(other
)
435 def __lt__(self
, other
):
436 return self
._value
< self
._value
_to
_str
(other
)
439 return bool(self
._value
)
442 return repr(self
._value
)
447 def __getitem__(self
, index
):
448 return self
._value
[index
]
451 return len(self
._value
)
453 def __iadd__(self
, value
):
454 curvalue
= self
._value
455 curvalue
+= self
._value
_to
_str
(value
)
456 self
.value
= curvalue
462 return len(self
) != 0
464 def __delitem__(self
, index
):
465 raise NotImplementedError
468 class ArrayValue(_Container
, collections
.abc
.MutableSequence
, _Value
):
471 def __init__(self
, value
=None):
472 ptr
= native_bt
.value_array_create()
473 self
._check
_create
_status
(ptr
)
474 super().__init
__(ptr
)
476 # Python will raise a TypeError if there's anything wrong with
477 # the iterable protocol.
478 if value
is not None:
482 def __eq__(self
, other
):
483 if not isinstance(other
, collections
.abc
.Sequence
):
486 if len(self
) != len(other
):
490 for self_elem
, other_elem
in zip(self
, other
):
491 if self_elem
!= other_elem
:
497 size
= native_bt
.value_array_get_size(self
._ptr
)
501 def _check_index(self
, index
):
502 # TODO: support slices also
503 if not isinstance(index
, numbers
.Integral
):
504 raise TypeError("'{}' object is not an integral number object: invalid index".format(index
.__class
__.__name
__))
508 if index
< 0 or index
>= len(self
):
509 raise IndexError('array value object index is out of range')
511 def __getitem__(self
, index
):
512 self
._check
_index
(index
)
513 ptr
= native_bt
.value_array_borrow_element_by_index(self
._ptr
, index
)
515 return _create_from_ptr_and_get_ref(ptr
)
517 def __setitem__(self
, index
, value
):
518 self
._check
_index
(index
)
519 value
= create_value(value
)
522 ptr
= native_bt
.value_null
526 status
= native_bt
.value_array_set_element_by_index(
527 self
._ptr
, index
, ptr
)
528 self
._handle
_status
(status
)
530 def append(self
, value
):
531 value
= create_value(value
)
534 ptr
= native_bt
.value_null
538 status
= native_bt
.value_array_append_element(self
._ptr
, ptr
)
539 self
._handle
_status
(status
)
541 def __iadd__(self
, iterable
):
542 # Python will raise a TypeError if there's anything wrong with
543 # the iterable protocol.
544 for elem
in iterable
:
550 return '[{}]'.format(', '.join([repr(v
) for v
in self
]))
552 def insert(self
, value
):
553 raise NotImplementedError
556 class _MapValueKeyIterator(collections
.abc
.Iterator
):
557 def __init__(self
, map_obj
):
558 self
._map
_obj
= map_obj
560 keys_ptr
= native_bt
.value_map_get_keys(map_obj
._ptr
)
563 raise RuntimeError('unexpected error: cannot get map value object keys')
565 self
._keys
= _create_from_ptr(keys_ptr
)
568 if self
._at
== len(self
._map
_obj
):
571 key
= self
._keys
[self
._at
]
576 class MapValue(_Container
, collections
.abc
.MutableMapping
, _Value
):
579 def __init__(self
, value
=None):
580 ptr
= native_bt
.value_map_create()
581 self
._check
_create
_status
(ptr
)
582 super().__init
__(ptr
)
584 # Python will raise a TypeError if there's anything wrong with
585 # the iterable/mapping protocol.
586 if value
is not None:
587 for key
, elem
in value
.items():
590 def __ne__(self
, other
):
591 return _Value
.__ne
__(self
, other
)
593 def __eq__(self
, other
):
594 if not isinstance(other
, collections
.abc
.Mapping
):
597 if len(self
) != len(other
):
601 for self_key
in self
:
602 if self_key
not in other
:
605 if self
[self_key
] != other
[self_key
]:
611 size
= native_bt
.value_map_get_size(self
._ptr
)
615 def __contains__(self
, key
):
616 self
._check
_key
_type
(key
)
617 return native_bt
.value_map_has_entry(self
._ptr
, key
)
619 def _check_key_type(self
, key
):
620 utils
._check
_str
(key
)
622 def _check_key(self
, key
):
626 def __getitem__(self
, key
):
628 ptr
= native_bt
.value_map_borrow_entry_value(self
._ptr
, key
)
630 return _create_from_ptr_and_get_ref(ptr
)
633 return _MapValueKeyIterator(self
)
635 def __setitem__(self
, key
, value
):
636 self
._check
_key
_type
(key
)
637 value
= create_value(value
)
640 ptr
= native_bt
.value_null
644 status
= native_bt
.value_map_insert_entry(self
._ptr
, key
, ptr
)
645 self
._handle
_status
(status
)
648 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in self
.items()]
649 return '{{{}}}'.format(', '.join(items
))
653 native_bt
.VALUE_TYPE_BOOL
: BoolValue
,
654 native_bt
.VALUE_TYPE_UNSIGNED_INTEGER
: UnsignedIntegerValue
,
655 native_bt
.VALUE_TYPE_SIGNED_INTEGER
: SignedIntegerValue
,
656 native_bt
.VALUE_TYPE_REAL
: RealValue
,
657 native_bt
.VALUE_TYPE_STRING
: StringValue
,
658 native_bt
.VALUE_TYPE_ARRAY
: ArrayValue
,
659 native_bt
.VALUE_TYPE_MAP
: MapValue
,