bt2: Adapt test_clock_class.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / clock_class.py
index 8eb25385d53560ec71be47cb602943ada5c17aad..59a93cd7884a8bf596352ed15f49d20bb64c0d21 100644 (file)
 # THE SOFTWARE.
 
 from bt2 import native_bt, object, utils
-import uuid as uuidp
-import numbers
 import bt2
-import bt2.clock_snapshot as clock_snapshot
+import uuid as uuidp
 
 
 class ClockClassOffset:
@@ -42,9 +40,6 @@ class ClockClassOffset:
     def cycles(self):
         return self._cycles
 
-    def __hash__(self):
-        return hash((self.seconds, self.cycles))
-
     def __eq__(self, other):
         if not isinstance(other, self.__class__):
             # not comparing apples to apples
@@ -53,159 +48,73 @@ class ClockClassOffset:
         return (self.seconds, self.cycles) == (other.seconds, other.cycles)
 
 
-class ClockClass(object._SharedObject):
+class _ClockClass(object._SharedObject):
     _get_ref = staticmethod(native_bt.clock_class_get_ref)
     _put_ref = staticmethod(native_bt.clock_class_put_ref)
 
-    def __init__(self, name, frequency, description=None, precision=None,
-                 offset=None, is_absolute=None, uuid=None):
-        utils._check_str(name)
-        utils._check_uint64(frequency)
-        ptr = native_bt.clock_class_create(name, frequency)
-
-        if ptr is None:
-            raise bt2.CreationError('cannot create clock class object')
-
-        super().__init__(ptr)
-
-        if description is not None:
-            self.description = description
-
-        if frequency is not None:
-            self.frequency = frequency
-
-        if precision is not None:
-            self.precision = precision
-
-        if offset is not None:
-            self.offset = offset
-
-        if is_absolute is not None:
-            self.is_absolute = is_absolute
-
-        if uuid is not None:
-            self.uuid = uuid
-
-    def __eq__(self, other):
-        if type(self) is not type(other):
-            # not comparing apples to apples
-            return False
-
-        self_props = (
-            self.name,
-            self.description,
-            self.frequency,
-            self.precision,
-            self.offset,
-            self.is_absolute,
-            self.uuid
-        )
-        other_props = (
-            other.name,
-            other.description,
-            other.frequency,
-            other.precision,
-            other.offset,
-            other.is_absolute,
-            other.uuid
-        )
-        return self_props == other_props
-
-    def __copy__(self):
-        return ClockClass(name=self.name, description=self.description,
-                          frequency=self.frequency, precision=self.precision,
-                          offset=self.offset, is_absolute=self.is_absolute,
-                          uuid=self.uuid)
-
-    def __deepcopy__(self, memo):
-        cpy = self.__copy__()
-        memo[id(self)] = cpy
-        return cpy
-
-    def __hash__(self):
-        return hash((
-            self.name,
-            self.description,
-            self.frequency,
-            self.precision,
-            self.offset.seconds,
-            self.offset.cycles,
-            self.is_absolute,
-            self.uuid))
-
     @property
     def name(self):
-        name = native_bt.clock_class_get_name(self._ptr)
-        assert(name is not None)
-        return name
+        return native_bt.clock_class_get_name(self._ptr)
 
-    @name.setter
-    def name(self, name):
+    def _name(self, name):
         utils._check_str(name)
         ret = native_bt.clock_class_set_name(self._ptr, name)
         utils._handle_ret(ret, "cannot set clock class object's name")
 
+    _name = property(fset=_name)
+
     @property
     def description(self):
         return native_bt.clock_class_get_description(self._ptr)
 
-    @description.setter
-    def description(self, description):
+    def _description(self, description):
         utils._check_str(description)
         ret = native_bt.clock_class_set_description(self._ptr, description)
         utils._handle_ret(ret, "cannot set clock class object's description")
 
+    _description = property(fset=_description)
+
     @property
     def frequency(self):
