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