Spaces:
Running
Running
File size: 916 Bytes
9d60e8e aa7b4a6 9d60e8e aa7b4a6 9d60e8e aa7b4a6 9d60e8e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Load executable scripts for isolated offline unit tests."""
from __future__ import annotations
import importlib.util
from pathlib import Path
import sys
from types import ModuleType
def load_script_module(name: str) -> ModuleType:
path = Path(__file__).parents[1] / "scripts" / f"{name}.py"
module_name = f"doc_inspector_script_{name}"
spec = importlib.util.spec_from_file_location(module_name, path)
if spec is None or spec.loader is None:
raise RuntimeError(f"無法載入 script:{name}")
module = importlib.util.module_from_spec(spec)
# Registering before execution lets dataclasses resolve the postponed
# annotations that `from __future__ import annotations` leaves as strings.
sys.modules[module_name] = module
try:
spec.loader.exec_module(module)
except BaseException:
sys.modules.pop(module_name, None)
raise
return module
|