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 _handle_status(status
, gen_error_msg
):
31 if status
== native_bt
.PLUGIN_STATUS_LOADING_ERROR
:
32 raise bt2
.PluginLoadingError
34 raise bt2
.Error(gen_error_msg
)
37 def find_plugins(path
, recurse
=True, fail_on_load_error
=False):
38 utils
._check
_str
(path
)
39 utils
._check
_bool
(recurse
)
40 utils
._check
_bool
(fail_on_load_error
)
43 if os
.path
.isfile(path
):
44 status
, plugin_set_ptr
= native_bt
.plugin_find_all_from_file_wrapper(path
, fail_on_load_error
)
45 elif os
.path
.isdir(path
):
46 status
, plugin_set_ptr
= native_bt
.plugin_find_all_from_dir_wrapper(path
, int(recurse
), int(fail_on_load_error
))
48 raise bt2
.Error("invalid path: '{}'".format(path
))
50 _handle_status(status
, 'failed to find plugins')
52 if status
== native_bt
.PLUGIN_STATUS_NOT_FOUND
:
55 assert plugin_set_ptr
is not None
56 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
59 def find_plugin(name
, fail_on_load_error
=False):
60 utils
._check
_str
(name
)
61 utils
._check
_bool
(fail_on_load_error
)
62 status
, ptr
= native_bt
.plugin_find_wrapper(name
, int(fail_on_load_error
))
63 _handle_status(status
, 'failed to find plugin')
65 if status
== native_bt
.PLUGIN_STATUS_NOT_FOUND
:
68 assert ptr
is not None
69 return _Plugin
._create
_from
_ptr
(ptr
)
72 class _PluginSet(object._SharedObject
, collections
.abc
.Sequence
):
73 _put_ref
= staticmethod(native_bt
.plugin_set_put_ref
)
74 _get_ref
= staticmethod(native_bt
.plugin_set_get_ref
)
77 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
81 def __getitem__(self
, index
):
82 utils
._check
_uint
64(index
)
84 if index
>= len(self
):
87 plugin_ptr
= native_bt
.plugin_set_borrow_plugin_by_index_const(self
._ptr
, index
)
88 assert plugin_ptr
is not None
89 return _Plugin
._create
_from
_ptr
_and
_get
_ref
(plugin_ptr
)
93 def __init__(self
, major
, minor
, patch
, extra
):
118 if self
._extra
is not None:
121 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
124 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
125 def __init__(self
, plugin_comp_cls
):
126 self
._plugin
_comp
_cls
= plugin_comp_cls
130 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
131 total
= self
._plugin
_comp
_cls
._component
_class
_count
(plugin_ptr
)
133 if self
._at
== total
:
136 comp_cls_ptr
= self
._plugin
_comp
_cls
._borrow
_component
_class
_by
_index
(plugin_ptr
, self
._at
)
137 assert comp_cls_ptr
is not None
140 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
141 comp_cls_pycls
= bt2
.component
._COMP
_CLS
_TYPE
_TO
_GENERIC
_COMP
_CLS
_PYCLS
[comp_cls_type
]
142 comp_cls_ptr
= comp_cls_pycls
._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
(cc_ptr
, self
._comp
_cls
_type
)
162 return self
._component
_class
_count
(self
._plugin
._ptr
)
165 return _PluginComponentClassesIterator(self
)
168 class _PluginSourceComponentClasses(_PluginComponentClasses
):
169 _component_class_count
= staticmethod(native_bt
.plugin_get_source_component_class_count
)
170 _borrow_component_class_by_name
= staticmethod(native_bt
.plugin_borrow_source_component_class_by_name_const
)
171 _borrow_component_class_by_index
= staticmethod(native_bt
.plugin_borrow_source_component_class_by_index_const
)
172 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
175 class _PluginFilterComponentClasses(_PluginComponentClasses
):
176 _component_class_count
= staticmethod(native_bt
.plugin_get_filter_component_class_count
)
177 _borrow_component_class_by_name
= staticmethod(native_bt
.plugin_borrow_filter_component_class_by_name_const
)
178 _borrow_component_class_by_index
= staticmethod(native_bt
.plugin_borrow_filter_component_class_by_index_const
)
179 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
182 class _PluginSinkComponentClasses(_PluginComponentClasses
):
183 _component_class_count
= staticmethod(native_bt
.plugin_get_sink_component_class_count
)
184 _borrow_component_class_by_name
= staticmethod(native_bt
.plugin_borrow_sink_component_class_by_name_const
)
185 _borrow_component_class_by_index
= staticmethod(native_bt
.plugin_borrow_sink_component_class_by_index_const
)
186 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
189 class _Plugin(object._SharedObject
):
190 _put_ref
= staticmethod(native_bt
.plugin_put_ref
)
191 _get_ref
= staticmethod(native_bt
.plugin_get_ref
)
195 name
= native_bt
.plugin_get_name(self
._ptr
)
196 assert(name
is not None)
201 return native_bt
.plugin_get_author(self
._ptr
)
205 return native_bt
.plugin_get_license(self
._ptr
)
208 def description(self
):
209 return native_bt
.plugin_get_description(self
._ptr
)
213 return native_bt
.plugin_get_path(self
._ptr
)
217 status
, major
, minor
, patch
, extra
= native_bt
.plugin_get_version_wrapper(self
._ptr
)
219 if status
== native_bt
.PROPERTY_AVAILABILITY_NOT_AVAILABLE
:
222 return _PluginVersion(major
, minor
, patch
, extra
)
225 def source_component_classes(self
):
226 return _PluginSourceComponentClasses(self
)
229 def filter_component_classes(self
):
230 return _PluginFilterComponentClasses(self
)
233 def sink_component_classes(self
):
234 return _PluginSinkComponentClasses(self
)