| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | import math |
| 6 | import numbers |
| 7 | import functools |
| 8 | import collections.abc |
| 9 | |
| 10 | from bt2 import utils as bt2_utils |
| 11 | from bt2 import object as bt2_object |
| 12 | from bt2 import native_bt |
| 13 | from bt2 import field_class as bt2_field_class |
| 14 | |
| 15 | |
| 16 | def _create_field_from_ptr_template( |
| 17 | object_map, ptr, owner_ptr, owner_get_ref, owner_put_ref |
| 18 | ): |
| 19 | field_class_ptr = native_bt.field_borrow_class_const(ptr) |
| 20 | typeid = native_bt.field_class_get_type(field_class_ptr) |
| 21 | field = object_map[typeid]._create_from_ptr_and_get_ref( |
| 22 | ptr, owner_ptr, owner_get_ref, owner_put_ref |
| 23 | ) |
| 24 | return field |
| 25 | |
| 26 | |
| 27 | def _create_field_from_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref): |
| 28 | return _create_field_from_ptr_template( |
| 29 | _TYPE_ID_TO_OBJ, ptr, owner_ptr, owner_get_ref, owner_put_ref |
| 30 | ) |
| 31 | |
| 32 | |
| 33 | def _create_field_from_const_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref): |
| 34 | return _create_field_from_ptr_template( |
| 35 | _TYPE_ID_TO_CONST_OBJ, ptr, owner_ptr, owner_get_ref, owner_put_ref |
| 36 | ) |
| 37 | |
| 38 | |
| 39 | # Get the "effective" field of `field`. If `field` is a variant, return |
| 40 | # the currently selected field. If `field` is an option, return the |
| 41 | # content field. If `field` is of any other type, return `field` |
| 42 | # directly. |
| 43 | |
| 44 | |
| 45 | def _get_leaf_field(field): |
| 46 | if isinstance(field, _VariantFieldConst): |
| 47 | return _get_leaf_field(field.selected_option) |
| 48 | |
| 49 | if isinstance(field, _OptionFieldConst): |
| 50 | return _get_leaf_field(field.field) |
| 51 | |
| 52 | return field |
| 53 | |
| 54 | |
| 55 | class _FieldConst(bt2_object._UniqueObject): |
| 56 | _create_field_from_ptr = staticmethod(_create_field_from_const_ptr) |
| 57 | _create_field_class_from_ptr_and_get_ref = staticmethod( |
| 58 | bt2_field_class._create_field_class_from_const_ptr_and_get_ref |
| 59 | ) |
| 60 | _borrow_class_ptr = staticmethod(native_bt.field_borrow_class_const) |
| 61 | |
| 62 | def __eq__(self, other): |
| 63 | other = _get_leaf_field(other) |
| 64 | return self._spec_eq(other) |
| 65 | |
| 66 | @property |
| 67 | def cls(self): |
| 68 | field_class_ptr = self._borrow_class_ptr(self._ptr) |
| 69 | assert field_class_ptr is not None |
| 70 | return self._create_field_class_from_ptr_and_get_ref(field_class_ptr) |
| 71 | |
| 72 | def _repr(self): |
| 73 | raise NotImplementedError |
| 74 | |
| 75 | def __repr__(self): |
| 76 | return self._repr() |
| 77 | |
| 78 | |
| 79 | class _Field(_FieldConst): |
| 80 | _create_field_from_ptr = staticmethod(_create_field_from_ptr) |
| 81 | _create_field_class_from_ptr_and_get_ref = staticmethod( |
| 82 | bt2_field_class._create_field_class_from_ptr_and_get_ref |
| 83 | ) |
| 84 | _borrow_class_ptr = staticmethod(native_bt.field_borrow_class) |
| 85 | |
| 86 | |
| 87 | class _BitArrayFieldConst(_FieldConst): |
| 88 | _NAME = "Const bit array" |
| 89 | |
| 90 | @property |
| 91 | def value_as_integer(self): |
| 92 | return native_bt.field_bit_array_get_value_as_integer(self._ptr) |
| 93 | |
| 94 | def _spec_eq(self, other): |
| 95 | if type(other) is not type(self): |
| 96 | return False |
| 97 | |
| 98 | return self.value_as_integer == other.value_as_integer |
| 99 | |
| 100 | def _repr(self): |
| 101 | return repr(self.value_as_integer) |
| 102 | |
| 103 | def __str__(self): |
| 104 | return str(self.value_as_integer) |
| 105 | |
| 106 | def __len__(self): |
| 107 | return self.cls.length |
| 108 | |
| 109 | |
| 110 | class _BitArrayField(_BitArrayFieldConst, _Field): |
| 111 | _NAME = "Bit array" |
| 112 | |
| 113 | def _value_as_integer(self, value): |
| 114 | bt2_utils._check_uint64(value) |
| 115 | native_bt.field_bit_array_set_value_as_integer(self._ptr, value) |
| 116 | |
| 117 | value_as_integer = property( |
| 118 | fget=_BitArrayFieldConst.value_as_integer.fget, fset=_value_as_integer |
| 119 | ) |
| 120 | |
| 121 | |
| 122 | @functools.total_ordering |
| 123 | class _NumericFieldConst(_FieldConst): |
| 124 | @staticmethod |
| 125 | def _extract_value(other): |
| 126 | if isinstance(other, _BoolFieldConst) or isinstance(other, bool): |
| 127 | return bool(other) |
| 128 | |
| 129 | if isinstance(other, numbers.Integral): |
| 130 | return int(other) |
| 131 | |
| 132 | if isinstance(other, numbers.Real): |
| 133 | return float(other) |
| 134 | |
| 135 | if isinstance(other, numbers.Complex): |
| 136 | return complex(other) |
| 137 | |
| 138 | raise TypeError( |
| 139 | "'{}' object is not a number object".format(other.__class__.__name__) |
| 140 | ) |
| 141 | |
| 142 | def __int__(self): |
| 143 | return int(self._value) |
| 144 | |
| 145 | def __float__(self): |
| 146 | return float(self._value) |
| 147 | |
| 148 | def _repr(self): |
| 149 | return repr(self._value) |
| 150 | |
| 151 | def __lt__(self, other): |
| 152 | if not isinstance(other, numbers.Number): |
| 153 | raise TypeError( |
| 154 | "unorderable types: {}() < {}()".format( |
| 155 | self.__class__.__name__, other.__class__.__name__ |
| 156 | ) |
| 157 | ) |
| 158 | |
| 159 | return self._value < self._extract_value(other) |
| 160 | |
| 161 | def _spec_eq(self, other): |
| 162 | try: |
| 163 | return self._value == self._extract_value(other) |
| 164 | except Exception: |
| 165 | return False |
| 166 | |
| 167 | def __hash__(self): |
| 168 | return hash(self._value) |
| 169 | |
| 170 | def __rmod__(self, other): |
| 171 | return self._extract_value(other) % self._value |
| 172 | |
| 173 | def __mod__(self, other): |
| 174 | return self._value % self._extract_value(other) |
| 175 | |
| 176 | def __rfloordiv__(self, other): |
| 177 | return self._extract_value(other) // self._value |
| 178 | |
| 179 | def __floordiv__(self, other): |
| 180 | return self._value // self._extract_value(other) |
| 181 | |
| 182 | def __round__(self, ndigits=None): |
| 183 | if ndigits is None: |
| 184 | return round(self._value) |
| 185 | else: |
| 186 | return round(self._value, ndigits) |
| 187 | |
| 188 | def __ceil__(self): |
| 189 | return math.ceil(self._value) |
| 190 | |
| 191 | def __floor__(self): |
| 192 | return math.floor(self._value) |
| 193 | |
| 194 | def __trunc__(self): |
| 195 | return int(self._value) |
| 196 | |
| 197 | def __abs__(self): |
| 198 | return abs(self._value) |
| 199 | |
| 200 | def __add__(self, other): |
| 201 | return self._value + self._extract_value(other) |
| 202 | |
| 203 | def __radd__(self, other): |
| 204 | return self.__add__(other) |
| 205 | |
| 206 | def __neg__(self): |
| 207 | return -self._value |
| 208 | |
| 209 | def __pos__(self): |
| 210 | return +self._value |
| 211 | |
| 212 | def __mul__(self, other): |
| 213 | return self._value * self._extract_value(other) |
| 214 | |
| 215 | def __rmul__(self, other): |
| 216 | return self.__mul__(other) |
| 217 | |
| 218 | def __truediv__(self, other): |
| 219 | return self._value / self._extract_value(other) |
| 220 | |
| 221 | def __rtruediv__(self, other): |
| 222 | return self._extract_value(other) / self._value |
| 223 | |
| 224 | def __pow__(self, exponent): |
| 225 | return self._value ** self._extract_value(exponent) |
| 226 | |
| 227 | def __rpow__(self, base): |
| 228 | return self._extract_value(base) ** self._value |
| 229 | |
| 230 | |
| 231 | class _NumericField(_NumericFieldConst, _Field): |
| 232 | def __hash__(self): |
| 233 | # Non const field are not hashable as their value may be modified |
| 234 | # without changing the underlying Python object. |
| 235 | raise TypeError("unhashable type: '{}'".format(self._NAME)) |
| 236 | |
| 237 | |
| 238 | class _IntegralFieldConst(_NumericFieldConst, numbers.Integral): |
| 239 | def __lshift__(self, other): |
| 240 | return self._value << self._extract_value(other) |
| 241 | |
| 242 | def __rlshift__(self, other): |
| 243 | return self._extract_value(other) << self._value |
| 244 | |
| 245 | def __rshift__(self, other): |
| 246 | return self._value >> self._extract_value(other) |
| 247 | |
| 248 | def __rrshift__(self, other): |
| 249 | return self._extract_value(other) >> self._value |
| 250 | |
| 251 | def __and__(self, other): |
| 252 | return self._value & self._extract_value(other) |
| 253 | |
| 254 | def __rand__(self, other): |
| 255 | return self._extract_value(other) & self._value |
| 256 | |
| 257 | def __xor__(self, other): |
| 258 | return self._value ^ self._extract_value(other) |
| 259 | |
| 260 | def __rxor__(self, other): |
| 261 | return self._extract_value(other) ^ self._value |
| 262 | |
| 263 | def __or__(self, other): |
| 264 | return self._value | self._extract_value(other) |
| 265 | |
| 266 | def __ror__(self, other): |
| 267 | return self._extract_value(other) | self._value |
| 268 | |
| 269 | def __invert__(self): |
| 270 | return ~self._value |
| 271 | |
| 272 | |
| 273 | class _IntegralField(_IntegralFieldConst, _NumericField): |
| 274 | pass |
| 275 | |
| 276 | |
| 277 | class _BoolFieldConst(_IntegralFieldConst, _FieldConst): |
| 278 | _NAME = "Const boolean" |
| 279 | |
| 280 | def __bool__(self): |
| 281 | return self._value |
| 282 | |
| 283 | @classmethod |
| 284 | def _value_to_bool(cls, value): |
| 285 | if isinstance(value, _BoolFieldConst): |
| 286 | value = value._value |
| 287 | |
| 288 | if not isinstance(value, bool): |
| 289 | raise TypeError( |
| 290 | "'{}' object is not a 'bool', '_BoolFieldConst', or '_BoolField' object".format( |
| 291 | value.__class__ |
| 292 | ) |
| 293 | ) |
| 294 | |
| 295 | return value |
| 296 | |
| 297 | @property |
| 298 | def _value(self): |
| 299 | return bool(native_bt.field_bool_get_value(self._ptr)) |
| 300 | |
| 301 | |
| 302 | class _BoolField(_BoolFieldConst, _IntegralField, _Field): |
| 303 | _NAME = "Boolean" |
| 304 | |
| 305 | def _set_value(self, value): |
| 306 | value = self._value_to_bool(value) |
| 307 | native_bt.field_bool_set_value(self._ptr, value) |
| 308 | |
| 309 | value = property(fset=_set_value) |
| 310 | |
| 311 | |
| 312 | class _IntegerFieldConst(_IntegralFieldConst, _FieldConst): |
| 313 | pass |
| 314 | |
| 315 | |
| 316 | class _IntegerField(_IntegerFieldConst, _IntegralField, _Field): |
| 317 | def _check_range(self, value): |
| 318 | if not (value >= self._lower_bound and value <= self._upper_bound): |
| 319 | raise ValueError( |
| 320 | "Value {} is outside valid range [{}, {}]".format( |
| 321 | value, self._lower_bound, self._upper_bound |
| 322 | ) |
| 323 | ) |
| 324 | |
| 325 | |
| 326 | class _UnsignedIntegerFieldConst(_IntegerFieldConst, _FieldConst): |
| 327 | _NAME = "Const unsigned integer" |
| 328 | |
| 329 | @classmethod |
| 330 | def _value_to_int(cls, value): |
| 331 | if not isinstance(value, numbers.Integral): |
| 332 | raise TypeError("expecting an integral number object") |
| 333 | |
| 334 | return int(value) |
| 335 | |
| 336 | @property |
| 337 | def _value(self): |
| 338 | return native_bt.field_integer_unsigned_get_value(self._ptr) |
| 339 | |
| 340 | |
| 341 | class _UnsignedIntegerField(_UnsignedIntegerFieldConst, _IntegerField, _Field): |
| 342 | _NAME = "Unsigned integer" |
| 343 | |
| 344 | def _set_value(self, value): |
| 345 | value = self._value_to_int(value) |
| 346 | |
| 347 | self._check_range(value) |
| 348 | |
| 349 | native_bt.field_integer_unsigned_set_value(self._ptr, value) |
| 350 | |
| 351 | value = property(fset=_set_value) |
| 352 | |
| 353 | @property |
| 354 | def _lower_bound(self): |
| 355 | return 0 |
| 356 | |
| 357 | @property |
| 358 | def _upper_bound(self): |
| 359 | return (2**self.cls.field_value_range) - 1 |
| 360 | |
| 361 | |
| 362 | class _SignedIntegerFieldConst(_IntegerFieldConst, _FieldConst): |
| 363 | _NAME = "Const signed integer" |
| 364 | |
| 365 | @classmethod |
| 366 | def _value_to_int(cls, value): |
| 367 | if not isinstance(value, numbers.Integral): |
| 368 | raise TypeError("expecting an integral number object") |
| 369 | |
| 370 | return int(value) |
| 371 | |
| 372 | @property |
| 373 | def _value(self): |
| 374 | return native_bt.field_integer_signed_get_value(self._ptr) |
| 375 | |
| 376 | |
| 377 | class _SignedIntegerField(_SignedIntegerFieldConst, _IntegerField, _Field): |
| 378 | _NAME = "Signed integer" |
| 379 | |
| 380 | def _set_value(self, value): |
| 381 | value = self._value_to_int(value) |
| 382 | |
| 383 | self._check_range(value) |
| 384 | |
| 385 | native_bt.field_integer_signed_set_value(self._ptr, value) |
| 386 | |
| 387 | value = property(fset=_set_value) |
| 388 | |
| 389 | @property |
| 390 | def _lower_bound(self): |
| 391 | return -1 * (2 ** (self.cls.field_value_range - 1)) |
| 392 | |
| 393 | @property |
| 394 | def _upper_bound(self): |
| 395 | return (2 ** (self.cls.field_value_range - 1)) - 1 |
| 396 | |
| 397 | |
| 398 | class _RealFieldConst(_NumericFieldConst, numbers.Real): |
| 399 | _NAME = "Const real" |
| 400 | |
| 401 | @classmethod |
| 402 | def _value_to_float(cls, value): |
| 403 | if not isinstance(value, numbers.Real): |
| 404 | raise TypeError("expecting a real number object") |
| 405 | |
| 406 | return float(value) |
| 407 | |
| 408 | |
| 409 | class _SinglePrecisionRealFieldConst(_RealFieldConst): |
| 410 | _NAME = "Const single-precision real" |
| 411 | |
| 412 | @property |
| 413 | def _value(self): |
| 414 | return native_bt.field_real_single_precision_get_value(self._ptr) |
| 415 | |
| 416 | |
| 417 | class _DoublePrecisionRealFieldConst(_RealFieldConst): |
| 418 | _NAME = "Const double-precision real" |
| 419 | |
| 420 | @property |
| 421 | def _value(self): |
| 422 | return native_bt.field_real_double_precision_get_value(self._ptr) |
| 423 | |
| 424 | |
| 425 | class _RealField(_RealFieldConst, _NumericField): |
| 426 | _NAME = "Real" |
| 427 | |
| 428 | |
| 429 | class _SinglePrecisionRealField(_SinglePrecisionRealFieldConst, _RealField): |
| 430 | _NAME = "Single-precision real" |
| 431 | |
| 432 | def _set_value(self, value): |
| 433 | value = self._value_to_float(value) |
| 434 | native_bt.field_real_single_precision_set_value(self._ptr, value) |
| 435 | |
| 436 | value = property(fset=_set_value) |
| 437 | |
| 438 | |
| 439 | class _DoublePrecisionRealField(_DoublePrecisionRealFieldConst, _RealField): |
| 440 | _NAME = "Double-precision real" |
| 441 | |
| 442 | def _set_value(self, value): |
| 443 | value = self._value_to_float(value) |
| 444 | native_bt.field_real_double_precision_set_value(self._ptr, value) |
| 445 | |
| 446 | value = property(fset=_set_value) |
| 447 | |
| 448 | |
| 449 | class _EnumerationFieldConst(_IntegerFieldConst): |
| 450 | def _repr(self): |
| 451 | return "{} ({})".format(self._value, ", ".join(self.labels)) |
| 452 | |
| 453 | @property |
| 454 | def labels(self): |
| 455 | status, labels = self._get_mapping_labels(self._ptr) |
| 456 | bt2_utils._handle_func_status(status, "cannot get label for enumeration field") |
| 457 | |
| 458 | assert labels is not None |
| 459 | return labels |
| 460 | |
| 461 | |
| 462 | class _EnumerationField(_EnumerationFieldConst, _IntegerField): |
| 463 | pass |
| 464 | |
| 465 | |
| 466 | class _UnsignedEnumerationFieldConst( |
| 467 | _EnumerationFieldConst, _UnsignedIntegerFieldConst |
| 468 | ): |
| 469 | _NAME = "Const unsigned Enumeration" |
| 470 | _get_mapping_labels = staticmethod( |
| 471 | native_bt.field_enumeration_unsigned_get_mapping_labels |
| 472 | ) |
| 473 | |
| 474 | |
| 475 | class _UnsignedEnumerationField( |
| 476 | _UnsignedEnumerationFieldConst, _EnumerationField, _UnsignedIntegerField |
| 477 | ): |
| 478 | _NAME = "Unsigned enumeration" |
| 479 | |
| 480 | |
| 481 | class _SignedEnumerationFieldConst(_EnumerationFieldConst, _SignedIntegerFieldConst): |
| 482 | _NAME = "Const signed Enumeration" |
| 483 | _get_mapping_labels = staticmethod( |
| 484 | native_bt.field_enumeration_signed_get_mapping_labels |
| 485 | ) |
| 486 | |
| 487 | |
| 488 | class _SignedEnumerationField( |
| 489 | _SignedEnumerationFieldConst, _EnumerationField, _SignedIntegerField |
| 490 | ): |
| 491 | _NAME = "Signed enumeration" |
| 492 | |
| 493 | |
| 494 | @functools.total_ordering |
| 495 | class _StringFieldConst(_FieldConst): |
| 496 | _NAME = "Const string" |
| 497 | |
| 498 | @classmethod |
| 499 | def _value_to_str(cls, value): |
| 500 | if isinstance(value, _StringFieldConst): |
| 501 | value = value._value |
| 502 | |
| 503 | if not isinstance(value, str): |
| 504 | raise TypeError("expecting a 'str' object") |
| 505 | |
| 506 | return value |
| 507 | |
| 508 | @property |
| 509 | def _value(self): |
| 510 | return native_bt.field_string_get_value(self._ptr) |
| 511 | |
| 512 | def _spec_eq(self, other): |
| 513 | try: |
| 514 | return self._value == self._value_to_str(other) |
| 515 | except Exception: |
| 516 | return False |
| 517 | |
| 518 | def __lt__(self, other): |
| 519 | return self._value < self._value_to_str(other) |
| 520 | |
| 521 | def __bool__(self): |
| 522 | return bool(self._value) |
| 523 | |
| 524 | def __hash__(self): |
| 525 | return hash(self._value) |
| 526 | |
| 527 | def _repr(self): |
| 528 | return repr(self._value) |
| 529 | |
| 530 | def __str__(self): |
| 531 | return str(self._value) |
| 532 | |
| 533 | def __getitem__(self, index): |
| 534 | return self._value[index] |
| 535 | |
| 536 | def __len__(self): |
| 537 | return native_bt.field_string_get_length(self._ptr) |
| 538 | |
| 539 | |
| 540 | class _StringField(_StringFieldConst, _Field): |
| 541 | _NAME = "String" |
| 542 | |
| 543 | def _set_value(self, value): |
| 544 | value = self._value_to_str(value) |
| 545 | native_bt.field_string_set_value(self._ptr, value) |
| 546 | |
| 547 | value = property(fset=_set_value) |
| 548 | |
| 549 | def __iadd__(self, value): |
| 550 | value = self._value_to_str(value) |
| 551 | status = native_bt.field_string_append(self._ptr, value) |
| 552 | bt2_utils._handle_func_status( |
| 553 | status, "cannot append to string field object's value" |
| 554 | ) |
| 555 | return self |
| 556 | |
| 557 | def __hash__(self): |
| 558 | # Non const field are not hashable as their value may be modified |
| 559 | # without changing the underlying Python object. |
| 560 | raise TypeError("unhashable type: '{}'".format(self._NAME)) |
| 561 | |
| 562 | |
| 563 | class _ContainerFieldConst(_FieldConst): |
| 564 | def __bool__(self): |
| 565 | return len(self) != 0 |
| 566 | |
| 567 | def _count(self): |
| 568 | return len(self.cls) |
| 569 | |
| 570 | def __len__(self): |
| 571 | count = self._count() |
| 572 | assert count >= 0 |
| 573 | return count |
| 574 | |
| 575 | def __delitem__(self, index): |
| 576 | raise NotImplementedError |
| 577 | |
| 578 | def __setitem__(self, index, value): |
| 579 | raise TypeError( |
| 580 | "'{}' object does not support item assignment".format(self.__class__) |
| 581 | ) |
| 582 | |
| 583 | |
| 584 | class _ContainerField(_ContainerFieldConst, _Field): |
| 585 | pass |
| 586 | |
| 587 | |
| 588 | class _StructureFieldConst(_ContainerFieldConst, collections.abc.Mapping): |
| 589 | _NAME = "Const structure" |
| 590 | _borrow_member_field_ptr_by_index = staticmethod( |
| 591 | native_bt.field_structure_borrow_member_field_by_index_const |
| 592 | ) |
| 593 | _borrow_member_field_ptr_by_name = staticmethod( |
| 594 | native_bt.field_structure_borrow_member_field_by_name_const |
| 595 | ) |
| 596 | |
| 597 | def _count(self): |
| 598 | return len(self.cls) |
| 599 | |
| 600 | def __iter__(self): |
| 601 | # same name iterator |
| 602 | return iter(self.cls) |
| 603 | |
| 604 | def _spec_eq(self, other): |
| 605 | if not isinstance(other, collections.abc.Mapping): |
| 606 | return False |
| 607 | |
| 608 | if len(self) != len(other): |
| 609 | # early mismatch |
| 610 | return False |
| 611 | |
| 612 | for self_key in self: |
| 613 | if self_key not in other: |
| 614 | return False |
| 615 | |
| 616 | if self[self_key] != other[self_key]: |
| 617 | return False |
| 618 | |
| 619 | return True |
| 620 | |
| 621 | def _repr(self): |
| 622 | items = ["{}: {}".format(repr(k), repr(v)) for k, v in self.items()] |
| 623 | return "{{{}}}".format(", ".join(items)) |
| 624 | |
| 625 | def __getitem__(self, key): |
| 626 | bt2_utils._check_str(key) |
| 627 | field_ptr = self._borrow_member_field_ptr_by_name(self._ptr, key) |
| 628 | |
| 629 | if field_ptr is None: |
| 630 | raise KeyError(key) |
| 631 | |
| 632 | return self._create_field_from_ptr( |
| 633 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
| 634 | ) |
| 635 | |
| 636 | def member_at_index(self, index): |
| 637 | bt2_utils._check_uint64(index) |
| 638 | |
| 639 | if index >= len(self): |
| 640 | raise IndexError |
| 641 | field_ptr = self._borrow_member_field_ptr_by_index(self._ptr, index) |
| 642 | assert field_ptr is not None |
| 643 | return self._create_field_from_ptr( |
| 644 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
| 645 | ) |
| 646 | |
| 647 | |
| 648 | class _StructureField( |
| 649 | _StructureFieldConst, _ContainerField, collections.abc.MutableMapping |
| 650 | ): |
| 651 | _NAME = "Structure" |
| 652 | _borrow_member_field_ptr_by_index = staticmethod( |
| 653 | native_bt.field_structure_borrow_member_field_by_index |
| 654 | ) |
| 655 | _borrow_member_field_ptr_by_name = staticmethod( |
| 656 | native_bt.field_structure_borrow_member_field_by_name |
| 657 | ) |
| 658 | |
| 659 | def __setitem__(self, key, value): |
| 660 | # raises if key is somehow invalid |
| 661 | field = self[key] |
| 662 | |
| 663 | # the field's property does the appropriate conversion or raises |
| 664 | # the appropriate exception |
| 665 | field.value = value |
| 666 | |
| 667 | def _set_value(self, values): |
| 668 | try: |
| 669 | for key, value in values.items(): |
| 670 | self[key].value = value |
| 671 | except Exception: |
| 672 | raise |
| 673 | |
| 674 | value = property(fset=_set_value) |
| 675 | |
| 676 | |
| 677 | class _OptionFieldConst(_FieldConst): |
| 678 | _NAME = "Const option" |
| 679 | _borrow_field_ptr = staticmethod(native_bt.field_option_borrow_field_const) |
| 680 | |
| 681 | @property |
| 682 | def field(self): |
| 683 | field_ptr = self._borrow_field_ptr(self._ptr) |
| 684 | |
| 685 | if field_ptr is None: |
| 686 | return |
| 687 | |
| 688 | return self._create_field_from_ptr( |
| 689 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
| 690 | ) |
| 691 | |
| 692 | @property |
| 693 | def has_field(self): |
| 694 | return self.field is not None |
| 695 | |
| 696 | def _spec_eq(self, other): |
| 697 | return _get_leaf_field(self) == other |
| 698 | |
| 699 | def __bool__(self): |
| 700 | return self.has_field |
| 701 | |
| 702 | def __str__(self): |
| 703 | return str(self.field) |
| 704 | |
| 705 | def _repr(self): |
| 706 | return repr(self.field) |
| 707 | |
| 708 | |
| 709 | class _OptionField(_OptionFieldConst, _Field): |
| 710 | _NAME = "Option" |
| 711 | _borrow_field_ptr = staticmethod(native_bt.field_option_borrow_field) |
| 712 | |
| 713 | def _has_field(self, value): |
| 714 | bt2_utils._check_bool(value) |
| 715 | native_bt.field_option_set_has_field(self._ptr, value) |
| 716 | |
| 717 | has_field = property(fget=_OptionFieldConst.has_field.fget, fset=_has_field) |
| 718 | |
| 719 | def _set_value(self, value): |
| 720 | self.has_field = True |
| 721 | field = self.field |
| 722 | assert field is not None |
| 723 | field.value = value |
| 724 | |
| 725 | value = property(fset=_set_value) |
| 726 | |
| 727 | |
| 728 | class _VariantFieldConst(_ContainerFieldConst, _FieldConst): |
| 729 | _NAME = "Const variant" |
| 730 | _borrow_selected_option_field_ptr = staticmethod( |
| 731 | native_bt.field_variant_borrow_selected_option_field_const |
| 732 | ) |
| 733 | |
| 734 | def _count(self): |
| 735 | return len(self.cls) |
| 736 | |
| 737 | @property |
| 738 | def selected_option_index(self): |
| 739 | return native_bt.field_variant_get_selected_option_index(self._ptr) |
| 740 | |
| 741 | @property |
| 742 | def selected_option(self): |
| 743 | # TODO: Is there a way to check if the variant field has a selected_option, |
| 744 | # so we can raise an exception instead of hitting a pre-condition check? |
| 745 | # If there is something, that check should be added to selected_option_index too. |
| 746 | field_ptr = self._borrow_selected_option_field_ptr(self._ptr) |
| 747 | |
| 748 | return self._create_field_from_ptr( |
| 749 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
| 750 | ) |
| 751 | |
| 752 | def _spec_eq(self, other): |
| 753 | return _get_leaf_field(self) == other |
| 754 | |
| 755 | def __bool__(self): |
| 756 | raise NotImplementedError |
| 757 | |
| 758 | def __str__(self): |
| 759 | return str(self.selected_option) |
| 760 | |
| 761 | def _repr(self): |
| 762 | return repr(self.selected_option) |
| 763 | |
| 764 | |
| 765 | class _VariantField(_VariantFieldConst, _ContainerField, _Field): |
| 766 | _NAME = "Variant" |
| 767 | _borrow_selected_option_field_ptr = staticmethod( |
| 768 | native_bt.field_variant_borrow_selected_option_field |
| 769 | ) |
| 770 | |
| 771 | def _selected_option_index(self, index): |
| 772 | if index < 0 or index >= len(self): |
| 773 | raise IndexError("{} field object index is out of range".format(self._NAME)) |
| 774 | |
| 775 | native_bt.field_variant_select_option_by_index(self._ptr, index) |
| 776 | |
| 777 | selected_option_index = property( |
| 778 | fget=_VariantFieldConst.selected_option_index.fget, fset=_selected_option_index |
| 779 | ) |
| 780 | |
| 781 | def _set_value(self, value): |
| 782 | self.selected_option.value = value |
| 783 | |
| 784 | value = property(fset=_set_value) |
| 785 | |
| 786 | |
| 787 | class _ArrayFieldConst(_ContainerFieldConst, _FieldConst, collections.abc.Sequence): |
| 788 | _borrow_element_field_ptr_by_index = staticmethod( |
| 789 | native_bt.field_array_borrow_element_field_by_index_const |
| 790 | ) |
| 791 | |
| 792 | def _get_length(self): |
| 793 | return native_bt.field_array_get_length(self._ptr) |
| 794 | |
| 795 | length = property(fget=_get_length) |
| 796 | |
| 797 | def __getitem__(self, index): |
| 798 | if not isinstance(index, numbers.Integral): |
| 799 | raise TypeError( |
| 800 | "'{}' is not an integral number object: invalid index".format( |
| 801 | index.__class__.__name__ |
| 802 | ) |
| 803 | ) |
| 804 | |
| 805 | index = int(index) |
| 806 | |
| 807 | if index < 0 or index >= len(self): |
| 808 | raise IndexError("{} field object index is out of range".format(self._NAME)) |
| 809 | |
| 810 | field_ptr = self._borrow_element_field_ptr_by_index(self._ptr, index) |
| 811 | assert field_ptr |
| 812 | return self._create_field_from_ptr( |
| 813 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
| 814 | ) |
| 815 | |
| 816 | def insert(self, index, value): |
| 817 | raise NotImplementedError |
| 818 | |
| 819 | def _spec_eq(self, other): |
| 820 | if not isinstance(other, collections.abc.Sequence): |
| 821 | return False |
| 822 | |
| 823 | if len(self) != len(other): |
| 824 | # early mismatch |
| 825 | return False |
| 826 | |
| 827 | for self_elem, other_elem in zip(self, other): |
| 828 | if self_elem != other_elem: |
| 829 | return False |
| 830 | |
| 831 | return True |
| 832 | |
| 833 | def _repr(self): |
| 834 | return "[{}]".format(", ".join([repr(v) for v in self])) |
| 835 | |
| 836 | |
| 837 | class _ArrayField( |
| 838 | _ArrayFieldConst, _ContainerField, _Field, collections.abc.MutableSequence |
| 839 | ): |
| 840 | _borrow_element_field_ptr_by_index = staticmethod( |
| 841 | native_bt.field_array_borrow_element_field_by_index |
| 842 | ) |
| 843 | |
| 844 | def __setitem__(self, index, value): |
| 845 | # raises if index is somehow invalid |
| 846 | field = self[index] |
| 847 | |
| 848 | if not isinstance(field, (_NumericField, _StringField)): |
| 849 | raise TypeError("can only set the value of a number or string field") |
| 850 | |
| 851 | # the field's property does the appropriate conversion or raises |
| 852 | # the appropriate exception |
| 853 | field.value = value |
| 854 | |
| 855 | |
| 856 | class _StaticArrayFieldConst(_ArrayFieldConst, _FieldConst): |
| 857 | _NAME = "Const static array" |
| 858 | |
| 859 | def _count(self): |
| 860 | return native_bt.field_array_get_length(self._ptr) |
| 861 | |
| 862 | |
| 863 | class _StaticArrayField(_StaticArrayFieldConst, _ArrayField, _Field): |
| 864 | _NAME = "Static array" |
| 865 | |
| 866 | def _set_value(self, values): |
| 867 | if len(self) != len(values): |
| 868 | raise ValueError( |
| 869 | "expected length of value ({}) and array field ({}) to match".format( |
| 870 | len(values), len(self) |
| 871 | ) |
| 872 | ) |
| 873 | |
| 874 | for index, value in enumerate(values): |
| 875 | if value is not None: |
| 876 | self[index].value = value |
| 877 | |
| 878 | value = property(fset=_set_value) |
| 879 | |
| 880 | |
| 881 | class _DynamicArrayFieldConst(_ArrayFieldConst, _FieldConst): |
| 882 | _NAME = "Const dynamic array" |
| 883 | |
| 884 | def _count(self): |
| 885 | return self.length |
| 886 | |
| 887 | |
| 888 | class _DynamicArrayField(_DynamicArrayFieldConst, _ArrayField, _Field): |
| 889 | _NAME = "Dynamic array" |
| 890 | |
| 891 | def _set_length(self, length): |
| 892 | bt2_utils._check_uint64(length) |
| 893 | status = native_bt.field_array_dynamic_set_length(self._ptr, length) |
| 894 | bt2_utils._handle_func_status(status, "cannot set dynamic array length") |
| 895 | |
| 896 | length = property(fget=_ArrayField._get_length, fset=_set_length) |
| 897 | |
| 898 | def _set_value(self, values): |
| 899 | if len(values) != self.length: |
| 900 | self.length = len(values) |
| 901 | |
| 902 | for index, value in enumerate(values): |
| 903 | if value is not None: |
| 904 | self[index].value = value |
| 905 | |
| 906 | value = property(fset=_set_value) |
| 907 | |
| 908 | |
| 909 | _TYPE_ID_TO_CONST_OBJ = { |
| 910 | native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldConst, |
| 911 | native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldConst, |
| 912 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldConst, |
| 913 | native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldConst, |
| 914 | native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealFieldConst, |
| 915 | native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealFieldConst, |
| 916 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationFieldConst, |
| 917 | native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationFieldConst, |
| 918 | native_bt.FIELD_CLASS_TYPE_STRING: _StringFieldConst, |
| 919 | native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureFieldConst, |
| 920 | native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayFieldConst, |
| 921 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayFieldConst, |
| 922 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayFieldConst, |
| 923 | native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionFieldConst, |
| 924 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionFieldConst, |
| 925 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionFieldConst, |
| 926 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionFieldConst, |
| 927 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantFieldConst, |
| 928 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldConst, |
| 929 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantFieldConst, |
| 930 | } |
| 931 | |
| 932 | _TYPE_ID_TO_OBJ = { |
| 933 | native_bt.FIELD_CLASS_TYPE_BOOL: _BoolField, |
| 934 | native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayField, |
| 935 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerField, |
| 936 | native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerField, |
| 937 | native_bt.FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: _SinglePrecisionRealField, |
| 938 | native_bt.FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: _DoublePrecisionRealField, |
| 939 | native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationField, |
| 940 | native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationField, |
| 941 | native_bt.FIELD_CLASS_TYPE_STRING: _StringField, |
| 942 | native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureField, |
| 943 | native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayField, |
| 944 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: _DynamicArrayField, |
| 945 | native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: _DynamicArrayField, |
| 946 | native_bt.FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: _OptionField, |
| 947 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: _OptionField, |
| 948 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _OptionField, |
| 949 | native_bt.FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _OptionField, |
| 950 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: _VariantField, |
| 951 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: _VariantField, |
| 952 | native_bt.FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: _VariantField, |
| 953 | } |