#!/usr/bin/python3 """Pure utility functions — no GTK/GI dependencies. These can be imported and tested in isolation. """ import os import re from pathlib import Path def format_title(title): """Normalize a title into a slug-like identifier. Lowercases, strips, removes non-alphanumeric characters (except spaces), and collapses whitespace to single hyphens. """ title = title.strip().lower() title = re.sub(r"[^a-z0-9 ]+", "", title) return re.sub(r"\s+", "-", title) def is_valid_prefix_path(path: str, prefixes_dir: str | None = None) -> bool: """Check whether *path* lies within the configured prefixes directory. Returns ``True`` only if the resolved path is the prefixes directory itself or a descendant of it. Parameters ---------- path : str The path to check. prefixes_dir : str, optional The prefixes directory to check against. Defaults to the value from ``fungus.constants``. """ if prefixes_dir is None: from fungus.constants import prefixes_dir as _pd prefixes_dir = _pd expanded = os.path.expanduser(path) resolved = os.path.realpath(expanded) safe_root = os.path.realpath(prefixes_dir) return resolved == safe_root or resolved.startswith(safe_root + os.sep) def is_safe_prefix_path( path: str, user_data: str | None = None, user_config: str | None = None, ) -> bool: """Check whether *path* is a safe target for recursive deletion. Returns ``True`` only if the resolved path lies within the fungus user-data or user-config directories (``~/.local/share/fungus-launcher`` or ``~/.config/fungus-launcher``), or is one of those directories itself. Parameters ---------- path : str The path to check. user_data : str, optional The user-data directory. Defaults to ``PathManager.user_data()``. user_config : str, optional The user-config directory. Defaults to ``PathManager.user_config()``. """ if user_data is None: from fungus.path_manager import PathManager user_data = PathManager.user_data() if user_config is None: from fungus.path_manager import PathManager user_config = PathManager.user_config() expanded = os.path.expanduser(path) resolved = os.path.realpath(expanded) safe_roots = [ os.path.realpath(user_data), os.path.realpath(user_config), ] for root in safe_roots: if resolved == root or resolved.startswith(root + os.sep): return True return False def _resolve_runner_to_directory(runner): """Resolve an old-style runner name to the actual directory on disk. If a matching directory exists in compatibilitytools.d, return it. Otherwise fall back to the newest installed version of that family, or None if nothing is available. """ from fungus.constants import share_dir compat_path = Path(share_dir) / "Steam/compatibilitytools.d" if not compat_path.exists(): return None # Direct directory names pass through unchanged if (compat_path / runner).is_dir(): return runner old_aliases = { "Proton-GE Latest": "GE-Proton", "GE-Proton Latest (default)": "GE-Proton", "Proton-EM Latest": "Proton-EM", "UMU-Proton Latest": None, # no directory equivalent } family = old_aliases.get(runner) if family is None: return None # Linux-Native, Steam, or unknown — caller handles # Find matching directories in compatibilitytools.d matches = [ d for d in compat_path.iterdir() if d.is_dir() and d.name.startswith(family) ] if not matches: return None # user hasn't installed any version yet def sort_key(d): parts = re.split(r'(\d+)', d.name.replace(family, '')) return [int(p) if p.isdigit() else p for p in parts] matches.sort(key=sort_key, reverse=True) return matches[0].name def has_proton_installed() -> bool: """Check whether any proton/compatibility tool directories exist on disk.""" from fungus.constants import share_dir compat_path = Path(share_dir) / "Steam/compatibilitytools.d" if not compat_path.exists(): return False for entry in compat_path.iterdir(): if entry.is_dir() and entry.name not in ("LegacyRuntime",): return True return False def is_executable_in_whitelist(exec_path: str, game_dirs: list[str]) -> bool: """Check whether *exec_path* resides inside one of the whitelisted directories. The executable directory (or the file itself) must be a prefix of, or equal to, at least one whitelisted directory. Returns ``True`` if *game_dirs* is empty (no whitelist configured) or if the executable path falls within a whitelisted directory. """ if not game_dirs: return True # no whitelist configured — allow everything exec_dir = os.path.realpath(os.path.dirname(os.path.abspath(exec_path))) for whitelisted in game_dirs: wl_real = os.path.realpath(whitelisted) match_exact = exec_dir == wl_real match_prefix = exec_dir.startswith(wl_real + os.sep) if match_exact or match_prefix: return True return False