2024-07-26 02:58:19 +03:00

87 lines
2.0 KiB
Python

import os
from dataclasses import dataclass
@dataclass
class BotConfig:
token: str
skip_pending: bool
timeout: int
polling_timeout: int
num_threads: int
parse_mode: str
@classmethod
def from_env(cls):
return cls(os.getenv("BOT_TOKEN"),
bool(int(os.getenv("BOT_SKIP_PENDING", True))),
int(os.getenv("BOT_TIMEOUT", 20)),
int(os.getenv("BOT_POLLING_TIMEOUT", 20)),
int(os.getenv("BOT_NUM_THREADS", 2)),
os.getenv("BOT_PARSE_MODE", "html"))
@dataclass
class I18NConfig:
path: str
lang: str
fallback_lang: str
@classmethod
def from_env(cls):
return cls(os.getenv("I18N_PATH", "i18n.yaml"),
os.getenv("I18N_LANG", "en"),
os.getenv("I18N_FALLBACK_LANG", "en"))
@dataclass
class StateStorageConfig:
type: str
redis_host: str
redis_port: int
redis_db: int
redis_pass: str
@classmethod
def from_env(cls):
return cls(os.getenv("SS_TYPE"),
os.getenv("SS_REDIS_HOST"),
int(os.getenv("SS_REDIS_PORT", 6379)),
int(os.getenv("SS_REDIS_DB", 0)),
os.getenv("SS_REDIS_PASS"))
@dataclass
class DatabaseConfig:
url: str
@classmethod
def from_env(cls):
return cls(os.getenv("DATABASE_URL", "sqlite:///bot.db"))
@dataclass
class Config:
bot: BotConfig
i18n: I18NConfig
states: StateStorageConfig
database: DatabaseConfig
log_level: str
owner_id: int
@classmethod
def from_env(cls):
return cls(
bot=BotConfig.from_env(),
i18n=I18NConfig.from_env(),
states=StateStorageConfig.from_env(),
database=DatabaseConfig.from_env(),
log_level=os.getenv("LOG_LEVEL", "INFO"),
owner_id=int(os.getenv("OWNER_ID", 1)),
)
def load_config() -> Config:
return Config.from_env()