utils/gatelock/gatelock/_textmirror.py
Krzysztof kuhy Rudnicki 9f271b0d37 Fix the cap hook in CI, and the re-exports its --all-files run exposed
The hook entry was an absolute /home/kuhy/utils path, copied from a config
written for ~/.claude where it only ever ran locally. On an Actions runner
that is exit code 127 — "No such file or directory" — so gatelock-precommit
and crdt-sync-precommit failed on every commit of this refactor while the
hook silently checked nothing. Now repo-relative, matching the sibling hooks
(`entry: scripts/check_subproject_scope.sh`): pre-commit always runs from the
git root, so it resolves the same in both places.

Fixing that made the CI configs runnable locally, and `--all-files` then found
what per-file runs had missed — ruff pruning imports that exist *for* their
re-export. `import crdt_sync` was broken three separate ways (FileSyncStateStore,
encode_key, FileCredentialStore). Each affected module now states an explicit
__all__ so the autofixer cannot take them again, and the two files that pushed
past the cap once __all__ was added are split further:

  _firebase.py 260 -> 247 + _fberrors 25
  _sync.py     257 -> 188 + _syncargs 91

Test patch targets followed the code they patch: gatelock._outputs.shutil ->
_xrandr.shutil, gatelock._arbiter.Path -> _claims.Path.

gatelock and crdt-sync both pass `pre-commit run --all-files` in full;
389 and 259 tests at 100% branch coverage; the root gate exits 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
2026-08-22 15:52:59 +02:00

76 lines
2.6 KiB
Python

"""Keeping several Text widgets showing the same string.
Split from :mod:`gatelock._surfaces` for the 250-line cap. A lock renders one
surface per output, so a multi-line field exists once per monitor and all the
copies have to agree; ``tk.StringVar`` does this for Entry and Spinbox but not
for ``tk.Text``, which has no textvariable.
Re-exported from :mod:`gatelock._surfaces` and from :mod:`gatelock`, so
existing imports of either name keep working.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Sequence
import tkinter as tk
# Tk text indices: the first character, and the last one before the newline
# Text always appends.
_TEXT_START = "1.0"
_TEXT_END = "end-1c"
class TextMirror:
"""Keeps N ``tk.Text`` widgets showing the same value.
``tk.Text`` has no ``textvariable``, so it is the one widget that cannot
ride the shared-``StringVar`` mechanism the rest of the mirroring uses.
"""
def __init__(self, widgets: Sequence[tk.Text], var: tk.StringVar) -> None:
"""Bind ``widgets`` to each other through ``var``."""
self._widgets = list(widgets)
self._var = var
self._syncing = False
for widget in self._widgets:
widget.bind("<KeyRelease>", self._on_key_release, add="+")
self._var.trace_add("write", self._on_var_write)
def _on_key_release(self, event: tk.Event[tk.Text]) -> None:
"""Push the edited widget's content into the shared variable."""
if self._syncing:
return
self._syncing = True
try:
self._var.set(event.widget.get(_TEXT_START, _TEXT_END))
finally:
self._syncing = False
self._fan_out(source=event.widget)
def _on_var_write(self, *_args: str) -> None:
"""Push the shared variable's value into every widget."""
if self._syncing:
return
self._fan_out(source=None)
def _fan_out(self, *, source: tk.Text | None) -> None:
"""Write the variable's value into every widget but ``source``."""
value = self._var.get()
self._syncing = True
try:
for widget in self._widgets:
if widget is source or widget.get(_TEXT_START, _TEXT_END) == value:
continue
widget.delete(_TEXT_START, "end")
widget.insert(_TEXT_START, value)
finally:
self._syncing = False
def mirror_text_widgets(widgets: Sequence[tk.Text], var: tk.StringVar) -> TextMirror:
"""Keep several ``tk.Text`` mirrors in sync through one variable."""
return TextMirror(widgets, var)