test_error.py: remove dangling print()
[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
25import bt2.component
811644b8 26import os.path
81447b5b
PP
27import bt2
28
29
577fa92f 30def find_plugins_in_path(path, recurse=True, fail_on_load_error=False):
811644b8
PP
31 utils._check_str(path)
32 utils._check_bool(recurse)
9736d991 33 utils._check_bool(fail_on_load_error)
811644b8 34 plugin_set_ptr = None
81447b5b 35
811644b8 36 if os.path.isfile(path):
cfbd7cf3
FD
37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(
38 path, fail_on_load_error
39 )
811644b8 40 elif os.path.isdir(path):
cfbd7cf3
FD
41 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(
42 path, int(recurse), int(fail_on_load_error)
43 )
9736d991 44 else:
ce4923b0 45 raise ValueError("invalid path: '{}'".format(path))
9736d991 46
d24d5663 47 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 48 return
81447b5b 49
d24d5663 50 utils._handle_func_status(status, 'failed to find plugins')
9736d991 51 assert plugin_set_ptr is not None
811644b8 52 return _PluginSet._create_from_ptr(plugin_set_ptr)
81447b5b 53
81447b5b 54
577fa92f
PP
55def find_plugins(
56 find_in_std_env_var=True,
57 find_in_user_dir=True,
58 find_in_sys_dir=True,
59 find_in_static=True,
60 fail_on_load_error=False,
61):
62 utils._check_bool(find_in_std_env_var)
63 utils._check_bool(find_in_user_dir)
64 utils._check_bool(find_in_sys_dir)
65 utils._check_bool(find_in_static)
66 utils._check_bool(fail_on_load_error)
67 plugin_set_ptr = None
68
69 status, plugin_set_ptr = native_bt.bt2_plugin_find_all(
70 int(find_in_std_env_var),
71 int(find_in_user_dir),
72 int(find_in_sys_dir),
73 int(find_in_static),
74 int(fail_on_load_error),
75 )
76
77 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
78 return
79
80 utils._handle_func_status(status, 'failed to find plugins')
81 assert plugin_set_ptr is not None
82 return _PluginSet._create_from_ptr(plugin_set_ptr)
83
84
85def find_plugin(
86 name,
87 find_in_std_env_var=True,
88 find_in_user_dir=True,
89 find_in_sys_dir=True,
90 find_in_static=True,
91 fail_on_load_error=False,
92):
811644b8 93 utils._check_str(name)
9736d991 94 utils._check_bool(fail_on_load_error)
577fa92f
PP
95 status, ptr = native_bt.bt2_plugin_find(
96 name,
97 int(find_in_std_env_var),
98 int(find_in_user_dir),
99 int(find_in_sys_dir),
100 int(find_in_static),
101 int(fail_on_load_error),
102 )
81447b5b 103
d24d5663 104 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 105 return
81447b5b 106
d24d5663 107 utils._handle_func_status(status, 'failed to find plugin')
9736d991 108 assert ptr is not None
811644b8 109 return _Plugin._create_from_ptr(ptr)
81447b5b 110
81447b5b 111
78288f58 112class _PluginSet(object._SharedObject, collections.abc.Sequence):
2f16a6a2
PP
113 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
114 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
ab1bca6c 115
811644b8
PP
116 def __len__(self):
117 count = native_bt.plugin_set_get_plugin_count(self._ptr)
cfbd7cf3 118 assert count >= 0
811644b8 119 return count
81447b5b 120
811644b8
PP
121 def __getitem__(self, index):
122 utils._check_uint64(index)
7eb48359 123
811644b8
PP
124 if index >= len(self):
125 raise IndexError
7eb48359 126
ab1bca6c
SM
127 plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index)
128 assert plugin_ptr is not None
129 return _Plugin._create_from_ptr_and_get_ref(plugin_ptr)
7eb48359
PP
130
131
81447b5b
PP
132class _PluginVersion:
133 def __init__(self, major, minor, patch, extra):
134 self._major = major
135 self._minor = minor
136 self._patch = patch
137 self._extra = extra
138
139 @property
140 def major(self):
141 return self._major
142
143 @property
144 def minor(self):
145 return self._minor
146
147 @property
148 def patch(self):
149 return self._patch
150
151 @property
152 def extra(self):
153 return self._extra
154
155 def __str__(self):
156 extra = ''
157
158 if self._extra is not None:
159 extra = self._extra
160
161 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
162
163
811644b8
PP
164class _PluginComponentClassesIterator(collections.abc.Iterator):
165 def __init__(self, plugin_comp_cls):
166 self._plugin_comp_cls = plugin_comp_cls
167 self._at = 0
168
169 def __next__(self):
170 plugin_ptr = self._plugin_comp_cls._plugin._ptr
ab1bca6c 171 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
811644b8 172
ab1bca6c
SM
173 if self._at == total:
174 raise StopIteration
811644b8 175
cfbd7cf3
FD
176 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(
177 plugin_ptr, self._at
178 )
ab1bca6c
SM
179 assert comp_cls_ptr is not None
180 self._at += 1
811644b8 181
ab1bca6c 182 comp_cls_type = self._plugin_comp_cls._comp_cls_type
cfbd7cf3
FD
183 comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
184 comp_cls_type
185 ]
85906b6b 186 comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr)
811644b8 187 name = native_bt.component_class_get_name(comp_cls_ptr)
ab1bca6c 188 assert name is not None
811644b8
PP
189 return name
190
191
192class _PluginComponentClasses(collections.abc.Mapping):
ab1bca6c 193 def __init__(self, plugin):
811644b8 194 self._plugin = plugin
811644b8
PP
195
196 def __getitem__(self, key):
197 utils._check_str(key)
ab1bca6c 198 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
811644b8
PP
199
200 if cc_ptr is None:
201 raise KeyError(key)
202
cfbd7cf3
FD
203 return bt2.component._create_component_class_from_ptr_and_get_ref(
204 cc_ptr, self._comp_cls_type
205 )
811644b8
PP
206
207 def __len__(self):
ab1bca6c
SM
208 return self._component_class_count(self._plugin._ptr)
209
210 def __iter__(self):
211 return _PluginComponentClassesIterator(self)
811644b8 212
811644b8 213
ab1bca6c 214class _PluginSourceComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
215 _component_class_count = staticmethod(
216 native_bt.plugin_get_source_component_class_count
217 )
218 _borrow_component_class_by_name = staticmethod(
219 native_bt.plugin_borrow_source_component_class_by_name_const
220 )
221 _borrow_component_class_by_index = staticmethod(
222 native_bt.plugin_borrow_source_component_class_by_index_const
223 )
ab1bca6c 224 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
811644b8 225
811644b8 226
ab1bca6c 227class _PluginFilterComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
228 _component_class_count = staticmethod(
229 native_bt.plugin_get_filter_component_class_count
230 )
231 _borrow_component_class_by_name = staticmethod(
232 native_bt.plugin_borrow_filter_component_class_by_name_const
233 )
234 _borrow_component_class_by_index = staticmethod(
235 native_bt.plugin_borrow_filter_component_class_by_index_const
236 )
ab1bca6c 237 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 238
ab1bca6c
SM
239
240class _PluginSinkComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
241 _component_class_count = staticmethod(
242 native_bt.plugin_get_sink_component_class_count
243 )
244 _borrow_component_class_by_name = staticmethod(
245 native_bt.plugin_borrow_sink_component_class_by_name_const
246 )
247 _borrow_component_class_by_index = staticmethod(
248 native_bt.plugin_borrow_sink_component_class_by_index_const
249 )
ab1bca6c 250 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
811644b8
PP
251
252
78288f58 253class _Plugin(object._SharedObject):
2f16a6a2
PP
254 _put_ref = staticmethod(native_bt.plugin_put_ref)
255 _get_ref = staticmethod(native_bt.plugin_get_ref)
ab1bca6c 256
81447b5b
PP
257 @property
258 def name(self):
259 name = native_bt.plugin_get_name(self._ptr)
cfbd7cf3 260 assert name is not None
81447b5b
PP
261 return name
262
263 @property
264 def author(self):
265 return native_bt.plugin_get_author(self._ptr)
266
267 @property
268 def license(self):
269 return native_bt.plugin_get_license(self._ptr)
270
271 @property
272 def description(self):
273 return native_bt.plugin_get_description(self._ptr)
274
275 @property
276 def path(self):
277 return native_bt.plugin_get_path(self._ptr)
278
279 @property
280 def version(self):
d24d5663 281 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
81447b5b 282
ab1bca6c 283 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
81447b5b
PP
284 return
285
286 return _PluginVersion(major, minor, patch, extra)
287
811644b8
PP
288 @property
289 def source_component_classes(self):
ab1bca6c 290 return _PluginSourceComponentClasses(self)
81447b5b 291
811644b8
PP
292 @property
293 def filter_component_classes(self):
ab1bca6c 294 return _PluginFilterComponentClasses(self)
81447b5b 295
811644b8
PP
296 @property
297 def sink_component_classes(self):
ab1bca6c 298 return _PluginSinkComponentClasses(self)
This page took 0.054943 seconds and 4 git commands to generate.