mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 16:53:57 +01:00
Third app onto freedays (~/utils/freedays), the shared pool of pre-declared days when every gate app stands down. due_slots() returns nothing on a free day, so no slot is ever due, the lock never opens and nothing is said about the missing meals. Read from one local file, never the network, so this cannot hang the systemd-fired gate -- and an unreadable pool reads as "not free", which means the normal rules apply rather than the gate quietly switching itself off. Tested explicitly: fail closed, not open. The conftest redirect is load-bearing. The pool lives under ~/.local/share, outside every diet_guard path the suite already isolates, so without it a real free day on this machine would fail every "the gate is due" test here for a reason that looks nothing like the cause. gatelock moves v0.7.2 -> v0.7.3, and the pylint hook's own pins (which had drifted further, to gatelock v0.7.1 and crdt-sync v0.6.0) come with it. Not cosmetic: pip refuses two different direct URLs for one distribution, so any stale pin alongside freedays -- which requires v0.7.3 -- is an unresolvable environment rather than a warning. requirements.txt also picks up pylint 4.0.8, pytest-randomly 5.0.0 and ruff 0.16.6, which the freshness gate flagged once this file was touched. 1043 tests pass at 100% coverage across the pytest-randomly major bump. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
"""Decision logic for the diet_guard slot-based log-to-unlock gate.
|
|
|
|
This module is GUI-free and side-effect-free so the lock/no-lock decision can
|
|
be verified headlessly: the fullscreen window in ``_gatelock.py`` is only a
|
|
thin shell around :func:`gate_is_due` and :func:`due_slots`. It composes the
|
|
pure slot arithmetic in :mod:`diet_guard._slots` with the logged-slot
|
|
state in :mod:`diet_guard._state`; ``now`` is injectable so the
|
|
time-of-day rules stay deterministically testable.
|
|
|
|
The gate fires when any *elapsed* meal slot for today carries no logged meal.
|
|
Coming home late therefore surfaces several unlogged slots at once -- a single
|
|
lock that backfills the whole day before the PC is usable -- while a normal day
|
|
prompts one slot at a time, with no separate weekday code path.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
import freedays
|
|
|
|
from diet_guard._meal_schedule_store import current_schedule
|
|
from diet_guard._slots import missing_slots, slot_label
|
|
from diet_guard._state import (
|
|
now_local,
|
|
)
|
|
from diet_guard._state_today import (
|
|
logged_slots_today,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import datetime
|
|
|
|
|
|
def due_slots(now: datetime | None = None) -> tuple[int, ...]:
|
|
"""Return today's elapsed-but-unlogged meal slots, ascending.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
The slot hours that still need a meal logged (empty == nothing due).
|
|
|
|
Empty on a shared free day, whatever the schedule says. The pool is read
|
|
from a single local file and never the network, so this cannot hang the
|
|
systemd-fired gate, and an unreadable pool reads as "not free" -- the
|
|
normal rules apply rather than the gate switching itself off.
|
|
"""
|
|
reference = now if now is not None else now_local()
|
|
if freedays.is_free_day(reference.date()):
|
|
return ()
|
|
return missing_slots(reference, logged_slots_today(), current_schedule())
|
|
|
|
|
|
def gate_is_due(now: datetime | None = None) -> bool:
|
|
"""Return True if the screen should lock until the missing slots are filled.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
True if at least one elapsed slot today is unlogged, else False.
|
|
"""
|
|
return bool(due_slots(now))
|
|
|
|
|
|
def gate_message(now: datetime | None = None) -> str:
|
|
"""Return the lock-screen reason line listing the slots to backfill.
|
|
|
|
Args:
|
|
now: Reference time (defaults to the current local time); injectable.
|
|
|
|
Returns:
|
|
A short human-readable explanation of which meals are missing.
|
|
"""
|
|
missing = due_slots(now)
|
|
if not missing:
|
|
return "All meals are logged. You're up to date."
|
|
labels = ", ".join(slot_label(slot) for slot in missing)
|
|
if len(missing) == 1:
|
|
return f"Log your {labels} meal to unlock."
|
|
return f"Log your meals for {labels} to unlock."
|