Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
c7eee084 PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
c7eee084 PP |
4 | |
5 | from bt2 import native_bt, object, utils | |
3fb99a22 | 6 | from bt2 import interrupter as bt2_interrupter |
3fb99a22 | 7 | from bt2 import value as bt2_value |
c7eee084 PP |
8 | import bt2 |
9 | ||
10 | ||
79935628 SM |
11 | def _bt2_component(): |
12 | from bt2 import component as bt2_component | |
13 | ||
14 | return bt2_component | |
15 | ||
16 | ||
3c729b9a PP |
17 | class _QueryExecutorCommon: |
18 | @property | |
19 | def _common_ptr(self): | |
20 | return self._as_query_executor_ptr() | |
601c0026 | 21 | |
3c729b9a PP |
22 | @property |
23 | def is_interrupted(self): | |
24 | is_interrupted = native_bt.query_executor_is_interrupted(self._common_ptr) | |
25 | return bool(is_interrupted) | |
c7eee084 | 26 | |
3c729b9a PP |
27 | @property |
28 | def logging_level(self): | |
29 | return native_bt.query_executor_get_logging_level(self._common_ptr) | |
c7eee084 | 30 | |
c7eee084 | 31 | |
3c729b9a | 32 | class QueryExecutor(object._SharedObject, _QueryExecutorCommon): |
9dee90bd SM |
33 | @staticmethod |
34 | def _get_ref(ptr): | |
35 | native_bt.query_executor_get_ref(ptr) | |
36 | ||
37 | @staticmethod | |
38 | def _put_ref(ptr): | |
39 | native_bt.query_executor_put_ref(ptr) | |
9b4f9b42 | 40 | |
3c729b9a PP |
41 | def _as_query_executor_ptr(self): |
42 | return self._ptr | |
c7eee084 | 43 | |
0b9ddc00 | 44 | def __init__(self, component_class, object_name, params=None, method_obj=None): |
79935628 | 45 | if not isinstance(component_class, _bt2_component()._ComponentClassConst): |
c7eee084 PP |
46 | err = False |
47 | ||
48 | try: | |
79935628 | 49 | if not issubclass(component_class, _bt2_component()._UserComponent): |
c7eee084 PP |
50 | err = True |
51 | except TypeError: | |
52 | err = True | |
53 | ||
54 | if err: | |
55 | o = component_class | |
56 | raise TypeError("'{}' is not a component class object".format(o)) | |
57 | ||
0b9ddc00 | 58 | utils._check_str(object_name) |
c7eee084 PP |
59 | |
60 | if params is None: | |
61 | params_ptr = native_bt.value_null | |
62 | else: | |
63 | params = bt2.create_value(params) | |
64 | params_ptr = params._ptr | |
65 | ||
85906b6b | 66 | cc_ptr = component_class._bt_component_class_ptr() |
3c729b9a | 67 | assert cc_ptr is not None |
7c14d641 PP |
68 | |
69 | if method_obj is not None and not native_bt.bt2_is_python_component_class( | |
70 | cc_ptr | |
71 | ): | |
72 | raise ValueError( | |
f5567ea8 | 73 | "cannot pass a Python object to a non-Python component class" |
7c14d641 PP |
74 | ) |
75 | ||
76 | ptr = native_bt.bt2_query_executor_create( | |
0b9ddc00 | 77 | cc_ptr, object_name, params_ptr, method_obj |
7c14d641 | 78 | ) |
3c729b9a PP |
79 | |
80 | if ptr is None: | |
f5567ea8 | 81 | raise bt2._MemoryError("cannot create query executor object") |
3c729b9a PP |
82 | |
83 | super().__init__(ptr) | |
84 | ||
7c14d641 PP |
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 | |
89 | ||
3c729b9a PP |
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) | |
93 | ||
88d1a0b7 SM |
94 | @property |
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) | |
3c729b9a PP |
98 | |
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") | |
c7eee084 | 103 | |
3c729b9a PP |
104 | logging_level = property( |
105 | fget=_QueryExecutorCommon.logging_level, fset=_set_logging_level | |
106 | ) | |
107 | ||
108 | @property | |
109 | def is_interrupted(self): | |
110 | is_interrupted = native_bt.query_executor_is_interrupted(self._ptr) | |
111 | return bool(is_interrupted) | |
112 | ||
113 | def query(self): | |
114 | status, result_ptr = native_bt.query_executor_query(self._ptr) | |
f5567ea8 | 115 | utils._handle_func_status(status, "cannot query component class") |
3c729b9a | 116 | assert result_ptr is not None |
e42e1587 | 117 | return bt2_value._create_from_const_ptr(result_ptr) |
3c729b9a PP |
118 | |
119 | ||
120 | class _PrivateQueryExecutor(_QueryExecutorCommon): | |
121 | def __init__(self, ptr): | |
122 | self._ptr = ptr | |
123 | ||
124 | def _check_validity(self): | |
125 | if self._ptr is None: | |
f5567ea8 | 126 | raise RuntimeError("this object is not valid anymore") |
3c729b9a PP |
127 | |
128 | def _as_query_executor_ptr(self): | |
129 | self._check_validity() | |
130 | return native_bt.private_query_executor_as_query_executor_const(self._ptr) | |
131 | ||
132 | def _invalidate(self): | |
133 | self._ptr = None |