mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-17 22:05:17 +01:00
main was red on file length, the noqa ban, codespell, test naming and dart coverage; this change touches every one of those jobs. - sickdays: set_enabled is keyword-only; the panel test splits in two with shared probes; the sync wire format moves to DOCS-sync.md. - earned_time: _focus types the project as a Protocol, no noqa; the gate-maturity section moves to DOCS-maturity.md; codespell knows the `checkin` event name. - focus_project: real fixes for every noqa (local-day clocks, typed fakes, logged broad catches, split helpers). The public API that focus-guard calls keeps its signatures; their PLR0913 exemption lives in pyproject with the reason, not in noqa. - crdt-sync: pylint gets nacl/argon2, docstrings, and allows the public E2eLayout.of; _blobs moves to _e2e_blobs.py. - crdt_sync_dart: Argon2Params and the header errors move to e2e_params.dart (re-exported); tests split and reach 100% lines. - sql_drills: test helpers get a leading underscore, as in earned_time. Todo: fda71ed1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Adapter for accepted LeetCode submissions (leetcode-guard's ledger).
|
|
|
|
Read-only. Each accepted submission (``ac:<id>`` credit row, HMAC verified)
|
|
is a heartbeat at ``detail.submitted_at``, so it covers the five minutes
|
|
before it. The window is deliberately NOT longer: the time spent reading and
|
|
thinking on the site is the browser adapter's to count, and a submission
|
|
alone proves only that work happened, not for how long. Applies only to a
|
|
project whose ``site_patterns`` name leetcode; any other project is not
|
|
LeetCode practice. No readable signing key: contributes nothing (logged).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
import earned_time
|
|
|
|
from focus_project import ledger
|
|
from focus_project.sources import Interval, heartbeat_interval
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import date
|
|
|
|
from focus_project._model import Project
|
|
|
|
LEDGER_ENV = "FOCUS_PROJECT_LEETCODE_LEDGER"
|
|
_LOG = logging.getLogger(__name__)
|
|
|
|
|
|
def ledger_file() -> Path:
|
|
"""leetcode-guard's ledger, or the test override."""
|
|
override = os.environ.get(LEDGER_ENV)
|
|
if override:
|
|
return Path(override)
|
|
return Path.home() / ".local" / "share" / "leetcode_guard" / "ledger.json"
|
|
|
|
|
|
def _submitted_at(row: object, key: bytes) -> float | None:
|
|
"""The submission time of a verified accepted-submission row, else None."""
|
|
if not (
|
|
isinstance(row, dict)
|
|
and row.get("kind") == "credit"
|
|
and str(row.get("entry_id", "")).startswith("ac:")
|
|
and earned_time.verified(row, key)
|
|
):
|
|
return None
|
|
detail = row.get("detail")
|
|
if not isinstance(detail, dict):
|
|
return None
|
|
try:
|
|
return float(detail["submitted_at"])
|
|
except KeyError, TypeError, ValueError:
|
|
return None
|
|
|
|
|
|
def submission_times(project: Project) -> list[float]:
|
|
"""Times of verified accepted submissions; [] for a non-leetcode project."""
|
|
if not any("leetcode" in pattern.lower() for pattern in project.site_patterns):
|
|
return []
|
|
key = ledger.read_key()
|
|
if key is None:
|
|
_LOG.warning("leetcode source: no signing key, contributes 0")
|
|
return []
|
|
stamps = (_submitted_at(row, key) for row in ledger.read_rows(ledger_file()))
|
|
return [ts for ts in stamps if ts is not None]
|
|
|
|
|
|
def intervals(day: date, project: Project) -> list[Interval]:
|
|
"""One heartbeat per accepted submission (the registry clips to the day)."""
|
|
del day # the adapter signature takes the day; the registry does the clipping
|
|
return [heartbeat_interval(ts) for ts in submission_times(project)]
|