from __future__ import annotations import os from pathlib import Path ROOT_DIR = Path(__file__).resolve().parents[1] DOTENV_PATH = ROOT_DIR / ".env" def _parse_env_value(raw_value: str) -> str: value = raw_value.strip() if not value: return "" if value[:1] == value[-1:] and value[:1] in {"'", '"'} and len(value) >= 2: value = value[1:-1] return value def load_project_env() -> None: if not DOTENV_PATH.exists(): return try: content = DOTENV_PATH.read_text(encoding="utf-8") except Exception: return for raw_line in content.splitlines(): line = raw_line.strip() if not line or line.startswith("#"): continue if line.startswith("export "): line = line[len("export ") :].lstrip() if "=" not in line: continue key, value = line.split("=", 1) key = key.strip() if not key or key in os.environ: continue comment_index = value.find(" #") if comment_index != -1 and value[:1] not in {"'", '"'}: value = value[:comment_index] os.environ[key] = _parse_env_value(value) load_project_env()