shaiwatcher/modules/common/settings.py
Franz Rolfsvaag 5368d21be4 0.3.9.2.a5
performance improvements, stability, and primarily settings-handling improvements.
  - Due to the container transition, some settings handling became quietly broken or defunct.
2025-08-10 20:23:09 +02:00

35 lines
1.0 KiB
Python

# modules/common/settings.py
import os
class ConfigView:
"""Unified read: ENV first, then optional bot.config['DEFAULT'], then fallback."""
def __init__(self, bot):
self._env = os.environ
self._default = {}
try:
self._default = getattr(bot, "config", {}).get("DEFAULT", {}) or {}
except Exception:
pass
def get(self, key: str, default: str = "") -> str:
v = self._env.get(key.upper(), "")
if not v:
v = self._default.get(key, "")
v = (v or "").strip().strip('"').strip("'")
return v if v else default
def int(self, key: str, default: int = 0) -> int:
try:
return int(self.get(key, ""))
except Exception:
return default
def bool(self, key: str, default: bool = False) -> bool:
v = self.get(key, "")
if not v:
return default
return v.lower() in ("1", "true", "yes", "on")
def cfg(bot) -> ConfigView:
return ConfigView(bot)