1
0
Fork 0
mirror of synced 2024-06-17 10:04:43 +12:00

Use a loggin handler in stead of monkeypatching for raising LgndrException

This commit is contained in:
loathingKernel 2022-06-26 14:30:17 +03:00
parent 78fb13830e
commit 2c2f44c97c
4 changed files with 54 additions and 25 deletions

View file

@ -12,6 +12,7 @@ from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtWidgets import QFileDialog, QGroupBox, QCompleter, QTreeView, QHeaderView, qApp, QMessageBox
from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.exception import LgndrException
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ApiResultsSingleton
from rare.ui.components.tabs.games.import_sync.import_group import Ui_ImportGroup
from rare.utils import legendary_utils
@ -307,8 +308,11 @@ class ImportGroup(QGroupBox):
skip_dlcs=False,
with_dlcs=False,
yes=False,
log_dest=self.ui.info_label,
)
cli = LegendaryCLI()
cli.core = LegendaryCoreSingleton()
cli.import_game(args)
try:
cli.import_game(args)
except LgndrException as ret:
self.ui.info_label.setText(ret.message)

View file

@ -2,11 +2,10 @@ import logging
import legendary.cli
from PyQt5.QtWidgets import QLabel, QMessageBox
from legendary.cli import LegendaryCLI as LegendaryCLIReal
from legendary.cli import LegendaryCLI as LegendaryCLIReal, logger
from .core import LegendaryCore
logger = logging.getLogger('cli')
from .exception import LgndrException, LgndrLogHandler
def get_boolean_choice(message):
@ -31,10 +30,14 @@ class LegendaryCLI(LegendaryCLIReal):
self.logging_queue = None
def import_game(self, args):
handler = UILogHandler(args.log_dest)
handler = LgndrLogHandler()
logger.addHandler(handler)
old_choice = legendary.cli.get_boolean_choice
legendary.cli.get_boolean_choice = get_boolean_choice
super(LegendaryCLI, self).import_game(args)
legendary.cli.get_boolean_choice = old_choice
logger.removeHandler(handler)
try:
super(LegendaryCLI, self).import_game(args)
except LgndrException as e:
raise e
finally:
legendary.cli.get_boolean_choice = old_choice
logger.removeHandler(handler)

View file

@ -1,3 +1,4 @@
import logging
import os
import sys
@ -12,14 +13,11 @@ from legendary.utils.selective_dl import get_sdl_appname
from legendary.core import LegendaryCore as LegendaryCoreReal
from .manager import DLManager
from .exception import LgndrException
from .exception import LgndrException, LgndrLogHandler
class LegendaryCore(LegendaryCoreReal):
def __log_exception(self, error):
raise LgndrException(error)
def prepare_download(self, app_name: str, base_path: str = '',
status_q: Queue = None, max_shm: int = 0, max_workers: int = 0,
force: bool = False, disable_patching: bool = False,
@ -148,8 +146,8 @@ class LegendaryCore(LegendaryCoreReal):
preferred_cdn=preferred_cdn,
status_q=status_q,
disable_https=disable_https)
dlm.run_real = DLManager.run_real.__get__(dlm, DLManager)
# game is either up to date or hasn't changed, so we have nothing to do
if not analysis.dl_size:
self.log.info('Download size is 0, the game is either already up to date or has not changed. Exiting...')
@ -235,17 +233,22 @@ class LegendaryCore(LegendaryCoreReal):
self.install_game(old_igame)
def egl_import(self, app_name):
__log_error = self.log.error
__log_fatal = self.log.fatal
self.log.error = self.__log_exception
self.log.fatal = self.__log_exception
super(LegendaryCore, self).egl_import(app_name)
self.log.error = __log_error
self.log.fatal = __log_fatal
handler = LgndrLogHandler()
self.log.addHandler(handler)
try:
super(LegendaryCore, self).egl_import(app_name)
except LgndrException as ret:
raise ret
finally:
self.log.removeHandler(handler)
def egl_export(self, app_name):
__log_error = self.log.error
self.log.error = self.__log_exception
super(LegendaryCore, self).egl_export(app_name)
self.log.error = __log_error
handler = LgndrLogHandler()
self.log.addHandler(handler)
try:
super(LegendaryCore, self).egl_export(app_name)
except LgndrException as ret:
raise ret
finally:
self.log.removeHandler(handler)

View file

@ -1,4 +1,23 @@
import logging
import warnings
class LgndrException(RuntimeError):
def __init__(self, message="Error in Legendary"):
self.message = message
super(LgndrException, self).__init__(self.message)
class LgndrWarning(RuntimeWarning):
def __init__(self, message="Warning in Legendary"):
self.message = message
super(LgndrWarning, self).__init__(self.message)
class LgndrLogHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
# lk: FATAL is the same as CRITICAL
if record.levelno == logging.ERROR or record.levelno == logging.CRITICAL:
raise LgndrException(record.getMessage())
if record.levelno == logging.INFO or record.levelno == logging.WARNING:
warnings.warn(record.getMessage())