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
)
60 def __init__(self
, major
, minor
, patch
, extra
):
85 if self
._extra
is not None:
88 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
91 class _Plugin(object._Object
, collections
.abc
.Sequence
):
94 name
= native_bt
.plugin_get_name(self
._ptr
)
95 utils
._handle
_ptr
(name
, "cannot get plugin object's name")
100 return native_bt
.plugin_get_author(self
._ptr
)
104 return native_bt
.plugin_get_license(self
._ptr
)
107 def description(self
):
108 return native_bt
.plugin_get_description(self
._ptr
)
112 return native_bt
.plugin_get_path(self
._ptr
)
116 status
, major
, minor
, patch
, extra
= native_bt
.plugin_get_version(self
._ptr
)
121 return _PluginVersion(major
, minor
, patch
, extra
)
123 def source_component_class(self
, name
):
124 utils
._check
_str
(name
)
125 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
127 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
)
132 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
134 def filter_component_class(self
, name
):
135 utils
._check
_str
(name
)
136 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
138 native_bt
.COMPONENT_CLASS_TYPE_FILTER
)
143 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
145 def sink_component_class(self
, name
):
146 utils
._check
_str
(name
)
147 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._ptr
,
149 native_bt
.COMPONENT_CLASS_TYPE_SINK
)
154 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
157 count
= native_bt
.plugin_get_component_class_count(self
._ptr
)
158 utils
._handle
_ret
(count
, "cannot get plugin object's component class count")
161 def __getitem__(self
, index
):
162 utils
._check
_uint
64(index
)
164 if index
>= len(self
):
167 cc_ptr
= native_bt
.plugin_get_component_class(self
._ptr
, index
)
168 utils
._handle
_ptr
(cc_ptr
, "cannot get plugin object's component class object")
169 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)