From abce32c3c83db7cbad3110e9c2a591ec39a55b73 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Wed, 26 Jan 2022 09:07:39 +0100 Subject: [PATCH] moved log_error into Error.py --- source/Common.py | 3 +++ source/Error.py | 30 ++++++++++++++++++++++++++++++ source/Game.py | 5 ++--- source/Gui/Main.py | 26 +++----------------------- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/source/Common.py b/source/Common.py index 5c33f26..d86abb3 100644 --- a/source/Common.py +++ b/source/Common.py @@ -4,6 +4,7 @@ from source.Game import Game from source.Gui.Main import Main from source.Gui.TrackConfiguration import TrackConfiguration from source.Translation import Translator +from source.Error import ErrorLogger class Common: @@ -16,6 +17,8 @@ class Common: self.json_frame_filter = None self.translator = Translator(common=self) self.translate = self.translator.translate # shortcut for the method + self.errorlogger = ErrorLogger(common=self) + self.log_error = self.errorlogger.log_error # shortcut for the method self.option = Option().load_from_file("./option.json") self.ct_config = CT_Config() diff --git a/source/Error.py b/source/Error.py index 68c3454..4693cd2 100644 --- a/source/Error.py +++ b/source/Error.py @@ -1,3 +1,8 @@ +from tkinter import messagebox +import traceback +import os + + class RomAlreadyPatched(Exception): def __init__(self): super().__init__("ROM Already patched !") @@ -31,3 +36,28 @@ class CantConvertTrack(Exception): class MissingTrackWU8(Exception): def __init__(self): super().__init__("The original wu8 track file is missing !") + + +class ErrorLogger: + def __init__(self, common): + self.common = common + + def log_error(self) -> None: + """ + When an error occur, will show it in a messagebox and write it in error.log + """ + error = traceback.format_exc() + with open("./error.log", "a") as f: + f.write( + f"---\n" + f"For game version : {self.common.ct_config.version}\n" + f"./file/ directory : {os.listdir('./file/')}\n" + f"ctconfig directory : {os.listdir(self.common.ct_config.pack_path)}\n" + f"GAME/files/ information : {self.common.game.path, self.common.game.region}\n" + f"{error}\n" + ) + + messagebox.showerror( + self.common.translate("Error"), + self.common.translate("An error occured", " :", "\n", error, "\n\n") + ) \ No newline at end of file diff --git a/source/Game.py b/source/Game.py index db0c3e1..df66116 100644 --- a/source/Game.py +++ b/source/Game.py @@ -1,4 +1,3 @@ -from tkinter import messagebox from PIL import Image, ImageDraw, ImageFont import shutil import glob @@ -264,7 +263,7 @@ class Game: messagebox.showinfo(self.common.translate("End"), self.common.translate("The mod have been installed !")) except Exception as e: - self.common.gui_main.log_error() + self.common.log_error() raise e finally: @@ -442,7 +441,7 @@ class Game: self.patch_tracks() except Exception as e: - self.common.gui_main.log_error() + self.common.log_error() raise e finally: diff --git a/source/Gui/Main.py b/source/Gui/Main.py index dd204a2..5b02fba 100644 --- a/source/Gui/Main.py +++ b/source/Gui/Main.py @@ -1,8 +1,7 @@ from distutils.version import StrictVersion -from tkinter import filedialog, ttk, messagebox +from tkinter import filedialog, ttk from tkinter import * import webbrowser -import traceback import requests import zipfile import glob @@ -105,7 +104,7 @@ class Main: messagebox.showerror(self.common.translate("Error"), self.common.translate("This game's format is invalid")) raise InvalidFormat except Exception as e: - self.log_error() + self.common.log_error() raise e finally: self.progress(show=False) @@ -309,26 +308,7 @@ class Main: self.common.option.disable_download = True except: - self.log_error() - - def log_error(self) -> None: - """ - When an error occur, will show it in a messagebox and write it in error.log - """ - error = traceback.format_exc() - with open("./error.log", "a") as f: - f.write( - f"---\n" - f"For game version : {self.common.ct_config.version}\n" - f"./file/ directory : {os.listdir('./file/')}\n" - f"ctconfig directory : {os.listdir(self.common.ct_config.pack_path)}\n" - f"GAME/files/ information : {self.common.game.path, self.common.game.region}\n" - f"{error}\n" - ) - messagebox.showerror( - self.common.translate("Error"), - self.common.translate("An error occured", " :", "\n", error, "\n\n") - ) + self.common.log_error() def progress(self, show: bool = None, indeter: bool = None, step: int = None, statut: str = None, max: int = None, add: int = None) -> None: