bt2: make bt2.Error wrap current thread's error
[babeltrace.git] / src / bindings / python / bt2 / bt2 / plugin.py
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
23 from bt2 import native_bt, object, utils
24 import collections.abc
25 import bt2.component
26 import os.path
27 import bt2
28
29
30 def find_plugins(path, recurse=True, fail_on_load_error=False):
31 utils._check_str(path)
32 utils._check_bool(recurse)
33 utils._check_bool(fail_on_load_error)
34 plugin_set_ptr = None
35
36 if os.path.isfile(path):
37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(
38 path, fail_on_load_error
39 )
40 elif os.path.isdir(path):
41 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(
42 path, int(recurse), int(fail_on_load_error)
43 )
44 else:
45 raise ValueError("invalid path: '{}'".format(path))
46
47 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
48 return
49
50 utils._handle_func_status(status, 'failed to find plugins')
51 assert plugin_set_ptr is not None
52 return _PluginSet._create_from_ptr(plugin_set_ptr)
53
54
55 def find_plugin(name, fail_on_load_error=False):
56 utils._check_str(name)
57 utils._check_bool(fail_on_load_error)
58 status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error))
59
60 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
61 return
62
63 utils._handle_func_status(status, 'failed to find plugin')
64 assert ptr is not None
65 return _Plugin._create_from_ptr(ptr)
66
67
68 class _PluginSet(object._SharedObject, collections.abc.Sequence):
69 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
70 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
71
72 def __len__(self):
73 count = native_bt.plugin_set_get_plugin_count(self._ptr)
74 assert count >= 0
75 return count
76
77 def __getitem__(self, index):
78 utils._check_uint64(index)
79
80 if index >= len(self):
81 raise IndexError
82
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)
86
87
88 class _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
120 class _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
127 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
128
129 if self._at == total:
130 raise StopIteration
131
132 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(
133 plugin_ptr, self._at
134 )
135 assert comp_cls_ptr is not None
136 self._at += 1
137
138 comp_cls_type = self._plugin_comp_cls._comp_cls_type
139 comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
140 comp_cls_type
141 ]
142 comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr)
143 name = native_bt.component_class_get_name(comp_cls_ptr)
144 assert name is not None
145 return name
146
147
148 class _PluginComponentClasses(collections.abc.Mapping):
149 def __init__(self, plugin):
150 self._plugin = plugin
151
152 def __getitem__(self, key):
153 utils._check_str(key)
154 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
155
156 if cc_ptr is None:
157 raise KeyError(key)
158
159 return bt2.component._create_component_class_from_ptr_and_get_ref(
160 cc_ptr, self._comp_cls_type
161 )
162
163 def __len__(self):
164 return self._component_class_count(self._plugin._ptr)
165
166 def __iter__(self):
167 return _PluginComponentClassesIterator(self)
168
169
170 class _PluginSourceComponentClasses(_PluginComponentClasses):
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 )
180 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
181
182
183 class _PluginFilterComponentClasses(_PluginComponentClasses):
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 )
193 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
194
195
196 class _PluginSinkComponentClasses(_PluginComponentClasses):
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 )
206 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
207
208
209 class _Plugin(object._SharedObject):
210 _put_ref = staticmethod(native_bt.plugin_put_ref)
211 _get_ref = staticmethod(native_bt.plugin_get_ref)
212
213 @property
214 def name(self):
215 name = native_bt.plugin_get_name(self._ptr)
216 assert name is not None
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):
237 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
238
239 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
240 return
241
242 return _PluginVersion(major, minor, patch, extra)
243
244 @property
245 def source_component_classes(self):
246 return _PluginSourceComponentClasses(self)
247
248 @property
249 def filter_component_classes(self):
250 return _PluginFilterComponentClasses(self)
251
252 @property
253 def sink_component_classes(self):
254 return _PluginSinkComponentClasses(self)
This page took 0.035219 seconds and 4 git commands to generate.