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