"""Load simple project-local environment files without overriding the process environment.""" from __future__ import annotations import os import re from pathlib import Path _ENV_KEY_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$") def load_project_env(path: Path) -> None: """Load KEY=VALUE entries from a project-local env file if it exists.""" if not path.is_file(): return try: lines = path.read_text(encoding="utf-8-sig").splitlines() except OSError: return for raw_line in lines: line = raw_line.strip() if not line or line.startswith("#"): continue if line.startswith("export "): line = line[7:].lstrip() key, separator, value = line.partition("=") key = key.strip() if not separator or not _ENV_KEY_RE.fullmatch(key): continue value = value.strip() if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}: value = value[1:-1] os.environ.setdefault(key, value)