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.
- codetta/CHANGELOG.md +153 -0
- codetta/__init__.py +8 -0
- codetta/__main__.py +5 -0
- codetta/cli/__init__.py +0 -0
- codetta/cli/__main__.py +59 -0
- codetta/cli/run.py +129 -0
- codetta/core/__init__.py +0 -0
- codetta/core/acoustid.py +225 -0
- codetta/core/activity.py +161 -0
- codetta/core/artwork.py +238 -0
- codetta/core/auth.py +362 -0
- codetta/core/avatar.py +92 -0
- codetta/core/changelog.py +51 -0
- codetta/core/connections.py +159 -0
- codetta/core/describe.py +69 -0
- codetta/core/engine.py +470 -0
- codetta/core/health.py +520 -0
- codetta/core/inbox.py +195 -0
- codetta/core/index.py +672 -0
- codetta/core/library.py +177 -0
- codetta/core/musicbrainz.py +662 -0
- codetta/core/notifications.py +77 -0
- codetta/core/onboarding.py +39 -0
- codetta/core/organize.py +490 -0
- codetta/core/plugins.py +183 -0
- codetta/core/presets.py +452 -0
- codetta/core/providers.py +203 -0
- codetta/core/rawtags.py +135 -0
- codetta/core/slskd.py +650 -0
- codetta/core/tagmap.py +538 -0
- codetta/core/undo.py +186 -0
- codetta/core/updates.py +132 -0
- codetta/plugins/__init__.py +63 -0
- codetta/plugins/builtin/__init__.py +3 -0
- codetta/plugins/builtin/tidy.py +70 -0
- codetta/web/__init__.py +0 -0
- codetta/web/app.py +2909 -0
- codetta/web/security.py +170 -0
- codetta/web/static/app.css +2684 -0
- codetta/web/static/app.js +5694 -0
- codetta/web/static/apple-touch-icon.png +0 -0
- codetta/web/static/fonts/LICENSE-Inter.txt +93 -0
- codetta/web/static/fonts/LICENSE-SchibstedGrotesk.txt +93 -0
- codetta/web/static/fonts/inter-cyrillic.woff2 +0 -0
- codetta/web/static/fonts/schibsted-grotesk.woff2 +0 -0
- codetta/web/static/icon.svg +1 -0
- codetta/web/static/icons/codetta-dark.png +0 -0
- codetta/web/static/icons/codetta-dark.svg +1 -0
- codetta/web/static/icons/codetta-dark.webp +0 -0
- codetta/web/static/icons/codetta-light.png +0 -0
- codetta/web/static/icons/codetta-light.svg +1 -0
- codetta/web/static/icons/codetta-light.webp +0 -0
- codetta/web/static/icons/codetta.png +0 -0
- codetta/web/static/icons/codetta.svg +1 -0
- codetta/web/static/icons/codetta.webp +0 -0
- codetta/web/static/index.html +473 -0
- codetta/web/static/theme.js +12 -0
- codetta-0.8.1.dist-info/METADATA +282 -0
- codetta-0.8.1.dist-info/RECORD +63 -0
- codetta-0.8.1.dist-info/WHEEL +5 -0
- codetta-0.8.1.dist-info/entry_points.txt +2 -0
- codetta-0.8.1.dist-info/licenses/LICENSE +661 -0
- codetta-0.8.1.dist-info/top_level.txt +1 -0
codetta/core/inbox.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Import inbox: a folder where new music lands (downloads, rips) before it
|
|
2
|
+
joins the library.
|
|
3
|
+
|
|
4
|
+
Codetta groups what's in it into albums, finds each album on MusicBrainz
|
|
5
|
+
(by its tags, or by sound when AcoustID is on), and either files it straight
|
|
6
|
+
away (confident matches, if the user allows it) or waits for a review.
|
|
7
|
+
"Filing" an album = writing the MusicBrainz tags + organizing it into the
|
|
8
|
+
library with the saved pattern, each an ordinary undoable activity entry.
|
|
9
|
+
|
|
10
|
+
This module only knows about groups and their state; the matching and filing
|
|
11
|
+
use the same code as "Find in MusicBrainz" and "Organize" (see web/app.py).
|
|
12
|
+
State lives in ``inbox.json`` in the config folder.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import hashlib
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
import tempfile
|
|
22
|
+
import threading
|
|
23
|
+
import time
|
|
24
|
+
from dataclasses import asdict, dataclass, field
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
from codetta.core.library import Library
|
|
28
|
+
|
|
29
|
+
# Statuses an album in the inbox can have.
|
|
30
|
+
DOWNLOADING = "downloading" # files changed moments ago (still arriving): wait before looking it up
|
|
31
|
+
WAITING = "waiting" # found, not looked up yet
|
|
32
|
+
MATCHING = "matching"
|
|
33
|
+
READY = "ready" # confident match: can be filed in one tap (or automatically)
|
|
34
|
+
REVIEW = "review" # a match that needs a look
|
|
35
|
+
NO_MATCH = "nomatch"
|
|
36
|
+
LOCAL = "local" # online lookups are off: file it by its own tags
|
|
37
|
+
FILED = "filed"
|
|
38
|
+
SKIPPED = "skipped"
|
|
39
|
+
ERROR = "error"
|
|
40
|
+
|
|
41
|
+
DISC_FOLDER = re.compile(r"^(cd|disc|disk)[\s_.-]*\d+\b", re.IGNORECASE)
|
|
42
|
+
MAX_GROUPS = 500
|
|
43
|
+
MAX_SCAN_FOLDERS = 5000 # one look at the inbox never walks more folders than this (e.g. a symlink to "/")
|
|
44
|
+
SETTLE_SECONDS = 120 # an album is looked up once its files have been still this long
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class InboxSettings:
|
|
49
|
+
folder: str = "" # relative to the music folder; "" = not set up
|
|
50
|
+
auto: bool = False # file confident matches without asking
|
|
51
|
+
organize: bool = True # move filed albums into the library with the Organize pattern
|
|
52
|
+
watch: bool = False # check the inbox every 15 minutes (off: only when you open it)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class Group:
|
|
57
|
+
key: str # the album's folder (relative to the music folder)
|
|
58
|
+
paths: list[str]
|
|
59
|
+
signature: str # changes when files are added, removed or modified
|
|
60
|
+
status: str = WAITING
|
|
61
|
+
title: str = "" # best guess from tags or the match
|
|
62
|
+
artist: str = ""
|
|
63
|
+
release: dict | None = None # summary of the chosen MusicBrainz release
|
|
64
|
+
score: int | None = None
|
|
65
|
+
recordings: dict[str, list[str]] = field(default_factory=dict) # from Identify by sound
|
|
66
|
+
note: str = ""
|
|
67
|
+
dest: str = "" # where it was filed (a library folder)
|
|
68
|
+
source: str = "" # the Soulseek job that brought it ("" = it just appeared)
|
|
69
|
+
upgrade: str = "" # a library folder it replaces when filed (Find in slskd on an album)
|
|
70
|
+
updated: float = 0.0
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def find_groups(library: Library, inbox: str, limit: int = MAX_GROUPS) -> list[tuple[str, list[str]]]:
|
|
74
|
+
"""Albums in the inbox: each folder that holds audio files is one album,
|
|
75
|
+
except disc folders ("CD1", "Disc 2"), which join their parent. Loose
|
|
76
|
+
files right in the inbox are one item each."""
|
|
77
|
+
groups: dict[str, list[str]] = {}
|
|
78
|
+
pending = [inbox]
|
|
79
|
+
seen = 0
|
|
80
|
+
while pending and len(groups) <= limit and seen < MAX_SCAN_FOLDERS:
|
|
81
|
+
listing = library.list_folder(pending.pop(0))
|
|
82
|
+
seen += 1
|
|
83
|
+
if listing.files and listing.path == inbox:
|
|
84
|
+
for rel in listing.files: # loose tracks: usually unrelated singles, so one item each
|
|
85
|
+
groups[rel] = [rel]
|
|
86
|
+
elif listing.files:
|
|
87
|
+
name = listing.path.rsplit("/", 1)[-1]
|
|
88
|
+
parent = listing.path.rsplit("/", 1)[0] if "/" in listing.path else ""
|
|
89
|
+
key = parent if DISC_FOLDER.match(name) and listing.path != inbox and parent else listing.path
|
|
90
|
+
groups.setdefault(key, []).extend(listing.files)
|
|
91
|
+
pending.extend(listing.folders)
|
|
92
|
+
return sorted(groups.items())[:limit]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def signature(library: Library, paths: list[str]) -> tuple[str, float]:
|
|
96
|
+
"""(fingerprint of names, sizes and times; when the newest file last changed)."""
|
|
97
|
+
h = hashlib.sha256()
|
|
98
|
+
newest = 0.0
|
|
99
|
+
for rel in sorted(paths):
|
|
100
|
+
try:
|
|
101
|
+
st = library.resolve(rel).stat()
|
|
102
|
+
h.update(f"{rel}\0{st.st_size}\0{st.st_mtime_ns}\n".encode())
|
|
103
|
+
# ctime too: a downloader that moves a finished file in keeps its old mtime.
|
|
104
|
+
newest = max(newest, st.st_mtime, st.st_ctime)
|
|
105
|
+
except OSError:
|
|
106
|
+
h.update(f"{rel}\0missing\n".encode())
|
|
107
|
+
return h.hexdigest()[:16], newest
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class InboxStore:
|
|
111
|
+
"""Settings and per-album state, saved to inbox.json."""
|
|
112
|
+
|
|
113
|
+
def __init__(self, file: Path, settle_seconds: float = SETTLE_SECONDS) -> None:
|
|
114
|
+
self.file = file
|
|
115
|
+
self.settle_seconds = settle_seconds
|
|
116
|
+
self.lock = threading.RLock()
|
|
117
|
+
self.settings = InboxSettings()
|
|
118
|
+
self.groups: dict[str, Group] = {}
|
|
119
|
+
self._load()
|
|
120
|
+
|
|
121
|
+
def _load(self) -> None:
|
|
122
|
+
try:
|
|
123
|
+
data = json.loads(self.file.read_text("utf-8"))
|
|
124
|
+
except (OSError, ValueError):
|
|
125
|
+
return
|
|
126
|
+
s = data.get("settings") if isinstance(data, dict) else None
|
|
127
|
+
if isinstance(s, dict):
|
|
128
|
+
self.settings = InboxSettings(
|
|
129
|
+
folder=str(s.get("folder") or ""), auto=bool(s.get("auto")),
|
|
130
|
+
organize=s.get("organize") is not False, watch=bool(s.get("watch")),
|
|
131
|
+
)
|
|
132
|
+
for g in (data.get("groups") or {}).values() if isinstance(data, dict) else []:
|
|
133
|
+
try:
|
|
134
|
+
group = Group(**{k: v for k, v in g.items() if k in Group.__dataclass_fields__})
|
|
135
|
+
except TypeError:
|
|
136
|
+
continue
|
|
137
|
+
if group.status == MATCHING:
|
|
138
|
+
group.status = WAITING # interrupted by a restart
|
|
139
|
+
self.groups[group.key] = group
|
|
140
|
+
|
|
141
|
+
def save(self) -> None:
|
|
142
|
+
with self.lock:
|
|
143
|
+
data = {"settings": asdict(self.settings), "groups": {k: asdict(g) for k, g in self.groups.items()}}
|
|
144
|
+
self.file.parent.mkdir(parents=True, exist_ok=True)
|
|
145
|
+
fd, tmp = tempfile.mkstemp(prefix=".inbox-", dir=self.file.parent)
|
|
146
|
+
try:
|
|
147
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
148
|
+
json.dump(data, f, ensure_ascii=False)
|
|
149
|
+
os.replace(tmp, self.file)
|
|
150
|
+
except BaseException:
|
|
151
|
+
Path(tmp).unlink(missing_ok=True)
|
|
152
|
+
raise
|
|
153
|
+
|
|
154
|
+
def sync(self, library: Library) -> list[Group]:
|
|
155
|
+
"""Re-read the inbox folder: new albums appear, changed ones are looked
|
|
156
|
+
up again, albums that left the inbox are forgotten (unless filed)."""
|
|
157
|
+
with self.lock:
|
|
158
|
+
inbox = self.settings.folder
|
|
159
|
+
if not inbox or not library.resolve(inbox).is_dir():
|
|
160
|
+
return []
|
|
161
|
+
found = find_groups(library, inbox)
|
|
162
|
+
keys = set()
|
|
163
|
+
now = time.time()
|
|
164
|
+
for key, paths in found:
|
|
165
|
+
keys.add(key)
|
|
166
|
+
sig, newest = signature(library, paths)
|
|
167
|
+
settled = now - newest >= self.settle_seconds
|
|
168
|
+
g = self.groups.get(key)
|
|
169
|
+
if g is None or (g.signature != sig and g.status not in (SKIPPED, FILED)):
|
|
170
|
+
self.groups[key] = Group(key=key, paths=paths, signature=sig, updated=now,
|
|
171
|
+
status=WAITING if settled else DOWNLOADING,
|
|
172
|
+
note="" if settled else "Still arriving: waiting for the download to finish")
|
|
173
|
+
else:
|
|
174
|
+
g.paths = paths
|
|
175
|
+
if g.status == DOWNLOADING and settled:
|
|
176
|
+
g.status, g.note = WAITING, ""
|
|
177
|
+
for key in [k for k, g in self.groups.items() if k not in keys and g.status != FILED]:
|
|
178
|
+
del self.groups[key]
|
|
179
|
+
# Filed albums are kept for a while so the list can say where they went.
|
|
180
|
+
filed = sorted((g for g in self.groups.values() if g.status == FILED), key=lambda g: -g.updated)
|
|
181
|
+
for g in filed[50:]:
|
|
182
|
+
del self.groups[g.key]
|
|
183
|
+
self.save()
|
|
184
|
+
return list(self.groups.values())
|
|
185
|
+
|
|
186
|
+
def update(self, key: str, **changes) -> Group | None:
|
|
187
|
+
with self.lock:
|
|
188
|
+
g = self.groups.get(key)
|
|
189
|
+
if g is None:
|
|
190
|
+
return None
|
|
191
|
+
for k, v in changes.items():
|
|
192
|
+
setattr(g, k, v)
|
|
193
|
+
g.updated = time.time()
|
|
194
|
+
self.save()
|
|
195
|
+
return g
|