mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
6.5 KiB
6.5 KiB
CLAUDE.md
Guidance for working in this repo. Read before making changes.
What this is
Konduktor — a library-management and track-preparation tool for Native
Instruments Traktor (Pro 4 / NML v20), built on the traktor-nml-utils
library. The user is a DJ; the goal is a polished, multiplatform web app for
browsing the collection and editing playlists, later possibly wrapped in Tauri.
Architecture
Two independent apps that talk over HTTP:
backend/— Python + FastAPI. Parsescollection.nmlviatraktor-nml-utilsand serves a query API. Entry point: backend/konduktor/main.py.collection_service.py— loads the NML once into memory, flattens ENTRYs intoTrackobjects, indexes them by primary key, and builds the playlist tree. All query logic (filter/sort/facets/stats) lives here.schemas.py— Pydantic response models.main.py— FastAPI routes (all under/api).
frontend/— React + TypeScript + Vite. A dark, virtualized track explorer. Entry: frontend/src/App.tsx.api.ts— typed client + all API types (keep in sync with backendschemas.py).components/—Sidebar(playlist tree),Toolbar(search/filters),TrackTable(TanStack Table + Virtual grid),StatusBar,RatingStars.- Data flow: the whole library is fetched once (
/api/tracks?limit=20000); filtering and sorting happen client-side for instant interaction. Styling is Tailwind v4 with design tokens insrc/index.css.
Commands
./dev.sh # start both servers (see README for one-time setup)
# Backend
cd backend && source .venv/bin/activate
uvicorn konduktor.main:app --reload --port 8000
# point at another file: KONDUKTOR_NML=/path/to/collection.nml uvicorn ...
# Frontend
cd frontend
npm run dev # dev server (proxies /api -> :8000)
npm run build # tsc -b && vite build — run this to typecheck
Tests: cd backend && ./run_tests.sh (runs against a temp copy of the real
collection). ALWAYS run this before changing anything in the save /
serialization path. It enforces:
test_save_fidelity.py— a no-op save is byte-identical, and an edit changes only the edited playlist's block (this is the guard that catches serialization regressions like the lxml reformatting bug).test_phase3.py— full create/add/reorder/rename/delete/save cycle stays Traktor-valid, backup-first, COLLECTION byte-identical, original untouched.
Also validate the backend interactively at http://localhost:8000/docs and the
frontend at http://localhost:5173.
Critical gotchas — read these
traktor-nml-utilsMUST come from GitHub, not PyPI. PyPI's3.1.0is stale and cannot parse Traktor Pro 4 (NML v20) files — its strict parser dies on the v4<GRID>element insideCUE_V2.requirements.txtpins the GitHubmaster(v4.0.0). Do not "simplify" this to a plain PyPI pin.- Never write to the user's
collection.nml. It is real, irreplaceable data (8,485 tracks). The app is read-only today. Phase 3 writes must operate on a copy and back up first. - Saving is lossy. The library's
save()re-serializes the entire file (reflows whitespace,124.000000→124.0, reorders attributes) and does not update count attributes (e.g.PLAYLIST ENTRIES). For Phase 3, prefer surgical edits to just thePLAYLISTSnode (e.g. via lxml), recompute counts, and back up — do not round-trip the whole 14 MB collection.
Data model notes
- Track primary key =
f"{location.volume}{location.dir}{location.file}", e.g.Macintosh HD/:Music/:one.mp3. This is how playlist entries (PRIMARYKEY.KEY) join to collection tracks.Track.iduses this. - Rating =
RANKING / 51, giving 0–5 stars (_rating_stars). - Key is Traktor's display key string, e.g.
"10m"(Open Key notation). - Playlist tree: recurse
nml.playlists.node.subnodes.node. A node's.typeisFOLDER,PLAYLIST, orSMARTLIST. Smart playlists are rule-based and have no static entry list. Synthetic ids (pl-N/fld-N/sl-N) are assigned at load and map to entries inCollectionService._playlist_entries. - Model classes use single-capital names:
Entrytype,Primarykeytype,CueV2Type.Entrytypeis shared by collection entries and playlist entries.
Conventions
- Backend: type hints, Pydantic models for all responses, keep query logic in
collection_service.py(routes stay thin). - Frontend: functional components, TanStack Query for fetching, Tailwind
utility classes with the
ink-*/accent/goldtokens fromindex.css. Keepapi.tstypes aligned withschemas.py. - Python 3.14 note: pin dependencies loosely enough to get prebuilt wheels
(older
pydantic-corepins force a from-source Rust build that fails).
Roadmap
- ✅ Phase 1 — backend core + read-only API
- ✅ Phase 2 — track explorer UI
- ✅ Phase 3 — playlist editing/creation + safe (backup-first, surgical) saves
- ⬜ Phase 4 — polish + optional Tauri desktop packaging
Phase 3 write path (playlists)
playlist_store.py(PlaylistStore) edits the traktor-nml-utils dataclass model in memory (create/rename/delete/set-entries). Onsave()it renders the model, extracts only the freshly rendered<PLAYLISTS>…</PLAYLISTS>span, and splices it into the original file bytes — COLLECTION stays byte-identical, and a timestamped.bakis written first.- Minimal-diff is load-bearing — do NOT re-serialize with lxml. lxml
reformats the whole PLAYLISTS section (~2500 changed lines). The render MUST
use the library's pipeline:
XmlSerializer().render(nml)→restore_traktor_float_format(s, nml)→format_traktor_layout(s)(both imported fromtraktor_nml_utils). That reproduces Traktor's exact layout, so unedited playlists stay byte-identical and only edited entries diff (verified: a 13-track reorder changes exactly 24 lines). - Playlists are keyed by their stable
UUID; folders by synthetic path id (fld:<name>/<name>). PRIMARYKEYTYPEisSTEMif the track has a STEMS child (CollectionService.stem_keys, resolved viaentries_for), elseTRACK. POST /api/savealways writes (backup-first); no write gate. Endpoints:POST/PATCH/DELETE /api/playlists[...],PUT .../entries(replace),POST .../add(append),POST /api/save.- Test:
backend/test_phase3.py(runs against a temp copy; asserts surgical + backup + Traktor-valid). Run:python test_phase3.pyin the backend venv.