screen-locker/screen_locker/_wake_state.py
Krzysztof kuhy Rudnicki 70cf6f5425 Migrate to the shared gatelock backend
ScreenLocker now composes gatelock.GateRoot + gatelock.LockWindow for the
actual lock window instead of the inline WindowSetupMixin mechanics; the
verify/relaxed-day auxiliary windows (never the lock itself) stay as
plain Tk windows. The hand-copied _log_integrity.py is deleted in favor
of gatelock.log_integrity (the canonical, non-duplicated module). This
is the second of three migrations (diet_guard done, wake_alarm next).

Two deliberate behavior changes, both confirmed:
- dependencies = [] (pure stdlib) now includes gatelock, a documented
  departure from the prior zero-deps stance.
- production grab upgraded from single-attempt-then-local-fallback to
  diet_guard's retry-forever (robust to e.g. a fullscreen game holding
  the grab).

Net hardening as a side effect: run()/close() now go through gatelock's
signal-safe lifecycle, so SIGTERM/SIGINT restore VT switching on every
exit path -- previously only a clean close() did, leaving VT switching
disabled if the service was killed mid-lock.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XCdT46zV8hESDvbgYMGDLt
2026-06-21 20:11:16 +02:00

65 lines
1.8 KiB
Python

"""Read wake-alarm state to check if a workout skip was earned today.
This module reads the JSON state file written by the companion wake_alarm
service. It does not import wake_alarm directly — the two packages
communicate only through the shared state file on disk.
The state file path defaults to WAKE_STATE_FILE in _constants.py, which
points to the sibling wake_alarm package directory. Override it in tests
by patching ``screen_locker._wake_state.WAKE_STATE_FILE``.
"""
from __future__ import annotations
from datetime import datetime, timezone
import json
import logging
from gatelock.log_integrity import verify_entry_hmac
from screen_locker._constants import WAKE_STATE_FILE
_logger = logging.getLogger(__name__)
def _today_str() -> str:
"""Return today's date as YYYY-MM-DD in UTC."""
return datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
def load_wake_state() -> dict[str, object] | None:
"""Load and verify today's wake state.
Returns the state dict if it exists, is valid (HMAC OK), and is
for today. Returns None otherwise.
"""
if not WAKE_STATE_FILE.exists():
return None
try:
with WAKE_STATE_FILE.open() as f:
state = json.load(f)
except (OSError, json.JSONDecodeError):
_logger.warning("Cannot read wake state file")
return None
if not isinstance(state, dict):
return None
if state.get("date") != _today_str():
return None
if not verify_entry_hmac(state):
_logger.warning("Wake state HMAC verification failed")
return None
return state
def has_workout_skip_today() -> bool:
"""Check if the user earned a workout skip for today."""
state = load_wake_state()
if state is None:
return False
return bool(state.get("skip_workout"))