mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-07-04 15:03:15 +02:00
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py), split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date to keep cyclomatic complexity ≤10 - Generalise TCX lookup to any date in the ISO week (was today-only); all gap days Mon→today auto-filled on every startup and 08:30 timer firing - Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout minimum, capped at midnight (hour=24) - Add _shutdown_base.py: daily reset of shutdown config to a stored base so the bonus doesn't silently accumulate across days - Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks - Add --status mode (_status.py): non-locking CLI view showing per-day breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak, skip credits, and early-bird status - Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill pushes weekly count above the minimum - Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck, codespell, max-file-length); 508 tests at 100% branch coverage Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
"""Mixin: workout log persistence (read/write workout_log.json)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
import logging
|
|
|
|
from gatelock.log_integrity import compute_entry_hmac, verify_entry_hmac
|
|
|
|
from screen_locker._constants import SCHEDULED_SKIPS_FILE
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class LogMixin:
|
|
"""Handles reading and writing workout_log.json for the ScreenLocker."""
|
|
|
|
def has_logged_today(self) -> bool:
|
|
"""Check if workout has been logged today with valid HMAC."""
|
|
if not self.log_file.exists(): # type: ignore[attr-defined]
|
|
return False
|
|
try:
|
|
with self.log_file.open() as f: # type: ignore[attr-defined]
|
|
logs = json.load(f)
|
|
except (OSError, json.JSONDecodeError):
|
|
return False
|
|
else:
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
entry = logs.get(today)
|
|
if entry is None:
|
|
return False
|
|
if verify_entry_hmac(entry):
|
|
return entry.get("workout_data", {}).get("type") != "early_bird"
|
|
if compute_entry_hmac({"_probe": True}) is None and "hmac" not in entry:
|
|
_logger.info("HMAC key unavailable — accepting unsigned entry")
|
|
return entry.get("workout_data", {}).get("type") != "early_bird"
|
|
_logger.warning("HMAC verification failed for today's log entry")
|
|
return False
|
|
|
|
def _load_existing_logs(self) -> dict:
|
|
"""Load existing workout logs from file."""
|
|
if not self.log_file.exists(): # type: ignore[attr-defined]
|
|
return {}
|
|
try:
|
|
with self.log_file.open() as f: # type: ignore[attr-defined]
|
|
return json.load(f)
|
|
except (OSError, json.JSONDecodeError):
|
|
return {}
|
|
|
|
def _is_scheduled_skip_today(self) -> bool:
|
|
"""Return True if today's date is listed in the scheduled skips file."""
|
|
if not SCHEDULED_SKIPS_FILE.exists():
|
|
return False
|
|
try:
|
|
with SCHEDULED_SKIPS_FILE.open() as f:
|
|
skips = json.load(f)
|
|
except (OSError, json.JSONDecodeError):
|
|
return False
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
return today in skips
|
|
|
|
def save_workout_log(self) -> None:
|
|
"""Save workout data to log file with HMAC signature."""
|
|
logs = self._load_existing_logs()
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
entry: dict[str, object] = {
|
|
"timestamp": datetime.now(tz=timezone.utc).isoformat(),
|
|
"workout_data": self.workout_data, # type: ignore[attr-defined]
|
|
}
|
|
signature = compute_entry_hmac(entry)
|
|
if signature is not None:
|
|
entry["hmac"] = signature
|
|
else:
|
|
_logger.warning("HMAC key unavailable — saving unsigned entry")
|
|
logs[today] = entry
|
|
try:
|
|
with self.log_file.open("w") as f: # type: ignore[attr-defined]
|
|
json.dump(logs, f, indent=2)
|
|
except OSError as e:
|
|
_logger.warning("Could not save workout log: %s", e)
|