diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-07-03 20:56:34 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-07-03 20:56:34 +0300 |
| commit | c79cf21cd27dfed67d064b515a58ad6ef9aa499a (patch) | |
| tree | 14076b6f7ac4f4be4e3ad8c851e4926f6bd7c44f | |
| parent | 887b26f923f01d46ee30d0f0397c5d2da907e03f (diff) | |
| download | fungus-launcher-c79cf21cd27dfed67d064b515a58ad6ef9aa499a.tar.gz fungus-launcher-c79cf21cd27dfed67d064b515a58ad6ef9aa499a.tar.xz | |
remove everything related to writing to monolithic games.json
| -rw-r--r-- | fungus/__init__.py | 3 | ||||
| -rw-r--r-- | fungus/constants.py | 1 | ||||
| -rw-r--r-- | fungus/game_model.py | 103 | ||||
| -rw-r--r-- | tests/conftest.py | 5 | ||||
| -rw-r--r-- | tests/test_game_model.py | 125 |
5 files changed, 14 insertions, 223 deletions
diff --git a/fungus/__init__.py b/fungus/__init__.py index fb28812..a603336 100644 --- a/fungus/__init__.py +++ b/fungus/__init__.py @@ -11,7 +11,6 @@ __all__ = [ "SandboxWarningDialog", "SettingsDialog", "app_dir", - "backup_dir", "config_file_dir", "desktop_dir", "envar_dir", @@ -49,7 +48,6 @@ def __getattr__(name): # Fall back to module-level constants from fungus.constants import ( app_dir, - backup_dir, config_file_dir, desktop_dir, envar_dir, @@ -62,7 +60,6 @@ def __getattr__(name): _constants = { "app_dir": app_dir, - "backup_dir": backup_dir, "config_file_dir": config_file_dir, "desktop_dir": desktop_dir, "envar_dir": envar_dir, diff --git a/fungus/constants.py b/fungus/constants.py index 6d94351..1e69850 100644 --- a/fungus/constants.py +++ b/fungus/constants.py @@ -25,7 +25,6 @@ icons_dir = PathManager.user_config('fungus-launcher/icons') config_file_dir = PathManager.user_config('fungus-launcher/config.ini') envar_dir = PathManager.user_config('fungus-launcher/envar.txt') share_dir = PathManager.user_data() -backup_dir = PathManager.user_config("fungus-launcher/games-backup") app_dir = PathManager.user_data('applications') fungus_png = PathManager.get_icon('fungus-launcher.png') diff --git a/fungus/game_model.py b/fungus/game_model.py index 396df0f..3930811 100644 --- a/fungus/game_model.py +++ b/fungus/game_model.py @@ -3,10 +3,8 @@ import configparser import json import os -import shutil import tempfile from dataclasses import dataclass, field -from datetime import datetime from typing import TYPE_CHECKING # ── Game data model ───────────────────────────────────────────────────── @@ -80,98 +78,6 @@ def load_games_json(path): return [Game.from_dict(d) for d in raw] -def save_games_json(path, games, *, deleted_gameid=None, backup=True): - """Save a list of Game objects to a JSON file. - - Hidden and visible games are both preserved unless *deleted_gameid* is - given — in that case the matching game is excluded from output. - - Parameters - ---------- - path : str - Path to the games JSON file. - games : list[Game] - Games to persist. - deleted_gameid : str, optional - If given, exclude the matching game from output. - backup : bool, default True - If True, create a timestamped backup before writing. - """ - if backup: - backup_games_file(path) - - if deleted_gameid: - games = [g for g in games if g.gameid != deleted_gameid] - - with open(path, "w", encoding="utf-8") as f: - json.dump([g.to_dict() for g in games], f, ensure_ascii=False, indent=4) - - -def update_game_field(path, gameid, field, value): - """Update a single field on a game entry in the JSON file. - - Loads all games, finds the one matching *gameid*, sets the field, - and saves back with a backup. - - Parameters - ---------- - path : str - Path to the games JSON file. - gameid : str - The gameid of the game to update. - field : str - Attribute name to set on the Game object. - value : any - Value to assign. - """ - games = load_games_json(path) - for game in games: - if game.gameid == gameid: - setattr(game, field, value) - break - else: - return # gameid not found — silently skip - - save_games_json(path, games) - - -def backup_games_file(source_path, backup_dir=None): - """Create a timestamped backup of *source_path*. - - Keeps at most 20 backups (oldest are pruned). - - Parameters - ---------- - source_path : str - Path to the file to back up. - backup_dir : str, optional - Directory to store backups in. If not provided, uses the default - ``backup_dir`` constant (must be set before calling). - """ - if backup_dir is None: - from fungus.constants import backup_dir as _default_backup_dir - backup_dir = _default_backup_dir - - if not os.path.isfile(source_path): - return - - os.makedirs(backup_dir, exist_ok=True) - - now = datetime.now() - timestamp = now.strftime("%Y-%m-%d_%H-%M-%S") - - backup_file = os.path.join(backup_dir, f"games-data-{timestamp}.json") - shutil.copy2(source_path, backup_file) - - backups = sorted( - f for f in os.listdir(backup_dir) if f.startswith("games-data-") and f.endswith(".json") - ) - - while len(backups) > 20: - oldest = backups.pop(0) - os.remove(os.path.join(backup_dir, oldest)) - - # ── Per-game INI storage ──────────────────────────────────────────────── @@ -432,13 +338,8 @@ def migrate_to_per_game( """ result = MigrationResult() - # Step 1: back up the monolithic file - try: - backup_games_file(games_json_path) - except Exception as exc: - result.errors.append(f"Backup failed: {exc}") - - # Step 2: load games from JSON + # Step 1: load games from JSON + # (games.json is preserved as games.json.migrated — no separate backup needed) games = load_games_json(games_json_path) if not games: # Empty or missing file — nothing to migrate, but still rename if source exists diff --git a/tests/conftest.py b/tests/conftest.py index 64039b7..0181757 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -67,9 +67,10 @@ def sample_games(): @pytest.fixture() def populated_games_file(games_file, sample_games): """Write sample games to a JSON file and return the path.""" - from fungus.game_model import save_games_json + import json - save_games_json(str(games_file), sample_games) + with open(games_file, "w", encoding="utf-8") as f: + json.dump([g.to_dict() for g in sample_games], f, ensure_ascii=False, indent=4) return games_file diff --git a/tests/test_game_model.py b/tests/test_game_model.py index 7558648..190f52e 100644 --- a/tests/test_game_model.py +++ b/tests/test_game_model.py @@ -3,16 +3,19 @@ import json import os + +def _write_games_json(path, games): + """Write a list of Game objects to a JSON file (test helper).""" + with open(path, "w", encoding="utf-8") as f: + json.dump([g.to_dict() for g in games], f, ensure_ascii=False, indent=4) + from fungus.game_model import ( Game, GameLibrary, MigrationResult, - backup_games_file, load_games_json, migrate_to_per_game, needs_migration, - save_games_json, - update_game_field, ) @@ -120,115 +123,6 @@ class TestLoadGamesJson: assert games == [] -# ── save_games_json ───────────────────────────────────────────────────── - - -class TestSaveGamesJson: - def test_save_and_reload(self, games_file, sample_games): - save_games_json(str(games_file), sample_games, backup=False) - games = load_games_json(str(games_file)) - assert len(games) == 2 - assert games[0].gameid == "umu-1234" - assert games[1].favorite is False # should be preserved - - def test_save_with_deleted_gameid(self, games_file, sample_games): - save_games_json( - str(games_file), sample_games, deleted_gameid="umu-1234", backup=False - ) - games = load_games_json(str(games_file)) - assert len(games) == 1 - assert games[0].gameid == "umu-5678" - - def test_save_hidden_games_preserved(self, games_file, sample_games): - save_games_json(str(games_file), sample_games, backup=False) - games = load_games_json(str(games_file)) - hidden_games = [g for g in games if g.hidden] - assert len(hidden_games) == 1 - assert hidden_games[0].gameid == "umu-5678" - - def test_save_empty_list(self, games_file): - save_games_json(str(games_file), [], backup=False) - with open(games_file) as f: - data = json.load(f) - assert data == [] - - def test_backup_is_created(self, games_file, sample_games, tmp_path): - # Write initial file - save_games_json(str(games_file), sample_games, backup=False) - initial_mtime = os.path.getmtime(str(games_file)) - - # Now save again with backup - save_games_json(str(games_file), sample_games, backup=True) - assert os.path.getmtime(str(games_file)) > initial_mtime - - -# ── update_game_field ─────────────────────────────────────────────────── - - -class TestUpdateGameField: - def test_update_existing_game(self, populated_games_file): - update_game_field( - str(populated_games_file), "umu-1234", "favorite", True - ) - games = load_games_json(str(populated_games_file)) - game = next(g for g in games if g.gameid == "umu-1234") - assert game.favorite is True - - def test_update_nonexistent_gameid(self, populated_games_file): - # Should not raise; silently skips - update_game_field( - str(populated_games_file), "nonexistent", "title", "New Title" - ) - games = load_games_json(str(populated_games_file)) - assert all(g.title != "New Title" for g in games) - - def test_update_playtime(self, populated_games_file): - update_game_field( - str(populated_games_file), "umu-5678", "playtime", 9999 - ) - games = load_games_json(str(populated_games_file)) - game = next(g for g in games if g.gameid == "umu-5678") - assert game.playtime == 9999 - - -# ── backup_games_file ─────────────────────────────────────────────────── - - -class TestBackupGamesFile: - def test_creates_backup(self, games_file, tmp_config_dir, sample_games): - - # Write a games file - save_games_json(str(games_file), sample_games, backup=False) - - # Set backup dir - backup_dest = tmp_config_dir / "backups" - - backup_games_file(str(games_file), backup_dir=str(backup_dest)) - - backups = list(backup_dest.glob("games-data-*.json")) - assert len(backups) == 1 - - def test_prunes_old_backups(self, games_file, tmp_config_dir, sample_games): - backup_dest = tmp_config_dir / "backups" - backup_dest.mkdir(parents=True, exist_ok=True) - - # Create 22 fake old backups - for i in range(22): - ts = f"2025-01-01_00-00-{i:02d}" - (backup_dest / f"games-data-{ts}.json").write_text("[]") - - save_games_json(str(games_file), sample_games, backup=False) - backup_games_file(str(games_file), backup_dir=str(backup_dest)) - - backups = list(backup_dest.glob("games-data-*.json")) - assert len(backups) <= 20 - - def test_nonexistent_source_does_nothing(self, tmp_config_dir): - backup_dest = tmp_config_dir / "backups" - backup_games_file("/nonexistent/path.json", backup_dir=str(backup_dest)) - assert not backup_dest.exists() - - # ── GameLibrary ───────────────────────────────────────────────────────── @@ -405,7 +299,7 @@ class TestMigrateToPerGame: games_json = tmp_path / "games.json" games_dir = tmp_path / "games" # Write monolithic JSON - save_games_json(str(games_json), sample_games, backup=False) + _write_games_json(str(games_json), sample_games) assert games_json.exists() result = migrate_to_per_game(str(games_json), str(games_dir)) @@ -445,13 +339,12 @@ class TestMigrateToPerGame: games_json = tmp_path / "games.json" games_dir = tmp_path / "games" # Two games with same gameid - save_games_json( + _write_games_json( str(games_json), [ Game(gameid="dup", title="First", path="/first"), Game(gameid="dup", title="Second", path="/second"), ], - backup=False, ) result = migrate_to_per_game(str(games_json), str(games_dir)) @@ -465,7 +358,7 @@ class TestMigrateToPerGame: def test_migrate_dry_run(self, tmp_path, sample_games): games_json = tmp_path / "games.json" games_dir = tmp_path / "games" - save_games_json(str(games_json), sample_games, backup=False) + _write_games_json(str(games_json), sample_games) result = migrate_to_per_game( str(games_json), str(games_dir), dry_run=True |
