stable-diffusion-webui/modules/script_loading.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

39 lines
1.2 KiB
Python
Raw Normal View History

import os
import importlib.util
from modules import errors
import sys
loaded_scripts = {}
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)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
2024-04-15 20:34:01 -06:00
loaded_scripts[path] = module
2024-04-15 20:14:19 -06:00
sys.modules[full_module_name] = module
return module
def preload_extensions(extensions_dir, parser, extension_list=None):
if not os.path.isdir(extensions_dir):
return
extensions = extension_list if extension_list is not None else os.listdir(extensions_dir)
for dirname in sorted(extensions):
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:
errors.report(f"Error running preload() for {preload_script}", exc_info=True)