feat(config): add filename_replacements to replace text in output names

This commit is contained in:
imSp4rky
2026-11-28 15:03:01 -07:00
parent 127e8e0dcc
commit 418d69b1ec
9 changed files with 115 additions and 10 deletions
+13
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import logging
import re
import unicodedata
import warnings
from pathlib import Path
from typing import Any
@@ -62,6 +63,17 @@ def restore_bool_languages(data: Any) -> Any:
return fixed
def check_filename_replacements(value: Any) -> dict[str, str]:
"""The ``filename_replacements`` map with NFC keys; exits with one line when it does not map text to text."""
if value is None:
return {}
if not isinstance(value, dict) or not all(
isinstance(k, str) and k and isinstance(v, str) for k, v in value.items()
):
raise SystemExit(f"ERROR: filename_replacements must map text to text, not {value!r}")
return {unicodedata.normalize("NFC", k): v for k, v in value.items()}
def resolve_decryption(decryption_map: dict, default: str, service: str) -> str:
"""Pick the decryption backend for a service (case-insensitive), falling back to default."""
return ci_get(decryption_map, service, default)
@@ -229,6 +241,7 @@ class Config:
self.validate_output_templates()
self.unicode_filenames: bool = kwargs.get("unicode_filenames", False)
self.filename_replacements = check_filename_replacements(kwargs.get("filename_replacements"))
self.title_cache_time: int = kwargs.get("title_cache_time", 1800) # 30 minutes default
self.title_cache_max_retention: int = kwargs.get("title_cache_max_retention", 86400) # 24 hours default
+1 -6
View File
@@ -256,17 +256,12 @@ class Episode(Title):
derived_template = re.sub(r"\s{2,}", " ", derived_template)
derived_template = re.sub(r"^[\.\s]+|[\.\s]+$", "", derived_template)
formatter = TemplateFormatter(derived_template)
context = self.build_template_context(media_info, show_service, include_part=False)
context["season"] = self.folder_season()
# folders keep the year, unless the season label is already the air year
context["year"] = "" if str(self.year) == context["season"] else self.year or ""
folder_name = formatter.format(context)
separators = re.sub(r"\{[^}]*\}", "", derived_template)
spacer = "." if "." in separators and " " not in separators else " "
return sanitize_filename(folder_name, spacer)
return TemplateFormatter(derived_template).format(context)
else:
name = f"{self.title}"
if self.year:
+5
View File
@@ -128,8 +128,13 @@ def sanitize_filename(filename: str, spacer: str = ".", unicode: bool | None = N
transliterating them to ASCII equivalents. Pass ``unicode`` to decide that
for one call instead of the config value. The name goes to NFC first, so
composed and decomposed spellings of a title give the same name.
`filename_replacements` in config replaces text first. A word that replaces text with no letters
or digits gets a space on each side, so it stays a separate word.
"""
filename = unicodedata.normalize("NFC", filename)
for old, new in config.filename_replacements.items():
word = any(c.isalnum() for c in new) and not any(c.isalnum() for c in old)
filename = filename.replace(old, f" {new} " if word else new)
if not (config.unicode_filenames if unicode is None else unicode):
filename = unidecode(filename)
filename = re.sub(r"\[\(+", "[", filename)