Implemented traktor utility fixes

This commit is contained in:
patben8 committed 2026-10-01 10:54:27 +02:00
1 parent 08c53e1150
commit 2c02019bb2
5 files changed
+150 -2

No files matched your search

+96
View File
@@ -515,6 +515,56 @@ with tempfile.TemporaryDirectory() as d:
check("H2: old playlist PRIMARYKEY gone", old_key not in all_pl_keys)
check("H2: new playlist PRIMARYKEY present", new_key in all_pl_keys)
# H3: a remap re-keys the entry index, so the track is still reachable by its
# NEW id before any save — and a tag edit made BEFORE the remap still reaches
# the file on save (it was silently dropped when the index kept the old key).
with tempfile.TemporaryDirectory() as d:
work = Path(d) / "collection.nml"
shutil.copy2(REAL, work)
store = TraktorStore(work)
target = next(e for e in store._nml.collection.entry if e.location and e.location.file)
old_key = _key(target.location)
from_prefix = str(resolve_path(target.location.volume, target.location.dir, target.location.file))
to_prefix = "/Volumes/KONDUKTOR_TEST_H3/remapped__rekey__one.mp3"
new_key = "".join(os_path_to_location(Path(to_prefix)))
store.set_track_metadata(old_key, {"genre": "H3 before remap"})
store.set_track_art(old_key, b"\xff\xd8H3", "image/jpeg")
moved = store.remap_locations(PathMapping.make(from_prefix, to_prefix))
check("H3: the remap reports the new id", moved.get(old_key) == new_key, str(moved))
check("H3: the index finds the ENTRY by its new id", store._entry_by_key.get(new_key) is target)
check("H3: …and no longer by its old one", old_key not in store._entry_by_key)
check("H3: staged cover art follows the track", new_key in store._track_art and old_key not in store._track_art)
try:
store.set_track_metadata(new_key, {"label": "H3 after remap"})
check("H3: an edit by the new id works before saving", target.info.label == "H3 after remap")
except PlaylistError as exc:
check("H3: an edit by the new id works before saving", False, str(exc))
# The tag sync must visit the moved track with BOTH edits. Stub the file
# side: this is about which tracks it reaches, not about mutagen.
import konduktor.core.audio_tags as _tags # noqa: E402
written = []
real_write_tags, real_write_cover = _tags.write_tags, _tags.write_cover
ok = SimpleNamespace(ok=True, status="ok", detail=None)
_tags.write_tags = lambda path, meta, **k: (written.append(("tags", meta)), ok)[1]
_tags.write_cover = lambda path, data, mime: (written.append(("cover", data)), ok)[1]
store._resolve = lambda loc: Path(to_prefix)
try:
results = store._sync_file_tags()
finally:
_tags.write_tags, _tags.write_cover = real_write_tags, real_write_cover
tag_meta = next((m for kind, m in written if kind == "tags"), {})
check("H3: the file-tag sync reaches the remapped track",
[r.track_id for r in results] == [new_key, new_key], str([r.track_id for r in results]))
check("H3: …with the edit made before the remap AND the one after",
tag_meta.get("genre") == "H3 before remap" and tag_meta.get("label") == "H3 after remap",
str(tag_meta))
check("H3: …and the staged cover art", ("cover", b"\xff\xd8H3") in written)
# ---- Invariant I: flexible (multi-marker) beatgrids round-trip ---------
print("== I. flexible beatgrid: add/move/delete markers, localized + reversible ==")
@@ -736,5 +786,51 @@ with tempfile.TemporaryDirectory() as d:
all((n.playlist.entries or 0) == len(n.playlist.entry or []) for n in pl_nodes(re)))
# ---- Invariant M: dates cross in Traktor's own form -----------------------
# The projection turns Traktor's "YYYY/M/D" into ISO; everything that writes a
# date back must turn it round again, or the NML ends up holding a notation
# Traktor never writes.
print("== M. dates are written as Traktor's YYYY/M/D, tags get ISO ==")
from konduktor.adapters.traktor.projection import iso_date, traktor_date # noqa: E402
for given, want in [
("2021-03-04", "2021/3/4"), ("2021-12-25", "2021/12/25"), ("2021", "2021/1/1"),
("2021-07", "2021/7/1"), ("2021-03-04T10:20:30", "2021/3/4"), ("2021/03/04", "2021/3/4"),
("2021/3/4", "2021/3/4"), ("sometime", "sometime"), ("", None), (None, None),
]:
check(f"M: traktor_date({given!r}) == {want!r}", traktor_date(given) == want, repr(traktor_date(given)))
check("M: every date in the collection round-trips through ISO",
all(traktor_date(iso_date(v)) == v
for e in TraktorCollection(path=REAL).nml.collection.entry if e.info
for v in (e.info.release_date, e.info.import_date, e.info.last_played) if v))
with tempfile.TemporaryDirectory() as d:
work = Path(d) / "collection.nml"
shutil.copy2(REAL, work)
store = TraktorStore(work)
target = next(e for e in store._nml.collection.entry
if e.location and e.info and e.info.release_date)
key = store._key_of(target)
original = store._render()
store.set_track_metadata(key, {"release_date": "2019-06-07"})
check("M: an ISO edit is stored as Traktor's form", target.info.release_date == "2019/6/7",
target.info.release_date)
check("M: …and the file tag is given ISO",
store._entry_field_value(target, "release_date") == "2019-06-07")
changed = [dl for dl in difflib.unified_diff(tag_lines(original), tag_lines(store._render()), n=0)
if dl[:1] in "+-" and dl[:3] not in ("+++", "---")]
check("M: only that ENTRY's INFO line changes", len(changed) == 2 and "RELEASE_DATE=\"2019/6/7\"" in changed[1],
"\n".join(changed[:4]))
new_id = store.add_entry(
SimpleNamespace(title="M dates", release_date="2020", import_date="2026-09-30"),
Path("/Volumes/KONDUKTOR_TEST_M/dates.mp3"),
)
info = store._entry_by_key[new_id].info
check("M: an added track's dates are Traktor's form",
(info.release_date, info.import_date) == ("2020/1/1", "2026/9/30"),
f"{info.release_date!r} {info.import_date!r}")
print("\nRESULT:", "FAILED" if failed else "ALL PASSED")
sys.exit(1 if failed else 0)