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 PP |
30 | def _handle_status(status, gen_error_msg): |
31 | if status == native_bt.PLUGIN_STATUS_LOADING_ERROR: | |
32 | raise bt2.PluginLoadingError | |
33 | elif status < 0: | |
34 | raise bt2.Error(gen_error_msg) | |
35 | ||
36 | ||
37 | def find_plugins(path, recurse=True, fail_on_load_error=False): | |
f6a5e476 PP |
38 | utils._check_str(path) |
39 | utils._check_bool(recurse) | |
01f50d54 | 40 | utils._check_bool(fail_on_load_error) |
f6a5e476 | 41 | plugin_set_ptr = None |
81447b5b | 42 | |
f6a5e476 | 43 | if os.path.isfile(path): |
01f50d54 | 44 | status, plugin_set_ptr = native_bt.plugin_find_all_from_file_wrapper(path, fail_on_load_error) |
f6a5e476 | 45 | elif os.path.isdir(path): |
01f50d54 PP |
46 | status, plugin_set_ptr = native_bt.plugin_find_all_from_dir_wrapper(path, int(recurse), int(fail_on_load_error)) |
47 | else: | |
48 | raise bt2.Error("invalid path: '{}'".format(path)) | |
49 | ||
50 | _handle_status(status, 'failed to find plugins') | |
81447b5b | 51 | |
01f50d54 | 52 | if status == native_bt.PLUGIN_STATUS_NOT_FOUND: |
f6a5e476 | 53 | return |
81447b5b | 54 | |
01f50d54 | 55 | assert plugin_set_ptr is not None |
f6a5e476 | 56 | return _PluginSet._create_from_ptr(plugin_set_ptr) |
81447b5b | 57 | |
81447b5b | 58 | |
01f50d54 | 59 | def find_plugin(name, fail_on_load_error=False): |
f6a5e476 | 60 | utils._check_str(name) |
01f50d54 PP |
61 | utils._check_bool(fail_on_load_error) |
62 | status, ptr = native_bt.plugin_find_wrapper(name, int(fail_on_load_error)) | |
63 | _handle_status(status, 'failed to find plugin') | |
81447b5b | 64 | |
01f50d54 | 65 | if status == native_bt.PLUGIN_STATUS_NOT_FOUND: |
f6a5e476 | 66 | return |
81447b5b | 67 | |
01f50d54 | 68 | assert ptr is not None |
f6a5e476 | 69 | return _Plugin._create_from_ptr(ptr) |
81447b5b | 70 | |
81447b5b | 71 | |
c3044a97 | 72 | class _PluginSet(object._SharedObject, collections.abc.Sequence): |
a49e2cc3 PP |
73 | _put_ref = staticmethod(native_bt.plugin_set_put_ref) |
74 | _get_ref = staticmethod(native_bt.plugin_set_get_ref) | |
bb93d641 | 75 | |
f6a5e476 PP |
76 | def __len__(self): |
77 | count = native_bt.plugin_set_get_plugin_count(self._ptr) | |
78 | assert(count >= 0) | |
79 | return count | |
81447b5b | 80 | |
f6a5e476 PP |
81 | def __getitem__(self, index): |
82 | utils._check_uint64(index) | |
7eb48359 | 83 | |
f6a5e476 PP |
84 | if index >= len(self): |
85 | raise IndexError | |
7eb48359 | 86 | |
bb93d641 SM |
87 | plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index) |
88 | assert plugin_ptr is not None | |
89 | return _Plugin._create_from_ptr_and_get_ref(plugin_ptr) | |
7eb48359 PP |
90 | |
91 | ||
81447b5b PP |
92 | class _PluginVersion: |
93 | def __init__(self, major, minor, patch, extra): | |
94 | self._major = major | |
95 | self._minor = minor | |
96 | self._patch = patch | |
97 | self._extra = extra | |
98 | ||
99 | @property | |
100 | def major(self): | |
101 | return self._major | |
102 | ||
103 | @property | |
104 | def minor(self): | |
105 | return self._minor | |
106 | ||
107 | @property | |
108 | def patch(self): | |
109 | return self._patch | |
110 | ||
111 | @property | |
112 | def extra(self): | |
113 | return self._extra | |
114 | ||
115 | def __str__(self): | |
116 | extra = '' | |
117 | ||
118 | if self._extra is not None: | |
119 | extra = self._extra | |
120 | ||
121 | return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra) | |
122 | ||
123 | ||
f6a5e476 PP |
124 | class _PluginComponentClassesIterator(collections.abc.Iterator): |
125 | def __init__(self, plugin_comp_cls): | |
126 | self._plugin_comp_cls = plugin_comp_cls | |
127 | self._at = 0 | |
128 | ||
129 | def __next__(self): | |
130 | plugin_ptr = self._plugin_comp_cls._plugin._ptr | |
bb93d641 | 131 | total = self._plugin_comp_cls._component_class_count(plugin_ptr) |
f6a5e476 | 132 | |
bb93d641 SM |
133 | if self._at == total: |
134 | raise StopIteration | |
f6a5e476 | 135 | |
bb93d641 SM |
136 | comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(plugin_ptr, self._at) |
137 | assert comp_cls_ptr is not None | |
138 | self._at += 1 | |
f6a5e476 | 139 | |
bb93d641 SM |
140 | comp_cls_type = self._plugin_comp_cls._comp_cls_type |
141 | comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type] | |
142 | comp_cls_ptr = comp_cls_pycls._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 | ||
bb93d641 | 159 | return bt2.component._create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) |
f6a5e476 PP |
160 | |
161 | def __len__(self): | |
bb93d641 SM |
162 | return self._component_class_count(self._plugin._ptr) |
163 | ||
164 | def __iter__(self): | |
165 | return _PluginComponentClassesIterator(self) | |
f6a5e476 | 166 | |
f6a5e476 | 167 | |
bb93d641 | 168 | class _PluginSourceComponentClasses(_PluginComponentClasses): |
a49e2cc3 PP |
169 | _component_class_count = staticmethod(native_bt.plugin_get_source_component_class_count) |
170 | _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_source_component_class_by_name_const) | |
171 | _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_source_component_class_by_index_const) | |
bb93d641 | 172 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
f6a5e476 | 173 | |
f6a5e476 | 174 | |
bb93d641 | 175 | class _PluginFilterComponentClasses(_PluginComponentClasses): |
a49e2cc3 PP |
176 | _component_class_count = staticmethod(native_bt.plugin_get_filter_component_class_count) |
177 | _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_filter_component_class_by_name_const) | |
178 | _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_filter_component_class_by_index_const) | |
bb93d641 | 179 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
f6a5e476 | 180 | |
bb93d641 SM |
181 | |
182 | class _PluginSinkComponentClasses(_PluginComponentClasses): | |
a49e2cc3 PP |
183 | _component_class_count = staticmethod(native_bt.plugin_get_sink_component_class_count) |
184 | _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_sink_component_class_by_name_const) | |
185 | _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_sink_component_class_by_index_const) | |
bb93d641 | 186 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
f6a5e476 PP |
187 | |
188 | ||
c3044a97 | 189 | class _Plugin(object._SharedObject): |
a49e2cc3 PP |
190 | _put_ref = staticmethod(native_bt.plugin_put_ref) |
191 | _get_ref = staticmethod(native_bt.plugin_get_ref) | |
bb93d641 | 192 | |
81447b5b PP |
193 | @property |
194 | def name(self): | |
195 | name = native_bt.plugin_get_name(self._ptr) | |
f6a5e476 | 196 | assert(name is not None) |
81447b5b PP |
197 | return name |
198 | ||
199 | @property | |
200 | def author(self): | |
201 | return native_bt.plugin_get_author(self._ptr) | |
202 | ||
203 | @property | |
204 | def license(self): | |
205 | return native_bt.plugin_get_license(self._ptr) | |
206 | ||
207 | @property | |
208 | def description(self): | |
209 | return native_bt.plugin_get_description(self._ptr) | |
210 | ||
211 | @property | |
212 | def path(self): | |
213 | return native_bt.plugin_get_path(self._ptr) | |
214 | ||
215 | @property | |
216 | def version(self): | |
ec656f03 | 217 | status, major, minor, patch, extra = native_bt.plugin_get_version_wrapper(self._ptr) |
81447b5b | 218 | |
bb93d641 | 219 | if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE: |
81447b5b PP |
220 | return |
221 | ||
222 | return _PluginVersion(major, minor, patch, extra) | |
223 | ||
f6a5e476 PP |
224 | @property |
225 | def source_component_classes(self): | |
bb93d641 | 226 | return _PluginSourceComponentClasses(self) |
81447b5b | 227 | |
f6a5e476 PP |
228 | @property |
229 | def filter_component_classes(self): | |
bb93d641 | 230 | return _PluginFilterComponentClasses(self) |
81447b5b | 231 | |
f6a5e476 PP |
232 | @property |
233 | def sink_component_classes(self): | |
bb93d641 | 234 | return _PluginSinkComponentClasses(self) |