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
):
33 _get_ref
= staticmethod(native_bt
.query_executor_get_ref
)
34 _put_ref
= staticmethod(native_bt
.query_executor_put_ref
)
36 def _as_query_executor_ptr(self
):
39 def __init__(self
, component_class
, object_name
, params
=None, method_obj
=None):
40 if not isinstance(component_class
, _bt2_component()._ComponentClassConst
):
44 if not issubclass(component_class
, _bt2_component()._UserComponent
):
51 raise TypeError("'{}' is not a component class object".format(o
))
53 utils
._check
_str
(object_name
)
56 params_ptr
= native_bt
.value_null
58 params
= bt2
.create_value(params
)
59 params_ptr
= params
._ptr
61 cc_ptr
= component_class
._bt
_component
_class
_ptr
()
62 assert cc_ptr
is not None
64 if method_obj
is not None and not native_bt
.bt2_is_python_component_class(
68 'cannot pass a Python object to a non-Python component class'
71 ptr
= native_bt
.bt2_query_executor_create(
72 cc_ptr
, object_name
, params_ptr
, method_obj
76 raise bt2
._MemoryError('cannot create query executor object')
80 # Keep a reference of `method_obj` as the native query executor
81 # does not have any. This ensures that, when this object's
82 # query() method is called, the Python object still exists.
83 self
._method
_obj
= method_obj
85 def add_interrupter(self
, interrupter
):
86 utils
._check
_type
(interrupter
, bt2_interrupter
.Interrupter
)
87 native_bt
.query_executor_add_interrupter(self
._ptr
, interrupter
._ptr
)
90 def default_interrupter(self
):
91 ptr
= native_bt
.query_executor_borrow_default_interrupter(self
._ptr
)
92 return bt2_interrupter
.Interrupter
._create
_from
_ptr
_and
_get
_ref
(ptr
)
94 def _set_logging_level(self
, log_level
):
95 utils
._check
_log
_level
(log_level
)
96 status
= native_bt
.query_executor_set_logging_level(self
._ptr
, log_level
)
97 utils
._handle
_func
_status
(status
, "cannot set query executor's logging level")
99 logging_level
= property(
100 fget
=_QueryExecutorCommon
.logging_level
, fset
=_set_logging_level
104 def is_interrupted(self
):
105 is_interrupted
= native_bt
.query_executor_is_interrupted(self
._ptr
)
106 return bool(is_interrupted
)
109 status
, result_ptr
= native_bt
.query_executor_query(self
._ptr
)
110 utils
._handle
_func
_status
(status
, 'cannot query component class')
111 assert result_ptr
is not None
112 return bt2_value
._create
_from
_const
_ptr
(result_ptr
)
115 class _PrivateQueryExecutor(_QueryExecutorCommon
):
116 def __init__(self
, ptr
):
119 def _check_validity(self
):
120 if self
._ptr
is None:
121 raise RuntimeError('this object is not valid anymore')
123 def _as_query_executor_ptr(self
):
124 self
._check
_validity
()
125 return native_bt
.private_query_executor_as_query_executor_const(self
._ptr
)
127 def _invalidate(self
):
This page took 0.031395 seconds and 4 git commands to generate.