steam-backlog-enforcer/steam_backlog_enforcer/_cancel_window.py
Krzysztof kuhy Rudnicki 9ed4725d45 Add cancel window to hiding jobs
done, check, pick, pick-manual and hide can be cancelled only while they
hide games in the Steam library: cancel_window events, a SIGTERM handler
during that phase that skips the hide, and the web UI shows the cancel
button while the window is open. Docs fixed (check is mutating).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYNS1qAMRdgAtbr5Rq
2026-11-16 09:44:37 +01:00

40 lines
1.4 KiB
Python

"""A stretch of a command during which a web job may be cancelled.
Most commands cannot be stopped midway, but the library-hiding phase of
``done`` and ``check`` can: it only drives Steam, and a hide that is aborted is
already a skip. The command code marks that phase with :func:`cancel_window`;
*who* acts on the mark is decided by the caller, exactly like
:mod:`steam_backlog_enforcer._progress`. The CLI and the root daemon install
nothing, so for them the window is a no-op; a web job installs one that
announces it to the server and turns SIGTERM into a skipped hide.
"""
from __future__ import annotations
from contextlib import AbstractContextManager, contextmanager, nullcontext
from contextvars import ContextVar
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
WindowFactory = Callable[[], AbstractContextManager[None]]
_ACTIVE: ContextVar[WindowFactory | None] = ContextVar("cancel_window", default=None)
def cancel_window() -> AbstractContextManager[None]:
"""Return the context manager that brackets a cancellable phase."""
factory = _ACTIVE.get()
return nullcontext() if factory is None else factory()
@contextmanager
def use_cancel_window(factory: WindowFactory) -> Iterator[None]:
"""Make *factory* build the cancel windows for the duration of the context."""
token = _ACTIVE.set(factory)
try:
yield
finally:
_ACTIVE.reset(token)