From 214687c9ce2285320125b07a64be59ea34476dac Mon Sep 17 00:00:00 2001 From: Bananaman <38923130+Bananaman@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:26:24 +0100 Subject: [PATCH] Cleanup: Remove backwards-compatible code Since we aren't interested in backwards compatibility this early in development, let's remove those code chunks to keep the code shorter. --- src/meson.build | 1 - src/utils/game_data_to_json.py | 25 ---------------------- src/utils/get_games.py | 25 +--------------------- src/utils/run_command.py | 39 ++++++++++------------------------ src/utils/save_games.py | 4 +--- 5 files changed, 13 insertions(+), 81 deletions(-) delete mode 100644 src/utils/game_data_to_json.py diff --git a/src/meson.build b/src/meson.build index f2c7981..78d6144 100644 --- a/src/meson.build +++ b/src/meson.build @@ -30,7 +30,6 @@ cartridges_sources = [ 'utils/get_cover.py', 'utils/save_games.py', 'utils/save_cover.py', - 'utils/game_data_to_json.py', 'utils/toggle_hidden.py', 'utils/create_dialog.py', 'utils/create_details_window.py' diff --git a/src/utils/game_data_to_json.py b/src/utils/game_data_to_json.py deleted file mode 100644 index f927809..0000000 --- a/src/utils/game_data_to_json.py +++ /dev/null @@ -1,25 +0,0 @@ -# game_data_to_json.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 - -import json -import os - - -def game_data_to_json(data): - return json.dumps(data, indent=4, sort_keys=True) diff --git a/src/utils/get_games.py b/src/utils/get_games.py index 73acfe9..9125107 100644 --- a/src/utils/get_games.py +++ b/src/utils/get_games.py @@ -19,9 +19,6 @@ import json import os -import shlex - -from .game_data_to_json import game_data_to_json def get_games(game_ids=None): @@ -42,28 +39,8 @@ def get_games(game_ids=None): game_files = os.listdir(games_dir) 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: data = json.loads(open_file.read()) - - # Convert any outdated JSON values to our newest data format. - needs_rewrite = False - if "executable" in data and isinstance(data["executable"], str): - needs_rewrite = True - try: - # Use shell parsing to determine what the individual components are. - executable_split = shlex.split( - data["executable"], comments=False, posix=True - ) - except: - # Fallback: Split once at earliest space (1 part if no spaces, else 2 parts). - executable_split = data["executable"].split(" ", 1) - data["executable"] = executable_split - - if needs_rewrite: - open_file.seek(0) - open_file.truncate() - open_file.write(game_data_to_json(data)) - open_file.close() games[data["game_id"]] = data diff --git a/src/utils/run_command.py b/src/utils/run_command.py index 4614c2d..78b1d0f 100644 --- a/src/utils/run_command.py +++ b/src/utils/run_command.py @@ -26,33 +26,16 @@ from gi.repository import Gio def run_command(executable): - use_shell = False - if not use_shell: - # The host environment is automatically passed through by Popen. - subprocess.Popen( - ["flatpak-spawn", "--host", *executable] # Flatpak - if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" - else executable # Windows - if os.name == "nt" - else executable, # Linux/Others - shell=False, # If true, the extra arguments would incorrectly be given to the shell instead. - start_new_session=True, - creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0, - ) - else: - # When launching as a shell, we must pass 1 string with the exact command - # line exactly as we would type it in a shell (with escaped arguments). - subprocess.Popen( - shlex.join( - ["flatpak-spawn", "--host", *executable] # Flatpak - if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" - else executable # Windows - if os.name == "nt" - else executable # Linux/Others - ), - shell=True, - start_new_session=True, - creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0, - ) + # The host environment vars are automatically passed through by Popen. + subprocess.Popen( + ["flatpak-spawn", "--host", *executable] # Flatpak + if os.getenv("FLATPAK_ID") == "hu.kramo.Cartridges" + else executable # Windows + if os.name == "nt" + else executable, # Linux/Others + shell=False, # If true, the extra arguments would incorrectly be given to the shell instead. + start_new_session=True, + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0, + ) if Gio.Settings.new("hu.kramo.Cartridges").get_boolean("exit-after-launch"): sys.exit() diff --git a/src/utils/save_games.py b/src/utils/save_games.py index fdc6e77..858f08c 100644 --- a/src/utils/save_games.py +++ b/src/utils/save_games.py @@ -20,8 +20,6 @@ import json import os -from .game_data_to_json import game_data_to_json - def save_games(games): games_dir = os.path.join( @@ -36,5 +34,5 @@ def save_games(games): for game in games: with open(os.path.join(games_dir, f"{game}.json"), "w") as open_file: - open_file.write(game_data_to_json(games[game])) + open_file.write(json.dumps(games[game], indent=4, sort_keys=True)) open_file.close()