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 bt2.field_types | |
25 | import collections.abc | |
26 | import functools | |
27 | import numbers | |
28 | import math | |
29 | import abc | |
30 | import bt2 | |
31 | ||
32 | ||
33 | def _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 | ||
46 | class _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 | |
63 | class _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 | ||
198 | class _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 | ||
253 | class _RealField(_NumericField, numbers.Real): | |
254 | pass | |
255 | ||
256 | ||
257 | class _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 | ||
f6a5e476 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 | ||
298 | class _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) | |
f6a5e476 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 | ||
324 | class _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) | |
f6a5e476 | 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): | |
f6a5e476 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 | |
350 | class _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) | |
f6a5e476 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 | ||
411 | class _ContainerField(_Field): | |
412 | def __bool__(self): | |
413 | return len(self) != 0 | |
414 | ||
415 | def __len__(self): | |
416 | count = self._count() | |
f6a5e476 | 417 | assert(count >= 0) |
81447b5b PP |
418 | return count |
419 | ||
420 | def __delitem__(self, index): | |
421 | raise NotImplementedError | |
422 | ||
423 | ||
424 | class _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) | |
f6a5e476 | 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) | |
f6a5e476 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) |
f6a5e476 | 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 | ||
d5697082 JG |
486 | @property |
487 | def value(self): | |
488 | return {key: field.value for key, field in self.items()} | |
489 | ||
490 | @value.setter | |
491 | def value(self, values): | |
492 | if not hasattr(type(values), '__getitem__'): | |
493 | raise TypeError('expecting a Mapping collection') | |
494 | ||
495 | for key, value in values.items(): | |
496 | self[key].value = value | |
497 | ||
81447b5b PP |
498 | |
499 | class _VariantField(_Field): | |
500 | _NAME = 'Variant' | |
501 | ||
502 | @property | |
503 | def tag_field(self): | |
504 | field_ptr = native_bt.ctf_field_variant_get_tag(self._ptr) | |
f6a5e476 PP |
505 | |
506 | if field_ptr is None: | |
507 | return | |
508 | ||
81447b5b PP |
509 | return _create_from_ptr(field_ptr) |
510 | ||
511 | @property | |
512 | def selected_field(self): | |
513 | return self.field() | |
514 | ||
515 | def field(self, tag_field=None): | |
516 | if tag_field is None: | |
517 | field_ptr = native_bt.ctf_field_variant_get_current_field(self._ptr) | |
f6a5e476 PP |
518 | |
519 | if field_ptr is None: | |
520 | return | |
81447b5b PP |
521 | else: |
522 | utils._check_type(tag_field, _EnumerationField) | |
523 | field_ptr = native_bt.ctf_field_variant_get_field(self._ptr, tag_field._ptr) | |
524 | utils._handle_ptr(field_ptr, "cannot select variant field object's field") | |
525 | ||
526 | return _create_from_ptr(field_ptr) | |
527 | ||
528 | def __eq__(self, other): | |
f6a5e476 PP |
529 | if type(other) is not type(self): |
530 | return False | |
531 | ||
532 | if self.addr == other.addr: | |
533 | return True | |
534 | ||
535 | return self.selected_field == other.selected_field | |
536 | ||
537 | def __bool__(self): | |
538 | return bool(self.selected_field) | |
81447b5b PP |
539 | |
540 | ||
541 | class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence): | |
542 | def __getitem__(self, index): | |
543 | if not isinstance(index, numbers.Integral): | |
544 | raise TypeError("'{}' is not an integral number object: invalid index".format(index.__class__.__name__)) | |
545 | ||
546 | index = int(index) | |
547 | ||
548 | if index < 0 or index >= len(self): | |
549 | raise IndexError('{} field object index is out of range'.format(self._NAME)) | |
550 | ||
551 | field_ptr = self._get_field_ptr_at_index(index) | |
f6a5e476 | 552 | assert(field_ptr) |
81447b5b PP |
553 | return _create_from_ptr(field_ptr) |
554 | ||
555 | def __setitem__(self, index, value): | |
556 | # we can only set numbers and strings | |
557 | if not isinstance(value, (numbers.Number, _StringField, str)): | |
558 | raise TypeError('expecting number or string object') | |
559 | ||
560 | # raises if index is somehow invalid | |
561 | field = self[index] | |
562 | ||
563 | if not isinstance(field, (_NumericField, _StringField)): | |
564 | raise TypeError('can only set the value of a number or string field') | |
565 | ||
566 | # the field's property does the appropriate conversion or raises | |
567 | # the appropriate exception | |
568 | field.value = value | |
569 | ||
570 | def insert(self, index, value): | |
571 | raise NotImplementedError | |
572 | ||
573 | def __eq__(self, other): | |
574 | if not isinstance(other, collections.abc.Sequence): | |
575 | return False | |
576 | ||
577 | if len(self) != len(other): | |
578 | return False | |
579 | ||
580 | for self_field, other_field in zip(self, other): | |
581 | if self_field != other_field: | |
582 | return False | |
583 | ||
584 | return True | |
585 | ||
d5697082 JG |
586 | @property |
587 | def value(self): | |
588 | return [field.value for field in self] | |
589 | ||
590 | @value.setter | |
591 | def value(self, values): | |
592 | if not hasattr(type(values), '__iter__'): | |
593 | raise TypeError('expecting an iterable container (Sequence)') | |
594 | ||
595 | if len(self) != len(values): | |
596 | raise ValueError('expected length of value and field to match') | |
597 | ||
598 | for index, value in enumerate(values): | |
599 | self[index].value = value | |
600 | ||
81447b5b PP |
601 | |
602 | class _ArrayField(_ArraySequenceField): | |
603 | _NAME = 'Array' | |
604 | ||
605 | def _count(self): | |
606 | return self.field_type.length | |
607 | ||
608 | def _get_field_ptr_at_index(self, index): | |
609 | return native_bt.ctf_field_array_get_field(self._ptr, index) | |
610 | ||
611 | ||
612 | class _SequenceField(_ArraySequenceField): | |
613 | _NAME = 'Sequence' | |
614 | ||
615 | def _count(self): | |
616 | return self.length_field.value | |
617 | ||
618 | @property | |
619 | def length_field(self): | |
620 | field_ptr = native_bt.ctf_field_sequence_get_length(self._ptr) | |
621 | utils._handle_ptr("cannot get sequence field object's length field") | |
622 | return _create_from_ptr(field_ptr) | |
623 | ||
624 | @length_field.setter | |
625 | def length_field(self, length_field): | |
626 | utils._check_type(length_field, _IntegerField) | |
627 | ret = native_bt.ctf_field_sequence_set_length(self._ptr, length_field._ptr) | |
628 | utils._handle_ret(ret, "cannot set sequence field object's length field") | |
629 | ||
630 | def _get_field_ptr_at_index(self, index): | |
631 | return native_bt.ctf_field_sequence_get_field(self._ptr, index) | |
632 | ||
633 | ||
634 | _TYPE_ID_TO_OBJ = { | |
f6a5e476 PP |
635 | native_bt.CTF_FIELD_TYPE_ID_INTEGER: _IntegerField, |
636 | native_bt.CTF_FIELD_TYPE_ID_FLOAT: _FloatingPointNumberField, | |
637 | native_bt.CTF_FIELD_TYPE_ID_ENUM: _EnumerationField, | |
638 | native_bt.CTF_FIELD_TYPE_ID_STRING: _StringField, | |
639 | native_bt.CTF_FIELD_TYPE_ID_STRUCT: _StructureField, | |
640 | native_bt.CTF_FIELD_TYPE_ID_ARRAY: _ArrayField, | |
641 | native_bt.CTF_FIELD_TYPE_ID_SEQUENCE: _SequenceField, | |
642 | native_bt.CTF_FIELD_TYPE_ID_VARIANT: _VariantField, | |
81447b5b | 643 | } |