"""中控交易所配置(hub_settings.json)。""" from __future__ import annotations import json import os from pathlib import Path DIR = Path(__file__).resolve().parent SETTINGS_PATH = DIR / "hub_settings.json" DEFAULT_EXCHANGES = [ { "id": "0", "key": "binance", "name": "币安 · crypto_monitor_binance", "agent_url": "http://127.0.0.1:15200", "flask_url": "http://127.0.0.1:5001", "review_url": "http://127.0.0.1:5001/records", "enabled": True, "capabilities": ["order", "key"], }, { "id": "1", "key": "okx", "name": "OKX · crypto_monitor_okx", "agent_url": "http://127.0.0.1:15201", "flask_url": "http://127.0.0.1:5004", "review_url": "http://127.0.0.1:5004/records", "enabled": False, "capabilities": ["order", "key"], }, { "id": "2", "key": "gate", "name": "Gate训练 · crypto_monitor_gate", "agent_url": "http://127.0.0.1:15202", "flask_url": "http://127.0.0.1:5000", "review_url": "http://127.0.0.1:5000/records", "enabled": True, "capabilities": ["order", "key"], }, { "id": "3", "key": "gate_bot", "name": "Gate趋势 · crypto_monitor_gate_bot", "agent_url": "http://127.0.0.1:15203", "flask_url": "http://127.0.0.1:5002", "review_url": "http://127.0.0.1:5002/records", "enabled": True, "capabilities": ["order", "trend"], }, ] def _ids_from_csv(raw: str | None) -> set[str]: if not raw or not str(raw).strip(): return set() return {x.strip() for x in str(raw).split(",") if x.strip()} def env_force_disabled_ids() -> set[str]: raw = os.getenv("HUB_DISABLED_IDS", "1").strip() return _ids_from_csv(raw) def load_settings() -> dict: data = {"exchanges": [dict(x) for x in DEFAULT_EXCHANGES], "version": 1} if SETTINGS_PATH.is_file(): try: loaded = json.loads(SETTINGS_PATH.read_text(encoding="utf-8")) if isinstance(loaded, dict) and isinstance(loaded.get("exchanges"), list): data = loaded except Exception: pass force_off = env_force_disabled_ids() for ex in data.get("exchanges") or []: if str(ex.get("id")) in force_off: ex["enabled"] = False ex["env_disabled"] = True else: ex.setdefault("env_disabled", False) return data def save_settings(data: dict) -> None: SETTINGS_PATH.write_text( json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8", ) def enabled_exchanges(data: dict | None = None) -> list[dict]: data = data or load_settings() return [x for x in data.get("exchanges") or [] if x.get("enabled")]