| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | import os.path |
| 6 | import collections.abc |
| 7 | |
| 8 | from bt2 import utils as bt2_utils |
| 9 | from bt2 import object as bt2_object |
| 10 | from bt2 import component as bt2_component |
| 11 | from bt2 import native_bt |
| 12 | |
| 13 | |
| 14 | def find_plugins_in_path(path, recurse=True, fail_on_load_error=False): |
| 15 | bt2_utils._check_str(path) |
| 16 | bt2_utils._check_bool(recurse) |
| 17 | bt2_utils._check_bool(fail_on_load_error) |
| 18 | plugin_set_ptr = None |
| 19 | |
| 20 | if os.path.isfile(path): |
| 21 | status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_file( |
| 22 | path, fail_on_load_error |
| 23 | ) |
| 24 | elif os.path.isdir(path): |
| 25 | status, plugin_set_ptr = native_bt.bt2_plugin_find_all_from_dir( |
| 26 | path, int(recurse), int(fail_on_load_error) |
| 27 | ) |
| 28 | else: |
| 29 | raise ValueError("invalid path: '{}'".format(path)) |
| 30 | |
| 31 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
| 32 | return |
| 33 | |
| 34 | bt2_utils._handle_func_status(status, "failed to find plugins") |
| 35 | assert plugin_set_ptr is not None |
| 36 | return _PluginSet._create_from_ptr(plugin_set_ptr) |
| 37 | |
| 38 | |
| 39 | def find_plugins( |
| 40 | find_in_std_env_var=True, |
| 41 | find_in_user_dir=True, |
| 42 | find_in_sys_dir=True, |
| 43 | find_in_static=True, |
| 44 | fail_on_load_error=False, |
| 45 | ): |
| 46 | bt2_utils._check_bool(find_in_std_env_var) |
| 47 | bt2_utils._check_bool(find_in_user_dir) |
| 48 | bt2_utils._check_bool(find_in_sys_dir) |
| 49 | bt2_utils._check_bool(find_in_static) |
| 50 | bt2_utils._check_bool(fail_on_load_error) |
| 51 | plugin_set_ptr = None |
| 52 | |
| 53 | status, plugin_set_ptr = native_bt.bt2_plugin_find_all( |
| 54 | int(find_in_std_env_var), |
| 55 | int(find_in_user_dir), |
| 56 | int(find_in_sys_dir), |
| 57 | int(find_in_static), |
| 58 | int(fail_on_load_error), |
| 59 | ) |
| 60 | |
| 61 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
| 62 | return |
| 63 | |
| 64 | bt2_utils._handle_func_status(status, "failed to find plugins") |
| 65 | assert plugin_set_ptr is not None |
| 66 | return _PluginSet._create_from_ptr(plugin_set_ptr) |
| 67 | |
| 68 | |
| 69 | def find_plugin( |
| 70 | name, |
| 71 | find_in_std_env_var=True, |
| 72 | find_in_user_dir=True, |
| 73 | find_in_sys_dir=True, |
| 74 | find_in_static=True, |
| 75 | fail_on_load_error=False, |
| 76 | ): |
| 77 | bt2_utils._check_str(name) |
| 78 | bt2_utils._check_bool(fail_on_load_error) |
| 79 | status, ptr = native_bt.bt2_plugin_find( |
| 80 | name, |
| 81 | int(find_in_std_env_var), |
| 82 | int(find_in_user_dir), |
| 83 | int(find_in_sys_dir), |
| 84 | int(find_in_static), |
| 85 | int(fail_on_load_error), |
| 86 | ) |
| 87 | |
| 88 | if status == native_bt.__BT_FUNC_STATUS_NOT_FOUND: |
| 89 | return |
| 90 | |
| 91 | bt2_utils._handle_func_status(status, "failed to find plugin") |
| 92 | assert ptr is not None |
| 93 | return _Plugin._create_from_ptr(ptr) |
| 94 | |
| 95 | |
| 96 | class _PluginSet(bt2_object._SharedObject, collections.abc.Sequence): |
| 97 | @staticmethod |
| 98 | def _put_ref(ptr): |
| 99 | native_bt.plugin_set_put_ref(ptr) |
| 100 | |
| 101 | @staticmethod |
| 102 | def _get_ref(ptr): |
| 103 | native_bt.plugin_set_get_ref(ptr) |
| 104 | |
| 105 | def __len__(self): |
| 106 | count = native_bt.plugin_set_get_plugin_count(self._ptr) |
| 107 | assert count >= 0 |
| 108 | return count |
| 109 | |
| 110 | def __getitem__(self, index): |
| 111 | bt2_utils._check_uint64(index) |
| 112 | |
| 113 | if index >= len(self): |
| 114 | raise IndexError |
| 115 | |
| 116 | plugin_ptr = native_bt.plugin_set_borrow_plugin_by_index_const(self._ptr, index) |
| 117 | assert plugin_ptr is not None |
| 118 | return _Plugin._create_from_ptr_and_get_ref(plugin_ptr) |
| 119 | |
| 120 | |
| 121 | class _PluginVersion: |
| 122 | def __init__(self, major, minor, patch, extra): |
| 123 | self._major = major |
| 124 | self._minor = minor |
| 125 | self._patch = patch |
| 126 | self._extra = extra |
| 127 | |
| 128 | @property |
| 129 | def major(self): |
| 130 | return self._major |
| 131 | |
| 132 | @property |
| 133 | def minor(self): |
| 134 | return self._minor |
| 135 | |
| 136 | @property |
| 137 | def patch(self): |
| 138 | return self._patch |
| 139 | |
| 140 | @property |
| 141 | def extra(self): |
| 142 | return self._extra |
| 143 | |
| 144 | def __str__(self): |
| 145 | extra = "" |
| 146 | |
| 147 | if self._extra is not None: |
| 148 | extra = self._extra |
| 149 | |
| 150 | return "{}.{}.{}{}".format(self._major, self._minor, self._patch, extra) |
| 151 | |
| 152 | |
| 153 | class _PluginComponentClassesIterator(collections.abc.Iterator): |
| 154 | def __init__(self, plugin_comp_cls): |
| 155 | self._plugin_comp_cls = plugin_comp_cls |
| 156 | self._at = 0 |
| 157 | |
| 158 | def __next__(self): |
| 159 | plugin_ptr = self._plugin_comp_cls._plugin._ptr |
| 160 | total = self._plugin_comp_cls._component_class_count(plugin_ptr) |
| 161 | |
| 162 | if self._at == total: |
| 163 | raise StopIteration |
| 164 | |
| 165 | comp_cls_ptr = self._plugin_comp_cls._borrow_component_class_by_index( |
| 166 | plugin_ptr, self._at |
| 167 | ) |
| 168 | assert comp_cls_ptr is not None |
| 169 | self._at += 1 |
| 170 | |
| 171 | comp_cls_type = self._plugin_comp_cls._comp_cls_type |
| 172 | comp_cls_pycls = bt2_component._COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[ |
| 173 | comp_cls_type |
| 174 | ] |
| 175 | comp_cls_ptr = comp_cls_pycls._bt_as_component_class_ptr(comp_cls_ptr) |
| 176 | name = native_bt.component_class_get_name(comp_cls_ptr) |
| 177 | assert name is not None |
| 178 | return name |
| 179 | |
| 180 | |
| 181 | class _PluginComponentClasses(collections.abc.Mapping): |
| 182 | def __init__(self, plugin): |
| 183 | self._plugin = plugin |
| 184 | |
| 185 | def __getitem__(self, key): |
| 186 | bt2_utils._check_str(key) |
| 187 | cc_ptr = self._borrow_component_class_by_name(self._plugin._ptr, key) |
| 188 | |
| 189 | if cc_ptr is None: |
| 190 | raise KeyError(key) |
| 191 | |
| 192 | return bt2_component._create_component_class_from_const_ptr_and_get_ref( |
| 193 | cc_ptr, self._comp_cls_type |
| 194 | ) |
| 195 | |
| 196 | def __len__(self): |
| 197 | return self._component_class_count(self._plugin._ptr) |
| 198 | |
| 199 | def __iter__(self): |
| 200 | return _PluginComponentClassesIterator(self) |
| 201 | |
| 202 | |
| 203 | class _PluginSourceComponentClasses(_PluginComponentClasses): |
| 204 | _component_class_count = staticmethod( |
| 205 | native_bt.plugin_get_source_component_class_count |
| 206 | ) |
| 207 | _borrow_component_class_by_name = staticmethod( |
| 208 | native_bt.plugin_borrow_source_component_class_by_name_const |
| 209 | ) |
| 210 | _borrow_component_class_by_index = staticmethod( |
| 211 | native_bt.plugin_borrow_source_component_class_by_index_const |
| 212 | ) |
| 213 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
| 214 | |
| 215 | |
| 216 | class _PluginFilterComponentClasses(_PluginComponentClasses): |
| 217 | _component_class_count = staticmethod( |
| 218 | native_bt.plugin_get_filter_component_class_count |
| 219 | ) |
| 220 | _borrow_component_class_by_name = staticmethod( |
| 221 | native_bt.plugin_borrow_filter_component_class_by_name_const |
| 222 | ) |
| 223 | _borrow_component_class_by_index = staticmethod( |
| 224 | native_bt.plugin_borrow_filter_component_class_by_index_const |
| 225 | ) |
| 226 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
| 227 | |
| 228 | |
| 229 | class _PluginSinkComponentClasses(_PluginComponentClasses): |
| 230 | _component_class_count = staticmethod( |
| 231 | native_bt.plugin_get_sink_component_class_count |
| 232 | ) |
| 233 | _borrow_component_class_by_name = staticmethod( |
| 234 | native_bt.plugin_borrow_sink_component_class_by_name_const |
| 235 | ) |
| 236 | _borrow_component_class_by_index = staticmethod( |
| 237 | native_bt.plugin_borrow_sink_component_class_by_index_const |
| 238 | ) |
| 239 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
| 240 | |
| 241 | |
| 242 | class _Plugin(bt2_object._SharedObject): |
| 243 | @staticmethod |
| 244 | def _put_ref(ptr): |
| 245 | native_bt.plugin_put_ref(ptr) |
| 246 | |
| 247 | @staticmethod |
| 248 | def _get_ref(ptr): |
| 249 | native_bt.plugin_get_ref(ptr) |
| 250 | |
| 251 | @property |
| 252 | def name(self): |
| 253 | name = native_bt.plugin_get_name(self._ptr) |
| 254 | assert name is not None |
| 255 | return name |
| 256 | |
| 257 | @property |
| 258 | def author(self): |
| 259 | return native_bt.plugin_get_author(self._ptr) |
| 260 | |
| 261 | @property |
| 262 | def license(self): |
| 263 | return native_bt.plugin_get_license(self._ptr) |
| 264 | |
| 265 | @property |
| 266 | def description(self): |
| 267 | return native_bt.plugin_get_description(self._ptr) |
| 268 | |
| 269 | @property |
| 270 | def path(self): |
| 271 | return native_bt.plugin_get_path(self._ptr) |
| 272 | |
| 273 | @property |
| 274 | def version(self): |
| 275 | status, major, minor, patch, extra = native_bt.bt2_plugin_get_version(self._ptr) |
| 276 | |
| 277 | if status == native_bt.PROPERTY_AVAILABILITY_NOT_AVAILABLE: |
| 278 | return |
| 279 | |
| 280 | return _PluginVersion(major, minor, patch, extra) |
| 281 | |
| 282 | @property |
| 283 | def source_component_classes(self): |
| 284 | return _PluginSourceComponentClasses(self) |
| 285 | |
| 286 | @property |
| 287 | def filter_component_classes(self): |
| 288 | return _PluginFilterComponentClasses(self) |
| 289 | |
| 290 | @property |
| 291 | def sink_component_classes(self): |
| 292 | return _PluginSinkComponentClasses(self) |