1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
25 from bt2
import component
as bt2_component
30 def find_plugins_in_path(path
, recurse
=True, fail_on_load_error
=False):
31 utils
._check
_str
(path
)
32 utils
._check
_bool
(recurse
)
33 utils
._check
_bool
(fail_on_load_error
)
36 if os
.path
.isfile(path
):
37 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_file(
38 path
, fail_on_load_error
40 elif os
.path
.isdir(path
):
41 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_dir(
42 path
, int(recurse
), int(fail_on_load_error
)
45 raise ValueError("invalid path: '{}'".format(path
))
47 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
50 utils
._handle
_func
_status
(status
, 'failed to find plugins')
51 assert plugin_set_ptr
is not None
52 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
56 find_in_std_env_var
=True,
57 find_in_user_dir
=True,
60 fail_on_load_error
=False,
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
)
69 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all(
70 int(find_in_std_env_var
),
71 int(find_in_user_dir
),
74 int(fail_on_load_error
),
77 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
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
)
87 find_in_std_env_var
=True,
88 find_in_user_dir
=True,
91 fail_on_load_error
=False,
93 utils
._check
_str
(name
)
94 utils
._check
_bool
(fail_on_load_error
)
95 status
, ptr
= native_bt
.bt2_plugin_find(
97 int(find_in_std_env_var
),
98 int(find_in_user_dir
),
101 int(fail_on_load_error
),
104 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
107 utils
._handle
_func
_status
(status
, 'failed to find plugin')
108 assert ptr
is not None
109 return _Plugin
._create
_from
_ptr
(ptr
)
112 class _PluginSet(object._SharedObject
, collections
.abc
.Sequence
):
113 _put_ref
= staticmethod(native_bt
.plugin_set_put_ref
)
114 _get_ref
= staticmethod(native_bt
.plugin_set_get_ref
)
117 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
121 def __getitem__(self
, index
):
122 utils
._check
_uint
64(index
)
124 if index
>= len(self
):
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
)
132 class _PluginVersion
:
133 def __init__(self
, major
, minor
, patch
, extra
):
158 if self
._extra
is not None:
161 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
164 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
165 def __init__(self
, plugin_comp_cls
):
166 self
._plugin
_comp
_cls
= plugin_comp_cls
170 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
171 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
173 if self
._at
== total
:
176 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(
179 assert comp_cls_ptr
is not None
182 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
183 comp_cls_pycls
= bt2_component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[
186 comp_cls_ptr
= comp_cls_pycls
._bt
_as
_component
_class
_ptr
(comp_cls_ptr
)
187 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
188 assert name
is not None
192 class _PluginComponentClasses(collections
.abc
.Mapping
):
193 def __init__(self
, plugin
):
194 self
._plugin
= plugin
196 def __getitem__(self
, key
):
197 utils
._check
_str
(key
)
198 cc_ptr
= self
._borrow
_component
_class
_by
_name
(self
._plugin
._ptr
, key
)
203 return bt2_component
._create
_component
_class
_from
_ptr
_and
_get
_ref
(
204 cc_ptr
, self
._comp
_cls
_type
208 return self
._component
_class
_count
(self
._plugin
._ptr
)
211 return _PluginComponentClassesIterator(self
)
214 class _PluginSourceComponentClasses(_PluginComponentClasses
):
215 _component_class_count
= staticmethod(
216 native_bt
.plugin_get_source_component_class_count
218 _borrow_component_class_by_name
= staticmethod(
219 native_bt
.plugin_borrow_source_component_class_by_name_const
221 _borrow_component_class_by_index
= staticmethod(
222 native_bt
.plugin_borrow_source_component_class_by_index_const
224 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
227 class _PluginFilterComponentClasses(_PluginComponentClasses
):
228 _component_class_count
= staticmethod(
229 native_bt
.plugin_get_filter_component_class_count
231 _borrow_component_class_by_name
= staticmethod(
232 native_bt
.plugin_borrow_filter_component_class_by_name_const
234 _borrow_component_class_by_index
= staticmethod(
235 native_bt
.plugin_borrow_filter_component_class_by_index_const
237 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
240 class _PluginSinkComponentClasses(_PluginComponentClasses
):
241 _component_class_count
= staticmethod(
242 native_bt
.plugin_get_sink_component_class_count
244 _borrow_component_class_by_name
= staticmethod(
245 native_bt
.plugin_borrow_sink_component_class_by_name_const
247 _borrow_component_class_by_index
= staticmethod(
248 native_bt
.plugin_borrow_sink_component_class_by_index_const
250 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
253 class _Plugin(object._SharedObject
):
254 _put_ref
= staticmethod(native_bt
.plugin_put_ref
)
255 _get_ref
= staticmethod(native_bt
.plugin_get_ref
)
259 name
= native_bt
.plugin_get_name(self
._ptr
)
260 assert name
is not None
265 return native_bt
.plugin_get_author(self
._ptr
)
269 return native_bt
.plugin_get_license(self
._ptr
)
272 def description(self
):
273 return native_bt
.plugin_get_description(self
._ptr
)
277 return native_bt
.plugin_get_path(self
._ptr
)
281 status
, major
, minor
, patch
, extra
= native_bt
.bt2_plugin_get_version(self
._ptr
)
283 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
286 return _PluginVersion(major
, minor
, patch
, extra
)
289 def source_component_classes(self
):
290 return _PluginSourceComponentClasses(self
)
293 def filter_component_classes(self
):
294 return _PluginFilterComponentClasses(self
)
297 def sink_component_classes(self
):
298 return _PluginSinkComponentClasses(self
)