from __future__ import annotations import os from dataclasses import dataclass from pathlib import Path from dotenv import load_dotenv DEFAULT_COLLECTIONS = { "Core": "3521297585", "Content": "3521319712", "Cosmetics": "3637541646", } def _as_bool(value: str | None, default: bool) -> bool: if value is None: return default return value.strip().lower() in {"1", "true", "yes", "on"} def _collection_id(value: str | None, fallback: str) -> str: """Accept either a raw Workshop id or one of the existing URL settings.""" if not value: return fallback digits = "".join(character for character in value if character.isdigit()) return digits or fallback @dataclass(frozen=True) class Settings: host: str port: int database_path: Path steamcmd_path: str workshop_content_dir: Path steamcmd_batch_size: int steamcmd_timeout_seconds: int refresh_interval_seconds: int refresh_on_startup: bool game_version: str admin_token: str | None collections: dict[str, str] static_dir: Path @classmethod def load(cls, root: Path | None = None) -> Settings: root = (root or Path.cwd()).resolve() load_dotenv(root / ".env") default_steamcmd = ".steamcmd/steamcmd.exe" if os.name == "nt" else "steamcmd" return cls( host=os.getenv("PROGRESSION_HOST", "127.0.0.1"), port=int(os.getenv("PROGRESSION_PORT", "8000")), database_path=( root / os.getenv("PROGRESSION_DB_PATH", ".data/progression.db") ).resolve(), steamcmd_path=os.getenv("STEAMCMD_PATH", default_steamcmd), workshop_content_dir=( root / os.getenv( "STEAMCMD_WORKSHOP_DIR", ".steamcmd/steamapps/workshop/content/294100", ) ).resolve(), steamcmd_batch_size=max(1, int(os.getenv("STEAMCMD_BATCH_SIZE", "25"))), steamcmd_timeout_seconds=max( 60, int(os.getenv("STEAMCMD_TIMEOUT_SECONDS", "1800")) ), refresh_interval_seconds=max( 60, int(os.getenv("REFRESH_INTERVAL_SECONDS", "3600")) ), refresh_on_startup=_as_bool(os.getenv("REFRESH_ON_STARTUP"), True), game_version=os.getenv("MODLIST_GAME_VERSION", "1.6").strip() or "1.6", admin_token=os.getenv("PROGRESSION_ADMIN_TOKEN") or None, collections={ "Core": _collection_id( os.getenv("CORE_COLLECTION_URL"), DEFAULT_COLLECTIONS["Core"] ), "Content": _collection_id( os.getenv("CONTENT_COLLECTION_URL"), DEFAULT_COLLECTIONS["Content"] ), "Cosmetics": _collection_id( os.getenv("COSMETICS_COLLECTION_URL"), DEFAULT_COLLECTIONS["Cosmetics"], ), }, static_dir=root / "progression_backend" / "static", )