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