Explicit is better than implicit 🐍🤓

This commit is contained in:
kramo
2023-10-14 20:32:22 +02:00
parent 89ba4aecaa
commit 815c1ec088
3 changed files with 8 additions and 6 deletions

View File

@@ -172,7 +172,9 @@ class CartridgesApplication(Adw.Application):
try:
game_id = args[index + 1]
data = json.load(
(path := shared.games_dir / (game_id + ".json")).open("r")
(path := shared.games_dir / (game_id + ".json")).open(
"r", encoding="utf-8"
)
)
executable = (
shlex.join(data["executable"])
@@ -184,7 +186,7 @@ class CartridgesApplication(Adw.Application):
run_executable(executable)
data["last_played"] = int(time())
json.dump(data, path.open("w"))
json.dump(data, path.open("w", encoding="utf-8"))
except (IndexError, KeyError, OSError, json.decoder.JSONDecodeError):
return 1

View File

@@ -53,7 +53,7 @@ class FileManager(AsyncManager):
json.dump(
{attr: getattr(game, attr) for attr in attrs if attr},
(shared.games_dir / f"{game.game_id}.json").open("w"),
(shared.games_dir / f"{game.game_id}.json").open("w", encoding="utf-8"),
indent=4,
sort_keys=True,
)

View File

@@ -76,7 +76,7 @@ def migrate_files_v1_to_v2() -> None:
imported_execs = set()
for game_path in shared.games_dir.glob("imported_*.json"):
try:
game_data = json.load(game_path.open("r"))
game_data = json.load(game_path.open("r", encoding="utf-8"))
except (OSError, json.JSONDecodeError):
continue
number = int(game_data["game_id"].replace("imported_", ""))
@@ -86,7 +86,7 @@ def migrate_files_v1_to_v2() -> None:
# Migrate imported game files
for game_path in old_imported_game_paths:
try:
game_data = json.load(game_path.open("r"))
game_data = json.load(game_path.open("r", encoding="utf-8"))
except (OSError, json.JSONDecodeError):
continue
@@ -104,7 +104,7 @@ def migrate_files_v1_to_v2() -> None:
)
json.dump(
game_data,
destination_game_path.open("w"),
destination_game_path.open("w", encoding="utf-8"),
indent=4,
sort_keys=True,
)