Files
muxplex/muxplex/settings.py
T
Brian Krabach 395278818f feat: add remote_instances and device_name to settings
- Add 'remote_instances': [] to DEFAULT_SETTINGS for peer instance configs
- Add 'device_name': '' to DEFAULT_SETTINGS (empty for clean JSON serialization)
- Add import socket to settings.py
- In load_settings(), fill empty device_name with socket.gethostname() so the
  hostname is always current even if the machine is renamed
- Add 7 new tests covering defaults, hostname fallback, round-trips, and
  mutation isolation
- Update 2 existing tests that compared result == DEFAULT_SETTINGS to account
  for the hostname fill-in behavior

Co-authored-by: Amplifier <amplifier@amplified.dev>
2026-03-30 20:48:38 -07:00

71 lines
2.0 KiB
Python

"""
Server-side settings management for muxplex.
Settings are stored at ~/.config/muxplex/settings.json.
"""
import copy
import json
import socket
from pathlib import Path
SETTINGS_PATH = Path.home() / ".config" / "muxplex" / "settings.json"
DEFAULT_SETTINGS: dict = {
"default_session": None,
"sort_order": "manual",
"hidden_sessions": [],
"window_size_largest": False,
"auto_open_created": True,
"new_session_template": "tmux new-session -d -s {name}",
"remote_instances": [],
"device_name": "",
}
def load_settings() -> dict:
"""Load settings from disk, merging saved values over defaults.
Returns DEFAULT_SETTINGS if the file does not exist or contains corrupt JSON.
Unknown keys in the file are ignored.
"""
result = copy.deepcopy(DEFAULT_SETTINGS)
try:
text = SETTINGS_PATH.read_text()
data = json.loads(text)
for key in DEFAULT_SETTINGS:
if key in data:
result[key] = data[key]
except (FileNotFoundError, json.JSONDecodeError):
pass
if not result["device_name"]:
result["device_name"] = socket.gethostname()
return result
def save_settings(data: dict) -> None:
"""Save settings to disk, merging *data* with defaults first.
Creates parent directories as needed. Writes JSON with indent=2 and a
trailing newline.
"""
merged = copy.deepcopy(DEFAULT_SETTINGS)
for key in DEFAULT_SETTINGS:
if key in data:
merged[key] = data[key]
SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True)
SETTINGS_PATH.write_text(json.dumps(merged, indent=2) + "\n")
def patch_settings(patch: dict) -> dict:
"""Merge known keys from *patch* into the current settings, save, and return result.
Unknown keys in *patch* are silently ignored.
"""
current = load_settings()
for key in DEFAULT_SETTINGS:
if key in patch:
current[key] = patch[key]
save_settings(current)
return current