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
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Notifications: things Codetta did or noticed on its own, in the background.
|
|
2
|
+
|
|
3
|
+
Things *you* do (save, organize, a MusicBrainz match) already answer with a
|
|
4
|
+
toast and land in the activity log. Notifications are for the rest: the inbox
|
|
5
|
+
finished looking things up or filed an album by itself, a new version of
|
|
6
|
+
Codetta is out, the library index finished its first scan...
|
|
7
|
+
|
|
8
|
+
Kept in ``notifications.json`` in the config folder (the newest 200).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import os
|
|
15
|
+
import tempfile
|
|
16
|
+
import threading
|
|
17
|
+
import time
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
KEEP = 200
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Notifications:
|
|
24
|
+
def __init__(self, file: Path) -> None:
|
|
25
|
+
self.file = file
|
|
26
|
+
self._lock = threading.Lock()
|
|
27
|
+
self._items: list[dict] = []
|
|
28
|
+
self._next = 1
|
|
29
|
+
try:
|
|
30
|
+
data = json.loads(file.read_text("utf-8"))
|
|
31
|
+
if isinstance(data, list):
|
|
32
|
+
self._items = [n for n in data if isinstance(n, dict) and isinstance(n.get("id"), int)][-KEEP:]
|
|
33
|
+
self._next = max((n["id"] for n in self._items), default=0) + 1
|
|
34
|
+
except (OSError, ValueError):
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
def _save(self) -> None:
|
|
38
|
+
try:
|
|
39
|
+
self.file.parent.mkdir(parents=True, exist_ok=True)
|
|
40
|
+
fd, tmp = tempfile.mkstemp(prefix=".notifications-", dir=self.file.parent)
|
|
41
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
42
|
+
json.dump(self._items, f, ensure_ascii=False)
|
|
43
|
+
os.replace(tmp, self.file)
|
|
44
|
+
except OSError:
|
|
45
|
+
pass # a read-only config folder: notifications still work until restart
|
|
46
|
+
|
|
47
|
+
def add(self, kind: str, title: str, body: str = "", action: dict | None = None, key: str | None = None) -> dict | None:
|
|
48
|
+
"""Add one. ``key`` makes it once-only (e.g. "update:0.6.0" is never repeated).
|
|
49
|
+
``action`` tells the page what tapping it does: {"open": "inbox"|"news"|"folder", "path": ...}."""
|
|
50
|
+
with self._lock:
|
|
51
|
+
if key and any(n.get("key") == key for n in self._items):
|
|
52
|
+
return None
|
|
53
|
+
n = {"id": self._next, "time": int(time.time()), "kind": kind, "title": title[:200], "body": body[:1000],
|
|
54
|
+
"action": action or None, "key": key, "read": False}
|
|
55
|
+
self._next += 1
|
|
56
|
+
self._items.append(n)
|
|
57
|
+
del self._items[:-KEEP]
|
|
58
|
+
self._save()
|
|
59
|
+
return n
|
|
60
|
+
|
|
61
|
+
def recent(self, after_id: int = 0, limit: int = 50) -> dict:
|
|
62
|
+
with self._lock:
|
|
63
|
+
items = [n for n in self._items if n["id"] > after_id][-limit:]
|
|
64
|
+
unread = sum(not n["read"] for n in self._items)
|
|
65
|
+
return {"items": list(reversed(items)), "unread": unread, "last_id": self._next - 1}
|
|
66
|
+
|
|
67
|
+
def mark_read(self, ids: list[int] | None = None) -> None:
|
|
68
|
+
with self._lock:
|
|
69
|
+
for n in self._items:
|
|
70
|
+
if ids is None or n["id"] in ids:
|
|
71
|
+
n["read"] = True
|
|
72
|
+
self._save()
|
|
73
|
+
|
|
74
|
+
def clear(self) -> None:
|
|
75
|
+
with self._lock:
|
|
76
|
+
self._items = []
|
|
77
|
+
self._save()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""First-run setup: which steps someone has been through.
|
|
2
|
+
|
|
3
|
+
Each core feature that needs a choice gets a step. New installs see them all
|
|
4
|
+
after creating the account; existing installs see only the steps added since
|
|
5
|
+
(once). Steps can always be skipped and changed later.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import tempfile
|
|
13
|
+
import threading
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
STEPS = ("connections", "organize", "inbox", "health", "slskd") # in the order they're shown
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class OnboardingStore:
|
|
20
|
+
def __init__(self, file: Path) -> None:
|
|
21
|
+
self.file = file
|
|
22
|
+
self._lock = threading.Lock()
|
|
23
|
+
try:
|
|
24
|
+
data = json.loads(file.read_text("utf-8"))
|
|
25
|
+
done = data.get("done") if isinstance(data, dict) else None
|
|
26
|
+
self.done = {s for s in done if s in STEPS} if isinstance(done, list) else set()
|
|
27
|
+
except (OSError, ValueError):
|
|
28
|
+
self.done = set()
|
|
29
|
+
|
|
30
|
+
def mark(self, step: str) -> None:
|
|
31
|
+
if step not in STEPS:
|
|
32
|
+
raise ValueError(f"Unknown step: {step}")
|
|
33
|
+
with self._lock:
|
|
34
|
+
self.done.add(step)
|
|
35
|
+
self.file.parent.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
fd, tmp = tempfile.mkstemp(prefix=".onboarding-", dir=self.file.parent)
|
|
37
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
38
|
+
json.dump({"done": sorted(self.done)}, f)
|
|
39
|
+
os.replace(tmp, self.file)
|
codetta/core/organize.py
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
"""Organize: rename and move files into folders from their tags.
|
|
2
|
+
|
|
3
|
+
A *pattern* describes where a file belongs, relative to the music folder:
|
|
4
|
+
|
|
5
|
+
{albumartist|artist}/{album}[ ({year})]/{disctrack} {title}
|
|
6
|
+
|
|
7
|
+
- ``{name}`` is a tag (or one of the extras below); ``{a|b}`` uses the first
|
|
8
|
+
one that has a value.
|
|
9
|
+
- ``[...]`` is optional: it disappears when a tag inside it is empty, so
|
|
10
|
+
``{album}[ ({year})]`` gives "Abbey Road (1969)" or just "Abbey Road".
|
|
11
|
+
- ``/`` starts a new folder. The file extension is added automatically.
|
|
12
|
+
|
|
13
|
+
Extras: ``{track}`` ("01"), ``{disc}`` ("2"), ``{disctrack}`` ("01", or
|
|
14
|
+
"2-01" on multi-disc albums), ``{year}``, ``{ext}``/``{format}``, ``{filename}``
|
|
15
|
+
(the current name, without extension).
|
|
16
|
+
|
|
17
|
+
Nothing here touches a file until ``apply_moves``; ``plan_moves`` only works
|
|
18
|
+
out where everything would go, and why something can't move. Moves never
|
|
19
|
+
overwrite: a name that's already taken is reported as a conflict. Undo records
|
|
20
|
+
every move (see ``undo_moves``).
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
import shutil
|
|
28
|
+
import threading
|
|
29
|
+
import unicodedata
|
|
30
|
+
from collections.abc import Callable, Iterable
|
|
31
|
+
from dataclasses import dataclass, field
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
from codetta.core.engine import SUPPORTED_EXTENSIONS, file_lock
|
|
35
|
+
|
|
36
|
+
PRESETS: tuple[tuple[str, str], ...] = (
|
|
37
|
+
("Artist / Album (Year) / 01 Title", "{albumartist|artist}/{album}[ ({year})]/{disctrack} {title}"),
|
|
38
|
+
("Artist / Album / 01 - Title", "{albumartist|artist}/{album}/{disctrack} - {title}"),
|
|
39
|
+
("Artist / Year - Album / 01. Title", "{albumartist|artist}/[{year} - ]{album}/{disctrack}. {title}"),
|
|
40
|
+
("Artist / Album / Artist - Title", "{albumartist|artist}/{album}/{disctrack} {artist} - {title}"),
|
|
41
|
+
)
|
|
42
|
+
DEFAULT_PATTERN = PRESETS[0][1]
|
|
43
|
+
MAX_PATTERN = 300
|
|
44
|
+
MAX_COMPONENT_BYTES = 200 # most filesystems allow 255 bytes per name; leave room for " (2)"
|
|
45
|
+
# Values used when a pattern's tag is empty, so a file never lands in "/".
|
|
46
|
+
FALLBACKS = {"albumartist": "Unknown Artist", "artist": "Unknown Artist", "album": "Unknown Album"}
|
|
47
|
+
SIDECAR_SUFFIXES = (".lrc",) # follow their audio file when renamed
|
|
48
|
+
_TOKEN = re.compile(r"\{([^{}]*)\}")
|
|
49
|
+
_OPTIONAL = re.compile(r"\[([^\[\]]*)\]")
|
|
50
|
+
_UNSAFE = re.compile(r'[<>:"/\\|?*\x00-\x1f\x7f]')
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class PatternError(ValueError):
|
|
54
|
+
"""The pattern can't be used (empty, unbalanced, or would leave the library)."""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def check_pattern(pattern: str) -> str:
|
|
58
|
+
pattern = (pattern or "").strip().strip("/")
|
|
59
|
+
if not pattern:
|
|
60
|
+
raise PatternError("Choose a pattern first.")
|
|
61
|
+
if len(pattern) > MAX_PATTERN:
|
|
62
|
+
raise PatternError(f"The pattern is too long (max {MAX_PATTERN} characters).")
|
|
63
|
+
if pattern.count("{") != pattern.count("}") or pattern.count("[") != pattern.count("]"):
|
|
64
|
+
raise PatternError("Every { needs a } and every [ needs a ].")
|
|
65
|
+
if "\\" in pattern:
|
|
66
|
+
raise PatternError("Use / to separate folders.")
|
|
67
|
+
last = pattern.rsplit("/", 1)[-1]
|
|
68
|
+
if not _TOKEN.search(last):
|
|
69
|
+
raise PatternError("The file name part (after the last /) needs at least one tag, like {title}.")
|
|
70
|
+
return pattern
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def safe_name(text: str, limit: int = MAX_COMPONENT_BYTES) -> str:
|
|
74
|
+
"""One folder or file name that works on Linux, SMB shares, Windows and macOS."""
|
|
75
|
+
text = unicodedata.normalize("NFC", text)
|
|
76
|
+
text = " ".join(text.split()) # tabs, newlines, runs of spaces
|
|
77
|
+
text = _UNSAFE.sub("_", text)
|
|
78
|
+
text = text.strip().rstrip(".").strip() # Windows drops trailing dots and spaces
|
|
79
|
+
if text.startswith("."):
|
|
80
|
+
text = "_" + text[1:] # never a hidden file
|
|
81
|
+
if text.startswith("@"):
|
|
82
|
+
# "@inbox" / "@replaced" are Codetta's own folder shortcuts: a tag (say, from a
|
|
83
|
+
# downloaded file) must never steer files there. A full-width @ looks the same.
|
|
84
|
+
text = "\uff20" + text[1:]
|
|
85
|
+
raw = text.encode("utf-8")
|
|
86
|
+
if len(raw) > limit:
|
|
87
|
+
text = raw[:limit].decode("utf-8", "ignore").rstrip(" .")
|
|
88
|
+
return text or "_"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _first(tags: dict[str, list[str]], key: str) -> str:
|
|
92
|
+
for v in tags.get(key, []):
|
|
93
|
+
if v.strip():
|
|
94
|
+
return v.strip()
|
|
95
|
+
return ""
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _number(value: str) -> int | None:
|
|
99
|
+
m = re.match(r"^\s*(\d+)", value or "")
|
|
100
|
+
return int(m.group(1)) if m else None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _total(value: str) -> int | None:
|
|
104
|
+
m = re.match(r"^\s*\d+\s*/\s*(\d+)", value or "")
|
|
105
|
+
return int(m.group(1)) if m else None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@dataclass
|
|
109
|
+
class Source:
|
|
110
|
+
"""One audio file and what the pattern needs to know about it."""
|
|
111
|
+
|
|
112
|
+
rel: str
|
|
113
|
+
tags: dict[str, list[str]]
|
|
114
|
+
format: str = ""
|
|
115
|
+
|
|
116
|
+
def value(self, key: str, multi_disc: bool) -> str:
|
|
117
|
+
key = key.strip().lower()
|
|
118
|
+
stem, ext = os.path.splitext(self.rel.rsplit("/", 1)[-1])
|
|
119
|
+
if key == "track":
|
|
120
|
+
n = _number(_first(self.tags, "tracknumber"))
|
|
121
|
+
return f"{n:02d}" if n is not None else ""
|
|
122
|
+
if key == "disc":
|
|
123
|
+
n = _number(_first(self.tags, "discnumber"))
|
|
124
|
+
return str(n) if n is not None else ""
|
|
125
|
+
if key == "disctrack":
|
|
126
|
+
track = self.value("track", multi_disc)
|
|
127
|
+
disc = self.value("disc", multi_disc)
|
|
128
|
+
if not track:
|
|
129
|
+
return ""
|
|
130
|
+
return f"{disc}-{track}" if multi_disc and disc else track
|
|
131
|
+
if key == "year":
|
|
132
|
+
m = re.match(r"^\s*(\d{4})", _first(self.tags, "date") or _first(self.tags, "originaldate"))
|
|
133
|
+
return m.group(1) if m else ""
|
|
134
|
+
if key == "originalyear":
|
|
135
|
+
m = re.match(r"^\s*(\d{4})", _first(self.tags, "originaldate"))
|
|
136
|
+
return m.group(1) if m else ""
|
|
137
|
+
if key in ("ext", "format"):
|
|
138
|
+
return ext.lstrip(".").lower()
|
|
139
|
+
if key == "filename":
|
|
140
|
+
return stem
|
|
141
|
+
return _first(self.tags, key)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _token_value(src: Source, expr: str, multi_disc: bool) -> str:
|
|
145
|
+
for alt in expr.split("|"):
|
|
146
|
+
v = src.value(alt, multi_disc)
|
|
147
|
+
if v:
|
|
148
|
+
return v
|
|
149
|
+
return ""
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def render(pattern: str, src: Source, multi_disc: bool = False) -> str:
|
|
153
|
+
"""The library-relative path ``src`` would get (extension included)."""
|
|
154
|
+
pattern = check_pattern(pattern)
|
|
155
|
+
|
|
156
|
+
def optional(m: re.Match) -> str:
|
|
157
|
+
part = m.group(1)
|
|
158
|
+
tokens = _TOKEN.findall(part)
|
|
159
|
+
if any(not _token_value(src, t, multi_disc) for t in tokens):
|
|
160
|
+
return ""
|
|
161
|
+
return part
|
|
162
|
+
|
|
163
|
+
expanded_parts = []
|
|
164
|
+
for component in _OPTIONAL.sub(optional, pattern).split("/"):
|
|
165
|
+
def fill(m: re.Match) -> str:
|
|
166
|
+
expr = m.group(1)
|
|
167
|
+
v = _token_value(src, expr, multi_disc)
|
|
168
|
+
if not v:
|
|
169
|
+
first_key = expr.split("|")[0].strip().lower()
|
|
170
|
+
v = FALLBACKS.get(first_key, "")
|
|
171
|
+
if not v and first_key == "title":
|
|
172
|
+
v = src.value("filename", multi_disc)
|
|
173
|
+
return v.replace("/", "_").replace("\\", "_")
|
|
174
|
+
|
|
175
|
+
expanded_parts.append(_TOKEN.sub(fill, component))
|
|
176
|
+
ext = os.path.splitext(src.rel)[1].lower()
|
|
177
|
+
folders = [safe_name(p) for p in expanded_parts[:-1] if p.strip()]
|
|
178
|
+
name = safe_name(expanded_parts[-1], MAX_COMPONENT_BYTES - len(ext.encode()))
|
|
179
|
+
return "/".join([*folders, name + ext])
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def album_key(src: Source) -> tuple[str, str]:
|
|
183
|
+
return (_first(src.tags, "albumartist") or _first(src.tags, "artist")).casefold(), _first(src.tags, "album").casefold()
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def multi_disc_albums(sources: Iterable[Source]) -> set[tuple[str, str]]:
|
|
187
|
+
"""Albums (by album artist + album) that span more than one disc."""
|
|
188
|
+
out = set()
|
|
189
|
+
for s in sources:
|
|
190
|
+
disc = _first(s.tags, "discnumber")
|
|
191
|
+
if (_number(disc) or 1) > 1 or (_total(disc) or 1) > 1:
|
|
192
|
+
out.add(album_key(s))
|
|
193
|
+
return out
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def load_pattern(file: Path) -> str:
|
|
197
|
+
"""The pattern saved in the config folder, or the default."""
|
|
198
|
+
import json
|
|
199
|
+
|
|
200
|
+
try:
|
|
201
|
+
pattern = json.loads(file.read_text("utf-8")).get("pattern")
|
|
202
|
+
return check_pattern(pattern) if isinstance(pattern, str) else DEFAULT_PATTERN
|
|
203
|
+
except (OSError, ValueError, AttributeError):
|
|
204
|
+
return DEFAULT_PATTERN
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def save_pattern(file: Path, pattern: str) -> None:
|
|
208
|
+
import json
|
|
209
|
+
import tempfile
|
|
210
|
+
|
|
211
|
+
pattern = check_pattern(pattern)
|
|
212
|
+
file.parent.mkdir(parents=True, exist_ok=True)
|
|
213
|
+
fd, tmp = tempfile.mkstemp(prefix=".organize-", dir=file.parent)
|
|
214
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
215
|
+
json.dump({"pattern": pattern}, f, indent=2)
|
|
216
|
+
os.replace(tmp, file)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
Root = Path | Callable[[str], Path] # the music folder, or a resolver that also knows "@inbox/..."
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _at(root: Root, rel: str) -> Path:
|
|
224
|
+
if callable(root):
|
|
225
|
+
return root(rel)
|
|
226
|
+
return root / rel if rel else root
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _is_top(folder: str) -> bool:
|
|
230
|
+
"""The music folder itself, or a root alias like "@inbox": never removed."""
|
|
231
|
+
return folder == "" or ("/" not in folder and folder.startswith("@"))
|
|
232
|
+
|
|
233
|
+
# --- Planning -------------------------------------------------------------------------------
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@dataclass
|
|
237
|
+
class Move:
|
|
238
|
+
src: str
|
|
239
|
+
dst: str
|
|
240
|
+
status: str # "move", "same" (already there), "conflict", "error"
|
|
241
|
+
reason: str = ""
|
|
242
|
+
companions: list[tuple[str, str]] = field(default_factory=list) # (src, dst) carried along
|
|
243
|
+
|
|
244
|
+
def to_dict(self) -> dict:
|
|
245
|
+
return {"src": self.src, "dst": self.dst, "status": self.status, "reason": self.reason,
|
|
246
|
+
"companions": [{"src": a, "dst": b} for a, b in self.companions]}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _same_file(a: Path, b: Path) -> bool:
|
|
250
|
+
try:
|
|
251
|
+
return os.path.samefile(a, b)
|
|
252
|
+
except OSError:
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _has_audio(folder: Path, limit: int = 2000) -> bool:
|
|
257
|
+
seen = 0
|
|
258
|
+
for _dirpath, _dirs, files in os.walk(folder):
|
|
259
|
+
for f in files:
|
|
260
|
+
seen += 1
|
|
261
|
+
if Path(f).suffix.lower() in SUPPORTED_EXTENSIONS or seen > limit:
|
|
262
|
+
return True
|
|
263
|
+
return False
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def plan_moves(root: Root, sources: list[Source], pattern: str) -> list[Move]:
|
|
267
|
+
"""Where each file would go. Reads nothing but names; writes nothing."""
|
|
268
|
+
check_pattern(pattern)
|
|
269
|
+
multi = multi_disc_albums(sources)
|
|
270
|
+
moves: list[Move] = []
|
|
271
|
+
claimed: dict[str, str] = {} # destination (case-folded) -> source that takes it
|
|
272
|
+
for s in sources:
|
|
273
|
+
try:
|
|
274
|
+
dst = render(pattern, s, album_key(s) in multi)
|
|
275
|
+
except PatternError as exc:
|
|
276
|
+
moves.append(Move(s.rel, s.rel, "error", str(exc)))
|
|
277
|
+
continue
|
|
278
|
+
src_path, dst_path = _at(root, s.rel), _at(root, dst)
|
|
279
|
+
if dst == s.rel:
|
|
280
|
+
moves.append(Move(s.rel, dst, "same"))
|
|
281
|
+
continue
|
|
282
|
+
key = dst.casefold() # SMB and macOS shares don't tell "a" from "A"
|
|
283
|
+
if key in claimed:
|
|
284
|
+
moves.append(Move(s.rel, dst, "conflict", f"Same new name as {claimed[key].rsplit('/', 1)[-1]}"))
|
|
285
|
+
continue
|
|
286
|
+
if os.path.lexists(dst_path) and not _same_file(src_path, dst_path):
|
|
287
|
+
moves.append(Move(s.rel, dst, "conflict", "A different file already has that name"))
|
|
288
|
+
continue
|
|
289
|
+
claimed[key] = s.rel
|
|
290
|
+
moves.append(Move(s.rel, dst, "move"))
|
|
291
|
+
|
|
292
|
+
taken = {m.dst.casefold() for m in moves if m.status in ("move", "same")}
|
|
293
|
+
# Sidecars (song.lrc) follow their audio file.
|
|
294
|
+
for m in moves:
|
|
295
|
+
if m.status != "move":
|
|
296
|
+
continue
|
|
297
|
+
src_stem = os.path.splitext(m.src)[0]
|
|
298
|
+
dst_stem = os.path.splitext(m.dst)[0]
|
|
299
|
+
for suffix in SIDECAR_SUFFIXES:
|
|
300
|
+
side = _at(root, src_stem + suffix)
|
|
301
|
+
new = dst_stem + suffix
|
|
302
|
+
if side.is_file() and not os.path.lexists(_at(root, new)) and new.casefold() not in taken:
|
|
303
|
+
m.companions.append((src_stem + suffix, new))
|
|
304
|
+
taken.add(new.casefold())
|
|
305
|
+
|
|
306
|
+
# The album's other files (cover.jpg, booklet.pdf, logs, scans/) move along
|
|
307
|
+
# when every audio file in their folder goes to one new folder.
|
|
308
|
+
by_folder: dict[str, list[Move]] = {}
|
|
309
|
+
for m in moves:
|
|
310
|
+
by_folder.setdefault(m.src.rsplit("/", 1)[0] if "/" in m.src else "", []).append(m)
|
|
311
|
+
sidecars = {a for m in moves for a, _ in m.companions}
|
|
312
|
+
for folder, group in by_folder.items():
|
|
313
|
+
dests = {m.dst.rsplit("/", 1)[0] if "/" in m.dst else "" for m in group}
|
|
314
|
+
if len(dests) != 1 or any(m.status != "move" for m in group) or _is_top(folder):
|
|
315
|
+
continue
|
|
316
|
+
new_folder = dests.pop()
|
|
317
|
+
if new_folder == folder:
|
|
318
|
+
continue
|
|
319
|
+
audio_here = {m.src for m in group}
|
|
320
|
+
try:
|
|
321
|
+
entries = list(os.scandir(_at(root, folder)))
|
|
322
|
+
except OSError:
|
|
323
|
+
continue
|
|
324
|
+
audio_in_folder = {f"{folder}/{e.name}" for e in entries
|
|
325
|
+
if e.is_file() and Path(e.name).suffix.lower() in SUPPORTED_EXTENSIONS}
|
|
326
|
+
if audio_in_folder - audio_here:
|
|
327
|
+
continue # some audio here isn't part of this move: leave the extras alone
|
|
328
|
+
carrier = group[0]
|
|
329
|
+
for e in sorted(entries, key=lambda e: e.name):
|
|
330
|
+
rel = f"{folder}/{e.name}"
|
|
331
|
+
if e.name.startswith(".") or rel in audio_here or rel in sidecars:
|
|
332
|
+
continue
|
|
333
|
+
if e.is_dir(follow_symlinks=False) and _has_audio(Path(e.path)):
|
|
334
|
+
continue # e.g. "CD2/": its own files move by themselves
|
|
335
|
+
new = f"{new_folder}/{e.name}" if new_folder else e.name
|
|
336
|
+
if os.path.lexists(_at(root, new)) or new.casefold() in taken:
|
|
337
|
+
continue # never overwrite; it just stays where it is
|
|
338
|
+
carrier.companions.append((rel, new))
|
|
339
|
+
taken.add(new.casefold())
|
|
340
|
+
return moves
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
# --- Moving -----------------------------------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
_move_lock = threading.Lock() # one organize at a time: name checks and moves stay consistent
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
@dataclass
|
|
349
|
+
class MoveResult:
|
|
350
|
+
src: str
|
|
351
|
+
dst: str
|
|
352
|
+
ok: bool
|
|
353
|
+
error: str | None = None
|
|
354
|
+
moved: list[tuple[str, str]] = field(default_factory=list) # every (src, dst) actually moved
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _move_one(root: Root, src: str, dst: str) -> None:
|
|
358
|
+
a, b = _at(root, src), _at(root, dst)
|
|
359
|
+
if not os.path.lexists(a):
|
|
360
|
+
raise FileNotFoundError("It's no longer there")
|
|
361
|
+
if os.path.lexists(b) and not _same_file(a, b):
|
|
362
|
+
raise FileExistsError("A different file already has that name")
|
|
363
|
+
b.parent.mkdir(parents=True, exist_ok=True)
|
|
364
|
+
try:
|
|
365
|
+
os.rename(a, b) # same filesystem: instant, and the file itself is never rewritten
|
|
366
|
+
except OSError as exc:
|
|
367
|
+
if exc.errno != 18: # EXDEV: another disk or mount (e.g. the inbox folder)
|
|
368
|
+
raise
|
|
369
|
+
_copy_then_remove(a, b)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _copy_then_remove(a: Path, b: Path) -> None:
|
|
373
|
+
"""Move across disks: copy to a temporary name next to the target, check
|
|
374
|
+
it, give it its real name, and only then delete the original."""
|
|
375
|
+
tmp = b.with_name(f".codetta-move-{os.getpid()}-{b.name}")
|
|
376
|
+
try:
|
|
377
|
+
if a.is_dir() and not a.is_symlink():
|
|
378
|
+
shutil.copytree(a, tmp, symlinks=True)
|
|
379
|
+
if sum(1 for _ in tmp.rglob("*")) != sum(1 for _ in a.rglob("*")):
|
|
380
|
+
raise OSError("The copy came out incomplete")
|
|
381
|
+
else:
|
|
382
|
+
shutil.copy2(a, tmp, follow_symlinks=False)
|
|
383
|
+
if tmp.stat().st_size != a.stat().st_size:
|
|
384
|
+
raise OSError("The copy came out the wrong size")
|
|
385
|
+
if os.path.lexists(b):
|
|
386
|
+
raise FileExistsError("A different file already has that name")
|
|
387
|
+
os.rename(tmp, b)
|
|
388
|
+
except BaseException:
|
|
389
|
+
if tmp.is_dir() and not tmp.is_symlink():
|
|
390
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
391
|
+
else:
|
|
392
|
+
tmp.unlink(missing_ok=True)
|
|
393
|
+
raise
|
|
394
|
+
if a.is_dir() and not a.is_symlink():
|
|
395
|
+
shutil.rmtree(a)
|
|
396
|
+
else:
|
|
397
|
+
a.unlink()
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def remove_empty_dirs(root: Root, folders: Iterable[str]) -> None:
|
|
401
|
+
"""Remove folders a move left empty (and their empty parents), never the
|
|
402
|
+
music folder or the inbox folder themselves."""
|
|
403
|
+
for folder in sorted(set(folders), key=lambda f: -f.count("/")):
|
|
404
|
+
while not _is_top(folder):
|
|
405
|
+
try:
|
|
406
|
+
_at(root, folder).rmdir() # only succeeds when empty
|
|
407
|
+
except OSError:
|
|
408
|
+
break
|
|
409
|
+
folder = folder.rsplit("/", 1)[0] if "/" in folder else ""
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def apply_moves(root: Root, moves: list[Move]) -> list[MoveResult]:
|
|
413
|
+
"""Do the planned moves. Each file is moved on its own; one failure doesn't stop the rest."""
|
|
414
|
+
results = []
|
|
415
|
+
left: list[str] = []
|
|
416
|
+
with _move_lock:
|
|
417
|
+
for m in moves:
|
|
418
|
+
if m.status != "move":
|
|
419
|
+
continue
|
|
420
|
+
r = MoveResult(m.src, m.dst, ok=False)
|
|
421
|
+
try:
|
|
422
|
+
with file_lock(_at(root, m.src)): # never while that file is being saved
|
|
423
|
+
_move_one(root, m.src, m.dst)
|
|
424
|
+
r.ok = True
|
|
425
|
+
r.moved.append((m.src, m.dst))
|
|
426
|
+
left.append(m.src.rsplit("/", 1)[0] if "/" in m.src else "")
|
|
427
|
+
except OSError as exc:
|
|
428
|
+
r.error = exc.strerror if getattr(exc, "strerror", None) else str(exc)
|
|
429
|
+
if r.ok:
|
|
430
|
+
for a, b in m.companions:
|
|
431
|
+
try:
|
|
432
|
+
_move_one(root, a, b)
|
|
433
|
+
r.moved.append((a, b))
|
|
434
|
+
left.append(a.rsplit("/", 1)[0] if "/" in a else "")
|
|
435
|
+
except OSError:
|
|
436
|
+
pass # an extra that can't move stays where it is
|
|
437
|
+
results.append(r)
|
|
438
|
+
remove_empty_dirs(root, left)
|
|
439
|
+
return results
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def undo_moves(root: Root, moves: list[list[str]]) -> tuple[list[tuple[str, str, bool, str | None]], list[list[str]]]:
|
|
443
|
+
"""Move files back. Returns ([(original src, dst, ok, error)], moves done in reverse)."""
|
|
444
|
+
report, reverse, left = [], [], []
|
|
445
|
+
with _move_lock:
|
|
446
|
+
for src, dst in reversed(moves): # companions first, then their audio file, in reverse order
|
|
447
|
+
try:
|
|
448
|
+
if not os.path.lexists(_at(root, dst)):
|
|
449
|
+
raise FileNotFoundError("Moved or deleted since, so it was left alone")
|
|
450
|
+
if os.path.lexists(_at(root, src)):
|
|
451
|
+
raise FileExistsError("Something else now has the old name, so it was left alone")
|
|
452
|
+
with file_lock(_at(root, dst)):
|
|
453
|
+
_move_one(root, dst, src)
|
|
454
|
+
reverse.append([dst, src])
|
|
455
|
+
left.append(dst.rsplit("/", 1)[0] if "/" in dst else "")
|
|
456
|
+
report.append((src, dst, True, None))
|
|
457
|
+
except OSError as exc:
|
|
458
|
+
report.append((src, dst, False, str(exc) if not getattr(exc, "strerror", None) else exc.strerror))
|
|
459
|
+
remove_empty_dirs(root, left)
|
|
460
|
+
reverse.reverse()
|
|
461
|
+
return report, reverse
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
REPLACED_DAYS = 30 # like "Recently Deleted": an album an upgrade replaced is kept this long
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def prune_replaced(folder: Path, days: int = REPLACED_DAYS, now: float | None = None) -> int:
|
|
469
|
+
"""Delete albums set aside by upgrades ("replaced/<YYYYmmdd-HHMMSS>/...") once
|
|
470
|
+
they're older than ``days``. Only ever touches folders named like that. Returns
|
|
471
|
+
how many were removed."""
|
|
472
|
+
import shutil
|
|
473
|
+
import time as _time
|
|
474
|
+
from datetime import datetime as _dt
|
|
475
|
+
|
|
476
|
+
now = _time.time() if now is None else now
|
|
477
|
+
removed = 0
|
|
478
|
+
try:
|
|
479
|
+
entries = list(folder.iterdir())
|
|
480
|
+
except OSError:
|
|
481
|
+
return 0
|
|
482
|
+
for entry in entries:
|
|
483
|
+
try:
|
|
484
|
+
when = _dt.strptime(entry.name, "%Y%m%d-%H%M%S").timestamp()
|
|
485
|
+
except ValueError:
|
|
486
|
+
continue # not one of ours
|
|
487
|
+
if entry.is_dir() and not entry.is_symlink() and now - when > days * 86400:
|
|
488
|
+
shutil.rmtree(entry, ignore_errors=True)
|
|
489
|
+
removed += 1
|
|
490
|
+
return removed
|