From 464425e1cee38fb2bbfb8f88cacc0de90abc0580 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Wed, 20 Nov 2013 01:09:31 -0500 Subject: [PATCH] Python-bindings: Refactor the TraceHandle class to use properties MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit TraceHandle now exposes properties instead of getters. It also keeps a reference to its parent TraceCollection. Signed-off-by: Jérémie Galarneau --- bindings/python/babeltrace.i.in | 58 ++++++++++++--------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/bindings/python/babeltrace.i.in b/bindings/python/babeltrace.i.in index ac7c6402..ef2474ad 100644 --- a/bindings/python/babeltrace.i.in +++ b/bindings/python/babeltrace.i.in @@ -147,13 +147,13 @@ class TraceCollection: Return: the corresponding TraceHandle on success or None on error. """ - ret = _bt_context_add_trace(self._tc, path, format_str, None, - None, None) + ret = _bt_context_add_trace(self._tc, path, format_str, None, None, None) if ret < 0: return None th = TraceHandle.__new__(TraceHandle) th._id = ret + th._trace_collection = self return th def add_traces_recursive(self, path, format_str): @@ -357,20 +357,6 @@ struct bt_iter_pos { } u; }; -/* ================================================================= - CLOCK-TYPE.H - ¯¯¯¯¯¯¯¯¯¯¯¯ - *** Enum copied from clock-type.h­ - All changes must also be made here -*/ -%rename("CLOCK_CYCLES") BT_CLOCK_CYCLES; -%rename("CLOCK_REAL") BT_CLOCK_REAL; - -enum bt_clock_type { - BT_CLOCK_CYCLES = 0, - BT_CLOCK_REAL -}; - /* ================================================================= TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ @@ -400,6 +386,11 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event); %pythoncode%{ +# Based on enum bt_clock_type in clock-type.h­ +class ClockType: + CLOCK_CYCLES = 0 + CLOCK_REAL = 1 + class TraceHandle(object): """ The TraceHandle allows the user to manipulate a trace file directly. @@ -413,35 +404,27 @@ class TraceHandle(object): def __repr__(self): return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id) - def get_id(self): + @property + def id(self): """Return the TraceHandle id.""" return self._id - def get_path(self, trace_collection): + @property + def path(self): """Return the path of a TraceHandle.""" - try: - return _bt_trace_handle_get_path(trace_collection._tc, self._id) - except AttributeError: - raise TypeError("in get_path, " - "argument 2 must be a TraceCollection instance") + return _bt_trace_handle_get_path(self._trace_collection._tc, self._id) - def get_timestamp_begin(self, trace_collection, clock_type): + @property + def timestamp_begin(self): """Return the creation time of the buffers of a trace.""" - try: - return _bt_trace_handle_get_timestamp_begin( - trace_collection._tc, self._id,clock_type) - except AttributeError: - raise TypeError("in get_timestamp_begin, " - "argument 2 must be a TraceCollection instance") + return _bt_trace_handle_get_timestamp_begin( + self._trace_collection._tc, self._id, ClockType.CLOCK_REAL) - def get_timestamp_end(self, trace_collection, clock_type): + @property + def timestamp_end(self): """Return the destruction timestamp of the buffers of a trace.""" - try: - return _bt_trace_handle_get_timestamp_end( - trace_collection._tc, self._id, clock_type) - except AttributeError: - raise TypeError("in get_timestamp_end, " - "argument 2 must be a TraceCollection instance") + return _bt_trace_handle_get_timestamp_end( + self._trace_collection._tc, self._id, ClockType.CLOCK_REAL) %} @@ -749,6 +732,7 @@ class CTFReader: th = TraceHandle.__new__(TraceHandle) th._id = ret + th._trace_collection = self.get_trace_collection() return th def get_trace_collection(self): -- 2.34.1