From d5de4465f4efeb21a88ec3b7da3c4fb867ad936b Mon Sep 17 00:00:00 2001 From: Daniel Johnson Date: Wed, 27 Dec 2023 18:07:05 -0500 Subject: [PATCH] Retire "PATH_NOT_SET", "NOT_EXECUTABLE" and "NO_BIOS" in error codes in favor of exceptions. Only NO_BIOS gets its own exception class, as its hte only one raised in more than one place. This way we can avoid repeating the message. elif error == "NO_BIOS": message_text = _("A bios file is required to run this game") --- lutris/exceptions.py | 7 +++++++ lutris/game.py | 7 ------- lutris/runners/atari800.py | 3 ++- lutris/runners/hatari.py | 3 ++- lutris/runners/jzintv.py | 3 ++- lutris/runners/linux.py | 14 ++++++-------- lutris/runners/mame.py | 3 ++- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lutris/exceptions.py b/lutris/exceptions.py index bcd6b728e..c4e4826ff 100644 --- a/lutris/exceptions.py +++ b/lutris/exceptions.py @@ -21,6 +21,13 @@ class GameConfigError(MisconfigurationError): running properly.""" +class MissingBiosError(GameConfigError): + """Throw this error when the game requires a BIOS, but none is configured.""" + + def __init__(self, message=None, *args, **kwarg): + super().__init__(message or _("A bios file is required to run this game"), *args, **kwarg) + + class AuthenticationError(LutrisError): """Raised when authentication to a service fails""" diff --git a/lutris/game.py b/lutris/game.py index 6403b4a4e..487c021fe 100644 --- a/lutris/game.py +++ b/lutris/game.py @@ -275,19 +275,12 @@ class Game(GObject.Object): error = gameplay_info["error"] if error == "CUSTOM": message_text = gameplay_info["text"] - elif error == "NO_BIOS": - message_text = _("A bios file is required to run this game") elif error == "FILE_NOT_FOUND": filename = gameplay_info["file"] if filename: message_text = _("The file {} could not be found").format(filename) else: message_text = _("This game has no executable set. The install process didn't finish properly.") - elif error == "NOT_EXECUTABLE": - file = gameplay_info["file"] - message_text = _("The file %s is not executable") % file - elif error == "PATH_NOT_SET": - message_text = _("The path '%s' is not set. please set it in the options.") % gameplay_info["path"] else: message_text = _("Unhandled error: %s") % gameplay_info["error"] return GameConfigError(message_text) diff --git a/lutris/runners/atari800.py b/lutris/runners/atari800.py index 5875ecab7..e27e58e37 100644 --- a/lutris/runners/atari800.py +++ b/lutris/runners/atari800.py @@ -3,6 +3,7 @@ import os.path from gettext import gettext as _ from lutris.config import LutrisConfig +from lutris.exceptions import MissingBiosError from lutris.runners.runner import Runner from lutris.util import display, extract, system @@ -132,7 +133,7 @@ class atari800(Runner): bios_path = self.runner_config.get("bios_path") if not system.path_exists(bios_path): - return {"error": "NO_BIOS"} + raise MissingBiosError() good_bios = self.find_good_bioses(bios_path) for bios, filename in good_bios.items(): arguments.append("-%s" % bios) diff --git a/lutris/runners/hatari.py b/lutris/runners/hatari.py index 15a743531..be0d42f34 100644 --- a/lutris/runners/hatari.py +++ b/lutris/runners/hatari.py @@ -5,6 +5,7 @@ from gettext import gettext as _ # Lutris Modules from lutris.config import LutrisConfig +from lutris.exceptions import MissingBiosError from lutris.runners.runner import Runner from lutris.util import system @@ -175,7 +176,7 @@ class hatari(Runner): params.append("--tos") params.append(self.runner_config["bios_file"]) else: - return {"error": "NO_BIOS"} + raise MissingBiosError() diska = self.game_config.get("disk-a") if not system.path_exists(diska): return {"error": "FILE_NOT_FOUND", "file": diska} diff --git a/lutris/runners/jzintv.py b/lutris/runners/jzintv.py index 2505055a1..9de84a6e4 100644 --- a/lutris/runners/jzintv.py +++ b/lutris/runners/jzintv.py @@ -2,6 +2,7 @@ import os from gettext import gettext as _ +from lutris.exceptions import MissingBiosError # Lutris Modules from lutris.runners.runner import Runner from lutris.util import system @@ -77,7 +78,7 @@ class jzintv(Runner): arguments.append("--execimg=%s/exec.bin" % bios_path) arguments.append("--gromimg=%s/grom.bin" % bios_path) else: - return {"error": "NO_BIOS"} + raise MissingBiosError() rom_path = self.game_config.get("main_file") or "" if not system.path_exists(rom_path): return {"error": "FILE_NOT_FOUND", "file": rom_path} diff --git a/lutris/runners/linux.py b/lutris/runners/linux.py index 5292a1aa4..b7d474175 100644 --- a/lutris/runners/linux.py +++ b/lutris/runners/linux.py @@ -156,16 +156,14 @@ class linux(Runner): """Run native game.""" launch_info = {} - if not self.game_exe or not system.path_exists(self.game_exe): - return {"error": "FILE_NOT_FOUND", "file": self.game_exe} + exe = self.game_exe + if not exe or not system.path_exists(exe): + return {"error": "FILE_NOT_FOUND", "file": exe} # Quit if the file is not executable - mode = os.stat(self.game_exe).st_mode + mode = os.stat(exe).st_mode if not mode & stat.S_IXUSR: - return {"error": "NOT_EXECUTABLE", "file": self.game_exe} - - if not system.path_exists(self.game_exe): - return {"error": "FILE_NOT_FOUND", "file": self.game_exe} + raise GameConfigError(_("The file %s is not executable") % exe) ld_preload = self.game_config.get("ld_preload") if ld_preload: @@ -175,7 +173,7 @@ class linux(Runner): if ld_library_path: launch_info["ld_library_path"] = os.path.expanduser(ld_library_path) - command = [self.get_relative_exe(self.game_exe, self.working_dir)] + command = [self.get_relative_exe(exe, self.working_dir)] args = self.game_config.get("args") or "" for arg in split_arguments(args): diff --git a/lutris/runners/mame.py b/lutris/runners/mame.py index 26de31dc6..6c5a5dea9 100644 --- a/lutris/runners/mame.py +++ b/lutris/runners/mame.py @@ -3,6 +3,7 @@ import os from gettext import gettext as _ from lutris import runtime, settings +from lutris.exceptions import GameConfigError from lutris.runners.runner import Runner from lutris.util import system from lutris.util.jobs import AsyncCall @@ -328,7 +329,7 @@ class mame(Runner): # pylint: disable=invalid-name rompath = self.runner_config.get("rompath") rom = os.path.basename(self.game_config.get("main_file")) if not rompath: - return {'error': 'PATH_NOT_SET', 'path': 'rompath'} + raise GameConfigError(_("The path '%s' is not set. please set it in the options.") % 'rompath') command += ["-rompath", rompath, rom] if self.game_config.get("autoboot_command"):