utils/earned_time/earned_time/_registry.py
Krzysztof kuhy Rudnicki 2f8cf7d6e5 earned_time: add the LeetCode review top-up
A finished coach review pays 20 shutdown minutes, once a day, as a
top-up: never a rung, never in the floor, added last and capped at the
ceiling (sick days too). A row pays only if signed, sourced, first for
its slug ever, and backed by a signed non-manual accepted submission
in the guard ledger from the 14 days before. From 2026-11-23.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXLvi9aq4MxL4XHKJ4
2026-11-21 22:09:35 +01:00

82 lines
2.9 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""Which registry is in force on a day: the tutor cutover (``TUTOR_FROM``).
Before ``TUTOR_FROM`` every day resolves on :data:`~earned_time.EARNERS`,
exactly as it always has; from it on :data:`~earned_time.TUTOR_EARNERS`
(Anki and Automation retired, the tutor paid per minute). The switch is a date, so it
never rewrites how an old day resolved.
"""
from __future__ import annotations
from datetime import UTC, date, datetime
from functools import cache
import sys
from typing import TYPE_CHECKING, Final
from earned_time._focus import FOCUS_WATCH, with_focus
from earned_time._ladder import ANKI_WAIVED_FROM, TUTOR_FROM
from earned_time._review import REVIEW, REVIEW_FROM
if TYPE_CHECKING:
from earned_time._policy import Earner
# Waived from ``ANKI_WAIVED_FROM`` until ``TUTOR_FROM``.
_WAIVED: Final = frozenset({"anki", "automation"})
def registries() -> tuple[tuple[Earner, ...], ...]:
"""Every registry, oldest first.
Read through the package, not this module: consumers' test suites patch
``earned_time.EARNERS`` to register a stand-in earner, and the patch must
keep reaching :func:`earners_for` and :func:`all_earners`.
"""
package = sys.modules["earned_time"] # loaded: it imports this module
return (package.EARNERS, package.TUTOR_EARNERS)
def earners_for(day: date | None = None) -> tuple[Earner, ...]:
"""The registry in force on ``day`` (default today).
Between ``ANKI_WAIVED_FROM`` and ``TUTOR_FROM`` the old registry without
Anki and the anki-backed Automation: neither penalised nor paid. From
``TUTOR_FROM`` an active focus project swaps its replaced earners for one
``focus`` earner (:mod:`earned_time._focus`, 0.19.0). From
``REVIEW_FROM`` the review top-up joins it (0.23.0).
"""
target = day or datetime.now(tz=UTC).astimezone().date()
before, tutor = registries()
if target >= TUTOR_FROM:
return with_focus(_with_review(tutor, target), target)
if target >= ANKI_WAIVED_FROM:
return _without_waived(before)
return before
def _with_review(registry: tuple[Earner, ...], day: date) -> tuple[Earner, ...]:
"""``registry`` plus ``REVIEW`` from ``REVIEW_FROM``, never twice."""
if day < REVIEW_FROM or REVIEW in registry:
return registry
return (*registry, REVIEW)
@cache
def _without_waived(registry: tuple[Earner, ...]) -> tuple[Earner, ...]:
"""``registry`` minus the waived earners; one stable tuple per registry."""
return tuple(e for e in registry if e.name not in _WAIVED)
def all_earners() -> tuple[Earner, ...]:
"""Every earner of every registry, once each, oldest registry first.
What a reader that must not miss a ledger across a cutover watches
(screen-locker's path unit).
"""
seen: dict[Earner, None] = {}
for registry in registries():
seen.update(dict.fromkeys(registry))
seen[REVIEW] = None
seen[FOCUS_WATCH] = None
return tuple(seen)