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
29 def _plugin_ptrs_to_plugins(plugin_ptrs
):
32 for plugin_ptr
in plugin_ptrs
:
33 plugin
= _Plugin
._create
_from
_ptr
(plugin_ptr
)
34 plugins
.append(plugin
)
39 def create_plugins_from_file(path
):
40 utils
._check
_str
(path
)
41 plugin_ptrs
= native_bt
.py3_plugin_create_all_from_file(path
)
43 if plugin_ptrs
is None:
44 raise bt2
.Error('cannot get plugin objects from file')
46 return _plugin_ptrs_to_plugins(plugin_ptrs
)
49 def create_plugins_from_dir(path
, recurse
=True):
50 utils
._check
_str
(path
)
51 plugin_ptrs
= native_bt
.py3_plugin_create_all_from_dir(path
, recurse
)
53 if plugin_ptrs
is None:
54 raise bt2
.Error('cannot get plugin objects from directory')
56 return _plugin_ptrs_to_plugins(plugin_ptrs
)
59 def create_plugin_from_name(name
):
60 utils
._check
_str
(name
)
61 plugin_ptr
= native_bt
.plugin_create_from_name(name
)
63 if plugin_ptr
is None:
64 raise bt2
.NoSuchPluginError(name
)
66 return _Plugin
._create
_from
_ptr
(plugin_ptr
)
70 def __init__(self
, major
, minor
, patch
, extra
):
95 if self
._extra
is not None:
98 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
101 class _Plugin(object._Object
, collections
.abc
.Sequence
):
104 name
= native_bt
.plugin_get_name(self
._ptr
)
105 utils
._handle
_ptr
(name
, "cannot get plugin object's name")
110 return native_bt
.plugin_get_author(self
._ptr
)
114 return native_bt
.plugin_get_license(self
._ptr
)
117 def description(self
):
118 return native_bt
.plugin_get_description(self
._ptr
)
122 return native_bt
.plugin_get_path(self
._ptr
)
126 status
, major
, minor
, patch
, extra
= native_bt
.plugin_get_version(self
._ptr
)
131 return _PluginVersion(major
, minor
, patch
, extra
)
133 def source_component_class(self
, name
):
134 utils
._check
_str
(name
)
135 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
137 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
)
142 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
144 def filter_component_class(self
, name
):
145 utils
._check
_str
(name
)
146 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
148 native_bt
.COMPONENT_CLASS_TYPE_FILTER
)
153 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
155 def sink_component_class(self
, name
):
156 utils
._check
_str
(name
)
157 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
159 native_bt
.COMPONENT_CLASS_TYPE_SINK
)
164 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
167 count
= native_bt
.plugin_get_component_class_count(self
._ptr
)
168 utils
._handle
_ret
(count
, "cannot get plugin object's component class count")
171 def __getitem__(self
, index
):
172 utils
._check
_uint
64(index
)
174 if index
>= len(self
):
177 cc_ptr
= native_bt
.plugin_get_component_class(self
._ptr
, index
)
178 utils
._handle
_ptr
(cc_ptr
, "cannot get plugin object's component class object")
179 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)