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