refactor(core): one TitleSource base for Service, remote and import

This commit is contained in:
imSp4rky
2026-11-25 21:46:55 -07:00
parent 9febbb20c5
commit 92e8abbdbc
21 changed files with 619 additions and 470 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ from typing import Any
from uuid import UUID
from unshackle.core.remote_service import RemoteService, ServerVault
from unshackle.core.title_source import KeyArrival
def remote_keys(
@@ -31,7 +32,7 @@ class MemoryKeySource:
A ``licence`` of None hands the track to this machine, as a capped server does.
"""
server_cdm = True
key_arrival = KeyArrival.SERVER
server_cdm_type = "widevine"
server_vault: Any = None
@@ -1,10 +1,4 @@
"""Contract tests for the RemoteService adapter.
RemoteService copies the Service interface but does not inherit from it. When a
method is added to the base Service and wired into dl.py, nothing forces it onto
the adapter, so it can go missing here until a real download calls it. These
tests pin the methods dl.py depends on so a gap fails in CI, not on the devbox.
"""
"""RemoteService licence callbacks that must not touch the remote session."""
from __future__ import annotations
@@ -14,30 +8,6 @@ from unshackle.core.remote_service import RemoteService
pytestmark = pytest.mark.unit
SERVICE_CONTRACT = (
"authenticate",
"get_titles",
"get_titles_cached",
"get_tracks",
"get_chapters",
"get_widevine_license",
"get_playready_license",
"get_clearkey_license",
"get_widevine_service_certificate",
"resolve_server_keys",
"on_segment_downloaded",
"on_track_downloaded",
"on_track_decrypted",
"on_track_repacked",
"on_track_multiplex",
"close",
)
@pytest.mark.parametrize("name", SERVICE_CONTRACT)
def test_remote_service_implements_contract(name: str) -> None:
assert callable(getattr(RemoteService, name, None)), f"RemoteService is missing {name}()"
def test_get_clearkey_license_returns_none() -> None:
# Regression: clearkey uses no CDM, so the adapter returns None and the
@@ -647,7 +647,7 @@ def _remote_service(parent_params: dict, service_params: dict) -> RemoteService:
svc._server_accounts = {"global": True}
svc._server_cdm = True
svc._service_params = service_params
svc._session = requests.Session()
svc.session = requests.Session()
svc.log = logging.getLogger("test")
svc.client = SimpleNamespace(post=post, server_url="https://server.example")
return svc
@@ -671,8 +671,8 @@ def test_the_client_session_downloads_through_the_proxy_it_sends(
svc = _remote_service({"no_proxy": no_proxy, "proxy": "http://proxy.example:8080"}, {})
with pytest.raises(_SentCreate) as exc:
svc.authenticate()
assert dict(svc._session.proxies) == expected
assert svc._session.trust_env is no_proxy # a proxied session ignores HTTP(S)_PROXY
assert dict(svc.session.proxies) == expected
assert svc.session.trust_env is no_proxy # a proxied session ignores HTTP(S)_PROXY
assert exc.value.args[0].get("proxy") == (None if no_proxy else "http://proxy.example:8080")
@@ -17,6 +17,7 @@ from tests.remote.sessions import session_entry
from unshackle.core.api import handlers
from unshackle.core.api.errors import APIError
from unshackle.core.drm import BlowfishStripe
from unshackle.core.drm.cenc import drm_stub
from unshackle.core.remote_service import RemoteService
pytestmark = pytest.mark.unit
@@ -286,7 +287,7 @@ def test_resolve_server_keys_names_only_the_manifest_kids():
widevine = Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=[KID]))
assert resolve([widevine]).kids == [KID]
playready = RemoteService.create_drm_stub("playready", [KID.hex])
playready = drm_stub("playready", [KID.hex])
assert resolve([playready], "playready").kids == [KID]
+3 -2
View File
@@ -25,6 +25,7 @@ from unshackle.core.cdm.detect import cdm_type_stub
from unshackle.core.config import config
from unshackle.core.drm import Widevine, key_resolution
from unshackle.core.remote_service import RemoteService
from unshackle.core.title_source import KeyArrival
from unshackle.core.vaults import Vaults
from unshackle.vaults.SQLite import SQLite
@@ -134,7 +135,7 @@ def decrypt(
assert vaults.get_key(VAULTED) == (OTHER_KEY, local)
pushed: list = []
source = SimpleNamespace(
server_cdm=False,
key_arrival=KeyArrival.CALLBACK,
server_vault=SimpleNamespace(add_keys=lambda service, keys, track: pushed.append((keys, track))),
)
keys = resolver(vaults, tag="EXAMPLE", source=source)
@@ -306,7 +307,7 @@ def test_cdm_only_declines_the_server_cdm_and_its_vault(monkeypatch, tmp_path) -
try:
svc.authenticate()
assert svc.posts[0]["server_cdm"] is False
assert (svc.server_cdm, svc._server_vault) == (False, False)
assert (svc.key_arrival, svc._server_vault) == (KeyArrival.CALLBACK, False)
svc.resolve_server_keys(SimpleNamespace(tracks=SimpleNamespace(videos=["v"], audio=[])))
assert len(svc.posts) == 1
finally: