mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
fix(remote): send the off form of a service --x/--no-x flag over --remote
This commit is contained in:
@@ -95,6 +95,7 @@ Show the streaming services available on this server, filtered by your allowlist
|
||||
"name": "movie",
|
||||
"kind": "option",
|
||||
"opts": ["-m", "--movie"],
|
||||
"secondary_opts": [],
|
||||
"is_flag": true,
|
||||
"default": false,
|
||||
"help": "Title is a movie.",
|
||||
@@ -124,7 +125,7 @@ Field notes:
|
||||
| `title_regex` | URL/ID pattern the service matches, as a string or list of strings (or `null`). |
|
||||
| `url` | Service URL, taken from the service's CLI short help. |
|
||||
| `help` | The service module's docstring. |
|
||||
| `cli_params` | Array of parameter descriptors. Arguments have `{name, kind: "argument", required}`; options have `{name, kind: "option", opts, is_flag, default, help, type, multiple}` and, for choice options, a `choices` list. |
|
||||
| `cli_params` | Array of parameter descriptors. Arguments have `{name, kind: "argument", required}`; options have `{name, kind: "option", opts, secondary_opts, is_flag, default, help, type, multiple}` and, for choice options, a `choices` list. `secondary_opts` holds the off names of a boolean flag such as `--x/--no-x`, and is empty for other options. |
|
||||
| `needs_auth` | Whether the service overrides `authenticate()`. |
|
||||
| `has_search` | Whether the service supports `POST /api/search`. |
|
||||
| `anonymous_search` | Whether `POST /api/search` operates with no login for this service. The server then ignores `credentials` and `cookies` on a search. |
|
||||
@@ -208,6 +209,7 @@ Get the list of titles behind a title ID, for example episodes or a movie, witho
|
||||
|
||||
You can also pass a service's own CLI options, in a nested `service_params` object or as
|
||||
additional keys. When the body has `service_params`, the server reads service options only from it.
|
||||
To turn off a flag such as `--x/--no-x`, send `false` under its name (`"x": false`), not under its off name.
|
||||
|
||||
#### Logins that need the user
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
"""A service --flag/--no-flag option over --remote: the listing, the client command and the params sent to the server."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import click
|
||||
import pytest
|
||||
|
||||
from unshackle.core.api import handlers, routes
|
||||
from unshackle.core.remote_service import remote_service_params
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.services import Services
|
||||
|
||||
|
||||
class EXAMPLE(Service):
|
||||
"""Example service."""
|
||||
|
||||
@staticmethod
|
||||
@click.command(name="EXAMPLE")
|
||||
@click.argument("title", type=str)
|
||||
@click.option("--flag/--no-flag", default=True)
|
||||
@click.option("--loud", is_flag=True, default=False)
|
||||
@click.option("--quiet/--no-quiet", " /-Q", default=lambda: True)
|
||||
@click.pass_context
|
||||
def cli(ctx: click.Context, **kwargs: Any) -> dict[str, Any]:
|
||||
return kwargs
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def listing(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]:
|
||||
monkeypatch.setattr(Services, "load", staticmethod(lambda tag: EXAMPLE))
|
||||
monkeypatch.setattr(routes, "SERVICE_DETAILS", {})
|
||||
return dict(json.loads(json.dumps(routes.service_details("EXAMPLE"))))
|
||||
|
||||
|
||||
def sent(info: dict[str, Any], argv: list[str]) -> dict[str, Any]:
|
||||
with patch.object(Services, "fetch_remote_services", return_value=[info]):
|
||||
cmd = Services.make_remote_command("EXAMPLE", None)
|
||||
params = cmd.make_context("EXAMPLE", [*argv, "an-id"]).params
|
||||
return remote_service_params(info, {"service_params": {k: v for k, v in params.items() if k != "title"}})
|
||||
|
||||
|
||||
def served(params: dict[str, Any]) -> dict[str, Any]:
|
||||
ctx = click.Context(click.Command("dl"))
|
||||
return dict(handlers.instantiate_service(ctx, EXAMPLE, "an-id", {"service_params": params}))
|
||||
|
||||
|
||||
def test_no_flag_reaches_the_server_service(listing: dict[str, Any]) -> None:
|
||||
params = sent(listing, ["--no-flag"])
|
||||
assert params == {"flag": False}
|
||||
assert served(params) == {"title": "an-id", "flag": False, "loud": False, "quiet": True}
|
||||
|
||||
|
||||
def test_a_secondary_only_alias_with_a_callable_default_reaches_the_server(listing: dict[str, Any]) -> None:
|
||||
params = sent(listing, ["-Q"])
|
||||
assert params == {"flag": True, "quiet": False}
|
||||
assert served(params)["quiet"] is False
|
||||
|
||||
|
||||
def test_a_default_value_is_sent_as_before(listing: dict[str, Any]) -> None:
|
||||
assert sent(listing, []) == {"flag": True}
|
||||
assert sent(listing, ["--loud"]) == {"flag": True, "loud": True}
|
||||
|
||||
|
||||
def test_a_listing_without_secondary_names_keeps_the_primary_flag_only(listing: dict[str, Any]) -> None:
|
||||
for p in listing["cli_params"]:
|
||||
p.pop("secondary_opts", None)
|
||||
assert sent(listing, ["--flag"]) == {"flag": True}
|
||||
with pytest.raises(click.NoSuchOption):
|
||||
sent(listing, ["--no-flag"])
|
||||
|
||||
|
||||
def test_a_rest_relay_sends_false_for_a_default_true_flag(listing: dict[str, Any]) -> None:
|
||||
assert remote_service_params(listing, {"service_params": {"flag": False, "loud": False}}) == {"flag": False}
|
||||
options, _ = handlers.requested_service_options(
|
||||
{"service_params": {"flag": False}}, EXAMPLE.cli.params, handlers.DL_TRANSPORT_KEYS
|
||||
)
|
||||
assert options == {"flag": False}
|
||||
@@ -406,6 +406,7 @@ def service_details(tag: str) -> dict[str, Any]:
|
||||
else:
|
||||
param_info["kind"] = "option"
|
||||
param_info["opts"] = list(param.opts) if hasattr(param, "opts") else []
|
||||
param_info["secondary_opts"] = list(getattr(param, "secondary_opts", []))
|
||||
param_info["is_flag"] = getattr(param, "is_flag", False)
|
||||
default = param.default
|
||||
if callable(default) or type(default).__name__ == "Sentinel":
|
||||
|
||||
@@ -581,9 +581,13 @@ def find_remote_service(services: list[dict], name: str) -> dict | None:
|
||||
|
||||
|
||||
def remote_service_params(info: dict, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""The remote service's cli options in ``data``, read by ``service_options``."""
|
||||
names = {p.get("name") for p in info.get("cli_params") or [] if p.get("kind") == "option"}
|
||||
return {k: v for k, v in service_options(data, names).items() if v is not None and v is not False}
|
||||
"""The remote service's cli options in ``data``, without None, and without False for an option off by default."""
|
||||
defaults = {p.get("name"): p.get("default") for p in info.get("cli_params") or [] if p.get("kind") == "option"}
|
||||
return {
|
||||
k: v
|
||||
for k, v in service_options(data, defaults).items()
|
||||
if v is not None and (v is not False or defaults[k] is not False)
|
||||
}
|
||||
|
||||
|
||||
def make_remote_service(
|
||||
|
||||
@@ -561,6 +561,7 @@ class Services(click.Group):
|
||||
for param in cli_params:
|
||||
if param.get("kind") == "option":
|
||||
opts = param.get("opts", [f"--{param['name']}"])
|
||||
opts = [*opts, *(f" /{s}" for s in param.get("secondary_opts") or [])]
|
||||
kwargs: dict = {}
|
||||
if param.get("is_flag"):
|
||||
kwargs["is_flag"] = True
|
||||
|
||||
Reference in New Issue
Block a user