From 42cfe4823b0aa42484425c1399607c3fe381b79a Mon Sep 17 00:00:00 2001 From: Toasterkitten Date: Wed, 24 Jun 2026 18:32:51 -0400 Subject: [PATCH] migration system --- tests/test_persistence.py | 8 ++++++++ untitled/app.py | 3 ++- untitled/migration.py | 7 +++++++ untitled/persistence.py | 4 ++-- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 untitled/migration.py diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 34de4c5..67a2c63 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -24,3 +24,11 @@ def test_save_load_roundtrip(tmp_path): "personality": "judges you silently", } assert loaded.version == 1 + + +def test_list_saves(tmp_path): + open(tmp_path / "test.kitten", "w").close() + open(tmp_path / "test2.kitten", "w").close() + saves = persistence.list_saves(tmp_path) + assert "test" in saves + assert "test2" in saves diff --git a/untitled/app.py b/untitled/app.py index c526381..4faae25 100644 --- a/untitled/app.py +++ b/untitled/app.py @@ -1,3 +1,4 @@ +import json import os import time @@ -67,7 +68,7 @@ class App: continue try: save = persistence.load(save_name) - except persistence.json.JSONDecodeError: + except (json.JSONDecodeError, KeyError): print( "There was an error loading your savefile, it may be corrupt :(" ) diff --git a/untitled/migration.py b/untitled/migration.py new file mode 100644 index 0000000..90c7b2d --- /dev/null +++ b/untitled/migration.py @@ -0,0 +1,7 @@ +_MIGRATIONS = {} + + +def migrate(data): + while data["version"] in _MIGRATIONS: + data = _MIGRATIONS[data["version"]](data) + return data diff --git a/untitled/persistence.py b/untitled/persistence.py index bc27881..9c56996 100644 --- a/untitled/persistence.py +++ b/untitled/persistence.py @@ -1,7 +1,7 @@ import json from pathlib import Path -from untitled import PACKAGE_ROOT, model +from untitled import PACKAGE_ROOT, migration, model SAVE_FOLDER = PACKAGE_ROOT / "saves" @@ -26,7 +26,7 @@ def load(name, folder=None): with open(save_file) as f: data = json.load(f) - + data = migration.migrate(data) save = model.Save.from_dict(data) return save