mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-22 09:42:20 +01:00
Pulls the day's delivered dishes and their real per-portion macros out of
panel.kuchniavikinga.pl, so a catering meal is one keypress instead of five
retyped numbers.
A delivered meal is not an eaten meal, so nothing is ever logged unattended:
dishes land in the *curated* bank, and entries are written only after an
explicit confirmation. Auto-logging would let the gate satisfy its own
checkpoint from a delivery note, and would record what the courier dropped off
rather than what was eaten.
Three surfaces:
- `diet-guard kuchnia [--log] [--yes]`
- the lock screen's "Today's delivery" button, which prefills the meal form
dish by dish (the user still clicks Log & Continue)
- a warm-up after each logged meal, on the existing background thread
All event-driven; no new systemd timer. The two automatic paths go through
`refresh_delivery_once`, guarded to one fetch per day.
The panel is an undocumented white-labelled Dietly API; the endpoint map was
recovered from its JS bundles and confirmed against live responses. Notes,
including the traps, are in docs/kuchnia-wikinga.md.
Load-bearing details:
* Macros are per portion and `weight` is that portion in grams -- proven, not
assumed: 4P+4C+9F reproduces the stated calories within ~1% on every meal of
the captured day. `_energy_is_consistent` re-runs that check per dish and
drops any that fails; a per-100g mix-up is invisible in a payload and would
silently skew every logged meal by a factor of several.
* `bank_dishes` compares nutritional fields before calling `add_manual_entry`,
which restamps `t` unconditionally. Without the compare, every refresh
republishes the whole curated bank to every peer. Tests assert on call count.
* Duplicate log suppression reads today's synced log, not a local marker:
`log_meal` mints a fresh uuid, so a re-import after marker loss would merge
as duplicates on every peer.
* The gate hook is in `on_focus_ready`, not `_cli_gate._should_lock`:
`gate_is_due()` never reads the food bank, so a refresh there could not
change the lock decision while adding a third-party round trip to the ~105ms
`gate --check` fast path.
* `requests` is lazy-imported so the gate's not-due tick does not pay for it.
* Credentials and the cached session cookie live in ~/.config/diet_guard at
mode 600, outside the synced tree. The credentials file is written by hand,
never by the package -- the same contract as sync_token.
`_GateDashboard` is merged into `_GateMealFlow`, and both "pull it in from
elsewhere" flows share one `_PullFlows` mixin: pylint's max-parents counts
every MRO entry, so the gate's controller chain had no room for a new class.
Also adds a side-by-side row for the two pull buttons -- stacking them cost
42px and pushed the tab past the viewport at 1366x768.
989 tests, 100% branch coverage, pre-commit clean.
87 lines
3.5 KiB
Python
87 lines
3.5 KiB
Python
"""Assign catering dishes to meal slots, in the provider's own order.
|
|
|
|
The panel returns each dish with a ``mealPriority`` (1..N: Śniadanie, II
|
|
śniadanie, Obiad, Podwieczorek, Kolacja) -- the order the catering intends them
|
|
to be eaten in. That beats inferring an order from the dish names, and it
|
|
beats spreading them arithmetically, so this module sorts by priority and then
|
|
maps position onto the user's configured slots.
|
|
|
|
Counts rarely match: a 5-meal plan against the default 4 slots means two dishes
|
|
share the first slot. The mapping is ``i * S // N``, which is monotonic
|
|
non-decreasing, keeps the first dish on the first slot and the last on the
|
|
last, and doubles up the earliest slots rather than dropping anything.
|
|
|
|
Integer ``//`` only, never ``round()``. That is repo convention for slot
|
|
arithmetic (``docs/meal-schedule.md``): Python's banker's rounding and Dart's
|
|
half-away-from-zero disagree on ``.5``, and a slot one device offers while the
|
|
other does not is a checkpoint that can never be satisfied. The parity risk is
|
|
theoretical *here* -- the phone has no importer and so no mirror of this code
|
|
(see the plan's §8) -- but the rule is cheap to keep and expensive to
|
|
rediscover.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, NamedTuple
|
|
|
|
from diet_guard._meal_schedule_store import current_schedule
|
|
from diet_guard._slots import day_slots
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
from diet_guard._kuchnia_parse import Dish
|
|
|
|
|
|
class SlottedDish(NamedTuple):
|
|
"""A dish paired with the meal-slot hour it was assigned to."""
|
|
|
|
dish: Dish
|
|
slot: int
|
|
|
|
|
|
def assign_slots(dishes: Sequence[Dish], slots: Sequence[int]) -> list[SlottedDish]:
|
|
"""Pair each dish with a slot hour, following the provider's meal order.
|
|
|
|
Args:
|
|
dishes: The day's dishes, in any order (sorted here by ``priority``).
|
|
slots: The configured slot hours, ascending, e.g. ``(8, 12, 16, 20)``.
|
|
|
|
Returns:
|
|
One :class:`SlottedDish` per dish, ordered by the provider's priority.
|
|
Empty when either input is empty -- a day with no delivery and a
|
|
schedule with no slots are both "nothing to assign", not errors.
|
|
"""
|
|
if not dishes or not slots:
|
|
return []
|
|
# Ties (two dishes sharing a priority) fall back to name so the result is
|
|
# deterministic; a bank import that reshuffles between runs would look like
|
|
# a change and re-stamp every record.
|
|
ordered = sorted(dishes, key=lambda dish: (dish.priority, dish.name))
|
|
count = len(ordered)
|
|
span = len(slots)
|
|
return [
|
|
SlottedDish(dish=dish, slot=slots[index * span // count])
|
|
for index, dish in enumerate(ordered)
|
|
]
|
|
|
|
|
|
def dishes_in_slot_order(dishes: tuple[Dish, ...]) -> tuple[Dish, ...]:
|
|
"""Return ``dishes`` ordered by the provider's own meal priority.
|
|
|
|
So the dish offered for the slot being filled is the one the catering
|
|
actually intends for it, rather than whatever order the payload arrived in.
|
|
"""
|
|
slotted = assign_slots(dishes, day_slots(current_schedule()))
|
|
return tuple(item.dish for item in slotted)
|
|
|
|
|
|
def dish_field_values(dish: Dish) -> tuple[str, tuple[str, ...]]:
|
|
"""Return ``(portion_grams, (kcal, protein, carbs, fat))`` as form strings.
|
|
|
|
Formatted here rather than in the widget code so the numeric formatting is
|
|
covered by a test that needs no display.
|
|
"""
|
|
macros = (dish.kcal, dish.protein_g, dish.carbs_g, dish.fat_g)
|
|
return f"{dish.grams:g}", tuple(f"{value:g}" for value in macros)
|