mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 22:32:30 +01:00
Per-output locking: the alarm now paints one full challenge per live monitor instead of one window on the primary. `_ViewGroup` presents the N copies as one -- every label update fans out, and because all entries share a single root-mastered StringVar, answering on whichever screen you are actually looking at dismisses the alarm everywhere. The escape hatch is the reason this repo could not stay unmigrated. The alarm was the only locker with no way out but four correct rounds against a 30-second refresh, which is a lot to ask of someone who has just woken up and nothing at all to fall back on if the alarm misfires at the wrong hour. It is deliberately exactly as hard as sick mode: hidden for the first five minutes so it is never the easy first move, gone entirely once a rolling budget is spent, and costing a written justification plus a wait that doubles with each recent use. It grants no workout skip. `EscapeHatch` owns the whole flow so open and close stay symmetric. That symmetry is the fix for a real bug: `_open_escape` used to pack_forget the challenge widgets and cancel only flipped a bool, so "Back to the alarm" left a dead form on a blank screen, leaked the dialog frame, and made the next open sweep the dialog instead of the challenge. pack_forget does not remember its options, so the challenge now lives in one `challenge_frame` that is forgotten and re-packed as a unit. Tests: the single-output default made every "on every monitor" assertion degenerate into 1 == 1, and a shared MagicMock return_value made N surfaces N references to one widget -- a loop cropped to containers[:1] passed the whole suite. A `dual_output` fixture plus distinct-identity mocks now cover all five fan-out loops; each was mutation-checked by cropping it and confirming the suite goes red. The suite also no longer reads the real escape history or signs with the production HMAC key. 326 tests, 100% branch coverage, pre-commit clean, no lint suppressions.
290 lines
9.2 KiB
Python
290 lines
9.2 KiB
Python
"""The alarm's sanctioned way out when the challenge genuinely isn't happening.
|
|
|
|
Until now the alarm had exactly one exit: four correct challenge rounds against
|
|
a 30-second refresh. That is a lot to ask of someone who has just woken up, and
|
|
it is *nothing* to fall back on if the alarm misfires at the wrong hour. Every
|
|
other locker here has a bounded escape -- screen-locker's sick mode,
|
|
diet-guard's log-a-meal -- so the alarm now has the same, built on the shared
|
|
:mod:`gatelock._escape` core rather than a private copy.
|
|
|
|
The shape, deliberately identical to sick mode:
|
|
|
|
* the button only appears after the alarm has been up a while, so it is never
|
|
the *easy* first move;
|
|
* it disappears entirely once any rolling budget is spent;
|
|
* it costs a written justification, with the last ten shown back to you;
|
|
* it costs a wait that doubles with each recent use;
|
|
* every entry is HMAC-signed, so editing the history is detectable.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
from dataclasses import dataclass
|
|
import logging
|
|
import tkinter as tk
|
|
from typing import TYPE_CHECKING
|
|
|
|
from gatelock import EscapeDraft, EscapePolicy, EscapeTracker
|
|
|
|
from wake_alarm._constants import ESCAPE_HISTORY_FILE, ESCAPE_OFFER_AFTER_SECONDS
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
from gatelock import LockConfig
|
|
|
|
from wake_alarm._alarm_view import _ViewGroup
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
ALARM_ESCAPE_POLICY = EscapePolicy(
|
|
name="cant-wake",
|
|
label="Can't-wake",
|
|
)
|
|
"""Budgets left at the shared defaults, so this is exactly as hard as sick mode."""
|
|
|
|
|
|
def build_tracker() -> EscapeTracker:
|
|
"""Build the alarm's escape tracker over its own history file."""
|
|
tracker = EscapeTracker(ALARM_ESCAPE_POLICY, ESCAPE_HISTORY_FILE)
|
|
tracker.load()
|
|
return tracker
|
|
|
|
|
|
def is_offerable(tracker: EscapeTracker, elapsed_seconds: float) -> bool:
|
|
"""Whether the hatch should be shown at all.
|
|
|
|
Two independent gates: the alarm must have been up long enough that this
|
|
is not the first thing you reach for, and the budget must not be spent.
|
|
"""
|
|
if elapsed_seconds < ESCAPE_OFFER_AFTER_SECONDS:
|
|
return False
|
|
return not tracker.is_budget_exhausted()
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class EscapeCallbacks:
|
|
"""Who to tell how the form ended."""
|
|
|
|
on_granted: Callable[[], None]
|
|
on_cancel: Callable[[], None]
|
|
|
|
|
|
@dataclass
|
|
class _FormFields:
|
|
"""The four inputs the justification is read back out of."""
|
|
|
|
reason: tk.Entry
|
|
onset: tk.Entry
|
|
severity: tk.Entry
|
|
description: tk.Text
|
|
|
|
|
|
class EscapeDialog:
|
|
"""The justification form and its escalating wait."""
|
|
|
|
def __init__(
|
|
self,
|
|
parent: tk.Misc,
|
|
config: LockConfig,
|
|
tracker: EscapeTracker,
|
|
callbacks: EscapeCallbacks,
|
|
) -> None:
|
|
"""Build the form inside ``parent``."""
|
|
self._tracker = tracker
|
|
self._callbacks = callbacks
|
|
self._remaining = 0
|
|
|
|
self.frame = tk.Frame(parent, bg=config.bg)
|
|
self._status = tk.Label(
|
|
self.frame,
|
|
text=tracker.budget_summary(),
|
|
font=(config.font_family, 16),
|
|
fg=config.muted,
|
|
bg=config.bg,
|
|
)
|
|
self._status.pack(pady=6)
|
|
|
|
tk.Label(
|
|
self.frame,
|
|
text="Why can you not do this right now?",
|
|
font=(config.font_family, 22, "bold"),
|
|
fg=config.fg,
|
|
bg=config.bg,
|
|
).pack(pady=8)
|
|
|
|
tk.Label(
|
|
self.frame,
|
|
text=tracker.format_recent(),
|
|
font=("Courier", 11),
|
|
fg=config.muted,
|
|
bg=config.bg,
|
|
justify="left",
|
|
).pack(pady=6)
|
|
|
|
description = tk.Text(
|
|
self.frame,
|
|
height=4,
|
|
width=60,
|
|
wrap="word",
|
|
bg=config.field_bg,
|
|
fg=config.fg,
|
|
insertbackground=config.fg,
|
|
)
|
|
self._fields = _FormFields(
|
|
reason=self._field("What is wrong", config),
|
|
onset=self._field("When did it start", config),
|
|
severity=self._field("Severity 1-10", config),
|
|
description=description,
|
|
)
|
|
description.pack(pady=6)
|
|
|
|
self._complaint = tk.Label(
|
|
self.frame,
|
|
text="",
|
|
font=(config.font_family, 14),
|
|
fg=config.danger,
|
|
bg=config.bg,
|
|
)
|
|
self._complaint.pack(pady=4)
|
|
|
|
self._submit = tk.Button(
|
|
self.frame,
|
|
text="Submit",
|
|
font=(config.font_family, 16, "bold"),
|
|
bg=config.warning,
|
|
fg=config.on_fill,
|
|
command=self.submit,
|
|
)
|
|
self._submit.pack(pady=6)
|
|
|
|
tk.Button(
|
|
self.frame,
|
|
text="Back to the alarm",
|
|
font=(config.font_family, 14),
|
|
bg=config.field_bg,
|
|
fg=config.fg,
|
|
command=callbacks.on_cancel,
|
|
).pack(pady=4)
|
|
|
|
def _field(self, label: str, config: LockConfig) -> tk.Entry:
|
|
"""Add a labelled single-line field and return its entry."""
|
|
tk.Label(
|
|
self.frame,
|
|
text=label,
|
|
font=(config.font_family, 13),
|
|
fg=config.muted,
|
|
bg=config.bg,
|
|
).pack()
|
|
entry = tk.Entry(self.frame, width=40)
|
|
entry.pack(pady=2)
|
|
return entry
|
|
|
|
def _draft(self) -> EscapeDraft:
|
|
"""Read the form into a draft."""
|
|
try:
|
|
severity = int(self._fields.severity.get().strip() or "0")
|
|
except ValueError:
|
|
severity = 0
|
|
return EscapeDraft(
|
|
reason=self._fields.reason.get(),
|
|
onset=self._fields.onset.get(),
|
|
severity=severity,
|
|
description=self._fields.description.get("1.0", "end-1c"),
|
|
)
|
|
|
|
def submit(self) -> None:
|
|
"""Validate, then start the escalating wait."""
|
|
draft = self._draft()
|
|
complaint = self._tracker.validate(draft)
|
|
if complaint is not None:
|
|
self._complaint.configure(text=complaint)
|
|
return
|
|
if self._tracker.is_budget_exhausted():
|
|
# Re-checked at submit time: the budget could have been spent by
|
|
# another surface, or the day could have rolled over mid-form.
|
|
self._complaint.configure(text="No budget left. Solve the challenge.")
|
|
return
|
|
self._tracker.record(draft)
|
|
self._remaining = self._tracker.compute_lockout_seconds()
|
|
self._submit.configure(state="disabled")
|
|
self._tick()
|
|
|
|
def _tick(self) -> None:
|
|
"""Count the wait down, then grant."""
|
|
if self._remaining <= 0:
|
|
_logger.warning(
|
|
"alarm escape hatch used -- %s", self._tracker.budget_summary()
|
|
)
|
|
self._callbacks.on_granted()
|
|
return
|
|
self._complaint.configure(text=f"Wait {self._remaining}s...")
|
|
self._remaining -= 1
|
|
self.frame.after(1000, self._tick)
|
|
|
|
|
|
class EscapeHatch:
|
|
"""The alarm's way out, from the hidden button to putting the screen back.
|
|
|
|
The alarm delegates the whole flow here so that opening and closing the
|
|
form stay symmetric: every ``open`` that hides a challenge and builds a
|
|
dialog has a ``close`` that destroys the dialog and packs the challenge
|
|
back. Splitting those halves across two objects is what previously left
|
|
"Back to the alarm" showing a blank screen.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
tracker: EscapeTracker,
|
|
views: _ViewGroup,
|
|
on_granted: Callable[[], None],
|
|
) -> None:
|
|
"""Bind the hatch to the surfaces it swaps and the alarm it releases."""
|
|
self._tracker = tracker
|
|
self._views = views
|
|
self._on_granted = on_granted
|
|
self._dialogs: list[EscapeDialog] = []
|
|
self.is_open = False
|
|
|
|
def maybe_offer(self, elapsed_seconds: float) -> None:
|
|
"""Reveal the button on every monitor once it is earned, and only then."""
|
|
if self.is_open:
|
|
return
|
|
if not is_offerable(self._tracker, elapsed_seconds):
|
|
return
|
|
for button in self._views.escape_buttons:
|
|
with contextlib.suppress(tk.TclError):
|
|
button.pack(pady=8)
|
|
|
|
def open(self) -> None:
|
|
"""Swap every surface over to the justification form."""
|
|
self.is_open = True
|
|
callbacks = EscapeCallbacks(
|
|
on_granted=self._on_granted,
|
|
on_cancel=self.close,
|
|
)
|
|
for view in self._views.surfaces:
|
|
with contextlib.suppress(tk.TclError):
|
|
view.challenge_frame.pack_forget()
|
|
dialog = EscapeDialog(
|
|
view.container,
|
|
self._views.config,
|
|
self._tracker,
|
|
callbacks,
|
|
)
|
|
dialog.frame.pack()
|
|
self._dialogs.append(dialog)
|
|
|
|
def close(self) -> None:
|
|
"""Back out of the form and put the challenge back on every monitor."""
|
|
for dialog in self._dialogs:
|
|
with contextlib.suppress(tk.TclError):
|
|
dialog.frame.destroy()
|
|
self._dialogs.clear()
|
|
for view in self._views.surfaces:
|
|
with contextlib.suppress(tk.TclError):
|
|
view.challenge_frame.pack()
|
|
self.is_open = False
|