AutoRpa/rpa_oms/app/local_env.py

25 lines
852 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""从 rpa_oms/.env 读本机配置。该文件已 gitignore,不覆盖已有环境变量。"""
from __future__ import annotations
import os
from pathlib import Path
_ENV_PATH = Path(__file__).resolve().parents[1] / ".env"
def load_local_env(path: Path | None = None) -> None:
env_path = path if path is not None else _ENV_PATH
if not env_path.is_file():
return
for raw in env_path.read_text(encoding="utf-8").splitlines():
line = raw.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
key = key.strip()
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in {'"', "'"}:
value = value[1:-1]
if key and key not in os.environ:
os.environ[key] = value