chore: refactor imports and adopt new utils utilities

This commit is contained in:
sankalp committed 2026-07-14 18:45:52 +05:45
1 parent b2114f52b1
commit 6f2d0bf444
65 files changed
+221 -266

No files matched your search

View File
Whitespace-only changes.
+23 -43
View File
@@ -1,7 +1,6 @@
import importlib
import importlib.util
import inspect
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
@@ -237,53 +236,34 @@ class PluginLoader:
def _create_venv(self, venv_dir: Path) -> bool:
"""Create a virtualenv using uv."""
try:
result = subprocess.run(
["uv", "venv", str(venv_dir)],
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
logger.error(f"[PluginLoader] uv venv failed: {result.stderr[:300]}")
return result.returncode == 0
except FileNotFoundError:
logger.error("[PluginLoader] uv not found — install it first")
return False
except Exception as e:
logger.error(f"[PluginLoader] Failed to create venv: {e}")
return False
from utils.functions import run_command
result = run_command(["uv", "venv", str(venv_dir)], timeout=30)
if result.returncode != 0:
logger.error(f"[PluginLoader] uv venv failed: {result.stderr[:300]}")
return result.returncode == 0
def _install_deps(self, venv_dir: Path, req_file: Path) -> bool:
"""Install dependencies from requirements.txt using uv."""
from utils.functions import run_command
venv_python = venv_dir / "bin" / "python"
try:
result = subprocess.run(
[
"uv",
"pip",
"install",
"-r",
str(req_file),
"--python",
str(venv_python),
],
capture_output=True,
text=True,
timeout=120,
)
if result.returncode != 0:
logger.error(
f"[PluginLoader] uv pip install failed: {result.stderr[:500]}"
)
return False
return True
except FileNotFoundError:
logger.error("[PluginLoader] uv not found — install it first")
return False
except Exception as e:
logger.error(f"[PluginLoader] Failed to install deps: {e}")
result = run_command(
[
"uv",
"pip",
"install",
"-r",
str(req_file),
"--python",
str(venv_python),
],
timeout=120,
)
if result.returncode != 0:
logger.error(f"[PluginLoader] uv pip install failed: {result.stderr[:500]}")
return False
return True
def _find_site_packages(self, venv_dir: Path) -> Path | None:
"""Find site-packages directory in a venv."""
+6 -6
View File
@@ -186,7 +186,7 @@ class PluginManager:
entry.instance = None
def _config_path(self) -> Path:
from utils.utils import toml_file
from utils.gtk_utils import toml_file
return Path(toml_file("config.toml")).parent / "plugins.json"
@@ -195,12 +195,12 @@ class PluginManager:
if not path.exists():
self._disabled_ids = set()
return
try:
with open(path) as f:
data = json.load(f)
from utils.functions import read_json_file
data = read_json_file(str(path))
if data is not None:
self._disabled_ids = set(data.get("disabled", []))
except Exception as e:
logger.error(f"[PluginManager] Failed to load disabled list: {e}")
else:
self._disabled_ids = set()
def _save_disabled_list(self) -> None:
+1 -1
View File
@@ -75,7 +75,7 @@ class ApplicationPlugin(SpotlightPlugin):
@staticmethod
def _launch_app(app: DesktopApp) -> None:
from globalmenu.launch import launch_desktop_app
from window.globalmenu.launch import launch_desktop_app
launch_desktop_app(app)
+13 -28
View File
@@ -1,10 +1,10 @@
import os
import re
import signal
import subprocess
from fabric.utils import logger
from utils.functions import format_duration, parse_timeout_string
from window.spotlight.api import SearchResult, SpotlightPlugin
_PID_FILE = os.path.join(
@@ -16,24 +16,6 @@ _INHIBIT_WHY = "Modus Caffeine"
# indefinite mode instead of relying on an in-process timer.
_INDEFINITE_SECS = 100_000_000
_DURATION_RE = re.compile(
r"^\s*(\d+)\s*(s|sec|secs|m|min|mins|h|hr|hrs|hour|hours)\s*$",
re.IGNORECASE,
)
_UNIT_SECS = {
"s": 1,
"sec": 1,
"secs": 1,
"m": 60,
"min": 60,
"mins": 60,
"h": 3600,
"hr": 3600,
"hrs": 3600,
"hour": 3600,
"hours": 3600,
}
class CaffeinePlugin(SpotlightPlugin):
id = "caffeine"
@@ -89,18 +71,21 @@ class CaffeinePlugin(SpotlightPlugin):
@staticmethod
def _parse_duration(text: str) -> int | None:
m = _DURATION_RE.match(text)
if not m:
return None
return int(m.group(1)) * _UNIT_SECS[m.group(2).lower()]
seconds = parse_timeout_string(text, in_seconds=True)
return seconds if seconds != 5 else None # 5 is the default on parse failure
@staticmethod
def _humanize(secs: int) -> str:
if secs >= 3600:
return f"{secs // 3600}h"
if secs >= 60:
return f"{secs // 60}m"
return f"{secs}s"
result = format_duration(secs)
# format_duration returns "N/A" for 0, but we want "0s"
if result == "N/A":
return "0s"
# format_duration returns "Xh Ym" or "Xm", we want shorter format
# For hours only: "1h" instead of "1h 0m"
if "h" in result and "m" in result:
hours = result.split("h")[0]
return f"{hours}h"
return result
# --- inhibition ----------------------------------------------------
+1 -1
View File
@@ -164,7 +164,7 @@ class EmojiPlugin(SpotlightPlugin):
result = self._normalize_annotations(raw) or None
if result:
logger.info(f"[Emoji] Normalized {len(result)} emojis")
logger.debug(f"[Emoji] Normalized {len(result)} emojis")
else:
logger.error("[Emoji] Normalization returned empty")
return result