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