Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b | 2 | # |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
4 | |
5 | from bt2 import native_bt, object, utils | |
6 | import collections.abc | |
7 | import functools | |
8 | import numbers | |
9 | import math | |
10 | import abc | |
11 | import bt2 | |
12 | ||
13 | ||
e42e1587 | 14 | def _create_from_ptr_template(ptr, object_map): |
b5947615 SM |
15 | if ptr is None: |
16 | return | |
17 | ||
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: | |
3fb99a22 | 22 | _Value._put_ref(ptr) |
81447b5b PP |
23 | return |
24 | ||
25 | typeid = native_bt.value_get_type(ptr) | |
e42e1587 | 26 | return object_map[typeid]._create_from_ptr(ptr) |
81447b5b PP |
27 | |
28 | ||
e42e1587 FD |
29 | def _create_from_ptr(ptr): |
30 | return _create_from_ptr_template(ptr, _TYPE_TO_OBJ) | |
31 | ||
32 | ||
33 | def _create_from_const_ptr(ptr): | |
34 | return _create_from_ptr_template(ptr, _TYPE_TO_CONST_OBJ) | |
35 | ||
36 | ||
37 | def _create_from_ptr_and_get_ref_template(ptr, object_map): | |
10a19b49 SM |
38 | if ptr is None or ptr == native_bt.value_null: |
39 | return | |
40 | ||
41 | typeid = native_bt.value_get_type(ptr) | |
e42e1587 FD |
42 | return object_map[typeid]._create_from_ptr_and_get_ref(ptr) |
43 | ||
44 | ||
45 | def _create_from_ptr_and_get_ref(ptr): | |
46 | return _create_from_ptr_and_get_ref_template(ptr, _TYPE_TO_OBJ) | |
47 | ||
48 | ||
49 | def _create_from_const_ptr_and_get_ref(ptr): | |
50 | return _create_from_ptr_and_get_ref_template(ptr, _TYPE_TO_CONST_OBJ) | |
10a19b49 SM |
51 | |
52 | ||
81447b5b PP |
53 | def create_value(value): |
54 | if value is None: | |
55 | # null value object | |
56 | return | |
57 | ||
58 | if isinstance(value, _Value): | |
59 | return value | |
60 | ||
61 | if isinstance(value, bool): | |
62 | return BoolValue(value) | |
63 | ||
665658c2 | 64 | if isinstance(value, numbers.Integral): |
fdd3a2da | 65 | return SignedIntegerValue(value) |
81447b5b | 66 | |
665658c2 | 67 | if isinstance(value, numbers.Real): |
10a19b49 | 68 | return RealValue(value) |
81447b5b PP |
69 | |
70 | if isinstance(value, str): | |
71 | return StringValue(value) | |
72 | ||
665658c2 | 73 | if isinstance(value, collections.abc.Sequence): |
81447b5b | 74 | return ArrayValue(value) |
665658c2 PP |
75 | |
76 | if isinstance(value, collections.abc.Mapping): | |
77 | return MapValue(value) | |
81447b5b | 78 | |
cfbd7cf3 FD |
79 | raise TypeError( |
80 | "cannot create value object from '{}' object".format(value.__class__.__name__) | |
81 | ) | |
81447b5b PP |
82 | |
83 | ||
e42e1587 | 84 | class _ValueConst(object._SharedObject, metaclass=abc.ABCMeta): |
9dee90bd SM |
85 | @staticmethod |
86 | def _get_ref(ptr): | |
87 | native_bt.value_get_ref(ptr) | |
88 | ||
89 | @staticmethod | |
90 | def _put_ref(ptr): | |
91 | native_bt.value_put_ref(ptr) | |
92 | ||
e42e1587 FD |
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 | |
96 | ) | |
10a19b49 | 97 | |
81447b5b PP |
98 | def __ne__(self, other): |
99 | return not (self == other) | |
100 | ||
81447b5b PP |
101 | def _check_create_status(self, ptr): |
102 | if ptr is None: | |
694c792b | 103 | raise bt2._MemoryError( |
f5567ea8 | 104 | "cannot create {} value object".format(self._NAME.lower()) |
cfbd7cf3 | 105 | ) |
81447b5b PP |
106 | |
107 | ||
e42e1587 FD |
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) | |
111 | ||
112 | ||
81447b5b | 113 | @functools.total_ordering |
e42e1587 | 114 | class _NumericValueConst(_ValueConst): |
81447b5b PP |
115 | @staticmethod |
116 | def _extract_value(other): | |
e42e1587 | 117 | if isinstance(other, _BoolValueConst) or isinstance(other, bool): |
b00a769c | 118 | return bool(other) |
81447b5b PP |
119 | |
120 | if isinstance(other, numbers.Integral): | |
121 | return int(other) | |
122 | ||
123 | if isinstance(other, numbers.Real): | |
124 | return float(other) | |
125 | ||
126 | if isinstance(other, numbers.Complex): | |
127 | return complex(other) | |
128 | ||
cfbd7cf3 FD |
129 | raise TypeError( |
130 | "'{}' object is not a number object".format(other.__class__.__name__) | |
131 | ) | |
81447b5b PP |
132 | |
133 | def __int__(self): | |
9b6cd4a7 | 134 | return int(self._value) |
81447b5b PP |
135 | |
136 | def __float__(self): | |
9b6cd4a7 | 137 | return float(self._value) |
81447b5b | 138 | |
83656269 JG |
139 | def __repr__(self): |
140 | return repr(self._value) | |
81447b5b PP |
141 | |
142 | def __lt__(self, other): | |
09a926c1 | 143 | return self._value < self._extract_value(other) |
81447b5b | 144 | |
81447b5b | 145 | def __eq__(self, other): |
b00a769c PP |
146 | try: |
147 | return self._value == self._extract_value(other) | |
4c4935bf | 148 | except Exception: |
81447b5b PP |
149 | return False |
150 | ||
81447b5b | 151 | def __rmod__(self, other): |
9b6cd4a7 | 152 | return self._extract_value(other) % self._value |
81447b5b PP |
153 | |
154 | def __mod__(self, other): | |
9b6cd4a7 | 155 | return self._value % self._extract_value(other) |
81447b5b PP |
156 | |
157 | def __rfloordiv__(self, other): | |
9b6cd4a7 | 158 | return self._extract_value(other) // self._value |
81447b5b PP |
159 | |
160 | def __floordiv__(self, other): | |
9b6cd4a7 | 161 | return self._value // self._extract_value(other) |
81447b5b PP |
162 | |
163 | def __round__(self, ndigits=None): | |
164 | if ndigits is None: | |
9b6cd4a7 | 165 | return round(self._value) |
81447b5b | 166 | else: |
9b6cd4a7 | 167 | return round(self._value, ndigits) |
81447b5b PP |
168 | |
169 | def __ceil__(self): | |
9b6cd4a7 | 170 | return math.ceil(self._value) |
81447b5b PP |
171 | |
172 | def __floor__(self): | |
9b6cd4a7 | 173 | return math.floor(self._value) |
81447b5b PP |
174 | |
175 | def __trunc__(self): | |
9b6cd4a7 | 176 | return int(self._value) |
81447b5b PP |
177 | |
178 | def __abs__(self): | |
9b6cd4a7 | 179 | return abs(self._value) |
81447b5b PP |
180 | |
181 | def __add__(self, other): | |
9b6cd4a7 | 182 | return self._value + self._extract_value(other) |
81447b5b PP |
183 | |
184 | def __radd__(self, other): | |
185 | return self.__add__(other) | |
186 | ||
187 | def __neg__(self): | |
9b6cd4a7 | 188 | return -self._value |
81447b5b PP |
189 | |
190 | def __pos__(self): | |
9b6cd4a7 | 191 | return +self._value |
81447b5b PP |
192 | |
193 | def __mul__(self, other): | |
9b6cd4a7 | 194 | return self._value * self._extract_value(other) |
81447b5b PP |
195 | |
196 | def __rmul__(self, other): | |
197 | return self.__mul__(other) | |
198 | ||
199 | def __truediv__(self, other): | |
9b6cd4a7 | 200 | return self._value / self._extract_value(other) |
81447b5b PP |
201 | |
202 | def __rtruediv__(self, other): | |
9b6cd4a7 | 203 | return self._extract_value(other) / self._value |
81447b5b PP |
204 | |
205 | def __pow__(self, exponent): | |
9b6cd4a7 | 206 | return self._value ** self._extract_value(exponent) |
81447b5b PP |
207 | |
208 | def __rpow__(self, base): | |
9b6cd4a7 | 209 | return self._extract_value(base) ** self._value |
81447b5b | 210 | |
81447b5b | 211 | |
e42e1587 FD |
212 | class _NumericValue(_NumericValueConst, _Value): |
213 | pass | |
214 | ||
215 | ||
216 | class _IntegralValueConst(_NumericValueConst, numbers.Integral): | |
81447b5b | 217 | def __lshift__(self, other): |
9b6cd4a7 | 218 | return self._value << self._extract_value(other) |
81447b5b PP |
219 | |
220 | def __rlshift__(self, other): | |
9b6cd4a7 | 221 | return self._extract_value(other) << self._value |
81447b5b PP |
222 | |
223 | def __rshift__(self, other): | |
9b6cd4a7 | 224 | return self._value >> self._extract_value(other) |
81447b5b PP |
225 | |
226 | def __rrshift__(self, other): | |
9b6cd4a7 | 227 | return self._extract_value(other) >> self._value |
81447b5b PP |
228 | |
229 | def __and__(self, other): | |
9b6cd4a7 | 230 | return self._value & self._extract_value(other) |
81447b5b PP |
231 | |
232 | def __rand__(self, other): | |
9b6cd4a7 | 233 | return self._extract_value(other) & self._value |
81447b5b PP |
234 | |
235 | def __xor__(self, other): | |
9b6cd4a7 | 236 | return self._value ^ self._extract_value(other) |
81447b5b PP |
237 | |
238 | def __rxor__(self, other): | |
9b6cd4a7 | 239 | return self._extract_value(other) ^ self._value |
81447b5b PP |
240 | |
241 | def __or__(self, other): | |
9b6cd4a7 | 242 | return self._value | self._extract_value(other) |
81447b5b PP |
243 | |
244 | def __ror__(self, other): | |
9b6cd4a7 | 245 | return self._extract_value(other) | self._value |
81447b5b PP |
246 | |
247 | def __invert__(self): | |
9b6cd4a7 | 248 | return ~self._value |
81447b5b | 249 | |
81447b5b | 250 | |
e42e1587 | 251 | class _IntegralValue(_IntegralValueConst, _NumericValue): |
81447b5b PP |
252 | pass |
253 | ||
254 | ||
e42e1587 | 255 | class _BoolValueConst(_IntegralValueConst): |
f5567ea8 | 256 | _NAME = "Const boolean" |
e42e1587 FD |
257 | |
258 | def __bool__(self): | |
259 | return self._value | |
260 | ||
261 | def __repr__(self): | |
262 | return repr(self._value) | |
263 | ||
264 | @property | |
265 | def _value(self): | |
266 | value = native_bt.value_bool_get(self._ptr) | |
267 | return value != 0 | |
268 | ||
269 | ||
270 | class BoolValue(_BoolValueConst, _IntegralValue): | |
f5567ea8 | 271 | _NAME = "Boolean" |
81447b5b PP |
272 | |
273 | def __init__(self, value=None): | |
274 | if value is None: | |
275 | ptr = native_bt.value_bool_create() | |
276 | else: | |
277 | ptr = native_bt.value_bool_create_init(self._value_to_bool(value)) | |
278 | ||
279 | self._check_create_status(ptr) | |
280 | super().__init__(ptr) | |
281 | ||
e42e1587 FD |
282 | @classmethod |
283 | def _value_to_bool(cls, value): | |
284 | if isinstance(value, _BoolValueConst): | |
9b6cd4a7 | 285 | value = value._value |
81447b5b PP |
286 | |
287 | if not isinstance(value, bool): | |
cfbd7cf3 | 288 | raise TypeError( |
e42e1587 | 289 | "'{}' object is not a 'bool', 'BoolValue', or '_BoolValueConst' object".format( |
cfbd7cf3 FD |
290 | value.__class__ |
291 | ) | |
292 | ) | |
81447b5b | 293 | |
b00a769c | 294 | return value |
81447b5b | 295 | |
9b6cd4a7 | 296 | def _set_value(self, value): |
10a19b49 | 297 | native_bt.value_bool_set(self._ptr, self._value_to_bool(value)) |
81447b5b | 298 | |
9b6cd4a7 PP |
299 | value = property(fset=_set_value) |
300 | ||
81447b5b | 301 | |
e42e1587 FD |
302 | class _IntegerValueConst(_IntegralValueConst): |
303 | @property | |
304 | def _value(self): | |
305 | return self._get_value(self._ptr) | |
306 | ||
307 | ||
308 | class _IntegerValue(_IntegerValueConst, _IntegralValue): | |
81447b5b PP |
309 | def __init__(self, value=None): |
310 | if value is None: | |
fdd3a2da | 311 | ptr = self._create_default_value() |
81447b5b | 312 | else: |
fdd3a2da | 313 | ptr = self._create_value(self._value_to_int(value)) |
81447b5b PP |
314 | |
315 | self._check_create_status(ptr) | |
316 | super().__init__(ptr) | |
317 | ||
e42e1587 FD |
318 | @classmethod |
319 | def _value_to_int(cls, value): | |
e502b15a | 320 | if not isinstance(value, numbers.Integral): |
f5567ea8 | 321 | raise TypeError("expecting an integral number object") |
81447b5b PP |
322 | |
323 | value = int(value) | |
e42e1587 | 324 | cls._check_int_range(value) |
81447b5b PP |
325 | return value |
326 | ||
fdd3a2da PP |
327 | def _prop_set_value(self, value): |
328 | self._set_value(self._ptr, self._value_to_int(value)) | |
81447b5b | 329 | |
fdd3a2da PP |
330 | value = property(fset=_prop_set_value) |
331 | ||
332 | ||
e42e1587 | 333 | class _UnsignedIntegerValueConst(_IntegerValueConst): |
f5567ea8 | 334 | _NAME = "Const unsigned integer" |
e42e1587 FD |
335 | _get_value = staticmethod(native_bt.value_integer_unsigned_get) |
336 | ||
337 | ||
338 | class UnsignedIntegerValue(_UnsignedIntegerValueConst, _IntegerValue): | |
f5567ea8 | 339 | _NAME = "Unsigned integer" |
fdd3a2da | 340 | _check_int_range = staticmethod(utils._check_uint64) |
9c08c816 PP |
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) | |
fdd3a2da PP |
344 | |
345 | ||
e42e1587 | 346 | class _SignedIntegerValueConst(_IntegerValueConst): |
f5567ea8 | 347 | _NAME = "Const signed integer" |
e42e1587 FD |
348 | _get_value = staticmethod(native_bt.value_integer_signed_get) |
349 | ||
350 | ||
351 | class SignedIntegerValue(_SignedIntegerValueConst, _IntegerValue): | |
f5567ea8 | 352 | _NAME = "Signed integer" |
fdd3a2da | 353 | _check_int_range = staticmethod(utils._check_int64) |
9c08c816 PP |
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) | |
9b6cd4a7 | 357 | |
81447b5b | 358 | |
e42e1587 | 359 | class _RealValueConst(_NumericValueConst, numbers.Real): |
f5567ea8 | 360 | _NAME = "Const real number" |
e42e1587 FD |
361 | |
362 | @property | |
363 | def _value(self): | |
364 | return native_bt.value_real_get(self._ptr) | |
365 | ||
366 | ||
367 | class RealValue(_RealValueConst, _NumericValue): | |
f5567ea8 | 368 | _NAME = "Real number" |
81447b5b PP |
369 | |
370 | def __init__(self, value=None): | |
371 | if value is None: | |
10a19b49 | 372 | ptr = native_bt.value_real_create() |
81447b5b PP |
373 | else: |
374 | value = self._value_to_float(value) | |
10a19b49 | 375 | ptr = native_bt.value_real_create_init(value) |
81447b5b PP |
376 | |
377 | self._check_create_status(ptr) | |
378 | super().__init__(ptr) | |
379 | ||
e42e1587 FD |
380 | @classmethod |
381 | def _value_to_float(cls, value): | |
81447b5b PP |
382 | if not isinstance(value, numbers.Real): |
383 | raise TypeError("expecting a real number object") | |
384 | ||
385 | return float(value) | |
386 | ||
9b6cd4a7 | 387 | def _set_value(self, value): |
10a19b49 | 388 | native_bt.value_real_set(self._ptr, self._value_to_float(value)) |
81447b5b | 389 | |
9b6cd4a7 PP |
390 | value = property(fset=_set_value) |
391 | ||
81447b5b PP |
392 | |
393 | @functools.total_ordering | |
e42e1587 | 394 | class _StringValueConst(collections.abc.Sequence, _Value): |
f5567ea8 | 395 | _NAME = "Const string" |
81447b5b | 396 | |
e42e1587 FD |
397 | @classmethod |
398 | def _value_to_str(cls, value): | |
399 | if isinstance(value, _StringValueConst): | |
9b6cd4a7 | 400 | value = value._value |
81447b5b PP |
401 | |
402 | utils._check_str(value) | |
403 | return value | |
404 | ||
405 | @property | |
9b6cd4a7 | 406 | def _value(self): |
10a19b49 | 407 | return native_bt.value_string_get(self._ptr) |
81447b5b | 408 | |
b00a769c | 409 | def __eq__(self, other): |
81447b5b | 410 | try: |
9b6cd4a7 | 411 | return self._value == self._value_to_str(other) |
4c4935bf | 412 | except Exception: |
b00a769c | 413 | return False |
81447b5b | 414 | |
81447b5b | 415 | def __lt__(self, other): |
9b6cd4a7 | 416 | return self._value < self._value_to_str(other) |
81447b5b PP |
417 | |
418 | def __bool__(self): | |
9b6cd4a7 | 419 | return bool(self._value) |
81447b5b | 420 | |
83656269 | 421 | def __repr__(self): |
10a19b49 | 422 | return repr(self._value) |
83656269 | 423 | |
81447b5b | 424 | def __str__(self): |
9b6cd4a7 | 425 | return self._value |
81447b5b PP |
426 | |
427 | def __getitem__(self, index): | |
9b6cd4a7 | 428 | return self._value[index] |
81447b5b PP |
429 | |
430 | def __len__(self): | |
9b6cd4a7 | 431 | return len(self._value) |
81447b5b | 432 | |
5cf62322 FD |
433 | def __contains__(self, item): |
434 | return self._value_to_str(item) in self._value | |
435 | ||
e42e1587 FD |
436 | |
437 | class StringValue(_StringValueConst, _Value): | |
f5567ea8 | 438 | _NAME = "String" |
e42e1587 FD |
439 | |
440 | def __init__(self, value=None): | |
441 | if value is None: | |
442 | ptr = native_bt.value_string_create() | |
443 | else: | |
444 | ptr = native_bt.value_string_create_init(self._value_to_str(value)) | |
445 | ||
446 | self._check_create_status(ptr) | |
447 | super().__init__(ptr) | |
448 | ||
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) | |
452 | ||
453 | value = property(fset=_set_value) | |
454 | ||
81447b5b | 455 | def __iadd__(self, value): |
9b6cd4a7 | 456 | curvalue = self._value |
81447b5b PP |
457 | curvalue += self._value_to_str(value) |
458 | self.value = curvalue | |
459 | return self | |
460 | ||
461 | ||
e42e1587 | 462 | class _ContainerConst: |
81447b5b PP |
463 | def __bool__(self): |
464 | return len(self) != 0 | |
465 | ||
e42e1587 FD |
466 | |
467 | class _Container(_ContainerConst): | |
81447b5b PP |
468 | def __delitem__(self, index): |
469 | raise NotImplementedError | |
470 | ||
471 | ||
e42e1587 | 472 | class _ArrayValueConst(_ContainerConst, collections.abc.Sequence, _ValueConst): |
f5567ea8 | 473 | _NAME = "Const array" |
e42e1587 FD |
474 | _borrow_element_by_index = staticmethod( |
475 | native_bt.value_array_borrow_element_by_index_const | |
476 | ) | |
477 | _is_const = True | |
81447b5b | 478 | |
b00a769c PP |
479 | def __eq__(self, other): |
480 | if not isinstance(other, collections.abc.Sequence): | |
481 | return False | |
81447b5b | 482 | |
b00a769c PP |
483 | if len(self) != len(other): |
484 | # early mismatch | |
485 | return False | |
81447b5b | 486 | |
b00a769c PP |
487 | for self_elem, other_elem in zip(self, other): |
488 | if self_elem != other_elem: | |
489 | return False | |
490 | ||
491 | return True | |
81447b5b PP |
492 | |
493 | def __len__(self): | |
393729a6 | 494 | size = native_bt.value_array_get_length(self._ptr) |
cfbd7cf3 | 495 | assert size >= 0 |
81447b5b PP |
496 | return size |
497 | ||
498 | def _check_index(self, index): | |
499 | # TODO: support slices also | |
500 | if not isinstance(index, numbers.Integral): | |
cfbd7cf3 FD |
501 | raise TypeError( |
502 | "'{}' object is not an integral number object: invalid index".format( | |
503 | index.__class__.__name__ | |
504 | ) | |
505 | ) | |
81447b5b PP |
506 | |
507 | index = int(index) | |
508 | ||
509 | if index < 0 or index >= len(self): | |
f5567ea8 | 510 | raise IndexError("array value object index is out of range") |
81447b5b PP |
511 | |
512 | def __getitem__(self, index): | |
513 | self._check_index(index) | |
e42e1587 | 514 | ptr = self._borrow_element_by_index(self._ptr, index) |
cfbd7cf3 | 515 | assert ptr |
e42e1587 FD |
516 | return self._create_value_from_ptr_and_get_ref(ptr) |
517 | ||
518 | def __repr__(self): | |
f5567ea8 | 519 | return "[{}]".format(", ".join([repr(v) for v in self])) |
e42e1587 FD |
520 | |
521 | ||
522 | class ArrayValue(_ArrayValueConst, _Container, collections.abc.MutableSequence, _Value): | |
f5567ea8 | 523 | _NAME = "Array" |
e42e1587 FD |
524 | _borrow_element_by_index = staticmethod( |
525 | native_bt.value_array_borrow_element_by_index | |
526 | ) | |
527 | ||
528 | def __init__(self, value=None): | |
529 | ptr = native_bt.value_array_create() | |
530 | self._check_create_status(ptr) | |
531 | super().__init__(ptr) | |
532 | ||
533 | # Python will raise a TypeError if there's anything wrong with | |
534 | # the iterable protocol. | |
535 | if value is not None: | |
536 | for elem in value: | |
537 | self.append(elem) | |
81447b5b PP |
538 | |
539 | def __setitem__(self, index, value): | |
540 | self._check_index(index) | |
541 | value = create_value(value) | |
542 | ||
543 | if value is None: | |
544 | ptr = native_bt.value_null | |
545 | else: | |
546 | ptr = value._ptr | |
547 | ||
cfbd7cf3 | 548 | status = native_bt.value_array_set_element_by_index(self._ptr, index, ptr) |
d24d5663 | 549 | utils._handle_func_status(status) |
81447b5b PP |
550 | |
551 | def append(self, value): | |
552 | value = create_value(value) | |
553 | ||
554 | if value is None: | |
555 | ptr = native_bt.value_null | |
556 | else: | |
557 | ptr = value._ptr | |
558 | ||
10a19b49 | 559 | status = native_bt.value_array_append_element(self._ptr, ptr) |
d24d5663 | 560 | utils._handle_func_status(status) |
81447b5b PP |
561 | |
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: | |
566 | self.append(elem) | |
567 | ||
568 | return self | |
569 | ||
81447b5b PP |
570 | def insert(self, value): |
571 | raise NotImplementedError | |
572 | ||
573 | ||
574 | class _MapValueKeyIterator(collections.abc.Iterator): | |
575 | def __init__(self, map_obj): | |
576 | self._map_obj = map_obj | |
577 | self._at = 0 | |
10a19b49 | 578 | keys_ptr = native_bt.value_map_get_keys(map_obj._ptr) |
81447b5b PP |
579 | |
580 | if keys_ptr is None: | |
f5567ea8 | 581 | raise RuntimeError("unexpected error: cannot get map value object keys") |
81447b5b PP |
582 | |
583 | self._keys = _create_from_ptr(keys_ptr) | |
584 | ||
585 | def __next__(self): | |
586 | if self._at == len(self._map_obj): | |
587 | raise StopIteration | |
588 | ||
589 | key = self._keys[self._at] | |
590 | self._at += 1 | |
591 | return str(key) | |
592 | ||
593 | ||
e42e1587 | 594 | class _MapValueConst(_ContainerConst, collections.abc.Mapping, _ValueConst): |
f5567ea8 | 595 | _NAME = "Const map" |
e42e1587 | 596 | _borrow_entry_value_ptr = staticmethod(native_bt.value_map_borrow_entry_value_const) |
81447b5b | 597 | |
81447b5b PP |
598 | def __ne__(self, other): |
599 | return _Value.__ne__(self, other) | |
600 | ||
b00a769c PP |
601 | def __eq__(self, other): |
602 | if not isinstance(other, collections.abc.Mapping): | |
603 | return False | |
81447b5b | 604 | |
b00a769c PP |
605 | if len(self) != len(other): |
606 | # early mismatch | |
607 | return False | |
81447b5b | 608 | |
b00a769c PP |
609 | for self_key in self: |
610 | if self_key not in other: | |
611 | return False | |
81447b5b | 612 | |
b00a769c PP |
613 | if self[self_key] != other[self_key]: |
614 | return False | |
81447b5b | 615 | |
b00a769c | 616 | return True |
81447b5b PP |
617 | |
618 | def __len__(self): | |
10a19b49 | 619 | size = native_bt.value_map_get_size(self._ptr) |
cfbd7cf3 | 620 | assert size >= 0 |
81447b5b PP |
621 | return size |
622 | ||
623 | def __contains__(self, key): | |
624 | self._check_key_type(key) | |
10a19b49 | 625 | return native_bt.value_map_has_entry(self._ptr, key) |
81447b5b PP |
626 | |
627 | def _check_key_type(self, key): | |
628 | utils._check_str(key) | |
629 | ||
630 | def _check_key(self, key): | |
631 | if key not in self: | |
632 | raise KeyError(key) | |
633 | ||
634 | def __getitem__(self, key): | |
635 | self._check_key(key) | |
e42e1587 | 636 | ptr = self._borrow_entry_value_ptr(self._ptr, key) |
cfbd7cf3 | 637 | assert ptr |
e42e1587 | 638 | return self._create_value_from_ptr_and_get_ref(ptr) |
81447b5b PP |
639 | |
640 | def __iter__(self): | |
641 | return _MapValueKeyIterator(self) | |
642 | ||
e42e1587 | 643 | def __repr__(self): |
f5567ea8 FD |
644 | items = ["{}: {}".format(repr(k), repr(v)) for k, v in self.items()] |
645 | return "{{{}}}".format(", ".join(items)) | |
e42e1587 FD |
646 | |
647 | ||
648 | class MapValue(_MapValueConst, _Container, collections.abc.MutableMapping, _Value): | |
f5567ea8 | 649 | _NAME = "Map" |
e42e1587 FD |
650 | _borrow_entry_value_ptr = staticmethod(native_bt.value_map_borrow_entry_value) |
651 | ||
652 | def __init__(self, value=None): | |
653 | ptr = native_bt.value_map_create() | |
654 | self._check_create_status(ptr) | |
655 | super().__init__(ptr) | |
656 | ||
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(): | |
661 | self[key] = elem | |
662 | ||
81447b5b PP |
663 | def __setitem__(self, key, value): |
664 | self._check_key_type(key) | |
665 | value = create_value(value) | |
666 | ||
667 | if value is None: | |
668 | ptr = native_bt.value_null | |
669 | else: | |
670 | ptr = value._ptr | |
671 | ||
10a19b49 | 672 | status = native_bt.value_map_insert_entry(self._ptr, key, ptr) |
d24d5663 | 673 | utils._handle_func_status(status) |
81447b5b | 674 | |
81447b5b PP |
675 | |
676 | _TYPE_TO_OBJ = { | |
677 | native_bt.VALUE_TYPE_BOOL: BoolValue, | |
fdd3a2da PP |
678 | native_bt.VALUE_TYPE_UNSIGNED_INTEGER: UnsignedIntegerValue, |
679 | native_bt.VALUE_TYPE_SIGNED_INTEGER: SignedIntegerValue, | |
10a19b49 | 680 | native_bt.VALUE_TYPE_REAL: RealValue, |
81447b5b PP |
681 | native_bt.VALUE_TYPE_STRING: StringValue, |
682 | native_bt.VALUE_TYPE_ARRAY: ArrayValue, | |
683 | native_bt.VALUE_TYPE_MAP: MapValue, | |
684 | } | |
e42e1587 FD |
685 | |
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, | |
694 | } |