From ceb3b30e5c328462db57f0466e2ff00cce78b98c Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 20 Dec 2023 21:24:12 -0800 Subject: [PATCH] Always apply the default page list when writing metadata --- comicapi/comicarchive.py | 16 ++-------------- comicapi/genericmetadata.py | 1 + comicapi/utils.py | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 1f7e517..ed33e5d 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -21,7 +21,6 @@ import pathlib import shutil import sys import traceback -from typing import cast from comicapi import utils from comicapi.archivers import Archiver, UnknownArchiver, ZipArchiver @@ -179,6 +178,7 @@ class ComicArchive: def write_metadata(self, metadata: GenericMetadata, style: str) -> bool: if style in self.md: del self.md[style] + metadata.apply_default_page_list(self.get_page_name_list()) return metadata_styles[style].set_metadata(metadata, self.archiver) def has_metadata(self, style: str) -> bool: @@ -270,19 +270,7 @@ class ComicArchive: def get_page_name_list(self) -> list[str]: if not self.page_list: - # get the list file names in the archive, and sort - files: list[str] = self.archiver.get_filename_list() - - files = cast(list[str], utils.os_sorted(files)) - - # make a sub-list of image files - self.page_list = [] - for name in files: - if ( - os.path.splitext(name)[1].casefold() in [".jpg", ".jpeg", ".png", ".gif", ".webp"] - and os.path.basename(name)[0] != "." - ): - self.page_list.append(name) + self.page_list = utils.get_page_name_list(self.archiver.get_filename_list()) return self.page_list diff --git a/comicapi/genericmetadata.py b/comicapi/genericmetadata.py index 22f89c7..765ac28 100644 --- a/comicapi/genericmetadata.py +++ b/comicapi/genericmetadata.py @@ -289,6 +289,7 @@ class GenericMetadata: def apply_default_page_list(self, page_list: Sequence[str]) -> None: # generate a default page list, with the first page marked as the cover + # Create a dictionary of all pages in the metadata pages = {p["image_index"]: p for p in self.pages} cover_set = False diff --git a/comicapi/utils.py b/comicapi/utils.py index 56e8c85..db511e1 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -23,7 +23,7 @@ import unicodedata from collections import defaultdict from collections.abc import Iterable, Mapping from shutil import which # noqa: F401 -from typing import Any, TypeVar +from typing import Any, TypeVar, cast import comicapi.data from comicapi import filenamelexer, filenameparser @@ -195,6 +195,21 @@ def path_to_short_str(original_path: pathlib.Path, renamed_path: pathlib.Path | return path_str +def get_page_name_list(files: list[str]) -> list[str]: + # get the list file names in the archive, and sort + files = cast(list[str], os_sorted(files)) + + # make a sub-list of image files + page_list = [] + for name in files: + if ( + os.path.splitext(name)[1].casefold() in [".jpg", ".jpeg", ".png", ".gif", ".webp"] + and os.path.basename(name)[0] != "." + ): + page_list.append(name) + return page_list + + def get_recursive_filelist(pathlist: list[str]) -> list[str]: """Get a recursive list of of all files under all path items in the list"""