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