Refactory of the code (#19)
* Refactory of the code * Autoformatting with black --------- Co-authored-by: Domefemia <domefemia@hotmail.com>
This commit is contained in:
@@ -65,7 +65,6 @@ class CartridgesApplication(Adw.Application):
|
|||||||
self.win = None
|
self.win = None
|
||||||
|
|
||||||
def do_activate(self):
|
def do_activate(self):
|
||||||
|
|
||||||
# Create the main window
|
# Create the main window
|
||||||
self.win = self.props.active_window
|
self.win = self.props.active_window
|
||||||
if not self.win:
|
if not self.win:
|
||||||
@@ -149,7 +148,6 @@ class CartridgesApplication(Adw.Application):
|
|||||||
self.win.update_games(games.keys())
|
self.win.update_games(games.keys())
|
||||||
|
|
||||||
def on_launch_game_action(self, _widget, _callback=None):
|
def on_launch_game_action(self, _widget, _callback=None):
|
||||||
|
|
||||||
# Launch the game and update the last played value
|
# Launch the game and update the last played value
|
||||||
|
|
||||||
game_id = self.win.active_game_id
|
game_id = self.win.active_game_id
|
||||||
@@ -178,7 +176,6 @@ class CartridgesApplication(Adw.Application):
|
|||||||
create_details_window(self.win)
|
create_details_window(self.win)
|
||||||
|
|
||||||
def on_remove_game_action(self, _widget, _callback=None):
|
def on_remove_game_action(self, _widget, _callback=None):
|
||||||
|
|
||||||
# Add "removed=True" to the game properties so it can be deleted on next init
|
# Add "removed=True" to the game properties so it can be deleted on next init
|
||||||
game_id = self.win.active_game_id
|
game_id = self.win.active_game_id
|
||||||
|
|
||||||
|
|||||||
@@ -84,9 +84,7 @@ def bottles_parser(parent_widget, action):
|
|||||||
_("Set Bottles Location"),
|
_("Set Bottles Location"),
|
||||||
).connect("response", response)
|
).connect("response", response)
|
||||||
|
|
||||||
if os.path.isfile(os.path.join(bottles_dir, "library.yml")):
|
if not os.path.isfile(os.path.join(bottles_dir, "library.yml")):
|
||||||
pass
|
|
||||||
else:
|
|
||||||
bottles_not_found()
|
bottles_not_found()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -137,7 +135,7 @@ def bottles_parser(parent_widget, action):
|
|||||||
|
|
||||||
bottles_games[values["game_id"]] = values
|
bottles_games[values["game_id"]] = values
|
||||||
|
|
||||||
if len(bottles_games) == 0:
|
if not bottles_games:
|
||||||
create_dialog(
|
create_dialog(
|
||||||
parent_widget,
|
parent_widget,
|
||||||
_("No Games Found"),
|
_("No Games Found"),
|
||||||
|
|||||||
@@ -202,7 +202,6 @@ def create_details_window(parent_widget, game_id=None):
|
|||||||
final_executable = executable.get_buffer().get_text()
|
final_executable = executable.get_buffer().get_text()
|
||||||
|
|
||||||
if game_id is None:
|
if game_id is None:
|
||||||
|
|
||||||
if final_name == "":
|
if final_name == "":
|
||||||
create_dialog(
|
create_dialog(
|
||||||
window, _("Couldn't Add Game"), _("Game title cannot be empty.")
|
window, _("Couldn't Add Game"), _("Game title cannot be empty.")
|
||||||
@@ -223,7 +222,7 @@ def create_details_window(parent_widget, game_id=None):
|
|||||||
if "imported_" in game:
|
if "imported_" in game:
|
||||||
numbers.append(int(game.replace("imported_", "")))
|
numbers.append(int(game.replace("imported_", "")))
|
||||||
|
|
||||||
game_id = "imported_" + str(max(numbers) + 1)
|
game_id = f"imported_{str(max(numbers) + 1)}"
|
||||||
|
|
||||||
values["game_id"] = game_id
|
values["game_id"] = game_id
|
||||||
values["hidden"] = False
|
values["hidden"] = False
|
||||||
@@ -252,7 +251,7 @@ def create_details_window(parent_widget, game_id=None):
|
|||||||
save_cover(None, parent_widget, None, pixbuf, game_id)
|
save_cover(None, parent_widget, None, pixbuf, game_id)
|
||||||
|
|
||||||
values["name"] = final_name
|
values["name"] = final_name
|
||||||
values["developer"] = final_developer if final_developer else None
|
values["developer"] = final_developer or None
|
||||||
values["executable"] = final_executable
|
values["executable"] = final_executable
|
||||||
|
|
||||||
path = os.path.join(
|
path = os.path.join(
|
||||||
@@ -261,7 +260,7 @@ def create_details_window(parent_widget, game_id=None):
|
|||||||
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
||||||
"cartridges",
|
"cartridges",
|
||||||
"games",
|
"games",
|
||||||
game_id + ".json",
|
f"{game_id}.json",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,9 @@ def get_cover(game_id, parent_widget):
|
|||||||
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
||||||
"cartridges",
|
"cartridges",
|
||||||
"covers",
|
"covers",
|
||||||
game_id + ".tiff",
|
f"{game_id}.tiff",
|
||||||
)
|
)
|
||||||
|
|
||||||
if not os.path.isfile(cover_path):
|
if os.path.isfile(cover_path):
|
||||||
return parent_widget.placeholder_pixbuf
|
|
||||||
|
|
||||||
return GdkPixbuf.Pixbuf.new_from_file(cover_path)
|
return GdkPixbuf.Pixbuf.new_from_file(cover_path)
|
||||||
|
return parent_widget.placeholder_pixbuf
|
||||||
|
|||||||
@@ -33,12 +33,10 @@ def get_games(game_ids=None):
|
|||||||
if not os.path.exists(games_dir):
|
if not os.path.exists(games_dir):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
if not game_ids:
|
if game_ids:
|
||||||
game_files = os.listdir(games_dir)
|
game_files = [f"{game_id}.json" for game_id in game_ids]
|
||||||
else:
|
else:
|
||||||
game_files = []
|
game_files = os.listdir(games_dir)
|
||||||
for game_id in game_ids:
|
|
||||||
game_files.append(game_id + ".json")
|
|
||||||
|
|
||||||
for game in game_files:
|
for game in game_files:
|
||||||
with open(os.path.join(games_dir, game), "r") as open_file:
|
with open(os.path.join(games_dir, game), "r") as open_file:
|
||||||
|
|||||||
@@ -90,9 +90,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
_("Set Heroic Location"),
|
_("Set Heroic Location"),
|
||||||
).connect("response", response)
|
).connect("response", response)
|
||||||
|
|
||||||
if os.path.exists(os.path.join(heroic_dir, "config.json")):
|
if not os.path.exists(os.path.join(heroic_dir, "config.json")):
|
||||||
pass
|
|
||||||
else:
|
|
||||||
heroic_not_found()
|
heroic_not_found()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -120,7 +118,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
values = {}
|
values = {}
|
||||||
|
|
||||||
app_name = game["app_name"]
|
app_name = game["app_name"]
|
||||||
values["game_id"] = "heroic_epic_" + app_name
|
values["game_id"] = f"heroic_epic_{app_name}"
|
||||||
|
|
||||||
if (
|
if (
|
||||||
values["game_id"] in parent_widget.games
|
values["game_id"] in parent_widget.games
|
||||||
@@ -131,9 +129,9 @@ def heroic_parser(parent_widget, action):
|
|||||||
values["name"] = game["title"]
|
values["name"] = game["title"]
|
||||||
values["developer"] = game["developer"]
|
values["developer"] = game["developer"]
|
||||||
values["executable"] = (
|
values["executable"] = (
|
||||||
"start heroic://launch/" + app_name
|
f"start heroic://launch/{app_name}"
|
||||||
if os.name == "nt"
|
if os.name == "nt"
|
||||||
else "xdg-open heroic://launch/" + app_name
|
else f"xdg-open heroic://launch/{app_name}"
|
||||||
)
|
)
|
||||||
values["hidden"] = False
|
values["hidden"] = False
|
||||||
values["source"] = "heroic_epic"
|
values["source"] = "heroic_epic"
|
||||||
@@ -168,7 +166,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
values = {}
|
values = {}
|
||||||
app_name = item["appName"]
|
app_name = item["appName"]
|
||||||
|
|
||||||
values["game_id"] = "heroic_gog_" + app_name
|
values["game_id"] = f"heroic_gog_{app_name}"
|
||||||
|
|
||||||
if (
|
if (
|
||||||
values["game_id"] in parent_widget.games
|
values["game_id"] in parent_widget.games
|
||||||
@@ -197,9 +195,9 @@ def heroic_parser(parent_widget, action):
|
|||||||
break
|
break
|
||||||
|
|
||||||
values["executable"] = (
|
values["executable"] = (
|
||||||
"start heroic://launch/" + app_name
|
f"start heroic://launch/{app_name}"
|
||||||
if os.name == "nt"
|
if os.name == "nt"
|
||||||
else "xdg-open heroic://launch/" + app_name
|
else f"xdg-open heroic://launch/{app_name}"
|
||||||
)
|
)
|
||||||
values["hidden"] = False
|
values["hidden"] = False
|
||||||
values["source"] = "heroic_gog"
|
values["source"] = "heroic_gog"
|
||||||
@@ -222,7 +220,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
values = {}
|
values = {}
|
||||||
app_name = item["app_name"]
|
app_name = item["app_name"]
|
||||||
|
|
||||||
values["game_id"] = "heroic_sideload_" + app_name
|
values["game_id"] = f"heroic_sideload_{app_name}"
|
||||||
|
|
||||||
if (
|
if (
|
||||||
values["game_id"] in parent_widget.games
|
values["game_id"] in parent_widget.games
|
||||||
@@ -232,9 +230,9 @@ def heroic_parser(parent_widget, action):
|
|||||||
|
|
||||||
values["name"] = item["title"]
|
values["name"] = item["title"]
|
||||||
values["executable"] = (
|
values["executable"] = (
|
||||||
"start heroic://launch/" + app_name
|
f"start heroic://launch/{app_name}"
|
||||||
if os.name == "nt"
|
if os.name == "nt"
|
||||||
else "xdg-open heroic://launch/" + app_name
|
else f"xdg-open heroic://launch/{app_name}"
|
||||||
)
|
)
|
||||||
values["hidden"] = False
|
values["hidden"] = False
|
||||||
values["source"] = "heroic_sideload"
|
values["source"] = "heroic_sideload"
|
||||||
@@ -250,7 +248,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
|
|
||||||
heroic_games[values["game_id"]] = values
|
heroic_games[values["game_id"]] = values
|
||||||
|
|
||||||
if len(heroic_games) == 0:
|
if not heroic_games:
|
||||||
create_dialog(
|
create_dialog(
|
||||||
parent_widget,
|
parent_widget,
|
||||||
_("No Games Found"),
|
_("No Games Found"),
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ from gi.repository import Gio
|
|||||||
|
|
||||||
def run_command(executable):
|
def run_command(executable):
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
["flatpak-spawn --host " + executable]
|
[f"flatpak-spawn --host {executable}"]
|
||||||
if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges"
|
if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges"
|
||||||
else executable.split()
|
else executable.split()
|
||||||
if os.name == "nt"
|
if os.name == "nt"
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ def save_cover(game, parent_widget, file_path, pixbuf=None, game_id=None):
|
|||||||
if not os.path.exists(covers_dir):
|
if not os.path.exists(covers_dir):
|
||||||
os.makedirs(covers_dir)
|
os.makedirs(covers_dir)
|
||||||
|
|
||||||
open_file = Gio.File.new_for_path(os.path.join(covers_dir, game_id + ".tiff"))
|
open_file = Gio.File.new_for_path(os.path.join(covers_dir, f"{game_id}.tiff"))
|
||||||
parent_widget.pixbufs[game_id] = pixbuf
|
parent_widget.pixbufs[game_id] = pixbuf
|
||||||
pixbuf.save_to_streamv_async(
|
pixbuf.save_to_streamv_async(
|
||||||
open_file.replace(None, False, Gio.FileCreateFlags.NONE),
|
open_file.replace(None, False, Gio.FileCreateFlags.NONE),
|
||||||
|
|||||||
@@ -33,6 +33,6 @@ def save_games(games):
|
|||||||
os.makedirs(games_dir)
|
os.makedirs(games_dir)
|
||||||
|
|
||||||
for game in games:
|
for game in games:
|
||||||
with open(os.path.join(games_dir, game + ".json"), "w") as open_file:
|
with open(os.path.join(games_dir, f"{game}.json"), "w") as open_file:
|
||||||
open_file.write(json.dumps(games[game], indent=4, sort_keys=True))
|
open_file.write(json.dumps(games[game], indent=4, sort_keys=True))
|
||||||
open_file.close()
|
open_file.close()
|
||||||
|
|||||||
@@ -32,12 +32,12 @@ def toggle_hidden(game):
|
|||||||
if not os.path.exists(games_dir):
|
if not os.path.exists(games_dir):
|
||||||
return
|
return
|
||||||
|
|
||||||
with open(os.path.join(games_dir, game + ".json"), "r") as open_file:
|
with open(os.path.join(games_dir, f"{game}.json"), "r") as open_file:
|
||||||
data = json.loads(open_file.read())
|
data = json.loads(open_file.read())
|
||||||
open_file.close()
|
open_file.close()
|
||||||
|
|
||||||
data["hidden"] = not data["hidden"]
|
data["hidden"] = not data["hidden"]
|
||||||
|
|
||||||
with open(os.path.join(games_dir, game + ".json"), "w") as open_file:
|
with open(os.path.join(games_dir, f"{game}.json"), "w") as open_file:
|
||||||
open_file.write(json.dumps(data, indent=4))
|
open_file.write(json.dumps(data, indent=4))
|
||||||
open_file.close()
|
open_file.close()
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
||||||
"cartridges",
|
"cartridges",
|
||||||
"games",
|
"games",
|
||||||
current_game + ".json",
|
f"{current_game}.json",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
@@ -107,7 +107,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
or os.path.expanduser(os.path.join("~", ".local", "share")),
|
||||||
"cartridges",
|
"cartridges",
|
||||||
"covers",
|
"covers",
|
||||||
current_game + ".dat",
|
f"{current_game}.dat",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@@ -290,36 +290,28 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
name2 = child2.get_first_child().name.lower()
|
name2 = child2.get_first_child().name.lower()
|
||||||
if name1 > name2:
|
if name1 > name2:
|
||||||
return -1
|
return -1
|
||||||
if name1 < name2:
|
return 1 if name1 < name2 else self.a_z_sort(child1, child2)
|
||||||
return 1
|
|
||||||
return self.a_z_sort(child1, child2)
|
|
||||||
|
|
||||||
def newest_sort(self, child1, child2):
|
def newest_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id].added
|
time1 = self.games[child1.get_first_child().game_id].added
|
||||||
time2 = self.games[child2.get_first_child().game_id].added
|
time2 = self.games[child2.get_first_child().game_id].added
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return -1
|
return -1
|
||||||
if time1 < time2:
|
return 1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||||
return 1
|
|
||||||
return self.a_z_sort(child1, child2)
|
|
||||||
|
|
||||||
def oldest_sort(self, child1, child2):
|
def oldest_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id].added
|
time1 = self.games[child1.get_first_child().game_id].added
|
||||||
time2 = self.games[child2.get_first_child().game_id].added
|
time2 = self.games[child2.get_first_child().game_id].added
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return 1
|
return 1
|
||||||
if time1 < time2:
|
return -1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||||
return -1
|
|
||||||
return self.a_z_sort(child1, child2)
|
|
||||||
|
|
||||||
def last_played_sort(self, child1, child2):
|
def last_played_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id].last_played
|
time1 = self.games[child1.get_first_child().game_id].last_played
|
||||||
time2 = self.games[child2.get_first_child().game_id].last_played
|
time2 = self.games[child2.get_first_child().game_id].last_played
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return -1
|
return -1
|
||||||
if time1 < time2:
|
return 1 if time1 < time2 else self.a_z_sort(child1, child2)
|
||||||
return 1
|
|
||||||
return self.a_z_sort(child1, child2)
|
|
||||||
|
|
||||||
def on_go_back_action(self, _widget, _unused, _x=None, _y=None):
|
def on_go_back_action(self, _widget, _unused, _x=None, _y=None):
|
||||||
if self.stack.get_visible_child() == self.hidden_library_view:
|
if self.stack.get_visible_child() == self.hidden_library_view:
|
||||||
|
|||||||
Reference in New Issue
Block a user