From 493886497b43731503a063a62d45426ab556f4df Mon Sep 17 00:00:00 2001 From: Diego Garcia Gangl Date: Tue, 2 Jan 2024 20:59:23 -0300 Subject: [PATCH] Replace imp with importlib Imp has been deprecated for a while and has recently been removed. --- GTG/core/plugins/engine.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/GTG/core/plugins/engine.py b/GTG/core/plugins/engine.py index 15794ade06..500462e26d 100644 --- a/GTG/core/plugins/engine.py +++ b/GTG/core/plugins/engine.py @@ -15,7 +15,9 @@ # You should have received a copy of the GNU General Public License along with # this program. If not, see . # ----------------------------------------------------------------------------- -import imp + +import importlib +import inspect import os import logging from gi.repository import GLib @@ -101,15 +103,17 @@ def _load_module(self, module_paths): """Load the module containing this plugin.""" try: # import the module containing the plugin - f, pathname, desc = imp.find_module(self.module_name, module_paths) - module = imp.load_module(self.module_name, f, pathname, desc) - # find the class object for the actual plugin - for key, item in module.__dict__.items(): - if isinstance(item, type): - self.plugin_class = item - self.class_name = item.__dict__['__module__'].split('.')[1] - break + spec = importlib.machinery.PathFinder().find_spec(self.module_name, module_paths) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + + classes = inspect.getmembers(mod, inspect.isclass) + + self.class_name = classes[0][0] + self.plugin_class = classes[0][1] + except ImportError as e: + print(e) # load_module() failed, probably because of a module dependency if len(self.module_depends) > 0: self._check_module_depends()