cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / bindings / python / bt2 / bt2 / py_plugin.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
55bb57e0
PP
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
55bb57e0 4
9105c467
PP
5import sys
6
5995b304
SM
7from bt2 import utils as bt2_utils
8from bt2 import component as bt2_component
9105c467
PP
9
10# Python plugin path to `_PluginInfo` (cache)
11_plugin_infos = {}
55bb57e0
PP
12
13
14def plugin_component_class(component_class):
3fb99a22 15 if not issubclass(component_class, bt2_component._UserComponent):
f5567ea8 16 raise TypeError("component class is not a subclass of a user component class")
55bb57e0
PP
17
18 component_class._bt_plugin_component_class = None
19 return component_class
20
21
cfbd7cf3
FD
22def register_plugin(
23 module_name, name, description=None, author=None, license=None, version=None
24):
55bb57e0 25 if module_name not in sys.modules:
cfbd7cf3
FD
26 raise RuntimeError(
27 "cannot find module '{}' in loaded modules".format(module_name)
28 )
55bb57e0 29
e5914347 30 bt2_utils._check_str(name)
55bb57e0
PP
31
32 if description is not None:
e5914347 33 bt2_utils._check_str(description)
55bb57e0
PP
34
35 if author is not None:
e5914347 36 bt2_utils._check_str(author)
55bb57e0
PP
37
38 if license is not None:
e5914347 39 bt2_utils._check_str(license)
55bb57e0
PP
40
41 if version is not None:
42 if not _validate_version(version):
cfbd7cf3 43 raise ValueError(
f5567ea8 44 "wrong version: expecting a tuple: (major, minor, patch) or (major, minor, patch, extra)"
cfbd7cf3 45 )
55bb57e0 46
cfbd7cf3
FD
47 sys.modules[module_name]._bt_plugin_info = _PluginInfo(
48 name, description, author, license, version
49 )
55bb57e0
PP
50
51
52def _validate_version(version):
53 if version is None:
54 return True
55
56 if not isinstance(version, tuple):
57 return False
58
59 if len(version) < 3 or len(version) > 4:
60 return False
61
62 if not isinstance(version[0], int):
63 return False
64
65 if not isinstance(version[1], int):
66 return False
67
68 if not isinstance(version[2], int):
69 return False
70
71 if len(version) == 4:
72 if not isinstance(version[3], str):
73 return False
74
75 return True
76
77
78class _PluginInfo:
79 def __init__(self, name, description, author, license, version):
80 self.name = name
81 self.description = description
82 self.author = author
83 self.license = license
84 self.version = version
85 self.comp_class_addrs = None
86
87
88# called by the BT plugin system
89def _try_load_plugin_module(path):
9105c467
PP
90 if path in _plugin_infos:
91 # do not load module and create plugin info twice for this path
92 return _plugin_infos[path]
93
55bb57e0 94 import hashlib
5995b304
SM
95 import inspect
96 import importlib.machinery
55bb57e0
PP
97
98 if path is None:
f5567ea8 99 raise TypeError("missing path")
55bb57e0
PP
100
101 # In order to load the module uniquely from its path, even from
102 # different files which have the same basename, we hash the path
103 # and prefix with `bt_plugin_`. This is its key in sys.modules.
104 h = hashlib.sha256()
105 h.update(path.encode())
f5567ea8 106 module_name = "bt_plugin_{}".format(h.hexdigest())
9105c467 107 assert module_name not in sys.modules
69666e08 108
e7401568 109 # try loading the module: any raised exception is caught by the caller
69666e08
MJ
110 if sys.version_info < (3, 5):
111 mod = importlib.machinery.SourceFileLoader(module_name, path).load_module()
112 else:
113 import importlib.util
114
115 loader = importlib.machinery.SourceFileLoader(module_name, path)
116 spec = importlib.util.spec_from_file_location(module_name, path, loader=loader)
117 mod = importlib.util.module_from_spec(spec)
118 sys.modules[mod.__name__] = mod
119 loader.exec_module(mod)
55bb57e0
PP
120
121 # we have the module: look for its plugin info first
f5567ea8 122 if not hasattr(mod, "_bt_plugin_info"):
55bb57e0
PP
123 raise RuntimeError("missing '_bt_plugin_info' module attribute")
124
125 plugin_info = mod._bt_plugin_info
126
127 # search for user component classes
128 def is_user_comp_class(obj):
129 if not inspect.isclass(obj):
130 return False
131
f5567ea8 132 if not hasattr(obj, "_bt_plugin_component_class"):
55bb57e0
PP
133 return False
134
135 return True
136
137 comp_class_entries = inspect.getmembers(mod, is_user_comp_class)
138 plugin_info.comp_class_addrs = [entry[1].addr for entry in comp_class_entries]
9105c467 139 _plugin_infos[path] = plugin_info
55bb57e0 140 return plugin_info
This page took 0.112856 seconds and 5 git commands to generate.