TimberBot – commitdiff

You can use Git to clone the repository via the web URL. Download snapshot (zip)
Move from imp to importlib for Python >=3.12 compatibility
authorJulian Fietkau <git@fietkau.software>
Tue, 28 Mar 2023 23:27:59 +0000 (01:27 +0200)
committerJulian Fietkau <git@fietkau.software>
Tue, 28 Mar 2023 23:27:59 +0000 (01:27 +0200)
plugin_manager.py

index b3f291e9fbc8081027542da3897549f2fdf044df..8fd5fe3be510fafba70fefe95ea3ea43aeef201f 100644 (file)
@@ -8,7 +8,7 @@
 import codecs
 import configparser
 import datetime
-import imp
+import importlib.machinery
 import inspect
 import json
 import math
@@ -193,11 +193,13 @@ class PluginManager:
     def enable_plugin(self, plugin_name):
         plugin_file = os.path.join(self.plugin_path, plugin_name + '.py')
         if os.path.isfile(plugin_file):
-            fp, pathname, description = imp.find_module(plugin_name, [self.plugin_path])
             plugin_classes = []
             try:
-                plugin_module = imp.load_module(plugin_name, fp, pathname, description)
-                plugin_classes = [ c for c in inspect.getmembers(plugin_module, inspect.isclass) if c[1].__module__ == plugin_name]
+                loader = importlib.machinery.SourceFileLoader(plugin_name, plugin_file)
+                spec = importlib.util.spec_from_loader(loader.name, loader)
+                plugin_module = importlib.util.module_from_spec(spec)
+                loader.exec_module(plugin_module)
+                plugin_classes = [c for c in inspect.getmembers(plugin_module, inspect.isclass) if c[1].__module__ == plugin_name]
             except Exception as e:
                 print(e)
                 traceback.print_exception(type(e), e, e.__traceback__)