Python plugin provider: only finalize interpreter if we initialized it
[babeltrace.git] / 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):
31 utils._check_str(path)
32 utils._check_bool(recurse)
33 plugin_set_ptr = None
34
35 if os.path.isfile(path):
36 plugin_set_ptr = native_bt.plugin_create_all_from_file(path)
37 elif os.path.isdir(path):
38 plugin_set_ptr = native_bt.plugin_create_all_from_dir(path, int(recurse))
39
40 if plugin_set_ptr is None:
41 return
42
43 return _PluginSet._create_from_ptr(plugin_set_ptr)
44
45
46 def find_plugin(name):
47 utils._check_str(name)
48 ptr = native_bt.plugin_find(name)
49
50 if ptr is None:
51 return
52
53 return _Plugin._create_from_ptr(ptr)
54
55
56 class _PluginSet(object._Object, collections.abc.Sequence):
57 def __len__(self):
58 count = native_bt.plugin_set_get_plugin_count(self._ptr)
59 assert(count >= 0)
60 return count
61
62 def __getitem__(self, index):
63 utils._check_uint64(index)
64
65 if index >= len(self):
66 raise IndexError
67
68 plugin_ptr = native_bt.plugin_set_get_plugin(self._ptr, index)
69 assert(plugin_ptr)
70 return _Plugin._create_from_ptr(plugin_ptr)
71
72
73 class _PluginVersion:
74 def __init__(self, major, minor, patch, extra):
75 self._major = major
76 self._minor = minor
77 self._patch = patch
78 self._extra = extra
79
80 @property
81 def major(self):
82 return self._major
83
84 @property
85 def minor(self):
86 return self._minor
87
88 @property
89 def patch(self):
90 return self._patch
91
92 @property
93 def extra(self):
94 return self._extra
95
96 def __str__(self):
97 extra = ''
98
99 if self._extra is not None:
100 extra = self._extra
101
102 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
103
104
105 class _PluginComponentClassesIterator(collections.abc.Iterator):
106 def __init__(self, plugin_comp_cls):
107 self._plugin_comp_cls = plugin_comp_cls
108 self._at = 0
109
110 def __next__(self):
111 plugin_ptr = self._plugin_comp_cls._plugin._ptr
112 comp_cls_type = self._plugin_comp_cls._comp_cls_type
113 total = native_bt.plugin_get_component_class_count(plugin_ptr)
114
115 while True:
116 if self._at == total:
117 raise StopIteration
118
119 comp_cls_ptr = native_bt.plugin_get_component_class_by_index(plugin_ptr,
120 self._at)
121 assert(comp_cls_ptr)
122 cc_type = native_bt.component_class_get_type(comp_cls_ptr)
123 self._at += 1
124
125 if cc_type == comp_cls_type:
126 break
127
128 native_bt.put(comp_cls_ptr)
129
130 name = native_bt.component_class_get_name(comp_cls_ptr)
131 native_bt.put(comp_cls_ptr)
132 assert(name is not None)
133 return name
134
135
136 class _PluginComponentClasses(collections.abc.Mapping):
137 def __init__(self, plugin, comp_cls_type):
138 self._plugin = plugin
139 self._comp_cls_type = comp_cls_type
140
141 def __getitem__(self, key):
142 utils._check_str(key)
143 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._plugin._ptr,
144 key,
145 self._comp_cls_type)
146
147 if cc_ptr is None:
148 raise KeyError(key)
149
150 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
151
152 def __len__(self):
153 count = 0
154 total = native_bt.plugin_get_component_class_count(self._plugin._ptr)
155
156 for at in range(total):
157 comp_cls_ptr = native_bt.plugin_get_component_class_by_index(self._plugin._ptr,
158 at)
159 assert(comp_cls_ptr)
160 cc_type = native_bt.component_class_get_type(comp_cls_ptr)
161
162 if cc_type == self._comp_cls_type:
163 count += 1
164
165 native_bt.put(comp_cls_ptr)
166
167 return count
168
169 def __iter__(self):
170 return _PluginComponentClassesIterator(self)
171
172
173 class _Plugin(object._Object):
174 @property
175 def name(self):
176 name = native_bt.plugin_get_name(self._ptr)
177 assert(name is not None)
178 return name
179
180 @property
181 def author(self):
182 return native_bt.plugin_get_author(self._ptr)
183
184 @property
185 def license(self):
186 return native_bt.plugin_get_license(self._ptr)
187
188 @property
189 def description(self):
190 return native_bt.plugin_get_description(self._ptr)
191
192 @property
193 def path(self):
194 return native_bt.plugin_get_path(self._ptr)
195
196 @property
197 def version(self):
198 status, major, minor, patch, extra = native_bt.plugin_get_version(self._ptr)
199
200 if status < 0:
201 return
202
203 return _PluginVersion(major, minor, patch, extra)
204
205 @property
206 def source_component_classes(self):
207 return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_SOURCE)
208
209 @property
210 def filter_component_classes(self):
211 return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_FILTER)
212
213 @property
214 def sink_component_classes(self):
215 return _PluginComponentClasses(self, native_bt.COMPONENT_CLASS_TYPE_SINK)
This page took 0.033574 seconds and 4 git commands to generate.