Files
lintunes/lintunes/cassette/host.py
T
travandClaude Opus 5.5 0dad4cc95e v0.26.0: Cassette footing — Syncthing client, host machine, Sync Settings
First step of friend library sharing (Cassette):

- cassette/syncthing_api.py: a typed wrapper over the *local* Syncthing's
  REST API. Key and address come from Syncthing's config.xml (config.json
  may override); every failure is a SyncthingError naming the step, what
  Syncthing said, and whether it's not running / a bad key / a refusal.
- cassette/state.py: friends, invites and share selections, kept in the
  machine-local ~/.local/share/lintunes/cassette, never the synced data dir.
- cassette/host.py: one machine runs Cassette. config.json flags it,
  preferences.json names it, and the other machine greys the menu group
  out and says where friend sharing lives.
- Connections ▸ Sync Settings…: VLC-style list + page, editing a copy
  that only OK writes. General shows Syncthing's status with a next step
  for every failure, the display name and an avatar picker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 21:01:17 -07:00

60 lines
1.9 KiB
Python

"""Which of my machines runs Cassette.
trav runs LinTunes on two machines against one synced library. Friend folders,
outboxes and the delivery loop must run in exactly one place, or both machines
would import the same delivery. So one machine is the **host**:
* ``config.json`` ``cassette_host: true`` on that machine (per machine, never
synced), and
* ``preferences.json`` ``cassette_host: {machine_id, hostname}`` (synced), so
the *other* machine can say "Cassette runs on <hostname>" instead of just
greying the menu out.
The first Cassette action on a library with no host asks to claim it.
"""
from __future__ import annotations
import socket
from lintunes.config import load_config, save_config
from lintunes.storage.play_journal import machine_id
CONFIG_KEY = "cassette_host"
PREFS_KEY = "cassette_host"
HERE = "here"
ELSEWHERE = "elsewhere"
UNCLAIMED = "unclaimed"
def hostname() -> str:
try:
return socket.gethostname() or "this computer"
except OSError:
return "this computer"
def host_status(prefs) -> tuple[str, str]:
"""(HERE | ELSEWHERE | UNCLAIMED, the host's name)."""
record = prefs.get(PREFS_KEY) or {}
flagged = bool(load_config().get(CONFIG_KEY))
if record.get("machine_id"):
if record["machine_id"] == machine_id():
return HERE, record.get("hostname") or hostname()
return ELSEWHERE, record.get("hostname") or "another computer"
# No synced record: our own flag still counts (the record is rewritten
# the next time claim() runs), anything else is up for grabs.
return (HERE, hostname()) if flagged else (UNCLAIMED, "")
def is_host(prefs) -> bool:
return host_status(prefs)[0] == HERE
def claim(prefs):
"""Make this machine the Cassette host."""
config = load_config()
config[CONFIG_KEY] = True
save_config(config)
prefs.set(PREFS_KEY, {"machine_id": machine_id(), "hostname": hostname()})