mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-10 18:07: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.
131 lines
4.5 KiB
Python
131 lines
4.5 KiB
Python
"""Credentials and the cached session cookie for the catering import.
|
|
|
|
Sole reader of :data:`~diet_guard._constants.KUCHNIA_CREDENTIALS_FILE` and
|
|
:data:`~diet_guard._constants.KUCHNIA_SESSION_FILE`, so the test-suite redirect
|
|
in ``conftest._isolate_state`` has exactly one place to patch.
|
|
|
|
Two files, both mode 600, both under ``~/.config/diet_guard/`` -- deliberately
|
|
*outside* ``DATA_DIR``, which syncs. A password and a live session cookie must
|
|
not travel to another device.
|
|
|
|
The credentials file is **written by the user, never by this package**, which
|
|
is the same contract as ``sync_token``: nothing here can leak a password into a
|
|
backup or a log because nothing here has ever held one on disk.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
|
|
from diet_guard._constants import (
|
|
KUCHNIA_CREDENTIALS_FILE,
|
|
KUCHNIA_LAST_IMPORT_FILE,
|
|
KUCHNIA_SESSION_FILE,
|
|
)
|
|
from diet_guard._kuchnia_errors import KuchniaError
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
#: The only cookie the panel issues on login. It sets no ``XSRF-TOKEN``, so the
|
|
#: CSRF echo its own JavaScript performs is a no-op for this account -- the
|
|
#: session cookie alone authenticates.
|
|
SESSION_COOKIE = "SESSION"
|
|
|
|
_EXPECTED_LINES = 2
|
|
|
|
_SETUP_HINT = (
|
|
"create it with:\n"
|
|
" install -m 600 /dev/null {path}\n"
|
|
" printf '%s\\n%s\\n' 'you@example.com' 'your-password' > {path}"
|
|
)
|
|
|
|
|
|
def read_credentials() -> tuple[str, str]:
|
|
"""Return the panel ``(username, password)``.
|
|
|
|
Returns:
|
|
The credentials read from disk.
|
|
|
|
Raises:
|
|
KuchniaError: When the file is missing, empty, or not two lines. The
|
|
message carries the setup command, mirroring ``_read_token``.
|
|
"""
|
|
path = KUCHNIA_CREDENTIALS_FILE
|
|
if not path.exists():
|
|
msg = f"no catering credentials at {path} -- " + _SETUP_HINT.format(path=path)
|
|
raise KuchniaError(msg)
|
|
lines = [
|
|
line.strip()
|
|
for line in path.read_text(encoding="utf-8").splitlines()
|
|
if line.strip()
|
|
]
|
|
if len(lines) < _EXPECTED_LINES:
|
|
msg = f"{path} needs two lines (e-mail, then password); found {len(lines)}"
|
|
raise KuchniaError(msg)
|
|
return lines[0], lines[1]
|
|
|
|
|
|
def load_session_cookie() -> str | None:
|
|
"""Return the cached session cookie, or None when there is no usable cache.
|
|
|
|
Never raises: a missing, unreadable or corrupt cache simply means "log in
|
|
again", which is always a valid next step.
|
|
"""
|
|
if not KUCHNIA_SESSION_FILE.exists():
|
|
return None
|
|
try:
|
|
cached = json.loads(KUCHNIA_SESSION_FILE.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError):
|
|
_logger.warning("Catering session cache %s is unreadable", KUCHNIA_SESSION_FILE)
|
|
return None
|
|
value = cached.get(SESSION_COOKIE) if isinstance(cached, dict) else None
|
|
return value if isinstance(value, str) and value else None
|
|
|
|
|
|
def save_session_cookie(value: str) -> None:
|
|
"""Cache ``value``, created mode 600 before it holds anything.
|
|
|
|
``touch(mode=0o600)`` first, then write, then atomically replace: writing
|
|
and chmod'ing afterwards would leave a live session cookie readable by
|
|
every local user for the duration of the write.
|
|
"""
|
|
KUCHNIA_SESSION_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
temp = KUCHNIA_SESSION_FILE.with_suffix(".json.tmp")
|
|
temp.touch(mode=0o600)
|
|
temp.write_text(json.dumps({SESSION_COOKIE: value}), encoding="utf-8")
|
|
temp.replace(KUCHNIA_SESSION_FILE)
|
|
|
|
|
|
def clear_session_cookie() -> None:
|
|
"""Drop the cached cookie so the next refresh logs in afresh."""
|
|
KUCHNIA_SESSION_FILE.unlink(missing_ok=True)
|
|
|
|
|
|
def last_import_day() -> str:
|
|
"""Return the ISO date whose delivery was last fetched, or "" if none.
|
|
|
|
Never raises: an unreadable marker just means "fetch again", which is
|
|
always safe.
|
|
"""
|
|
if not KUCHNIA_LAST_IMPORT_FILE.exists():
|
|
return ""
|
|
try:
|
|
return KUCHNIA_LAST_IMPORT_FILE.read_text(encoding="utf-8").strip()
|
|
except OSError:
|
|
return ""
|
|
|
|
|
|
def record_import_day(day: str) -> None:
|
|
"""Remember that ``day``'s delivery has been fetched.
|
|
|
|
Best-effort: this is a rate limit, not state anyone depends on. A failure
|
|
to write it costs one extra fetch, which is strictly better than failing an
|
|
import that already succeeded.
|
|
"""
|
|
try:
|
|
KUCHNIA_LAST_IMPORT_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
KUCHNIA_LAST_IMPORT_FILE.write_text(day, encoding="utf-8")
|
|
except OSError:
|
|
_logger.warning("Could not record the catering import day")
|