script_loading.py 992 B

12345678910111213141516171819202122232425262728293031
  1. import os
  2. import importlib.util
  3. from modules import errors
  4. def load_module(path):
  5. module_spec = importlib.util.spec_from_file_location(os.path.basename(path), path)
  6. module = importlib.util.module_from_spec(module_spec)
  7. module_spec.loader.exec_module(module)
  8. return module
  9. def preload_extensions(extensions_dir, parser, extension_list=None):
  10. if not os.path.isdir(extensions_dir):
  11. return
  12. extensions = extension_list if extension_list is not None else os.listdir(extensions_dir)
  13. for dirname in sorted(extensions):
  14. preload_script = os.path.join(extensions_dir, dirname, "preload.py")
  15. if not os.path.isfile(preload_script):
  16. continue
  17. try:
  18. module = load_module(preload_script)
  19. if hasattr(module, 'preload'):
  20. module.preload(parser)
  21. except Exception:
  22. errors.report(f"Error running preload() for {preload_script}", exc_info=True)