import json import os # User config file (not tracked by git) CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json") DEFAULT_CONFIG = { "civitai": { "proxy": None, "timeout_seconds": 30, } } def load_config() -> dict: """Load user config from config.json, fall back to defaults.""" if not os.path.exists(CONFIG_PATH): return DEFAULT_CONFIG.copy() try: with open(CONFIG_PATH, "r", encoding="utf-8") as f: user_config = json.load(f) config = DEFAULT_CONFIG.copy() if "civitai" in user_config: config["civitai"].update(user_config["civitai"]) return config except (json.JSONDecodeError, OSError): return DEFAULT_CONFIG.copy() def get_proxy_url() -> str | None: """Get proxy URL string (e.g. 'http://127.0.0.1:7890') or None. Priority: 1. config.json 'civitai.proxy' (explicit user setting) 2. HTTP_PROXY / HTTPS_PROXY environment variables 3. No proxy """ config = load_config() proxy = config.get("civitai", {}).get("proxy") if proxy is not None: return str(proxy) if proxy else None # Fallback to environment variables https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy") if https_proxy: return https_proxy http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy") if http_proxy: return http_proxy return None def get_timeout() -> int: """Get request timeout in seconds.""" config = load_config() return config.get("civitai", {}).get("timeout_seconds", 30)