From 00e649bb4c55c7da38bfb1033293a0738c46c6b3 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 17 Aug 2022 16:16:38 -0700 Subject: [PATCH] Move colon handling when renaming to the MetadataFormatter class Fixes #356 --- comictaggerlib/filerenamer.py | 13 +++++-------- testing/filenames.py | 7 +++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/comictaggerlib/filerenamer.py b/comictaggerlib/filerenamer.py index a296058..f2160bc 100644 --- a/comictaggerlib/filerenamer.py +++ b/comictaggerlib/filerenamer.py @@ -45,7 +45,7 @@ class MetadataFormatter(string.Formatter): def __init__(self, smart_cleanup: bool = False, platform: str = "auto") -> None: super().__init__() self.smart_cleanup = smart_cleanup - self.platform = platform + self.platform = platform.casefold() def format_field(self, value: Any, format_spec: str) -> str: if value is None or value == "": @@ -72,6 +72,10 @@ 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"]: + # colons get special treatment + literal_text = literal_text.replace(": ", " - ") + literal_text = literal_text.replace(":", "-") lspace = literal_text[0].isspace() if literal_text else False rspace = literal_text[-1].isspace() if literal_text else False literal_text = " ".join(literal_text.split()) @@ -179,13 +183,6 @@ class FileRenamer: new_basename = "" for component in pathlib.PureWindowsPath(template).parts: - if ( - self.platform.casefold() in ["universal", "windows"] or sys.platform.casefold() in ["windows"] - ) and self.smart_cleanup: - # colons get special treatment - component = component.replace(": ", " - ") - component = component.replace(":", "-") - new_basename = str( sanitize_filename(fmt.vformat(component, args=[], kwargs=Default(md_dict)), platform=self.platform) ).strip() diff --git a/testing/filenames.py b/testing/filenames.py index 25e56ab..6af0923 100644 --- a/testing/filenames.py +++ b/testing/filenames.py @@ -757,6 +757,13 @@ rnames = [ "Cory Doctorow's Futuristic Tales of the Here and Now #001 - Anda's Game (2007).cbz", does_not_raise(), ), + ( + "{series} #{issue} - {title} {volume:02} ({year})", # Ensure format specifier works + False, + "universal", + "Cory Doctorow's Futuristic Tales of the Here and Now #001 - Anda's Game 01 (2007).cbz", + does_not_raise(), + ), ( "{series} #{issue} - {title} ({year})({price})", # price should be none, test no space between ')(' False,