From fa31dddd7ea1ba46fb64663e83d9d27f94373c9b Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Wed, 13 Mar 2024 00:12:20 -0500 Subject: [PATCH] cleanup --- Libraries/PyKotor/src/pykotor/common/misc.py | 4 +++- .../PyKotorGL/src/pykotor/gl/models/mdl.py | 1 - Tools/HolocronToolset/src/toolset/__main__.py | 2 +- .../src/toolset/gui/dialogs/asyncloader.py | 3 ++- .../src/toolset/gui/dialogs/insert_instance.py | 1 + .../src/toolset/gui/dialogs/select_module.py | 1 + .../HolocronToolset/src/toolset/gui/editor.py | 6 ++++-- .../src/toolset/gui/editors/are.py | 3 +-- .../src/toolset/gui/editors/erf.py | 3 ++- .../src/toolset/gui/editors/gff.py | 6 ------ .../src/toolset/gui/editors/tlk.py | 1 - .../src/toolset/gui/editors/twoda.py | 7 ++++--- .../src/toolset/gui/editors/utd.py | 1 - .../toolset/gui/widgets/renderer/walkmesh.py | 1 + .../src/toolset/gui/windows/help.py | 18 ++++++++++++++---- .../src/toolset/gui/windows/main.py | 2 +- .../src/toolset/gui/windows/module_designer.py | 4 ++-- 17 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Libraries/PyKotor/src/pykotor/common/misc.py b/Libraries/PyKotor/src/pykotor/common/misc.py index 4646484b4..9fd17a98a 100644 --- a/Libraries/PyKotor/src/pykotor/common/misc.py +++ b/Libraries/PyKotor/src/pykotor/common/misc.py @@ -3,7 +3,7 @@ from __future__ import annotations -from collections.abc import ItemsView, Iterable, Iterator, Mapping +from collections.abc import Mapping from enum import Enum, IntEnum from typing import TYPE_CHECKING, ClassVar, Generic, TypeVar, overload @@ -13,6 +13,8 @@ if TYPE_CHECKING: import os + from collections.abc import ItemsView, Iterable, Iterator + T = TypeVar("T") VT = TypeVar("VT") _unique_sentinel = object() diff --git a/Libraries/PyKotorGL/src/pykotor/gl/models/mdl.py b/Libraries/PyKotorGL/src/pykotor/gl/models/mdl.py index dbe70a4b3..67f9564e7 100644 --- a/Libraries/PyKotorGL/src/pykotor/gl/models/mdl.py +++ b/Libraries/PyKotorGL/src/pykotor/gl/models/mdl.py @@ -1,7 +1,6 @@ from __future__ import annotations import ctypes -import io import math import struct diff --git a/Tools/HolocronToolset/src/toolset/__main__.py b/Tools/HolocronToolset/src/toolset/__main__.py index f379105aa..bf2622001 100644 --- a/Tools/HolocronToolset/src/toolset/__main__.py +++ b/Tools/HolocronToolset/src/toolset/__main__.py @@ -10,7 +10,7 @@ from typing import TYPE_CHECKING -from PyQt5.QtCore import QFile, QTextStream, QThread +from PyQt5.QtCore import QThread from PyQt5.QtWidgets import QApplication if TYPE_CHECKING: diff --git a/Tools/HolocronToolset/src/toolset/gui/dialogs/asyncloader.py b/Tools/HolocronToolset/src/toolset/gui/dialogs/asyncloader.py index bb09fef14..cbeadb689 100644 --- a/Tools/HolocronToolset/src/toolset/gui/dialogs/asyncloader.py +++ b/Tools/HolocronToolset/src/toolset/gui/dialogs/asyncloader.py @@ -106,7 +106,8 @@ def _onFailed(self, error: Exception): file.write("\n----------------------\n") if self.errorTitle: - QMessageBox(QMessageBox.Critical, self.errorTitle, str(universal_simplify_exception(error))).exec_() + error_msg = str(universal_simplify_exception(error)).replace("\n", "
") + QMessageBox(QMessageBox.Critical, self.errorTitle, error_msg).exec_() class AsyncWorker(QThread): diff --git a/Tools/HolocronToolset/src/toolset/gui/dialogs/insert_instance.py b/Tools/HolocronToolset/src/toolset/gui/dialogs/insert_instance.py index 9fafc9db5..1ab02686f 100644 --- a/Tools/HolocronToolset/src/toolset/gui/dialogs/insert_instance.py +++ b/Tools/HolocronToolset/src/toolset/gui/dialogs/insert_instance.py @@ -25,6 +25,7 @@ if TYPE_CHECKING: from PyQt5.QtWidgets import QWidget + from pykotor.common.module import Module from pykotor.extract.file import FileResource from toolset.data.installation import HTInstallation diff --git a/Tools/HolocronToolset/src/toolset/gui/dialogs/select_module.py b/Tools/HolocronToolset/src/toolset/gui/dialogs/select_module.py index fd73f156a..18d605f30 100644 --- a/Tools/HolocronToolset/src/toolset/gui/dialogs/select_module.py +++ b/Tools/HolocronToolset/src/toolset/gui/dialogs/select_module.py @@ -10,6 +10,7 @@ if TYPE_CHECKING: from PyQt5.QtWidgets import QWidget + from pykotor.common.misc import CaseInsensitiveDict from toolset.data.installation import HTInstallation diff --git a/Tools/HolocronToolset/src/toolset/gui/editor.py b/Tools/HolocronToolset/src/toolset/gui/editor.py index 2e7fca9eb..3b4ca2dd9 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editor.py +++ b/Tools/HolocronToolset/src/toolset/gui/editor.py @@ -202,10 +202,11 @@ def saveAs(self): identifier = ResourceIdentifier.from_path(filepath_str).validate() except ValueError as e: print(format_exception_with_variables(e)) + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") QMessageBox( QMessageBox.Critical, "Invalid filename/extension", - f"Check the filename and try again. Could not save!{os.linesep * 2}{universal_simplify_exception(e)}", + f"Check the filename and try again. Could not save!

{error_msg}", ).exec_() return @@ -265,7 +266,8 @@ def save(self): lines = format_exception_with_variables(e) file.writelines(lines) file.write("\n----------------------\n") - QMessageBox(QMessageBox.Critical, "Failed to write to file", str(universal_simplify_exception(e))).exec_() + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") + QMessageBox(QMessageBox.Critical, "Failed to write to file", error_msg).exec_() def _saveCurrentFile(self): """Implementation of saving the current file. diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/are.py b/Tools/HolocronToolset/src/toolset/gui/editors/are.py index 77db160a1..cb1dfbfb8 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/are.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/are.py @@ -19,12 +19,11 @@ from toolset.gui.editor import Editor if TYPE_CHECKING: - from PyQt5.QtWidgets import QLabel, QWidget import os - from pykotor.extract.file import ResourceResult from PyQt5.QtWidgets import QLabel, QWidget + from pykotor.extract.file import ResourceResult from pykotor.resource.formats.bwm.bwm_data import BWM from pykotor.resource.formats.lyt.lyt_data import LYT from pykotor.resource.formats.tpc.tpc_data import TPC diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/erf.py b/Tools/HolocronToolset/src/toolset/gui/editors/erf.py index d0fe17483..8e7710620 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/erf.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/erf.py @@ -298,10 +298,11 @@ def addResources(self, filepaths: list[str]): lines = format_exception_with_variables(e) file.writelines(lines) file.write("\n----------------------\n") + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") QMessageBox( QMessageBox.Critical, "Failed to add resource", - f"Could not add resource at {c_filepath.absolute()}:\n{universal_simplify_exception(e)}", + f"Could not add resource at {c_filepath.absolute()}:

{error_msg}", ).exec_() def selectFilesToAdd(self): diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/gff.py b/Tools/HolocronToolset/src/toolset/gui/editors/gff.py index 531f7c8cd..a0e258d3b 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/gff.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/gff.py @@ -809,12 +809,6 @@ def selectTalkTable(self): Args: ---- self: The class instance - - Processing Logic: - ---------------- - - Open a file dialog to select a TLK file - - Get the selected file path and filter from the dialog - - If a file is selected, load it as a TalkTable and assign to self._talktable. """ filepath, filter = QFileDialog.getOpenFileName(self, "Select a TLK file", "", "TalkTable (*.tlk)") if not filepath: diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/tlk.py b/Tools/HolocronToolset/src/toolset/gui/editors/tlk.py index b3fb947b8..6de9bf62c 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/tlk.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/tlk.py @@ -17,7 +17,6 @@ from toolset.gui.editor import Editor from toolset.gui.widgets.settings.installations import GlobalSettings from toolset.utils.window import addWindow, openResourceEditor -from utility.misc import is_debug_mode if TYPE_CHECKING: import os diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/twoda.py b/Tools/HolocronToolset/src/toolset/gui/editors/twoda.py index 8783e0383..1a0872be2 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/twoda.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/twoda.py @@ -12,7 +12,7 @@ from pykotor.resource.formats.twoda import TwoDA, read_2da, write_2da from pykotor.resource.type import ResourceType from toolset.gui.editor import Editor -from utility.error_handling import assert_with_variable_trace +from utility.error_handling import assert_with_variable_trace, universal_simplify_exception if TYPE_CHECKING: import os @@ -117,8 +117,9 @@ def load(self, filepath: os.PathLike | str, resref: str, restype: ResourceType, try: self._load_main(data) - except ValueError: - QMessageBox(QMessageBox.Critical, "Failed to load file.", "Failed to open or load file data.").exec_() + except ValueError as e: + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") + QMessageBox(QMessageBox.Critical, "Failed to load file.", f"Failed to open or load file data.
{error_msg}").exec_() self.proxyModel.setSourceModel(self.model) self.new() diff --git a/Tools/HolocronToolset/src/toolset/gui/editors/utd.py b/Tools/HolocronToolset/src/toolset/gui/editors/utd.py index 79ac5703b..4f23f70f0 100644 --- a/Tools/HolocronToolset/src/toolset/gui/editors/utd.py +++ b/Tools/HolocronToolset/src/toolset/gui/editors/utd.py @@ -19,7 +19,6 @@ from toolset.utils.window import openResourceEditor if TYPE_CHECKING: - from PyQt5.QtWidgets import QWidget import os from PyQt5.QtCore import QObject diff --git a/Tools/HolocronToolset/src/toolset/gui/widgets/renderer/walkmesh.py b/Tools/HolocronToolset/src/toolset/gui/widgets/renderer/walkmesh.py index e9d34a1b7..a7d578821 100644 --- a/Tools/HolocronToolset/src/toolset/gui/widgets/renderer/walkmesh.py +++ b/Tools/HolocronToolset/src/toolset/gui/widgets/renderer/walkmesh.py @@ -42,6 +42,7 @@ QPaintEvent, QWheelEvent, ) + from pykotor.common.geometry import SurfaceMaterial from pykotor.resource.formats.bwm import BWM, BWMFace from pykotor.resource.formats.tpc import TPC diff --git a/Tools/HolocronToolset/src/toolset/gui/windows/help.py b/Tools/HolocronToolset/src/toolset/gui/windows/help.py index a2fdc2d0c..47c894855 100644 --- a/Tools/HolocronToolset/src/toolset/gui/windows/help.py +++ b/Tools/HolocronToolset/src/toolset/gui/windows/help.py @@ -76,7 +76,7 @@ def _setupContentsRecJSON(self, parent: QTreeWidgetItem | None, data: dict): if "structure" in data: for title in data["structure"]: item = QTreeWidgetItem([title]) - item.setData(0, QtCore.Qt.UserRole, data["structure"][title]["filename"]) # type: ignore[attr-defined] + item.setData(0, QtCore.Qt.UserRole, data["structure"][title]["filename"]) add(item) self._setupContentsRecJSON(item, data["structure"][title]) @@ -84,7 +84,7 @@ def _setupContentsRecXML(self, parent: QTreeWidgetItem | None, element: ElemTree add: Callable[..., None] = self.ui.contentsTree.addTopLevelItem if parent is None else parent.addChild for child in element: - item = QTreeWidgetItem([child.get("name")]) # FIXME: typing + item = QTreeWidgetItem([child.get("name", "")]) item.setData(0, QtCore.Qt.UserRole, child.get("file")) add(item) self._setupContentsRecXML(item, child) @@ -170,17 +170,27 @@ def task(): loader = AsyncLoader(self, "Download newer help files...", task, "Failed to update.") if loader.exec_(): self._setupContents() - except Exception as e: + except (ConnectionError, requests.HTTPError, requests.ConnectionError, requests.RequestException): + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") QMessageBox( QMessageBox.Information, "Unable to fetch latest version of the help booklet.", ( - f"{universal_simplify_exception(e)}\n" + f"{error_msg}
" "Check if you are connected to the internet." ), QMessageBox.Ok, self, ).exec_() + except Exception as e: + error_msg = str(universal_simplify_exception(e)).replace("\n", "
") + QMessageBox( + QMessageBox.Information, + "An unexpected error occurred while fetching the help booklet.", + error_msg, + QMessageBox.Ok, + self, + ).exec_() def _downloadUpdate(self): help_path = Path("help").resolve() diff --git a/Tools/HolocronToolset/src/toolset/gui/windows/main.py b/Tools/HolocronToolset/src/toolset/gui/windows/main.py index 4d4118120..62bebbb92 100644 --- a/Tools/HolocronToolset/src/toolset/gui/windows/main.py +++ b/Tools/HolocronToolset/src/toolset/gui/windows/main.py @@ -1061,7 +1061,7 @@ def refreshModuleList( def _getOverrideList(self, *, reload=True): if self.active is None: print("no installation is currently loaded, cannot refresh override list") - return + return None if reload: self.active.load_override() diff --git a/Tools/HolocronToolset/src/toolset/gui/windows/module_designer.py b/Tools/HolocronToolset/src/toolset/gui/windows/module_designer.py index b58867370..f95913624 100644 --- a/Tools/HolocronToolset/src/toolset/gui/windows/module_designer.py +++ b/Tools/HolocronToolset/src/toolset/gui/windows/module_designer.py @@ -17,7 +17,6 @@ from pykotor.common.module import Module, ModuleResource from pykotor.common.stream import BinaryWriter from pykotor.extract.file import ResourceIdentifier -from pykotor.resource.formats.bwm.bwm_data import BWM from pykotor.resource.generics.git import ( GITCamera, GITCreature, @@ -47,7 +46,6 @@ from toolset.utils.misc import QtMouse from toolset.utils.window import openResourceEditor from utility.error_handling import assert_with_variable_trace -from utility.system.path import Path if TYPE_CHECKING: from PyQt5.QtGui import QFont, QKeyEvent @@ -55,6 +53,7 @@ from glm import vec3 from pykotor.gl.scene import Camera + from pykotor.resource.formats.bwm.bwm_data import BWM from pykotor.resource.generics.are import ARE from pykotor.resource.generics.git import GIT from pykotor.resource.generics.ifo import IFO @@ -62,6 +61,7 @@ from toolset.data.installation import HTInstallation from toolset.gui.widgets.renderer.module import ModuleRenderer from toolset.gui.widgets.renderer.walkmesh import WalkmeshRenderer + from utility.system.path import Path class MoveCommand(QUndoCommand):