diet-guard/diet_guard/_cli_sync.py
Krzysztof kuhy Rudnicki 59d1c29dd2 Migrate sync to shared crdt_sync library (Python + Dart)
Replace the bespoke GitHub-Gist sync with the shared crdt-sync /
crdt_sync_dart libraries so diet-guard uses the same CRDT transport as
the other apps.

- Python: delete _sync_github.py (+ its test); rewrite _sync.py,
  _cli_sync.py, _sync_merge.py against crdt-sync. Add the crdt-sync git
  dependency (pyproject + pylint deps) and S107 to the test ignore list.
- Dart: rewrite app sync_service.dart / sync_merge.dart against
  crdt_sync_dart (new git dep + crypto); update log_meal and settings
  screens and their tests.

Verification: pytest 347 passed at 100% coverage; flutter analyze clean;
flutter test 306 passed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RRCdR4D8SaVrN2xraMQxM
2026-07-10 23:49:43 +02:00

57 lines
1.8 KiB
Python

"""CLI handler for the ``sync`` subcommand.
Split out from :mod:`diet_guard._cli` to keep that module under the repo's
500-line cap (see ``CLAUDE.md``'s "feat: split oversized modules" history) --
the same reason the gate window logic lives across ``_gatelock*.py`` instead
of one file.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from crdt_sync import GitHubSyncError
from diet_guard._sync import SyncError, run_sync
if TYPE_CHECKING:
import argparse
from collections.abc import Callable
def register_sync_subparser(sub: argparse._SubParsersAction) -> None:
"""Register the ``sync`` subcommand on ``sub``."""
sub.add_parser(
"sync",
help="Pull/merge/push the log with other devices via GitHub.",
)
def cmd_sync(emit: Callable[[str], None]) -> int:
"""Run one sync tick and report what happened via ``emit``.
Errors are caught here rather than left to propagate: a sync failure
(missing PAT, network error, repo misconfigured) is routine enough on a
timer-driven command that the CLI should report it and exit non-zero,
not crash with a traceback.
Args:
emit: A one-line output sink (``_cli._emit``, kept private to that
module -- passed in rather than imported, so this module has no
reach-in dependency on ``_cli``'s internals).
Returns:
0 on a successful sync, 1 if it could not run or failed partway.
"""
try:
merged = run_sync()
except SyncError as exc:
emit(f"sync not configured: {exc}")
return 1
except GitHubSyncError as exc:
emit(f"sync failed: {exc}")
return 1
total_entries = sum(len(entries) for entries in merged.values())
emit(f"synced: {total_entries} entries across {len(merged)} day(s).")
return 0