catBox-Docker / catbox /env_loader.py
sivaratrisrinivas
feat: polish Catbox observation flow
f84aefe
Raw
History Blame Contribute Delete
960 Bytes
from __future__ import annotations
import os
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
DEFAULT_ENV_FILE = ROOT / ".env"
def load_env_file(env_file: str | Path = DEFAULT_ENV_FILE) -> None:
path = Path(env_file)
if not path.is_file():
return
for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("export "):
line = line[len("export ") :].strip()
key, separator, value = line.partition("=")
if not separator:
continue
key = key.strip()
if not key or key in os.environ:
continue
os.environ[key] = _unquote_env_value(value.strip())
def _unquote_env_value(value: str) -> str:
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
return value[1:-1]
return value