-        frequency = native_bt.clock_class_get_frequency(self._ptr)
-        assert(frequency >= 1)
-        return frequency
+        return native_bt.clock_class_get_frequency(self._ptr)
 
-    @frequency.setter
-    def frequency(self, frequency):
+    def _frequency(self, frequency):
         utils._check_uint64(frequency)
-        ret = native_bt.clock_class_set_frequency(self._ptr, frequency)
-        utils._handle_ret(ret, "cannot set clock class object's frequency")
+        native_bt.clock_class_set_frequency(self._ptr, frequency)
+
+    _frequency = property(fset=_frequency)
 
     @property
     def precision(self):
         precision = native_bt.clock_class_get_precision(self._ptr)
-        assert(precision >= 0)
         return precision
 
-    @precision.setter
-    def precision(self, precision):
+    def _precision(self, precision):
         utils._check_uint64(precision)
-        ret = native_bt.clock_class_set_precision(self._ptr, precision)
-        utils._handle_ret(ret, "cannot set clock class object's precision")
+        native_bt.clock_class_set_precision(self._ptr, precision)
+
+    _precision = property(fset=_precision)
 
     @property
     def offset(self):
-        ret, offset_s = native_bt.clock_class_get_offset_s(self._ptr)
-        assert(ret == 0)
-        ret, offset_cycles = native_bt.clock_class_get_offset_cycles(self._ptr)
-        assert(ret == 0)
+        offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr)
         return ClockClassOffset(offset_s, offset_cycles)
 
-    @offset.setter
-    def offset(self, offset):
+    def _offset(self, offset):
         utils._check_type(offset, ClockClassOffset)
-        ret = native_bt.clock_class_set_offset_s(self._ptr, offset.seconds)
-        utils._handle_ret(ret, "cannot set clock class object's offset (seconds)")
-        ret = native_bt.clock_class_set_offset_cycles(self._ptr, offset.cycles)
-        utils._handle_ret(ret, "cannot set clock class object's offset (cycles)")
+        native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles)
+
+    _offset = property(fset=_offset)
 
     @property
-    def is_absolute(self):
-        is_absolute = native_bt.clock_class_is_absolute(self._ptr)
-        assert(is_absolute >= 0)
-        return is_absolute > 0
+    def origin_is_unix_epoch(self):
+        return native_bt.clock_class_origin_is_unix_epoch(self._ptr)
 
-    @is_absolute.setter
-    def is_absolute(self, is_absolute):
-        utils._check_bool(is_absolute)
-        ret = native_bt.clock_class_set_is_absolute(self._ptr, int(is_absolute))
-        utils._handle_ret(ret, "cannot set clock class object's absoluteness")
+    def _origin_is_unix_epoch(self, origin_is_unix_epoch):
+        utils._check_bool(origin_is_unix_epoch)
+        native_bt.clock_class_set_origin_is_unix_epoch(self._ptr, int(origin_is_unix_epoch))
+
+    _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch)
 
     @property
     def uuid(self):
@@ -216,11 +125,20 @@ class ClockClass(object._SharedObject):
 
         return uuidp.UUID(bytes=uuid_bytes)
 
-    @uuid.setter
-    def uuid(self, uuid):
+    def _uuid(self, uuid):
         utils._check_type(uuid, uuidp.UUID)
-        ret = native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
-        utils._handle_ret(ret, "cannot set clock class object's UUID")
+        native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
+
+    _uuid = property(fset=_uuid)
+
+    def cycles_to_ns_from_origin(self, cycles):
+        utils._check_uint64(cycles)
+        ret, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles)
+
+        error_msg = "cannot convert clock value to nanoseconds from origin for given clock class"
+
+        if ret == native_bt.CLOCK_CLASS_STATUS_OVERFLOW:
+            raise OverflowError(error_msg)
 
-    def __call__(self, cycles):
-        return clock_snapshot._ClockSnapshot(self._ptr, cycles)
+        utils._handle_ret(ret, error_msg)
+        return ns
This page took 0.028241 seconds and 4 git commands to generate.