Files
Fast-Export/pluginloader/__init__.py
Ekin Dursun a3d0562737 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.
2023-11-12 20:41:43 +03:00

24 lines
854 B
Python

import os
import importlib.machinery
import importlib.util
PluginFolder = os.path.join(os.path.dirname(os.path.realpath(__file__)),"..","plugins")
MainModule = "__init__"
def get_plugin(name, plugin_path):
search_dirs = [PluginFolder]
if plugin_path:
search_dirs = [plugin_path] + search_dirs
for dir in search_dirs:
location = os.path.join(dir, name)
if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
continue
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):
spec = plugin["spec"]
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module