2022-11-12 10:56:06 +03:00
|
|
|
import os
|
2023-01-25 19:55:59 +03:00
|
|
|
import importlib.util
|
2022-11-12 10:56:06 +03:00
|
|
|
|
2023-05-31 19:56:37 +03:00
|
|
|
from modules import errors
|
2023-05-29 08:54:13 +03:00
|
|
|
|
2022-11-12 10:56:06 +03:00
|
|
|
|
|
|
|
def load_module(path):
|
2023-01-25 19:55:59 +03:00
|
|
|
module_spec = importlib.util.spec_from_file_location(os.path.basename(path), path)
|
|
|
|
module = importlib.util.module_from_spec(module_spec)
|
|
|
|
module_spec.loader.exec_module(module)
|
2022-11-12 10:56:06 +03:00
|
|
|
|
|
|
|
return module
|
|
|
|
|
|
|
|
|
|
|
|
def preload_extensions(extensions_dir, parser):
|
|
|
|
if not os.path.isdir(extensions_dir):
|
|
|
|
return
|
|
|
|
|
|
|
|
for dirname in sorted(os.listdir(extensions_dir)):
|
|
|
|
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 19:56:37 +03:00
|
|
|
errors.report(f"Error running preload() for {preload_script}", exc_info=True)
|