| 1 | """Lab settings come from the environment or the repository's untracked `.env`.""" |
| 2 | import os |
| 3 | from pathlib import Path |
| 4 | |
| 5 | ROOT = Path(__file__).resolve().parents[2] |
| 6 | |
| 7 | |
| 8 | def setting(name): |
| 9 | path = ROOT / ".env" |
| 10 | if path.is_file(): |
| 11 | for line in path.read_text().splitlines(): |
| 12 | line = line.strip() |
| 13 | if not line or line.startswith("#") or "=" not in line: |
| 14 | continue |
| 15 | key, value = line.split("=", 1) |
| 16 | os.environ.setdefault(key.strip(), value.strip().strip('"')) |
| 17 | return os.environ.get(name) |
| 18 | |
| 19 | |
| 20 | def require(name): |
| 21 | value = setting(name) |
| 22 | if not value: |
| 23 | raise SystemExit("Set %s in %s (see .env.example)" % (name, ROOT / ".env")) |
| 24 | return value |