"""Where the organized music tree lives, on *this* machine. The library stores the music folder three ways (see `models/library.py`), and which one applies depends on the machine you are sitting at. `resolve()` is the single place that decides, in this order: 1. ``music_folder_rel`` resolved against the data dir — the portable value. Both the music and the data dir ride one Syncthing share, so the *shape* ("the media folder is one level up from my data dir") is identical on every machine even when the absolute paths are not. 2. ``music_folder`` — the legacy absolute value. Libraries written before 0.10, and libraries last written by an older LinTunes, only have this one. 3. The machine-local override in ``~/.config/lintunes/config.json`` — either ``music_folder_override`` (set by the folder prompt) or the older ``music_root`` that ``--music-root … --save-config`` has always written. The override is consulted **last, not first**, on purpose. If it won outright, a machine that once needed an override would be pinned to it forever and would never pick up a later shared change. Consulting it only after the shared value fails to resolve gives an escape hatch *and* self-healing: plug the drive back in and the shared setting takes over again on its own. Every candidate must be an existing directory. A path that isn't there is not a music folder — better to say "I can't find it" than to silently rebuild a phantom tree next to wherever the app happened to be launched from. """ from dataclasses import dataclass from pathlib import Path from lintunes.config import load_config, save_config from lintunes.paths import to_absolute # Keys in the per-machine config.json (never synced). OVERRIDE_KEY = "music_folder_override" PROMPT_SEEN_KEY = "music_folder_prompted" # The same fact under its pre-0.10 name: `--music-root … --save-config` has # always written this machine's media folder here, and the importer stores it as # `library.music_folder`. Every library set up that way therefore has the right # answer sitting in config.json, and ignoring it meant a machine that mounts the # drive somewhere else was asked to locate a folder it had already been told # about. Read-only — nothing writes it back, so the newer key stays canonical. LEGACY_OVERRIDE_KEY = "music_root" @dataclass(frozen=True) class Resolution: """What the music folder resolved to here, and where the answer came from.""" path: Path | None # an existing media folder, or None source: str # library | legacy | machine | missing | unset stored: str = "" # what we tried, for the "can't find it" copy @property def is_machine_local(self) -> bool: return self.source == "machine" def _existing_dir(candidate: str) -> Path | None: if not candidate: return None path = Path(candidate).expanduser() return path if path.is_dir() else None def resolve(library, data_dir, config=None) -> Resolution: """Pick the media folder for this machine. See the module docstring.""" config = load_config() if config is None else config stored = "" rel = getattr(library, "music_folder_rel", "") if rel: stored = to_absolute(rel, str(data_dir)) found = _existing_dir(stored) if found is not None: return Resolution(found, "library", stored) legacy = getattr(library, "music_folder", "") if legacy: stored = stored or legacy found = _existing_dir(legacy) if found is not None: return Resolution(found, "legacy", legacy) for key in (OVERRIDE_KEY, LEGACY_OVERRIDE_KEY): override = config.get(key, "") if not override: continue found = _existing_dir(override) if found is not None: return Resolution(found, "machine", override) stored = stored or override # Something is on file but not on this machine, versus never set at all — # two different conversations to have with the user. return Resolution(None, "missing" if stored else "unset", stored) def normalize_media_folder(chosen) -> Path: """Turn the folder a user picked into the value we store. ``organize_root()`` is ``/Music`` — the iTunes layout, where an "iTunes Media" folder contains "Music". Taken literally, a newcomer who picks ``~/Music`` would get ``~/Music/Music/Artist/Album``, which just looks broken. So a folder that is *itself* called Music and doesn't already contain one is treated as the tree, not its parent. Applied once, at set time, so the answer is persisted and can never flip later because a subfolder appeared. An "iTunes Media" folder (or any folder that already has a Music child) is stored unchanged, which leaves existing libraries exactly where they are. """ chosen = Path(chosen).expanduser() if chosen.name.lower() == "music" and not (chosen / "Music").is_dir(): return chosen.parent return chosen def organize_root_for(media_folder) -> Path: """The tree files are actually filed into, for preview text.""" return Path(media_folder) / "Music" def machine_override(config=None) -> str: config = load_config() if config is None else config return config.get(OVERRIDE_KEY, "") def set_machine_override(path: str | None): """Record (or clear) this machine's own music folder. Never synced.""" config = load_config() if path: config[OVERRIDE_KEY] = str(path) else: config.pop(OVERRIDE_KEY, None) save_config(config) def prompt_seen(config=None) -> bool: config = load_config() if config is None else config return bool(config.get(PROMPT_SEEN_KEY)) def mark_prompt_seen(): """Remember that this machine has been asked once. Deliberately in config.json rather than preferences.json: preferences ride the synced data dir, so a "Not Now" on one machine would silently suppress the question on the other — but each machine may genuinely need its own answer. """ config = load_config() config[PROMPT_SEEN_KEY] = True save_config(config)