Implemented export track grid locking

This commit is contained in:
patben8 committed 2026-10-19 10:48:59 +02:00
1 parent 2821ba124d
commit 9329236955
16 files changed
+196 -19

No files matched your search

@@ -109,6 +109,8 @@ class RekordboxExporter:
computer_only = True
library_filename = "master.db"
drive_root = False
#: `djmdContent.Analysed` bit 0x80 — from `TrackCues.grid_locked`.
locks_grids = True
def capabilities(self):
"""What a Rekordbox library can hold, with no library to read.
@@ -332,7 +334,9 @@ class RekordboxExporter:
"""
rel = new_content.anlz_rel(str(content.UUID))
content.AnalysisDataPath = rel
content.Analysed = 105 # what a rekordbox-analysed track carries
content.Analysed = new_content.ANALYSED
if item.cues is not None and item.cues.grid_locked:
content.Analysed |= new_content.ANALYSED_LOCKED
content.AnalysisUpdated = 1
db.flush()
@@ -34,6 +34,12 @@ from . import artwork
#: only what is written: 0x2C060E.
CONTENT_LINK = 2885134
#: `djmdContent.Analysed` as a rekordbox-analysed track carries it, and the bit
#: rekordbox 7 sets beside it when the grid is LOCKED (the padlock in Grid Edit):
#: measured 2026-10-19 by locking two tracks — 105 -> 233, nothing else changed.
ANALYSED = 105
ANALYSED_LOCKED = 0x80
def anlz_rel(track_uuid: str) -> str:
"""`djmdContent.AnalysisDataPath` for a track. Rekordbox derives it from
@@ -788,7 +788,7 @@ class RekordboxStore(ReplaceGridCommands):
track_uuid = str(content.UUID)
rel = new_content.anlz_rel(track_uuid)
content.AnalysisDataPath = rel
content.Analysed = 105 # what a rekordbox-analysed track carries
content.Analysed = new_content.ANALYSED
content.AnalysisUpdated = 1
files = new_content.anlz_files(audio, measured, [] if with_grid else None)
for suffix, data in files.items():
@@ -242,6 +242,10 @@ class TraktorAdapter:
# invent one outside Auto Grid), and it occupies pad A — which
# silently displaced the imported track's own pad A cue.
self.replace_grid(track_id, markers)
if cues.grid_locked:
# The source's lock, or an export's "Lock beatgrids". Unlocked, an
# added entry (no AUDIO_ID) is re-analysed on its first load.
self._store.set_lock(track_id, True)
slots = self.capabilities().cues.hotcue_slots
taken = {c.slot for c in self.track_cues(track_id).cues if c.slot is not None}
@@ -66,6 +66,8 @@ class TraktorExporter:
menu_order = 20
library_filename = "collection.nml"
drive_root = False
#: `LOCK` — honoured from `TrackCues.grid_locked` by `add_tracks`.
locks_grids = True
def capabilities(self):
"""What a Traktor library can hold — answered with no library to read.
@@ -22,6 +22,7 @@ import copy
import math
import threading
import uuid as uuidlib
from datetime import datetime, timezone
from pathlib import Path
from traktor_nml_utils import (
@@ -1210,9 +1211,19 @@ class TraktorStore:
self.dirty = True
def set_lock(self, track_id: str, locked: bool) -> None:
"""Lock or unlock a track's grid.
Locking writes what Traktor 4.5 writes (measured 2026-10-19):
``LOCK="1"`` plus ``LOCK_MODIFICATION_TIME``, an ISO time in UTC to the
second. What Traktor leaves on UNLOCK is unmeasured, so unlocking only
removes ``LOCK``, as it always has.
"""
with self._lock:
entry = self._entry_or_raise(track_id)
entry.lock = 1 if locked else None
if locked:
entry.lock_modification_time = (
datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S"))
self._note("lock", "on" if locked else "off", track_id)
self.dirty = True
+3
View File
@@ -234,6 +234,9 @@ class PlatformOption(BaseModel):
#: As an export TARGET: its library is only ever read on a computer, never
#: from a drive — so it does nothing beside a stick format.
computer_only: bool = False
#: As an export TARGET: its library can store a grid lock, so the export's
#: "Lock beatgrids" option means something there.
locks_grids: bool = False
class RemapSample(BaseModel):
+20 -2
View File
@@ -433,12 +433,30 @@ def _folder_paths(adapter) -> dict[str, list[str]]:
# ---- running -----------------------------------------------------------------
def _locked(cues, lock_grids: bool):
"""The cues as exported: with `lock_grids`, a grid is LOCKED.
The lock rides on the generic `TrackCues.grid_locked`, the same flag a
source's own lock arrives in, so a writer reads one signal and never learns
which of the two set it. Unlocked Traktor re-analyses an exported track on
its first load (it has no AUDIO_ID, which cannot be computed), re-measuring
the BPM — a 174 track can come back as 87 — and drifting the grid; a
locked grid is left alone. A track with no grid is never locked: that
would stop Traktor ever giving it one.
"""
if lock_grids and cues is not None and cues.grid_markers and not cues.grid_locked:
return cues.model_copy(update={"grid_locked": True})
return cues
def run(adapter, export_set, built: ExportPlan, handle: JobHandle, *,
root_folder: bool = False) -> dict:
root_folder: bool = False, lock_grids: bool = False) -> dict:
"""Copy what changed, then write every target's library over the audio.
`root_folder` nests the playlists in a folder named after the export (the
"Put exported playlists in a folder" setting); otherwise they sit at the top.
`lock_grids` locks every exported track that HAS a grid (`_locked`); off,
each track keeps the source's own lock.
Rolls back EVERYTHING this run wrote on cancel or on any failure —
including the libraries of targets that had already been written when a
@@ -530,7 +548,7 @@ def run(adapter, export_set, built: ExportPlan, handle: JobHandle, *,
ExportTrack(
track=adapter.track(planned.track_id),
destination=target,
cues=adapter.track_cues(planned.track_id),
cues=_locked(adapter.track_cues(planned.track_id), lock_grids),
art=_cover(adapter, planned.track_id),
fingerprint=_fingerprint(planned),
analysis_cache=cache,
+9 -1
View File
@@ -62,6 +62,10 @@ class ExportSet:
destination: str = "" # OS path of the folder the export is written into
playlist_ids: list[str] = field(default_factory=list)
track_ids: list[str] = field(default_factory=list) # loose tracks only
#: Lock every exported grid, so Traktor does not re-analyse a track on its
#: first load (it re-measures the BPM — 174 can come back as 87 — and the
#: grid drifts). Remembered per set: it is a decision about this stick.
lock_grids: bool = True
created: float = 0.0
modified: float = 0.0
@@ -170,6 +174,7 @@ def _load(library_id: str) -> dict[str, ExportSet]:
destination=values.get("destination") or "",
playlist_ids=list(values.get("playlist_ids") or []),
track_ids=list(values.get("track_ids") or []),
lock_grids=bool(values.get("lock_grids", True)),
created=float(values.get("created") or 0.0),
modified=float(values.get("modified") or 0.0),
)
@@ -251,7 +256,8 @@ def create(library_id: str, *, name: str, targets: list[str], destination: str)
def update(library_id: str, set_id: str, **fields) -> ExportSet | None:
"""Rename, retarget or relocate a set. Unknown fields are ignored."""
"""Rename, retarget or relocate a set, or change its options. Unknown fields
are ignored."""
sets = _load(library_id)
found = sets.get(set_id)
if found is None:
@@ -261,6 +267,8 @@ def update(library_id: str, set_id: str, **fields) -> ExportSet | None:
setattr(found, key, fields[key])
if fields.get("targets"):
found.targets = list(dict.fromkeys(fields["targets"]))
if fields.get("lock_grids") is not None:
found.lock_grids = bool(fields["lock_grids"])
found.modified = time.time()
_save(library_id, sets)
return found
+11 -2
View File
@@ -78,6 +78,7 @@ from .schemas import (
FsListing,
FsPlace,
UpdateExportSet,
RunExport,
AddGridMarker,
GridMarkerEdit,
ReplaceGrid,
@@ -655,6 +656,7 @@ def update_export(set_id: str, body: UpdateExportSet) -> ExportSetOut:
name=body.name,
targets=body.targets,
destination=body.destination,
lock_grids=body.lock_grids,
)
return ExportSetOut(**updated.as_dict())
@@ -764,14 +766,19 @@ def preview_export(set_id: str) -> ExportPreview:
@app.post("/api/exports/{set_id}/run", response_model=JobStatus)
def run_export(set_id: str) -> JobStatus:
def run_export(set_id: str, body: RunExport | None = None) -> JobStatus:
"""Start the export on a background thread; poll `/api/jobs/{id}`.
Planned again HERE rather than trusting whatever the preview showed: a
playlist can be edited, a file deleted or a drive unplugged between looking
and clicking, and the plan carries the destination-safety check.
The dialog's options arrive with the click and are remembered on the set,
so the next export of the same stick starts from the same answer.
"""
found = _require_set(set_id)
if body is not None and body.lock_grids is not None and body.lock_grids != found.lock_grids:
found = exports.update(_require_library_id(), set_id, lock_grids=body.lock_grids)
adapter = require_adapter()
built = exporter.plan(adapter, found)
if built.blocked:
@@ -779,7 +786,7 @@ def run_export(set_id: str) -> JobStatus:
# A Konduktor setting, read when the export starts.
root_folder = bool(prefs.load_prefs().get("exportRootFolder", False))
job = JOBS.submit("export", lambda handle: exporter.run(
adapter, found, built, handle, root_folder=root_folder))
adapter, found, built, handle, root_folder=root_folder, lock_grids=found.lock_grids))
return JobStatus(**job.as_dict())
@@ -829,6 +836,7 @@ def export_targets() -> list[PlatformOption]:
removable=bool(getattr(d, "removable", False)),
drive_root=bool(getattr(exporter, "drive_root", False)),
computer_only=bool(getattr(exporter, "computer_only", False)),
locks_grids=bool(getattr(exporter, "locks_grids", False)),
))
# Targets with no reader of their own (the stick's legacy Device Library):
# listed too, or registering one would not make it selectable.
@@ -847,6 +855,7 @@ def export_targets() -> list[PlatformOption]:
removable=bool(getattr(exporter, "drive_root", False)),
drive_root=bool(getattr(exporter, "drive_root", False)),
computer_only=bool(getattr(exporter, "computer_only", False)),
locks_grids=bool(getattr(exporter, "locks_grids", False)),
))
# Each exporter states its own place; a platform with no exporter (not yet a
# target, shown disabled) goes last. Stable, so equal places keep their order.
+8
View File
@@ -347,6 +347,7 @@ class ExportSetOut(BaseModel):
destination: str
playlist_ids: list[str] = []
track_ids: list[str] = [] # loose tracks only
lock_grids: bool = True
created: float = 0.0
modified: float = 0.0
@@ -361,6 +362,13 @@ class UpdateExportSet(BaseModel):
name: str | None = None
targets: list[str] | None = None
destination: str | None = None
lock_grids: bool | None = None
class RunExport(BaseModel):
#: The run dialog's "Lock beatgrids" — remembered on the set. None keeps the
#: set's own answer.
lock_grids: bool | None = None
class ExportSetMembers(BaseModel):