From 6899246d0149a047ef57541071f7dd34fd1281c4 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Sat, 1 Apr 2023 16:14:19 +0200 Subject: [PATCH] Use simpler syntax for I/O operations --- src/game.py | 3 +-- src/utils/bottles_parser.py | 3 +-- src/utils/create_details_window.py | 3 +-- src/utils/get_games.py | 3 +-- src/utils/heroic_parser.py | 12 ++++-------- src/utils/save_game.py | 5 +++-- src/utils/steam_parser.py | 3 +-- 7 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/game.py b/src/game.py index 2c3dbb8..1af3625 100644 --- a/src/game.py +++ b/src/game.py @@ -110,8 +110,7 @@ class game(Gtk.Box): # pylint: disable=invalid-name if not games_dir.exists(): return - with open((games_dir / f"{self.game_id}.json") / "r") as open_file: - data = json.loads(open_file.read()) + data = json.loads((games_dir / f"{self.game_id}.json").read_text()) data["hidden"] = not data["hidden"] diff --git a/src/utils/bottles_parser.py b/src/utils/bottles_parser.py index 3fa207a..3cb1905 100644 --- a/src/utils/bottles_parser.py +++ b/src/utils/bottles_parser.py @@ -46,8 +46,7 @@ def bottles_parser(parent_widget): bottles_dir = Path(schema.get_string("bottles-location")).expanduser() current_time = int(time()) - with open((bottles_dir / "library.yml"), "r") as open_file: - data = open_file.read() + data = (bottles_dir / "library.yml").read_text() library = yaml.load(data, Loader=yaml.Loader) diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index 48b9be8..c3581ea 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -277,8 +277,7 @@ def create_details_window(parent_widget, game_id=None): path = parent_widget.data_dir / "cartridges" / "games" / f"{game_id}.json" if path.exists(): - with open(path, "r") as open_file: - data = json.loads(open_file.read()) + data = json.loads(path.read_text()) data.update(values) save_game(parent_widget, data) else: diff --git a/src/utils/get_games.py b/src/utils/get_games.py index d9c74fa..2dd9d87 100644 --- a/src/utils/get_games.py +++ b/src/utils/get_games.py @@ -33,8 +33,7 @@ def get_games(parent_widget, game_ids=None): game_files = games_dir.iterdir() for game in game_files: - with open((games_dir / game), "r") as open_file: - data = json.loads(open_file.read()) + data = json.loads((games_dir / game).read_text()) games[data["game_id"]] = data return games diff --git a/src/utils/heroic_parser.py b/src/utils/heroic_parser.py index e8ca5a4..53c2141 100644 --- a/src/utils/heroic_parser.py +++ b/src/utils/heroic_parser.py @@ -58,8 +58,7 @@ def heroic_parser(parent_widget): if not schema.get_boolean("heroic-import-epic"): pass elif (heroic_dir / "lib-cache" / "library.json").exists(): - with open((heroic_dir / "lib-cache" / "library.json"), "r") as open_file: - data = open_file.read() + data = (heroic_dir / "lib-cache" / "library.json").read_text() library = json.loads(data) try: @@ -112,8 +111,7 @@ def heroic_parser(parent_widget): if not schema.get_boolean("heroic-import-gog"): pass elif (heroic_dir / "gog_store" / "installed.json").exists(): - with open((heroic_dir / "gog_store" / "installed.json"), "r") as open_file: - data = open_file.read() + data = (heroic_dir / "gog_store" / "installed.json").read_text() installed = json.loads(data) importer.total_queue += len(installed["installed"]) @@ -133,8 +131,7 @@ def heroic_parser(parent_widget): continue # Get game title and developer from library.json as they are not present in installed.json - with open((heroic_dir / "gog_store" / "library.json"), "r") as open_file: - data = open_file.read() + data = (heroic_dir / "gog_store" / "library.json").read_text() library = json.loads(data) for game in library["games"]: if game["app_name"] == app_name: @@ -165,8 +162,7 @@ def heroic_parser(parent_widget): if not schema.get_boolean("heroic-import-sideload"): pass elif (heroic_dir / "sideload_apps" / "library.json").exists(): - with open((heroic_dir / "sideload_apps" / "library.json"), "r") as open_file: - data = open_file.read() + data = (heroic_dir / "sideload_apps" / "library.json").read_text() library = json.loads(data) importer.total_queue += len(library["games"]) diff --git a/src/utils/save_game.py b/src/utils/save_game.py index 741c9db..3b906b1 100644 --- a/src/utils/save_game.py +++ b/src/utils/save_game.py @@ -25,5 +25,6 @@ def save_game(parent_widget, game): games_dir.mkdir(parents=True, exist_ok=True) - with open((games_dir / f'{game["game_id"]}.json'), "w") as open_file: - open_file.write(json.dumps(game, indent=4, sort_keys=True)) + (games_dir / f'{game["game_id"]}.json').write_text( + json.dumps(game, indent=4, sort_keys=True) + ) diff --git a/src/utils/steam_parser.py b/src/utils/steam_parser.py index 35d47fb..9a88b71 100644 --- a/src/utils/steam_parser.py +++ b/src/utils/steam_parser.py @@ -46,8 +46,7 @@ def get_game( ): values = {} - with open(appmanifest, "r") as open_file: - data = open_file.read() + data = appmanifest.read_text() for datatype in datatypes: value = re.findall(f'"{datatype}"\t\t"(.*)"\n', data) values[datatype] = value[0]