mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-25 10:25:09 +00:00
refactor(api): keep the dl option facts in one table in core/dl_options.py
This commit is contained in:
@@ -1,41 +1,56 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import logging
|
||||
|
||||
import click
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.api.handlers import (
|
||||
from unshackle.core.api import routes
|
||||
from unshackle.core.api.handlers import validate_download_parameters
|
||||
from unshackle.core.dl_options import (
|
||||
API_SHAPED_DL_PARAMS,
|
||||
DEFAULT_DOWNLOAD_PARAMS,
|
||||
DL_OPTION_ALIASES,
|
||||
DL_OPTIONS,
|
||||
FORWARDED_DL_PARAMS,
|
||||
cast_dl_params,
|
||||
validate_download_parameters,
|
||||
forwarded_dl_params,
|
||||
remote_dl_params,
|
||||
)
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.tracks import Audio, Subtitle, Video
|
||||
|
||||
CLI_ONLY = {
|
||||
"select_titles",
|
||||
"list_",
|
||||
"list_titles",
|
||||
"import_file",
|
||||
"cdm_name",
|
||||
"remote",
|
||||
"server",
|
||||
"local_proxy",
|
||||
"postscript",
|
||||
"no_postscript",
|
||||
"merge_video",
|
||||
"real_video_bitrate",
|
||||
"real_audio_bitrate",
|
||||
"speed_limit",
|
||||
}
|
||||
CLI_ONLY = {DL_OPTION_ALIASES.get(name, name) for name, option in DL_OPTIONS.items() if option is None}
|
||||
|
||||
|
||||
def test_every_dl_option_is_an_api_param_or_cli_only():
|
||||
"""A new dl option fails here until it is added to DEFAULT_DOWNLOAD_PARAMS or to CLI_ONLY."""
|
||||
api_names = set(cast_dl_params(dl.cli, {}))
|
||||
def test_the_table_decides_every_dl_option():
|
||||
"""A new dl option fails here until DL_OPTIONS says whether the REST API takes it."""
|
||||
cli_names = {p.name for p in dl.cli.params}
|
||||
assert api_names - cli_names == set()
|
||||
assert cli_names - api_names == CLI_ONLY
|
||||
assert {DL_OPTION_ALIASES.get(name, name) for name in DL_OPTIONS} == cli_names
|
||||
assert set(cast_dl_params(dl.cli, {})) == cli_names - CLI_ONLY
|
||||
|
||||
|
||||
def test_the_openapi_yaml_names_each_api_dl_option_and_no_other():
|
||||
"""The download request body documents every dl option the API takes, beside its own request keys."""
|
||||
schema = yaml.safe_load((routes.download.__doc__ or "").split("---", 1)[1])
|
||||
properties = schema["requestBody"]["content"]["application/json"]["schema"]["properties"]
|
||||
assert DEFAULT_DOWNLOAD_PARAMS.keys() <= properties.keys()
|
||||
assert properties.keys() - DEFAULT_DOWNLOAD_PARAMS.keys() == {"service", "title_id", "remote", "client_name", "cdm"}
|
||||
|
||||
|
||||
def test_the_server_reads_back_what_a_remote_client_forwards():
|
||||
params = {
|
||||
"lang": ["orig", "es-419"],
|
||||
"v_lang": [],
|
||||
"a_lang": ["en"],
|
||||
"s_lang": ["all"],
|
||||
"acodec": [Audio.Codec.EC3],
|
||||
"forced_subs": True,
|
||||
"exact_lang": True,
|
||||
}
|
||||
assert forwarded_dl_params({"dl_params": remote_dl_params(params)}) == params
|
||||
|
||||
|
||||
def test_api_defaults_cast_to_the_dl_defaults():
|
||||
@@ -69,11 +84,86 @@ def test_range_takes_the_api_spellings(value, expected):
|
||||
|
||||
|
||||
def test_values_are_cast_by_the_dl_click_types():
|
||||
cast = cast_dl_params(dl.cli, {"quality": "1080p,720", "vcodec": "H265", "acodec": ["EC3"], "sample": True})
|
||||
cast = cast_dl_params(
|
||||
dl.cli,
|
||||
{
|
||||
"quality": "1080p,720",
|
||||
"vcodec": "H265",
|
||||
"acodec": ["EC3"],
|
||||
"range": ["HDR10+", "dv"],
|
||||
"lang": ["en"],
|
||||
"slow": "20-40",
|
||||
"wanted": ["S01E01"],
|
||||
"downloads": 3,
|
||||
"sub_format": "srt",
|
||||
"output_dir": "",
|
||||
"cdm_only": False,
|
||||
"sample": "0-60",
|
||||
"tvdb_order": "dvd",
|
||||
},
|
||||
)
|
||||
assert cast["quality"] == [1080, 720]
|
||||
assert cast["vcodec"] == [Video.Codec.HEVC]
|
||||
assert [c.name for c in cast["acodec"]] == ["EC3"]
|
||||
assert cast["sample"] is not None
|
||||
assert cast["acodec"] == [Audio.Codec.EC3]
|
||||
assert cast["range_"] == [Video.Range.HDR10P, Video.Range.DV]
|
||||
assert cast["lang"] == ["en"]
|
||||
assert (cast["slow"], cast["wanted"]) == ("20-40", ["S01E01"])
|
||||
assert (cast["downloads"], cast["sample"], cast["tvdb_order"]) == (3, (0, 60), "dvd")
|
||||
assert cast["sub_format"] is Subtitle.Codec.SubRip
|
||||
assert (cast["output_dir"], cast["cdm_only"], cast["s_lang"]) == (None, False, ["all"])
|
||||
|
||||
|
||||
def test_every_dl_option_reaches_dl_result():
|
||||
"""An option outside the dl.__init__ and dl.result signatures is read from ctx.params elsewhere."""
|
||||
named = set(inspect.signature(dl.__init__).parameters) | set(inspect.signature(dl.result).parameters)
|
||||
assert {p.name for p in dl.cli.params} - named == {
|
||||
"server",
|
||||
"remote",
|
||||
"local_proxy",
|
||||
"reset_cache",
|
||||
"no_cache",
|
||||
"cdm_name",
|
||||
"import_file",
|
||||
}
|
||||
|
||||
|
||||
def test_the_server_reads_the_forwarded_dl_params(caplog):
|
||||
data = {
|
||||
"dl_params": {"a_lang": "x", "acodec": ["ddp", "NOPE"], "exact_lang": 1, "forced_subs": True, "lang": ["en"]}
|
||||
}
|
||||
with caplog.at_level(logging.WARNING, logger="api"):
|
||||
assert list(forwarded_dl_params(data).items()) == [
|
||||
("lang", ["en"]),
|
||||
("v_lang", []),
|
||||
("a_lang", []),
|
||||
("s_lang", ["all"]),
|
||||
("acodec", [Audio.Codec.EC3]),
|
||||
("forced_subs", True),
|
||||
("exact_lang", False),
|
||||
]
|
||||
assert [(r.name, r.getMessage()) for r in caplog.records] == [
|
||||
("api", "Ignoring dl_params.a_lang: it must be an array of strings"),
|
||||
("api", "Ignoring unknown dl_params.acodec value: NOPE"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"parent_params,sent",
|
||||
[
|
||||
(
|
||||
{"lang": ["orig"], "v_lang": None, "a_lang": [], "s_lang": ["all"], "acodec": None, "forced_subs": 0},
|
||||
{"lang": ["orig"], "a_lang": [], "s_lang": ["all"], "forced_subs": False},
|
||||
),
|
||||
({"exact_lang": None, "quality": [1080]}, {"exact_lang": False}),
|
||||
],
|
||||
)
|
||||
def test_the_client_sends_what_it_has_a_value_for(parent_params, sent):
|
||||
assert remote_dl_params(parent_params) == sent
|
||||
|
||||
|
||||
def test_no_forwarded_option_has_an_alias():
|
||||
"""remote_dl_params reads the API name from the click context, so an aliased option cannot be forwarded."""
|
||||
assert not {name for name, _ in FORWARDED_DL_PARAMS} & DL_OPTION_ALIASES.keys()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["lang", "v_lang", "a_lang", "s_lang", "forced_s_lang"])
|
||||
|
||||
@@ -10,6 +10,7 @@ import pytest
|
||||
from unshackle.core.api import download_manager as dm
|
||||
from unshackle.core.api import handlers
|
||||
from unshackle.core.api.errors import APIError, APIErrorCode
|
||||
from unshackle.core.dl_options import DEFAULT_DOWNLOAD_PARAMS
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -91,10 +92,10 @@ def test_relayed_job_skips_service_dl(env, monkeypatch):
|
||||
return "remote-server", {"tag": "EXAMPLE1"}, data
|
||||
|
||||
monkeypatch.setattr(handlers, "relay_target", relay)
|
||||
baseline_workers = handlers.DEFAULT_DOWNLOAD_PARAMS["workers"]
|
||||
baseline_workers = DEFAULT_DOWNLOAD_PARAMS["workers"]
|
||||
params = env(dl={"workers": 7})
|
||||
assert params["workers"] == baseline_workers
|
||||
assert set(params["client_keys"]) >= set(handlers.DEFAULT_DOWNLOAD_PARAMS)
|
||||
assert set(params["client_keys"]) >= set(DEFAULT_DOWNLOAD_PARAMS)
|
||||
|
||||
|
||||
def test_client_sent_keys_uses_the_stamp():
|
||||
|
||||
@@ -138,7 +138,7 @@ def test_job_status_values() -> None:
|
||||
def test_api_sub_format_keeps_the_original_sentinel() -> None:
|
||||
"""sub_format "original" keeps the source format, so the API cast must not turn it into a codec or None."""
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.api.handlers import cast_dl_params
|
||||
from unshackle.core.dl_options import cast_dl_params
|
||||
from unshackle.core.tracks import Subtitle
|
||||
|
||||
assert cast_dl_params(dl.cli, {"sub_format": "ORIGINAL"})["sub_format"] == "original"
|
||||
|
||||
Reference in New Issue
Block a user