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 | ||
01f50d54 | 30 | def find_plugins(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 | |
01f50d54 | 55 | def find_plugin(name, fail_on_load_error=False): |
f6a5e476 | 56 | utils._check_str(name) |
01f50d54 | 57 | utils._check_bool(fail_on_load_error) |
fb25b9e3 | 58 | status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error)) |
81447b5b | 59 | |
fb25b9e3 | 60 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
f6a5e476 | 61 | return |
81447b5b | 62 | |
fb25b9e3 | 63 | utils._handle_func_status(status, 'failed to find plugin') |
01f50d54 | 64 | assert ptr is not None |
f6a5e476 | 65 | return _Plugin._create_from_ptr(ptr) |
81447b5b | 66 | |
81447b5b | 67 | |
c3044a97 | 68 | class _PluginSet(object._SharedObject, collections.abc.Sequence): |
a49e2cc3 PP |
69 | _put_ref = staticmethod(native_bt.plugin_set_put_ref) |
70 | _get_ref = staticmethod(native_bt.plugin_set_get_ref) | |
bb93d641 | 71 | |
f6a5e476 PP |
72 | def __len__(self): |
73 | count = native_bt.plugin_set_get_plugin_count(self._ptr) | |
61d96b89 | 74 | assert count >= 0 |
f6a5e476 | 75 | return count |
81447b5b | 76 | |
f6a5e476 PP |
77 | def __getitem__(self, index): |
78 | utils._check_uint64(index) | |
7eb48359 | 79 | |
f6a5e476 PP |
80 | if index >= len(self): |
81 | raise IndexError | |
7eb48359 | 82 | |
bb93d641 SM |
83 | plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index) |
84 | assert plugin_ptr is not None | |
85 | return _Plugin._create_from_ptr_and_get_ref(plugin_ptr) | |
7eb48359 PP |
86 | |
87 | ||
81447b5b PP |
88 | class _PluginVersion: |
89 | def __init__(self, major, minor, patch, extra): | |
90 | self._major = major | |
91 | self._minor = minor | |
92 | self._patch = patch | |
93 | self._extra = extra | |
94 | ||
95 | @property | |
96 | def major(self): | |
97 | return self._major | |
98 | ||
99 | @property | |
100 | def minor(self): | |
101 | return self._minor | |
102 | ||
103 | @property | |
104 | def patch(self): | |
105 | return self._patch | |
106 | ||
107 | @property | |
108 | def extra(self): | |
109 | return self._extra | |
110 | ||
111 | def __str__(self): | |
112 | extra = '' | |
113 | ||
114 | if self._extra is not None: | |
115 | extra = self._extra | |
116 | ||
117 | return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra) | |
118 | ||
119 | ||
f6a5e476 PP |
120 | class _PluginComponentClassesIterator(collections.abc.Iterator): |
121 | def __init__(self, plugin_comp_cls): | |
122 | self._plugin_comp_cls = plugin_comp_cls | |
123 | self._at = 0 | |
124 | ||
125 | def __next__(self): | |
126 | plugin_ptr = self._plugin_comp_cls._plugin._ptr | |
bb93d641 | 127 | total = self._plugin_comp_cls._component_class_count(plugin_ptr) |
f6a5e476 | 128 | |
bb93d641 SM |
129 | if self._at == total: |
130 | raise StopIteration | |
f6a5e476 | 131 | |
61d96b89 FD |
132 | comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index( |
133 | plugin_ptr, self._at | |
134 | ) | |
bb93d641 SM |
135 | assert comp_cls_ptr is not None |
136 | self._at += 1 | |
f6a5e476 | 137 | |
bb93d641 | 138 | comp_cls_type = self._plugin_comp_cls._comp_cls_type |
61d96b89 FD |
139 | comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[ |
140 | comp_cls_type | |
141 | ] | |
deec48a6 | 142 | comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr) |
f6a5e476 | 143 | name = native_bt.component_class_get_name(comp_cls_ptr) |
bb93d641 | 144 | assert name is not None |
f6a5e476 PP |
145 | return name |
146 | ||
147 | ||
148 | class _PluginComponentClasses(collections.abc.Mapping): | |
bb93d641 | 149 | def __init__(self, plugin): |
f6a5e476 | 150 | self._plugin = plugin |
f6a5e476 PP |
151 | |
152 | def __getitem__(self, key): | |
153 | utils._check_str(key) | |
bb93d641 | 154 | cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key) |
f6a5e476 PP |
155 | |
156 | if cc_ptr is None: | |
157 | raise KeyError(key) | |
158 | ||
61d96b89 FD |
159 | return bt2.component._create_component_class_from_ptr_and_get_ref( |
160 | cc_ptr, self._comp_cls_type | |
161 | ) | |
f6a5e476 PP |
162 | |
163 | def __len__(self): | |
bb93d641 SM |
164 | return self._component_class_count(self._plugin._ptr) |
165 | ||
166 | def __iter__(self): | |
167 | return _PluginComponentClassesIterator(self) | |
f6a5e476 | 168 | |
f6a5e476 | 169 | |
bb93d641 | 170 | class _PluginSourceComponentClasses(_PluginComponentClasses): |
61d96b89 FD |
171 | _component_class_count = staticmethod( |
172 | native_bt.plugin_get_source_component_class_count | |
173 | ) | |
174 | _borrow_component_class_by_name = staticmethod( | |
175 | native_bt.plugin_borrow_source_component_class_by_name_const | |
176 | ) | |
177 | _borrow_component_class_by_index = staticmethod( | |
178 | native_bt.plugin_borrow_source_component_class_by_index_const | |
179 | ) | |
bb93d641 | 180 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
f6a5e476 | 181 | |
f6a5e476 | 182 | |
bb93d641 | 183 | class _PluginFilterComponentClasses(_PluginComponentClasses): |
61d96b89 FD |
184 | _component_class_count = staticmethod( |
185 | native_bt.plugin_get_filter_component_class_count | |
186 | ) | |
187 | _borrow_component_class_by_name = staticmethod( | |
188 | native_bt.plugin_borrow_filter_component_class_by_name_const | |
189 | ) | |
190 | _borrow_component_class_by_index = staticmethod( | |
191 | native_bt.plugin_borrow_filter_component_class_by_index_const | |
192 | ) | |
bb93d641 | 193 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
f6a5e476 | 194 | |
bb93d641 SM |
195 | |
196 | class _PluginSinkComponentClasses(_PluginComponentClasses): | |
61d96b89 FD |
197 | _component_class_count = staticmethod( |
198 | native_bt.plugin_get_sink_component_class_count | |
199 | ) | |
200 | _borrow_component_class_by_name = staticmethod( | |
201 | native_bt.plugin_borrow_sink_component_class_by_name_const | |
202 | ) | |
203 | _borrow_component_class_by_index = staticmethod( | |
204 | native_bt.plugin_borrow_sink_component_class_by_index_const | |
205 | ) | |
bb93d641 | 206 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
f6a5e476 PP |
207 | |
208 | ||
c3044a97 | 209 | class _Plugin(object._SharedObject): |
a49e2cc3 PP |
210 | _put_ref = staticmethod(native_bt.plugin_put_ref) |
211 | _get_ref = staticmethod(native_bt.plugin_get_ref) | |
bb93d641 | 212 | |
81447b5b PP |
213 | @property |
214 | def name(self): | |
215 | name = native_bt.plugin_get_name(self._ptr) | |
61d96b89 | 216 | assert name is not None |
81447b5b PP |
217 | return name |
218 | ||
219 | @property | |
220 | def author(self): | |
221 | return native_bt.plugin_get_author(self._ptr) | |
222 | ||
223 | @property | |
224 | def license(self): | |
225 | return native_bt.plugin_get_license(self._ptr) | |
226 | ||
227 | @property | |
228 | def description(self): | |
229 | return native_bt.plugin_get_description(self._ptr) | |
230 | ||
231 | @property | |
232 | def path(self): | |
233 | return native_bt.plugin_get_path(self._ptr) | |
234 | ||
235 | @property | |
236 | def version(self): | |
fb25b9e3 | 237 | status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr) |
81447b5b | 238 | |
bb93d641 | 239 | if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE: |
81447b5b PP |
240 | return |
241 | ||
242 | return _PluginVersion(major, minor, patch, extra) | |
243 | ||
f6a5e476 PP |
244 | @property |
245 | def source_component_classes(self): | |
bb93d641 | 246 | return _PluginSourceComponentClasses(self) |
81447b5b | 247 | |
f6a5e476 PP |
248 | @property |
249 | def filter_component_classes(self): | |
bb93d641 | 250 | return _PluginFilterComponentClasses(self) |
81447b5b | 251 | |
f6a5e476 PP |
252 | @property |
253 | def sink_component_classes(self): | |
bb93d641 | 254 | return _PluginSinkComponentClasses(self) |