lib: strictly type function return status enumerations
[babeltrace.git] / src / bindings / python / bt2 / bt2 / plugin.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4#
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:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
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
21# THE SOFTWARE.
22
23from bt2 import native_bt, object, utils
24import collections.abc
25import bt2.component
f6a5e476 26import os.path
81447b5b
PP
27import bt2
28
29
01f50d54 30def find_plugins(path, recurse=True, fail_on_load_error=False):
f6a5e476
PP
31 utils._check_str(path)
32 utils._check_bool(recurse)
01f50d54 33 utils._check_bool(fail_on_load_error)
f6a5e476 34 plugin_set_ptr = None
81447b5b 35
f6a5e476 36 if os.path.isfile(path):
fb25b9e3 37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(path, fail_on_load_error)
f6a5e476 38 elif os.path.isdir(path):
fb25b9e3 39 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(path, int(recurse), int(fail_on_load_error))
01f50d54
PP
40 else:
41 raise bt2.Error("invalid path: '{}'".format(path))
42
fb25b9e3 43 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
f6a5e476 44 return
81447b5b 45
fb25b9e3 46 utils._handle_func_status(status, 'failed to find plugins')
01f50d54 47 assert plugin_set_ptr is not None
f6a5e476 48 return _PluginSet._create_from_ptr(plugin_set_ptr)
81447b5b 49
81447b5b 50
01f50d54 51def find_plugin(name, fail_on_load_error=False):
f6a5e476 52 utils._check_str(name)
01f50d54 53 utils._check_bool(fail_on_load_error)
fb25b9e3 54 status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error))
81447b5b 55
fb25b9e3 56 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
f6a5e476 57 return
81447b5b 58
fb25b9e3 59 utils._handle_func_status(status, 'failed to find plugin')
01f50d54 60 assert ptr is not None
f6a5e476 61 return _Plugin._create_from_ptr(ptr)
81447b5b 62
81447b5b 63
c3044a97 64class _PluginSet(object._SharedObject, collections.abc.Sequence):
a49e2cc3
PP
65 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
66 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
bb93d641 67
f6a5e476
PP
68 def __len__(self):
69 count = native_bt.plugin_set_get_plugin_count(self._ptr)
70 assert(count >= 0)
71 return count
81447b5b 72
f6a5e476
PP
73 def __getitem__(self, index):
74 utils._check_uint64(index)
7eb48359 75
f6a5e476
PP
76 if index >= len(self):
77 raise IndexError
7eb48359 78
bb93d641
SM
79 plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index)
80 assert plugin_ptr is not None
81 return _Plugin._create_from_ptr_and_get_ref(plugin_ptr)
7eb48359
PP
82
83
81447b5b
PP
84class _PluginVersion:
85 def __init__(self, major, minor, patch, extra):
86 self._major = major
87 self._minor = minor
88 self._patch = patch
89 self._extra = extra
90
91 @property
92 def major(self):
93 return self._major
94
95 @property
96 def minor(self):
97 return self._minor
98
99 @property
100 def patch(self):
101 return self._patch
102
103 @property
104 def extra(self):
105 return self._extra
106
107 def __str__(self):
108 extra = ''
109
110 if self._extra is not None:
111 extra = self._extra
112
113 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
114
115
f6a5e476
PP
116class _PluginComponentClassesIterator(collections.abc.Iterator):
117 def __init__(self, plugin_comp_cls):
118 self._plugin_comp_cls = plugin_comp_cls
119 self._at = 0
120
121 def __next__(self):
122 plugin_ptr = self._plugin_comp_cls._plugin._ptr
bb93d641 123 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
f6a5e476 124
bb93d641
SM
125 if self._at == total:
126 raise StopIteration
f6a5e476 127
bb93d641
SM
128 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(plugin_ptr, self._at)
129 assert comp_cls_ptr is not None
130 self._at += 1
f6a5e476 131
bb93d641
SM
132 comp_cls_type = self._plugin_comp_cls._comp_cls_type
133 comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]
134 comp_cls_ptr = comp_cls_pycls._as_component_class_ptr(comp_cls_ptr)
f6a5e476 135 name = native_bt.component_class_get_name(comp_cls_ptr)
bb93d641 136 assert name is not None
f6a5e476
PP
137 return name
138
139
140class _PluginComponentClasses(collections.abc.Mapping):
bb93d641 141 def __init__(self, plugin):
f6a5e476 142 self._plugin = plugin
f6a5e476
PP
143
144 def __getitem__(self, key):
145 utils._check_str(key)
bb93d641 146 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
f6a5e476
PP
147
148 if cc_ptr is None:
149 raise KeyError(key)
150
bb93d641 151 return bt2.component._create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type)
f6a5e476
PP
152
153 def __len__(self):
bb93d641
SM
154 return self._component_class_count(self._plugin._ptr)
155
156 def __iter__(self):
157 return _PluginComponentClassesIterator(self)
f6a5e476 158
f6a5e476 159
bb93d641 160class _PluginSourceComponentClasses(_PluginComponentClasses):
a49e2cc3
PP
161 _component_class_count = staticmethod(native_bt.plugin_get_source_component_class_count)
162 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_source_component_class_by_name_const)
163 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_source_component_class_by_index_const)
bb93d641 164 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
f6a5e476 165
f6a5e476 166
bb93d641 167class _PluginFilterComponentClasses(_PluginComponentClasses):
a49e2cc3
PP
168 _component_class_count = staticmethod(native_bt.plugin_get_filter_component_class_count)
169 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_filter_component_class_by_name_const)
170 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_filter_component_class_by_index_const)
bb93d641 171 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
f6a5e476 172
bb93d641
SM
173
174class _PluginSinkComponentClasses(_PluginComponentClasses):
a49e2cc3
PP
175 _component_class_count = staticmethod(native_bt.plugin_get_sink_component_class_count)
176 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_sink_component_class_by_name_const)
177 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_sink_component_class_by_index_const)
bb93d641 178 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
f6a5e476
PP
179
180
c3044a97 181class _Plugin(object._SharedObject):
a49e2cc3
PP
182 _put_ref = staticmethod(native_bt.plugin_put_ref)
183 _get_ref = staticmethod(native_bt.plugin_get_ref)
bb93d641 184
81447b5b
PP
185 @property
186 def name(self):
187 name = native_bt.plugin_get_name(self._ptr)
f6a5e476 188 assert(name is not None)
81447b5b
PP
189 return name
190
191 @property
192 def author(self):
193 return native_bt.plugin_get_author(self._ptr)
194
195 @property
196 def license(self):
197 return native_bt.plugin_get_license(self._ptr)
198
199 @property
200 def description(self):
201 return native_bt.plugin_get_description(self._ptr)
202
203 @property
204 def path(self):
205 return native_bt.plugin_get_path(self._ptr)
206
207 @property
208 def version(self):
fb25b9e3 209 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
81447b5b 210
bb93d641 211 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
81447b5b
PP
212 return
213
214 return _PluginVersion(major, minor, patch, extra)
215
f6a5e476
PP
216 @property
217 def source_component_classes(self):
bb93d641 218 return _PluginSourceComponentClasses(self)
81447b5b 219
f6a5e476
PP
220 @property
221 def filter_component_classes(self):
bb93d641 222 return _PluginFilterComponentClasses(self)
81447b5b 223
f6a5e476
PP
224 @property
225 def sink_component_classes(self):
bb93d641 226 return _PluginSinkComponentClasses(self)
This page took 0.048907 seconds and 4 git commands to generate.