codetta 0.8.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. codetta/CHANGELOG.md +153 -0
  2. codetta/__init__.py +8 -0
  3. codetta/__main__.py +5 -0
  4. codetta/cli/__init__.py +0 -0
  5. codetta/cli/__main__.py +59 -0
  6. codetta/cli/run.py +129 -0
  7. codetta/core/__init__.py +0 -0
  8. codetta/core/acoustid.py +225 -0
  9. codetta/core/activity.py +161 -0
  10. codetta/core/artwork.py +238 -0
  11. codetta/core/auth.py +362 -0
  12. codetta/core/avatar.py +92 -0
  13. codetta/core/changelog.py +51 -0
  14. codetta/core/connections.py +159 -0
  15. codetta/core/describe.py +69 -0
  16. codetta/core/engine.py +470 -0
  17. codetta/core/health.py +520 -0
  18. codetta/core/inbox.py +195 -0
  19. codetta/core/index.py +672 -0
  20. codetta/core/library.py +177 -0
  21. codetta/core/musicbrainz.py +662 -0
  22. codetta/core/notifications.py +77 -0
  23. codetta/core/onboarding.py +39 -0
  24. codetta/core/organize.py +490 -0
  25. codetta/core/plugins.py +183 -0
  26. codetta/core/presets.py +452 -0
  27. codetta/core/providers.py +203 -0
  28. codetta/core/rawtags.py +135 -0
  29. codetta/core/slskd.py +650 -0
  30. codetta/core/tagmap.py +538 -0
  31. codetta/core/undo.py +186 -0
  32. codetta/core/updates.py +132 -0
  33. codetta/plugins/__init__.py +63 -0
  34. codetta/plugins/builtin/__init__.py +3 -0
  35. codetta/plugins/builtin/tidy.py +70 -0
  36. codetta/web/__init__.py +0 -0
  37. codetta/web/app.py +2909 -0
  38. codetta/web/security.py +170 -0
  39. codetta/web/static/app.css +2684 -0
  40. codetta/web/static/app.js +5694 -0
  41. codetta/web/static/apple-touch-icon.png +0 -0
  42. codetta/web/static/fonts/LICENSE-Inter.txt +93 -0
  43. codetta/web/static/fonts/LICENSE-SchibstedGrotesk.txt +93 -0
  44. codetta/web/static/fonts/inter-cyrillic.woff2 +0 -0
  45. codetta/web/static/fonts/schibsted-grotesk.woff2 +0 -0
  46. codetta/web/static/icon.svg +1 -0
  47. codetta/web/static/icons/codetta-dark.png +0 -0
  48. codetta/web/static/icons/codetta-dark.svg +1 -0
  49. codetta/web/static/icons/codetta-dark.webp +0 -0
  50. codetta/web/static/icons/codetta-light.png +0 -0
  51. codetta/web/static/icons/codetta-light.svg +1 -0
  52. codetta/web/static/icons/codetta-light.webp +0 -0
  53. codetta/web/static/icons/codetta.png +0 -0
  54. codetta/web/static/icons/codetta.svg +1 -0
  55. codetta/web/static/icons/codetta.webp +0 -0
  56. codetta/web/static/index.html +473 -0
  57. codetta/web/static/theme.js +12 -0
  58. codetta-0.8.1.dist-info/METADATA +282 -0
  59. codetta-0.8.1.dist-info/RECORD +63 -0
  60. codetta-0.8.1.dist-info/WHEEL +5 -0
  61. codetta-0.8.1.dist-info/entry_points.txt +2 -0
  62. codetta-0.8.1.dist-info/licenses/LICENSE +661 -0
  63. codetta-0.8.1.dist-info/top_level.txt +1 -0
