1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
7 from bt2
import component
as bt2_component
11 def find_plugins_in_path(path
, recurse
=True, fail_on_load_error
=False):
12 utils
._check
_str
(path
)
13 utils
._check
_bool
(recurse
)
14 utils
._check
_bool
(fail_on_load_error
)
17 if os
.path
.isfile(path
):
18 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_file(
19 path
, fail_on_load_error
21 elif os
.path
.isdir(path
):
22 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_dir(
23 path
, int(recurse
), int(fail_on_load_error
)
26 raise ValueError("invalid path: '{}'".format(path
))
28 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
31 utils
._handle
_func
_status
(status
, "failed to find plugins")
32 assert plugin_set_ptr
is not None
33 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
37 find_in_std_env_var
=True,
38 find_in_user_dir
=True,
41 fail_on_load_error
=False,
43 utils
._check
_bool
(find_in_std_env_var
)
44 utils
._check
_bool
(find_in_user_dir
)
45 utils
._check
_bool
(find_in_sys_dir
)
46 utils
._check
_bool
(find_in_static
)
47 utils
._check
_bool
(fail_on_load_error
)
50 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all(
51 int(find_in_std_env_var
),
52 int(find_in_user_dir
),
55 int(fail_on_load_error
),
58 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
61 utils
._handle
_func
_status
(status
, "failed to find plugins")
62 assert plugin_set_ptr
is not None
63 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
68 find_in_std_env_var
=True,
69 find_in_user_dir
=True,
72 fail_on_load_error
=False,
74 utils
._check
_str
(name
)
75 utils
._check
_bool
(fail_on_load_error
)
76 status
, ptr
= native_bt
.bt2_plugin_find(
78 int(find_in_std_env_var
),
79 int(find_in_user_dir
),
82 int(fail_on_load_error
),
85 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
88 utils
._handle
_func
_status
(status
, "failed to find plugin")
89 assert ptr
is not None
90 return _Plugin
._create
_from
_ptr
(ptr
)
93 class _PluginSet(object._SharedObject
, collections
.abc
.Sequence
):
96 native_bt
.plugin_set_put_ref(ptr
)
100 native_bt
.plugin_set_get_ref(ptr
)
103 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
107 def __getitem__(self
, index
):
108 utils
._check
_uint
64(index
)
110 if index
>= len(self
):
113 plugin_ptr
= native_bt
.plugin_set_borrow_plugin_by_index_const(self
._ptr
, index
)
114 assert plugin_ptr
is not None
115 return _Plugin
._create
_from
_ptr
_and
_get
_ref
(plugin_ptr
)
118 class _PluginVersion
:
119 def __init__(self
, major
, minor
, patch
, extra
):
144 if self
._extra
is not None:
147 return "{}.{}.{}{}".format(self
._major
, self
._minor
, self
._patch
, extra
)
150 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
151 def __init__(self
, plugin_comp_cls
):
152 self
._plugin
_comp
_cls
= plugin_comp_cls
156 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
157 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
159 if self
._at
== total
:
162 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(
165 assert comp_cls_ptr
is not None
168 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
169 comp_cls_pycls
= bt2_component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[
172 comp_cls_ptr
= comp_cls_pycls
._bt
_as
_component
_class
_ptr
(comp_cls_ptr
)
173 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
174 assert name
is not None
178 class _PluginComponentClasses(collections
.abc
.Mapping
):
179 def __init__(self
, plugin
):
180 self
._plugin
= plugin
182 def __getitem__(self
, key
):
183 utils
._check
_str
(key
)
184 cc_ptr
= self
._borrow
_component
_class
_by
_name
(self
._plugin
._ptr
, key
)
189 return bt2_component
._create
_component
_class
_from
_const
_ptr
_and
_get
_ref
(
190 cc_ptr
, self
._comp
_cls
_type
194 return self
._component
_class
_count
(self
._plugin
._ptr
)
197 return _PluginComponentClassesIterator(self
)
200 class _PluginSourceComponentClasses(_PluginComponentClasses
):
201 _component_class_count
= staticmethod(
202 native_bt
.plugin_get_source_component_class_count
204 _borrow_component_class_by_name
= staticmethod(
205 native_bt
.plugin_borrow_source_component_class_by_name_const
207 _borrow_component_class_by_index
= staticmethod(
208 native_bt
.plugin_borrow_source_component_class_by_index_const
210 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
213 class _PluginFilterComponentClasses(_PluginComponentClasses
):
214 _component_class_count
= staticmethod(
215 native_bt
.plugin_get_filter_component_class_count
217 _borrow_component_class_by_name
= staticmethod(
218 native_bt
.plugin_borrow_filter_component_class_by_name_const
220 _borrow_component_class_by_index
= staticmethod(
221 native_bt
.plugin_borrow_filter_component_class_by_index_const
223 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
226 class _PluginSinkComponentClasses(_PluginComponentClasses
):
227 _component_class_count
= staticmethod(
228 native_bt
.plugin_get_sink_component_class_count
230 _borrow_component_class_by_name
= staticmethod(
231 native_bt
.plugin_borrow_sink_component_class_by_name_const
233 _borrow_component_class_by_index
= staticmethod(
234 native_bt
.plugin_borrow_sink_component_class_by_index_const
236 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
239 class _Plugin(object._SharedObject
):
242 native_bt
.plugin_put_ref(ptr
)
246 native_bt
.plugin_get_ref(ptr
)
250 name
= native_bt
.plugin_get_name(self
._ptr
)
251 assert name
is not None
256 return native_bt
.plugin_get_author(self
._ptr
)
260 return native_bt
.plugin_get_license(self
._ptr
)
263 def description(self
):
264 return native_bt
.plugin_get_description(self
._ptr
)
268 return native_bt
.plugin_get_path(self
._ptr
)
272 status
, major
, minor
, patch
, extra
= native_bt
.bt2_plugin_get_version(self
._ptr
)
274 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
277 return _PluginVersion(major
, minor
, patch
, extra
)
280 def source_component_classes(self
):
281 return _PluginSourceComponentClasses(self
)
284 def filter_component_classes(self
):
285 return _PluginFilterComponentClasses(self
)
288 def sink_component_classes(self
):
289 return _PluginSinkComponentClasses(self
)