bt2: Mass clock_value -> clock_snapshot rename
[babeltrace.git] / bindings / python / bt2 / bt2 / fields.py
index 0bfbc58f8aaa296bac8f44ac78041a98961f92fa..77c5803ddd4a6cdcc6e319403f0a5f7a32af5bb0 100644 (file)
@@ -21,7 +21,7 @@
 # THE SOFTWARE.
 
 from bt2 import native_bt, object, utils
-import bt2.field_types
+import bt2.field_class
 import collections.abc
 import functools
 import numbers
@@ -38,15 +38,15 @@ def _get_leaf_field(obj):
 
 
 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
 
 
@@ -62,10 +62,10 @@ class _Field(object._Object, metaclass=abc.ABCMeta):
         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
 
@@ -73,8 +73,8 @@ class _Field(object._Object, metaclass=abc.ABCMeta):
         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):
@@ -85,6 +85,12 @@ class _Field(object._Object, metaclass=abc.ABCMeta):
         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):
@@ -110,7 +116,7 @@ class _NumericField(_Field):
     def __float__(self):
         return float(self._value)
 
-    def __repr__(self):
+    def _repr(self):
         return repr(self._value)
 
     def __lt__(self, other):
@@ -290,7 +296,7 @@ class _IntegerField(_IntegralField):
 
         value = int(value)
 
-        if self.field_type.is_signed:
+        if self.field_class.is_signed:
             utils._check_int64(value)
         else:
             utils._check_uint64(value)
@@ -299,7 +305,7 @@ class _IntegerField(_IntegralField):
 
     @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)
@@ -315,7 +321,7 @@ class _IntegerField(_IntegralField):
     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)
@@ -366,7 +372,7 @@ class _EnumerationField(_IntegerField):
     def _set_value(self, value):
         self.integer_field.value = value
 
-    def __repr__(self):
+    def _repr(self):
         labels = [repr(v.name) for v in self.mappings]
         return '{} ({})'.format(self._value, ', '.join(labels))
 
@@ -380,8 +386,8 @@ class _EnumerationField(_IntegerField):
     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
@@ -426,11 +432,11 @@ class _StringField(_Field, collections.abc.Sequence):
     def __bool__(self):
         return bool(self._value)
 
-    def __repr__(self):
+    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]
@@ -462,7 +468,7 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
     _NAME = 'Structure'
 
     def _count(self):
-        return len(self.field_type)
+        return len(self.field_class)
 
     def __getitem__(self, key):
         utils._check_str(key)
@@ -493,7 +499,7 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
 
     def __iter__(self):
         # same name iterator
-        return iter(self.field_type)
+        return iter(self.field_class)
 
     def _spec_eq(self, other):
         try:
@@ -529,7 +535,7 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping):
 
     value = property(fset=_set_value)
 
-    def __repr__(self):
+    def _repr(self):
         items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
         return '{{{}}}'.format(', '.join(items))
 
@@ -569,8 +575,11 @@ class _VariantField(_Field):
     def __bool__(self):
         return bool(self.selected_field)
 
-    def __repr__(self):
-        return repr(self._value)
+    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):
@@ -632,7 +641,7 @@ class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence):
     def _value(self):
         return [field._value for field in self]
 
-    def __repr__(self):
+    def _repr(self):
         return '[{}]'.format(', '.join([repr(v) for v in self]))
 
 
@@ -640,7 +649,7 @@ 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)
@@ -693,10 +702,10 @@ class _SequenceField(_ArraySequenceField):
 
         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):
@@ -716,12 +725,4 @@ class _SequenceField(_ArraySequenceField):
 
 
 _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,
 }
This page took 0.026477 seconds and 4 git commands to generate.