Added setting for export playlist root folder toggle

This commit is contained in:
patben8 committed 2026-10-12 10:56:32 +02:00
1 parent 2c352b76a1
commit 3385b794e9
12 files changed
+113 -40

No files matched your search

@@ -381,7 +381,8 @@ class OneLibraryExporter:
# ---- playlists -----------------------------------------------------------
def _write_playlists(self, con, payload: ExportPayload, by_source: dict) -> None:
"""One folder named after the export, with the tree preserved under it.
"""The tree preserved — at the top level (parent NULL), or with
`payload.root_folder` under one folder named after the export.
`playlist` is a flat table with `playlist_id_parent`, so folders and
playlists are the same row kind told apart by `attribute` (1 = folder).
@@ -397,8 +398,8 @@ class OneLibraryExporter:
)
return next_id[0]
root = add(payload.name, None, folder=True)
folders: dict[tuple[str, ...], int] = {(): root}
root = add(payload.name, None, folder=True) if payload.root_folder else None
folders: dict[tuple[str, ...], int | None] = {(): root}
for playlist in payload.playlists:
parent = root
@@ -198,8 +198,9 @@ class RekordboxDeviceExporter:
@staticmethod
def _playlists(payload: ExportPayload, by_source: dict) -> tuple[list[bytes], list[bytes]]:
"""The tree under one folder named after the export, as the other
targets build it. `sort_order` is a node's position among its siblings."""
"""The tree as the other targets build it — at the top level (parent
0), or with `payload.root_folder` under one folder named after the
export. `sort_order` is a node's position among its siblings."""
tree: list[bytes] = []
entries: list[bytes] = []
next_id = [0]
@@ -220,7 +221,7 @@ class RekordboxDeviceExporter:
index += 1
entries.append(pdb.playlist_entry_row(index, track_id, playlist_id))
root = add(payload.name, 0, folder=True)
root = add(payload.name, 0, folder=True) if payload.root_folder else 0
folders: dict[tuple[str, ...], int] = {(): root}
for playlist in payload.playlists:
parent = root
@@ -383,9 +383,12 @@ class RekordboxExporter:
# ---- playlists ---------------------------------------------------------------
def _write_playlists(self, db, payload: ExportPayload, rows: dict) -> None:
"""The tree, under one folder named after the export."""
root = db.create_playlist_folder(payload.name)
db.flush()
"""The tree — at the top level, or with `payload.root_folder` under one
folder named after the export (None = the top level)."""
root = None
if payload.root_folder:
root = db.create_playlist_folder(payload.name)
db.flush()
folders: dict[tuple[str, ...], object] = {(): root}
for playlist in payload.playlists:
+5 -9
View File
@@ -106,14 +106,10 @@ class TraktorExporter:
return WrittenLibrary(library=library)
def _write_playlists(self, adapter, payload: ExportPayload, moved: dict) -> None:
"""Recreate the tree under one folder named after the export.
Nested under the export's own name for the same reason import nests a
drive's playlists under the drive's name: a collection swapped onto
another machine should say where its playlists came from.
"""
root = adapter.create_folder(payload.name)
folders: dict[tuple[str, ...], str] = {(): root}
"""Recreate the tree — at the top level, or with `payload.root_folder`
under one folder named after the export (None = the top level)."""
root = adapter.create_folder(payload.name) if payload.root_folder else None
folders: dict[tuple[str, ...], str | None] = {(): root}
for playlist in payload.playlists:
parent = self._folder_for(adapter, folders, tuple(playlist.folders), root)
@@ -128,7 +124,7 @@ class TraktorExporter:
adapter.set_playlist_entries(node_id, loose)
@staticmethod
def _folder_for(adapter, folders: dict, path: tuple[str, ...], root: str) -> str:
def _folder_for(adapter, folders: dict, path: tuple[str, ...], root: str | None) -> str | None:
"""Create (once) the folder chain a playlist sits under, and return it."""
for depth in range(1, len(path) + 1):
branch = path[:depth]
+7 -1
View File
@@ -94,8 +94,14 @@ class ExportPayload:
tracks: list[ExportTrack] = field(default_factory=list)
playlists: list[ExportPlaylist] = field(default_factory=list)
#: The export's name, used for the folder its playlists are nested under.
#: The export's name, used for the folder its playlists are nested under
#: when `root_folder` is set.
name: str = "Export"
#: Nest the whole playlist tree (and "Other") in one folder named `name`.
#: Off — the default, a Konduktor setting — the playlists sit at the
#: library's top level: an export is a library of its own, so a folder
#: saying where it came from is one more click at the gig.
root_folder: bool = False
#: Called by a writer before each slow step (e.g. decoding a track for its
#: waveform) with a message for the status bar, and — for a per-track loop —
#: `step` of `of`, which drives the progress bar. RAISES when the user has
+6 -1
View File
@@ -433,9 +433,13 @@ def _folder_paths(adapter) -> dict[str, list[str]]:
# ---- running -----------------------------------------------------------------
def run(adapter, export_set, built: ExportPlan, handle: JobHandle) -> dict:
def run(adapter, export_set, built: ExportPlan, handle: JobHandle, *,
root_folder: 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.
Rolls back EVERYTHING this run wrote on cancel or on any failure —
including the libraries of targets that had already been written when a
later one failed. What it KEPT from the previous export stays, and the
@@ -545,6 +549,7 @@ def run(adapter, export_set, built: ExportPlan, handle: JobHandle) -> dict:
payload = ExportPayload(
name=export_set.name,
root_folder=root_folder,
tracks=payload_tracks,
playlists=built.playlists,
checkpoint=checkpoint,
+4 -1
View File
@@ -765,7 +765,10 @@ def run_export(set_id: str) -> JobStatus:
built = exporter.plan(adapter, found)
if built.blocked:
raise HTTPException(409, built.blocked)
job = JOBS.submit("export", lambda handle: exporter.run(adapter, found, built, handle))
# 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))
return JobStatus(**job.as_dict())
+10 -4
View File
@@ -177,9 +177,9 @@ check("bpm x100, rating, file type, bitmask as rekordbox writes them",
check("the file path is drive-relative", lib.tracks[0]["file_path"] == "/Contents/House/One.wav", lib.tracks[0]["file_path"])
check("no computer's masterDbId is claimed", all(t["u3"] == 0 and t["u4"] == 0 for t in lib.tracks))
tree = {p["name"]: p for p in lib.playlists}
check("the tree: a folder named after the export > House > Peak, plus Other for loose tracks",
tree["GIG"]["folder"] and tree["House"]["parent"] == tree["GIG"]["id"]
and tree["Peak"]["parent"] == tree["House"]["id"] and tree["Other"]["parent"] == tree["GIG"]["id"])
check("the tree: House > Peak, plus Other for loose tracks, at the top level (parent 0)",
"GIG" not in tree and tree["House"]["folder"] and tree["House"]["parent"] == 0
and tree["Peak"]["parent"] == tree["House"]["id"] and tree["Other"]["parent"] == 0)
peak = [e for e in lib.entries if e[2] == tree["Peak"]["id"]]
check("playlist entries in order, 1-based", peak == [(1, 1, tree["Peak"]["id"]), (2, 2, tree["Peak"]["id"])], peak)
check("every track has an analysis path, and the files exist",
@@ -188,6 +188,13 @@ check("artwork rows name the a<n>.jpg files, which exist",
all((root / p.lstrip("/")).is_file() for p in lib.artwork.values()) and len(lib.artwork) == 3)
check("every file written is reported, so a re-export can clear it",
all(p.exists() for p in written.all_paths))
import dataclasses # noqa: E402
rooted_tree = {p["name"]: p for p in P.read(
dev.write(dataclasses.replace(payload, root_folder=True), root).library.read_bytes()).playlists}
check("with the root-folder setting: a folder named after the export holds House and Other",
rooted_tree["GIG"]["folder"] and rooted_tree["GIG"]["parent"] == 0
and rooted_tree["House"]["parent"] == rooted_tree["GIG"]["id"]
and rooted_tree["Other"]["parent"] == rooted_tree["GIG"]["id"])
print("== alongside OneLibrary: one set of analysis files and artwork ==")
both = Path(tempfile.mkdtemp())
@@ -195,7 +202,6 @@ for t in tracks:
dst = both / t.destination.relative_to(root)
dst.parent.mkdir(parents=True, exist_ok=True)
dst.write_bytes(t.destination.read_bytes())
import dataclasses # noqa: E402
moved = [dataclasses.replace(t, destination=both / t.destination.relative_to(root)) for t in tracks]
both_payload = ExportPayload(name="GIG", tracks=moved, playlists=payload.playlists)
ol_written = core_export.for_platform("onelibrary").write(both_payload, both)
+24 -11
View File
@@ -137,19 +137,20 @@ def walk(nodes, path=()):
walk(back.playlist_tree())
check("one folder named after the export", ("GIG",) in tree and tree[("GIG",)].kind == "folder")
check("nested folders are preserved", ("GIG", "House") in tree)
check("and nest further", ("GIG", "House", "Slow") in tree)
# By default the tree sits at the top level: an export is a library of its own.
check("no folder named after the export by default", ("GIG",) not in tree)
check("nested folders are preserved", ("House",) in tree and tree[("House",)].kind == "folder")
check("and nest further", ("House", "Slow") in tree)
check("playlists land in their folder",
("GIG", "House", "Peak Time") in tree and ("GIG", "House", "Slow", "Warmup") in tree)
("House", "Peak Time") in tree and ("House", "Slow", "Warmup") in tree)
check("with the right counts",
tree[("GIG", "House", "Peak Time")].count == 2
and tree[("GIG", "House", "Slow", "Warmup")].count == 1)
tree[("House", "Peak Time")].count == 2
and tree[("House", "Slow", "Warmup")].count == 1)
# A track added individually would otherwise be reachable only by search.
check("loose tracks get an 'Other' playlist", ("GIG", "Other") in tree)
check("loose tracks get an 'Other' playlist, at the top level", ("Other",) in tree)
check("holding exactly the ones no playlist claimed",
tree[("GIG", "Other")].count == 1, tree[("GIG", "Other")].count)
other_ids = back.playlist_entries(tree[("GIG", "Other")].id)
tree[("Other",)].count == 1, tree[("Other",)].count)
other_ids = back.playlist_entries(tree[("Other",)].id)
check("and it is the right track",
back.track(other_ids[0]).title == plain.title)
@@ -182,7 +183,19 @@ again = exporter.write(payload, dest).library
check("the entry count did not double",
f'<COLLECTION ENTRIES="{len(chosen)}">' in again.read_text(encoding="utf-8"))
check("and the tree did not duplicate",
len(TraktorDriver().open(again).playlist_tree()) == 1)
len(TraktorDriver().open(again).playlist_tree()) == len(back.playlist_tree()))
print("== with the root-folder setting: everything under one folder named after the export ==")
payload.root_folder = True
rooted = TraktorDriver().open(exporter.write(payload, dest).library)
tree.clear()
walk(rooted.playlist_tree())
check("one top-level node: a folder named after the export",
[n.name for n in rooted.playlist_tree()] == ["GIG"] and tree[("GIG",)].kind == "folder")
check("the tree nests inside it",
("GIG", "House", "Peak Time") in tree and ("GIG", "House", "Slow", "Warmup") in tree)
check("and so does 'Other'", ("GIG", "Other") in tree and tree[("GIG", "Other")].count == 1)
payload.root_folder = False
# ==============================================================================
# Running an export: the copy, the safety rules, and the rollback.
@@ -207,7 +220,7 @@ for i, track in enumerate(src.tracks[:4]):
seed_items.append(ExportTrack(track=track, destination=audio, cues=src.track_cues(track.id)))
exporter_obj = core_export.for_platform("traktor")
exporter_obj.write(
_Payload(name="Source", tracks=seed_items,
_Payload(name="Source", tracks=seed_items, root_folder=True,
playlists=[ExportPlaylist(name="Peak Time",
track_ids=[src.tracks[0].id, src.tracks[1].id])]),
lib_root,
+16 -2
View File
@@ -173,9 +173,23 @@ for platform, reopen in (
tree.clear()
walk(back.playlist_tree())
check("a folder named after the export", ("GIG",) in tree)
check("nested folders are preserved", ("GIG", "House") in tree)
check("no folder named after the export by default", ("GIG",) not in tree)
check("nested folders are preserved, at the top level", ("House",) in tree)
check("with the playlist inside",
("House", "Peak Time") in tree
and tree[("House", "Peak Time")].count == len(chosen))
print(f"-- {platform}: with the root-folder setting")
rooted_root = Path(tempfile.mkdtemp()) / "GIG"
rooted_payload = build(rooted_root)
rooted_payload.root_folder = True
exporter.write(rooted_payload, rooted_root)
rooted = reopen(rooted_root)
tree.clear()
walk(rooted.playlist_tree())
check("one top-level node: a folder named after the export",
[n.name for n in rooted.playlist_tree()] == ["GIG"])
check("the tree nests inside it",
("GIG", "House", "Peak Time") in tree
and tree[("GIG", "House", "Peak Time")].count == len(chosen))