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
+6
View File
@@ -70,6 +70,12 @@ def default_codec_names(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(config, "codec_names", {})
@pytest.fixture(autouse=True)
def no_filename_replacements(monkeypatch: pytest.MonkeyPatch) -> None:
"""Keep names as sanitize_filename makes them, whatever filename_replacements the local unshackle.yaml sets."""
monkeypatch.setattr(config, "filename_replacements", {})
@pytest.fixture(autouse=True)
def no_speed_limit(monkeypatch: pytest.MonkeyPatch) -> None:
"""Start with no speed limit, so a limit that an earlier test locked cannot block a later one."""
+51 -1
View File
@@ -11,7 +11,7 @@ from __future__ import annotations
import pytest
from unshackle.core.config import config
from unshackle.core.config import Config, config
from unshackle.core.utilities import sanitize_filename
THAI = "\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e48\u0e2d" # a Thai title with vowel and tone marks
@@ -73,3 +73,53 @@ def test_sanitising_a_name_again_does_not_change_it() -> None:
once = sanitize_filename("e?\u0301", unicode=True)
assert once == "\u00e9"
assert sanitize_filename(once, unicode=True) == once
@pytest.mark.parametrize(
("mapping", "name", "spacer", "expected"),
[
({}, "Tom & Jerry", ".", "Tom.&.Jerry"),
({"&": "and"}, "Tom & Jerry", ".", "Tom.and.Jerry"),
({"&": "and"}, "Tom&Jerry", ".", "Tom.and.Jerry"),
({"&": "and"}, "Tom & Jerry", " ", "Tom and Jerry"),
({"&": "and"}, "Part 1/Part 2 & More", " ", "Part 1 & Part 2 and More"),
({"&": "and"}, "Part 1;Part 2", ".", "Part.1.&.Part.2"),
({"&": ""}, "Tom & Jerry", ".", "Tom.Jerry"),
({"\u00e4": "ae"}, "M\u00e4dchen", ".", "Maedchen"),
({"&": "and", "and": "plus"}, "Tom & Jerry", ".", "Tom.plus.Jerry"),
({"&": "+"}, "Tom&Jerry", ".", "Tom+Jerry"),
],
)
def test_filename_replacements_change_only_the_name_text(monkeypatch, mapping, name, spacer, expected) -> None:
monkeypatch.setattr(config, "filename_replacements", mapping)
assert sanitize_filename(name, spacer, unicode=True) == expected
def test_episode_folder_from_the_series_template_keeps_the_multi_episode_separator(monkeypatch) -> None:
from unshackle.core.titles.episode import Episode
class StubMediaInfo:
video_tracks: list = []
audio_tracks: list = []
monkeypatch.setattr(config, "filename_replacements", {"&": "and"})
monkeypatch.setattr(config, "folder_templates", {})
monkeypatch.setattr(config, "folder_template", "")
monkeypatch.setattr(config, "output_template", {"series": "{title}.{season_episode}"})
episode = Episode(id_="episode-0001", service=object, title="A/B & C", season=1, number=2, name="Pilot")
assert episode.get_filename(StubMediaInfo(), folder=True) == "A.&.B.and.C.S01"
assert episode.get_filename(StubMediaInfo()) == "A.&.B.and.C.S01E02"
@pytest.mark.parametrize("value", [["&"], [], "", False, {"&": None}, {"": "and"}, {1: "one"}, {"&": True}])
def test_a_bad_filename_replacements_value_is_an_error(value) -> None:
with pytest.raises(SystemExit, match="filename_replacements"):
Config(filename_replacements=value)
def test_filename_replacements_default_to_empty_and_use_nfc_keys(monkeypatch) -> None:
assert Config().filename_replacements == {}
monkeypatch.setattr(
config, "filename_replacements", Config(filename_replacements={"e\u0301": "e"}).filename_replacements
)
assert sanitize_filename("Caf\u00e9", unicode=True) == "Cafe"