File size: 4,311 Bytes
5f20dc6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import os
import json
from config import SETTINGS_FILE, DEFAULT_SETTINGS
# ---------- In-Memory Cache ----------
_settings = {}
# ---------- Load Settings ----------
from modules.storage import load_json, save_json
def load_settings():
"""Load settings from storage or use defaults"""
global _settings
saved = load_json(SETTINGS_FILE, default=None)
if saved is None:
_settings = DEFAULT_SETTINGS.copy()
save_settings()
print("Created default settings in storage")
else:
_settings = {**DEFAULT_SETTINGS, **saved}
if not isinstance(_settings.get('excluded_coins'), list):
_settings['excluded_coins'] = []
print("Loaded settings from storage")
return _settings
def save_settings():
"""Save current settings to storage"""
return save_json(SETTINGS_FILE, _settings)
# ---------- Get Settings ----------
def get_settings():
"""Get current settings"""
if not _settings:
load_settings()
return _settings.copy()
# ---------- Update Settings ----------
def update_settings(new_settings):
"""Update settings with validation for each type"""
global _settings
if not _settings:
load_settings()
validated = {}
for key, value in new_settings.items():
if key not in DEFAULT_SETTINGS:
continue
# Handle excluded_coins (list of strings)
if key == 'excluded_coins':
if isinstance(value, list):
validated[key] = [str(c) for c in value]
continue
# Handle auto_filter_bingx (boolean flag)
if key == 'auto_filter_bingx':
validated[key] = 1 if value else 0
continue
# Handle numeric settings
try:
validated[key] = int(float(value))
except (ValueError, TypeError):
print(f"⚠️ Invalid value for {key}: {value}, skipping")
_settings.update(validated)
save_settings()
print(f"✅ Settings updated: {list(validated.keys())}")
return _settings.copy()
# ---------- Reset Settings ----------
def reset_settings():
"""Reset settings to defaults (preserves excluded_coins)"""
global _settings
if not _settings:
load_settings()
excluded = _settings.get('excluded_coins', [])
_settings = {**DEFAULT_SETTINGS, "excluded_coins": excluded}
save_settings()
print("✅ Settings reset to defaults (exclusions preserved)")
return _settings.copy()
# ---------- Individual Getters ----------
def get_scan_min_market_cap():
return get_settings().get("scan_min_market_cap", DEFAULT_SETTINGS["scan_min_market_cap"])
def get_scan_min_volume():
return get_settings().get("scan_min_volume", DEFAULT_SETTINGS["scan_min_volume"])
def get_scan_max_coins():
return get_settings().get("scan_max_coins", DEFAULT_SETTINGS["scan_max_coins"])
def get_cache_ttl_hours():
return get_settings().get("coin_cache_ttl_hours", DEFAULT_SETTINGS["coin_cache_ttl_hours"])
def get_auto_filter_bingx():
"""Check if auto-filter by BingX availability is enabled"""
return bool(get_settings().get("auto_filter_bingx", 1))
def get_excluded_coins():
"""Get the list of excluded coin IDs"""
return get_settings().get("excluded_coins", [])
def add_excluded_coin(coin_id):
"""Add a coin to the exclusion list"""
global _settings
if not _settings:
load_settings()
excluded = _settings.get('excluded_coins', [])
if coin_id not in excluded:
excluded.append(coin_id)
_settings['excluded_coins'] = excluded
save_settings()
return True
return False
def remove_excluded_coin(coin_id):
"""Remove a coin from the exclusion list"""
global _settings
if not _settings:
load_settings()
excluded = _settings.get('excluded_coins', [])
if coin_id in excluded:
excluded.remove(coin_id)
_settings['excluded_coins'] = excluded
save_settings()
return True
return False
def set_excluded_coins(coin_ids):
"""Replace the entire exclusion list"""
global _settings
if not _settings:
load_settings()
_settings['excluded_coins'] = [str(c) for c in coin_ids]
save_settings()
return _settings['excluded_coins']
|