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