finance-telegram-bot/app/config.py
2026-07-14 12:21:12 +07:00

44 lines
1.5 KiB
Python
Raw Permalink 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.

from __future__ import annotations
import os
from dataclasses import dataclass
from datetime import time
from pathlib import Path
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
@dataclass(frozen=True, slots=True)
class Settings:
bot_token: str
database_path: Path
timezone: ZoneInfo
daily_report_time: time
log_level: str = "INFO"
@classmethod
def from_env(cls) -> "Settings":
token = os.getenv("BOT_TOKEN", "").strip()
if not token:
raise RuntimeError("Переменная BOT_TOKEN не задана")
timezone_name = os.getenv("BOT_TIMEZONE", "Asia/Novosibirsk").strip()
try:
timezone = ZoneInfo(timezone_name)
except ZoneInfoNotFoundError as exc:
raise RuntimeError(f"Неизвестная таймзона BOT_TIMEZONE: {timezone_name}") from exc
report_time_raw = os.getenv("DAILY_REPORT_TIME", "21:00").strip()
try:
hour, minute = (int(part) for part in report_time_raw.split(":", maxsplit=1))
report_time = time(hour=hour, minute=minute)
except (ValueError, TypeError) as exc:
raise RuntimeError("DAILY_REPORT_TIME должен иметь формат ЧЧ:ММ") from exc
return cls(
bot_token=token,
database_path=Path(os.getenv("DATABASE_PATH", "data/finance.db")),
timezone=timezone,
daily_report_time=report_time,
log_level=os.getenv("LOG_LEVEL", "INFO").upper(),
)