Refactor to use pathlib

This commit is contained in:
kramo
2023-04-01 11:33:48 +02:00
parent 397f267522
commit 3bcbf8457c
13 changed files with 169 additions and 266 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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),

View File

@@ -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))

View File

@@ -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