codetta/core/health.py ADDED
@@ -0,0 +1,520 @@
1
+ """Library health: one list of things worth fixing across the whole library.
2
+
3
+ Everything comes from the search index (no music file is opened), so a check of
4
+ thousands of albums takes a fraction of a second. An *album* is a folder of
5
+ audio files; disc folders ("CD1", "Disc 2") join their parent.
6
+
7
+ Each check has an id and a category, and the user can switch any of them off.
8
+ Where the fix is obvious (e.g. "use 'Kehlani' as the album artist on all 17
9
+ tracks") an issue carries it: a set of tag changes for some files, applied
10
+ through the normal save path (safe writes, activity log, undo). Everything else
11
+ points at the right tool (the album itself, Find in MusicBrainz, Find in slskd).
12
+
13
+ Settings (switched-off checks, ignored issues) live in ``health.json``.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import functools
19
+ import hashlib
20
+ import json
21
+ import os
22
+ import re
23
+ import tempfile
24
+ import threading
25
+ import unicodedata
26
+ from collections import Counter
27
+ from collections.abc import Callable, Iterable
28
+ from dataclasses import dataclass, field
29
+ from pathlib import Path
30
+
31
+ from codetta.core.inbox import DISC_FOLDER
32
+
33
+ CATEGORIES = {
34
+ "splits": "Shows up split in your player",
35
+ "tracks": "Tracks",
36
+ "covers": "Covers",
37
+ "tags": "Tags & files",
38
+ }
39
+
40
+
41
+ @dataclass(frozen=True)
42
+ class Check:
43
+ id: str
44
+ category: str
45
+ label: str
46
+ about: str
47
+
48
+
49
+ CHECKS: tuple[Check, ...] = (
50
+ Check("split_album", "splits", "Album name differs",
51
+ "Tracks in one folder spell the album differently, so players show two albums."),
52
+ Check("split_albumartist", "splits", "Album artist differs",
53
+ "Tracks in one folder have different album artists, so players split the album."),
54
+ Check("no_albumartist", "splits", "No album artist",
55
+ "Tracks have different artists (often features) and no album artist to hold them together."),
56
+ Check("split_year", "splits", "Year differs",
57
+ "Tracks in one album have different years, which some players treat as different releases."),
58
+ Check("track_gaps", "tracks", "Missing tracks",
59
+ "Gaps in the track numbers (1, 2, 4), or fewer tracks than the album says it has."),
60
+ Check("track_dupes", "tracks", "Same track number twice",
61
+ "Two tracks on the same disc share a track number."),
62
+ Check("disc_numbers", "tracks", "Disc numbers missing",
63
+ "A multi-disc album where some tracks have no disc number."),
64
+ Check("no_cover", "covers", "No cover",
65
+ "No embedded cover and no cover image in the folder."),
66
+ Check("small_cover", "covers", "Small cover",
67
+ "The only cover is smaller than 500 pixels, which looks blurry on big screens."),
68
+ Check("missing_tags", "tags", "Missing basic tags",
69
+ "Tracks without a title, artist, album or track number."),
70
+ Check("duplicate_album", "tags", "Album in two folders",
71
+ "The same album (same album artist and name) lives in more than one folder."),
72
+ Check("duplicate_track", "tags", "Same song twice",
73
+ "The same title appears twice on one disc of an album (often an MP3 next to a FLAC)."),
74
+ Check("unreadable", "tags", "Unreadable files",
75
+ "Audio files Codetta couldn't read (damaged, or not what their name says)."),
76
+ Check("mixed_quality", "tags", "Mixed quality",
77
+ "An album that mixes lossless files (FLAC, ALAC...) with lossy ones (MP3, AAC...)."),
78
+ )
79
+ BY_ID = {c.id: c for c in CHECKS}
80
+
81
+ LOSSLESS = {"flac", "wav", "aiff", "aif", "ape", "wv", "wavpack", "dsf", "tta", "alac", "oggflac"}
82
+ IMAGE_EXT = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
83
+ SMALL_COVER = 500
84
+ MB_RELEASE_KEYS = ("musicbrainz_albumid", "musicbrainz album id")
85
+ MAX_PATHS = 1000
86
+ MAX_DETAIL = 12
87
+
88
+ _FEAT = re.compile(r"\s+(?:feat\.?|ft\.?|featuring|with|x|&|,|/|;|\+|vs\.?)\s+|\s*[,;/]\s*", re.IGNORECASE)
89
+ EDITION_RE = re.compile(r"\s*[\(\[][^)\]]*(?:deluxe|edition|remaster|version|expanded|bonus|explicit|clean)[^)\]]*[\)\]]", re.IGNORECASE)
90
+ _LEADING_NUMBER = re.compile(r"^\s*(?:(\d{1,2})[-.])?(\d{1,3})(?=[\s._-])")
91
+
92
+
93
+ _NON_WORD = re.compile(r"[^\w]+")
94
+
95
+
96
+ @functools.lru_cache(maxsize=65536) # the same album and artist names come up track after track
97
+ def fold(text: str) -> str:
98
+ """Compare form: no accents, case, or punctuation differences."""
99
+ if not text.isascii():
100
+ text = unicodedata.normalize("NFKD", text)
101
+ text = "".join(c for c in text if not unicodedata.combining(c))
102
+ return " ".join(_NON_WORD.sub(" ", text.casefold()).split())
103
+
104
+
105
+ def _first(tags: dict, key: str) -> str:
106
+ v = tags.get(key)
107
+ if v.__class__ is str: # already reduced to its first value (see _slim)
108
+ return v
109
+ for x in v or []:
110
+ if isinstance(x, str) and x.strip():
111
+ return x.strip()
112
+ return ""
113
+
114
+
115
+ _NUM = re.compile(r"^\s*(\d+)")
116
+ _TOTAL = re.compile(r"^\s*\d*\s*/\s*(\d+)")
117
+
118
+
119
+ def _num(value: str) -> int | None:
120
+ m = _NUM.match(value or "")
121
+ return int(m.group(1)) if m else None
122
+
123
+
124
+ def _total(value: str) -> int | None:
125
+ m = _TOTAL.match(value or "")
126
+ return int(m.group(1)) if m else None
127
+
128
+
129
+ def _base_artist(artist: str) -> str:
130
+ """ "Kehlani feat. Zayn" -> "Kehlani"."""
131
+ return _FEAT.split(artist, 1)[0].strip() if artist else ""
132
+
133
+
134
+ def _lossless(track: dict) -> bool:
135
+ fmt = (track.get("format") or "").lower()
136
+ if fmt in ("m4a", "mp4", "m4b"): # ALAC or AAC: tell them apart by bitrate
137
+ return (track.get("bitrate") or 0) >= 500_000
138
+ return fmt in LOSSLESS
139
+
140
+
141
+ def issue_id(check: str, folder: str) -> str:
142
+ return hashlib.sha1(f"{check}\0{folder}".encode()).hexdigest()[:12]
143
+
144
+
145
+ @dataclass
146
+ class Album:
147
+ folder: str
148
+ tracks: list[tuple[str, dict]] = field(default_factory=list) # (path, track details)
149
+
150
+ def values(self, key: str) -> list[str]:
151
+ cache = self.__dict__.setdefault("_values", {})
152
+ if key not in cache:
153
+ cache[key] = [_first(t.get("tags") or {}, key) for _, t in self.tracks]
154
+ return cache[key]
155
+
156
+ def common(self, key: str) -> str:
157
+ """The most common non-empty value (ties: the first one seen)."""
158
+ vals = [v for v in self.values(key) if v]
159
+ return Counter(vals).most_common(1)[0][0] if vals else ""
160
+
161
+ @functools.cached_property
162
+ def is_album(self) -> bool:
163
+ """Most tracks agree on the album name (a "Singles" folder doesn't)."""
164
+ names = [fold(v) for v in self.values("album") if v]
165
+ if not names:
166
+ return False
167
+ return Counter(names).most_common(1)[0][1] >= 0.6 * len(self.tracks)
168
+
169
+ def disc_of(self, path: str, track: dict) -> int:
170
+ d = _num(_first(track.get("tags") or {}, "discnumber"))
171
+ if d:
172
+ return d
173
+ sub = path[len(self.folder) + 1:].split("/")[0] if path.startswith(self.folder + "/") else ""
174
+ m = re.search(r"(\d+)", sub) if DISC_FOLDER.match(sub or "") else None
175
+ return int(m.group(1)) if m else 1
176
+
177
+
178
+ def group_albums(tracks: Iterable[tuple[str, dict]], skip: Iterable[str] = ()) -> dict[str, Album]:
179
+ skip = tuple(s.rstrip("/") + "/" for s in skip if s)
180
+ albums: dict[str, Album] = {}
181
+ for path, track in tracks:
182
+ if path.startswith("@") or (skip and path.startswith(skip)):
183
+ continue
184
+ folder = path.rsplit("/", 1)[0] if "/" in path else ""
185
+ name = folder.rsplit("/", 1)[-1]
186
+ if folder and "/" in folder and DISC_FOLDER.match(name):
187
+ folder = folder.rsplit("/", 1)[0]
188
+ albums.setdefault(folder, Album(folder)).tracks.append((path, track))
189
+ return albums
190
+
191
+
192
+ def _issue(check: str, album: Album, message: str, paths: list[str], detail: list[str] | None = None,
193
+ fix: dict | None = None, actions: tuple[str, ...] = ()) -> dict:
194
+ # ``paths`` isn't kept: nothing needs the file list once the issue is found, and on a
195
+ # library of thousands of albums those lists were most of the memory health used.
196
+ return {
197
+ "id": issue_id(check, album.folder), "check": check, "category": BY_ID[check].category,
198
+ "folder": album.folder, "album": album.common("album") or album.folder.rsplit("/", 1)[-1] or "Library",
199
+ "artist": album.common("albumartist") or album.common("artist"),
200
+ "tracks": len(album.tracks), "message": message, "detail": (detail or [])[:MAX_DETAIL],
201
+ "more": max(0, len(detail or []) - MAX_DETAIL), "fix": fix, "actions": list(actions),
202
+ }
203
+
204
+
205
+ def _set_fix(album: Album, key: str, value: str, label: str, extra: dict | None = None) -> dict | None:
206
+ """Set ``key`` to ``value`` on every track that has something else."""
207
+ paths = [p for p, t in album.tracks if _first(t.get("tags") or {}, key) != value][:MAX_PATHS]
208
+ if not paths or not value:
209
+ return None
210
+ return {"label": label, "per_file": {p: {key: value, **(extra or {})} for p in paths},
211
+ "summary": f"Set {key.replace('albumartist', 'album artist').replace('date', 'year')} to “{value}” on "
212
+ f"{len(paths)} track{'s' if len(paths) != 1 else ''}"}
213
+
214
+
215
+ def _differs(album: Album, key: str) -> list[str]:
216
+ """``key``'s distinct values (as spelled), if the tracks disagree once case and punctuation are ignored."""
217
+ seen: dict[str, str] = {}
218
+ for v in album.values(key):
219
+ if v:
220
+ seen.setdefault(fold(v) if key != "date" else v[:4], v)
221
+ return list(seen.values()) if len(seen) > 1 else []
222
+
223
+
224
+ def check_album(album: Album, has_folder_image: Callable[[str], bool]) -> list[dict]:
225
+ out: list[dict] = []
226
+ tracks = album.tracks
227
+ paths = [p for p, _ in tracks]
228
+ is_album = album.is_album
229
+
230
+ # --- Shows up split in your player ---
231
+ if is_album:
232
+ names = _differs(album, "album")
233
+ if names:
234
+ best = album.common("album")
235
+ out.append(_issue("split_album", album, f"{len(names)} spellings of the album name",
236
+ paths, [f"“{n}”" for n in names],
237
+ _set_fix(album, "album", best, f"Use “{best}”")))
238
+ aas = _differs(album, "albumartist")
239
+ if aas:
240
+ best = album.common("albumartist")
241
+ out.append(_issue("split_albumartist", album, f"{len(aas)} different album artists",
242
+ paths, [f"“{a}”" for a in aas],
243
+ _set_fix(album, "albumartist", best, f"Use “{best}”")))
244
+ elif not any(album.values("albumartist")):
245
+ artists = [a for a in album.values("artist") if a]
246
+ if len({fold(a) for a in artists}) > 1:
247
+ bases = Counter(_base_artist(a) for a in artists)
248
+ base, n = bases.most_common(1)[0]
249
+ if n >= 0.6 * len(tracks) and base:
250
+ fix = _set_fix(album, "albumartist", base, f"Use “{base}”")
251
+ else:
252
+ fix = _set_fix(album, "albumartist", "Various Artists", "Use “Various Artists”", {"compilation": "1"})
253
+ out.append(_issue("no_albumartist", album, f"{len(set(artists))} artists and no album artist",
254
+ paths, [f"“{a}”" for a in dict.fromkeys(artists)], fix))
255
+ years = _differs(album, "date")
256
+ if years:
257
+ best = album.common("date")
258
+ out.append(_issue("split_year", album, f"{len(years)} different years", paths,
259
+ sorted(years), _set_fix(album, "date", best, f"Use {best}")))
260
+
261
+ # --- Tracks ---
262
+ if is_album and len(tracks) >= 2:
263
+ discs: dict[int, list[tuple[str, dict]]] = {}
264
+ for p, t in tracks:
265
+ discs.setdefault(album.disc_of(p, t), []).append((p, t))
266
+ missing, dupes = [], []
267
+ first_disc = min(discs)
268
+ for disc, items in sorted(discs.items()):
269
+ label = f"Disc {disc}, track " if len(discs) > 1 else "Track "
270
+ nums = Counter()
271
+ total = 0
272
+ for _, t in items:
273
+ tn = _first(t.get("tags") or {}, "tracknumber")
274
+ n = _num(tn)
275
+ if n:
276
+ nums[n] += 1
277
+ total = max(total, _total(tn) or 0,
278
+ _num(_first(t.get("tags") or {}, "tracktotal")) or 0,
279
+ _num(_first(t.get("tags") or {}, "totaltracks")) or 0)
280
+ if not nums:
281
+ continue
282
+ # Later discs may carry on the numbering (disc 2 starting at 17): count from where they start.
283
+ start = 1 if disc == first_disc else min(nums)
284
+ # A track total on a multi-disc album often counts every disc, so there only gaps count.
285
+ # A single disc trusts its total when it's plausible (not, say, 30 for 15 tracks).
286
+ plausible = len(discs) == 1 and total - len(items) <= max(3, len(items) // 2)
287
+ last = max(total, max(nums)) if plausible else max(nums)
288
+ if any(c > 1 for c in nums.values()) and len(nums) <= len(items) // 2:
289
+ last = max(nums) # numbering is broken (every track "1"): that's the duplicate check's job
290
+ missing += [f"{label}{n}" for n in range(start, last + 1) if n not in nums]
291
+ dupes += [f"{label}{n} ({c}×)" for n, c in sorted(nums.items()) if c > 1]
292
+ if missing:
293
+ out.append(_issue("track_gaps", album, f"{len(missing)} track{'s' if len(missing) != 1 else ''} missing",
294
+ paths, missing, None, ("open", "slskd")))
295
+ if dupes:
296
+ out.append(_issue("track_dupes", album, "The same track number twice", paths, dupes))
297
+ disc_vals = [_num(v) for v in album.values("discnumber")]
298
+ has_disc_folders = any(album.disc_of(p, {"tags": {}}) > 1 for p in paths)
299
+ lacking = [p for p, t in tracks if not _num(_first(t.get("tags") or {}, "discnumber"))]
300
+ if lacking and (any(d and d > 1 for d in disc_vals) or has_disc_folders):
301
+ fix = None
302
+ if all(p.rsplit("/", 1)[0] != album.folder for p in lacking):
303
+ # Every track without one sits in a "CD2"-style folder: number them from it.
304
+ fix = {"label": "Number from disc folders",
305
+ "per_file": {p: {"discnumber": str(album.disc_of(p, {"tags": {}}))} for p in lacking},
306
+ "summary": f"Set the disc number from its folder (“CD2” → 2) on {len(lacking)} track{'s' if len(lacking) != 1 else ''}"}
307
+ out.append(_issue("disc_numbers", album, f"{len(lacking)} track{'s' if len(lacking) != 1 else ''} without a disc number",
308
+ lacking, [p.rsplit("/", 1)[-1] for p in lacking], fix))
309
+
310
+ # --- Covers ---
311
+ arts = [t.get("art") for _, t in tracks if t.get("art")]
312
+ if not has_folder_image(album.folder):
313
+ if not arts:
314
+ out.append(_issue("no_cover", album, "No cover", paths, None, None, ("open", "musicbrainz")))
315
+ else:
316
+ biggest = max(max(a.get("width") or 0, a.get("height") or 0) for a in arts)
317
+ if 0 < biggest < SMALL_COVER:
318
+ out.append(_issue("small_cover", album, f"Cover is only {biggest} px", paths, None, None,
319
+ ("open", "musicbrainz")))
320
+
321
+ # --- Tags & files ---
322
+ needs = {"title": "title", "artist": "artist", "album": "album", "tracknumber": "track number"}
323
+ gaps: dict[str, list[str]] = {}
324
+ for p, t in tracks:
325
+ if t.get("error"):
326
+ continue
327
+ tags = t.get("tags") or {}
328
+ for key, name in needs.items():
329
+ if not _first(tags, key):
330
+ gaps.setdefault(name, []).append(p)
331
+ if gaps:
332
+ affected = sorted({p for ps in gaps.values() for p in ps})
333
+ fix = None
334
+ no_number = gaps.get("track number", [])
335
+ if no_number and len(gaps) == 1:
336
+ numbered = {p: _LEADING_NUMBER.match(p.rsplit("/", 1)[-1]) for p in no_number}
337
+ if all(numbered.values()):
338
+ fix = {"label": "Number from file names", "per_file": {p: {"tracknumber": str(int(m.group(2)))}
339
+ for p, m in numbered.items()},
340
+ "summary": f"Take the track number from the file name on {len(no_number)} track{'s' if len(no_number) != 1 else ''} (“01 Title.flac” → 1)"}
341
+ out.append(_issue("missing_tags", album,
342
+ "; ".join(f"{len(ps)} without {name}" for name, ps in gaps.items()),
343
+ affected, [p.rsplit("/", 1)[-1] for p in affected], fix))
344
+
345
+ if is_album:
346
+ by_title: dict[tuple[int, str], list[tuple[str, float]]] = {}
347
+ for p, t in tracks:
348
+ title = _first(t.get("tags") or {}, "title")
349
+ if title:
350
+ by_title.setdefault((album.disc_of(p, t), fold(title)), []).append((p, t.get("duration") or 0))
351
+ doubles = []
352
+ for items in by_title.values():
353
+ # The same recording twice (an MP3 next to the FLAC) is the same length; remixes and
354
+ # versions sharing a title aren't.
355
+ items.sort(key=lambda x: x[1])
356
+ same = [items[i] for i in range(len(items))
357
+ if any(j != i and abs(items[i][1] - items[j][1]) <= 2 for j in range(len(items)))]
358
+ if len(same) > 1:
359
+ doubles.append([p for p, _ in same])
360
+ if doubles:
361
+ out.append(_issue("duplicate_track", album, f"{len(doubles)} song{'s' if len(doubles) != 1 else ''} twice",
362
+ [p for ps in doubles for p in ps],
363
+ [" = ".join(p.rsplit("/", 1)[-1] for p in ps) for ps in doubles]))
364
+
365
+ broken = [p for p, t in tracks if t.get("error")]
366
+ if broken:
367
+ out.append(_issue("unreadable", album, f"{len(broken)} file{'s' if len(broken) != 1 else ''} can't be read",
368
+ broken, [p.rsplit("/", 1)[-1] for p in broken]))
369
+
370
+ readable = [(p, t) for p, t in tracks if not t.get("error")]
371
+ kinds = {_lossless(t) for _, t in readable}
372
+ if len(kinds) == 2:
373
+ lossy = [p for p, t in readable if not _lossless(t)]
374
+ out.append(_issue("mixed_quality", album, f"{len(lossy)} lossy track{'s' if len(lossy) != 1 else ''} in a lossless album"
375
+ if len(lossy) < len(readable) / 2 else "Lossless and lossy tracks mixed",
376
+ lossy, [p.rsplit("/", 1)[-1] for p in lossy], None, ("open", "slskd")))
377
+ return out
378
+
379
+
380
+ def duplicate_albums(albums: dict[str, Album]) -> list[dict]:
381
+ keyed: dict[tuple[str, str], list[Album]] = {}
382
+ for a in albums.values():
383
+ if a.is_album:
384
+ key = _album_key(a)
385
+ if key:
386
+ keyed.setdefault(key, []).append(a)
387
+ return [_duplicate_issue(group) for group in keyed.values() if len(group) > 1]
388
+
389
+
390
+ def _album_key(a: Album) -> tuple[str, str] | None:
391
+ name = fold(a.common("album"))
392
+ return (fold(a.common("albumartist") or _base_artist(a.common("artist"))), name) if name else None
393
+
394
+
395
+ def _duplicate_issue(group: list[Album]) -> dict:
396
+ first, *rest = sorted(group, key=lambda a: a.folder)
397
+ issue = _issue("duplicate_album", first, f"Also in {len(rest)} other folder{'s' if len(rest) != 1 else ''}",
398
+ [], [a.folder for a in group], None, ("open",))
399
+ issue["folders"] = [a.folder for a in sorted(group, key=lambda a: a.folder)]
400
+ return issue
401
+
402
+
403
+ # Only what the checks read is kept per track while an album is being checked.
404
+ _KEEP_TAGS = ("album", "albumartist", "artist", "title", "tracknumber", "discnumber", "date", "tracktotal", "totaltracks")
405
+
406
+
407
+ def _slim(track: dict) -> dict:
408
+ tags = track.get("tags") or {}
409
+ kept = {}
410
+ for k in _KEEP_TAGS:
411
+ first = _first(tags, k) if tags.get(k) else ""
412
+ if first:
413
+ kept[k] = first # the first value is all the checks read: keep just that
414
+ return {"tags": kept, "art": track.get("art"), "error": track.get("error"),
415
+ "format": track.get("format"), "bitrate": track.get("bitrate"), "duration": track.get("duration")}
416
+
417
+
418
+ def _album_folder(path: str) -> str:
419
+ folder = path.rsplit("/", 1)[0] if "/" in path else ""
420
+ name = folder.rsplit("/", 1)[-1]
421
+ if folder and "/" in folder and DISC_FOLDER.match(name):
422
+ folder = folder.rsplit("/", 1)[0]
423
+ return folder
424
+
425
+
426
+ def run(tracks: Iterable[tuple[str, dict]], has_folder_image: Callable[[str], bool],
427
+ matched_paths: set[str] = frozenset(), skip: Iterable[str] = ()) -> dict:
428
+ """Every issue (whatever the settings), plus a few numbers.
429
+
430
+ Streams: ``tracks`` in path order (as the index gives them) is checked one album at a
431
+ time, so a library of 100,000 tracks never sits in memory at once. A list is sorted first."""
432
+ if isinstance(tracks, list):
433
+ tracks = sorted(tracks, key=lambda x: x[0])
434
+ skip = tuple(s.rstrip("/") + "/" for s in skip if s)
435
+ issues: list[dict] = []
436
+ open_albums: dict[str, Album] = {} # albums whose files may still come (nested folders interleave)
437
+ dupes: dict[tuple[str, str], list[Album]] = {}
438
+ stats = {"albums": 0, "folders": 0, "tracks": 0, "unmatched": 0}
439
+
440
+ def finish(album: Album) -> None:
441
+ stats["folders"] += 1
442
+ issues.extend(check_album(album, has_folder_image))
443
+ if album.is_album:
444
+ stats["albums"] += 1
445
+ if not any(p in matched_paths for p, _ in album.tracks):
446
+ stats["unmatched"] += 1
447
+ key = _album_key(album)
448
+ if key: # keep a light copy (names only) for the duplicate check at the end
449
+ light = Album(album.folder, [(p, {"tags": {k: t["tags"].get(k, "") for k in ("album", "albumartist", "artist")}})
450
+ for p, t in album.tracks[:3]])
451
+ dupes.setdefault(key, []).append(light)
452
+
453
+ for path, track in tracks:
454
+ if path.startswith("@") or (skip and path.startswith(skip)):
455
+ continue
456
+ stats["tracks"] += 1
457
+ folder = _album_folder(path)
458
+ for key in [k for k in open_albums if k and k != folder and not path.startswith(k + "/")]:
459
+ finish(open_albums.pop(key)) # sorted input: this album's files are all behind us
460
+ open_albums.setdefault(folder, Album(folder)).tracks.append((path, _slim(track)))
461
+ for album in open_albums.values():
462
+ finish(album)
463
+ issues.extend(_duplicate_issue(g) for g in dupes.values() if len(g) > 1)
464
+ order = list(CATEGORIES)
465
+ issues.sort(key=lambda i: (order.index(i["category"]), i["folder"].casefold(), i["check"]))
466
+ return {**stats, "issues": issues}
467
+
468
+
469
+ def folder_has_image(root: Callable[[str], Path]) -> Callable[[str], bool]:
470
+ """A cover file (cover.jpg, folder.png, front.webp...) in the album folder or a disc folder."""
471
+ def check(folder: str) -> bool:
472
+ try:
473
+ top = root(folder)
474
+ with os.scandir(top) as entries:
475
+ subs = []
476
+ for e in entries:
477
+ if e.is_file() and Path(e.name).suffix.lower() in IMAGE_EXT and not e.name.startswith("."):
478
+ return True
479
+ if e.is_dir() and DISC_FOLDER.match(e.name):
480
+ subs.append(e.path)
481
+ for s in subs:
482
+ with os.scandir(s) as entries:
483
+ if any(e.is_file() and Path(e.name).suffix.lower() in IMAGE_EXT for e in entries):
484
+ return True
485
+ except OSError:
486
+ pass
487
+ return False
488
+ return check
489
+
490
+
491
+ class HealthStore:
492
+ """Switched-off checks and ignored issues, saved to health.json."""
493
+
494
+ def __init__(self, file: Path) -> None:
495
+ self.file = file
496
+ self.lock = threading.Lock()
497
+ self.enabled = True # the whole feature (off: no card, nothing is checked)
498
+ self.disabled: set[str] = set()
499
+ self.ignored: set[str] = set()
500
+ try:
501
+ data = json.loads(file.read_text("utf-8"))
502
+ if isinstance(data, dict):
503
+ self.enabled = data.get("enabled") is not False
504
+ self.disabled = {c for c in data.get("disabled") or [] if c in BY_ID}
505
+ self.ignored = {i for i in data.get("ignored") or [] if isinstance(i, str)}
506
+ except (OSError, ValueError):
507
+ pass
508
+
509
+ def save(self) -> None:
510
+ with self.lock:
511
+ self.file.parent.mkdir(parents=True, exist_ok=True)
512
+ fd, tmp = tempfile.mkstemp(prefix=".health-", dir=self.file.parent)
513
+ try:
514
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
515
+ json.dump({"enabled": self.enabled, "disabled": sorted(self.disabled),
516
+ "ignored": sorted(self.ignored)}, f)
517
+ os.replace(tmp, self.file)
518
+ except BaseException:
519
+ Path(tmp).unlink(missing_ok=True)
520
+ raise