# THE SOFTWARE.
from bt2 import native_bt, object, utils
-import bt2.field_types
+import bt2.field_class
import collections.abc
import functools
import numbers
def _create_from_ptr(ptr):
- # recreate the field type wrapper of this field's type (the identity
+ # recreate the field class wrapper of this field's type (the identity
# could be different, but the underlying address should be the
# same)
- field_type_ptr = native_bt.field_get_type(ptr)
- utils._handle_ptr(field_type_ptr, "cannot get field object's type")
- field_type = bt2.field_types._create_from_ptr(field_type_ptr)
- typeid = native_bt.field_type_get_type_id(field_type._ptr)
+ field_class_ptr = native_bt.field_get_type(ptr)
+ utils._handle_ptr(field_class_ptr, "cannot get field object's type")
+ field_class = bt2.field_class._create_from_ptr(field_class_ptr)
+ typeid = native_bt.field_class_get_type_id(field_class._ptr)
field = _TYPE_ID_TO_OBJ[typeid]._create_from_ptr(ptr)
- field._field_type = field_type
+ field._field_class = field_class
return field
return cpy
def __eq__(self, other):
- # special case: two unset fields with the same field type are equal
+ # special case: two unset fields with the same field class are equal
if isinstance(other, _Field):
if not self.is_set or not other.is_set:
- if not self.is_set and not other.is_set and self.field_type == other.field_type:
+ if not self.is_set and not other.is_set and self.field_class == other.field_class:
return True
return False
return self._spec_eq(other)
@property
- def field_type(self):
- return self._field_type
+ def field_class(self):
+ return self._field_class
@property
def is_set(self):
ret = native_bt.field_reset(self._ptr)
utils._handle_ret(ret, "cannot reset field object's value")
+ def _repr(self):
+ raise NotImplementedError
+
+ def __repr__(self):
+ return self._repr() if self.is_set else 'Unset'
+
@functools.total_ordering
class _NumericField(_Field):
def __float__(self):
return float(self._value)
- def __str__(self):
- return str(self._value)
+ def _repr(self):
+ return repr(self._value)
def __lt__(self, other):
if not isinstance(other, numbers.Number):
value = int(value)
- if self.field_type.is_signed:
+ if self.field_class.is_signed:
utils._check_int64(value)
else:
utils._check_uint64(value)
@property
def _value(self):
- if self.field_type.is_signed:
+ if self.field_class.is_signed:
ret, value = native_bt.field_signed_integer_get_value(self._ptr)
else:
ret, value = native_bt.field_unsigned_integer_get_value(self._ptr)
def _set_value(self, value):
value = self._value_to_int(value)
- if self.field_type.is_signed:
+ if self.field_class.is_signed:
ret = native_bt.field_signed_integer_set_value(self._ptr, value)
else:
ret = native_bt.field_unsigned_integer_set_value(self._ptr, value)
value = property(fset=_set_value)
+
class _FloatingPointNumberField(_RealField):
_NAME = 'Floating point number'
value = property(fset=_set_value)
+
class _EnumerationField(_IntegerField):
_NAME = 'Enumeration'
def _set_value(self, value):
self.integer_field.value = value
+ def _repr(self):
+ labels = [repr(v.name) for v in self.mappings]
+ return '{} ({})'.format(self._value, ', '.join(labels))
+
value = property(fset=_set_value)
@property
def mappings(self):
iter_ptr = native_bt.field_enumeration_get_mappings(self._ptr)
assert(iter_ptr)
- return bt2.field_types._EnumerationFieldTypeMappingIterator(iter_ptr,
- self.field_type.is_signed)
+ return bt2.field_class._EnumerationFieldClassMappingIterator(iter_ptr,
+ self.field_class.is_signed)
@functools.total_ordering
def __bool__(self):
return bool(self._value)
+ def _repr(self):
+ return repr(self._value)
+
def __str__(self):
- return self._value
+ return self._value if self.is_set else repr(self)
def __getitem__(self, index):
return self._value[index]
_NAME = 'Structure'
def _count(self):
- return len(self.field_type)
+ return len(self.field_class)
def __getitem__(self, key):
utils._check_str(key)
def __iter__(self):
# same name iterator
- return iter(self.field_type)
+ return iter(self.field_class)
def _spec_eq(self, other):
try:
value = property(fset=_set_value)
+ def _repr(self):
+ items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
+ return '{{{}}}'.format(', '.join(items))
+
+
class _VariantField(_Field):
_NAME = 'Variant'
def __bool__(self):
return bool(self.selected_field)
+ def __str__(self):
+ return str(self.selected_field) if self.is_set else repr(self)
+
+ def _repr(self):
+ return repr(self.selected_field)
+
@property
def _value(self):
if self.selected_field is not None:
value = property(fset=_set_value)
+
class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence):
def __getitem__(self, index):
if not isinstance(index, numbers.Integral):
def _value(self):
return [field._value for field in self]
+ def _repr(self):
+ return '[{}]'.format(', '.join([repr(v) for v in self]))
+
class _ArrayField(_ArraySequenceField):
_NAME = 'Array'
def _count(self):
- return self.field_type.length
+ return self.field_class.length
def _get_field_ptr_at_index(self, index):
return native_bt.field_array_get_field(self._ptr, index)
if len(values) != self.length_field:
if self.length_field is not None:
- length_ft = self.length_field.field_type
+ length_fc = self.length_field.field_class
else:
- length_ft = bt2.IntegerFieldType(size=64, is_signed=False)
- self.length_field = length_ft(len(values))
+ length_fc = bt2.IntegerFieldClass(size=64, is_signed=False)
+ self.length_field = length_fc(len(values))
try:
for index, value in enumerate(values):
value = property(fset=_set_value)
+
_TYPE_ID_TO_OBJ = {
- native_bt.FIELD_TYPE_ID_INTEGER: _IntegerField,
- native_bt.FIELD_TYPE_ID_FLOAT: _FloatingPointNumberField,
- native_bt.FIELD_TYPE_ID_ENUM: _EnumerationField,
- native_bt.FIELD_TYPE_ID_STRING: _StringField,
- native_bt.FIELD_TYPE_ID_STRUCT: _StructureField,
- native_bt.FIELD_TYPE_ID_ARRAY: _ArrayField,
- native_bt.FIELD_TYPE_ID_SEQUENCE: _SequenceField,
- native_bt.FIELD_TYPE_ID_VARIANT: _VariantField,
}