"""Storage path helpers — multi-instance via BOT_INSTANCE_ID env var. Without BOT_INSTANCE_ID (default): db_path("prefix.db") -> bot/db/prefix.db jsondb_path("x.json") -> bot/jsondb/x.json With BOT_INSTANCE_ID=prod-a: db_path("prefix.db") -> bot/db/prod-a/prefix.db jsondb_path("x.json") -> bot/db/prod-a/jsondb/x.json """ from __future__ import annotations import os from pathlib import Path _BOT_ROOT = Path(__file__).resolve().parent.parent _INSTANCE_ID = os.getenv("BOT_INSTANCE_ID", "").strip() def _instance_db_dir() -> Path: base = _BOT_ROOT / "db" if _INSTANCE_ID: base = base / _INSTANCE_ID base.mkdir(parents=True, exist_ok=True) return base def db_path(filename: str) -> str: """SQLite and other files under bot/db/ (or bot/db//).""" return str(_instance_db_dir() / filename) def jsondb_path(filename: str) -> str: """JSON config files — bot/jsondb/ or bot/db//jsondb/.""" if _INSTANCE_ID: base = _instance_db_dir() / "jsondb" else: base = _BOT_ROOT / "jsondb" base.mkdir(parents=True, exist_ok=True) return str(base / filename) def bot_root_json_path(filename: str) -> str: """Legacy JSON at bot/ (e.g. ignore.json) — isolated per instance when set.""" if _INSTANCE_ID: return jsondb_path(filename) return str(_BOT_ROOT / filename) def resolve_db_path(path: str) -> str: """Normalize legacy 'db/file.db' strings (safety net for API layer).""" if path.startswith("db/"): return db_path(path[3:]) if path.startswith("jsondb/"): return jsondb_path(path[7:]) return path