Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import collections.abc | |
c946c9de | 25 | from bt2 import component as bt2_component |
f6a5e476 | 26 | import os.path |
81447b5b PP |
27 | |
28 | ||
7d8f15e6 | 29 | def find_plugins_in_path(path, recurse=True, fail_on_load_error=False): |
f6a5e476 PP |
30 | utils._check_str(path) |
31 | utils._check_bool(recurse) | |
01f50d54 | 32 | utils._check_bool(fail_on_load_error) |
f6a5e476 | 33 | plugin_set_ptr = None |
81447b5b | 34 | |
f6a5e476 | 35 | if os.path.isfile(path): |
61d96b89 FD |
36 | status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file( |
37 | path, fail_on_load_error | |
38 | ) | |
f6a5e476 | 39 | elif os.path.isdir(path): |
61d96b89 FD |
40 | status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir( |
41 | path, int(recurse), int(fail_on_load_error) | |
42 | ) | |
01f50d54 | 43 | else: |
3b2be708 | 44 | raise ValueError("invalid path: '{}'".format(path)) |
01f50d54 | 45 | |
fb25b9e3 | 46 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
f6a5e476 | 47 | return |
81447b5b | 48 | |
fb25b9e3 | 49 | utils._handle_func_status(status, 'failed to find plugins') |
01f50d54 | 50 | assert plugin_set_ptr is not None |
f6a5e476 | 51 | return _PluginSet._create_from_ptr(plugin_set_ptr) |
81447b5b | 52 | |
81447b5b | 53 | |
7d8f15e6 PP |
54 | def 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 | ||
84 | def 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 | ): | |
f6a5e476 | 92 | utils._check_str(name) |
01f50d54 | 93 | utils._check_bool(fail_on_load_error) |
7d8f15e6 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 | |
fb25b9e3 | 103 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
f6a5e476 | 104 | return |
81447b5b | 105 | |
fb25b9e3 | 106 | utils._handle_func_status(status, 'failed to find plugin') |
01f50d54 | 107 | assert ptr is not None |
f6a5e476 | 108 | return _Plugin._create_from_ptr(ptr) |
81447b5b | 109 | |
81447b5b | 110 | |
c3044a97 | 111 | class _PluginSet(object._SharedObject, collections.abc.Sequence): |
a49e2cc3 PP |
112 | _put_ref = staticmethod(native_bt.plugin_set_put_ref) |
113 | _get_ref = staticmethod(native_bt.plugin_set_get_ref) | |
bb93d641 | 114 | |
f6a5e476 PP |
115 | def __len__(self): |
116 | count = native_bt.plugin_set_get_plugin_count(self._ptr) | |
61d96b89 | 117 | assert count >= 0 |
f6a5e476 | 118 | return count |
81447b5b | 119 | |
f6a5e476 PP |
120 | def __getitem__(self, index): |
121 | utils._check_uint64(index) | |
7eb48359 | 122 | |
f6a5e476 PP |
123 | if index >= len(self): |
124 | raise IndexError | |
7eb48359 | 125 | |
bb93d641 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 |
131 | class _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 | ||
f6a5e476 PP |
163 | class _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 | |
bb93d641 | 170 | total = self._plugin_comp_cls._component_class_count(plugin_ptr) |
f6a5e476 | 171 | |
bb93d641 SM |
172 | if self._at == total: |
173 | raise StopIteration | |
f6a5e476 | 174 | |
61d96b89 FD |
175 | comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index( |
176 | plugin_ptr, self._at | |
177 | ) | |
bb93d641 SM |
178 | assert comp_cls_ptr is not None |
179 | self._at += 1 | |
f6a5e476 | 180 | |
bb93d641 | 181 | comp_cls_type = self._plugin_comp_cls._comp_cls_type |
c946c9de | 182 | comp_cls_pycls = bt2_component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[ |
61d96b89 FD |
183 | comp_cls_type |
184 | ] | |
deec48a6 | 185 | comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr) |
f6a5e476 | 186 | name = native_bt.component_class_get_name(comp_cls_ptr) |
bb93d641 | 187 | assert name is not None |
f6a5e476 PP |
188 | return name |
189 | ||
190 | ||
191 | class _PluginComponentClasses(collections.abc.Mapping): | |
bb93d641 | 192 | def __init__(self, plugin): |
f6a5e476 | 193 | self._plugin = plugin |
f6a5e476 PP |
194 | |
195 | def __getitem__(self, key): | |
196 | utils._check_str(key) | |
bb93d641 | 197 | cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key) |
f6a5e476 PP |
198 | |
199 | if cc_ptr is None: | |
200 | raise KeyError(key) | |
201 | ||
c946c9de | 202 | return bt2_component._create_component_class_from_ptr_and_get_ref( |
61d96b89 FD |
203 | cc_ptr, self._comp_cls_type |
204 | ) | |
f6a5e476 PP |
205 | |
206 | def __len__(self): | |
bb93d641 SM |
207 | return self._component_class_count(self._plugin._ptr) |
208 | ||
209 | def __iter__(self): | |
210 | return _PluginComponentClassesIterator(self) | |
f6a5e476 | 211 | |
f6a5e476 | 212 | |
bb93d641 | 213 | class _PluginSourceComponentClasses(_PluginComponentClasses): |
61d96b89 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 | ) | |
bb93d641 | 223 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
f6a5e476 | 224 | |
f6a5e476 | 225 | |
bb93d641 | 226 | class _PluginFilterComponentClasses(_PluginComponentClasses): |
61d96b89 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 | ) | |
bb93d641 | 236 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
f6a5e476 | 237 | |
bb93d641 SM |
238 | |
239 | class _PluginSinkComponentClasses(_PluginComponentClasses): | |
61d96b89 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 | ) | |
bb93d641 | 249 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
f6a5e476 PP |
250 | |
251 | ||
c3044a97 | 252 | class _Plugin(object._SharedObject): |
a49e2cc3 PP |
253 | _put_ref = staticmethod(native_bt.plugin_put_ref) |
254 | _get_ref = staticmethod(native_bt.plugin_get_ref) | |
bb93d641 | 255 | |
81447b5b PP |
256 | @property |
257 | def name(self): | |
258 | name = native_bt.plugin_get_name(self._ptr) | |
61d96b89 | 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): | |
fb25b9e3 | 280 | status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr) |
81447b5b | 281 | |
bb93d641 | 282 | if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE: |
81447b5b PP |
283 | return |
284 | ||
285 | return _PluginVersion(major, minor, patch, extra) | |
286 | ||
f6a5e476 PP |
287 | @property |
288 | def source_component_classes(self): | |
bb93d641 | 289 | return _PluginSourceComponentClasses(self) |
81447b5b | 290 | |
f6a5e476 PP |
291 | @property |
292 | def filter_component_classes(self): | |
bb93d641 | 293 | return _PluginFilterComponentClasses(self) |
81447b5b | 294 | |
f6a5e476 PP |
295 | @property |
296 | def sink_component_classes(self): | |
bb93d641 | 297 | return _PluginSinkComponentClasses(self) |