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