forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_loader.py
More file actions
executable file
·36 lines (27 loc) · 1.05 KB
/
env_loader.py
File metadata and controls
executable file
·36 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Environment loading utilities for root-level vars interpolation."""
import os
from pathlib import Path
from typing import Dict
_DOTENV_LOADED = False
def load_dotenv_file(dotenv_path: Path | None = None) -> None:
"""Populate ``os.environ`` with key/value pairs from a .env file once per process."""
global _DOTENV_LOADED
if _DOTENV_LOADED:
return
path = dotenv_path or Path(".env")
if path.exists():
for line in path.read_text(encoding="utf-8").splitlines():
stripped = line.strip()
if not stripped or stripped.startswith("#"):
continue
if "=" not in stripped:
continue
key, value = stripped.split("=", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
os.environ.setdefault(key, value)
_DOTENV_LOADED = True
def build_env_var_map(extra_vars: Dict[str, str] | None = None) -> Dict[str, str]:
merged: Dict[str, str] = dict(os.environ)
merged.update(extra_vars or {})
return merged