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