From 03677ce4b8f319e6acca062f3b664a18df959d5e Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Fri, 19 Aug 2022 20:20:37 -0700 Subject: [PATCH] Fix renaming Make ComicArchive.path always absolute Fix unique_file not preserving the extension Fix incorrect output when renaming in CLI mode Fix handling of platform when renaming --- comicapi/comicarchive.py | 2 +- comicapi/utils.py | 4 ++-- comictaggerlib/cli.py | 4 ++-- comictaggerlib/filerenamer.py | 9 ++++----- tests/utils_test.py | 4 ++-- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index b01eff3..e887098 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -683,7 +683,7 @@ class ComicArchive: self._has_cbi: bool | None = None self._has_cix: bool | None = None self._has_comet: bool | None = None - self.path = pathlib.Path(path) + self.path = pathlib.Path(path).absolute() self.page_count: int | None = None self.page_list: list[str] = [] diff --git a/comicapi/utils.py b/comicapi/utils.py index c458365..f2cccfe 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -167,12 +167,12 @@ def titles_match(search_title: str, record_title: str, threshold: int = 90) -> b def unique_file(file_name: pathlib.Path) -> pathlib.Path: - name = file_name.name + name = file_name.stem counter = 1 while True: if not file_name.exists(): return file_name - file_name = file_name.with_name(name + " (" + str(counter) + ")") + file_name = file_name.with_stem(name + " (" + str(counter) + ")") counter += 1 diff --git a/comictaggerlib/cli.py b/comictaggerlib/cli.py index 2f6058e..ece4288 100644 --- a/comictaggerlib/cli.py +++ b/comictaggerlib/cli.py @@ -472,7 +472,7 @@ def process_file_cli( match_results.good_matches.append(str(ca.path.absolute())) elif opts.rename: - + original_path = ca.path msg_hdr = "" if batch_mode: msg_hdr = f"{ca.path}: " @@ -529,7 +529,7 @@ def process_file_cli( else: suffix = " (dry-run, no change)" - print(f"renamed '{os.path.basename(ca.path)}' -> '{new_name}' {suffix}") + print(f"renamed '{original_path.name}' -> '{new_name}' {suffix}") elif opts.export_to_zip: msg_hdr = "" diff --git a/comictaggerlib/filerenamer.py b/comictaggerlib/filerenamer.py index 4f09f49..6812555 100644 --- a/comictaggerlib/filerenamer.py +++ b/comictaggerlib/filerenamer.py @@ -20,10 +20,9 @@ import logging import os import pathlib import string -import sys from typing import Any, NamedTuple, cast -from pathvalidate import sanitize_filename +from pathvalidate import Platform, normalize_platform, sanitize_filename from comicapi.comicarchive import ComicArchive from comicapi.genericmetadata import GenericMetadata @@ -57,7 +56,7 @@ class MetadataFormatter(string.Formatter): ) -> None: super().__init__() self.smart_cleanup = smart_cleanup - self.platform = platform.casefold() + self.platform = normalize_platform(platform) self.replacements = replacements def format_field(self, value: Any, format_spec: str) -> str: @@ -90,7 +89,7 @@ class MetadataFormatter(string.Formatter): if lstrip: literal_text = literal_text.lstrip("-_)}]#") if self.smart_cleanup: - if self.platform in ["universal", "windows"] or sys.platform.casefold() in ["windows"]: + if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]: literal_text = self.handle_replacements(literal_text, self.replacements.literal_text) lspace = literal_text[0].isspace() if literal_text else False rspace = literal_text[-1].isspace() if literal_text else False @@ -137,7 +136,7 @@ class MetadataFormatter(string.Formatter): result[-1], _, _ = result[-1].rstrip().rpartition(" ") result[-1] = result[-1].rstrip("-_({[#") if self.smart_cleanup: - if self.platform in ["universal", "windows"] or sys.platform.casefold() in ["windows"]: + if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]: # colons and slashes get special treatment fmt_obj = self.handle_replacements(fmt_obj, self.replacements.format_value) fmt_obj = " ".join(fmt_obj.split()) diff --git a/tests/utils_test.py b/tests/utils_test.py index 0bf7713..397e823 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -77,11 +77,11 @@ def test_get_language(value, result): def test_unique_file(tmp_path): - file = tmp_path / "test" + file = tmp_path / "test.cbz" assert file == comicapi.utils.unique_file(file) file.mkdir() - assert (tmp_path / "test (1)") == comicapi.utils.unique_file(file) + assert (tmp_path / "test (1).cbz") == comicapi.utils.unique_file(file) def test_add_to_path(monkeypatch):