mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 21:15:18 +00:00
187 lines
6.8 KiB
Python
187 lines
6.8 KiB
Python
"""`-w`/`--wanted` track selection for music.
|
|
|
|
A song shares the episode key space, its disc reading as the season and its track as the
|
|
episode. These check the whole path a user takes: the tokens they type, the keys the
|
|
click type makes of them, and the titles the download loop keeps.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
import click
|
|
import jsonpickle
|
|
import pytest
|
|
|
|
from tests.lang_selection.test_kind_only_selection import FakeService, run
|
|
from unshackle.commands.dl import title_wanted
|
|
from unshackle.core.console import console
|
|
from unshackle.core.music.renderer import MusicRenderer
|
|
from unshackle.core.music.tagger import build_tag_values
|
|
from unshackle.core.titles.movie import Movie
|
|
from unshackle.core.titles.music import Album, Song
|
|
from unshackle.core.utils.click_types import SeasonRange
|
|
|
|
|
|
class DummyService:
|
|
pass
|
|
|
|
|
|
def make_song(track: int = 1, disc: int = 1, position: int | None = None, album: str = "Example Album") -> Song:
|
|
return Song(
|
|
id_=f"track-{disc:02}{track:04}",
|
|
service=DummyService,
|
|
name=f"Example Track {track}",
|
|
artist="Example Artist",
|
|
album=album,
|
|
track=track,
|
|
disc=disc,
|
|
position=position,
|
|
year=2025,
|
|
album_artist="Example Artist",
|
|
)
|
|
|
|
|
|
def make_movie() -> Movie:
|
|
return Movie(id_="movie-0001", service=DummyService, name="Example Movie", year=2025)
|
|
|
|
|
|
def select(tokens: str, songs: list[Song]) -> list[int]:
|
|
"""Track numbers kept, given what a user types after -w."""
|
|
wanted = SeasonRange().convert(tokens)
|
|
return [song.track for song in songs if title_wanted(song, wanted)]
|
|
|
|
|
|
def single_disc(count: int = 8) -> list[Song]:
|
|
return [make_song(track=n) for n in range(1, count + 1)]
|
|
|
|
|
|
def multi_disc(per_disc: int = 4) -> list[Song]:
|
|
return [make_song(track=n, disc=d) for d in (1, 2) for n in range(1, per_disc + 1)]
|
|
|
|
|
|
def test_a_bare_number_takes_one_track():
|
|
assert select("3", single_disc()) == [3]
|
|
|
|
|
|
def test_a_range_takes_the_tracks_inside_it():
|
|
assert select("2-5", single_disc()) == [2, 3, 4, 5]
|
|
|
|
|
|
def test_a_comma_list_takes_each_track():
|
|
assert select("1,3,7", single_disc()) == [1, 3, 7]
|
|
|
|
|
|
def test_a_leading_minus_removes_a_track_from_a_range():
|
|
assert select("1-5,-3", single_disc()) == [1, 2, 4, 5]
|
|
|
|
|
|
def test_the_disc_form_takes_one_track_of_that_disc():
|
|
songs = multi_disc()
|
|
kept = [(s.disc, s.track) for s in songs if title_wanted(s, SeasonRange().convert("2x3"))]
|
|
assert kept == [(2, 3)]
|
|
|
|
|
|
def test_the_disc_form_ranges_inside_one_disc():
|
|
songs = multi_disc()
|
|
kept = [(s.disc, s.track) for s in songs if title_wanted(s, SeasonRange().convert("2x1-2x3"))]
|
|
assert kept == [(2, 1), (2, 2), (2, 3)]
|
|
|
|
|
|
def test_a_bare_number_on_a_multi_disc_release_is_disc_one():
|
|
"""An omitted disc is disc 1, the number the tracklist shows for a one-disc release."""
|
|
songs = multi_disc()
|
|
kept = [(s.disc, s.track) for s in songs if title_wanted(s, SeasonRange().convert("1-2"))]
|
|
assert kept == [(1, 1), (1, 2)]
|
|
|
|
|
|
def test_discs_mix_in_one_selection():
|
|
songs = multi_disc()
|
|
kept = [(s.disc, s.track) for s in songs if title_wanted(s, SeasonRange().convert("1,2x4"))]
|
|
assert kept == [(1, 1), (2, 4)]
|
|
|
|
|
|
def test_a_selection_that_matches_nothing_keeps_no_track():
|
|
assert select("40-45", single_disc()) == []
|
|
|
|
|
|
def test_the_whole_release_downloads_without_a_selection():
|
|
songs = single_disc()
|
|
assert [s.track for s in songs if title_wanted(s, [])] == [1, 2, 3, 4, 5, 6, 7, 8]
|
|
|
|
|
|
def test_a_movie_stays_selected_whatever_the_tokens_are():
|
|
"""A movie has no selection key, so -w must not remove it."""
|
|
assert title_wanted(make_movie(), SeasonRange().convert("1-5")) is True
|
|
|
|
|
|
def test_a_range_across_discs_takes_the_rest_of_the_first_disc():
|
|
"""Same rule as a season range: the left disc runs to its end before the right one starts."""
|
|
songs = multi_disc()
|
|
kept = [(s.disc, s.track) for s in songs if title_wanted(s, SeasonRange().convert("1x3-2x2"))]
|
|
assert kept == [(1, 3), (1, 4), (2, 1), (2, 2)]
|
|
|
|
|
|
@pytest.mark.parametrize("token", ["1x", "x1", "1x2x3", "1.2"])
|
|
def test_bad_music_tokens_fail(token):
|
|
with pytest.raises(click.UsageError):
|
|
SeasonRange().parse_tokens(token)
|
|
|
|
|
|
def test_song_matches_only_its_own_key():
|
|
song = make_song(track=3, disc=2)
|
|
assert song.matches_wanted(["2x3"]) is True
|
|
assert song.matches_wanted(["1x3", "2x4"]) is False
|
|
|
|
|
|
def test_a_playlist_lists_and_selects_by_position_but_tags_by_album_track():
|
|
songs = [make_song(1, position=1, album="Z"), make_song(6, 2, 2, "B"), make_song(1, position=3, album="A")]
|
|
playlist = Album(reversed(songs), kind="playlist")
|
|
|
|
assert list(playlist) == songs
|
|
assert select("2", playlist) == [6]
|
|
assert not songs[1].matches_wanted(["2x6"])
|
|
assert [MusicRenderer().song_line(s, playlist).plain[:2] for s in playlist] == ["01", "02", "03"]
|
|
tags = build_tag_values(songs[1])
|
|
assert (tags["TRACKNUMBER"], tags["DISCNUMBER"]) == ("6", "2")
|
|
|
|
|
|
def test_a_playlist_shows_no_disc_count():
|
|
playlist = Album([make_song(6, 2, position=1), make_song(1, 3, position=2)], kind="playlist")
|
|
assert "Discs" not in list(MusicRenderer().render_header(playlist).columns[0].cells)
|
|
assert "Discs" in list(MusicRenderer().render_header(Album([make_song(6, 2)])).columns[0].cells)
|
|
|
|
|
|
@pytest.mark.parametrize(("position", "key", "rule"), [(9, "1x9", "Track 09"), (None, "2x6", "Track 06")])
|
|
def test_the_download_loop_names_the_number_that_selected_the_song(monkeypatch, caplog, position, key, rule):
|
|
service = FakeService({1: {}})
|
|
tracks = service.get_tracks(service.get_titles()[0])
|
|
service.get_titles = lambda: Album([make_song(6, 2, position, "B"), make_song(1)])
|
|
service.get_tracks = lambda title: tracks
|
|
|
|
with console.capture() as out:
|
|
run(monkeypatch, caplog, {}, service=service, wanted=[key])
|
|
|
|
assert re.findall(r"Track \d+: Example Track \d+", out.get()) == [f"{rule}: Example Track 6"]
|
|
|
|
|
|
def test_a_release_without_positions_keeps_its_order_and_numbers():
|
|
songs = [make_song(1, album="A"), make_song(6, 2, album="B"), make_song(1, album="Z")]
|
|
release = Album(reversed(songs))
|
|
|
|
assert list(release) == songs
|
|
assert [MusicRenderer().song_line(s, release).plain[:4].strip() for s in release] == ["01", "2.06", "01"]
|
|
|
|
|
|
def test_a_release_from_an_older_title_cache_reads_as_having_no_positions():
|
|
"""jsonpickle restores a Song without its __init__, so an older entry has no 'position' key."""
|
|
songs = [make_song(6, 2, album="B"), make_song(1, album="Z")]
|
|
for song in songs:
|
|
del song.__dict__["position"]
|
|
|
|
restored = jsonpickle.loads(jsonpickle.dumps(songs))
|
|
|
|
assert ["position" in vars(song) for song in restored] == [False, False]
|
|
assert [song.listing_number for song in restored] == [(2, 6), (1, 1)]
|
|
assert [song.album for song in Album(reversed(restored))] == ["B", "Z"]
|