Restructuration de l'application
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
|
||||
|
||||
def _user_config_path() -> Path | None:
|
||||
explicit = os.environ.get("COPIENATOR_CONFIG", "").strip()
|
||||
if explicit:
|
||||
return Path(explicit).expanduser().resolve()
|
||||
local = Path.cwd() / "config.py"
|
||||
return local.resolve() if local.is_file() else None
|
||||
|
||||
|
||||
def _load_user_config(path: Path) -> ModuleType:
|
||||
if not path.is_file():
|
||||
raise FileNotFoundError(f"Copienator configuration not found: {path}")
|
||||
spec = importlib.util.spec_from_file_location("_copienator_user_config", path)
|
||||
if spec is None or spec.loader is None:
|
||||
raise ImportError(f"Could not load Copienator configuration: {path}")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
CONFIG_PATH = _user_config_path()
|
||||
if CONFIG_PATH is None:
|
||||
import default_config as _configuration
|
||||
else:
|
||||
_configuration = _load_user_config(CONFIG_PATH)
|
||||
|
||||
for _name in dir(_configuration):
|
||||
if not _name.startswith("_"):
|
||||
globals()[_name] = getattr(_configuration, _name)
|
||||
Reference in New Issue
Block a user