Make pluginloader use importlib instead imp

Python 3.12 has removed imp and it's recommended to use importlib
instead. Python 2.7 doesn't have importlib, so Python 2.7 support is
ceased (not a big deal since it's been more than 3 years since it was
EOLed) as a part of this change.
This commit is contained in:
Ekin Dursun
2023-11-11 21:09:50 +03:00
parent 0d0e90d328
commit a3d0562737
2 changed files with 13 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
import os
import imp
import importlib.machinery
import importlib.util
PluginFolder = os.path.join(os.path.dirname(os.path.realpath(__file__)),"..","plugins")
MainModule = "__init__"
@@ -11,9 +12,12 @@ def get_plugin(name, plugin_path):
location = os.path.join(dir, name)
if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
continue
info = imp.find_module(MainModule, [location])
return {"name": name, "info": info, "path": location}
spec = importlib.machinery.PathFinder.find_spec(MainModule, [location])
return {"name": name, "spec": spec, "path": location}
raise Exception("Could not find plugin with name " + name)
def load_plugin(plugin):
return imp.load_module(MainModule, *plugin["info"])
spec = plugin["spec"]
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module