From 548ad4a81633af700e3150f73c5bdad0cd0e2df7 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sat, 29 Jul 2023 00:07:25 -0700 Subject: [PATCH] Fix folder archiver Implement supports_comment and is_writable Fix function call in ComicArchive for supports_comment Add a menu option to open a folder as an archive --- comicapi/archivers/folder.py | 11 ++++++++--- comicapi/comicarchive.py | 2 +- comicapi/utils.py | 2 +- comictaggerlib/taggerwindow.py | 31 ++++++++++++++++++++++--------- comictaggerlib/ui/taggerwindow.ui | 8 +++++++- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/comicapi/archivers/folder.py b/comicapi/archivers/folder.py index ec94a3a..c7341b0 100644 --- a/comicapi/archivers/folder.py +++ b/comicapi/archivers/folder.py @@ -19,7 +19,7 @@ class FolderArchiver(Archiver): def get_comment(self) -> str: try: - return self.read_file(self.comment_file_name).decode("utf-8") + return (self.path / self.comment_file_name).read_text() except OSError: return "" @@ -28,10 +28,12 @@ class FolderArchiver(Archiver): return self.write_file(self.comment_file_name, comment.encode("utf-8")) return True + def supports_comment(self) -> bool: + return True + def read_file(self, archive_file: str) -> bytes: try: - with open(self.path / archive_file, mode="rb") as f: - data = f.read() + data = (self.path / archive_file).read_bytes() except OSError as e: logger.error("Error reading folder archive [%s]: %s :: %s", e, self.path, archive_file) raise @@ -89,6 +91,9 @@ class FolderArchiver(Archiver): else: return True + def is_writable(self) -> bool: + return True + def name(self) -> str: return "Folder" diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 1433de7..87a234d 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -134,7 +134,7 @@ class ComicArchive: return True def is_writable_for_style(self, data_style: int) -> bool: - return not (data_style == MetaDataStyle.CBI and not self.archiver.supports_comment) + return not (data_style == MetaDataStyle.CBI and not self.archiver.supports_comment()) def is_zip(self) -> bool: return self.archiver.name() == "ZIP" diff --git a/comicapi/utils.py b/comicapi/utils.py index 8e041bd..5b9c00c 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -91,7 +91,7 @@ def get_recursive_filelist(pathlist: list[str]) -> list[str]: for root, _, files in os.walk(p): for f in files: filelist.append(os.path.join(root, f)) - else: + elif os.path.exists(p): filelist.append(p) return filelist diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index 1e59531..256875d 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -343,6 +343,10 @@ class TaggerWindow(QtWidgets.QMainWindow): self.actionLoadFolder.setStatusTip("Load folder with comic archives") self.actionLoadFolder.triggered.connect(self.select_folder) + self.actionOpenFolderAsComic.setShortcut("Ctrl+Shift+Alt+O") + self.actionOpenFolderAsComic.setStatusTip("Load folder as a comic archives") + self.actionOpenFolderAsComic.triggered.connect(self.select_folder_archive) + self.actionWrite_Tags.setShortcut("Ctrl+S") self.actionWrite_Tags.setStatusTip("Save tags to comic archive") self.actionWrite_Tags.triggered.connect(self.commit_metadata) @@ -438,6 +442,7 @@ class TaggerWindow(QtWidgets.QMainWindow): # ToolBar self.actionLoad.setIcon(QtGui.QIcon(str(graphics_path / "open.png"))) self.actionLoadFolder.setIcon(QtGui.QIcon(str(graphics_path / "longbox.png"))) + self.actionOpenFolderAsComic.setIcon(QtGui.QIcon(str(graphics_path / "open.png"))) self.actionWrite_Tags.setIcon(QtGui.QIcon(str(graphics_path / "save.png"))) self.actionParse_Filename.setIcon(QtGui.QIcon(str(graphics_path / "parse.png"))) self.actionParse_Filename_split_words.setIcon(QtGui.QIcon(str(graphics_path / "parse.png"))) @@ -987,24 +992,32 @@ class TaggerWindow(QtWidgets.QMainWindow): def select_folder(self) -> None: self.select_file(folder_mode=True) + def select_folder_archive(self) -> None: + dialog = self.file_dialog(folder_mode=True) + if dialog.exec(): + file_list = dialog.selectedFiles() + if file_list: + self.fileSelectionList.add_path_item(file_list[0]) + def select_file(self, folder_mode: bool = False) -> None: + dialog = self.file_dialog(folder_mode=folder_mode) + if dialog.exec(): + file_list = dialog.selectedFiles() + self.fileSelectionList.add_path_list(file_list) + + def file_dialog(self, folder_mode: bool = False) -> QtWidgets.QFileDialog: dialog = QtWidgets.QFileDialog(self) if folder_mode: dialog.setFileMode(QtWidgets.QFileDialog.FileMode.Directory) else: + archive_filter = "Comic archive files (*.cbz *.zip *.cbr *.rar *.cb7 *.7z)" + filters = [archive_filter, "Any files (*)"] + dialog.setNameFilters(filters) dialog.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFiles) if self.config[0].internal_last_opened_folder is not None: dialog.setDirectory(self.config[0].internal_last_opened_folder) - - if not folder_mode: - archive_filter = "Comic archive files (*.cbz *.zip *.cbr *.rar *.cb7 *.7z)" - filters = [archive_filter, "Any files (*)"] - dialog.setNameFilters(filters) - - if dialog.exec(): - file_list = dialog.selectedFiles() - self.fileSelectionList.add_path_list(file_list) + return dialog def auto_identify_search(self) -> None: if self.comic_archive is None: diff --git a/comictaggerlib/ui/taggerwindow.ui b/comictaggerlib/ui/taggerwindow.ui index 8312eee..fc1dcf8 100644 --- a/comictaggerlib/ui/taggerwindow.ui +++ b/comictaggerlib/ui/taggerwindow.ui @@ -1156,7 +1156,7 @@ 0 0 1096 - 30 + 21 @@ -1180,6 +1180,7 @@ + @@ -1444,6 +1445,11 @@ perform a literal search on the series and return the first 50 results + + + Open Folder as Comic + +