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):
31 utils
._check
_str
(path
)
32 utils
._check
_bool
(recurse
)
35 if os
.path
.isfile(path
):
36 plugin_set_ptr
= native_bt
.plugin_create_all_from_file(path
)
37 elif os
.path
.isdir(path
):
38 plugin_set_ptr
= native_bt
.plugin_create_all_from_dir(path
, int(recurse
))
40 if plugin_set_ptr
is None:
43 return _PluginSet
._create
_from
_ptr
(plugin_set_ptr
)
46 def find_plugin(name
):
47 utils
._check
_str
(name
)
48 ptr
= native_bt
.plugin_find(name
)
53 return _Plugin
._create
_from
_ptr
(ptr
)
56 class _PluginSet(object._Object
, collections
.abc
.Sequence
):
58 count
= native_bt
.plugin_set_get_plugin_count(self
._ptr
)
62 def __getitem__(self
, index
):
63 utils
._check
_uint
64(index
)
65 if index
>= len(self
):
68 plugin_ptr
= native_bt
.plugin_set_get_plugin(self
._ptr
, index
)
70 return _Plugin
._create
_from
_ptr
(plugin_ptr
)
74 def __init__(self
, major
, minor
, patch
, extra
):
99 if self
._extra
is not None:
102 return '{}.{}.{}{}'.format(self
._major
, self
._minor
, self
._patch
, extra
)
105 class _PluginComponentClassesIterator(collections
.abc
.Iterator
):
106 def __init__(self
, plugin_comp_cls
):
107 self
._plugin
_comp
_cls
= plugin_comp_cls
111 plugin_ptr
= self
._plugin
_comp
_cls
._plugin
._ptr
112 comp_cls_type
= self
._plugin
_comp
_cls
._comp
_cls
_type
113 total
= native_bt
.plugin_get_component_class_count(plugin_ptr
)
116 if self
._at
== total
:
119 comp_cls_ptr
= native_bt
.plugin_get_component_class_by_index(plugin_ptr
,
122 cc_type
= native_bt
.component_class_get_type(comp_cls_ptr
)
125 if cc_type
== comp_cls_type
:
128 native_bt
.put(comp_cls_ptr
)
130 name
= native_bt
.component_class_get_name(comp_cls_ptr
)
131 native_bt
.put(comp_cls_ptr
)
132 assert(name
is not None)
136 class _PluginComponentClasses(collections
.abc
.Mapping
):
137 def __init__(self
, plugin
, comp_cls_type
):
138 self
._plugin
= plugin
139 self
._comp
_cls
_type
= comp_cls_type
141 def __getitem__(self
, key
):
142 utils
._check
_str
(key
)
143 cc_ptr
= native_bt
.plugin_get_component_class_by_name_and_type(self
._plugin
._ptr
,
150 return bt2
.component
._create
_generic
_component
_class
_from
_ptr
(cc_ptr
)
154 total
= native_bt
.plugin_get_component_class_count(self
._plugin
._ptr
)
156 for at
in range(total
):
157 comp_cls_ptr
= native_bt
.plugin_get_component_class_by_index(self
._plugin
._ptr
,
160 cc_type
= native_bt
.component_class_get_type(comp_cls_ptr
)
162 if cc_type
== self
._comp
_cls
_type
:
165 native_bt
.put(comp_cls_ptr
)
170 return _PluginComponentClassesIterator(self
)
173 class _Plugin(object._Object
):
176 name
= native_bt
.plugin_get_name(self
._ptr
)
177 assert(name
is not None)
182 return native_bt
.plugin_get_author(self
._ptr
)
186 return native_bt
.plugin_get_license(self
._ptr
)
189 def description(self
):
190 return native_bt
.plugin_get_description(self
._ptr
)
194 return native_bt
.plugin_get_path(self
._ptr
)
198 status
, major
, minor
, patch
, extra
= native_bt
.plugin_get_version(self
._ptr
)
203 return _PluginVersion(major
, minor
, patch
, extra
)
206 def source_component_classes(self
):
207 return _PluginComponentClasses(self
, native_bt
.COMPONENT_CLASS_TYPE_SOURCE
)
210 def filter_component_classes(self
):
211 return _PluginComponentClasses(self
, native_bt
.COMPONENT_CLASS_TYPE_FILTER
)
214 def sink_component_classes(self
):
215 return _PluginComponentClasses(self
, native_bt
.COMPONENT_CLASS_TYPE_SINK
)