from __future__ import annotations import shutil from typing import Any, Dict, List from SYS.cmdlet_spec import Cmdlet from SYS.config import resolve_cookies_path from SYS import pipeline as ctx from SYS.result_table import Table from SYS.logger import set_debug, debug from cmdnat._status_shared import ( add_startup_check as _add_startup_check, collect_plugin_startup_checks as _collect_plugin_startup_checks, has_store_subtype as _has_store_subtype, ) CMDLET = Cmdlet( name=".status", summary="Check and display service/provider status", usage=".status", arg=[], ) def _run(result: Any, args: List[str], config: Dict[str, Any]) -> int: startup_table = Table( "*********************************************" ) startup_table._interactive(True)._perseverance(True) startup_table.set_value_case("upper") debug_enabled = bool(config.get("debug", False)) try: # Ensure global debug state follows config so HTTPClient and other helpers # emit debug-level information during the status check. set_debug(debug_enabled) except Exception: pass debug(f"Status check: debug_enabled={debug_enabled}") _add_startup_check(startup_table, "ENABLED" if debug_enabled else "DISABLED", "DEBUGGING") try: # MPV check try: from MPV.mpv_ipc import MPV MPV() mpv_path = shutil.which("mpv") _add_startup_check(startup_table, "ENABLED", "MPV", detail=mpv_path or "Available") debug(f"MPV check OK: path={mpv_path or 'Available'}") except Exception as exc: _add_startup_check(startup_table, "DISABLED", "MPV", detail=str(exc)) debug(f"MPV check failed: {exc}") # Store Registry store_registry = None try: from Store import Store as StoreRegistry store_registry = StoreRegistry(config=config, suppress_debug=True) try: backends = store_registry.list_backends() except Exception: backends = [] debug(f"StoreRegistry initialized. backends={backends}") except Exception as exc: debug(f"StoreRegistry initialization failed: {exc}") store_registry = None # Hydrus if _has_store_subtype(config, "hydrusnetwork"): hcfg = config.get("store", {}).get("hydrusnetwork", {}) for iname, icfg in hcfg.items(): if not isinstance(icfg, dict): continue nkey = str(icfg.get("NAME") or iname) uval = str(icfg.get("URL") or "").strip() debug(f"Hydrus network check: name={nkey}, url={uval}") ok = bool(store_registry and store_registry.is_available(nkey)) status = "ENABLED" if ok else "DISABLED" files = None detail = uval if ok and store_registry: try: backend = store_registry[nkey] files = getattr(backend, "total_count", None) if files is None and hasattr(backend, "get_total_count"): files = backend.get_total_count() debug(f"Hydrus backend '{nkey}' available: files={files}") except Exception as exc: debug(f"Hydrus backend '{nkey}' check failed: {exc}") else: err = store_registry.get_backend_error(iname) if store_registry else None debug(f"Hydrus backend '{nkey}' not available: {err}") detail = f"{uval} - {err or 'Unavailable'}" _add_startup_check(startup_table, status, nkey, store="hydrusnetwork", files=files, detail=detail) for check in _collect_plugin_startup_checks(config): _add_startup_check( startup_table, str(check.get("status") or "UNKNOWN"), str(check.get("name") or "Plugin"), provider=str(check.get("plugin") or ""), files=check.get("files"), detail=str(check.get("detail") or ""), ) # Cookies try: cf = resolve_cookies_path(config) _add_startup_check(startup_table, "FOUND" if cf else "MISSING", "Cookies", detail=str(cf) if cf else "Not found") debug(f"Cookies: resolved cookiefile={cf}") except Exception as exc: debug(f"Cookies check failed: {exc}") except Exception as exc: debug(f"Status check failed: {exc}") if startup_table.rows: # Mark as rendered to prevent CLI.py from auto-printing it to stdout # (avoiding duplication in TUI logs, while keeping it in TUI Results) setattr(startup_table, "_rendered_by_cmdlet", True) ctx.set_current_stage_table(startup_table) debug(f"Status check completed: {len(startup_table.rows)} checks recorded") return 0 CMDLET.exec = _run