From b78b6c5275d37162986d11b45791c0e19aabb3e8 Mon Sep 17 00:00:00 2001 From: trav Date: Tue, 17 Nov 2026 22:21:29 -0800 Subject: [PATCH] v0.96.1: songs are named in Unicode NFC in the music folder, since Syncthing renames anything else under the library (a Bengali YouTube title lost its file that way); a song already renamed so is followed --- CLAUDE.md | 7 ++++ lintunes/__init__.py | 2 +- lintunes/gui/main_window.py | 7 +++- lintunes/importers/file_importer.py | 8 ++++ lintunes/library_manager.py | 25 +++++++++++- tests/test_round130.py | 61 +++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 4 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 1431c7c..de6979b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1357,6 +1357,13 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal polished while parentless looks like a window to the installer, so a `ParentChange` takes the translucency back off — kept on, the now-playing panel (style sheet set before it had a parent) painted no background. +- **Names in the music tree are NFC** (Round 130): Syncthing (except on a + Mac) renames a non-NFC name to its NFC spelling as it scans, so a YouTube + title with Bengali "ড়" (U+09DC → U+09A1 U+09BC) was renamed under the + library seconds after import and the next tag write hit ENOENT. + `file_importer.sanitize_component` and `_maybe_move_file` write NFC, and + `LibraryManager.repair_location` follows a missing file to its NFC twin + (before every tag write, and on `track_missing` before asking). - **Music files are only touched deliberately:** tag edits via `tagging.py`, and — since Round 18 — artist/album_artist/album edits relocate the file inside `LibraryManager.organize_root()` (`/Music`) to keep the diff --git a/lintunes/__init__.py b/lintunes/__init__.py index d439943..971d5d7 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.96.0" +__version__ = "0.96.1" diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index 08cf0c4..13a4313 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -2931,7 +2931,12 @@ class MainWindow(QMainWindow): def _on_track_missing(self, track): """A track's file is gone (moved/renamed outside lintunes). Offer to - relocate it manually; on success, repoint the track and resume.""" + relocate it manually; on success, repoint the track and resume. One + renamed to its NFC spelling (Syncthing does that) is followed + without asking.""" + if self._manager.repair_location(track): + QTimer.singleShot(0, self.player.retry_current) + return box = QMessageBox(self) box.setIcon(QMessageBox.Icon.Warning) box.setWindowTitle("Can't play track") diff --git a/lintunes/importers/file_importer.py b/lintunes/importers/file_importer.py index b7aed10..a8ad7a4 100644 --- a/lintunes/importers/file_importer.py +++ b/lintunes/importers/file_importer.py @@ -1,5 +1,6 @@ import logging import re +import unicodedata import shutil from datetime import datetime, timezone from pathlib import Path @@ -20,6 +21,13 @@ def is_audio_file(path: Path) -> bool: def sanitize_component(name: str) -> str: + """A safe file or folder name, in Unicode NFC (Round 130): Syncthing + renames any name that isn't NFC to its NFC spelling as it scans (except + on a Mac), so a song imported as "ঘর বাড়ি…" from a YouTube title was + renamed under the library seconds later and Identify's tag write found + nothing there. Naming it NFC to begin with leaves Syncthing nothing to + rename.""" + name = unicodedata.normalize("NFC", name) name = _UNSAFE_CHARS.sub("_", name).strip().rstrip(".") return name or "Unknown" diff --git a/lintunes/library_manager.py b/lintunes/library_manager.py index 5a7be98..3f37b92 100644 --- a/lintunes/library_manager.py +++ b/lintunes/library_manager.py @@ -1,5 +1,6 @@ import logging import secrets +import unicodedata from datetime import datetime, timezone from pathlib import Path @@ -595,6 +596,22 @@ class LibraryManager(QObject): self._schedule_save() self.track_updated.emit(track_id) + def repair_location(self, track) -> bool: + """A track whose file is missing but whose path's NFC spelling + exists is pointed at that (Round 130): Syncthing renamed it so, + under us. True when the track's file is (now) there.""" + if track is None or not track.location: + return False + if Path(track.location).exists(): + return True + nfc = unicodedata.normalize("NFC", track.location) + if nfc != track.location and Path(nfc).exists(): + log.info("%s was renamed to its NFC spelling; following it", + track.location) + self.set_track_location(track.track_id, nfc) + return True + return False + def edit_track_fields(self, track_id: int, fields: dict): """Edit a track's metadata: write the changed tags to the audio file, update the in-memory library, and record the change for undo. @@ -779,6 +796,7 @@ class LibraryManager(QObject): if k in tagging.EDITABLE_FIELDS} if not writable or not track.location: return True + self.repair_location(track) try: tagging.write_tags(track.location, writable) return True @@ -918,9 +936,12 @@ class LibraryManager(QObject): return None merged = {key: changed.get(key, getattr(track, key)) for key in self._ORGANIZE_FIELDS} - # Keep the exact on-disk basename: it's already valid here, and + # Keep the on-disk basename: it's already valid here, and # re-sanitizing could gratuitously rename files that came from macOS. - dest = organized_destination(root, merged, src.name).with_name(src.name) + # Only its Unicode form changes, to NFC, which Syncthing would + # rename it to anyway (see file_importer.sanitize_component). + dest = organized_destination(root, merged, src.name).with_name( + unicodedata.normalize("NFC", src.name)) if dest == src: return None try: diff --git a/tests/test_round130.py b/tests/test_round130.py index db2972e..52703c0 100644 --- a/tests/test_round130.py +++ b/tests/test_round130.py @@ -437,3 +437,64 @@ def test_the_shown_playlists_cover_shows_while_nothing_plays(window, qapp, window._show_library() # no playlist shown assert not art.showing_playlist_art() and not art.has_art() + + +# -------------------------------------------------------------------------- +# H. names Syncthing would rename: written in NFC, and followed if renamed + +import os # noqa: E402 +import shutil # noqa: E402 +import unicodedata # noqa: E402 + +from lintunes.importers import file_importer # noqa: E402 +from lintunes.library_manager import LibraryManager # noqa: E402 +from lintunes.models import Library # noqa: E402 + +# "বাড়ি" as YouTube spells it: U+09DC, which NFC writes as U+09A1 U+09BC. +RAW = "বাড়ি" +NFC = unicodedata.normalize("NFC", RAW) + + +def test_the_raw_name_is_not_nfc(): + assert RAW != NFC + + +def _manager(tmp_path): + media = tmp_path / "media" + (media / "Music").mkdir(parents=True) + return LibraryManager(Library(music_folder=str(media)), tmp_path / "data") + + +def test_an_import_is_named_in_nfc(tmp_path, mp3_file, qapp): + src = tmp_path / "dl" / f"{RAW} [abc].mp3" + src.parent.mkdir() + shutil.copy(mp3_file, src) + manager = _manager(tmp_path) + track = file_importer.import_file(src, manager.organize_root(), manager) + assert os.path.basename(track.location) == f"{NFC} [abc].mp3" + assert os.path.exists(track.location) + + +def test_a_file_syncthing_renamed_is_followed(tmp_path, mp3_file, qapp): + manager = _manager(tmp_path) + folder = manager.organize_root() / "Unknown Artist" / "Unknown Album" + folder.mkdir(parents=True) + raw = folder / f"{RAW}.mp3" + shutil.copy(mp3_file, raw) + track = Track(track_id=1, name=RAW, location=str(raw)) + manager.add_track(track) + os.rename(raw, folder / f"{NFC}.mp3") # what Syncthing does + + manager.edit_track_fields(1, {"artist": "Someone", "album": "Bhandari"}) + assert os.path.exists(track.location) + assert track.location.endswith(f"Someone/Bhandari/{NFC}.mp3") + from lintunes import tagging + assert tagging.read_tags(track.location)["artist"] == "Someone" + + +def test_a_missing_file_with_no_nfc_twin_stays_missing(tmp_path, qapp): + manager = _manager(tmp_path) + track = Track(track_id=1, name="x", location=str(tmp_path / "gone.mp3")) + manager.add_track(track) + assert not manager.repair_location(track) + assert track.location == str(tmp_path / "gone.mp3")