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