Skip to content

Commit

Permalink
Merge branch 'main' into duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
bpepple committed Oct 7, 2023
2 parents 8078451 + 0a71fc0 commit 122b793
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
88 changes: 45 additions & 43 deletions metrontagger/filerenamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
class FileRenamer:
"""Class to rename a comic archive based on it's metadata tag"""

def __init__(self: "FileRenamer", metadata: Metadata) -> None:
self.set_metadata(metadata)
self.set_template("%series% v%volume% #%issue% (of %issuecount%) (%year%)")
self.smart_cleanup = True
self.issue_zero_padding = 3
def __init__(self: "FileRenamer", metadata: Optional[Metadata] = None) -> None:
self.metadata: Optional[Metadata] = metadata
self.template: str = "%series% v%volume% #%issue% (of %issuecount%) (%year%)"
self.smart_cleanup: bool = True
self.issue_zero_padding: int = 3

def set_smart_cleanup(self: "FileRenamer", on: bool) -> None:
self.smart_cleanup = on

def set_metadata(self: "FileRenamer", metadata: Metadata) -> None:
"""Method to set the metadata"""
self.metdata = metadata
self.metadata = metadata

def set_issue_zero_padding(self: "FileRenamer", count: int) -> None:
"""Method to set the padding for the issue's number"""
Expand All @@ -40,16 +40,15 @@ def set_issue_zero_padding(self: "FileRenamer", count: int) -> None:
def set_template(self: "FileRenamer", template: str) -> None:
"""
Method to use a user's custom file naming template.
Currently this hasn't been implemented
"""
self.template = template

def replace_token(self: "FileRenamer", text: str, value: Optional[str], token: str) -> str:
"""Method to replace a value with another value"""

# helper func
def is_token(word: str) -> bool:
return word[0] == "%" and word.endswith("%")
def is_token(txt: str) -> bool:
return txt[0] == "%" and txt.endswith("%")

if value is not None:
return text.replace(token, str(value))
Expand Down Expand Up @@ -101,74 +100,69 @@ def smart_cleanup_string(self: "FileRenamer", new_name: str) -> str:

def determine_name(self: "FileRenamer", filename: Path) -> Optional[str]:
"""Method to create the new filename based on the files metadata"""
meta_data = self.metdata
md = self.metadata
new_name = self.template

new_name = self.replace_token(new_name, meta_data.series.name, "%series%")
new_name = self.replace_token(new_name, meta_data.series.volume, "%volume%")
new_name = self.replace_token(new_name, md.series.name, "%series%")
new_name = self.replace_token(new_name, md.series.volume, "%volume%")

if meta_data.issue is None:
if md.issue is None:
issue_str = None
elif meta_data.issue == "½":
elif md.issue == "½":
issue_str = IssueString("0.5").as_string(pad=self.issue_zero_padding)
else:
issue_str = "{}".format(
IssueString(meta_data.issue).as_string(pad=self.issue_zero_padding),
)
issue_str = IssueString(md.issue).as_string(pad=self.issue_zero_padding)
new_name = self.replace_token(new_name, issue_str, "%issue%")

new_name = self.replace_token(new_name, meta_data.issue_count, "%issuecount%")
new_name = self.replace_token(new_name, meta_data.cover_date.year, "%year%")
new_name = self.replace_token(new_name, meta_data.publisher, "%publisher%")
new_name = self.replace_token(new_name, meta_data.stories, "%title%")
new_name = self.replace_token(new_name, meta_data.cover_date.month, "%month%")
new_name = self.replace_token(new_name, md.issue_count, "%issuecount%")
new_name = self.replace_token(new_name, md.cover_date.year, "%year%")
new_name = self.replace_token(new_name, md.publisher, "%publisher%")
new_name = self.replace_token(new_name, md.stories, "%title%")
new_name = self.replace_token(new_name, md.cover_date.month, "%month%")
month_name = None
if (
meta_data.cover_date.month is not None
md.cover_date.month is not None
and (
(
isinstance(meta_data.cover_date.month, str)
and meta_data.cover_date.month.isdigit()
)
or isinstance(meta_data.cover_date.month, int)
(isinstance(md.cover_date.month, str) and md.cover_date.month.isdigit())
or isinstance(md.cover_date.month, int)
)
and int(meta_data.cover_date.month) in range(1, 13)
and int(md.cover_date.month) in range(1, 13)
):
date_time = datetime.datetime( # noqa: DTZ001
1970,
int(meta_data.cover_date.month),
int(md.cover_date.month),
1,
0,
0,
)
month_name = date_time.strftime("%B")
new_name = self.replace_token(new_name, month_name, "%month_name%")

