2022-11-12 00:56:06 -07:00
|
|
|
import os
|
2023-01-25 09:55:59 -07:00
|
|
|
import importlib.util
|
2022-11-12 00:56:06 -07:00
|
|
|
|
2023-05-31 10:56:37 -06:00
|
|
|
from modules import errors
|
2024-04-01 03:13:07 -06:00
|
|
|
import sys
|
2022-11-12 00:56:06 -07:00
|
|
|
|
2024-04-06 00:05:04 -06:00
|
|
|
|
|
|
|
loaded_scripts = {}
|
|
|
|
|
|
|
|
|
2022-11-12 00:56:06 -07:00
|
|
|
def load_module(path):
|
2024-04-15 20:14:19 -06:00
|
|
|
module_name, _ = os.path.splitext(os.path.basename(path))
|
|
|
|
full_module_name = "scripts." + module_name
|
|
|
|
module_spec = importlib.util.spec_from_file_location(full_module_name, path)
|
2023-01-25 09:55:59 -07:00
|
|
|
module = importlib.util.module_from_spec(module_spec)
|
|
|
|
module_spec.loader.exec_module(module)
|
2024-04-15 20:14:19 -06:00
|
|
|
loaded_scripts[full_module_name] = module
|
|
|
|
sys.modules[full_module_name] = module
|
2022-11-12 00:56:06 -07:00
|
|
|
return module
|
|
|
|
|
|
|
|
|
2023-07-18 11:11:30 -06:00
|
|
|
def preload_extensions(extensions_dir, parser, extension_list=None):
|
2022-11-12 00:56:06 -07:00
|
|
|
if not os.path.isdir(extensions_dir):
|
|
|
|
return
|
|
|
|
|
2023-07-18 11:11:30 -06:00
|
|
|
extensions = extension_list if extension_list is not None else os.listdir(extensions_dir)
|
|
|
|
for dirname in sorted(extensions):
|
2022-11-12 00:56:06 -07:00
|
|
|
preload_script = os.path.join(extensions_dir, dirname, "preload.py")
|
|
|
|
if not os.path.isfile(preload_script):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
module = load_module(preload_script)
|
|
|
|
if hasattr(module, 'preload'):
|
|
|
|
module.preload(parser)
|
|
|
|
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running preload() for {preload_script}", exc_info=True)
|