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
30 def find_plugins(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 bt2
.Error("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
)
55 def find_plugin(name
, fail_on_load_error
=False):
56 utils
._check
_str
(name
)
57 utils
._check
_bool
(fail_on_load_error
)
58 status
, ptr
= native_bt
.bt2_plugin_find(name
, int(fail_on_load_error
))
60 if status
== native_bt
.__BT
_FUNC
_STATUS
_NOT
_FOUND
:
63 utils
._handle
_func
_status
(status
, 'failed to find plugin')
64 assert ptr
is not None
65 return _Plugin
._create
_from
_ptr
(ptr
)
68 class _PluginSet(object._SharedObject
, collections
.abc
.Sequence
):
69 _put_ref
= staticmethod(native_bt
.plugin_set_put_ref
)
70 _get_ref
= staticmethod(native_bt
.plugin_set_get_ref
)
73 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
77 def __getitem__(self
, index
):
78 utils
._check
_uint
64(index
)
80 if index
>= len(self
):
83 plugin_ptr
= native_bt
.plugin_set_borrow_plugin_by_index_const(self
._ptr
, index
)
84 assert plugin_ptr
is not None
85 return _Plugin
._create
_from
_ptr
_and
_get
_ref
(plugin_ptr
)
89 def __init__(self
, major
, minor
, patch
, extra
):
114 if self
._extra
is not None:
117 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
120 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
121 def __init__(self
, plugin_comp_cls
):
122 self
._plugin
_comp
_cls
= plugin_comp_cls
126 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
127 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
129 if self
._at
== total
:
132 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(
135 assert comp_cls_ptr
is not None
138 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
139 comp_cls_pycls
= bt2
.component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[
142 comp_cls_ptr
= comp_cls_pycls
._bt
_as
_component
_class
_ptr
(comp_cls_ptr
)
143 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
144 assert name
is not None
148 class _PluginComponentClasses(collections
.abc
.Mapping
):
149 def __init__(self
, plugin
):
150 self
._plugin
= plugin
152 def __getitem__(self
, key
):
153 utils
._check
_str
(key
)
154 cc_ptr
= self
._borrow
_component
_class
_by
_name
(self
._plugin
._ptr
, key
)
159 return bt2
.component
._create
_component
_class
_from
_ptr
_and
_get
_ref
(
160 cc_ptr
, self
._comp
_cls
_type
164 return self
._component
_class
_count
(self
._plugin
._ptr
)
167 return _PluginComponentClassesIterator(self
)
170 class _PluginSourceComponentClasses(_PluginComponentClasses
):
171 _component_class_count
= staticmethod(
172 native_bt
.plugin_get_source_component_class_count
174 _borrow_component_class_by_name
= staticmethod(
175 native_bt
.plugin_borrow_source_component_class_by_name_const
177 _borrow_component_class_by_index
= staticmethod(
178 native_bt
.plugin_borrow_source_component_class_by_index_const
180 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
183 class _PluginFilterComponentClasses(_PluginComponentClasses
):
184 _component_class_count
= staticmethod(
185 native_bt
.plugin_get_filter_component_class_count
187 _borrow_component_class_by_name
= staticmethod(
188 native_bt
.plugin_borrow_filter_component_class_by_name_const
190 _borrow_component_class_by_index
= staticmethod(
191 native_bt
.plugin_borrow_filter_component_class_by_index_const
193 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
196 class _PluginSinkComponentClasses(_PluginComponentClasses
):
197 _component_class_count
= staticmethod(
198 native_bt
.plugin_get_sink_component_class_count
200 _borrow_component_class_by_name
= staticmethod(
201 native_bt
.plugin_borrow_sink_component_class_by_name_const
203 _borrow_component_class_by_index
= staticmethod(
204 native_bt
.plugin_borrow_sink_component_class_by_index_const
206 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
209 class _Plugin(object._SharedObject
):
210 _put_ref
= staticmethod(native_bt
.plugin_put_ref
)
211 _get_ref
= staticmethod(native_bt
.plugin_get_ref
)
215 name
= native_bt
.plugin_get_name(self
._ptr
)
216 assert name
is not None
221 return native_bt
.plugin_get_author(self
._ptr
)
225 return native_bt
.plugin_get_license(self
._ptr
)
228 def description(self
):
229 return native_bt
.plugin_get_description(self
._ptr
)
233 return native_bt
.plugin_get_path(self
._ptr
)
237 status
, major
, minor
, patch
, extra
= native_bt
.bt2_plugin_get_version(self
._ptr
)
239 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
242 return _PluginVersion(major
, minor
, patch
, extra
)
245 def source_component_classes(self
):
246 return _PluginSourceComponentClasses(self
)
249 def filter_component_classes(self
):
250 return _PluginFilterComponentClasses(self
)
253 def sink_component_classes(self
):
254 return _PluginSinkComponentClasses(self
)