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