Cleanup: line exceeds 80 chars
[babeltrace.git] / bindings / python / bt2 / bt2 / fields.py
CommitLineData
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
23from bt2 import native_bt, object, utils
24import bt2.field_types
25import collections.abc
26import functools
27import numbers
28import math
29import abc
30import bt2
31
32
e1c6bebd
JG
33def _get_leaf_field(obj):
34 if type(obj) is not _VariantField:
35 return obj
36
37 return _get_leaf_field(obj.selected_field)
38
39
81447b5b
PP
40def _create_from_ptr(ptr):
41 # recreate the field type wrapper of this field's type (the identity
42 # could be different, but the underlying address should be the
43 # same)
44 field_type_ptr = native_bt.ctf_field_get_type(ptr)
45 utils._handle_ptr(field_type_ptr, "cannot get field object's type")
46 field_type = bt2.field_types._create_from_ptr(field_type_ptr)
47 typeid = native_bt.ctf_field_type_get_type_id(field_type._ptr)
48 field = _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr)
49 field._field_type = field_type
50 return field
51
52
53class _Field(object._Object, metaclass=abc.ABCMeta):
54 def __copy__(self):
55 ptr = native_bt.ctf_field_copy(self._ptr)
56 utils._handle_ptr(ptr, 'cannot copy {} field object'.format(self._NAME.lower()))
57 return _create_from_ptr(ptr)
58
59 def __deepcopy__(self, memo):
60 cpy = self.__copy__()
61 memo[id(self)] = cpy
62 return cpy
63
e1c6bebd
JG
64 def __eq__(self, other):
65 # special case: two unset fields with the same field type are equal
66 if isinstance(other, _Field):
67 if not self.is_set or not other.is_set:
68 if not self.is_set and not other.is_set and self.field_type == other.field_type:
69 return True
70 return False
71
72 other = _get_leaf_field(other)
73 return self._spec_eq(other)
74
81447b5b
PP
75 @property
76 def field_type(self):
77 return self._field_type
78
742e4747
JG
79 @property
80 def is_set(self):
81 is_set = native_bt.ctf_field_value_is_set(self._ptr)
82 return is_set > 0
83
84 def reset(self):
85 ret = native_bt.ctf_field_reset_value(self._ptr)
86 utils._handle_ret(ret, "cannot reset field object's value")
87
81447b5b
PP
88
89@functools.total_ordering
90class _NumericField(_Field):
91 @staticmethod
92 def _extract_value(other):
93 if other is True or other is False:
94 return other
95
96 if isinstance(other, numbers.Integral):
97 return int(other)
98
99 if isinstance(other, numbers.Real):
100 return float(other)
101
102 if isinstance(other, numbers.Complex):
103 return complex(other)
104
105 raise TypeError("'{}' object is not a number object".format(other.__class__.__name__))
106
107 def __int__(self):
e1c6bebd 108 return int(self._value)
81447b5b
PP
109
110 def __float__(self):
e1c6bebd 111 return float(self._value)
81447b5b
PP
112
113 def __str__(self):
e1c6bebd 114 return str(self._value)
81447b5b
PP
115
116 def __lt__(self, other):
117 if not isinstance(other, numbers.Number):
118 raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__,
119 other.__class__.__name__))
120
e1c6bebd 121 return self._value < float(other)
81447b5b
PP
122
123 def __le__(self, other):
124 if not isinstance(other, numbers.Number):
125 raise TypeError('unorderable types: {}() <= {}()'.format(self.__class__.__name__,
126 other.__class__.__name__))
127
e1c6bebd 128 return self._value <= float(other)
81447b5b 129
e1c6bebd 130 def _spec_eq(self, other):
81447b5b
PP
131 if not isinstance(other, numbers.Number):
132 return False
133
e1c6bebd 134 return self._value == complex(other)
81447b5b
PP
135
136 def __rmod__(self, other):
e1c6bebd 137 return self._extract_value(other) % self._value
81447b5b
PP
138
139 def __mod__(self, other):
e1c6bebd 140 return self._value % self._extract_value(other)
81447b5b
PP
141
142 def __rfloordiv__(self, other):
e1c6bebd 143 return self._extract_value(other) // self._value
81447b5b
PP
144
145 def __floordiv__(self, other):
e1c6bebd 146 return self._value // self._extract_value(other)
81447b5b
PP
147
148 def __round__(self, ndigits=None):
149 if ndigits is None:
e1c6bebd 150 return round(self._value)
81447b5b 151 else:
e1c6bebd 152 return round(self._value, ndigits)
81447b5b
PP
153
154 def __ceil__(self):
e1c6bebd 155 return math.ceil(self._value)
81447b5b
PP
156
157 def __floor__(self):
e1c6bebd 158 return math.floor(self._value)
81447b5b
PP
159
160 def __trunc__(self):
e1c6bebd 161 return int(self._value)
81447b5b
PP
162
163 def __abs__(self):
e1c6bebd 164 return abs(self._value)
81447b5b
PP
165
166 def __add__(self, other):
e1c6bebd 167 return self._value + self._extract_value(other)
81447b5b
PP
168
169 def __radd__(self, other):
170 return self.__add__(other)
171
172 def __neg__(self):
e1c6bebd 173 return -self._value
81447b5b
PP
174
175 def __pos__(self):
e1c6bebd 176 return +self._value
81447b5b
PP
177
178 def __mul__(self, other):
e1c6bebd 179 return self._value * self._extract_value(other)
81447b5b
PP
180
181 def __rmul__(self, other):
182 return self.__mul__(other)
183
184 def __truediv__(self, other):
e1c6bebd 185 return self._value / self._extract_value(other)
81447b5b
PP
186
187 def __rtruediv__(self, other):
e1c6bebd 188 return self._extract_value(other) / self._value
81447b5b
PP
189
190 def __pow__(self, exponent):
e1c6bebd 191 return self._value ** self._extract_value(exponent)
81447b5b
PP
192
193 def __rpow__(self, base):
e1c6bebd 194 return self._extract_value(base) ** self._value
81447b5b
PP
195
196 def __iadd__(self, other):
197 self.value = self + other
198 return self
199
200 def __isub__(self, other):
201 self.value = self - other
202 return self
203
204 def __imul__(self, other):
205 self.value = self * other
206 return self
207
208 def __itruediv__(self, other):
209 self.value = self / other
210 return self
211
212 def __ifloordiv__(self, other):
213 self.value = self // other
214 return self
215
216 def __imod__(self, other):
217 self.value = self % other
218 return self
219
220 def __ipow__(self, other):
221 self.value = self ** other
222 return self
223
224
225class _IntegralField(_NumericField, numbers.Integral):
226 def __lshift__(self, other):
e1c6bebd 227 return self._value << self._extract_value(other)
81447b5b
PP
228
229 def __rlshift__(self, other):
e1c6bebd 230 return self._extract_value(other) << self._value
81447b5b
PP
231
232 def __rshift__(self, other):
e1c6bebd 233 return self._value >> self._extract_value(other)
81447b5b
PP
234
235 def __rrshift__(self, other):
e1c6bebd 236 return self._extract_value(other) >> self._value
81447b5b
PP
237
238 def __and__(self, other):
e1c6bebd 239 return self._value & self._extract_value(other)
81447b5b
PP
240
241 def __rand__(self, other):
e1c6bebd 242 return self._extract_value(other) & self._value
81447b5b
PP
243
244 def __xor__(self, other):
e1c6bebd 245 return self._value ^ self._extract_value(other)
81447b5b
PP
246
247 def __rxor__(self, other):
e1c6bebd 248 return self._extract_value(other) ^ self._value
81447b5b
PP
249
250 def __or__(self, other):
e1c6bebd 251 return self._value | self._extract_value(other)
81447b5b
PP
252
253 def __ror__(self, other):
e1c6bebd 254 return self._extract_value(other) | self._value
81447b5b
PP
255
256 def __invert__(self):
e1c6bebd 257 return ~self._value
81447b5b
PP
258
259 def __ilshift__(self, other):
260 self.value = self << other
261 return self
262
263 def __irshift__(self, other):
264 self.value = self >> other
265 return self
266
267 def __iand__(self, other):
268 self.value = self & other
269 return self
270
271 def __ixor__(self, other):
272 self.value = self ^ other
273 return self
274
275 def __ior__(self, other):
276 self.value = self | other
277 return self
278
279
280class _RealField(_NumericField, numbers.Real):
281 pass
282
283
284class _IntegerField(_IntegralField):
285 _NAME = 'Integer'
286
287 def _value_to_int(self, value):
288 if not isinstance(value, numbers.Real):
289 raise TypeError('expecting a real number object')
290
291 value = int(value)
292
293 if self.field_type.is_signed:
294 utils._check_int64(value)
295 else:
296 utils._check_uint64(value)
297
298 return value
299
300 @property
e1c6bebd 301 def _value(self):
81447b5b
PP
302 if self.field_type.is_signed:
303 ret, value = native_bt.ctf_field_signed_integer_get_value(self._ptr)
304 else:
305 ret, value = native_bt.ctf_field_unsigned_integer_get_value(self._ptr)
306
811644b8 307 if ret < 0:
e1c6bebd
JG
308 if not self.is_set:
309 return
310
311 utils._handle_ret(ret, "cannot get integer field's value")
811644b8 312
81447b5b
PP
313 return value
314
e1c6bebd 315 def _set_value(self, value):
81447b5b
PP
316 value = self._value_to_int(value)
317
318 if self.field_type.is_signed:
319 ret = native_bt.ctf_field_signed_integer_set_value(self._ptr, value)
320 else:
321 ret = native_bt.ctf_field_unsigned_integer_set_value(self._ptr, value)
322
323 utils._handle_ret(ret, "cannot set integer field object's value")
324
e1c6bebd 325 value = property(fset=_set_value)
81447b5b
PP
326
327class _FloatingPointNumberField(_RealField):
328 _NAME = 'Floating point number'
329
330 def _value_to_float(self, value):
331 if not isinstance(value, numbers.Real):
332 raise TypeError("expecting a real number object")
333
334 return float(value)
335
336 @property
e1c6bebd 337 def _value(self):
81447b5b 338 ret, value = native_bt.ctf_field_floating_point_get_value(self._ptr)
811644b8
PP
339
340 if ret < 0:
e1c6bebd
JG
341 if not self.is_set:
342 return
343
344 utils._handle_ret(ret, "cannot get floating point number field's value")
811644b8 345
81447b5b
PP
346 return value
347
e1c6bebd 348 def _set_value(self, value):
81447b5b
PP
349 value = self._value_to_float(value)
350 ret = native_bt.ctf_field_floating_point_set_value(self._ptr, value)
351 utils._handle_ret(ret, "cannot set floating point number field object's value")
352
e1c6bebd 353 value = property(fset=_set_value)
81447b5b
PP
354
355class _EnumerationField(_IntegerField):
356 _NAME = 'Enumeration'
357
358 @property
359 def integer_field(self):
360 int_field_ptr = native_bt.ctf_field_enumeration_get_container(self._ptr)
811644b8 361 assert(int_field_ptr)
81447b5b
PP
362 return _create_from_ptr(int_field_ptr)
363
e1c6bebd 364 def _set_value(self, value):
81447b5b
PP
365 self.integer_field.value = value
366
e1c6bebd
JG
367 value = property(fset=_set_value)
368
369 @property
370 def _value(self):
371 return self.integer_field._value
372
81447b5b
PP
373 @property
374 def mappings(self):
811644b8
PP
375 iter_ptr = native_bt.ctf_field_enumeration_get_mappings(self._ptr)
376 assert(iter_ptr)
377 return bt2.field_types._EnumerationFieldTypeMappingIterator(iter_ptr,
378 self.field_type.is_signed)
81447b5b
PP
379
380
381@functools.total_ordering
382class _StringField(_Field, collections.abc.Sequence):
383 _NAME = 'String'
384
385 def _value_to_str(self, value):
386 if isinstance(value, self.__class__):
e1c6bebd 387 value = value._value
81447b5b
PP
388
389 if not isinstance(value, str):
390 raise TypeError("expecting a 'str' object")
391
392 return value
393
394 @property
e1c6bebd 395 def _value(self):
81447b5b 396 value = native_bt.ctf_field_string_get_value(self._ptr)
81447b5b
PP
397 return value
398
e1c6bebd 399 def _set_value(self, value):
81447b5b
PP
400 value = self._value_to_str(value)
401 ret = native_bt.ctf_field_string_set_value(self._ptr, value)
402 utils._handle_ret(ret, "cannot set string field object's value")
403
e1c6bebd
JG
404 value = property(fset=_set_value)
405
406 def _spec_eq(self, other):
81447b5b
PP
407 try:
408 other = self._value_to_str(other)
409 except:
410 return False
411
e1c6bebd 412 return self._value == other
81447b5b
PP
413
414 def __le__(self, other):
e1c6bebd 415 return self._value <= self._value_to_str(other)
81447b5b
PP
416
417 def __lt__(self, other):
e1c6bebd 418 return self._value < self._value_to_str(other)
81447b5b
PP
419
420 def __bool__(self):
e1c6bebd 421 return bool(self._value)
81447b5b
PP
422
423 def __str__(self):
e1c6bebd 424 return self._value
81447b5b
PP
425
426 def __getitem__(self, index):
e1c6bebd 427 return self._value[index]
81447b5b
PP
428
429 def __len__(self):
e1c6bebd 430 return len(self._value)
81447b5b
PP
431
432 def __iadd__(self, value):
433 value = self._value_to_str(value)
434 ret = native_bt.ctf_field_string_append(self._ptr, value)
435 utils._handle_ret(ret, "cannot append to string field object's value")
436 return self
437
438
439class _ContainerField(_Field):
440 def __bool__(self):
441 return len(self) != 0
442
443 def __len__(self):
444 count = self._count()
811644b8 445 assert(count >= 0)
81447b5b
PP
446 return count
447
448 def __delitem__(self, index):
449 raise NotImplementedError
450
451
452class _StructureField(_ContainerField, collections.abc.MutableMapping):
453 _NAME = 'Structure'
454
455 def _count(self):
456 return len(self.field_type)
457
458 def __getitem__(self, key):
459 utils._check_str(key)
811644b8 460 ptr = native_bt.ctf_field_structure_get_field_by_name(self._ptr, key)
81447b5b
PP
461
462 if ptr is None:
463 raise KeyError(key)
464
465 return _create_from_ptr(ptr)
466
467 def __setitem__(self, key, value):
468 # we can only set numbers and strings
469 if not isinstance(value, (numbers.Number, str)):
470 raise TypeError('expecting number object or string')
471
472 # raises if index is somehow invalid
473 field = self[key]
474
475 if not isinstance(field, (_NumericField, _StringField)):
476 raise TypeError('can only set the value of a number or string field')
477
478 # the field's property does the appropriate conversion or raises
479 # the appropriate exception
480 field.value = value
481
482 def at_index(self, index):
483 utils._check_uint64(index)
811644b8
PP
484
485 if index >= len(self):
486 raise IndexError
487
81447b5b 488 field_ptr = native_bt.ctf_field_structure_get_field_by_index(self._ptr, index)
811644b8 489 assert(field_ptr)
81447b5b
PP
490 return _create_from_ptr(field_ptr)
491
492 def __iter__(self):
493 # same name iterator
494 return iter(self.field_type)
495
e1c6bebd
JG
496 def _spec_eq(self, other):
497 try:
498 if len(self) != len(other):
81447b5b
PP
499 return False
500
e1c6bebd
JG
501 for self_key, self_value in self.items():
502 if self_key not in other:
503 return False
81447b5b 504
e1c6bebd 505 other_value = other[self_key]
81447b5b 506
e1c6bebd
JG
507 if self_value != other_value:
508 return False
509
510 return True
511 except:
512 return False
81447b5b 513
7c54e2e7 514 @property
e1c6bebd
JG
515 def _value(self):
516 return {key: value._value for key, value in self.items()}
7c54e2e7 517
e1c6bebd
JG
518 def _set_value(self, values):
519 original_values = self._value
7c54e2e7 520
e1c6bebd
JG
521 try:
522 for key, value in values.items():
523 self[key].value = value
524 except:
525 self.value = original_values
526 raise
7c54e2e7 527
e1c6bebd 528 value = property(fset=_set_value)
81447b5b
PP
529
530class _VariantField(_Field):
531 _NAME = 'Variant'
532
533 @property
534 def tag_field(self):
535 field_ptr = native_bt.ctf_field_variant_get_tag(self._ptr)
811644b8
PP
536
537 if field_ptr is None:
538 return
539
81447b5b
PP
540 return _create_from_ptr(field_ptr)
541
542 @property
543 def selected_field(self):
544 return self.field()
545
546 def field(self, tag_field=None):
547 if tag_field is None:
548 field_ptr = native_bt.ctf_field_variant_get_current_field(self._ptr)
811644b8
PP
549
550 if field_ptr is None:
551 return
81447b5b
PP
552 else:
553 utils._check_type(tag_field, _EnumerationField)
554 field_ptr = native_bt.ctf_field_variant_get_field(self._ptr, tag_field._ptr)
555 utils._handle_ptr(field_ptr, "cannot select variant field object's field")
556
557 return _create_from_ptr(field_ptr)
558
e1c6bebd
JG
559 def _spec_eq(self, other):
560 return _get_leaf_field(self) == other
811644b8
PP
561
562 def __bool__(self):
563 return bool(self.selected_field)
81447b5b 564
e1c6bebd
JG
565 @property
566 def _value(self):
567 if self.selected_field is not None:
568 return self.selected_field._value
569
570 def _set_value(self, value):
571 self.selected_field.value = value
572
573 value = property(fset=_set_value)
81447b5b
PP
574
575class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence):
576 def __getitem__(self, index):
577 if not isinstance(index, numbers.Integral):
578 raise TypeError("'{}' is not an integral number object: invalid index".format(index.__class__.__name__))
579
580 index = int(index)
581
582 if index < 0 or index >= len(self):
583 raise IndexError('{} field object index is out of range'.format(self._NAME))
584
585 field_ptr = self._get_field_ptr_at_index(index)
811644b8 586 assert(field_ptr)
81447b5b
PP
587 return _create_from_ptr(field_ptr)
588
589 def __setitem__(self, index, value):
590 # we can only set numbers and strings
591 if not isinstance(value, (numbers.Number, _StringField, str)):
592 raise TypeError('expecting number or string object')
593
594 # raises if index is somehow invalid
595 field = self[index]
596
597 if not isinstance(field, (_NumericField, _StringField)):
598 raise TypeError('can only set the value of a number or string field')
599
600 # the field's property does the appropriate conversion or raises
601 # the appropriate exception
602 field.value = value
603
604 def insert(self, index, value):
605 raise NotImplementedError
606
e1c6bebd
JG
607 def _spec_eq(self, other):
608 try:
609 if len(self) != len(other):
81447b5b
PP
610 return False
611
e1c6bebd
JG
612 for self_field, other_field in zip(self, other):
613 if self_field != other_field:
614 return False
7c54e2e7 615
e1c6bebd
JG
616 return True
617 except:
618 return False
7c54e2e7 619
e1c6bebd
JG
620 @property
621 def _value(self):
622 return [field._value for field in self]
7c54e2e7 623
81447b5b
PP
624
625class _ArrayField(_ArraySequenceField):
626 _NAME = 'Array'
627
628 def _count(self):
629 return self.field_type.length
630
631 def _get_field_ptr_at_index(self, index):
632 return native_bt.ctf_field_array_get_field(self._ptr, index)
633
e1c6bebd
JG
634 def _set_value(self, values):
635 if len(self) != len(values):
636 raise ValueError(
637 'expected length of value and array field to match')
638
639 original_values = self._value
640 try:
641 for index, value in enumerate(values):
642 if value is not None:
643 self[index].value = value
644 else:
645 self[index].reset()
646 except:
647 self.value = original_values
648 raise
649
650 value = property(fset=_set_value)
651
81447b5b
PP
652
653class _SequenceField(_ArraySequenceField):
654 _NAME = 'Sequence'
655
656 def _count(self):
a29094be 657 return int(self.length_field)
81447b5b
PP
658
659 @property
660 def length_field(self):
661 field_ptr = native_bt.ctf_field_sequence_get_length(self._ptr)
a29094be
JG
662 if field_ptr is None:
663 return
81447b5b
PP
664 return _create_from_ptr(field_ptr)
665
666 @length_field.setter
667 def length_field(self, length_field):
668 utils._check_type(length_field, _IntegerField)
669 ret = native_bt.ctf_field_sequence_set_length(self._ptr, length_field._ptr)
670 utils._handle_ret(ret, "cannot set sequence field object's length field")
671
672 def _get_field_ptr_at_index(self, index):
673 return native_bt.ctf_field_sequence_get_field(self._ptr, index)
674
e1c6bebd
JG
675 def _set_value(self, values):
676 original_length_field = self.length_field
677 if original_length_field is not None:
678 original_values = self._value
679
680 if len(values) != self.length_field:
681 if self.length_field is not None:
682 length_ft = self.length_field.field_type
683 else:
684 length_ft = bt2.IntegerFieldType(size=64, is_signed=False)
685 self.length_field = length_ft(len(values))
686
687 try:
688 for index, value in enumerate(values):
689 if value is not None:
690 self[index].value = value
691 else:
692 self[index].reset()
693 except:
694 if original_length_field is not None:
695 self.length_field = original_length_field
696 self.value = original_values
697 else:
698 self.reset()
699 raise
700
701 value = property(fset=_set_value)
81447b5b
PP
702
703_TYPE_ID_TO_OBJ = {
811644b8
PP
704 native_bt.CTF_FIELD_TYPE_ID_INTEGER: _IntegerField,
705 native_bt.CTF_FIELD_TYPE_ID_FLOAT: _FloatingPointNumberField,
706 native_bt.CTF_FIELD_TYPE_ID_ENUM: _EnumerationField,
707 native_bt.CTF_FIELD_TYPE_ID_STRING: _StringField,
708 native_bt.CTF_FIELD_TYPE_ID_STRUCT: _StructureField,
709 native_bt.CTF_FIELD_TYPE_ID_ARRAY: _ArrayField,
710 native_bt.CTF_FIELD_TYPE_ID_SEQUENCE: _SequenceField,
711 native_bt.CTF_FIELD_TYPE_ID_VARIANT: _VariantField,
81447b5b 712}
This page took 0.058461 seconds and 4 git commands to generate.