From 43e8317c467940ff7cfb27ea21cce1fdf767e208 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Fri, 14 Apr 2023 14:12:27 +0200 Subject: [PATCH] Refactor install location checking - fixes #69 --- src/importers/bottles_importer.py | 34 +++++++++++---------- src/importers/heroic_importer.py | 51 +++++++++++++++++-------------- src/importers/itch_importer.py | 26 ++++++++++------ src/importers/lutris_importer.py | 49 ++++++++++++++++------------- src/importers/steam_importer.py | 45 ++++++++++++--------------- src/meson.build | 3 +- src/utils/check_install.py | 32 +++++++++++++++++++ src/utils/save_cover.py | 2 +- 8 files changed, 143 insertions(+), 99 deletions(-) create mode 100644 src/utils/check_install.py diff --git a/src/importers/bottles_importer.py b/src/importers/bottles_importer.py index 9e29c34..bb7e82f 100644 --- a/src/importers/bottles_importer.py +++ b/src/importers/bottles_importer.py @@ -22,28 +22,30 @@ from time import time import yaml +from .check_install import check_install + def bottles_importer(parent_widget): schema = parent_widget.schema - bottles_dir = Path(schema.get_string("bottles-location")).expanduser() + location_key = "bottles-location" + bottles_dir = Path(schema.get_string(location_key)).expanduser() + check = "library.yml" - 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 (parent_widget.data_dir / "bottles").exists(): - schema.set_string( - "bottles-location", str(parent_widget.data_dir / "bottles") - ) - else: + if not (bottles_dir / check).is_file(): + locations = ( + Path.home() + / ".var" + / "app" + / "com.usebottles.bottles" + / "data" + / "bottles", + parent_widget.data_dir / "bottles", + ) + + bottles_dir = check_install(check, locations, (schema, location_key)) + if not bottles_dir: return - bottles_dir = Path(schema.get_string("bottles-location")).expanduser() current_time = int(time()) data = (bottles_dir / "library.yml").read_text("utf-8") diff --git a/src/importers/heroic_importer.py b/src/importers/heroic_importer.py index e9a62a5..04d7b51 100644 --- a/src/importers/heroic_importer.py +++ b/src/importers/heroic_importer.py @@ -23,33 +23,36 @@ import os from pathlib import Path from time import time +from .check_install import check_install + def heroic_importer(parent_widget): schema = parent_widget.schema - heroic_dir = Path(schema.get_string("heroic-location")).expanduser() + location_key = "heroic-location" + heroic_dir = Path(schema.get_string(location_key)).expanduser() + check = "config.json" - 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 (parent_widget.config_dir / "heroic").exists(): - schema.set_string( - "heroic-location", str(parent_widget.config_dir / "heroic") - ) - elif os.name == "nt" and (Path(os.getenv("appdata")) / "heroic").exists(): - schema.set_string( - "heroic-location", str(Path(os.getenv("appdata")) / "heroic") - ) - else: + if not (heroic_dir / check).is_file(): + locations = ( + Path.home() + / ".var" + / "app" + / "com.heroicgameslauncher.hgl" + / "config" + / "heroic", + parent_widget.config_dir / "heroic", + ) + + if os.name == "nt": + locations += (Path(os.getenv("appdata")) / "heroic",) + + heroic_dir = check_install(check, locations, (schema, location_key)) + if not heroic_dir: return - heroic_dir = Path(schema.get_string("heroic-location")).expanduser() + schema = parent_widget.schema + check = "config.json" + current_time = int(time()) importer = parent_widget.importer @@ -57,8 +60,10 @@ def heroic_importer(parent_widget): # Import Epic games if not schema.get_boolean("heroic-import-epic"): pass - elif (heroic_dir / "lib-cache" / "library.json").exists(): - data = (heroic_dir / "lib-cache" / "library.json").read_text("utf-8") + elif (heroic_dir / "store_cache" / "legendary_library.json").exists(): + data = (heroic_dir / "store_cache" / "legendary_library.json").read_text( + "utf-8" + ) library = json.loads(data) try: diff --git a/src/importers/itch_importer.py b/src/importers/itch_importer.py index cea7111..fe00160 100644 --- a/src/importers/itch_importer.py +++ b/src/importers/itch_importer.py @@ -26,6 +26,8 @@ from time import time import requests from gi.repository import GdkPixbuf, Gio +from .check_install import check_install + def get_game(task, current_time, parent_widget, row): values = {} @@ -117,19 +119,23 @@ def get_games_async(parent_widget, rows, importer): def itch_importer(parent_widget): schema = parent_widget.schema + location_key = "itch-location" + itch_dir = Path(schema.get_string(location_key)).expanduser() + check = Path("db") / "butler.db" - database_path = (Path(schema.get_string("itch-location")) / "db").expanduser() - if not database_path.exists(): - if Path("~/.var/app/io.itch.itch/config/itch/").expanduser().exists(): - schema.set_string("itch-location", "~/.var/app/io.itch.itch/config/itch/") - elif (parent_widget.config_dir / "itch").exists(): - schema.set_string("itch-location", str(parent_widget.config_dir / "itch")) - elif os.name == "nt" and (Path(os.getenv("appdata")) / "itch").exists(): - schema.set_string("itch-location", str(Path(os.getenv("appdata")) / "itch")) - else: + if not (itch_dir / check).is_file(): + locations = ( + Path.home() / ".var" / "app" / "io.itch.itch" / "config" / "itch", + parent_widget.config_dir / "itch", + ) + + if os.name == "nt": + locations += (Path(os.getenv("appdata")) / "itch",) + + if not check_install(check, locations, (schema, location_key)): return - database_path = (Path(schema.get_string("itch-location")) / "db").expanduser() + database_path = (Path(schema.get_string(location_key)) / "db").expanduser() db_cache_dir = parent_widget.cache_dir / "cartridges" / "itch" db_cache_dir.mkdir(parents=True, exist_ok=True) diff --git a/src/importers/lutris_importer.py b/src/importers/lutris_importer.py index 5536dc4..554bb8f 100644 --- a/src/importers/lutris_importer.py +++ b/src/importers/lutris_importer.py @@ -22,36 +22,41 @@ from shutil import copyfile from sqlite3 import connect from time import time +from .check_install import check_install + def lutris_importer(parent_widget): schema = parent_widget.schema + location_key = "lutris-location" + lutris_dir = Path(schema.get_string(location_key)).expanduser() + check = "pga.db" - database_path = (Path(schema.get_string("lutris-location"))).expanduser() - if not database_path.exists(): - if Path("~/.var/app/net.lutris.Lutris/data/lutris/").expanduser().exists(): - schema.set_string( - "lutris-location", "~/.var/app/net.lutris.Lutris/data/lutris/" - ) - elif (parent_widget.data_dir / "lutris").exists(): - schema.set_string("lutris-location", str(parent_widget.data_dir / "lutris")) - else: + if not (lutris_dir / check).is_file(): + locations = ( + Path.home() / ".var" / "app" / "net.lutris.Lutris" / "data" / "lutris", + parent_widget.data_dir / "lutris", + ) + + lutris_dir = check_install(check, locations, (schema, location_key)) + if not lutris_dir: return - 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 (parent_widget.cache_dir / "lutris").exists(): - schema.set_string( - "lutris-cache-location", str(parent_widget.cache_dir / "lutris") - ) - else: + cache_key = "lutris-cache-location" + cache_dir = Path(schema.get_string(cache_key)).expanduser() + cache_check = "coverart" + + if not (cache_dir / cache_check).exists(): + cache_locations = ( + Path.home() / ".var" / "app" / "net.lutris.Lutris" / "cache" / "lutris", + parent_widget.cache_dir / "lutris", + ) + + cache_dir = check_install(check, cache_locations, (schema, location_key)) + if not cache_dir: return - database_path = (Path(schema.get_string("lutris-location"))).expanduser() - cache_dir = Path(schema.get_string("lutris-cache-location")).expanduser() + database_path = (Path(schema.get_string(location_key))).expanduser() + cache_dir = Path(schema.get_string(cache_key)).expanduser() db_cache_dir = parent_widget.cache_dir / "cartridges" / "lutris" db_cache_dir.mkdir(parents=True, exist_ok=True) diff --git a/src/importers/steam_importer.py b/src/importers/steam_importer.py index 0da7108..d8319ca 100644 --- a/src/importers/steam_importer.py +++ b/src/importers/steam_importer.py @@ -25,6 +25,8 @@ from time import time import requests from gi.repository import Gio +from .check_install import check_install + def update_values_from_data(content, values): basic_data = content[values["appid"]] @@ -125,35 +127,26 @@ def get_games_async(parent_widget, appmanifests, steam_dir, importer): def steam_importer(parent_widget): schema = parent_widget.schema - steam_dir = Path(schema.get_string("steam-location")).expanduser() + location_key = "steam-location" + steam_dir = Path(schema.get_string(location_key)).expanduser() + check = "steamapps" - def steam_not_found(): - if Path("~/.var/app/com.valvesoftware.Steam/data/Steam/").expanduser().exists(): - schema.set_string( - "steam-location", "~/.var/app/com.valvesoftware.Steam/data/Steam/" - ) - elif Path("~/.steam/steam/").expanduser().exists(): - schema.set_string("steam-location", "~/.steam/steam/") - elif ( - os.name == "nt" - and (Path(os.getenv("programfiles(x86)")) / "Steam").exists() - ): - schema.set_string( - "steam-location", str(Path(os.getenv("programfiles(x86)")) / "Steam") - ) + if not (steam_dir / check).is_file(): + subdirs = ("steam", "Steam") - if (steam_dir / "steamapps").exists(): - pass - 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_importer(parent_widget) - return + locations = ( + Path.home() / ".steam" / "steam", + parent_widget.data_dir / "Steam", + Path.home() / ".var" / "app" / "com.valvesoftware.Steam" / "data" / "Steam", + ) + + if os.name == "nt": + locations += (Path(os.getenv("programfiles(x86)")) / "Steam",) + + steam_dir = check_install(check, locations, (schema, location_key), subdirs) + if not steam_dir: + return - steam_dir = Path(schema.get_string("steam-location")).expanduser() appmanifests = [] steam_dirs = [Path(directory) for directory in schema.get_strv("steam-extra-dirs")] diff --git a/src/meson.build b/src/meson.build index d2f7708..fda2582 100644 --- a/src/meson.build +++ b/src/meson.build @@ -34,7 +34,8 @@ cartridges_sources = [ 'utils/save_game.py', 'utils/save_cover.py', 'utils/create_dialog.py', - 'utils/create_details_window.py' + 'utils/create_details_window.py', + 'utils/check_install.py' ] install_data(cartridges_sources, install_dir: moduledir) diff --git a/src/utils/check_install.py b/src/utils/check_install.py new file mode 100644 index 0000000..59d9e67 --- /dev/null +++ b/src/utils/check_install.py @@ -0,0 +1,32 @@ +# check_install.py +# +# Copyright 2022-2023 kramo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later + +from pathlib import Path + + +def check_install(check, locations, setting=None, subdirs=(Path(),)): + for location in locations: + for subdir in subdirs: + if (location / subdir / check).is_file() or ( + location / subdir / check + ).exists(): + if setting: + setting[0].set_string(setting[1], str(location)) + + return location diff --git a/src/utils/save_cover.py b/src/utils/save_cover.py index 87330be..78603f9 100644 --- a/src/utils/save_cover.py +++ b/src/utils/save_cover.py @@ -21,7 +21,7 @@ from pathlib import Path from shutil import copyfile -from gi.repository import GdkPixbuf, Gio, GLib +from gi.repository import GdkPixbuf, Gio from PIL import Image, ImageSequence