forked from kenzuya/unshackle
refactor(api): give every served service its dl options from the run plan
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""The run plan: one check of the dl options for the CLI and the REST download job."""
|
||||
"""The run plan: one check of the dl options for the CLI and the REST download job, one cast for a served service."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -11,9 +11,10 @@ from click.core import ParameterSource
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.api.errors import categorize_exception
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.dl_options import remote_dl_params
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.run_plan import PlanError, api_plan, bitrate_range, check_options
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.run_plan import PlanError, api_plan, bitrate_range, check_options, served_plan
|
||||
from unshackle.core.tracks import Audio, Video
|
||||
|
||||
|
||||
def cli_params(*args: str) -> dict[str, Any]:
|
||||
@@ -107,3 +108,80 @@ def test_dl_checks_the_plan_before_the_service_loads(monkeypatch: pytest.MonkeyP
|
||||
monkeypatch.setattr(config, "output_template", {"movies": "{title}"})
|
||||
with pytest.raises(PlanError, match="--sample and --include-sample"):
|
||||
dl.cli.main(["--sample", "--include-sample", "EXAMPLE", "t"], standalone_mode=False)
|
||||
|
||||
|
||||
def test_the_served_plan_reads_what_a_remote_client_sends() -> None:
|
||||
parent = {
|
||||
"lang": ["orig", "es-419"],
|
||||
"v_lang": [],
|
||||
"a_lang": ["en"],
|
||||
"s_lang": ["all"],
|
||||
"acodec": [Audio.Codec.EC3],
|
||||
"forced_subs": True,
|
||||
"exact_lang": True,
|
||||
}
|
||||
data = {
|
||||
"range_": ["DV", "HDR10P"],
|
||||
"vcodec": ["HEVC"],
|
||||
"quality": [720, 1080],
|
||||
"best_available": True,
|
||||
"generate_dv": True,
|
||||
"no_proxy": True,
|
||||
"dl_params": remote_dl_params(parent),
|
||||
"service_params": {},
|
||||
}
|
||||
plan = served_plan(data, "http://proxy", "work")
|
||||
assert plan.params["range_"] == [Video.Range.DV, Video.Range.HDR10P]
|
||||
assert plan.params["vcodec"] == [Video.Codec.HEVC]
|
||||
assert plan.params["quality"] == [1080, 720]
|
||||
assert {k: plan.params[k] for k in ("best_available", "generate_dv", "no_proxy")} == dict.fromkeys(
|
||||
("best_available", "generate_dv", "no_proxy"), True
|
||||
)
|
||||
assert {k: plan.params[k] for k in parent} == parent
|
||||
assert (plan.params["proxy"], plan.params["profile"]) == ("http://proxy", "work")
|
||||
assert plan.sources["range_"] is ParameterSource.COMMANDLINE
|
||||
assert plan.sources["proxy"] is ParameterSource.COMMANDLINE
|
||||
assert plan.sources["workers"] is ParameterSource.DEFAULT
|
||||
|
||||
|
||||
def test_the_served_plan_gives_an_old_client_the_dl_defaults() -> None:
|
||||
params = served_plan({}).params
|
||||
assert params["range_"] == [Video.Range.SDR]
|
||||
assert (params["vcodec"], params["quality"], params["acodec"]) == ([], [], [])
|
||||
assert (params["lang"], params["s_lang"]) == (["orig"], ["all"])
|
||||
assert (params["forced_subs"], params["exact_lang"], params["no_proxy"]) == (False, False, False)
|
||||
|
||||
|
||||
def test_dl_params_set_only_the_options_a_remote_client_forwards() -> None:
|
||||
data = {"dl_params": {"proxy": "http://other", "no_cache": True, "range": ["DV"], "lang": ["en"]}}
|
||||
params = served_plan(data, "http://server").params
|
||||
assert (params["proxy"], params["no_cache"], params["range_"]) == ("http://server", False, [Video.Range.SDR])
|
||||
assert params["lang"] == ["en"]
|
||||
|
||||
|
||||
def test_a_flat_key_counts_only_when_it_cannot_be_a_service_option() -> None:
|
||||
assert served_plan({"quality": "UHD"}).params["quality"] == []
|
||||
assert served_plan({"quality": [1080]}, flat={"quality"}).params["quality"] == [1080]
|
||||
assert served_plan({"quality": [1080], "service_params": {}}).params["quality"] == [1080]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("value", "expected"), [("false", True), (1, True), (0, False), (None, False)])
|
||||
def test_no_proxy_counts_by_truth_value_as_the_proxy_gate_reads_it(value: Any, expected: bool) -> None:
|
||||
assert served_plan({"no_proxy": value}).params["no_proxy"] is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("data", "key"),
|
||||
[
|
||||
({"range_": ["SDR", "NOPE"]}, "range"),
|
||||
({"vcodec": ["NOPE"]}, "vcodec"),
|
||||
({"quality": ["best"]}, "quality"),
|
||||
({"quality": [1080, 0]}, "quality"),
|
||||
({"dl_params": {"acodec": ["NOPE"]}}, "acodec"),
|
||||
({"dl_params": {"lang": [None, {"x": 1}]}}, "lang"),
|
||||
({"dl_params": {"a_lang": [1]}}, "a_lang"),
|
||||
],
|
||||
)
|
||||
def test_the_served_plan_refuses_an_unknown_value(data: dict[str, Any], key: str) -> None:
|
||||
with pytest.raises(PlanError, match=f"Invalid {key}"):
|
||||
served_plan({**data, "service_params": {}})
|
||||
|
||||
Reference in New Issue
Block a user