diff --git a/CLI.py b/CLI.py index 5164155..25db071 100644 --- a/CLI.py +++ b/CLI.py @@ -571,7 +571,7 @@ class ConfigLoader: def load(self) -> Dict[str, Any]: try: - return deepcopy(load_config(config_dir=self._root)) + return deepcopy(load_config()) except Exception: return {} diff --git a/MPV/pipeline_helper.py b/MPV/pipeline_helper.py index 0d686ad..8ded2c2 100644 --- a/MPV/pipeline_helper.py +++ b/MPV/pipeline_helper.py @@ -275,7 +275,7 @@ def _run_op(op: str, data: Any) -> Dict[str, Any]: from Store import Store # noqa: WPS433 config_root = _runtime_config_root() - cfg = reload_config(config_dir=config_root) + cfg = reload_config() storage = Store(config=cfg, suppress_debug=True) backends = storage.list_backends() or [] diff --git a/SYS/config.py b/SYS/config.py index a56d944..670713a 100644 --- a/SYS/config.py +++ b/SYS/config.py @@ -13,10 +13,9 @@ from SYS.logger import log from SYS.utils import expand_path from SYS.database import db, get_config_all, save_config_value -DEFAULT_CONFIG_FILENAME = "config.conf" SCRIPT_DIR = Path(__file__).resolve().parent -_CONFIG_CACHE: Dict[str, Dict[str, Any]] = {} +_CONFIG_CACHE: Dict[str, Any] = {} _CONFIG_SAVE_MAX_RETRIES = 5 _CONFIG_SAVE_RETRY_DELAY = 0.15 @@ -41,14 +40,8 @@ def global_config() -> List[Dict[str, Any]]: def clear_config_cache() -> None: """Clear the configuration cache.""" - _CONFIG_CACHE.clear() - - -def _make_cache_key(config_dir: Optional[Path], filename: str, actual_path: Optional[Path]) -> str: - if actual_path: - return str(actual_path.resolve()) - base_dir = config_dir or SCRIPT_DIR - return str((base_dir / filename).resolve()) + global _CONFIG_CACHE + _CONFIG_CACHE = {} def get_hydrus_instance( @@ -341,44 +334,26 @@ def resolve_debug_log(config: Dict[str, Any]) -> Optional[Path]: return path -def load_config( - config_dir: Optional[Path] = None, filename: str = DEFAULT_CONFIG_FILENAME -) -> Dict[str, Any]: - base_dir = config_dir or SCRIPT_DIR - config_path = base_dir / filename - cache_key = _make_cache_key(config_dir, filename, config_path) - - if cache_key in _CONFIG_CACHE: - return _CONFIG_CACHE[cache_key] +def load_config() -> Dict[str, Any]: + global _CONFIG_CACHE + if _CONFIG_CACHE: + return _CONFIG_CACHE - # Load from database + # Load strictly from database db_config = get_config_all() if db_config: - _CONFIG_CACHE[cache_key] = db_config + _CONFIG_CACHE = db_config return db_config return {} -def reload_config( - config_dir: Optional[Path] = None, filename: str = DEFAULT_CONFIG_FILENAME -) -> Dict[str, Any]: - base_dir = config_dir or SCRIPT_DIR - config_path = base_dir / filename - cache_key = _make_cache_key(config_dir, filename, config_path) - _CONFIG_CACHE.pop(cache_key, None) - return load_config(config_dir=config_dir, filename=filename) +def reload_config() -> Dict[str, Any]: + clear_config_cache() + return load_config() - -def save_config( - config: Dict[str, Any], - config_dir: Optional[Path] = None, - filename: str = DEFAULT_CONFIG_FILENAME, -) -> int: - base_dir = config_dir or SCRIPT_DIR - config_path = base_dir / filename - +def save_config(config: Dict[str, Any]) -> int: def _write_entries() -> int: count = 0 with db.transaction(): @@ -409,26 +384,24 @@ def save_config( while True: try: saved_entries = _write_entries() - log(f"Saved {saved_entries} configuration entries to database.") + log(f"Synced {saved_entries} entries to {db.db_path}") break except sqlite3.OperationalError as exc: attempts += 1 locked_error = "locked" in str(exc).lower() if not locked_error or attempts >= _CONFIG_SAVE_MAX_RETRIES: - log(f"CRITICAL: Failed to save config to database: {exc}") + log(f"CRITICAL: Database write failed: {exc}") raise delay = _CONFIG_SAVE_RETRY_DELAY * attempts - log( - f"Database busy locking medios.db (attempt {attempts}/{_CONFIG_SAVE_MAX_RETRIES}); retrying in {delay:.2f}s." - ) + log(f"Database locked; retry {attempts}/{_CONFIG_SAVE_MAX_RETRIES} in {delay:.2f}s") time.sleep(delay) except Exception as exc: - log(f"CRITICAL: Failed to save config to database: {exc}") + log(f"CRITICAL: Configuration save failed: {exc}") raise - cache_key = _make_cache_key(config_dir, filename, config_path) clear_config_cache() - _CONFIG_CACHE[cache_key] = config + global _CONFIG_CACHE + _CONFIG_CACHE = config return saved_entries diff --git a/SYS/database.py b/SYS/database.py index 5676601..4221b98 100644 --- a/SYS/database.py +++ b/SYS/database.py @@ -263,11 +263,6 @@ def save_config_value(category: str, subtype: str, item_name: str, key: str, val def get_config_all() -> Dict[str, Any]: """Retrieve all configuration from the database in the legacy dict format.""" - try: - db.execute("DELETE FROM config WHERE category='store' AND LOWER(subtype) in ('folder', 'zerotier')") - db.execute("DELETE FROM config WHERE category='networking'") - except Exception: - pass rows = db.fetchall("SELECT category, subtype, item_name, key, value FROM config") config: Dict[str, Any] = {}