simple logging activeated by verbose
This commit is contained in:
parent
71b84e9540
commit
0d2fd4b9d2
@ -22,6 +22,8 @@ def main():
|
|||||||
name = args.path.name
|
name = args.path.name
|
||||||
cfnparser = ComicFilenameParser(name, verbose=args.verbose)
|
cfnparser = ComicFilenameParser(name, verbose=args.verbose)
|
||||||
metadata = cfnparser.parse()
|
metadata = cfnparser.parse()
|
||||||
|
if args.verbose:
|
||||||
|
print("=" * 80)
|
||||||
pprint(metadata) # noqa:T203
|
pprint(metadata) # noqa:T203
|
||||||
|
|
||||||
|
|
||||||
|
9
comicfn2dict/log.py
Normal file
9
comicfn2dict/log.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
"""Print log header."""
|
||||||
|
|
||||||
|
|
||||||
|
def print_log_header(label: str) -> None:
|
||||||
|
"""Print log header."""
|
||||||
|
prefix = "-" * 3 + label
|
||||||
|
suffix_len = 80 - len(prefix)
|
||||||
|
suffix = "-" * suffix_len
|
||||||
|
print(prefix + suffix)
|
@ -1,11 +1,11 @@
|
|||||||
"""Parse comic book archive names using the simple 'parse' parser."""
|
"""Parse comic book archive names using the simple 'parse' parser."""
|
||||||
from pprint import pprint
|
from pprint import pformat
|
||||||
from calendar import month_abbr
|
from calendar import month_abbr
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from re import Pattern
|
from re import Pattern
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from comicfn2dict.log import print_log_header
|
||||||
from comicfn2dict.regex import (
|
from comicfn2dict.regex import (
|
||||||
ALPHA_MONTH_RANGE_RE,
|
ALPHA_MONTH_RANGE_RE,
|
||||||
BOOK_VOLUME_RE,
|
BOOK_VOLUME_RE,
|
||||||
@ -215,24 +215,24 @@ class ComicFilenameParser:
|
|||||||
if remainders:
|
if remainders:
|
||||||
self.metadata["remainders"] = tuple(remainders)
|
self.metadata["remainders"] = tuple(remainders)
|
||||||
|
|
||||||
def _log_progress(self, label):
|
def _log(self, label):
|
||||||
if not self._debug:
|
if not self._debug:
|
||||||
return
|
return
|
||||||
print(label + ":")
|
print_log_header(label)
|
||||||
combined = {}
|
combined = {}
|
||||||
for key in self.metadata:
|
for key in self.metadata:
|
||||||
combined[key] = (self.metadata.get(key), self.path_index(key))
|
combined[key] = (self.metadata.get(key), self.path_index(key))
|
||||||
pprint(combined)
|
print(" " + self._unparsed_path)
|
||||||
print(self._unparsed_path)
|
print(" " + pformat(combined))
|
||||||
|
|
||||||
def parse(self) -> dict[str, Any]:
|
def parse(self) -> dict[str, Any]:
|
||||||
"""Parse the filename with a hierarchy of regexes."""
|
"""Parse the filename with a hierarchy of regexes."""
|
||||||
# Init
|
# Init
|
||||||
#
|
#
|
||||||
self._log_progress("INITIAL")
|
self._log("Init")
|
||||||
self._parse_ext()
|
self._parse_ext()
|
||||||
self._clean_dividers()
|
self._clean_dividers()
|
||||||
self._log_progress("CLEANED")
|
self._log("After Clean Path")
|
||||||
|
|
||||||
# Issue
|
# Issue
|
||||||
#
|
#
|
||||||
@ -240,15 +240,19 @@ class ComicFilenameParser:
|
|||||||
if "issue" not in self.metadata:
|
if "issue" not in self.metadata:
|
||||||
self._parse_items(ISSUE_WITH_COUNT_RE)
|
self._parse_items(ISSUE_WITH_COUNT_RE)
|
||||||
# self._parse_items(ISSUE_COUNT_RE)
|
# self._parse_items(ISSUE_COUNT_RE)
|
||||||
self._log_progress("AFTER ISSUE")
|
self._log("After Issue")
|
||||||
|
|
||||||
# Volume and Date
|
# Volume
|
||||||
#
|
#
|
||||||
self._parse_items(VOLUME_RE)
|
self._parse_items(VOLUME_RE)
|
||||||
if "volume" not in self.metadata:
|
if "volume" not in self.metadata:
|
||||||
self._parse_items(VOLUME_WITH_COUNT_RE)
|
self._parse_items(VOLUME_WITH_COUNT_RE)
|
||||||
|
self._log("After Volume")
|
||||||
|
|
||||||
|
# Date
|
||||||
|
#
|
||||||
self._parse_dates()
|
self._parse_dates()
|
||||||
self._log_progress("AFTER VOLUME & DATE")
|
self._log("After Date")
|
||||||
|
|
||||||
# Format & Scan Info
|
# Format & Scan Info
|
||||||
#
|
#
|
||||||
@ -260,26 +264,26 @@ class ComicFilenameParser:
|
|||||||
self._parse_items(
|
self._parse_items(
|
||||||
ORIGINAL_FORMAT_SCAN_INFO_SEPARATE_RE,
|
ORIGINAL_FORMAT_SCAN_INFO_SEPARATE_RE,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._parse_items(SCAN_INFO_SECONDARY_RE)
|
self._parse_items(SCAN_INFO_SECONDARY_RE)
|
||||||
if (
|
if (
|
||||||
scan_info_secondary := self.metadata.pop("secondary_scan_info", "")
|
scan_info_secondary := self.metadata.pop("secondary_scan_info", "")
|
||||||
) and "scan_info" not in self.metadata:
|
) and "scan_info" not in self.metadata:
|
||||||
self.metadata["scan_info"] = scan_info_secondary # type: ignore
|
self.metadata["scan_info"] = scan_info_secondary # type: ignore
|
||||||
|
self._log("After original_format & scan_info")
|
||||||
self._log_progress("AFTER PAREN TOKENS")
|
|
||||||
|
|
||||||
# Series and Title
|
# Series and Title
|
||||||
#
|
#
|
||||||
# Volume left on the end of string tokens
|
# Volume left on the end of string tokens
|
||||||
if "volume" not in self.metadata:
|
if "volume" not in self.metadata:
|
||||||
self._parse_items(BOOK_VOLUME_RE)
|
self._parse_items(BOOK_VOLUME_RE)
|
||||||
|
self._log("After original_format & scan_info")
|
||||||
|
|
||||||
# Years left on the end of string tokens
|
# Years left on the end of string tokens
|
||||||
year_end_matched = False
|
year_end_matched = False
|
||||||
if "year" not in self.metadata:
|
if "year" not in self.metadata:
|
||||||
self._parse_items(YEAR_END_RE, pop=False)
|
self._parse_items(YEAR_END_RE, pop=False)
|
||||||
year_end_matched = "year" in self.metadata
|
year_end_matched = "year" in self.metadata
|
||||||
|
self._log("After Year on end of token")
|
||||||
|
|
||||||
# Issue left on the end of string tokens
|
# Issue left on the end of string tokens
|
||||||
if "issue" not in self.metadata and not year_end_matched:
|
if "issue" not in self.metadata and not year_end_matched:
|
||||||
@ -287,7 +291,7 @@ class ComicFilenameParser:
|
|||||||
self._parse_items(ISSUE_END_RE, exclude=exclude)
|
self._parse_items(ISSUE_END_RE, exclude=exclude)
|
||||||
if "issue" not in self.metadata:
|
if "issue" not in self.metadata:
|
||||||
self._parse_items(ISSUE_BEGIN_RE)
|
self._parse_items(ISSUE_BEGIN_RE)
|
||||||
self._log_progress("AFTER ISSUE PICKUP")
|
self._log("After Issue on ends of tokens")
|
||||||
|
|
||||||
# Publisher
|
# Publisher
|
||||||
#
|
#
|
||||||
@ -299,20 +303,22 @@ class ComicFilenameParser:
|
|||||||
self._parse_items(PUBLISHER_UNAMBIGUOUS_RE, pop=False, first_only=True)
|
self._parse_items(PUBLISHER_UNAMBIGUOUS_RE, pop=False, first_only=True)
|
||||||
if "publisher" not in self.metadata:
|
if "publisher" not in self.metadata:
|
||||||
self._parse_items(PUBLISHER_AMBIGUOUS_RE, pop=False, first_only=True)
|
self._parse_items(PUBLISHER_AMBIGUOUS_RE, pop=False, first_only=True)
|
||||||
|
self._log("After publisher")
|
||||||
|
|
||||||
self._assign_remaining_groups()
|
self._assign_remaining_groups()
|
||||||
self._log_progress("AFTER SERIES AND TITLE")
|
self._log("After Series & Title")
|
||||||
|
|
||||||
# Final try for issue number.
|
# Final try for issue number.
|
||||||
# TODO unused
|
# TODO unused
|
||||||
if "issue" not in self.metadata:
|
if "issue" not in self.metadata:
|
||||||
self._parse_items(ISSUE_ANYWHERE_RE)
|
self._parse_items(ISSUE_ANYWHERE_RE)
|
||||||
self._log_progress("AFTER ISSUE PICKUP")
|
self._log("AFTER ISSUE PICKUP")
|
||||||
|
|
||||||
# Copy volume into issue if it's all we have.
|
# Copy volume into issue if it's all we have.
|
||||||
#
|
#
|
||||||
if "issue" not in self.metadata and "volume" in self.metadata:
|
if "issue" not in self.metadata and "volume" in self.metadata:
|
||||||
self.metadata["issue"] = self.metadata["volume"]
|
self.metadata["issue"] = self.metadata["volume"]
|
||||||
|
self._log("After issue can be volume")
|
||||||
|
|
||||||
self._add_remainders()
|
self._add_remainders()
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from collections.abc import Callable, Mapping, Sequence
|
|||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from calendar import month_abbr
|
from calendar import month_abbr
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
|
from comicfn2dict.log import print_log_header
|
||||||
|
|
||||||
|
|
||||||
def issue_formatter(issue: str) -> str:
|
def issue_formatter(issue: str) -> str:
|
||||||
@ -36,7 +37,17 @@ _DATE_KEYS = ("year", "month", "day")
|
|||||||
|
|
||||||
|
|
||||||
class ComicFilenameSerializer:
|
class ComicFilenameSerializer:
|
||||||
|
"""Serialize Comic Filenames from dict."""
|
||||||
|
|
||||||
|
def _log(self, label, fn):
|
||||||
|
"""Log progress."""
|
||||||
|
if not self._debug:
|
||||||
|
return
|
||||||
|
print_log_header(label)
|
||||||
|
print(fn)
|
||||||
|
|
||||||
def _add_date(self) -> None:
|
def _add_date(self) -> None:
|
||||||
|
"""Construct date from Y-m-D if they exist."""
|
||||||
if "date" in self.metadata:
|
if "date" in self.metadata:
|
||||||
return
|
return
|
||||||
parts = []
|
parts = []
|
||||||
@ -52,9 +63,11 @@ class ComicFilenameSerializer:
|
|||||||
break
|
break
|
||||||
if parts:
|
if parts:
|
||||||
date = "-".join(parts)
|
date = "-".join(parts)
|
||||||
|
self._log("After date", date)
|
||||||
self.metadata = MappingProxyType({**self.metadata, "date": date})
|
self.metadata = MappingProxyType({**self.metadata, "date": date})
|
||||||
|
|
||||||
def _tokenize_tag(self, tag: str, fmt: str | Callable) -> str:
|
def _tokenize_tag(self, tag: str, fmt: str | Callable) -> str:
|
||||||
|
"""Add tags to the string."""
|
||||||
val = self.metadata.get(tag)
|
val = self.metadata.get(tag)
|
||||||
if val in _EMPTY_VALUES:
|
if val in _EMPTY_VALUES:
|
||||||
return ""
|
return ""
|
||||||
@ -63,6 +76,7 @@ class ComicFilenameSerializer:
|
|||||||
return token
|
return token
|
||||||
|
|
||||||
def _add_remainder(self) -> str:
|
def _add_remainder(self) -> str:
|
||||||
|
"""Add the remainders specially."""
|
||||||
if remainders := self.metadata.get("remainders"):
|
if remainders := self.metadata.get("remainders"):
|
||||||
if isinstance(remainders, Sequence):
|
if isinstance(remainders, Sequence):
|
||||||
remainder = " ".join(remainders)
|
remainder = " ".join(remainders)
|
||||||
@ -79,19 +93,23 @@ class ComicFilenameSerializer:
|
|||||||
for tag, fmt in _FILENAME_FORMAT_TAGS:
|
for tag, fmt in _FILENAME_FORMAT_TAGS:
|
||||||
if token := self._tokenize_tag(tag, fmt):
|
if token := self._tokenize_tag(tag, fmt):
|
||||||
tokens.append(token)
|
tokens.append(token)
|
||||||
|
self._log(f"After {tag}", tokens)
|
||||||
fn = " ".join(tokens)
|
fn = " ".join(tokens)
|
||||||
|
|
||||||
fn += self._add_remainder()
|
fn += self._add_remainder()
|
||||||
|
self._log("After remainder", fn)
|
||||||
|
|
||||||
if self._ext:
|
if self._ext:
|
||||||
ext = self.metadata.get("ext", _DEFAULT_EXT)
|
ext = self.metadata.get("ext", _DEFAULT_EXT)
|
||||||
fn += f".{ext}"
|
fn += f".{ext}"
|
||||||
|
self._log("After ext", fn)
|
||||||
|
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
def __init__(self, metadata: Mapping, ext: bool = True):
|
def __init__(self, metadata: Mapping, ext: bool = True, verbose: int = 0):
|
||||||
self.metadata: Mapping = metadata
|
self.metadata: Mapping = metadata
|
||||||
self._ext: bool = ext
|
self._ext: bool = ext
|
||||||
|
self._debug: bool = bool(verbose)
|
||||||
|
|
||||||
|
|
||||||
def dict2comicfn(md: Mapping, ext: bool = True) -> str:
|
def dict2comicfn(md: Mapping, ext: bool = True) -> str:
|
||||||
|
Loading…
Reference in New Issue
Block a user