1
0
Fork 0
mirror of synced 2024-07-01 12:40:28 +12:00
Rare/rare/lgndr/api_monkeys.py
loathingKernel ee5adce18b Implement LgndrIndirectLogger to return the last logged message from LegendaryAPI
The indirect return is stored in a `LgndrIndirectStatus` object that provides checking and unpacking features

Lgndr: `LgndrInstallGameArgs.install_tag` default to None
Lgndr: add default `move` method.
Lgndr: monkeypatch `dlm.status_queue` after preparing a download to reduce `InstallQueueItemModel`
InstallOptionsModel: implement `as_install_kwargs` to pass only relevant arguments to `LgndrInstallGameArgs` in InstallDialog
InstallOptionsModel: rename `sdl_list` to `install_tag` as they were the same thing
LegendaryUtils: Update to use `LgndrIndirectStatus`
UninstallDialog: Add option to keep configuration decoupled from keeping game data
GameUtils: Add messagebox to show error messages from legendary after uninstalling a game
InstallDialog: Update to use `LgndrIndirectStatus`
InstallDialog: Update selectable download handling to match legendary's
DownloadThread: Remove multiple instance variables, instead reference them directly from `InstallQueueItemModel` instance
ImportGroup: Replace `info_label` with an `ElideLabel` for displaying long messages
ImportGroup: Don't translate message strings in the `ImportWorker`
GameInfo: Call `repair()` if needed after verification instead of handling it locally
GamesTab: Fix string matching for capitalized strings and scroll to top on when searching
2022-08-02 10:42:38 +03:00

92 lines
2.2 KiB
Python

import logging
from dataclasses import dataclass
from PyQt5.QtWidgets import QMessageBox, QLabel
def get_boolean_choice(prompt, default=True):
choice = QMessageBox.question(
None, "Import DLCs?", prompt,
defaultButton=QMessageBox.Yes if default else QMessageBox.No
)
return True if choice == QMessageBox.StandardButton.Yes else False
def return_exit(__status):
return __status
class UILogHandler(logging.Handler):
def __init__(self, dest: QLabel):
super(UILogHandler, self).__init__()
self.widget = dest
def emit(self, record: logging.LogRecord) -> None:
self.widget.setText(record.getMessage())
@dataclass
class LgndrIndirectStatus:
success: bool = False
message: str = ""
def __len__(self):
if self.message:
return 2
else:
return 0
def __bool__(self):
return self.success
def __getitem__(self, item):
if item == 0:
return self.success
elif item == 1:
return self.message
else:
raise IndexError
def __iter__(self):
return iter((self.success, self.message))
def __str__(self):
return self.message
class LgndrIndirectLogger:
def __init__(self, status: LgndrIndirectStatus, logger: logging.Logger = None, level: int = logging.ERROR):
self.logger = logger
self.level = level
self.status = status
def set_logger(self, logger: logging.Logger):
self.logger = logger
def set_level(self, level: int):
self.level = level
def _log(self, level: int, msg: str):
self.status.success = True if level < self.level else False
self.status.message = msg
if self.logger:
self.logger.log(level, msg)
def debug(self, msg: str):
self._log(logging.DEBUG, msg)
def info(self, msg: str):
self._log(logging.INFO, msg)
def warning(self, msg: str):
self._log(logging.WARNING, msg)
def error(self, msg: str):
self._log(logging.ERROR, msg)
def critical(self, msg: str):
self._log(logging.CRITICAL, msg)
def fatal(self, msg: str):
self.critical(msg)