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 | ||
32 | def _handle_status(status, obj_name): | |
33 | if status >= 0: | |
34 | return | |
35 | ||
36 | if status == native_bt.VALUE_STATUS_FROZEN: | |
f6a5e476 | 37 | raise bt2.Frozen('{} value object is frozen'.format(obj_name)) |
81447b5b PP |
38 | elif status == native_bt.VALUE_STATUS_INVAL: |
39 | # In practice, this should never happen, because arguments | |
40 | # should always be validated in this Python module before | |
41 | # calling the native functions. | |
42 | raise ValueError('unexpected invalid argument') | |
43 | else: | |
44 | # In practice, this should never happen, because arguments | |
45 | # should always be validated in this Python module before | |
46 | # calling the native functions. | |
47 | raise RuntimeError('unexpected error') | |
48 | ||
49 | ||
50 | def _create_from_ptr(ptr): | |
51 | if ptr is None or ptr == native_bt.value_null: | |
52 | return | |
53 | ||
54 | typeid = native_bt.value_get_type(ptr) | |
55 | return _TYPE_TO_OBJ[typeid]._create_from_ptr(ptr) | |
56 | ||
57 | ||
58 | def create_value(value): | |
59 | if value is None: | |
60 | # null value object | |
61 | return | |
62 | ||
63 | if isinstance(value, _Value): | |
64 | return value | |
65 | ||
66 | if isinstance(value, bool): | |
67 | return BoolValue(value) | |
68 | ||
69 | if isinstance(value, int): | |
70 | return IntegerValue(value) | |
71 | ||
72 | if isinstance(value, float): | |
73 | return FloatValue(value) | |
74 | ||
75 | if isinstance(value, str): | |
76 | return StringValue(value) | |
77 | ||
78 | try: | |
79 | return MapValue(value) | |
80 | except: | |
81 | pass | |
82 | ||
83 | try: | |
84 | return ArrayValue(value) | |
85 | except: | |
86 | pass | |
87 | ||
88 | raise TypeError("cannot create value object from '{}' object".format(value.__class__.__name__)) | |
89 | ||
90 | ||
91 | class _Value(object._Object, object._Freezable, metaclass=abc.ABCMeta): | |
81447b5b PP |
92 | def __eq__(self, other): |
93 | if other is None: | |
94 | # self is never the null value object | |
95 | return False | |
96 | ||
97 | # try type-specific comparison first | |
98 | spec_eq = self._spec_eq(other) | |
99 | ||
100 | if spec_eq is not None: | |
101 | return spec_eq | |
102 | ||
103 | if not isinstance(other, _Value): | |
104 | # not comparing apples to apples | |
105 | return False | |
106 | ||
107 | # fall back to native comparison function | |
108 | return native_bt.value_compare(self._ptr, other._ptr) | |
109 | ||
110 | def __ne__(self, other): | |
111 | return not (self == other) | |
112 | ||
113 | @abc.abstractmethod | |
114 | def _spec_eq(self, other): | |
115 | pass | |
116 | ||
117 | def _handle_status(self, status): | |
118 | _handle_status(status, self._NAME) | |
119 | ||
120 | def _check_create_status(self, ptr): | |
121 | if ptr is None: | |
122 | raise bt2.CreationError('cannot create {} value object'.format(self._NAME.lower())) | |
123 | ||
124 | def _is_frozen(self): | |
125 | return native_bt.value_is_frozen(self._ptr) | |
126 | ||
127 | def _freeze(self): | |
128 | status = native_bt.value_freeze(self._ptr) | |
129 | self._handle_status(status) | |
130 | ||
131 | ||
132 | class _BasicCopy: | |
133 | def __copy__(self): | |
134 | return self.__class__(self.value) | |
135 | ||
136 | def __deepcopy__(self, memo): | |
137 | copy = self.__copy__() | |
138 | memo[id(self)] = copy | |
139 | return copy | |
140 | ||
141 | ||
142 | @functools.total_ordering | |
143 | class _NumericValue(_Value, _BasicCopy): | |
144 | @staticmethod | |
145 | def _extract_value(other): | |
146 | if isinstance(other, _NumericValue): | |
147 | return other.value | |
148 | ||
149 | if other is True or other is False: | |
150 | return other | |
151 | ||
152 | if isinstance(other, numbers.Integral): | |
153 | return int(other) | |
154 | ||
155 | if isinstance(other, numbers.Real): | |
156 | return float(other) | |
157 | ||
158 | if isinstance(other, numbers.Complex): | |
159 | return complex(other) | |
160 | ||
161 | raise TypeError("'{}' object is not a number object".format(other.__class__.__name__)) | |
162 | ||
163 | def __int__(self): | |
164 | return int(self.value) | |
165 | ||
166 | def __float__(self): | |
167 | return float(self.value) | |
168 | ||
169 | def __str__(self): | |
170 | return str(self.value) | |
171 | ||
172 | def __lt__(self, other): | |
173 | if not isinstance(other, numbers.Number): | |
174 | raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__, | |
175 | other.__class__.__name__)) | |
176 | ||
177 | return self.value < float(other) | |
178 | ||
179 | def __le__(self, other): | |
180 | if not isinstance(other, numbers.Number): | |
181 | raise TypeError('unorderable types: {}() <= {}()'.format(self.__class__.__name__, | |
182 | other.__class__.__name__)) | |
183 | ||
184 | return self.value <= float(other) | |
185 | ||
186 | def _spec_eq(self, other): | |
f6a5e476 | 187 | pass |
81447b5b PP |
188 | |
189 | def __eq__(self, other): | |
190 | if not isinstance(other, numbers.Number): | |
191 | return False | |
192 | ||
193 | return self.value == complex(other) | |
194 | ||
195 | def __rmod__(self, other): | |
196 | return self._extract_value(other) % self.value | |
197 | ||
198 | def __mod__(self, other): | |
199 | return self.value % self._extract_value(other) | |
200 | ||
201 | def __rfloordiv__(self, other): | |
202 | return self._extract_value(other) // self.value | |
203 | ||
204 | def __floordiv__(self, other): | |
205 | return self.value // self._extract_value(other) | |
206 | ||
207 | def __round__(self, ndigits=None): | |
208 | if ndigits is None: | |
209 | return round(self.value) | |
210 | else: | |
211 | return round(self.value, ndigits) | |
212 | ||
213 | def __ceil__(self): | |
214 | return math.ceil(self.value) | |
215 | ||
216 | def __floor__(self): | |
217 | return math.floor(self.value) | |
218 | ||
219 | def __trunc__(self): | |
220 | return int(self.value) | |
221 | ||
222 | def __abs__(self): | |
223 | return abs(self.value) | |
224 | ||
225 | def __add__(self, other): | |
226 | return self.value + self._extract_value(other) | |
227 | ||
228 | def __radd__(self, other): | |
229 | return self.__add__(other) | |
230 | ||
231 | def __neg__(self): | |
232 | return -self.value | |
233 | ||
234 | def __pos__(self): | |
235 | return +self.value | |
236 | ||
237 | def __mul__(self, other): | |
238 | return self.value * self._extract_value(other) | |
239 | ||
240 | def __rmul__(self, other): | |
241 | return self.__mul__(other) | |
242 | ||
243 | def __truediv__(self, other): | |
244 | return self.value / self._extract_value(other) | |
245 | ||
246 | def __rtruediv__(self, other): | |
247 | return self._extract_value(other) / self.value | |
248 | ||
249 | def __pow__(self, exponent): | |
250 | return self.value ** self._extract_value(exponent) | |
251 | ||
252 | def __rpow__(self, base): | |
253 | return self._extract_value(base) ** self.value | |
254 | ||
255 | def __iadd__(self, other): | |
256 | self.value = self + other | |
257 | return self | |
258 | ||
259 | def __isub__(self, other): | |
260 | self.value = self - other | |
261 | return self | |
262 | ||
263 | def __imul__(self, other): | |
264 | self.value = self * other | |
265 | return self | |
266 | ||
267 | def __itruediv__(self, other): | |
268 | self.value = self / other | |
269 | return self | |
270 | ||
271 | def __ifloordiv__(self, other): | |
272 | self.value = self // other | |
273 | return self | |
274 | ||
275 | def __imod__(self, other): | |
276 | self.value = self % other | |
277 | return self | |
278 | ||
279 | def __ipow__(self, other): | |
280 | self.value = self ** other | |
281 | return self | |
282 | ||
283 | ||
284 | class _IntegralValue(_NumericValue, numbers.Integral): | |
285 | def __lshift__(self, other): | |
286 | return self.value << self._extract_value(other) | |
287 | ||
288 | def __rlshift__(self, other): | |
289 | return self._extract_value(other) << self.value | |
290 | ||
291 | def __rshift__(self, other): | |
292 | return self.value >> self._extract_value(other) | |
293 | ||
294 | def __rrshift__(self, other): | |
295 | return self._extract_value(other) >> self.value | |
296 | ||
297 | def __and__(self, other): | |
298 | return self.value & self._extract_value(other) | |
299 | ||
300 | def __rand__(self, other): | |
301 | return self._extract_value(other) & self.value | |
302 | ||
303 | def __xor__(self, other): | |
304 | return self.value ^ self._extract_value(other) | |
305 | ||
306 | def __rxor__(self, other): | |
307 | return self._extract_value(other) ^ self.value | |
308 | ||
309 | def __or__(self, other): | |
310 | return self.value | self._extract_value(other) | |
311 | ||
312 | def __ror__(self, other): | |
313 | return self._extract_value(other) | self.value | |
314 | ||
315 | def __invert__(self): | |
316 | return ~self.value | |
317 | ||
318 | def __ilshift__(self, other): | |
319 | self.value = self << other | |
320 | return self | |
321 | ||
322 | def __irshift__(self, other): | |
323 | self.value = self >> other | |
324 | return self | |
325 | ||
326 | def __iand__(self, other): | |
327 | self.value = self & other | |
328 | return self | |
329 | ||
330 | def __ixor__(self, other): | |
331 | self.value = self ^ other | |
332 | return self | |
333 | ||
334 | def __ior__(self, other): | |
335 | self.value = self | other | |
336 | return self | |
337 | ||
338 | ||
339 | class _RealValue(_NumericValue, numbers.Real): | |
340 | pass | |
341 | ||
342 | ||
343 | class BoolValue(_Value, _BasicCopy): | |
344 | _NAME = 'Boolean' | |
345 | ||
346 | def __init__(self, value=None): | |
347 | if value is None: | |
348 | ptr = native_bt.value_bool_create() | |
349 | else: | |
350 | ptr = native_bt.value_bool_create_init(self._value_to_bool(value)) | |
351 | ||
352 | self._check_create_status(ptr) | |
353 | super().__init__(ptr) | |
354 | ||
355 | def _spec_eq(self, other): | |
356 | if isinstance(other, numbers.Number): | |
357 | return self.value == bool(other) | |
358 | ||
359 | def __bool__(self): | |
360 | return self.value | |
361 | ||
362 | def __str__(self): | |
363 | return str(self.value) | |
364 | ||
365 | def _value_to_bool(self, value): | |
366 | if isinstance(value, BoolValue): | |
367 | value = value.value | |
368 | ||
369 | if not isinstance(value, bool): | |
370 | raise TypeError("'{}' object is not a 'bool' or 'BoolValue' object".format(value.__class__)) | |
371 | ||
f6a5e476 | 372 | return int(value) |
81447b5b PP |
373 | |
374 | @property | |
375 | def value(self): | |
376 | status, value = native_bt.value_bool_get(self._ptr) | |
f6a5e476 PP |
377 | assert(status == native_bt.VALUE_STATUS_OK) |
378 | return value > 0 | |
81447b5b PP |
379 | |
380 | @value.setter | |
381 | def value(self, value): | |
382 | status = native_bt.value_bool_set(self._ptr, self._value_to_bool(value)) | |
383 | self._handle_status(status) | |
384 | ||
385 | ||
386 | class IntegerValue(_IntegralValue): | |
387 | _NAME = 'Integer' | |
388 | ||
389 | def __init__(self, value=None): | |
390 | if value is None: | |
391 | ptr = native_bt.value_integer_create() | |
392 | else: | |
393 | ptr = native_bt.value_integer_create_init(self._value_to_int(value)) | |
394 | ||
395 | self._check_create_status(ptr) | |
396 | super().__init__(ptr) | |
397 | ||
398 | def _value_to_int(self, value): | |
399 | if not isinstance(value, numbers.Real): | |
400 | raise TypeError('expecting a number object') | |
401 | ||
402 | value = int(value) | |
403 | utils._check_int64(value) | |
404 | return value | |
405 | ||
406 | @property | |
407 | def value(self): | |
408 | status, value = native_bt.value_integer_get(self._ptr) | |
f6a5e476 | 409 | assert(status == native_bt.VALUE_STATUS_OK) |
81447b5b PP |
410 | return value |
411 | ||
412 | @value.setter | |
413 | def value(self, value): | |
414 | status = native_bt.value_integer_set(self._ptr, self._value_to_int(value)) | |
415 | self._handle_status(status) | |
416 | ||
417 | ||
418 | class FloatValue(_RealValue): | |
419 | _NAME = 'Floating point number' | |
420 | ||
421 | def __init__(self, value=None): | |
422 | if value is None: | |
423 | ptr = native_bt.value_float_create() | |
424 | else: | |
425 | value = self._value_to_float(value) | |
426 | ptr = native_bt.value_float_create_init(value) | |
427 | ||
428 | self._check_create_status(ptr) | |
429 | super().__init__(ptr) | |
430 | ||
431 | def _value_to_float(self, value): | |
432 | if not isinstance(value, numbers.Real): | |
433 | raise TypeError("expecting a real number object") | |
434 | ||
435 | return float(value) | |
436 | ||
437 | @property | |
438 | def value(self): | |
439 | status, value = native_bt.value_float_get(self._ptr) | |
f6a5e476 | 440 | assert(status == native_bt.VALUE_STATUS_OK) |
81447b5b PP |
441 | return value |
442 | ||
443 | @value.setter | |
444 | def value(self, value): | |
445 | value = self._value_to_float(value) | |
446 | status = native_bt.value_float_set(self._ptr, value) | |
447 | self._handle_status(status) | |
448 | ||
449 | ||
450 | @functools.total_ordering | |
451 | class StringValue(_BasicCopy, collections.abc.Sequence, _Value): | |
452 | _NAME = 'String' | |
453 | ||
454 | def __init__(self, value=None): | |
455 | if value is None: | |
456 | ptr = native_bt.value_string_create() | |
457 | else: | |
458 | ptr = native_bt.value_string_create_init(self._value_to_str(value)) | |
459 | ||
460 | self._check_create_status(ptr) | |
461 | super().__init__(ptr) | |
462 | ||
463 | def _value_to_str(self, value): | |
464 | if isinstance(value, self.__class__): | |
465 | value = value.value | |
466 | ||
467 | utils._check_str(value) | |
468 | return value | |
469 | ||
470 | @property | |
471 | def value(self): | |
472 | status, value = native_bt.value_string_get(self._ptr) | |
f6a5e476 | 473 | assert(status == native_bt.VALUE_STATUS_OK) |
81447b5b PP |
474 | return value |
475 | ||
476 | @value.setter | |
477 | def value(self, value): | |
478 | status = native_bt.value_string_set(self._ptr, self._value_to_str(value)) | |
479 | self._handle_status(status) | |
480 | ||
481 | def _spec_eq(self, other): | |
482 | try: | |
483 | return self.value == self._value_to_str(other) | |
484 | except: | |
485 | return | |
486 | ||
487 | def __le__(self, other): | |
488 | return self.value <= self._value_to_str(other) | |
489 | ||
490 | def __lt__(self, other): | |
491 | return self.value < self._value_to_str(other) | |
492 | ||
493 | def __bool__(self): | |
494 | return bool(self.value) | |
495 | ||
496 | def __str__(self): | |
497 | return self.value | |
498 | ||
499 | def __getitem__(self, index): | |
500 | return self.value[index] | |
501 | ||
502 | def __len__(self): | |
503 | return len(self.value) | |
504 | ||
505 | def __iadd__(self, value): | |
506 | curvalue = self.value | |
507 | curvalue += self._value_to_str(value) | |
508 | self.value = curvalue | |
509 | return self | |
510 | ||
511 | ||
512 | class _Container: | |
513 | def __bool__(self): | |
514 | return len(self) != 0 | |
515 | ||
516 | def __copy__(self): | |
517 | return self.__class__(self) | |
518 | ||
519 | def __deepcopy__(self, memo): | |
520 | ptr = native_bt.value_copy(self._ptr) | |
521 | ||
522 | if ptr is None: | |
f6a5e476 | 523 | raise RuntimeError('unexpected error: cannot deep-copy {} value object'.format(self._NAME)) |
81447b5b PP |
524 | |
525 | copy = self.__class__._create_from_ptr(ptr) | |
526 | memo[id(self)] = copy | |
527 | return copy | |
528 | ||
529 | def __delitem__(self, index): | |
530 | raise NotImplementedError | |
531 | ||
532 | ||
533 | class ArrayValue(_Container, collections.abc.MutableSequence, _Value): | |
534 | _NAME = 'Array' | |
535 | ||
536 | def __init__(self, value=None): | |
537 | ptr = native_bt.value_array_create() | |
538 | self._check_create_status(ptr) | |
539 | super().__init__(ptr) | |
540 | ||
541 | # Python will raise a TypeError if there's anything wrong with | |
542 | # the iterable protocol. | |
543 | if value is not None: | |
544 | for elem in value: | |
545 | self.append(elem) | |
546 | ||
547 | def _spec_eq(self, other): | |
548 | try: | |
549 | if len(self) != len(other): | |
550 | # early mismatch | |
551 | return False | |
552 | ||
553 | for self_elem, other_elem in zip(self, other): | |
554 | if self_elem != other_elem: | |
555 | return False | |
556 | ||
557 | return True | |
558 | except: | |
559 | return | |
560 | ||
561 | def __len__(self): | |
562 | size = native_bt.value_array_size(self._ptr) | |
f6a5e476 | 563 | assert(size >= 0) |
81447b5b PP |
564 | return size |
565 | ||
566 | def _check_index(self, index): | |
567 | # TODO: support slices also | |
568 | if not isinstance(index, numbers.Integral): | |
569 | raise TypeError("'{}' object is not an integral number object: invalid index".format(index.__class__.__name__)) | |
570 | ||
571 | index = int(index) | |
572 | ||
573 | if index < 0 or index >= len(self): | |
574 | raise IndexError('array value object index is out of range') | |
575 | ||
576 | def __getitem__(self, index): | |
577 | self._check_index(index) | |
578 | ptr = native_bt.value_array_get(self._ptr, index) | |
f6a5e476 | 579 | assert(ptr) |
81447b5b PP |
580 | return _create_from_ptr(ptr) |
581 | ||
582 | def __setitem__(self, index, value): | |
583 | self._check_index(index) | |
584 | value = create_value(value) | |
585 | ||
586 | if value is None: | |
587 | ptr = native_bt.value_null | |
588 | else: | |
589 | ptr = value._ptr | |
590 | ||
591 | status = native_bt.value_array_set(self._ptr, index, ptr) | |
592 | self._handle_status(status) | |
593 | ||
594 | def append(self, value): | |
595 | value = create_value(value) | |
596 | ||
597 | if value is None: | |
598 | ptr = native_bt.value_null | |
599 | else: | |
600 | ptr = value._ptr | |
601 | ||
602 | status = native_bt.value_array_append(self._ptr, ptr) | |
603 | self._handle_status(status) | |
604 | ||
605 | def __iadd__(self, iterable): | |
606 | # Python will raise a TypeError if there's anything wrong with | |
607 | # the iterable protocol. | |
608 | for elem in iterable: | |
609 | self.append(elem) | |
610 | ||
611 | return self | |
612 | ||
613 | def __str__(self): | |
614 | strings = [] | |
615 | ||
616 | for elem in self: | |
617 | if isinstance(elem, StringValue): | |
618 | strings.append(repr(elem.value)) | |
619 | else: | |
620 | strings.append(str(elem)) | |
621 | ||
622 | return '[{}]'.format(', '.join(strings)) | |
623 | ||
624 | def insert(self, value): | |
625 | raise NotImplementedError | |
626 | ||
627 | ||
628 | class _MapValueKeyIterator(collections.abc.Iterator): | |
629 | def __init__(self, map_obj): | |
630 | self._map_obj = map_obj | |
631 | self._at = 0 | |
632 | keys_ptr = native_bt.value_map_get_keys_private(map_obj._ptr) | |
633 | ||
634 | if keys_ptr is None: | |
635 | raise RuntimeError('unexpected error: cannot get map value object keys') | |
636 | ||
637 | self._keys = _create_from_ptr(keys_ptr) | |
638 | ||
639 | def __next__(self): | |
640 | if self._at == len(self._map_obj): | |
641 | raise StopIteration | |
642 | ||
643 | key = self._keys[self._at] | |
644 | self._at += 1 | |
645 | return str(key) | |
646 | ||
647 | ||
648 | class MapValue(_Container, collections.abc.MutableMapping, _Value): | |
649 | _NAME = 'Map' | |
650 | ||
651 | def __init__(self, value=None): | |
652 | ptr = native_bt.value_map_create() | |
653 | self._check_create_status(ptr) | |
654 | super().__init__(ptr) | |
655 | ||
656 | # Python will raise a TypeError if there's anything wrong with | |
657 | # the iterable/mapping protocol. | |
658 | if value is not None: | |
659 | for key, elem in value.items(): | |
660 | self[key] = elem | |
661 | ||
662 | def __eq__(self, other): | |
663 | return _Value.__eq__(self, other) | |
664 | ||
665 | def __ne__(self, other): | |
666 | return _Value.__ne__(self, other) | |
667 | ||
668 | def _spec_eq(self, other): | |
669 | try: | |
670 | if len(self) != len(other): | |
671 | # early mismatch | |
672 | return False | |
673 | ||
674 | for self_key in self: | |
675 | if self_key not in other: | |
676 | return False | |
677 | ||
678 | self_value = self[self_key] | |
679 | other_value = other[self_key] | |
680 | ||
681 | if self_value != other_value: | |
682 | return False | |
683 | ||
684 | return True | |
685 | except: | |
686 | return | |
687 | ||
688 | def __len__(self): | |
689 | size = native_bt.value_map_size(self._ptr) | |
f6a5e476 | 690 | assert(size >= 0) |
81447b5b PP |
691 | return size |
692 | ||
693 | def __contains__(self, key): | |
694 | self._check_key_type(key) | |
695 | return native_bt.value_map_has_key(self._ptr, key) | |
696 | ||
697 | def _check_key_type(self, key): | |
698 | utils._check_str(key) | |
699 | ||
700 | def _check_key(self, key): | |
701 | if key not in self: | |
702 | raise KeyError(key) | |
703 | ||
704 | def __getitem__(self, key): | |
705 | self._check_key(key) | |
706 | ptr = native_bt.value_map_get(self._ptr, key) | |
f6a5e476 | 707 | assert(ptr) |
81447b5b PP |
708 | return _create_from_ptr(ptr) |
709 | ||
710 | def __iter__(self): | |
711 | return _MapValueKeyIterator(self) | |
712 | ||
713 | def __setitem__(self, key, value): | |
714 | self._check_key_type(key) | |
715 | value = create_value(value) | |
716 | ||
717 | if value is None: | |
718 | ptr = native_bt.value_null | |
719 | else: | |
720 | ptr = value._ptr | |
721 | ||
722 | status = native_bt.value_map_insert(self._ptr, key, ptr) | |
723 | self._handle_status(status) | |
724 | ||
725 | def __str__(self): | |
726 | strings = [] | |
727 | ||
728 | for key, elem in self.items(): | |
729 | if isinstance(elem, StringValue): | |
730 | value = repr(elem.value) | |
731 | else: | |
732 | value = str(elem) | |
733 | ||
734 | strings.append('{}: {}'.format(repr(key), value)) | |
735 | ||
736 | return '{{{}}}'.format(', '.join(strings)) | |
737 | ||
738 | ||
739 | _TYPE_TO_OBJ = { | |
740 | native_bt.VALUE_TYPE_BOOL: BoolValue, | |
741 | native_bt.VALUE_TYPE_INTEGER: IntegerValue, | |
742 | native_bt.VALUE_TYPE_FLOAT: FloatValue, | |
743 | native_bt.VALUE_TYPE_STRING: StringValue, | |
744 | native_bt.VALUE_TYPE_ARRAY: ArrayValue, | |
745 | native_bt.VALUE_TYPE_MAP: MapValue, | |
746 | } |