From 1ff6f1768b8d8a0c7dd10a880ea4afa2a4dddcb4 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sat, 25 Nov 2023 12:32:50 -0800 Subject: [PATCH] Use importlib.resources instead of __file__ --- comicapi/data/__init__.py | 4 ++-- comictaggerlib/applicationlogwindow.py | 3 ++- comictaggerlib/autotagmatchwindow.py | 3 ++- comictaggerlib/autotagprogresswindow.py | 3 ++- comictaggerlib/autotagstartwindow.py | 3 ++- comictaggerlib/coverimagewidget.py | 3 ++- comictaggerlib/crediteditorwindow.py | 3 ++- comictaggerlib/exportwindow.py | 3 ++- comictaggerlib/fileselectionlist.py | 3 ++- comictaggerlib/graphics/__init__.py | 4 ++-- comictaggerlib/imagepopup.py | 3 ++- comictaggerlib/issueselectionwindow.py | 3 ++- comictaggerlib/logwindow.py | 3 ++- comictaggerlib/matchselectionwindow.py | 3 ++- comictaggerlib/pagebrowser.py | 3 ++- comictaggerlib/pagelisteditor.py | 3 ++- comictaggerlib/progresswindow.py | 3 ++- comictaggerlib/renamewindow.py | 3 ++- comictaggerlib/seriesselectionwindow.py | 3 ++- comictaggerlib/settingswindow.py | 6 ++++-- comictaggerlib/taggerwindow.py | 3 ++- comictaggerlib/ui/__init__.py | 4 ++-- testing/filenames.py | 5 +++-- tests/comicarchive_test.py | 7 ++++--- 24 files changed, 53 insertions(+), 31 deletions(-) diff --git a/comicapi/data/__init__.py b/comicapi/data/__init__.py index 0324902..e5952ca 100644 --- a/comicapi/data/__init__.py +++ b/comicapi/data/__init__.py @@ -1,5 +1,5 @@ from __future__ import annotations -import pathlib +import importlib.resources -data_path = pathlib.Path(__file__).parent +data_path = importlib.resources.files(__package__) diff --git a/comictaggerlib/applicationlogwindow.py b/comictaggerlib/applicationlogwindow.py index afece95..7505595 100644 --- a/comictaggerlib/applicationlogwindow.py +++ b/comictaggerlib/applicationlogwindow.py @@ -25,7 +25,8 @@ class QTextEditLogger(QtCore.QObject, logging.Handler): class ApplicationLogWindow(QtWidgets.QDialog): def __init__(self, log_handler: QTextEditLogger, parent: QtCore.QObject | None = None) -> None: super().__init__(parent) - uic.loadUi(ui_path / "logwindow.ui", self) + with (ui_path / "logwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.log_handler = log_handler self.log_handler.qlog.connect(self.textEdit.append) diff --git a/comictaggerlib/autotagmatchwindow.py b/comictaggerlib/autotagmatchwindow.py index 4df71d9..6e1b9f3 100644 --- a/comictaggerlib/autotagmatchwindow.py +++ b/comictaggerlib/autotagmatchwindow.py @@ -45,7 +45,8 @@ class AutoTagMatchWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "matchselectionwindow.ui", self) + with (ui_path / "matchselectionwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.config = config diff --git a/comictaggerlib/autotagprogresswindow.py b/comictaggerlib/autotagprogresswindow.py index 5650f55..6864fe3 100644 --- a/comictaggerlib/autotagprogresswindow.py +++ b/comictaggerlib/autotagprogresswindow.py @@ -31,7 +31,8 @@ class AutoTagProgressWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, talker: ComicTalker) -> None: super().__init__(parent) - uic.loadUi(ui_path / "autotagprogresswindow.ui", self) + with (ui_path / "autotagprogresswindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.archiveCoverWidget = CoverImageWidget( self.archiveCoverContainer, CoverImageWidget.DataMode, None, None, False diff --git a/comictaggerlib/autotagstartwindow.py b/comictaggerlib/autotagstartwindow.py index 957a941..4e44448 100644 --- a/comictaggerlib/autotagstartwindow.py +++ b/comictaggerlib/autotagstartwindow.py @@ -29,7 +29,8 @@ class AutoTagStartWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, config: ct_ns, msg: str) -> None: super().__init__(parent) - uic.loadUi(ui_path / "autotagstartwindow.ui", self) + with (ui_path / "autotagstartwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.label.setText(msg) self.setWindowFlags( diff --git a/comictaggerlib/coverimagewidget.py b/comictaggerlib/coverimagewidget.py index a3cea01..43d87f4 100644 --- a/comictaggerlib/coverimagewidget.py +++ b/comictaggerlib/coverimagewidget.py @@ -78,7 +78,8 @@ class CoverImageWidget(QtWidgets.QWidget): else: self.cover_fetcher = ImageFetcher(cache_folder) self.talker = None - uic.loadUi(ui_path / "coverimagewidget.ui", self) + with (ui_path / "coverimagewidget.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) reduce_widget_font_size(self.label) diff --git a/comictaggerlib/crediteditorwindow.py b/comictaggerlib/crediteditorwindow.py index f94c294..70ae1e0 100644 --- a/comictaggerlib/crediteditorwindow.py +++ b/comictaggerlib/crediteditorwindow.py @@ -32,7 +32,8 @@ class CreditEditorWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, mode: int, role: str, name: str, primary: bool) -> None: super().__init__(parent) - uic.loadUi(ui_path / "crediteditorwindow.ui", self) + with (ui_path / "crediteditorwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.mode = mode diff --git a/comictaggerlib/exportwindow.py b/comictaggerlib/exportwindow.py index 595e443..0dba3c9 100644 --- a/comictaggerlib/exportwindow.py +++ b/comictaggerlib/exportwindow.py @@ -34,7 +34,8 @@ class ExportWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, msg: str) -> None: super().__init__(parent) - uic.loadUi(ui_path / "exportwindow.ui", self) + with (ui_path / "exportwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.label.setText(msg) self.setWindowFlags( diff --git a/comictaggerlib/fileselectionlist.py b/comictaggerlib/fileselectionlist.py index 2d8a0b2..de8e26d 100644 --- a/comictaggerlib/fileselectionlist.py +++ b/comictaggerlib/fileselectionlist.py @@ -61,7 +61,8 @@ class FileSelectionList(QtWidgets.QWidget): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "fileselectionlist.ui", self) + with (ui_path / "fileselectionlist.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.config = config diff --git a/comictaggerlib/graphics/__init__.py b/comictaggerlib/graphics/__init__.py index 96eac3d..3e9ceb6 100644 --- a/comictaggerlib/graphics/__init__.py +++ b/comictaggerlib/graphics/__init__.py @@ -1,5 +1,5 @@ from __future__ import annotations -import pathlib +import importlib.resources -graphics_path = pathlib.Path(__file__).parent +graphics_path = importlib.resources.files(__package__) diff --git a/comictaggerlib/imagepopup.py b/comictaggerlib/imagepopup.py index 445e4a2..6e27291 100644 --- a/comictaggerlib/imagepopup.py +++ b/comictaggerlib/imagepopup.py @@ -29,7 +29,8 @@ class ImagePopup(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, image_pixmap: QtGui.QPixmap) -> None: super().__init__(parent) - uic.loadUi(ui_path / "imagepopup.ui", self) + with (ui_path / "imagepopup.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor)) diff --git a/comictaggerlib/issueselectionwindow.py b/comictaggerlib/issueselectionwindow.py index 8818bee..3abee0a 100644 --- a/comictaggerlib/issueselectionwindow.py +++ b/comictaggerlib/issueselectionwindow.py @@ -49,7 +49,8 @@ class IssueSelectionWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "issueselectionwindow.ui", self) + with (ui_path / "issueselectionwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.coverWidget = CoverImageWidget( self.coverImageContainer, diff --git a/comictaggerlib/logwindow.py b/comictaggerlib/logwindow.py index 8b3bda8..34e7ca3 100644 --- a/comictaggerlib/logwindow.py +++ b/comictaggerlib/logwindow.py @@ -28,7 +28,8 @@ class LogWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget) -> None: super().__init__(parent) - uic.loadUi(ui_path / "logwindow.ui", self) + with (ui_path / "logwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.setWindowFlags( QtCore.Qt.WindowType( diff --git a/comictaggerlib/matchselectionwindow.py b/comictaggerlib/matchselectionwindow.py index cd64148..7106372 100644 --- a/comictaggerlib/matchselectionwindow.py +++ b/comictaggerlib/matchselectionwindow.py @@ -42,7 +42,8 @@ class MatchSelectionWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "matchselectionwindow.ui", self) + with (ui_path / "matchselectionwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.altCoverWidget = CoverImageWidget( self.altCoverContainer, CoverImageWidget.AltCoverMode, config.Runtime_Options__config.user_cache_dir, talker diff --git a/comictaggerlib/pagebrowser.py b/comictaggerlib/pagebrowser.py index 567c72e..c2a20f3 100644 --- a/comictaggerlib/pagebrowser.py +++ b/comictaggerlib/pagebrowser.py @@ -33,7 +33,8 @@ class PageBrowserWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget, metadata: GenericMetadata) -> None: super().__init__(parent) - uic.loadUi(ui_path / "pagebrowser.ui", self) + with (ui_path / "pagebrowser.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode, None, None) gridlayout = QtWidgets.QGridLayout(self.pageContainer) diff --git a/comictaggerlib/pagelisteditor.py b/comictaggerlib/pagelisteditor.py index 0c83342..b4afdf9 100644 --- a/comictaggerlib/pagelisteditor.py +++ b/comictaggerlib/pagelisteditor.py @@ -68,7 +68,8 @@ class PageListEditor(QtWidgets.QWidget): def __init__(self, parent: QtWidgets.QWidget) -> None: super().__init__(parent) - uic.loadUi(ui_path / "pagelisteditor.ui", self) + with (ui_path / "pagelisteditor.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode, None, None) gridlayout = QtWidgets.QGridLayout(self.pageContainer) diff --git a/comictaggerlib/progresswindow.py b/comictaggerlib/progresswindow.py index 3544157..020e1de 100644 --- a/comictaggerlib/progresswindow.py +++ b/comictaggerlib/progresswindow.py @@ -29,7 +29,8 @@ class IDProgressWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget) -> None: super().__init__(parent) - uic.loadUi(ui_path / "progresswindow.ui", self) + with (ui_path / "progresswindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.setWindowFlags( QtCore.Qt.WindowType( diff --git a/comictaggerlib/renamewindow.py b/comictaggerlib/renamewindow.py index 28ef3df..16c5cdc 100644 --- a/comictaggerlib/renamewindow.py +++ b/comictaggerlib/renamewindow.py @@ -44,7 +44,8 @@ class RenameWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "renamewindow.ui", self) + with (ui_path / "renamewindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.label.setText(f"Preview (based on {MetaDataStyle.name[data_style]} tags):") self.setWindowFlags( diff --git a/comictaggerlib/seriesselectionwindow.py b/comictaggerlib/seriesselectionwindow.py index df9908f..3ce82f0 100644 --- a/comictaggerlib/seriesselectionwindow.py +++ b/comictaggerlib/seriesselectionwindow.py @@ -113,7 +113,8 @@ class SeriesSelectionWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "seriesselectionwindow.ui", self) + with (ui_path / "seriesselectionwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.imageWidget = CoverImageWidget( self.imageContainer, CoverImageWidget.URLMode, config.Runtime_Options__config.user_cache_dir, talker diff --git a/comictaggerlib/settingswindow.py b/comictaggerlib/settingswindow.py index a2dd42e..b0b8887 100644 --- a/comictaggerlib/settingswindow.py +++ b/comictaggerlib/settingswindow.py @@ -138,7 +138,8 @@ class SettingsWindow(QtWidgets.QDialog): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "settingswindow.ui", self) + with (ui_path / "settingswindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.setWindowFlags( QtCore.Qt.WindowType(self.windowFlags() & ~QtCore.Qt.WindowType.WindowContextHelpButtonHint) @@ -596,4 +597,5 @@ class TemplateHelpWindow(QtWidgets.QDialog): def __init__(self, parent: QtWidgets.QWidget) -> None: super().__init__(parent) - uic.loadUi(ui_path / "TemplateHelp.ui", self) + with (ui_path / "TemplateHelp.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index 5dbcd25..8f350ab 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -86,7 +86,8 @@ class TaggerWindow(QtWidgets.QMainWindow): ) -> None: super().__init__(parent) - uic.loadUi(ui_path / "taggerwindow.ui", self) + with (ui_path / "taggerwindow.ui").open(encoding="utf-8") as uifile: + uic.loadUi(uifile, self) self.config = config self.talkers = talkers self.log_window = self.setup_logger() diff --git a/comictaggerlib/ui/__init__.py b/comictaggerlib/ui/__init__.py index 61d1b32..f2c51f9 100644 --- a/comictaggerlib/ui/__init__.py +++ b/comictaggerlib/ui/__init__.py @@ -1,5 +1,5 @@ from __future__ import annotations -import pathlib +import importlib.resources -ui_path = pathlib.Path(__file__).parent +ui_path = importlib.resources.files(__package__) diff --git a/testing/filenames.py b/testing/filenames.py index fa9138c..ea79cdb 100644 --- a/testing/filenames.py +++ b/testing/filenames.py @@ -12,6 +12,7 @@ format is from __future__ import annotations import datetime +import importlib.resources import os import os.path import pathlib @@ -19,8 +20,8 @@ from contextlib import nullcontext as does_not_raise import pytest -datadir = pathlib.Path(__file__).parent / "data" -cbz_path = datadir / "Cory Doctorow's Futuristic Tales of the Here and Now #001 - Anda's Game (2007).cbz" +datadir = importlib.resources.files(__package__).joinpath("data") +cbz_path = datadir.joinpath("Cory Doctorow's Futuristic Tales of the Here and Now #001 - Anda's Game (2007).cbz") names: list[tuple[str, str, dict[str, str | bool], tuple[bool, bool]]] = [ ( diff --git a/tests/comicarchive_test.py b/tests/comicarchive_test.py index ecedc04..eebd5b2 100644 --- a/tests/comicarchive_test.py +++ b/tests/comicarchive_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +import pathlib import platform import shutil @@ -14,7 +15,7 @@ from testing.filenames import datadir @pytest.mark.xfail(not comicapi.archivers.rar.rar_support, reason="rar support") def test_getPageNameList(): - c = comicapi.comicarchive.ComicArchive(datadir / "fake_cbr.cbr") + c = comicapi.comicarchive.ComicArchive(pathlib.Path(str(datadir)) / "fake_cbr.cbr") assert c.seems_to_be_a_comic_archive() pageNameList = c.get_page_name_list() @@ -61,7 +62,7 @@ def test_save_cbi(tmp_comic): @pytest.mark.xfail(not (comicapi.archivers.rar.rar_support and shutil.which("rar")), reason="rar support") def test_save_cix_rar(tmp_path, md_saved): - cbr_path = datadir / "fake_cbr.cbr" + cbr_path = pathlib.Path(str(datadir)) / "fake_cbr.cbr" shutil.copy(cbr_path, tmp_path) tmp_comic = comicapi.comicarchive.ComicArchive(tmp_path / cbr_path.name) @@ -74,7 +75,7 @@ def test_save_cix_rar(tmp_path, md_saved): @pytest.mark.xfail(not (comicapi.archivers.rar.rar_support and shutil.which("rar")), reason="rar support") def test_save_cbi_rar(tmp_path, md_saved): - cbr_path = datadir / "fake_cbr.cbr" + cbr_path = pathlib.Path(str(datadir)) / "fake_cbr.cbr" shutil.copy(cbr_path, tmp_path) tmp_comic = comicapi.comicarchive.ComicArchive(tmp_path / cbr_path.name)