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 _create_from_ptr(ptr
):
33 if ptr
is None or ptr
== native_bt
.value_null
:
36 typeid
= native_bt
.value_get_type(ptr
)
37 return _TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
(ptr
)
40 def _create_from_ptr_and_get_ref(ptr
):
41 if ptr
is None or ptr
== native_bt
.value_null
:
44 typeid
= native_bt
.value_get_type(ptr
)
45 return _TYPE_TO_OBJ
[typeid
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
48 def create_value(value
):
53 if isinstance(value
, _Value
):
56 if isinstance(value
, bool):
57 return BoolValue(value
)
59 if isinstance(value
, numbers
.Integral
):
60 return SignedIntegerValue(value
)
62 if isinstance(value
, numbers
.Real
):
63 return RealValue(value
)
65 if isinstance(value
, str):
66 return StringValue(value
)
68 if isinstance(value
, collections
.abc
.Sequence
):
69 return ArrayValue(value
)
71 if isinstance(value
, collections
.abc
.Mapping
):
72 return MapValue(value
)
74 raise TypeError("cannot create value object from '{}' object".format(value
.__class
__.__name
__))
77 class _Value(object._SharedObject
, metaclass
=abc
.ABCMeta
):
78 _get_ref
= staticmethod(native_bt
.value_get_ref
)
79 _put_ref
= staticmethod(native_bt
.value_put_ref
)
81 def __ne__(self
, other
):
82 return not (self
== other
)
84 def _check_create_status(self
, ptr
):
86 raise bt2
.CreationError(
87 'cannot create {} value object'.format(self
._NAME
.lower()))
90 @functools.total_ordering
91 class _NumericValue(_Value
):
93 def _extract_value(other
):
94 if isinstance(other
, BoolValue
) or isinstance(other
, bool):
97 if isinstance(other
, numbers
.Integral
):
100 if isinstance(other
, numbers
.Real
):
103 if isinstance(other
, numbers
.Complex
):
104 return complex(other
)
106 raise TypeError("'{}' object is not a number object".format(other
.__class
__.__name
__))
109 return int(self
._value
)
112 return float(self
._value
)
115 return repr(self
._value
)
117 def __lt__(self
, other
):
118 return self
._value
< self
._extract
_value
(other
)
120 def __eq__(self
, other
):
122 return self
._value
== self
._extract
_value
(other
)
126 def __rmod__(self
, other
):
127 return self
._extract
_value
(other
) % self
._value
129 def __mod__(self
, other
):
130 return self
._value
% self
._extract
_value
(other
)
132 def __rfloordiv__(self
, other
):
133 return self
._extract
_value
(other
) // self
._value
135 def __floordiv__(self
, other
):
136 return self
._value
// self
._extract
_value
(other
)
138 def __round__(self
, ndigits
=None):
140 return round(self
._value
)
142 return round(self
._value
, ndigits
)
145 return math
.ceil(self
._value
)
148 return math
.floor(self
._value
)
151 return int(self
._value
)
154 return abs(self
._value
)
156 def __add__(self
, other
):
157 return self
._value
+ self
._extract
_value
(other
)
159 def __radd__(self
, other
):
160 return self
.__add
__(other
)
168 def __mul__(self
, other
):
169 return self
._value
* self
._extract
_value
(other
)
171 def __rmul__(self
, other
):
172 return self
.__mul
__(other
)
174 def __truediv__(self
, other
):
175 return self
._value
/ self
._extract
_value
(other
)
177 def __rtruediv__(self
, other
):
178 return self
._extract
_value
(other
) / self
._value
180 def __pow__(self
, exponent
):
181 return self
._value
** self
._extract
_value
(exponent
)
183 def __rpow__(self
, base
):
184 return self
._extract
_value
(base
) ** self
._value
187 class _IntegralValue(_NumericValue
, numbers
.Integral
):
188 def __lshift__(self
, other
):
189 return self
._value
<< self
._extract
_value
(other
)
191 def __rlshift__(self
, other
):
192 return self
._extract
_value
(other
) << self
._value
194 def __rshift__(self
, other
):
195 return self
._value
>> self
._extract
_value
(other
)
197 def __rrshift__(self
, other
):
198 return self
._extract
_value
(other
) >> self
._value
200 def __and__(self
, other
):
201 return self
._value
& self
._extract
_value
(other
)
203 def __rand__(self
, other
):
204 return self
._extract
_value
(other
) & self
._value
206 def __xor__(self
, other
):
207 return self
._value ^ self
._extract
_value
(other
)
209 def __rxor__(self
, other
):
210 return self
._extract
_value
(other
) ^ self
._value
212 def __or__(self
, other
):
213 return self
._value | self
._extract
_value
(other
)
215 def __ror__(self
, other
):
216 return self
._extract
_value
(other
) | self
._value
218 def __invert__(self
):
222 class _RealValue(_NumericValue
, numbers
.Real
):
226 class BoolValue(_IntegralValue
):
229 def __init__(self
, value
=None):
231 ptr
= native_bt
.value_bool_create()
233 ptr
= native_bt
.value_bool_create_init(self
._value
_to
_bool
(value
))
235 self
._check
_create
_status
(ptr
)
236 super().__init
__(ptr
)
242 return repr(self
._value
)
244 def _value_to_bool(self
, value
):
245 if isinstance(value
, BoolValue
):
248 if not isinstance(value
, bool):
249 raise TypeError("'{}' object is not a 'bool' or 'BoolValue' object".format(value
.__class
__))
255 value
= native_bt
.value_bool_get(self
._ptr
)
258 def _set_value(self
, value
):
259 native_bt
.value_bool_set(self
._ptr
, self
._value
_to
_bool
(value
))
261 value
= property(fset
=_set_value
)
264 class _IntegerValue(_IntegralValue
):
265 def __init__(self
, value
=None):
267 ptr
= self
._create
_default
_value
()
269 ptr
= self
._create
_value
(self
._value
_to
_int
(value
))
271 self
._check
_create
_status
(ptr
)
272 super().__init
__(ptr
)
274 def _value_to_int(self
, value
):
275 if not isinstance(value
, numbers
.Integral
):
276 raise TypeError('expecting an integral number object')
279 self
._check
_int
_range
(value
)
284 return self
._get
_value
(self
._ptr
)
286 def _prop_set_value(self
, value
):
287 self
._set
_value
(self
._ptr
, self
._value
_to
_int
(value
))
289 value
= property(fset
=_prop_set_value
)
292 class UnsignedIntegerValue(_IntegerValue
):
293 _check_int_range
= staticmethod(utils
._check
_uint
64)
294 _create_default_value
= staticmethod(native_bt
.value_unsigned_integer_create
)
295 _create_value
= staticmethod(native_bt
.value_unsigned_integer_create_init
)
296 _set_value
= staticmethod(native_bt
.value_unsigned_integer_set
)
297 _get_value
= staticmethod(native_bt
.value_unsigned_integer_get
)
300 class SignedIntegerValue(_IntegerValue
):
301 _check_int_range
= staticmethod(utils
._check
_int
64)
302 _create_default_value
= staticmethod(native_bt
.value_signed_integer_create
)
303 _create_value
= staticmethod(native_bt
.value_signed_integer_create_init
)
304 _set_value
= staticmethod(native_bt
.value_signed_integer_set
)
305 _get_value
= staticmethod(native_bt
.value_signed_integer_get
)
308 class RealValue(_RealValue
):
309 _NAME
= 'Real number'
311 def __init__(self
, value
=None):
313 ptr
= native_bt
.value_real_create()
315 value
= self
._value
_to
_float
(value
)
316 ptr
= native_bt
.value_real_create_init(value
)
318 self
._check
_create
_status
(ptr
)
319 super().__init
__(ptr
)
321 def _value_to_float(self
, value
):
322 if not isinstance(value
, numbers
.Real
):
323 raise TypeError("expecting a real number object")
329 return native_bt
.value_real_get(self
._ptr
)
331 def _set_value(self
, value
):
332 native_bt
.value_real_set(self
._ptr
, self
._value
_to
_float
(value
))
334 value
= property(fset
=_set_value
)
337 @functools.total_ordering
338 class StringValue(collections
.abc
.Sequence
, _Value
):
341 def __init__(self
, value
=None):
343 ptr
= native_bt
.value_string_create()
345 ptr
= native_bt
.value_string_create_init(self
._value
_to
_str
(value
))
347 self
._check
_create
_status
(ptr
)
348 super().__init
__(ptr
)
350 def _value_to_str(self
, value
):
351 if isinstance(value
, self
.__class
__):
354 utils
._check
_str
(value
)
359 return native_bt
.value_string_get(self
._ptr
)
361 def _set_value(self
, value
):
362 status
= native_bt
.value_string_set(self
._ptr
, self
._value
_to
_str
(value
))
363 utils
._handle
_func
_status
(status
)
365 value
= property(fset
=_set_value
)
367 def __eq__(self
, other
):
369 return self
._value
== self
._value
_to
_str
(other
)
373 def __lt__(self
, other
):
374 return self
._value
< self
._value
_to
_str
(other
)
377 return bool(self
._value
)
380 return repr(self
._value
)
385 def __getitem__(self
, index
):
386 return self
._value
[index
]
389 return len(self
._value
)
391 def __iadd__(self
, value
):
392 curvalue
= self
._value
393 curvalue
+= self
._value
_to
_str
(value
)
394 self
.value
= curvalue
400 return len(self
) != 0
402 def __delitem__(self
, index
):
403 raise NotImplementedError
406 class ArrayValue(_Container
, collections
.abc
.MutableSequence
, _Value
):
409 def __init__(self
, value
=None):
410 ptr
= native_bt
.value_array_create()
411 self
._check
_create
_status
(ptr
)
412 super().__init
__(ptr
)
414 # Python will raise a TypeError if there's anything wrong with
415 # the iterable protocol.
416 if value
is not None:
420 def __eq__(self
, other
):
421 if not isinstance(other
, collections
.abc
.Sequence
):
424 if len(self
) != len(other
):
428 for self_elem
, other_elem
in zip(self
, other
):
429 if self_elem
!= other_elem
:
435 size
= native_bt
.value_array_get_size(self
._ptr
)
439 def _check_index(self
, index
):
440 # TODO: support slices also
441 if not isinstance(index
, numbers
.Integral
):
442 raise TypeError("'{}' object is not an integral number object: invalid index".format(index
.__class
__.__name
__))
446 if index
< 0 or index
>= len(self
):
447 raise IndexError('array value object index is out of range')
449 def __getitem__(self
, index
):
450 self
._check
_index
(index
)
451 ptr
= native_bt
.value_array_borrow_element_by_index(self
._ptr
, index
)
453 return _create_from_ptr_and_get_ref(ptr
)
455 def __setitem__(self
, index
, value
):
456 self
._check
_index
(index
)
457 value
= create_value(value
)
460 ptr
= native_bt
.value_null
464 status
= native_bt
.value_array_set_element_by_index(
465 self
._ptr
, index
, ptr
)
466 utils
._handle
_func
_status
(status
)
468 def append(self
, value
):
469 value
= create_value(value
)
472 ptr
= native_bt
.value_null
476 status
= native_bt
.value_array_append_element(self
._ptr
, ptr
)
477 utils
._handle
_func
_status
(status
)
479 def __iadd__(self
, iterable
):
480 # Python will raise a TypeError if there's anything wrong with
481 # the iterable protocol.
482 for elem
in iterable
:
488 return '[{}]'.format(', '.join([repr(v
) for v
in self
]))
490 def insert(self
, value
):
491 raise NotImplementedError
494 class _MapValueKeyIterator(collections
.abc
.Iterator
):
495 def __init__(self
, map_obj
):
496 self
._map
_obj
= map_obj
498 keys_ptr
= native_bt
.value_map_get_keys(map_obj
._ptr
)
501 raise RuntimeError('unexpected error: cannot get map value object keys')
503 self
._keys
= _create_from_ptr(keys_ptr
)
506 if self
._at
== len(self
._map
_obj
):
509 key
= self
._keys
[self
._at
]
514 class MapValue(_Container
, collections
.abc
.MutableMapping
, _Value
):
517 def __init__(self
, value
=None):
518 ptr
= native_bt
.value_map_create()
519 self
._check
_create
_status
(ptr
)
520 super().__init
__(ptr
)
522 # Python will raise a TypeError if there's anything wrong with
523 # the iterable/mapping protocol.
524 if value
is not None:
525 for key
, elem
in value
.items():
528 def __ne__(self
, other
):
529 return _Value
.__ne
__(self
, other
)
531 def __eq__(self
, other
):
532 if not isinstance(other
, collections
.abc
.Mapping
):
535 if len(self
) != len(other
):
539 for self_key
in self
:
540 if self_key
not in other
:
543 if self
[self_key
] != other
[self_key
]:
549 size
= native_bt
.value_map_get_size(self
._ptr
)
553 def __contains__(self
, key
):
554 self
._check
_key
_type
(key
)
555 return native_bt
.value_map_has_entry(self
._ptr
, key
)
557 def _check_key_type(self
, key
):
558 utils
._check
_str
(key
)
560 def _check_key(self
, key
):
564 def __getitem__(self
, key
):
566 ptr
= native_bt
.value_map_borrow_entry_value(self
._ptr
, key
)
568 return _create_from_ptr_and_get_ref(ptr
)
571 return _MapValueKeyIterator(self
)
573 def __setitem__(self
, key
, value
):
574 self
._check
_key
_type
(key
)
575 value
= create_value(value
)
578 ptr
= native_bt
.value_null
582 status
= native_bt
.value_map_insert_entry(self
._ptr
, key
, ptr
)
583 utils
._handle
_func
_status
(status
)
586 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in self
.items()]
587 return '{{{}}}'.format(', '.join(items
))
591 native_bt
.VALUE_TYPE_BOOL
: BoolValue
,
592 native_bt
.VALUE_TYPE_UNSIGNED_INTEGER
: UnsignedIntegerValue
,
593 native_bt
.VALUE_TYPE_SIGNED_INTEGER
: SignedIntegerValue
,
594 native_bt
.VALUE_TYPE_REAL
: RealValue
,
595 native_bt
.VALUE_TYPE_STRING
: StringValue
,
596 native_bt
.VALUE_TYPE_ARRAY
: ArrayValue
,
597 native_bt
.VALUE_TYPE_MAP
: MapValue
,