lib: prepare the ground for stateful query operations
[babeltrace.git] / src / bindings / python / bt2 / bt2 / query_executor.py
index 146a2cf461b81b2cb59633d3e3049b8f7952e9ef..77b5857496fbae9b2143892734f5bb94c01df541 100644 (file)
@@ -28,37 +28,29 @@ from bt2 import value as bt2_value
 import bt2
 
 
-class QueryExecutor(object._SharedObject):
-    _get_ref = staticmethod(native_bt.query_executor_get_ref)
-    _put_ref = staticmethod(native_bt.query_executor_put_ref)
+class _QueryExecutorCommon:
+    @property
+    def _common_ptr(self):
+        return self._as_query_executor_ptr()
 
-    def __init__(self):
-        ptr = native_bt.query_executor_create()
+    @property
+    def is_interrupted(self):
+        is_interrupted = native_bt.query_executor_is_interrupted(self._common_ptr)
+        return bool(is_interrupted)
 
-        if ptr is None:
-            raise bt2._MemoryError('cannot create query executor object')
+    @property
+    def logging_level(self):
+        return native_bt.query_executor_get_logging_level(self._common_ptr)
 
-        super().__init__(ptr)
 
-    def add_interrupter(self, interrupter):
-        utils._check_type(interrupter, bt2_interrupter.Interrupter)
-        native_bt.query_executor_add_interrupter(self._ptr, interrupter._ptr)
+class QueryExecutor(object._SharedObject, _QueryExecutorCommon):
+    _get_ref = staticmethod(native_bt.query_executor_get_ref)
+    _put_ref = staticmethod(native_bt.query_executor_put_ref)
 
-    def interrupt(self):
-        native_bt.query_executor_interrupt(self._ptr)
+    def _as_query_executor_ptr(self):
+        return self._ptr
 
-    @property
-    def is_interrupted(self):
-        is_interrupted = native_bt.query_executor_is_interrupted(self._ptr)
-        return bool(is_interrupted)
-
-    def query(
-        self,
-        component_class,
-        object,
-        params=None,
-        logging_level=bt2_logging.LoggingLevel.NONE,
-    ):
+    def __init__(self, component_class, object, params=None):
         if not isinstance(component_class, bt2_component._ComponentClass):
             err = False
 
@@ -80,12 +72,54 @@ class QueryExecutor(object._SharedObject):
             params = bt2.create_value(params)
             params_ptr = params._ptr
 
-        utils._check_log_level(logging_level)
         cc_ptr = component_class._bt_component_class_ptr()
+        assert cc_ptr is not None
+        ptr = native_bt.query_executor_create(cc_ptr, object, params_ptr)
+
+        if ptr is None:
+            raise bt2._MemoryError('cannot create query executor object')
+
+        super().__init__(ptr)
+
+    def add_interrupter(self, interrupter):
+        utils._check_type(interrupter, bt2_interrupter.Interrupter)
+        native_bt.query_executor_add_interrupter(self._ptr, interrupter._ptr)
+
+    def interrupt(self):
+        native_bt.query_executor_interrupt(self._ptr)
+
+    def _set_logging_level(self, log_level):
+        utils._check_log_level(log_level)
+        status = native_bt.query_executor_set_logging_level(self._ptr, log_level)
+        utils._handle_func_status(status, "cannot set query executor's logging level")
 
-        status, result_ptr = native_bt.query_executor_query(
-            self._ptr, cc_ptr, object, params_ptr, logging_level
-        )
+    logging_level = property(
+        fget=_QueryExecutorCommon.logging_level, fset=_set_logging_level
+    )
+
+    @property
+    def is_interrupted(self):
+        is_interrupted = native_bt.query_executor_is_interrupted(self._ptr)
+        return bool(is_interrupted)
+
+    def query(self):
+        status, result_ptr = native_bt.query_executor_query(self._ptr)
         utils._handle_func_status(status, 'cannot query component class')
-        assert result_ptr
+        assert result_ptr is not None
         return bt2_value._create_from_ptr(result_ptr)
+
+
+class _PrivateQueryExecutor(_QueryExecutorCommon):
+    def __init__(self, ptr):
+        self._ptr = ptr
+
+    def _check_validity(self):
+        if self._ptr is None:
+            raise RuntimeError('this object is not valid anymore')
+
+    def _as_query_executor_ptr(self):
+        self._check_validity()
+        return native_bt.private_query_executor_as_query_executor_const(self._ptr)
+
+    def _invalidate(self):
+        self._ptr = None
This page took 0.026447 seconds and 4 git commands to generate.