Add bt_plugin_create_from_name() tests
[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
59class _PluginVersion:
60 def __init__(self, major, minor, patch, extra):
61 self._major = major
62 self._minor = minor
63 self._patch = patch
64 self._extra = extra
65
66 @property
67 def major(self):
68 return self._major
69
70 @property
71 def minor(self):
72 return self._minor
73
74 @property
75 def patch(self):
76 return self._patch
77
78 @property
79 def extra(self):
80 return self._extra
81
82 def __str__(self):
83 extra = ''
84
85 if self._extra is not None:
86 extra = self._extra
87
88 return '{}.{}.{}{}'.format(self._major, self._minor, self._patch, extra)
89
90
91class _Plugin(object._Object, collections.abc.Sequence):
92 @property
93 def name(self):
94 name = native_bt.plugin_get_name(self._ptr)
95 utils._handle_ptr(name, "cannot get plugin object's name")
96 return name
97
98 @property
99 def author(self):
100 return native_bt.plugin_get_author(self._ptr)
101
102 @property
103 def license(self):
104 return native_bt.plugin_get_license(self._ptr)
105
106 @property
107 def description(self):
108 return native_bt.plugin_get_description(self._ptr)
109
110 @property
111 def path(self):
112 return native_bt.plugin_get_path(self._ptr)
113
114 @property
115 def version(self):
116 status, major, minor, patch, extra = native_bt.plugin_get_version(self._ptr)
117
118 if status < 0:
119 return
120
121 return _PluginVersion(major, minor, patch, extra)
122
123 def source_component_class(self, name):
124 utils._check_str(name)
125 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
126 name,
127 native_bt.COMPONENT_CLASS_TYPE_SOURCE)
128
129 if cc_ptr is None:
130 return
131
132 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
133
134 def filter_component_class(self, name):
135 utils._check_str(name)
136 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
137 name,
138 native_bt.COMPONENT_CLASS_TYPE_FILTER)
139
140 if cc_ptr is None:
141 return
142
143 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
144
145 def sink_component_class(self, name):
146 utils._check_str(name)
147 cc_ptr = native_bt.plugin_get_component_class_by_name_and_type(self._ptr,
148 name,
149 native_bt.COMPONENT_CLASS_TYPE_SINK)
150
151 if cc_ptr is None:
152 return
153
154 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
155
156 def __len__(self):
157 count = native_bt.plugin_get_component_class_count(self._ptr)
158 utils._handle_ret(count, "cannot get plugin object's component class count")
159 return count
160
161 def __getitem__(self, index):
162 utils._check_uint64(index)
163
164 if index >= len(self):
165 raise IndexError
166
167 cc_ptr = native_bt.plugin_get_component_class(self._ptr, index)
168 utils._handle_ptr(cc_ptr, "cannot get plugin object's component class object")
169 return bt2.component._create_generic_component_class_from_ptr(cc_ptr)
This page took 0.028021 seconds and 4 git commands to generate.