1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
9 from bt2
import component
as bt2_component
13 def find_plugins_in_path(path
, recurse
=True, fail_on_load_error
=False):
14 bt2_utils
._check
_str
(path
)
15 bt2_utils
._check
_bool
(recurse
)
16 bt2_utils
._check
_bool
(fail_on_load_error
)
19 if os
.path
.isfile(path
):
20 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_file(
21 path
, fail_on_load_error
23 elif os
.path
.isdir(path
):
24 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_dir(
25 path
, int(recurse
), int(fail_on_load_error
)
28 raise ValueError("invalid path: '{}'".format(path
))
30 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
33 bt2_utils
._handle
_func
_status
(status
, "failed to find plugins")
34 assert plugin_set_ptr
is not None
35 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
39 find_in_std_env_var
=True,
40 find_in_user_dir
=True,
43 fail_on_load_error
=False,
45 bt2_utils
._check
_bool
(find_in_std_env_var
)
46 bt2_utils
._check
_bool
(find_in_user_dir
)
47 bt2_utils
._check
_bool
(find_in_sys_dir
)
48 bt2_utils
._check
_bool
(find_in_static
)
49 bt2_utils
._check
_bool
(fail_on_load_error
)
52 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all(
53 int(find_in_std_env_var
),
54 int(find_in_user_dir
),
57 int(fail_on_load_error
),
60 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
63 bt2_utils
._handle
_func
_status
(status
, "failed to find plugins")
64 assert plugin_set_ptr
is not None
65 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
70 find_in_std_env_var
=True,
71 find_in_user_dir
=True,
74 fail_on_load_error
=False,
76 bt2_utils
._check
_str
(name
)
77 bt2_utils
._check
_bool
(fail_on_load_error
)
78 status
, ptr
= native_bt
.bt2_plugin_find(
80 int(find_in_std_env_var
),
81 int(find_in_user_dir
),
84 int(fail_on_load_error
),
87 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
90 bt2_utils
._handle
_func
_status
(status
, "failed to find plugin")
91 assert ptr
is not None
92 return _Plugin
._create
_from
_ptr
(ptr
)
95 class _PluginSet(bt2_object
._SharedObject
, collections
.abc
.Sequence
):
98 native_bt
.plugin_set_put_ref(ptr
)
102 native_bt
.plugin_set_get_ref(ptr
)
105 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
109 def __getitem__(self
, index
):
110 bt2_utils
._check
_uint
64(index
)
112 if index
>= len(self
):
115 plugin_ptr
= native_bt
.plugin_set_borrow_plugin_by_index_const(self
._ptr
, index
)
116 assert plugin_ptr
is not None
117 return _Plugin
._create
_from
_ptr
_and
_get
_ref
(plugin_ptr
)
120 class _PluginVersion
:
121 def __init__(self
, major
, minor
, patch
, extra
):
146 if self
._extra
is not None:
149 return "{}.{}.{}{}".format(self
._major
, self
._minor
, self
._patch
, extra
)
152 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
153 def __init__(self
, plugin_comp_cls
):
154 self
._plugin
_comp
_cls
= plugin_comp_cls
158 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
159 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
161 if self
._at
== total
:
164 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(
167 assert comp_cls_ptr
is not None
170 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
171 comp_cls_pycls
= bt2_component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[
174 comp_cls_ptr
= comp_cls_pycls
._bt
_as
_component
_class
_ptr
(comp_cls_ptr
)
175 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
176 assert name
is not None
180 class _PluginComponentClasses(collections
.abc
.Mapping
):
181 def __init__(self
, plugin
):
182 self
._plugin
= plugin
184 def __getitem__(self
, key
):
185 bt2_utils
._check
_str
(key
)
186 cc_ptr
= self
._borrow
_component
_class
_by
_name
(self
._plugin
._ptr
, key
)
191 return bt2_component
._create
_component
_class
_from
_const
_ptr
_and
_get
_ref
(
192 cc_ptr
, self
._comp
_cls
_type
196 return self
._component
_class
_count
(self
._plugin
._ptr
)
199 return _PluginComponentClassesIterator(self
)
202 class _PluginSourceComponentClasses(_PluginComponentClasses
):
203 _component_class_count
= staticmethod(
204 native_bt
.plugin_get_source_component_class_count
206 _borrow_component_class_by_name
= staticmethod(
207 native_bt
.plugin_borrow_source_component_class_by_name_const
209 _borrow_component_class_by_index
= staticmethod(
210 native_bt
.plugin_borrow_source_component_class_by_index_const
212 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
215 class _PluginFilterComponentClasses(_PluginComponentClasses
):
216 _component_class_count
= staticmethod(
217 native_bt
.plugin_get_filter_component_class_count
219 _borrow_component_class_by_name
= staticmethod(
220 native_bt
.plugin_borrow_filter_component_class_by_name_const
222 _borrow_component_class_by_index
= staticmethod(
223 native_bt
.plugin_borrow_filter_component_class_by_index_const
225 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
228 class _PluginSinkComponentClasses(_PluginComponentClasses
):
229 _component_class_count
= staticmethod(
230 native_bt
.plugin_get_sink_component_class_count
232 _borrow_component_class_by_name
= staticmethod(
233 native_bt
.plugin_borrow_sink_component_class_by_name_const
235 _borrow_component_class_by_index
= staticmethod(
236 native_bt
.plugin_borrow_sink_component_class_by_index_const
238 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
241 class _Plugin(bt2_object
._SharedObject
):
244 native_bt
.plugin_put_ref(ptr
)
248 native_bt
.plugin_get_ref(ptr
)
252 name
= native_bt
.plugin_get_name(self
._ptr
)
253 assert name
is not None
258 return native_bt
.plugin_get_author(self
._ptr
)
262 return native_bt
.plugin_get_license(self
._ptr
)
265 def description(self
):
266 return native_bt
.plugin_get_description(self
._ptr
)
270 return native_bt
.plugin_get_path(self
._ptr
)
274 status
, major
, minor
, patch
, extra
= native_bt
.bt2_plugin_get_version(self
._ptr
)
276 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
279 return _PluginVersion(major
, minor
, patch
, extra
)
282 def source_component_classes(self
):
283 return _PluginSourceComponentClasses(self
)
286 def filter_component_classes(self
):
287 return _PluginFilterComponentClasses(self
)
290 def sink_component_classes(self
):
291 return _PluginSinkComponentClasses(self
)