Files
monitor-shared-utils/monitor_utils/config.py
T
2026-06-19 10:18:23 -07:00

62 lines
1.6 KiB
Python

import json
from enum import Enum
from functools import cache
import pathlib
from pydantic_settings import BaseSettings, SettingsConfigDict
from monitor_utils.constants import PROXIES_PATH, PROJECT_ROOT
class DevelopmentMode(str, Enum):
PRODUCTION = "PRODUCTION"
DEVELOPMENT = "DEVELOPMENT"
class Settings(BaseSettings):
mongo_url: str
mongo_database_name: str
production_bot_token: str
profit_api_key: str
development_bot_token: str | None = None
logging_level: str = "INFO"
development_mode: DevelopmentMode = DevelopmentMode.PRODUCTION
model_config = SettingsConfigDict(env_file=(".env", ".env.local"), extra="ignore")
@property
def bot_token(self) -> str:
if self.development_mode == DevelopmentMode.PRODUCTION:
return self.production_bot_token
return self.development_bot_token
@cache
def get_settings() -> Settings:
return Settings()
@cache
def get_proxies() -> list[str]:
with PROXIES_PATH.open() as f:
proxies = [f"http://{p.strip()}" for p in f if p.strip()]
if not proxies:
raise ValueError("Proxy list cannot be empty")
return proxies
def get_config(
monitor_name: str, use_production_path: bool = False
) -> tuple[dict, pathlib.Path]:
monitor_name = monitor_name.replace(".json", "")
if use_production_path:
config_path = PROJECT_ROOT / "configs" / f"{monitor_name}.json"
else:
config_path = PROJECT_ROOT / "tests" / "configs" / f"{monitor_name}.json"
with open(config_path) as f:
return json.load(f), config_path