1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
6 from bt2
import interrupter
as bt2_interrupter
7 from bt2
import value
as bt2_value
12 from bt2
import component
as bt2_component
17 class _QueryExecutorCommon
:
19 def _common_ptr(self
):
20 return self
._as
_query
_executor
_ptr
()
23 def is_interrupted(self
):
24 is_interrupted
= native_bt
.query_executor_is_interrupted(self
._common
_ptr
)
25 return bool(is_interrupted
)
28 def logging_level(self
):
29 return native_bt
.query_executor_get_logging_level(self
._common
_ptr
)
32 class QueryExecutor(object._SharedObject
, _QueryExecutorCommon
):
35 native_bt
.query_executor_get_ref(ptr
)
39 native_bt
.query_executor_put_ref(ptr
)
41 def _as_query_executor_ptr(self
):
44 def __init__(self
, component_class
, object_name
, params
=None, method_obj
=None):
45 if not isinstance(component_class
, _bt2_component()._ComponentClassConst
):
49 if not issubclass(component_class
, _bt2_component()._UserComponent
):
56 raise TypeError("'{}' is not a component class object".format(o
))
58 utils
._check
_str
(object_name
)
61 params_ptr
= native_bt
.value_null
63 params
= bt2
.create_value(params
)
64 params_ptr
= params
._ptr
66 cc_ptr
= component_class
._bt
_component
_class
_ptr
()
67 assert cc_ptr
is not None
69 if method_obj
is not None and not native_bt
.bt2_is_python_component_class(
73 "cannot pass a Python object to a non-Python component class"
76 ptr
= native_bt
.bt2_query_executor_create(
77 cc_ptr
, object_name
, params_ptr
, method_obj
81 raise bt2
._MemoryError("cannot create query executor object")
85 # Keep a reference of `method_obj` as the native query executor
86 # does not have any. This ensures that, when this object's
87 # query() method is called, the Python object still exists.
88 self
._method
_obj
= method_obj
90 def add_interrupter(self
, interrupter
):
91 utils
._check
_type
(interrupter
, bt2_interrupter
.Interrupter
)
92 native_bt
.query_executor_add_interrupter(self
._ptr
, interrupter
._ptr
)
95 def default_interrupter(self
):
96 ptr
= native_bt
.query_executor_borrow_default_interrupter(self
._ptr
)
97 return bt2_interrupter
.Interrupter
._create
_from
_ptr
_and
_get
_ref
(ptr
)
99 def _set_logging_level(self
, log_level
):
100 utils
._check
_log
_level
(log_level
)
101 status
= native_bt
.query_executor_set_logging_level(self
._ptr
, log_level
)
102 utils
._handle
_func
_status
(status
, "cannot set query executor's logging level")
104 logging_level
= property(
105 fget
=_QueryExecutorCommon
.logging_level
, fset
=_set_logging_level
109 def is_interrupted(self
):
110 is_interrupted
= native_bt
.query_executor_is_interrupted(self
._ptr
)
111 return bool(is_interrupted
)
114 status
, result_ptr
= native_bt
.query_executor_query(self
._ptr
)
115 utils
._handle
_func
_status
(status
, "cannot query component class")
116 assert result_ptr
is not None
117 return bt2_value
._create
_from
_const
_ptr
(result_ptr
)
120 class _PrivateQueryExecutor(_QueryExecutorCommon
):
121 def __init__(self
, ptr
):
124 def _check_validity(self
):
125 if self
._ptr
is None:
126 raise RuntimeError("this object is not valid anymore")
128 def _as_query_executor_ptr(self
):
129 self
._check
_validity
()
130 return native_bt
.private_query_executor_as_query_executor_const(self
._ptr
)
132 def _invalidate(self
):
This page took 0.039745 seconds and 4 git commands to generate.