improve KNOWN_OUTPUT_OVERRIDE loader

This commit is contained in:
JerryXiao 2021-02-11 11:02:09 +08:00
parent 716b695d7b
commit d49fba9196
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA

View file

@ -1,6 +1,7 @@
import json import json
from pathlib import Path from pathlib import Path
import importlib.util import importlib.util
import sys
from typing import Any from typing import Any
CONFIG_DIR = Path('/etc/pacroller') CONFIG_DIR = Path('/etc/pacroller')
@ -19,13 +20,16 @@ if (cfg := (CONFIG_DIR / CONFIG_FILE)).exists():
else: else:
_config = dict() _config = dict()
def _import_module(fpath: str) -> Any: def _import_module(fpath: Path) -> Any:
spec = importlib.util.spec_from_file_location(fpath.removesuffix('.py').replace('/', '.'), fpath) spec = importlib.util.spec_from_file_location(str(fpath).removesuffix('.py').replace('/', '.'), fpath)
mod = importlib.util.module_from_spec(spec) mod = importlib.util.module_from_spec(spec)
_wbc = sys.dont_write_bytecode
sys.dont_write_bytecode = True
spec.loader.exec_module(mod) spec.loader.exec_module(mod)
sys.dont_write_bytecode = _wbc
return mod return mod
if (_komf := (CONFIG_DIR / F_KNOWN_OUTPUT_OVERRIDE)).exists(): if (_komf := (CONFIG_DIR / F_KNOWN_OUTPUT_OVERRIDE)).exists():
_kom = _import_module(str(_komf.resolve())) _kom = _import_module(_komf.resolve())
KNOWN_OUTPUT_OVERRIDE = (_kom.KNOWN_HOOK_OUTPUT, _kom.KNOWN_PACKAGE_OUTPUT) KNOWN_OUTPUT_OVERRIDE = (_kom.KNOWN_HOOK_OUTPUT, _kom.KNOWN_PACKAGE_OUTPUT)
else: else:
KNOWN_OUTPUT_OVERRIDE = (dict(), dict()) KNOWN_OUTPUT_OVERRIDE = (dict(), dict())