-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
79 lines (62 loc) · 2.41 KB
/
Copy pathconfig.py
File metadata and controls
79 lines (62 loc) · 2.41 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
from __future__ import annotations
import os
from dataclasses import dataclass
from dotenv import load_dotenv
def _parse_int(name: str, raw: str | None, *, default: int) -> int:
if raw is None or raw.strip() == "":
return default
try:
return int(raw.strip())
except ValueError as e:
raise ValueError(f"{name} must be an int, got: {raw!r}") from e
def _parse_bool(name: str, raw: str | None, *, default: bool) -> bool:
if raw is None or raw.strip() == "":
return default
v = raw.strip().lower()
truthy = {"1", "true", "t", "yes", "y", "on"}
falsy = {"0", "false", "f", "no", "n", "off"}
if v in truthy:
return True
if v in falsy:
return False
raise ValueError(f"{name} must be a boolean-like value, got: {raw!r}")
@dataclass(frozen=True)
class Config:
leetcode_username: str
min_problems_per_day: int
processes: list[str]
poll_interval_seconds: int
cache_ttl_seconds: int
fail_open_on_api_error: bool
quotes: list[str]
def load(env_path: str = ".env") -> Config:
load_dotenv(dotenv_path=env_path, override=False)
leetcode_username = os.getenv("LEETCODE_USERNAME", "").strip()
if not leetcode_username:
raise ValueError("LEETCODE_USERNAME must be set in .env")
raw_processes = os.getenv("PROCESSES", "").strip()
if not raw_processes:
raise ValueError("PROCESSES must be set in .env (comma-separated list)")
processes = [p.strip() for p in raw_processes.split(",") if p.strip()]
if not processes:
raise ValueError("PROCESSES parsed to an empty list; check comma-separated values")
quotes = os.getenv("QUOTES", "").split("\n")
if not quotes:
raise ValueError("QUOTES must be set in .env (newline-separated list)")
return Config(
leetcode_username=leetcode_username,
min_problems_per_day=_parse_int(
"MIN_PROBLEMS_PER_DAY", os.getenv("MIN_PROBLEMS_PER_DAY"), default=1
),
processes=processes,
poll_interval_seconds=_parse_int(
"POLL_INTERVAL_SECONDS", os.getenv("POLL_INTERVAL_SECONDS"), default=5
),
cache_ttl_seconds=_parse_int(
"CACHE_TTL_SECONDS", os.getenv("CACHE_TTL_SECONDS"), default=60
),
fail_open_on_api_error=_parse_bool(
"FAIL_OPEN_ON_API_ERROR", os.getenv("FAIL_OPEN_ON_API_ERROR"), default=True
),
quotes=quotes,
)