Explicitly mention `black` in CodingStyle guidelines
[babeltrace.git] / src / bindings / python / bt2 / bt2 / plugin.py
CommitLineData
81447b5b
PP
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
23from bt2 import native_bt, object, utils
24import collections.abc
25import bt2.component
811644b8 26import os.path
81447b5b
PP
27import bt2
28
29
9736d991 30def find_plugins(path, recurse=True, fail_on_load_error=False):
811644b8
PP
31 utils._check_str(path)
32 utils._check_bool(recurse)
9736d991 33 utils._check_bool(fail_on_load_error)
811644b8 34 plugin_set_ptr = None
81447b5b 35
811644b8 36 if os.path.isfile(path):
d24d5663 37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(path, fail_on_load_error)
811644b8 38 elif os.path.isdir(path):
d24d5663 39 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(path, int(recurse), int(fail_on_load_error))
9736d991
PP
40 else:
41 raise bt2.Error("invalid path: '{}'".format(path))
42
d24d5663 43 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 44 return
81447b5b 45
d24d5663 46 utils._handle_func_status(status, 'failed to find plugins')
9736d991 47 assert plugin_set_ptr is not None
811644b8 48 return _PluginSet._create_from_ptr(plugin_set_ptr)
81447b5b 49
81447b5b 50
9736d991 51def find_plugin(name, fail_on_load_error=False):
811644b8 52 utils._check_str(name)
9736d991 53 utils._check_bool(fail_on_load_error)
d24d5663 54 status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error))
81447b5b 55
d24d5663 56 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
811644b8 57 return
81447b5b 58
d24d5663 59 utils._handle_func_status(status, 'failed to find plugin')
9736d991 60 assert ptr is not None
811644b8 61 return _Plugin._create_from_ptr(ptr)
81447b5b 62
81447b5b 63
78288f58 64class _PluginSet(object._SharedObject, collections.abc.Sequence):
2f16a6a2
PP
65 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
66 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
ab1bca6c 67
811644b8
PP
68 def __len__(self):
69 count = native_bt.plugin_set_get_plugin_count(self._ptr)
70 assert(count >= 0)
71 return count
81447b5b 72
811644b8
PP
73 def __getitem__(self, index):
74 utils._check_uint64(index)
7eb48359 75
811644b8
PP
76 if index >= len(self):
77 raise IndexError
7eb48359 78
ab1bca6c
SM
79 plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index)
80 assert plugin_ptr is not None
81 return _Plugin._create_from_ptr_and_get_ref(plugin_ptr)
7eb48359
PP
82
83
81447b5b
PP
84class _PluginVersion:
85 def __init__(self, major, minor, patch, extra):
86 self._major = major
87 self._minor = minor
88 self._patch = patch
89 self._extra = extra
90
91 @property
92 def major(self):
93 return self._major
94
95 @property
96 def minor(self):
97 return self._minor
98
99 @property
100 def patch(self):
101 return self._patch
102
103 @property
104 def extra(self):
105 return self._extra
106
107 def __str__(self):
108 extra = ''
109
110 if self._extra is not None:
111 extra = self._extra
112
113 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
114
115
811644b8
PP
116class _PluginComponentClassesIterator(collections.abc.Iterator):
117 def __init__(self, plugin_comp_cls):
118 self._plugin_comp_cls = plugin_comp_cls
119 self._at = 0
120
121 def __next__(self):
122 plugin_ptr = self._plugin_comp_cls._plugin._ptr
ab1bca6c 123 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
811644b8 124
ab1bca6c
SM
125 if self._at == total:
126 raise StopIteration
811644b8 127
ab1bca6c
SM
128 comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index(plugin_ptr, self._at)
129 assert comp_cls_ptr is not None
130 self._at += 1
811644b8 131
ab1bca6c
SM
132 comp_cls_type = self._plugin_comp_cls._comp_cls_type
133 comp_cls_pycls = bt2.component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]
85906b6b 134 comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr)
811644b8 135 name = native_bt.component_class_get_name(comp_cls_ptr)
ab1bca6c 136 assert name is not None
811644b8
PP
137 return name
138
139
140class _PluginComponentClasses(collections.abc.Mapping):
ab1bca6c 141 def __init__(self, plugin):
811644b8 142 self._plugin = plugin
811644b8
PP
143
144 def __getitem__(self, key):
145 utils._check_str(key)
ab1bca6c 146 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
811644b8
PP
147
148 if cc_ptr is None:
149 raise KeyError(key)
150
ab1bca6c 151 return bt2.component._create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type)
811644b8
PP
152
153 def __len__(self):
ab1bca6c
SM
154 return self._component_class_count(self._plugin._ptr)
155
156 def __iter__(self):
157 return _PluginComponentClassesIterator(self)
811644b8 158
811644b8 159
ab1bca6c 160class _PluginSourceComponentClasses(_PluginComponentClasses):
2f16a6a2
PP
161 _component_class_count = staticmethod(native_bt.plugin_get_source_component_class_count)
162 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_source_component_class_by_name_const)
163 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_source_component_class_by_index_const)
ab1bca6c 164 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
811644b8 165
811644b8 166
ab1bca6c 167class _PluginFilterComponentClasses(_PluginComponentClasses):
2f16a6a2
PP
168 _component_class_count = staticmethod(native_bt.plugin_get_filter_component_class_count)
169 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_filter_component_class_by_name_const)
170 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_filter_component_class_by_index_const)
ab1bca6c 171 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 172
ab1bca6c
SM
173
174class _PluginSinkComponentClasses(_PluginComponentClasses):
2f16a6a2
PP
175 _component_class_count = staticmethod(native_bt.plugin_get_sink_component_class_count)
176 _borrow_component_class_by_name = staticmethod(native_bt.plugin_borrow_sink_component_class_by_name_const)
177 _borrow_component_class_by_index = staticmethod(native_bt.plugin_borrow_sink_component_class_by_index_const)
ab1bca6c 178 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
811644b8
PP
179
180
78288f58 181class _Plugin(object._SharedObject):
2f16a6a2
PP
182 _put_ref = staticmethod(native_bt.plugin_put_ref)
183 _get_ref = staticmethod(native_bt.plugin_get_ref)
ab1bca6c 184
81447b5b
PP
185 @property
186 def name(self):
187 name = native_bt.plugin_get_name(self._ptr)
811644b8 188 assert(name is not None)
81447b5b
PP
189 return name
190
191 @property
192 def author(self):
193 return native_bt.plugin_get_author(self._ptr)
194
195 @property
196 def license(self):
197 return native_bt.plugin_get_license(self._ptr)
198
199 @property
200 def description(self):
201 return native_bt.plugin_get_description(self._ptr)
202
203 @property
204 def path(self):
205 return native_bt.plugin_get_path(self._ptr)
206
207 @property
208 def version(self):
d24d5663 209 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
81447b5b 210
ab1bca6c 211 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
81447b5b
PP
212 return
213
214 return _PluginVersion(major, minor, patch, extra)
215
811644b8
PP
216 @property
217 def source_component_classes(self):
ab1bca6c 218 return _PluginSourceComponentClasses(self)
81447b5b 219
811644b8
PP
220 @property
221 def filter_component_classes(self):
ab1bca6c 222 return _PluginFilterComponentClasses(self)
81447b5b 223
811644b8
PP
224 @property
225 def sink_component_classes(self):
ab1bca6c 226 return _PluginSinkComponentClasses(self)
This page took 0.051554 seconds and 4 git commands to generate.