mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
126 lines
5.2 KiB
Python
126 lines
5.2 KiB
Python
"""Combining marks and Unicode normalisation in sanitize_filename.
|
|
|
|
Thai, Devanagari, Bengali and other scripts write vowels and tone marks as combining marks on
|
|
a base letter. With ``unicode_filenames`` those marks are part of the word and must survive.
|
|
In both modes, composed and decomposed spellings of a title must give the same name.
|
|
Combining characters are written as escapes so an editor that normalises the file cannot
|
|
change what a case tests.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
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
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name",
|
|
[
|
|
THAI,
|
|
"\u0e01\u0e31\u0e49\u0e19", # Thai: a tone mark stacked on an upper vowel
|
|
"\u0939\u093f\u0902\u0926\u0940", # Devanagari
|
|
"\u09b0\u200d\u09cd\u09af\u09be\u09ac", # Bengali: the virama follows a zero width joiner
|
|
"\u1040\u102d\u102f\u1004\u103a\u1038", # Burmese: a mark on digit zero, typed for the letter wa
|
|
"\u0e19\u0e49\u0e33", # Thai sara am, which NFKC would split into two characters
|
|
],
|
|
)
|
|
def test_unicode_filenames_keep_marks(name: str) -> None:
|
|
assert sanitize_filename(name, unicode=True) == name
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("name", "expected"),
|
|
[
|
|
# Decomposed and composed spellings give the same name.
|
|
("Cafe\u0301", "Caf\u00e9"),
|
|
("Caf\u00e9", "Caf\u00e9"),
|
|
("Show \u2764\ufe0f", "Show.\u2764\ufe0f"),
|
|
("Some\x07Title", "SomeTitle"),
|
|
],
|
|
)
|
|
def test_unicode_filenames_use_nfc_and_drop_control_characters(name: str, expected: str) -> None:
|
|
assert sanitize_filename(name, unicode=True) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("name", "expected"),
|
|
[
|
|
("Cafe\u0301 Name", "Cafe.Name"),
|
|
("Caf\u00e9 Name", "Cafe.Name"),
|
|
# Katakana with separate voicing marks transliterates like the composed spelling.
|
|
("\u30ab\u3099\u30f3\u30bf\u3099\u30e0", "gandamu"),
|
|
("\u30ac\u30f3\u30c0\u30e0", "gandamu"),
|
|
("Some\x07Title", "SomeTitle"),
|
|
],
|
|
)
|
|
def test_ascii_filenames_transliterate_both_spellings(name: str, expected: str) -> None:
|
|
assert sanitize_filename(name, unicode=False) == expected
|
|
|
|
|
|
def test_config_option_decides_when_unicode_is_not_passed(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(config, "unicode_filenames", True)
|
|
assert sanitize_filename(THAI) == THAI
|
|
monkeypatch.setattr(config, "unicode_filenames", False)
|
|
assert sanitize_filename(THAI).isascii()
|
|
|
|
|
|
def test_sanitising_a_name_again_does_not_change_it() -> None:
|
|
# The removed "?" sits between the letter and its mark.
|
|
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"
|