Use importlib.resources instead of __file__

This commit is contained in:
Timmy Welch 2023-11-25 12:32:50 -08:00
parent 99325f40cf
commit 1ff6f1768b
24 changed files with 53 additions and 31 deletions

View File

@ -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__)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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)

View File

@ -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

View File

@ -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(

View File

@ -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

View File

@ -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__)

View File

@ -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))

View File

@ -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,

View File

@ -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(

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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(

View File

@ -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(

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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__)

View File

@ -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]]] = [
(

View File

@ -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)