Python bindings: add has_intersection property to TraceCollection
[babeltrace.git] / bindings / python / reader.py
index 6758043599276d3c535708cd08054f034e22895f..bb3d92811b71f3c4e631deeb37df09536f275ded 100644 (file)
@@ -45,12 +45,13 @@ class TraceCollection:
     trace from a trace collection.
     """
 
-    def __init__(self):
+    def __init__(self, intersect_mode=False):
         """
         Creates an empty trace collection.
         """
 
         self._tc = nbt._bt_context_create()
+        self._intersect_mode = intersect_mode
 
     def __del__(self):
         nbt._bt_context_put(self._tc)
@@ -132,6 +133,14 @@ class TraceCollection:
         except AttributeError:
             raise TypeError("in remove_trace, argument 2 must be a TraceHandle instance")
 
+    @property
+    def intersect_mode(self):
+        return self._intersect_mode
+
+    @property
+    def has_intersection(self):
+        return nbt._bt_python_has_intersection(self._tc)
+
     @property
     def events(self):
         """
@@ -145,14 +154,19 @@ class TraceCollection:
         they need *from* an event before accessing the next one.
         """
 
-        begin_pos_ptr = nbt._bt_iter_pos()
-        end_pos_ptr = nbt._bt_iter_pos()
-        begin_pos_ptr.type = nbt.SEEK_BEGIN
-        end_pos_ptr.type = nbt.SEEK_LAST
+        begin_pos_ptr = nbt._bt_python_create_iter_pos()
+        end_pos_ptr = nbt._bt_python_create_iter_pos()
+
+        if not self.intersect_mode:
+            begin_pos_ptr.type = nbt.SEEK_BEGIN
+            end_pos_ptr.type = nbt.SEEK_LAST
 
         for event in self._events(begin_pos_ptr, end_pos_ptr):
             yield event
 
+        nbt._bt_iter_free_pos(begin_pos_ptr)
+        nbt._bt_iter_free_pos(end_pos_ptr)
+
     def events_timestamps(self, timestamp_begin, timestamp_end):
         """
         Generates the ordered :class:`Event` objects of all the opened
@@ -165,8 +179,8 @@ class TraceCollection:
         See :attr:`events` for notes and limitations.
         """
 
-        begin_pos_ptr = nbt._bt_iter_pos()
-        end_pos_ptr = nbt._bt_iter_pos()
+        begin_pos_ptr = nbt._bt_python_create_iter_pos()
+        end_pos_ptr = nbt._bt_python_create_iter_pos()
         begin_pos_ptr.type = end_pos_ptr.type = nbt.SEEK_TIME
         begin_pos_ptr.u.seek_time = timestamp_begin
         end_pos_ptr.u.seek_time = timestamp_end
@@ -174,6 +188,9 @@ class TraceCollection:
         for event in self._events(begin_pos_ptr, end_pos_ptr):
             yield event
 
+        nbt._bt_iter_free_pos(begin_pos_ptr)
+        nbt._bt_iter_free_pos(end_pos_ptr)
+
     @property
     def timestamp_begin(self):
         """
@@ -213,7 +230,18 @@ class TraceCollection:
         return ev.timestamp
 
     def _events(self, begin_pos_ptr, end_pos_ptr):
-        ctf_it_ptr = nbt._bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr)
+        if self.intersect_mode:
+            if not self.has_intersection:
+                # There are no events to provide.
+                return
+
+            ctf_it_ptr = nbt._bt_python_ctf_iter_create_intersect(
+                self._tc, begin_pos_ptr, end_pos_ptr
+            )
+        else:
+            ctf_it_ptr = nbt._bt_ctf_iter_create(
+                self._tc, begin_pos_ptr, end_pos_ptr
+            )
 
         if ctf_it_ptr is None:
             raise NotImplementedError("Creation of multiple iterators is unsupported.")
@@ -283,9 +311,11 @@ class TraceHandle:
         underlying trace.
         """
 
-        return nbt._bt_trace_handle_get_timestamp_begin(self._trace_collection._tc,
-                                                        self._id,
-                                                        _ClockType.CLOCK_REAL)
+        ret, value = nbt._bt_trace_handle_get_timestamp_begin(
+            self._trace_collection._tc, self._id, _ClockType.CLOCK_REAL)
+        if ret != 0:
+            raise ValueError("Invalid TraceHandle")
+        return value
 
     @property
     def timestamp_end(self):
@@ -294,9 +324,11 @@ class TraceHandle:
         underlying trace.
         """
 
-        return nbt._bt_trace_handle_get_timestamp_end(self._trace_collection._tc,
-                                                      self._id,
-                                                      _ClockType.CLOCK_REAL)
+        ret, value = nbt._bt_trace_handle_get_timestamp_end(
+            self._trace_collection._tc, self._id, _ClockType.CLOCK_REAL)
+        if ret != 0:
+            raise ValueError("Invalid TraceHandle")
+        return value
 
     @property
     def events(self):
@@ -413,10 +445,13 @@ class Event(collections.Mapping):
     @property
     def timestamp(self):
         """
-        Event timestamp (nanoseconds since Epoch) or -1 on error.
+        Event timestamp (nanoseconds since Epoch).
         """
 
-        return nbt._bt_ctf_get_timestamp(self._e)
+        ret, value = nbt._bt_ctf_get_timestamp(self._e)
+        if ret < 0:
+            raise RuntimeError("Failed to get event timestamp")
+        return value
 
     @property
     def datetime(self):
This page took 0.02568 seconds and 4 git commands to generate.