lib: strictly type function return status enumerations
[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 import bt2.component
26 import os.path
27 import bt2
28
29
30 def find_plugins(path, recurse=True, fail_on_load_error=False):
31 utils._check_str(path)
32 utils._check_bool(recurse)
33 utils._check_bool(fail_on_load_error)
34 plugin_set_ptr = None
35
36 if os.path.isfile(path):
37 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file(path, fail_on_load_error)
38 elif os.path.isdir(path):
39 status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir(path, int(recurse), int(fail_on_load_error))
40 else:
41 raise bt2.Error("invalid path: '{}'".format(path))
42
43 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
44 return
45
46 utils._handle_func_status(status, 'failed to find plugins')
47 assert plugin_set_ptr is not None
48 return _PluginSet._create_from_ptr(plugin_set_ptr)
49
50
51 def find_plugin(name, fail_on_load_error=False):
52 utils._check_str(name)
53 utils._check_bool(fail_on_load_error)
54 status, ptr = native_bt.bt2_plugin_find(name, int(fail_on_load_error))
55
56 if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND:
57 return
58
59 utils._handle_func_status(status, 'failed to find plugin')
60 assert ptr is not None
61 return _Plugin._create_from_ptr(ptr)
62
63
64 class _PluginSet(object._SharedObject, collections.abc.Sequence):
65 _put_ref = staticmethod(native_bt.plugin_set_put_ref)
66 _get_ref = staticmethod(native_bt.plugin_set_get_ref)
67
68 def __len__(self):
69 count = native_bt.plugin_set_get_plugin_count(self._ptr)
70 assert(count >= 0)
71 return count
72
73 def __getitem__(self, index):
74 utils._check_uint64(index)
75
76 if index >= len(self):
77 raise IndexError
78
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)
82
83
84 class _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
116 class _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
123 total = self._plugin_comp_cls._component_class_count(plugin_ptr)
124
125 if self._at == total:
126 raise StopIteration
127
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
131
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]
134 comp_cls_ptr = comp_cls_pycls._as_component_class_ptr(comp_cls_ptr)
135 name = native_bt.component_class_get_name(comp_cls_ptr)
136 assert name is not None
137 return name
138
139
140 class _PluginComponentClasses(collections.abc.Mapping):
141 def __init__(self, plugin):
142 self._plugin = plugin
143
144 def __getitem__(self, key):
145 utils._check_str(key)
146 cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key)
147
148 if cc_ptr is None:
149 raise KeyError(key)
150
151 return bt2.component._create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type)
152
153 def __len__(self):
154 return self._component_class_count(self._plugin._ptr)
155
156 def __iter__(self):
157 return _PluginComponentClassesIterator(self)
158
159
160 class _PluginSourceComponentClasses(_PluginComponentClasses):
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)
164 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
165
166
167 class _PluginFilterComponentClasses(_PluginComponentClasses):
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)
171 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
172
173
174 class _PluginSinkComponentClasses(_PluginComponentClasses):
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)
178 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
179
180
181 class _Plugin(object._SharedObject):
182 _put_ref = staticmethod(native_bt.plugin_put_ref)
183 _get_ref = staticmethod(native_bt.plugin_get_ref)
184
185 @property
186 def name(self):
187 name = native_bt.plugin_get_name(self._ptr)
188 assert(name is not None)
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):
209 status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr)
210
211 if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE:
212 return
213
214 return _PluginVersion(major, minor, patch, extra)
215
216 @property
217 def source_component_classes(self):
218 return _PluginSourceComponentClasses(self)
219
220 @property
221 def filter_component_classes(self):
222 return _PluginFilterComponentClasses(self)
223
224 @property
225 def sink_component_classes(self):
226 return _PluginSinkComponentClasses(self)
This page took 0.034189 seconds and 4 git commands to generate.