-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_loader.py
More file actions
81 lines (63 loc) · 2.16 KB
/
Copy pathenv_loader.py
File metadata and controls
81 lines (63 loc) · 2.16 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Helpers to load .env files and read typed environment variables."""
from __future__ import annotations
import os
from pathlib import Path
def load_env_file(path: Path) -> None:
"""Load KEY=VALUE pairs from a .env file into os.environ if missing."""
if not path.exists():
return
for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
if (
(value.startswith('"') and value.endswith('"'))
or (value.startswith("'") and value.endswith("'"))
):
value = value[1:-1]
if key:
os.environ.setdefault(key, value)
def get_env(name: str, default: str | None = None) -> str | None:
value = os.getenv(name)
if value is None or value.strip() == "":
return default
return value
def get_required_env(name: str) -> str:
value = get_env(name)
if value is None:
raise RuntimeError(f"Missing required environment variable: {name}")
return value
def get_int_env(name: str, default: int) -> int:
value = get_env(name)
if value is None:
return default
try:
return int(value)
except ValueError as exc:
raise RuntimeError(f"Environment variable {name} must be an integer") from exc
def get_float_env(name: str, default: float) -> float:
value = get_env(name)
if value is None:
return default
try:
return float(value)
except ValueError as exc:
raise RuntimeError(f"Environment variable {name} must be a float") from exc
def get_bool_env(name: str, default: bool) -> bool:
value = get_env(name)
if value is None:
return default
normalized = value.strip().lower()
if normalized in {"1", "true", "yes", "on"}:
return True
if normalized in {"0", "false", "no", "off"}:
return False
raise RuntimeError(
f"Environment variable {name} must be a boolean (true/false, 1/0, yes/no)"
)