1
0
Fork 0
mirror of synced 2024-06-26 18:20:50 +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 PyQt5.QtWidgets import QFileDialog, QGroupBox, QCompleter, QTreeView, QHeaderView, qApp, QMessageBox
from rare.lgndr.cli import LegendaryCLI from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.exception import LgndrException
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ApiResultsSingleton from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ApiResultsSingleton
from rare.ui.components.tabs.games.import_sync.import_group import Ui_ImportGroup from rare.ui.components.tabs.games.import_sync.import_group import Ui_ImportGroup
from rare.utils import legendary_utils from rare.utils import legendary_utils
@ -307,8 +308,11 @@ class ImportGroup(QGroupBox):
skip_dlcs=False, skip_dlcs=False,
with_dlcs=False, with_dlcs=False,
yes=False, yes=False,
log_dest=self.ui.info_label,
) )
cli = LegendaryCLI() cli = LegendaryCLI()
cli.core = LegendaryCoreSingleton() 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 import legendary.cli
from PyQt5.QtWidgets import QLabel, QMessageBox 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 from .core import LegendaryCore
from .exception import LgndrException, LgndrLogHandler
logger = logging.getLogger('cli')
def get_boolean_choice(message): def get_boolean_choice(message):
@ -31,10 +30,14 @@ class LegendaryCLI(LegendaryCLIReal):
self.logging_queue = None self.logging_queue = None
def import_game(self, args): def import_game(self, args):
handler = UILogHandler(args.log_dest) handler = LgndrLogHandler()
logger.addHandler(handler) logger.addHandler(handler)
old_choice = legendary.cli.get_boolean_choice old_choice = legendary.cli.get_boolean_choice
legendary.cli.get_boolean_choice = get_boolean_choice legendary.cli.get_boolean_choice = get_boolean_choice
super(LegendaryCLI, self).import_game(args) try:
legendary.cli.get_boolean_choice = old_choice super(LegendaryCLI, self).import_game(args)
logger.removeHandler(handler) 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 os
import sys import sys
@ -12,14 +13,11 @@ from legendary.utils.selective_dl import get_sdl_appname
from legendary.core import LegendaryCore as LegendaryCoreReal from legendary.core import LegendaryCore as LegendaryCoreReal
from .manager import DLManager from .manager import DLManager
from .exception import LgndrException from .exception import LgndrException, LgndrLogHandler
class LegendaryCore(LegendaryCoreReal): class LegendaryCore(LegendaryCoreReal):
def __log_exception(self, error):
raise LgndrException(error)
def prepare_download(self, app_name: str, base_path: str = '', def prepare_download(self, app_name: str, base_path: str = '',
status_q: Queue = None, max_shm: int = 0, max_workers: int = 0, status_q: Queue = None, max_shm: int = 0, max_workers: int = 0,
force: bool = False, disable_patching: bool = False, force: bool = False, disable_patching: bool = False,
@ -148,8 +146,8 @@ class LegendaryCore(LegendaryCoreReal):
preferred_cdn=preferred_cdn, preferred_cdn=preferred_cdn,
status_q=status_q, status_q=status_q,
disable_https=disable_https) disable_https=disable_https)
dlm.run_real = DLManager.run_real.__get__(dlm, DLManager) 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 # game is either up to date or hasn't changed, so we have nothing to do
if not analysis.dl_size: 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...') 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) self.install_game(old_igame)
def egl_import(self, app_name): def egl_import(self, app_name):
__log_error = self.log.error handler = LgndrLogHandler()
__log_fatal = self.log.fatal self.log.addHandler(handler)
self.log.error = self.__log_exception try:
self.log.fatal = self.__log_exception super(LegendaryCore, self).egl_import(app_name)
super(LegendaryCore, self).egl_import(app_name) except LgndrException as ret:
self.log.error = __log_error raise ret
self.log.fatal = __log_fatal finally:
self.log.removeHandler(handler)
def egl_export(self, app_name): def egl_export(self, app_name):
__log_error = self.log.error handler = LgndrLogHandler()
self.log.error = self.__log_exception self.log.addHandler(handler)
super(LegendaryCore, self).egl_export(app_name) try:
self.log.error = __log_error 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): class LgndrException(RuntimeError):
def __init__(self, message="Error in Legendary"): def __init__(self, message="Error in Legendary"):
self.message = message self.message = message
super(LgndrException, self).__init__(self.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())