lib: add bt_{graph,query_executor}_add_interrupter()
[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
811644b8 26import os.path
81447b5b
PP
27import bt2
28
29
9736d991 30def find_plugins(path, recurse=True, fail_on_load_error=False):
811644b8
PP
31 utils._check_str(path)
32 utils._check_bool(recurse)
9736d991 33 utils._check_bool(fail_on_load_error)
811644b8 34 plugin_set_ptr = None
81447b5b 35
811644b8 36 if os.path.isfile(path):
cfbd7cf3
FD
37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(
38 path, fail_on_load_error
39 )
811644b8 40 elif os.path.isdir(path):
cfbd7cf3
FD
41 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(
42 path, int(recurse), int(fail_on_load_error)
43 )
9736d991 44 else:
ce4923b0 45 raise ValueError("invalid path: '{}'".format(path))
9736d991 46
d24d5663 47 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 48 return
81447b5b 49
d24d5663 50 utils._handle_func_status(status, 'failed to find plugins')
9736d991 51 assert plugin_set_ptr is not None
811644b8 52 return _PluginSet._create_from_ptr(plugin_set_ptr)
81447b5b 53
81447b5b 54
9736d991 55def find_plugin(name, fail_on_load_error=False):
811644b8 56 utils._check_str(name)
9736d991 57 utils._check_bool(fail_on_load_error)
d24d5663 58 status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error))
81447b5b 59
d24d5663 60 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 61 return
81447b5b 62
d24d5663 63 utils._handle_func_status(status, 'failed to find plugin')
9736d991 64 assert ptr is not None
811644b8 65 return _Plugin._create_from_ptr(ptr)
81447b5b 66
81447b5b 67
78288f58 68class _PluginSet(object._SharedObject, collections.abc.Sequence):
2f16a6a2
PP
69 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
70 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
ab1bca6c 71
811644b8
PP
72 def __len__(self):
73 count = native_bt.plugin_set_get_plugin_count(self._ptr)
cfbd7cf3 74 assert count >= 0
811644b8 75 return count
81447b5b 76
811644b8
PP
77 def __getitem__(self, index):
78 utils._check_uint64(index)
7eb48359 79
811644b8
PP
80 if index >= len(self):
81 raise IndexError
7eb48359 82
ab1bca6c
SM
83 plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index)
84 assert plugin_ptr is not None
85 return _Plugin._create_from_ptr_and_get_ref(plugin_ptr)
7eb48359
PP
86
87
81447b5b
PP
88class _PluginVersion:
89 def __init__(self, major, minor, patch, extra):
90 self._major = major
91 self._minor = minor
92 self._patch = patch
93 self._extra = extra
94
95 @property
96 def major(self):
97 return self._major
98
99 @property
100 def minor(self):
101 return self._minor
102
103 @property
104 def patch(self):
105 return self._patch
106
107 @property
108 def extra(self):
109 return self._extra
110
111 def __str__(self):
112 extra = ''
113
114 if self._extra is not None:
115 extra = self._extra
116
117 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
118
119
811644b8
PP
120class _PluginComponentClassesIterator(collections.abc.Iterator):
121 def __init__(self, plugin_comp_cls):
122 self._plugin_comp_cls = plugin_comp_cls
123 self._at = 0
124
125 def __next__(self):
126 plugin_ptr = self._plugin_comp_cls._plugin._ptr
ab1bca6c 127 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
811644b8 128
ab1bca6c
SM
129 if self._at == total:
130 raise StopIteration
811644b8 131
cfbd7cf3
FD
132 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(
133 plugin_ptr, self._at
134 )
ab1bca6c
SM
135 assert comp_cls_ptr is not None
136 self._at += 1
811644b8 137
ab1bca6c 138 comp_cls_type = self._plugin_comp_cls._comp_cls_type
cfbd7cf3
FD
139 comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
140 comp_cls_type
141 ]
85906b6b 142 comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr)
811644b8 143 name = native_bt.component_class_get_name(comp_cls_ptr)
ab1bca6c 144 assert name is not None
811644b8
PP
145 return name
146
147
148class _PluginComponentClasses(collections.abc.Mapping):
ab1bca6c 149 def __init__(self, plugin):
811644b8 150 self._plugin = plugin
811644b8
PP
151
152 def __getitem__(self, key):
153 utils._check_str(key)
ab1bca6c 154 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
811644b8
PP
155
156 if cc_ptr is None:
157 raise KeyError(key)
158
cfbd7cf3
FD
159 return bt2.component._create_component_class_from_ptr_and_get_ref(
160 cc_ptr, self._comp_cls_type
161 )
811644b8
PP
162
163 def __len__(self):
ab1bca6c
SM
164 return self._component_class_count(self._plugin._ptr)
165
166 def __iter__(self):
167 return _PluginComponentClassesIterator(self)
811644b8 168
811644b8 169
ab1bca6c 170class _PluginSourceComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
171 _component_class_count = staticmethod(
172 native_bt.plugin_get_source_component_class_count
173 )
174 _borrow_component_class_by_name = staticmethod(
175 native_bt.plugin_borrow_source_component_class_by_name_const
176 )
177 _borrow_component_class_by_index = staticmethod(
178 native_bt.plugin_borrow_source_component_class_by_index_const
179 )
ab1bca6c 180 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
811644b8 181
811644b8 182
ab1bca6c 183class _PluginFilterComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
184 _component_class_count = staticmethod(
185 native_bt.plugin_get_filter_component_class_count
186 )
187 _borrow_component_class_by_name = staticmethod(
188 native_bt.plugin_borrow_filter_component_class_by_name_const
189 )
190 _borrow_component_class_by_index = staticmethod(
191 native_bt.plugin_borrow_filter_component_class_by_index_const
192 )
ab1bca6c 193 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 194
ab1bca6c
SM
195
196class _PluginSinkComponentClasses(_PluginComponentClasses):
cfbd7cf3
FD
197 _component_class_count = staticmethod(
198 native_bt.plugin_get_sink_component_class_count
199 )
200 _borrow_component_class_by_name = staticmethod(
201 native_bt.plugin_borrow_sink_component_class_by_name_const
202 )
203 _borrow_component_class_by_index = staticmethod(
204 native_bt.plugin_borrow_sink_component_class_by_index_const
205 )
ab1bca6c 206 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
811644b8
PP
207
208
78288f58 209class _Plugin(object._SharedObject):
2f16a6a2
PP
210 _put_ref = staticmethod(native_bt.plugin_put_ref)
211 _get_ref = staticmethod(native_bt.plugin_get_ref)
ab1bca6c 212
81447b5b
PP
213 @property
214 def name(self):
215 name = native_bt.plugin_get_name(self._ptr)
cfbd7cf3 216 assert name is not None
81447b5b
PP
217 return name
218
219 @property
220 def author(self):
221 return native_bt.plugin_get_author(self._ptr)
222
223 @property
224 def license(self):
225 return native_bt.plugin_get_license(self._ptr)
226
227 @property
228 def description(self):
229 return native_bt.plugin_get_description(self._ptr)
230
231 @property
232 def path(self):
233 return native_bt.plugin_get_path(self._ptr)
234
235 @property
236 def version(self):
d24d5663 237 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
81447b5b 238
ab1bca6c 239 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
81447b5b
PP
240 return
241
242 return _PluginVersion(major, minor, patch, extra)
243
811644b8
PP
244 @property
245 def source_component_classes(self):
ab1bca6c 246 return _PluginSourceComponentClasses(self)
81447b5b 247
811644b8
PP
248 @property
249 def filter_component_classes(self):
ab1bca6c 250 return _PluginFilterComponentClasses(self)
81447b5b 251
811644b8
PP
252 @property
253 def sink_component_classes(self):
ab1bca6c 254 return _PluginSinkComponentClasses(self)
This page took 0.053873 seconds and 4 git commands to generate.