From 3bcbf8457c75551cec3ffaa4f126b347e7d11454 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Sat, 1 Apr 2023 11:33:48 +0200 Subject: [PATCH] Refactor to use pathlib --- src/game.py | 28 ++++------ src/main.py | 8 +-- src/preferences.py | 25 +++++---- src/utils/bottles_parser.py | 43 ++++++--------- src/utils/create_details_window.py | 16 ++---- src/utils/get_games.py | 16 ++---- src/utils/heroic_parser.py | 88 ++++++++++++------------------ src/utils/importer.py | 2 +- src/utils/lutris_parser.py | 78 ++++++++------------------ src/utils/save_cover.py | 16 ++---- src/utils/save_game.py | 14 ++--- src/utils/steam_parser.py | 60 ++++++++++---------- src/window.py | 41 ++++++-------- 13 files changed, 169 insertions(+), 266 deletions(-) diff --git a/src/game.py b/src/game.py index 24a9ef1..2c3dbb8 100644 --- a/src/game.py +++ b/src/game.py @@ -105,22 +105,17 @@ class game(Gtk.Box): # pylint: disable=invalid-name sys.exit() def toggle_hidden(self): - games_dir = os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "games", - ) + games_dir = self.parent_widget.data_dir / "cartridges" / "games" - if not os.path.exists(games_dir): + if not games_dir.exists(): return - with open(os.path.join(games_dir, f"{self.game_id}.json"), "r") as open_file: + with open((games_dir / f"{self.game_id}.json") / "r") as open_file: data = json.loads(open_file.read()) data["hidden"] = not data["hidden"] - save_game(data) + save_game(self.parent_widget, data) def get_cover(self): @@ -129,16 +124,15 @@ class game(Gtk.Box): # pylint: disable=invalid-name return self.parent_widget.pixbufs[self.game_id] # Create a new pixbuf - cover_path = os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "covers", - f"{self.game_id}.tiff", + cover_path = ( + self.parent_widget.data_dir + / "cartridges" + / "covers" + / f"{self.game_id}.tiff" ) - if os.path.isfile(cover_path): - return GdkPixbuf.Pixbuf.new_from_file(cover_path) + if cover_path.is_file(): + return GdkPixbuf.Pixbuf.new_from_file(str(cover_path)) # Return the placeholder pixbuf return self.parent_widget.placeholder_pixbuf diff --git a/src/main.py b/src/main.py index 50953ca..b13c000 100644 --- a/src/main.py +++ b/src/main.py @@ -138,9 +138,9 @@ class CartridgesApplication(Adw.Application): game_id = self.win.active_game_id - data = get_games([game_id])[game_id] + data = get_games(self.win, [game_id])[game_id] data["last_played"] = int(time.time()) - save_game(data) + save_game(self.win, data) self.win.games[game_id].launch() @@ -188,9 +188,9 @@ class CartridgesApplication(Adw.Application): # Add "removed=True" to the game properties so it can be deleted on next init game_id = self.win.active_game_id - data = get_games([game_id])[game_id] + data = get_games(self.win, [game_id])[game_id] data["removed"] = True - save_game(data) + save_game(self.win, data) self.win.update_games([game_id]) if self.win.stack.get_visible_child() == self.win.overview: diff --git a/src/preferences.py b/src/preferences.py index d7201ca..37b2b77 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -18,6 +18,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import os +from pathlib import Path from gi.repository import Adw, Gio, GLib, Gtk @@ -46,10 +47,7 @@ class ImportPreferences: if response == "choose_folder": window.choose_folder(widget, set_dir) - if not any( - os.path.exists(os.path.join(path, current_path)) - for current_path in paths - ): + if not any((path / current_path).exists() for current_path in paths): create_dialog( window.parent_widget, _("Installation Not Found"), @@ -163,8 +161,8 @@ class PreferencesWindow(Adw.PreferencesWindow): "steam-location", [ "steamapps", - os.path.join("steam", "steamapps"), - os.path.join("Steam", "steamapps"), + Path("steam") / "steamapps", + Path("Steam") / "steamapps", ], self.steam_expander_row, self.steam_file_chooser_button, @@ -209,13 +207,13 @@ class PreferencesWindow(Adw.PreferencesWindow): def set_cache_dir(_source, result, _unused): try: - path = self.file_chooser.select_folder_finish(result).get_path() + path = Path(self.file_chooser.select_folder_finish(result).get_path()) def response(widget, response): if response == "choose_folder": self.choose_folder(widget, set_cache_dir) - if not os.path.exists(os.path.join(path, "coverart")): + if not (path / "coverart").exists(): create_dialog( self.parent_widget, _("Cache Not Found"), @@ -235,6 +233,9 @@ class PreferencesWindow(Adw.PreferencesWindow): "clicked", self.choose_folder, set_cache_dir ) + if os.name == "nt": + self.sources_group.remove(self.lutris_expander_row) + # Heroic ImportPreferences( self, @@ -285,20 +286,20 @@ class PreferencesWindow(Adw.PreferencesWindow): def undo_remove_all(self, _widget, _unused): for game_id in self.removed_games: - data = get_games([game_id])[game_id] + data = get_games(self.parent_widget, [game_id])[game_id] if "removed" in data: data.pop("removed") - save_game(data) + save_game(self.parent_widget, data) self.parent_widget.update_games(self.removed_games) self.removed_games = [] self.toast.dismiss() def remove_all_games(self, _widget): - for game in get_games().values(): + for game in get_games(self.parent_widget).values(): if "removed" not in game: self.removed_games.append(game["game_id"]) game["removed"] = True - save_game(game) + save_game(self.parent_widget, game) self.parent_widget.update_games(self.parent_widget.games) if self.parent_widget.stack.get_visible_child() == self.parent_widget.overview: diff --git a/src/utils/bottles_parser.py b/src/utils/bottles_parser.py index 35bccb4..3fa207a 100644 --- a/src/utils/bottles_parser.py +++ b/src/utils/bottles_parser.py @@ -17,7 +17,7 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -import os +from pathlib import Path from time import time import yaml @@ -25,37 +25,28 @@ import yaml def bottles_parser(parent_widget): schema = parent_widget.schema - bottles_dir = os.path.expanduser(schema.get_string("bottles-location")) + bottles_dir = Path(schema.get_string("bottles-location")).expanduser() - if not os.path.isfile(os.path.join(bottles_dir, "library.yml")): - if os.path.exists( - os.path.expanduser("~/.var/app/com.usebottles.bottles/data/bottles/") + if not (bottles_dir / "library.yml").is_file(): + if ( + Path("~/.var/app/com.usebottles.bottles/data/bottles/") + .expanduser() + .exists() ): schema.set_string( "bottles-location", "~/.var/app/com.usebottles.bottles/data/bottles/" ) - elif os.path.exists( - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "bottles", - ) - ): + elif (parent_widget.data_dir / "bottles").exists(): schema.set_string( - "bottles-location", - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "bottles", - ), + "bottles-location", str(parent_widget.data_dir / "bottles") ) else: return - bottles_dir = os.path.expanduser(schema.get_string("bottles-location")) + bottles_dir = Path(schema.get_string("bottles-location")).expanduser() current_time = int(time()) - with open(os.path.join(bottles_dir, "library.yml"), "r") as open_file: + with open((bottles_dir / "library.yml"), "r") as open_file: data = open_file.read() library = yaml.load(data, Loader=yaml.Loader) @@ -90,12 +81,12 @@ def bottles_parser(parent_widget): if game["thumbnail"]: importer.save_cover( values["game_id"], - os.path.join( - bottles_dir, - "bottles", - game["bottle"]["path"], - "grids", - game["thumbnail"].split(":")[1], + ( + bottles_dir + / "bottles" + / game["bottle"]["path"] + / "grids" + / game["thumbnail"].split(":")[1] ), ) importer.save_game(values) diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index 20a2159..48b9be8 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -274,23 +274,15 @@ def create_details_window(parent_widget, game_id=None): values["developer"] = final_developer or None values["executable"] = final_executable_split - path = os.path.join( - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "games", - f"{game_id}.json", - ) - ) + path = parent_widget.data_dir / "cartridges" / "games" / f"{game_id}.json" - if os.path.exists(path): + if path.exists(): with open(path, "r") as open_file: data = json.loads(open_file.read()) data.update(values) - save_game(data) + save_game(parent_widget, data) else: - save_game(values) + save_game(parent_widget, values) parent_widget.update_games([game_id]) if parent_widget.stack.get_visible_child() == parent_widget.overview: diff --git a/src/utils/get_games.py b/src/utils/get_games.py index c727c7c..d9c74fa 100644 --- a/src/utils/get_games.py +++ b/src/utils/get_games.py @@ -18,28 +18,22 @@ # SPDX-License-Identifier: GPL-3.0-or-later import json -import os -def get_games(game_ids=None): - games_dir = os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "games", - ) +def get_games(parent_widget, game_ids=None): + games_dir = parent_widget.data_dir / "cartridges" / "games" games = {} - if not os.path.exists(games_dir): + if not games_dir.exists(): return {} if game_ids: game_files = [f"{game_id}.json" for game_id in game_ids] else: - game_files = os.listdir(games_dir) + game_files = games_dir.iterdir() for game in game_files: - with open(os.path.join(games_dir, game), "r") as open_file: + with open((games_dir / game), "r") as open_file: data = json.loads(open_file.read()) games[data["game_id"]] = data diff --git a/src/utils/heroic_parser.py b/src/utils/heroic_parser.py index e78998a..e8ca5a4 100644 --- a/src/utils/heroic_parser.py +++ b/src/utils/heroic_parser.py @@ -20,46 +20,36 @@ import hashlib import json import os +from pathlib import Path from time import time def heroic_parser(parent_widget): schema = parent_widget.schema - heroic_dir = os.path.expanduser(schema.get_string("heroic-location")) + heroic_dir = Path(schema.get_string("heroic-location")).expanduser() - if not os.path.exists(os.path.join(heroic_dir, "config.json")): - if os.path.exists( - os.path.expanduser("~/.var/app/com.heroicgameslauncher.hgl/config/heroic/") + if not (heroic_dir / "config.json").exists(): + if ( + Path("~/.var/app/com.heroicgameslauncher.hgl/config/heroic/") + .expanduser() + .exists() ): schema.set_string( "heroic-location", "~/.var/app/com.heroicgameslauncher.hgl/config/heroic/", ) - elif os.path.exists( - os.path.join( - os.getenv("XDG_CONFIG_HOME") - or os.path.expanduser(os.path.join("~", ".config")), - "heroic", - ) - ): + elif (parent_widget.config_dir / "heroic").exists(): schema.set_string( - "heroic-location", - os.path.join( - os.getenv("XDG_CONFIG_HOME") - or os.path.expanduser(os.path.join("~", ".config")), - "heroic", - ), + "heroic-location", str(parent_widget.config_dir / "heroic") ) - elif os.name == "nt" and os.path.exists( - os.path.join(os.getenv("appdata"), "heroic") - ): + elif os.name == "nt" and (Path(os.getenv("appdata")) / "heroic").exists(): schema.set_string( - "heroic-location", os.path.join(os.getenv("appdata"), "heroic") + "heroic-location", str(Path((os.getenv("appdata") / "heroic"))) ) else: return - heroic_dir = os.path.expanduser(schema.get_string("heroic-location")) + heroic_dir = Path(schema.get_string("heroic-location")).expanduser() current_time = int(time()) importer = parent_widget.importer @@ -67,10 +57,8 @@ def heroic_parser(parent_widget): # Import Epic games if not schema.get_boolean("heroic-import-epic"): pass - elif os.path.exists(os.path.join(heroic_dir, "lib-cache", "library.json")): - with open( - os.path.join(heroic_dir, "lib-cache", "library.json"), "r" - ) as open_file: + elif (heroic_dir / "lib-cache" / "library.json").exists(): + with open((heroic_dir / "lib-cache" / "library.json"), "r") as open_file: data = open_file.read() library = json.loads(data) @@ -106,14 +94,14 @@ def heroic_parser(parent_widget): values["added"] = current_time values["last_played"] = 0 - image_path = os.path.join( - heroic_dir, - "images-cache", - hashlib.sha256( + image_path = ( + heroic_dir + / "images-cache" + / hashlib.sha256( (f'{game["art_square"]}?h=400&resize=1&w=300').encode() - ).hexdigest(), + ).hexdigest() ) - if os.path.exists(image_path): + if image_path.exists(): importer.save_cover(values["game_id"], image_path) importer.save_game(values) @@ -123,10 +111,8 @@ def heroic_parser(parent_widget): # Import GOG games if not schema.get_boolean("heroic-import-gog"): pass - elif os.path.exists(os.path.join(heroic_dir, "gog_store", "installed.json")): - with open( - os.path.join(heroic_dir, "gog_store", "installed.json"), "r" - ) as open_file: + elif (heroic_dir / "gog_store" / "installed.json").exists(): + with open((heroic_dir / "gog_store" / "installed.json"), "r") as open_file: data = open_file.read() installed = json.loads(data) @@ -147,21 +133,19 @@ def heroic_parser(parent_widget): continue # Get game title and developer from library.json as they are not present in installed.json - with open( - os.path.join(heroic_dir, "gog_store", "library.json"), "r" - ) as open_file: + with open((heroic_dir / "gog_store" / "library.json"), "r") as open_file: data = open_file.read() library = json.loads(data) for game in library["games"]: if game["app_name"] == app_name: values["developer"] = game["developer"] values["name"] = game["title"] - image_path = os.path.join( - heroic_dir, - "images-cache", - hashlib.sha256(game["art_square"].encode()).hexdigest(), + image_path = ( + heroic_dir + / "images-cache" + / hashlib.sha256(game["art_square"].encode()).hexdigest() ) - if os.path.exists(image_path): + if image_path.exists(): importer.save_cover(values["game_id"], image_path) break @@ -180,10 +164,8 @@ def heroic_parser(parent_widget): # Import sideloaded games if not schema.get_boolean("heroic-import-sideload"): pass - elif os.path.exists(os.path.join(heroic_dir, "sideload_apps", "library.json")): - with open( - os.path.join(heroic_dir, "sideload_apps", "library.json"), "r" - ) as open_file: + elif (heroic_dir / "sideload_apps" / "library.json").exists(): + with open((heroic_dir / "sideload_apps" / "library.json"), "r") as open_file: data = open_file.read() library = json.loads(data) @@ -213,12 +195,12 @@ def heroic_parser(parent_widget): values["source"] = "heroic_sideload" values["added"] = current_time values["last_played"] = 0 - image_path = os.path.join( - heroic_dir, - "images-cache", - hashlib.sha256(item["art_square"].encode()).hexdigest(), + image_path = ( + heroic_dir + / "images-cache" + / hashlib.sha256(item["art_square"].encode()).hexdigest() ) - if os.path.exists(image_path): + if image_path.extsts(): importer.save_cover(values["game_id"], image_path) importer.save_game(values) diff --git a/src/utils/importer.py b/src/utils/importer.py index 295dd9c..02a4a51 100644 --- a/src/utils/importer.py +++ b/src/utils/importer.py @@ -55,7 +55,7 @@ class Importer: def save_game(self, values=None): if values: self.games_no += 1 - save_game(values) + save_game(self.parent_widget, values) self.parent_widget.update_games([values["game_id"]]) if "blacklisted" in values: self.games_no -= 1 diff --git a/src/utils/lutris_parser.py b/src/utils/lutris_parser.py index beb58b9..ef08abe 100644 --- a/src/utils/lutris_parser.py +++ b/src/utils/lutris_parser.py @@ -17,8 +17,7 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -import os -import shutil +from pathlib import Path from sqlite3 import connect from time import time @@ -27,73 +26,40 @@ def lutris_parser(parent_widget): schema = parent_widget.schema - database_path = os.path.join( - os.path.expanduser(schema.get_string("lutris-location")), "pga.db" - ) - if not os.path.isfile(database_path): - if os.path.exists( - os.path.expanduser("~/.var/app/net.lutris.Lutris/data/lutris/") - ): + database_path = (Path(schema.get_string("lutris-location")) / "pga.db").expanduser() + if not database_path.is_file(): + if Path("~/.var/app/net.lutris.Lutris/data/lutris/").expanduser().exists(): schema.set_string( "lutris-location", "~/.var/app/net.lutris.Lutris/data/lutris/" ) - elif os.path.exists( - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "lutris", - ) - ): - schema.set_string( - "lutris-location", - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "lutris", - ), - ) + elif (parent_widget.data_dir / "lutris").exists(): + schema.set_string("lutris-location", str(parent_widget.data_dir / "lutris")) else: return - cache_dir = os.path.expanduser(schema.get_string("lutris-cache-location")) - if not os.path.exists(cache_dir): - if os.path.exists( - os.path.expanduser("~/.var/app/net.lutris.Lutris/cache/lutris/") - ): + cache_dir = Path(schema.get_string("lutris-cache-location")).expanduser() + if not cache_dir.exists(): + if Path("~/.var/app/net.lutris.Lutris/cache/lutris/").expanduser().exists(): schema.set_string( "lutris-cache-location", "~/.var/app/net.lutris.Lutris/cache/lutris/" ) - elif os.path.exists( - os.path.join( - os.getenv("XDG_CACHE_HOME") - or os.path.expanduser(os.path.join("~", ".cache")), - "lutris", - ) - ): + elif (parent_widget.cache_dir / "lutris").exists(): schema.set_string( - "lutris-cache-location", - os.path.join( - os.getenv("XDG_CACHE_HOME") - or os.path.expanduser(os.path.join("~", ".cache")), - "lutris", - ), + "lutris-cache-location", str(parent_widget.cache_dir / "lutris") ) else: return - database_path = os.path.join( - os.path.expanduser(schema.get_string("lutris-location")), "pga.db" - ) - cache_dir = os.path.expanduser(schema.get_string("lutris-cache-location")) + database_path = Path(schema.get_string("lutris-location")) / "pga.db" + database_path.expanduser() + cache_dir = Path(schema.get_string("lutris-cache-location")).expanduser() - db_cache_dir = os.path.join( - os.getenv("XDG_CACHE_HOME") or os.path.expanduser(os.path.join("~", ".cache")), - "cartridges", - "lutris", - ) - os.makedirs(db_cache_dir, exist_ok=True) + db_cache_dir = parent_widget.cache_dir / "cartridges" / "lutris" + db_cache_dir.mkdir(parents=True, exist_ok=True) - shutil.copyfile(database_path, os.path.join(db_cache_dir, "pga.db")) + database_link_path = db_cache_dir / "pga.db" + database_link_path.unlink(True) + database_link_path.symlink_to(database_path) db_request = """ SELECT @@ -108,7 +74,7 @@ def lutris_parser(parent_widget): ; """ - connection = connect(os.path.join(db_cache_dir, "pga.db")) + connection = connect(database_link_path) cursor = connection.execute(db_request) rows = cursor.fetchall() connection.close() @@ -141,9 +107,9 @@ def lutris_parser(parent_widget): values["name"] = row[1] values["source"] = f"lutris_{row[3]}" - if os.path.isfile(os.path.join(cache_dir, "coverart", f"{row[2]}.jpg")): + if (cache_dir / "coverart" / f"{row[2]}.jpg").is_file(): importer.save_cover( - values["game_id"], os.path.join(cache_dir, "coverart", f"{row[2]}.jpg") + values["game_id"], (cache_dir / "coverart" / f"{row[2]}.jpg") ) importer.save_game(values) diff --git a/src/utils/save_cover.py b/src/utils/save_cover.py index a4c9711..e6d75e8 100644 --- a/src/utils/save_cover.py +++ b/src/utils/save_cover.py @@ -17,28 +17,24 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -import os from gi.repository import GdkPixbuf, Gio def save_cover(parent_widget, game_id, cover_path, pixbuf=None): - covers_dir = os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "covers", - ) + covers_dir = parent_widget.data_dir / "cartridges" / "covers" - os.makedirs(covers_dir, exist_ok=True) + covers_dir.mkdir(parents=True, exist_ok=True) if pixbuf is None: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(cover_path, 400, 600, False) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( + str(cover_path), 400, 600, False + ) def cover_callback(*_unused): pass - open_file = Gio.File.new_for_path(os.path.join(covers_dir, f"{game_id}.tiff")) + open_file = Gio.File.new_for_path(str(covers_dir / f"{game_id}.tiff")) parent_widget.pixbufs[game_id] = pixbuf pixbuf.save_to_streamv_async( open_file.replace(None, False, Gio.FileCreateFlags.NONE), diff --git a/src/utils/save_game.py b/src/utils/save_game.py index 4ecda93..741c9db 100644 --- a/src/utils/save_game.py +++ b/src/utils/save_game.py @@ -18,18 +18,12 @@ # SPDX-License-Identifier: GPL-3.0-or-later import json -import os -def save_game(game): - games_dir = os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "games", - ) +def save_game(parent_widget, game): + games_dir = parent_widget.data_dir / "cartridges" / "games" - os.makedirs(games_dir, exist_ok=True) + games_dir.mkdir(parents=True, exist_ok=True) - with open(os.path.join(games_dir, f'{game["game_id"]}.json'), "w") as open_file: + with open((games_dir / f'{game["game_id"]}.json'), "w") as open_file: open_file.write(json.dumps(game, indent=4, sort_keys=True)) diff --git a/src/utils/steam_parser.py b/src/utils/steam_parser.py index 23fcf37..35d47fb 100644 --- a/src/utils/steam_parser.py +++ b/src/utils/steam_parser.py @@ -21,6 +21,7 @@ import json import os import re import urllib.request +from pathlib import Path from time import time from gi.repository import Gio, GLib @@ -89,21 +90,19 @@ def get_game( if content: values = update_values_from_data(content, values) - if os.path.isfile( - os.path.join( - steam_dir, - "appcache", - "librarycache", - f'{values["appid"]}_library_600x900.jpg', - ) - ): + if ( + steam_dir + / "appcache" + / "librarycache" + / f'{values["appid"]}_library_600x900.jpg' + ).is_file(): importer.save_cover( values["game_id"], - os.path.join( - steam_dir, - "appcache", - "librarycache", - f'{values["appid"]}_library_600x900.jpg', + ( + steam_dir + / "appcache" + / "librarycache" + / f'{values["appid"]}_library_600x900.jpg' ), ) @@ -151,49 +150,48 @@ def get_games_async(parent_widget, appmanifests, steam_dir, importer): def steam_parser(parent_widget): schema = parent_widget.schema - steam_dir = os.path.expanduser(schema.get_string("steam-location")) + steam_dir = Path(schema.get_string("steam-location")).expanduser() def steam_not_found(): - if os.path.exists( - os.path.expanduser("~/.var/app/com.valvesoftware.Steam/data/Steam/") - ): + if Path("~/.var/app/com.valvesoftware.Steam/data/Steam/").expanduser().exists(): schema.set_string( "steam-location", "~/.var/app/com.valvesoftware.Steam/data/Steam/" ) - elif os.path.exists(os.path.expanduser("~/.steam/steam/")): + elif Path("~/.steam/steam/").expanduser().exists(): schema.set_string("steam-location", "~/.steam/steam/") - elif os.name == "nt" and os.path.exists( - os.path.join(os.getenv("programfiles(x86)"), "Steam") + elif ( + os.name == "nt" + and (Path((os.getenv("programfiles(x86)"))) / "Steam").exists() ): schema.set_string( - "steam-location", os.path.join(os.getenv("programfiles(x86)"), "Steam") + "steam-location", str(Path(os.getenv("programfiles(x86)")) / "Steam") ) - if os.path.exists(os.path.join(steam_dir, "steamapps")): + if (steam_dir / "steamapps").exists(): pass - elif os.path.exists(os.path.join(steam_dir, "steam", "steamapps")): - schema.set_string("steam-location", os.path.join(steam_dir, "steam")) - elif os.path.exists(os.path.join(steam_dir, "Steam", "steamapps")): - schema.set_string("steam-location", os.path.join(steam_dir, "Steam")) + elif (steam_dir / "steam" / "steamapps").exists(): + schema.set_string("steam-location", str(steam_dir / "steam")) + elif (steam_dir / "Steam" / "steamapps").exists(): + schema.set_string("steam-location", str(steam_dir / "Steam")) else: steam_not_found() steam_parser(parent_widget) return - steam_dir = os.path.expanduser(schema.get_string("steam-location")) + steam_dir = Path(schema.get_string("steam-location")).expanduser() appmanifests = [] steam_dirs = schema.get_strv("steam-extra-dirs") steam_dirs.append(steam_dir) for directory in steam_dirs: - if not os.path.exists(os.path.join(directory, "steamapps")): + if not (directory / "steamapps").exists(): steam_dirs.remove(directory) for directory in steam_dirs: - for open_file in os.listdir(os.path.join(directory, "steamapps")): - path = os.path.join(directory, "steamapps", open_file) - if os.path.isfile(path) and "appmanifest" in open_file: + for open_file in (directory / "steamapps").iterdir(): + path = directory / "steamapps" / open_file + if path.is_file() and "appmanifest" in open_file.name: appmanifests.append(path) importer = parent_widget.importer diff --git a/src/window.py b/src/window.py index 730421b..4a80c2d 100644 --- a/src/window.py +++ b/src/window.py @@ -19,6 +19,7 @@ import datetime import os +from pathlib import Path from gi.repository import Adw, GdkPixbuf, Gio, GLib, Gtk @@ -68,6 +69,12 @@ class CartridgesWindow(Adw.ApplicationWindow): def __init__(self, **kwargs): super().__init__(**kwargs) + self.data_dir = ( + Path(os.getenv("XDG_DATA_HOME")) or Path.home() / ".local" / "share" + ) + self.config_dir = Path(os.getenv("XDG_CONFIG_HOME")) or Path.home() / ".config" + self.cache_dir = Path(os.getenv("XDG_CACHE_HOME")) or Path.home() / ".cache" + self.games = {} self.visible_widgets = {} self.hidden_widgets = {} @@ -85,35 +92,23 @@ class CartridgesWindow(Adw.ApplicationWindow): self.placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale( "/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False ) - current_games = get_games() + current_games = get_games(self) for current_game in current_games: if "removed" in current_games[current_game]: - os.remove( - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "games", - f"{current_game}.json", - ) - ) + ( + self.data_dir / "cartridges" / "games" / f"{current_game}.json" + ).remove() try: - os.remove( - os.path.join( - os.getenv("XDG_DATA_HOME") - or os.path.expanduser(os.path.join("~", ".local", "share")), - "cartridges", - "covers", - f"{current_game}.tiff", - ) - ) + ( + self.data_dir / "cartridges" / "covers" / f"{current_game}.tiff" + ).remove() except FileNotFoundError: pass self.library.set_filter_func(self.search_filter) self.hidden_library.set_filter_func(self.hidden_search_filter) - self.update_games(get_games()) + self.update_games(get_games(self)) # Connect signals self.search_entry.connect("search-changed", self.search_changed, False) @@ -124,7 +119,7 @@ class CartridgesWindow(Adw.ApplicationWindow): self.add_controller(back_mouse_button) def update_games(self, games): - current_games = get_games() + current_games = get_games(self) for game_id in games: if game_id in self.visible_widgets: @@ -420,9 +415,9 @@ class CartridgesWindow(Adw.ApplicationWindow): game_id = list(self.toasts)[-1] except IndexError: return - data = get_games([game_id])[game_id] + data = get_games(self, [game_id])[game_id] data.pop("removed", None) - save_game(data) + save_game(self, data) self.update_games([game_id]) self.toasts[game_id].dismiss() self.toasts.pop(game_id)