new_name = self.replace_token(new_name, meta_data.genres, "%genre%")
new_name = self.replace_token(new_name, meta_data.series.language, "%language_code%")
new_name = self.replace_token(new_name, meta_data.critical_rating, "%criticalrating%")
new_name = self.replace_token(new_name, md.genres, "%genre%")
new_name = self.replace_token(new_name, md.series.language, "%language_code%")
new_name = self.replace_token(new_name, md.critical_rating, "%criticalrating%")
new_name = self.replace_token(
new_name,
meta_data.alternate_series,
md.alternate_series,
"%alternateseries%",
)
new_name = self.replace_token(
new_name,
meta_data.alternate_number,
md.alternate_number,
"%alternatenumber%",
)
new_name = self.replace_token(new_name, meta_data.alternate_count, "%alternatecount%")
new_name = self.replace_token(new_name, meta_data.imprint, "%imprint%")
if meta_data.series.format == "Hard Cover":
new_name = self.replace_token(new_name, md.alternate_count, "%alternatecount%")
new_name = self.replace_token(new_name, md.imprint, "%imprint%")
if md.series.format == "Hard Cover":
new_name = self.replace_token(new_name, "HC", "%format%")
elif meta_data.series.format == "Trade Paperback":
elif md.series.format == "Trade Paperback":
new_name = self.replace_token(new_name, "TPB", "%format%")
else:
new_name = self.replace_token(new_name, "", "%format%")
new_name = self.replace_token(new_name, meta_data.age_rating, "%maturityrating%")
new_name = self.replace_token(new_name, meta_data.stories, "%storyarc%")
new_name = self.replace_token(new_name, meta_data.series_group, "%seriesgroup%")
new_name = self.replace_token(new_name, meta_data.scan_info, "%scaninfo%")
new_name = self.replace_token(new_name, md.age_rating, "%maturityrating%")
new_name = self.replace_token(new_name, md.stories, "%storyarc%")
new_name = self.replace_token(new_name, md.series_group, "%seriesgroup%")
new_name = self.replace_token(new_name, md.scan_info, "%scaninfo%")

if self.smart_cleanup:
new_name = self.smart_cleanup_string(new_name)
Expand All @@ -179,6 +173,14 @@ def determine_name(self: "FileRenamer", filename: Path) -> Optional[str]:
return cleanup_string(new_name)

def rename_file(self: "FileRenamer", comic: Path) -> Optional[Path]:
# This shouldn't happen, but just in case let's make sure there is metadata.
if self.metadata is None:
questionary.print(
f"Metadata hasn't been set for {comic}. Skipping...",
style=Styles.WARNING,
)
return None

new_name = self.determine_name(comic)
if not new_name:
return None
Expand Down
3 changes: 2 additions & 1 deletion metrontagger/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def rename_comics(self: "Runner", file_list: list[Path]) -> list[Path]:
# Lists to track filename changes
new_file_names: list[Path] = []
original_files_changed: list[Path] = []
renamer = FileRenamer()
for comic in file_list:
comic_archive = Comic(comic)
if not comic_archive.has_metadata():
Expand All @@ -39,7 +40,7 @@ def rename_comics(self: "Runner", file_list: list[Path]) -> list[Path]:
continue

meta_data = comic_archive.read_metadata()
renamer = FileRenamer(meta_data)
renamer.set_metadata(meta_data)
renamer.set_template(self.config.rename_template)
renamer.set_issue_zero_padding(self.config.rename_issue_number_padding)
renamer.set_smart_cleanup(self.config.rename_use_smart_string_cleanup)
Expand Down

0 comments on commit 122b793

Please sign in to comment.