migration system
This commit is contained in:
@@ -24,3 +24,11 @@ def test_save_load_roundtrip(tmp_path):
|
|||||||
"personality": "judges you silently",
|
"personality": "judges you silently",
|
||||||
}
|
}
|
||||||
assert loaded.version == 1
|
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
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ class App:
|
|||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
save = persistence.load(save_name)
|
save = persistence.load(save_name)
|
||||||
except persistence.json.JSONDecodeError:
|
except (json.JSONDecodeError, KeyError):
|
||||||
print(
|
print(
|
||||||
"There was an error loading your savefile, it may be corrupt :("
|
"There was an error loading your savefile, it may be corrupt :("
|
||||||
)
|
)
|
||||||
|
|||||||
7
untitled/migration.py
Normal file
7
untitled/migration.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
_MIGRATIONS = {}
|
||||||
|
|
||||||
|
|
||||||
|
def migrate(data):
|
||||||
|
while data["version"] in _MIGRATIONS:
|
||||||
|
data = _MIGRATIONS[data["version"]](data)
|
||||||
|
return data
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from untitled import PACKAGE_ROOT, model
|
from untitled import PACKAGE_ROOT, migration, model
|
||||||
|
|
||||||
SAVE_FOLDER = PACKAGE_ROOT / "saves"
|
SAVE_FOLDER = PACKAGE_ROOT / "saves"
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ def load(name, folder=None):
|
|||||||
|
|
||||||
with open(save_file) as f:
|
with open(save_file) as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
data = migration.migrate(data)
|
||||||
save = model.Save.from_dict(data)
|
save = model.Save.from_dict(data)
|
||||||
|
|
||||||
return save
|
return save
|
||||||
|
|||||||
Reference in New Issue
Block a user