From 930d12fa4d6c902e3f237cc688829fd02cc50f08 Mon Sep 17 00:00:00 2001 From: Eric Dahl Date: Fri, 13 Nov 2026 11:01:32 -0600 Subject: [PATCH] Refactor: choose the contacts file in one place choose_contacts_file(flag) -> ContactsFile(path, origin, searched) is the only code that weighs --contacts-file, $IMSG_AGENT_CONTACTS and the search. ContactBook.open, --config and the --contacts empty-file hint all ask it, instead of each re-deriving flag/env precedence. Messages are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VCgWWsJbTMzsnA2WJkmsjC --- imsgread.py | 84 ++++++++++++++++++++++++++++++------------------ test_imsgread.py | 22 +++++++++++++ 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/imsgread.py b/imsgread.py index 52b45df..7313b6e 100644 --- a/imsgread.py +++ b/imsgread.py @@ -144,20 +144,53 @@ def contacts_candidates() -> list[Path]: return paths -def contacts_path() -> Path: - """The contacts file in effect, or where to create one if none exists. - - A set $IMSG_AGENT_CONTACTS is the answer whether or not the file exists - yet: falling through to another file would put a different set of - contacts in force than the one the caller named. +@dataclass(frozen=True) +class ContactsFile: + """The contacts.toml this run uses, and why that one. + + origin is "flag" (--contacts-file), "env" ($IMSG_AGENT_CONTACTS) or + "search" (the first of `searched` that exists, else where to create one). + A named file -- flag or env -- is the answer whether or not it exists yet: + falling through to another file would put a different set of contacts in + force than the one the caller named. """ + + path: Path + origin: str + searched: tuple[Path, ...] + + def load(self) -> dict[str, "Contact"]: + if self.origin == "env" and not self.path.exists(): + raise ConfigError( + f"${CONTACTS_ENV} points to {self.path}, which does not exist" + ) + return load_contact_entries(self.path) + + def empty_hint(self) -> str: + """What to do when this file has no contacts.""" + if self.path.exists(): + return f"add contacts to {self.path}" + if self.origin != "search": + return f"create {self.path}" + searched = "\n ".join(str(c) for c in self.searched) + return f"create {self.path}\nsearched:\n {searched}" + + +def choose_contacts_file(flag: Path | None = None) -> ContactsFile: + """The one place that decides which contacts.toml is in force.""" + searched = tuple(contacts_candidates()) + if flag is not None: + return ContactsFile(flag, "flag", searched) env = os.environ.get(CONTACTS_ENV) if env: - return Path(env).expanduser() - for candidate in contacts_candidates(): - if candidate.exists(): - return candidate - return CONFIG_CONTACTS + return ContactsFile(Path(env).expanduser(), "env", searched) + found = next((c for c in searched if c.exists()), CONFIG_CONTACTS) + return ContactsFile(found, "search", searched) + + +def contacts_path() -> Path: + """The contacts file in effect, or where to create one if none exists.""" + return choose_contacts_file().path @dataclass(frozen=True) @@ -221,11 +254,7 @@ def _parse_contact(name: str, value: object) -> Contact: def load_contact_entries(path: Path | None = None) -> dict[str, Contact]: """Every contact with its settings.""" if path is None: - path = contacts_path() - if os.environ.get(CONTACTS_ENV) and not path.exists(): - raise ConfigError( - f"${CONTACTS_ENV} points to {path}, which does not exist" - ) + return choose_contacts_file().load() if not path.exists(): return {} with path.open("rb") as fh: @@ -270,7 +299,8 @@ def __init__(self, entries: dict[str, Contact]): @classmethod def open(cls, path: Path | None = None) -> "ContactBook": - return cls(load_contact_entries(path)) + """The book from --contacts-file `path`, else the file in force.""" + return cls(choose_contacts_file(path).load()) def items(self) -> list[tuple[str, Contact]]: """Every contact, sorted by name.""" @@ -1197,7 +1227,7 @@ def _run_config(args: argparse.Namespace, terminal: Terminal) -> int: ) return 1 - path = args.contacts_file or contacts_path() + path = choose_contacts_file(args.contacts_file).path try: entries = load_contact_entries(path) except ConfigError as exc: @@ -1342,24 +1372,16 @@ def main(argv: list[str] | None = None, terminal: Terminal | None = None) -> int return _run_config(args, terminal) if args.contacts: + # The hint names the file this run reads; pointing anywhere else + # sends someone to edit a file that is never used. + in_force = choose_contacts_file(args.contacts_file) try: - entries = ContactBook.open(args.contacts_file).items() + entries = ContactBook(in_force.load()).items() except ConfigError as exc: print(f"error: {exc}", file=sys.stderr) return 1 if not entries: - # Name the file actually in force: --contacts-file, then a set - # $IMSG_AGENT_CONTACTS, then the search. Pointing anywhere else - # sends someone to edit a file this run never reads. - in_force = args.contacts_file or contacts_path() - if in_force.exists(): - hint = f"add contacts to {in_force}" - elif args.contacts_file or os.environ.get(CONTACTS_ENV): - hint = f"create {in_force}" - else: - searched = "\n ".join(str(c) for c in contacts_candidates()) - hint = f"create {in_force}\nsearched:\n {searched}" - print(f"no contacts configured -- {hint}", file=sys.stderr) + print(f"no contacts configured -- {in_force.empty_hint()}", file=sys.stderr) return 1 for name, contact in entries: # Only shown when set, so a contact with no standing decision reads diff --git a/test_imsgread.py b/test_imsgread.py index 3c09f70..3b3585f 100644 --- a/test_imsgread.py +++ b/test_imsgread.py @@ -493,6 +493,28 @@ def test_contacts_file_flag_overrides_every_default(tmp_path, monkeypatch): assert imsgread.ContactBook.open(explicit).resolve(contact="friend").identifier == "+1888" +@pytest.mark.parametrize("flag, env, expected", [ + (True, True, "flag"), # --contacts-file beats everything + (False, True, "env"), # then $IMSG_AGENT_CONTACTS, even if missing + (False, False, "search"), # then the first file found, or where to create one +]) +def test_one_choice_says_which_contacts_file_is_in_force_and_why(tmp_path, monkeypatch, flag, env, expected): + explicit = tmp_path / "explicit.toml" + named = tmp_path / "named-by-env.toml" # never created: a set env var still wins + config = write_contacts(tmp_path / "config.toml", "friend", "+1555") + monkeypatch.setattr(imsgread, "CONFIG_CONTACTS", config) + if env: + monkeypatch.setenv(imsgread.CONTACTS_ENV, str(named)) + else: + monkeypatch.delenv(imsgread.CONTACTS_ENV, raising=False) + + chosen = imsgread.choose_contacts_file(explicit if flag else None) + + assert chosen.origin == expected + assert chosen.path == {"flag": explicit, "env": named, "search": config}[expected] + assert config in chosen.searched + + def test_resolve_chat_honors_contacts_file_from_cli(tmp_path, messages, send_db): explicit = write_contacts(tmp_path / "explicit.toml", "friend", "+1888")