lib/plugin/plugin.c: do not use G_MODULE_BIND_LOCAL for Python plugin provider
[babeltrace.git] / bindings / python / 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
26import bt2
27
28
29def _plugin_ptrs_to_plugins(plugin_ptrs):
30 plugins = []
31
32 for plugin_ptr in plugin_ptrs:
33 plugin = _Plugin._create_from_ptr(plugin_ptr)
34 plugins.append(plugin)
35
36 return plugins
37
38
39def create_plugins_from_file(path):
40 utils._check_str(path)
41 plugin_ptrs = native_bt.py3_plugin_create_all_from_file(path)
42
43 if plugin_ptrs is None:
44 raise bt2.Error('cannot get plugin objects from file')
45
46 return _plugin_ptrs_to_plugins(plugin_ptrs)
47
48
49def create_plugins_from_dir(path, recurse=True):
50 utils._check_str(path)
51 plugin_ptrs = native_bt.py3_plugin_create_all_from_dir(path, recurse)
52
53 if plugin_ptrs is None:
54 raise bt2.Error('cannot get plugin objects from directory')
55
56 return _plugin_ptrs_to_plugins(plugin_ptrs)
57
58
7eb48359
PP
59def create_plugin_from_name(name):
60 utils._check_str(name)
61 plugin_ptr = native_bt.plugin_create_from_name(name)
62
63 if plugin_ptr is None:
9a0a47ae 64 raise bt2.NoSuchPluginError(name)
7eb48359
PP
65
66 return _Plugin._create_from_ptr(plugin_ptr)
67
68
81447b5b
PP
69class _PluginVersion:
70 def __init__(self, major, minor, patch, extra):
71 self._major = major
72 self._minor = minor
73 self._patch = patch
74 self._extra = extra
75
76 @property
77 def major(self):
78 return self._major
79
80 @property
81 def minor(self):
82 return self._minor
83
84 @property
85 def patch(self):
86 return self._patch
87
88 @property
89 def extra(self):
90 return self._extra
91
92 def __str__(self):
93 extra = ''
94
95 if self._extra is not None:
96 extra = self._extra
97
98 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
99
100
101class _Plugin(object._Object, collections.abc.Sequence):
102 @property
103 def name(self):
104 name = native_bt.plugin_get_name(self._ptr)
105 utils._handle_ptr(name, "cannot get plugin object's name")
106 return name
107
108 @property
109 def author(self):
110 return native_bt.plugin_get_author(self._ptr)
111
112 @property
113 def license(self):
114 return native_bt.plugin_get_license(self._ptr)
115
116 @property
117 def description(self):
118 return native_bt.plugin_get_description(self._ptr)
119
120 @property
121 def path(self):
122 return native_bt.plugin_get_path(self._ptr)
123
124 @property
125 def version(self):
126 status, major, minor, patch, extra = native_bt.plugin_get_version(self._ptr)
127
128 if status < 0:
129 return
130
131 return _PluginVersion(major, minor, patch, extra)
132
133 def source_component_class(self, name):
134 utils._check_str(name)
135 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
136 name,
137 native_bt.COMPONENT_CLASS_TYPE_SOURCE)
138
139 if cc_ptr is None:
140 return
141
142 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
143
144 def filter_component_class(self, name):
145 utils._check_str(name)
146 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
147 name,
148 native_bt.COMPONENT_CLASS_TYPE_FILTER)
149
150 if cc_ptr is None:
151 return
152
153 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
154
155 def sink_component_class(self, name):
156 utils._check_str(name)
157 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
158 name,
159 native_bt.COMPONENT_CLASS_TYPE_SINK)
160
161 if cc_ptr is None:
162 return
163
164 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
165
166 def __len__(self):
167 count = native_bt.plugin_get_component_class_count(self._ptr)
168 utils._handle_ret(count, "cannot get plugin object's component class count")
169 return count
170
171 def __getitem__(self, index):
172 utils._check_uint64(index)
173
174 if index >= len(self):
175 raise IndexError
176
177 cc_ptr = native_bt.plugin_get_component_class(self._ptr, index)
178 utils._handle_ptr(cc_ptr, "cannot get plugin object's component class object")
179 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
This page took 0.035111 seconds and 4 git commands to generate.