1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
8 from bt2
import utils
as bt2_utils
9 from bt2
import object as bt2_object
10 from bt2
import component
as bt2_component
11 from bt2
import native_bt
14 def find_plugins_in_path(path
, recurse
=True, fail_on_load_error
=False):
15 bt2_utils
._check
_str
(path
)
16 bt2_utils
._check
_bool
(recurse
)
17 bt2_utils
._check
_bool
(fail_on_load_error
)
20 if os
.path
.isfile(path
):
21 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_file(
22 path
, fail_on_load_error
24 elif os
.path
.isdir(path
):
25 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all_from_dir(
26 path
, int(recurse
), int(fail_on_load_error
)
29 raise ValueError("invalid path: '{}'".format(path
))
31 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
34 bt2_utils
._handle
_func
_status
(status
, "failed to find plugins")
35 assert plugin_set_ptr
is not None
36 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
40 find_in_std_env_var
=True,
41 find_in_user_dir
=True,
44 fail_on_load_error
=False,
46 bt2_utils
._check
_bool
(find_in_std_env_var
)
47 bt2_utils
._check
_bool
(find_in_user_dir
)
48 bt2_utils
._check
_bool
(find_in_sys_dir
)
49 bt2_utils
._check
_bool
(find_in_static
)
50 bt2_utils
._check
_bool
(fail_on_load_error
)
53 status
, plugin_set_ptr
= native_bt
.bt2_plugin_find_all(
54 int(find_in_std_env_var
),
55 int(find_in_user_dir
),
58 int(fail_on_load_error
),
61 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
64 bt2_utils
._handle
_func
_status
(status
, "failed to find plugins")
65 assert plugin_set_ptr
is not None
66 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
71 find_in_std_env_var
=True,
72 find_in_user_dir
=True,
75 fail_on_load_error
=False,
77 bt2_utils
._check
_str
(name
)
78 bt2_utils
._check
_bool
(fail_on_load_error
)
79 status
, ptr
= native_bt
.bt2_plugin_find(
81 int(find_in_std_env_var
),
82 int(find_in_user_dir
),
85 int(fail_on_load_error
),
88 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
91 bt2_utils
._handle
_func
_status
(status
, "failed to find plugin")
92 assert ptr
is not None
93 return _Plugin
._create
_from
_ptr
(ptr
)
96 class _PluginSet(bt2_object
._SharedObject
, collections
.abc
.Sequence
):
99 native_bt
.plugin_set_put_ref(ptr
)
103 native_bt
.plugin_set_get_ref(ptr
)
106 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
110 def __getitem__(self
, index
):
111 bt2_utils
._check
_uint
64(index
)
113 if index
>= len(self
):
116 plugin_ptr
= native_bt
.plugin_set_borrow_plugin_by_index_const(self
._ptr
, index
)
117 assert plugin_ptr
is not None
118 return _Plugin
._create
_from
_ptr
_and
_get
_ref
(plugin_ptr
)
121 class _PluginVersion
:
122 def __init__(self
, major
, minor
, patch
, extra
):
147 if self
._extra
is not None:
150 return "{}.{}.{}{}".format(self
._major
, self
._minor
, self
._patch
, extra
)
153 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
154 def __init__(self
, plugin_comp_cls
):
155 self
._plugin
_comp
_cls
= plugin_comp_cls
159 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
160 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
162 if self
._at
== total
:
165 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(
168 assert comp_cls_ptr
is not None
171 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
172 comp_cls_pycls
= bt2_component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[
175 comp_cls_ptr
= comp_cls_pycls
._bt
_as
_component
_class
_ptr
(comp_cls_ptr
)
176 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
177 assert name
is not None
181 class _PluginComponentClasses(collections
.abc
.Mapping
):
182 def __init__(self
, plugin
):
183 self
._plugin
= plugin
185 def __getitem__(self
, key
):
186 bt2_utils
._check
_str
(key
)
187 cc_ptr
= self
._borrow
_component
_class
_by
_name
(self
._plugin
._ptr
, key
)
192 return bt2_component
._create
_component
_class
_from
_const
_ptr
_and
_get
_ref
(
193 cc_ptr
, self
._comp
_cls
_type
197 return self
._component
_class
_count
(self
._plugin
._ptr
)
200 return _PluginComponentClassesIterator(self
)
203 class _PluginSourceComponentClasses(_PluginComponentClasses
):
204 _component_class_count
= staticmethod(
205 native_bt
.plugin_get_source_component_class_count
207 _borrow_component_class_by_name
= staticmethod(
208 native_bt
.plugin_borrow_source_component_class_by_name_const
210 _borrow_component_class_by_index
= staticmethod(
211 native_bt
.plugin_borrow_source_component_class_by_index_const
213 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
216 class _PluginFilterComponentClasses(_PluginComponentClasses
):
217 _component_class_count
= staticmethod(
218 native_bt
.plugin_get_filter_component_class_count
220 _borrow_component_class_by_name
= staticmethod(
221 native_bt
.plugin_borrow_filter_component_class_by_name_const
223 _borrow_component_class_by_index
= staticmethod(
224 native_bt
.plugin_borrow_filter_component_class_by_index_const
226 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
229 class _PluginSinkComponentClasses(_PluginComponentClasses
):
230 _component_class_count
= staticmethod(
231 native_bt
.plugin_get_sink_component_class_count
233 _borrow_component_class_by_name
= staticmethod(
234 native_bt
.plugin_borrow_sink_component_class_by_name_const
236 _borrow_component_class_by_index
= staticmethod(
237 native_bt
.plugin_borrow_sink_component_class_by_index_const
239 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
242 class _Plugin(bt2_object
._SharedObject
):
245 native_bt
.plugin_put_ref(ptr
)
249 native_bt
.plugin_get_ref(ptr
)
253 name
= native_bt
.plugin_get_name(self
._ptr
)
254 assert name
is not None
259 return native_bt
.plugin_get_author(self
._ptr
)
263 return native_bt
.plugin_get_license(self
._ptr
)
266 def description(self
):
267 return native_bt
.plugin_get_description(self
._ptr
)
271 return native_bt
.plugin_get_path(self
._ptr
)
275 status
, major
, minor
, patch
, extra
= native_bt
.bt2_plugin_get_version(self
._ptr
)
277 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
280 return _PluginVersion(major
, minor
, patch
, extra
)
283 def source_component_classes(self
):
284 return _PluginSourceComponentClasses(self
)
287 def filter_component_classes(self
):
288 return _PluginFilterComponentClasses(self
)
291 def sink_component_classes(self
):
292 return _PluginSinkComponentClasses(self
)