forked from kenzuya/unshackle
style: turn on ruff UP and B rules and fix every finding
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"""The dl option table: which dl options the REST API takes, their API defaults, and what a remote client forwards."""
|
||||
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, List, Literal, Mapping, Optional, get_args
|
||||
from typing import Any, Literal, get_args
|
||||
|
||||
import click
|
||||
|
||||
@@ -27,12 +28,12 @@ class ApiOption:
|
||||
"""
|
||||
|
||||
default: Any
|
||||
remote: Optional[RemoteKind] = None
|
||||
remote: RemoteKind | None = None
|
||||
|
||||
|
||||
CLI_ONLY = None
|
||||
|
||||
DL_OPTIONS: Dict[str, Optional[ApiOption]] = {
|
||||
DL_OPTIONS: dict[str, ApiOption | None] = {
|
||||
"profile": ApiOption(None),
|
||||
"quality": ApiOption([]),
|
||||
"vcodec": ApiOption([]),
|
||||
@@ -129,7 +130,7 @@ DL_OPTIONS: Dict[str, Optional[ApiOption]] = {
|
||||
An option the REST API takes has an ``ApiOption``; a ``CLI_ONLY`` option never reaches dl from the API.
|
||||
"""
|
||||
|
||||
DEFAULT_DOWNLOAD_PARAMS: Dict[str, Any] = {name: option.default for name, option in DL_OPTIONS.items() if option}
|
||||
DEFAULT_DOWNLOAD_PARAMS: dict[str, Any] = {name: option.default for name, option in DL_OPTIONS.items() if option}
|
||||
FORWARDED_DL_PARAMS = [
|
||||
(name, option)
|
||||
for kind in get_args(RemoteKind)
|
||||
@@ -138,15 +139,15 @@ FORWARDED_DL_PARAMS = [
|
||||
]
|
||||
|
||||
|
||||
def cast_dl_params(cli: click.Command, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def cast_dl_params(cli: click.Command, params: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Cast each ``DEFAULT_DOWNLOAD_PARAMS`` parameter in ``params`` by its dl click type, under its dl option name.
|
||||
|
||||
A missing, ``None`` or empty-string parameter, and an empty ``range``, take the API default, so
|
||||
``output_dir: ""`` is not the working directory. A parameter outside ``DEFAULT_DOWNLOAD_PARAMS`` never reaches
|
||||
dl, so API clients cannot set a new CLI option until the table opens it. Raises ``click.BadParameter``.
|
||||
"""
|
||||
kept: Dict[str, Any] = {}
|
||||
to_cast: Dict[str, Any] = {}
|
||||
kept: dict[str, Any] = {}
|
||||
to_cast: dict[str, Any] = {}
|
||||
for key, default in DEFAULT_DOWNLOAD_PARAMS.items():
|
||||
value = params.get(key)
|
||||
if value is None or value == "" or (key == "range" and value == []):
|
||||
@@ -160,12 +161,12 @@ def cast_dl_params(cli: click.Command, params: Dict[str, Any]) -> Dict[str, Any]
|
||||
raise click.BadParameter(f"Invalid {key}: {e.message}") from e
|
||||
|
||||
|
||||
def remote_dl_params(params: Mapping[str, Any]) -> Dict[str, Any]:
|
||||
def remote_dl_params(params: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""The ``dl_params`` a remote client sends when it creates a remote session, from its dl context params.
|
||||
|
||||
An empty list is sent as-is, because only an absent key falls back to the dl default on the server.
|
||||
"""
|
||||
sent: Dict[str, Any] = {}
|
||||
sent: dict[str, Any] = {}
|
||||
for key, option in FORWARDED_DL_PARAMS:
|
||||
if option.remote == "flag":
|
||||
if key in params:
|
||||
@@ -175,7 +176,7 @@ def remote_dl_params(params: Mapping[str, Any]) -> Dict[str, Any]:
|
||||
return sent
|
||||
|
||||
|
||||
def forwarded_dl_params(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def forwarded_dl_params(data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Read the dl track selection that a remote-dl client sends under ``dl_params``.
|
||||
|
||||
Services read these values from ``ctx.parent.params`` to pick the manifests they
|
||||
@@ -189,7 +190,7 @@ def forwarded_dl_params(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
if not isinstance(raw, dict):
|
||||
raw = {}
|
||||
|
||||
def str_list(key: str) -> Optional[List[str]]:
|
||||
def str_list(key: str) -> list[str] | None:
|
||||
value = raw.get(key)
|
||||
if isinstance(value, list) and all(isinstance(v, str) for v in value):
|
||||
return list(value)
|
||||
@@ -197,7 +198,7 @@ def forwarded_dl_params(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
log.warning(f"Ignoring dl_params.{key}: it must be an array of strings")
|
||||
return None
|
||||
|
||||
params: Dict[str, Any] = {}
|
||||
params: dict[str, Any] = {}
|
||||
for key, option in FORWARDED_DL_PARAMS:
|
||||
if option.remote == "flag":
|
||||
params[key] = raw.get(key) is True
|
||||
|
||||
Reference in New Issue
Block a user