mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 22:47:14 +01:00
The three Python packages pinned ruff twice at different versions: requirements.txt at 0.16.7, .pre-commit-config.yaml at v0.15.2. Only the older one ever ran, so `select = ["ALL"]` was enforced two minor versions behind. Measured before aligning: v0.15.2 reports 0 findings, every 0.16.x reports CPY001 on each file -- 70 crdt-sync, 23 freedays, 71 gatelock, and no other rule. Resolved by adding the notice rather than ignoring the rule, so the gate keeps its teeth. Two gatelock modules sat at exactly the 250-line cap, so a one-line header tipped them to 251. Both are split along a real seam rather than shaved: _widget_fills.py colour arithmetic over LockConfig, no tkinter import _surfaces_focus.py selects among existing surfaces, never mutates them The focus split keeps _surfaces.py's documented containment (it stays the only module that creates, moves or destroys windows), and the static guard in test_recovery_invariants.py globs _surfaces*.py, so the new sibling is covered with no list to extend. widgets.py re-exports the fills helpers, which keeps gatelock.widgets the single public surface its tests import. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019kMvSMtsVMB3d2uoZ56pbp
72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Local JSON persistence for a ``Log``.
|
|
|
|
Mirrors ``crdt_sync_dart``'s ``logToJson``/``FileLogPersistence`` serialization
|
|
so a log written by the Dart side parses here and vice versa. Deliberately
|
|
*non-reactive*: the change-stream/reactive store is a Dart/Flutter-only
|
|
convenience (it exists to drive a live UI). On the Python side, where consumers
|
|
are headless sync ticks, this is plain load and (atomic) save.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from typing import TYPE_CHECKING
|
|
|
|
from crdt_sync._record import Record
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
from crdt_sync._log import Log
|
|
|
|
|
|
def dump_log(log: Log) -> str:
|
|
"""Serialize ``log`` to canonical JSON text (id -> record dict)."""
|
|
return json.dumps({rid: record.to_dict() for rid, record in log.items()})
|
|
|
|
|
|
def load_log(text: str) -> Log:
|
|
"""Parse text produced by :func:`dump_log` back into a ``Log``.
|
|
|
|
Raises ``ValueError`` (bad JSON), ``KeyError`` or ``TypeError`` (valid
|
|
JSON, wrong shape) on a corrupt payload -- callers reading from disk go
|
|
through :func:`read_log`, which turns those into an empty log.
|
|
"""
|
|
raw = json.loads(text)
|
|
if not isinstance(raw, dict):
|
|
msg = f"expected a JSON object mapping id -> record, got {type(raw).__name__}"
|
|
raise TypeError(msg)
|
|
return {rid: Record.from_dict(value) for rid, value in raw.items()}
|
|
|
|
|
|
def read_log(path: Path) -> Log:
|
|
"""Read a ``Log`` from ``path``; empty on a missing or unparsable file.
|
|
|
|
A truncated or corrupt file can never raise here: it is treated as "no log
|
|
yet", matching the Dart store's defensive load so a half-written file can't
|
|
brick a consumer.
|
|
"""
|
|
try:
|
|
text = path.read_text(encoding="utf-8")
|
|
except FileNotFoundError:
|
|
return {}
|
|
try:
|
|
return load_log(text)
|
|
except ValueError, KeyError, TypeError:
|
|
return {}
|
|
|
|
|
|
def write_log(path: Path, log: Log) -> None:
|
|
"""Write ``log`` to ``path`` atomically (temp file then rename).
|
|
|
|
Writes to a per-process temp file and ``replace``s it over the real path,
|
|
so a concurrent reader never observes a half-written file. Mirrors the
|
|
Dart ``FileLogPersistence`` write scheme.
|
|
"""
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_name(f"{path.name}.{os.getpid()}.tmp")
|
|
tmp.write_text(dump_log(log), encoding="utf-8")
|
|
tmp.replace(path)
|