bt2: Add `Const` suffix to `_*Port` classes and adapt tests
[babeltrace.git] / src / bindings / python / bt2 / bt2 / plugin.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23from bt2 import native_bt, object, utils
24import collections.abc
3fb99a22 25from bt2 import component as bt2_component
811644b8 26import os.path
81447b5b
PP
27
28
577fa92f 29def find_plugins_in_path(path, recurse=True, fail_on_load_error=False):
811644b8
PP
30 utils._check_str(path)
31 utils._check_bool(recurse)
9736d991 32 utils._check_bool(fail_on_load_error)
811644b8 33 plugin_set_ptr = None
81447b5b 34
811644b8 35 if os.path.isfile(path):
cfbd7cf3
FD
36 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(
37 path, fail_on_load_error
38 )
811644b8 39 elif os.path.isdir(path):
cfbd7cf3
FD
40 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(
41 path, int(recurse), int(fail_on_load_error)
42 )
9736d991 43 else:
ce4923b0 44 raise ValueError("invalid path: '{}'".format(path))
9736d991 45
d24d5663 46 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 47 return
81447b5b 48
d24d5663 49 utils._handle_func_status(status, 'failed to find plugins')
9736d991 50 assert plugin_set_ptr is not None
811644b8 51 return _PluginSet._create_from_ptr(plugin_set_ptr)
81447b5b 52
81447b5b 53
577fa92f
PP
54def find_plugins(
55 find_in_std_env_var=True,
56 find_in_user_dir=True,
57 find_in_sys_dir=True,
58 find_in_static=True,
59 fail_on_load_error=False,
60):
61 utils._check_bool(find_in_std_env_var)
62 utils._check_bool(find_in_user_dir)
63 utils._check_bool(find_in_sys_dir)
64 utils._check_bool(find_in_static)
65 utils._check_bool(fail_on_load_error)
66 plugin_set_ptr = None
67
68 status, plugin_set_ptr = native_bt.bt2_plugin_find_all(
69 int(find_in_std_env_var),
70 int(find_in_user_dir),
71 int(find_in_sys_dir),
72 int(find_in_static),
73 int(fail_on_load_error),
74 )
75
76 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
77 return
78
79 utils._handle_func_status(status, 'failed to find plugins')
80 assert plugin_set_ptr is not None
81 return _PluginSet._create_from_ptr(plugin_set_ptr)
82
83
84def find_plugin(
85 name,
86 find_in_std_env_var=True,
87 find_in_user_dir=True,
88 find_in_sys_dir=True,
89 find_in_static=True,
90 fail_on_load_error=False,
91):
811644b8 92 utils._check_str(name)
9736d991 93 utils._check_bool(fail_on_load_error)
577fa92f
PP
94 status, ptr = native_bt.bt2_plugin_find(
95 name,
96 int(find_in_std_env_var),
97 int(find_in_user_dir),
98 int(find_in_sys_dir),
99 int(find_in_static),
100 int(fail_on_load_error),
101 )
81447b5b 102
d24d5663 103 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 104 return
81447b5b 105
d24d5663 106 utils._handle_func_status(status, 'failed to find plugin')
9736d991 107 assert ptr is not None
811644b8 108 return _Plugin._create_from_ptr(ptr)
81447b5b 109
81447b5b 110
78288f58 111class _PluginSet(object._SharedObject, collections.abc.Sequence):
2f16a6a2
PP
112 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
113 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
ab1bca6c 114
811644b8
PP
115 def __len__(self):
116 count = native_bt.plugin_set_get_plugin_count(self._ptr)
cfbd7cf3 117 assert count >= 0
811644b8 118 return count
81447b5b 119
811644b8
PP
120 def __getitem__(self, index):
121 utils._check_uint64(index)
7eb48359 122
811644b8
PP
123 if index >= len(self):
124 raise IndexError
7eb48359 125
ab1bca6c
SM
126 plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index)
127 assert plugin_ptr is not None
128 return _Plugin._create_from_ptr_and_get_ref(plugin_ptr)
7eb48359
PP
129
130
81447b5b
PP
131class _PluginVersion:
132 def __init__(self, major, minor, patch, extra):
133 self._major = major
134 self._minor = minor
135 self._patch = patch
136 self._extra = extra
137
138 @property
139 def major(self):
140 return self._major
141
142 @property
143 def minor(self):
144 return self._minor
145
146 @property
147 def patch(self):
148 return self._patch
149
150 @property
151 def extra(self):
152 return self._extra
153
154 def __str__(self):
155 extra = ''
156
157 if self._extra is not None:
158 extra = self._extra
159
160 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
161
162
811644b8
PP
163class _PluginComponentClassesIterator(collections.abc.Iterator):
164 def __init__(self, plugin_comp_cls):
165 self._plugin_comp_cls = plugin_comp_cls
166 self._at = 0
167
168 def __next__(self):
169 plugin_ptr = self._plugin_comp_cls._plugin._ptr
ab1bca6c 170 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
811644b8 171
ab1bca6c
SM
172 if self._at == total:
173 raise StopIteration
811644b8 174
cfbd7cf3
FD
175 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(
176 plugin_ptr, self._at
177 )
ab1bca6c
SM
178 assert comp_cls_ptr is not None
179 self._at += 1
811644b8 180
ab1bca6c 181 comp_cls_type = self._plugin_comp_cls._comp_cls_type
3fb99a22 182 comp_cls_pycls = bt2_component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
cfbd7cf3
FD
183 comp_cls_type
184 ]
85906b6b 185 comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr)
811644b8 186 name = native_bt.component_class_get_name(comp_cls_ptr)
ab1bca6c 187 assert name is not None
811644b8
PP
188 return name
189
190
191class _PluginComponentClasses(collections.abc.Mapping):
ab1bca6c 192 def __init__(self, plugin):
811644b8 193 self._plugin = plugin
811644b8
PP
194
195 def __getitem__(self, key):
196 utils._check_str(key)
ab1bca6c 197 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
811644b8
PP
198
199 if cc_ptr is None:
200 raise KeyError(key)
201
3fb99a22 202 return bt2_component._create_component_class_from_ptr_and_get_ref(
cfbd7cf3
FD
203 cc_ptr, self._comp_cls_type
204 )
811644b8
PP
205
206 def __len__(self):
ab1bca6c
SM
207 return self._component_class_count(self._plugin._ptr)
208
209 def __iter__(self):
210 return _PluginComponentClassesIterator(self)
811644b8 211
811644b8 212
ab1bca6c 213class _PluginSourceComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
214 _component_class_count = staticmethod(
215 native_bt.plugin_get_source_component_class_count
216 )
217 _borrow_component_class_by_name = staticmethod(
218 native_bt.plugin_borrow_source_component_class_by_name_const
219 )
220 _borrow_component_class_by_index = staticmethod(
221 native_bt.plugin_borrow_source_component_class_by_index_const
222 )
ab1bca6c 223 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
811644b8 224
811644b8 225
ab1bca6c 226class _PluginFilterComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
227 _component_class_count = staticmethod(
228 native_bt.plugin_get_filter_component_class_count
229 )
230 _borrow_component_class_by_name = staticmethod(
231 native_bt.plugin_borrow_filter_component_class_by_name_const
232 )
233 _borrow_component_class_by_index = staticmethod(
234 native_bt.plugin_borrow_filter_component_class_by_index_const
235 )
ab1bca6c 236 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 237
ab1bca6c
SM
238
239class _PluginSinkComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
240 _component_class_count = staticmethod(
241 native_bt.plugin_get_sink_component_class_count
242 )
243 _borrow_component_class_by_name = staticmethod(
244 native_bt.plugin_borrow_sink_component_class_by_name_const
245 )
246 _borrow_component_class_by_index = staticmethod(
247 native_bt.plugin_borrow_sink_component_class_by_index_const
248 )
ab1bca6c 249 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
811644b8
PP
250
251
78288f58 252class _Plugin(object._SharedObject):
2f16a6a2
PP
253 _put_ref = staticmethod(native_bt.plugin_put_ref)
254 _get_ref = staticmethod(native_bt.plugin_get_ref)
ab1bca6c 255
81447b5b
PP
256 @property
257 def name(self):
258 name = native_bt.plugin_get_name(self._ptr)
cfbd7cf3 259 assert name is not None
81447b5b
PP
260 return name
261
262 @property
263 def author(self):
264 return native_bt.plugin_get_author(self._ptr)
265
266 @property
267 def license(self):
268 return native_bt.plugin_get_license(self._ptr)
269
270 @property
271 def description(self):
272 return native_bt.plugin_get_description(self._ptr)
273
274 @property
275 def path(self):
276 return native_bt.plugin_get_path(self._ptr)
277
278 @property
279 def version(self):
d24d5663 280 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
81447b5b 281
ab1bca6c 282 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
81447b5b
PP
283 return
284
285 return _PluginVersion(major, minor, patch, extra)
286
811644b8
PP
287 @property
288 def source_component_classes(self):
ab1bca6c 289 return _PluginSourceComponentClasses(self)
81447b5b 290
811644b8
PP
291 @property
292 def filter_component_classes(self):
ab1bca6c 293 return _PluginFilterComponentClasses(self)
81447b5b 294
811644b8
PP
295 @property
296 def sink_component_classes(self):
ab1bca6c 297 return _PluginSinkComponentClasses(self)
This page took 0.061247 seconds and 4 git commands to generate.