Files
konduktor/backend/konduktor/core/model.py
T

315 lines
11 KiB
Python

"""The generic model — Konduktor's platform-independent read projection.
Every adapter projects its native library into these types, and the HTTP layer
serves them directly. Nothing here may reference a platform's own encodings: a
value that only makes sense for one format belongs in that adapter, not here.
`AutoHotcue` is the one command-input type that lives alongside them, because it
is the shape a batch cue placement takes in generic terms.
"""
from __future__ import annotations
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
from .capabilities import CueRole, CueType, MediaKind, PlaylistKind, TrackColor
class HotcueChip(BaseModel):
"""One occupied hotcue slot, as the library table previews it.
Deliberately far smaller than a `CuePoint`: it rides on every `Track` in the
whole-library fetch, and a row only needs enough to draw a coloured dot.
"""
slot: int
type: CueType = "cue"
color: str | None = None # "#RRGGBB" as the platform stored it
class Track(BaseModel):
"""A single track in the library, as the app sees it."""
id: str # the adapter's stable track key; opaque above the adapter
artist: str | None = None
title: str | None = None
album: str | None = None
genre: str | None = None
label: str | None = None
remixer: str | None = None
producer: str | None = None
mix: str | None = None
comment: str | None = None
# A second free-text comment. Traktor's "Comment 2", which it keeps only in
# the collection (`INFO@RATING` — not the star rating, that is RANKING) and
# never writes into the audio file.
comment2: str | None = None
bpm: float | None = None
# The platform's own display string, shown verbatim ("10m", "8A", "Am").
key: str | None = None
# Camelot wheel position 1-12 + mode, parsed BY THE ADAPTER — notation is
# platform knowledge. None when the key is absent or unparseable.
key_wheel: int | None = None
key_mode: Literal["major", "minor"] | None = None
rating: int = 0 # 0-5 stars (derived from RANKING/51)
playcount: int | None = None
length: int | None = None # seconds
bitrate: int | None = None # bits per second (Pioneer libraries store kbps)
# ISO-8601 "YYYY-MM-DD", normalised by the adapter.
import_date: str | None = None
last_played: str | None = None
release_date: str | None = None
filepath: str | None = None # human-readable OS path
cue_count: int = 0
hotcue_count: int = 0
# The occupied hotcue slots, ordered by slot. Where cues are parsed lazily
# (OneLibrary) this is empty until `track_cues()` corrects it, exactly like
# `hotcue_count` — the two must always agree.
hotcues: list[HotcueChip] = Field(default_factory=list)
# 0 = no beatgrid, 1 = constant tempo, >1 = a flexible (multi-tempo) grid.
grid_marker_count: int = 0
grid_locked: bool = False
media_kind: MediaKind = "audio"
# The track's colour tag: the neutral identity (what crosses platforms) and
# the platform's own display colour for it, both mapped from the stored
# code by the adapter. None = no colour.
color: TrackColor | None = None
color_hex: str | None = None
# The DJ app's own auto gain for the track in dB (Traktor's deck gain,
# rekordbox's Auto Gain), as the adapter reads it. None = the track has
# none, which is what "fill where missing" looks for.
auto_gain_db: float | None = None
class TrackPage(BaseModel):
total: int # total matching the filter (before pagination)
offset: int
limit: int
items: list[Track]
class GenreCount(BaseModel):
name: str
count: int
class Facets(BaseModel):
"""Distinct values available for building filter UI."""
genres: list[GenreCount]
keys: list[GenreCount]
bpm_min: float | None
bpm_max: float | None
total_tracks: int
class Stats(BaseModel):
total_tracks: int
total_playlists: int
rated: int
unrated: int
missing_key: int
missing_genre: int
missing_bpm: int
no_cues: int
rating_breakdown: dict[int, int] # stars -> count
bpm_histogram: list[dict] # [{bucket: "120-130", count: n}]
top_genres: list[GenreCount]
class PlaylistNode(BaseModel):
"""A node in the playlist tree.
`id` is OPAQUE: the adapter mints it and decodes it, and the UI never
constructs, parses or prefixes one. `kind` is for choosing an icon — every
behavioural question is answered by the flags, so the UI never has to infer
what a node can do from what it is called.
"""
id: str
name: str
kind: PlaylistKind = "playlist"
count: int = 0 # track count (for playlists)
children: list["PlaylistNode"] = []
selectable: bool = False # has a static, listable entry set
can_add_tracks: bool = False
can_reorder: bool = False
can_rename: bool = False
can_delete: bool = False
can_contain_children: bool = False # a valid parent for a new playlist
# Can be dragged elsewhere in the tree (`move_playlist`): reordered among
# its siblings or moved into another folder.
can_move: bool = False
PlaylistNode.model_rebuild()
# ---- write request bodies ----
class CuePoint(BaseModel):
name: str | None = None
type: CueType = "cue"
# A "memory" cue has no bank slot: Rekordbox's / OneLibrary's memory cues,
# Traktor's cues on no pad (HOTCUE="-1"). Addressed by `id`, not `slot`.
role: CueRole = "hotcue"
start: float # seconds
length: float # seconds (>0 for loops)
slot: int | None = None # bank slot; None for a cue not in a bank
color: str | None = None # "#RRGGBB" as the platform stored it
# False when the adapter refuses commands on this cue — the UI gates on THIS
# rather than on any platform-specific reason.
editable: bool = True
readonly_reason: Literal["platform_managed"] | None = None
# Index of the grid marker this cue mirrors, where the platform pairs them.
# A display hint only; None on platforms that do not.
grid_marker: int | None = None
# The handle the memory-cue commands take (None on a hot cue, which is
# addressed by slot). Opaque and adapter-defined; an edit of THIS cue may
# change it, which is fine because every command returns fresh TrackCues.
id: str | None = None
class GridMarker(BaseModel):
"""One beatgrid marker. Its position in the list IS its index/identity."""
start: float # seconds
bpm: float # governs from this marker until the next one
name: str | None = None # Traktor's label — display only, NOT a discriminator
companion: int | None = None # hotcue slot of the paired white cue, if any
class TrackCues(BaseModel):
# THE beatgrid, ordered by start. Empty = no grid; one marker = constant
# tempo. There is deliberately no scalar bpm/anchor here: a single "the BPM"
# is what made flexible grids render and edit wrongly.
grid_markers: list[GridMarker] = []
grid_locked: bool = False
cues: list[CuePoint] = [] # cue/loop markers (grid markers themselves excluded)
class AutoHotcue(BaseModel):
"""One cue in a batch placement (Auto Hotcues)."""
slot: int
start: float # seconds
name: str | None = None # positional label (e.g. "Drop")
type: CueType = "cue"
length: float = 0.0 # seconds (>0 for a loop cue)
class LibraryInfo(BaseModel):
"""Identity of the loaded library, for display and for composing copy."""
platform: str
name: str # the DJ app's name, e.g. "Traktor"
library_label: str # what the user thinks of the artefact as, e.g. "collection.nml"
path: str
display_name: str # short label for the status bar
version: str | None = None
# Set when the library is held by a server: {id, name, via} of the saved
# remote it was opened through. None for a library on this computer.
remote: dict | None = None
class PlatformOption(BaseModel):
"""A platform the picker can offer before any library is open.
Facts only, never finished sentences: `lib/platformCopy.ts` composes the
wording. The distinction that matters is `removable` — a platform with no
library found is "not installed" for Traktor and "nothing plugged in" for
OneLibrary, and only the driver knows which of those is true.
"""
platform: str
name: str
library_label: str
selects: Literal["file", "directory", "remote"] = "file"
installed: bool = False
#: How many libraries this platform has right now; `installed` is `found > 0`.
found: int = 0
#: The library lives on a plugged-in drive, so `found` changes between calls.
removable: bool = False
#: As an export TARGET: its library is only found at a drive's root.
drive_root: bool = False
#: 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
#: As an export TARGET: its library keeps the DJ app's auto gain, so the
#: export's "Auto gain" option means something there (no stick format does).
writes_auto_gain: bool = False
class RemapSample(BaseModel):
model_config = ConfigDict(populate_by_name=True)
from_: str = Field(alias="from")
to: str
exists: bool
class RemapPreview(BaseModel):
total: int # tracks with a resolvable location
matched: int # how many match the `from` prefix
existing: int # of matched, how many exist at the `to` target
samples: list[RemapSample] = []
# Target paths more than one track would land on. Non-zero means the remap
# would be refused: two entries sharing a path corrupt the library.
collisions: int = 0
collision_samples: list[str] = []
class PrefixGroup(BaseModel):
prefix: str # common directory prefix of a group of tracks
count: int # tracks sharing it
class PrefixSuggestions(BaseModel):
primary: str # best guess for the `from` prefix (largest group)
groups: list[PrefixGroup] = [] # alternatives, ranked by count
class RelocationCandidate(BaseModel):
"""One place a volume's tracks were found, as a mapping the user can pick."""
model_config = ConfigDict(populate_by_name=True)
from_: str = Field(alias="from")
to: str
found: int # of the volume's tracks, how many exist under this mapping
class RelocationVolume(BaseModel):
"""A stored volume in which NO track resolves, and where its tracks are now.
`ambiguous` means more than one candidate resolves most of the volume (a
drive and its backup clone): the user chooses, nothing is preselected.
"""
label: str # how the library names the place, e.g. Traktor's "X:"
root: str # the stored OS-path prefix
total: int
status: Literal["found", "ambiguous", "not_found"]
candidates: list[RelocationCandidate] = []
class Relocation(BaseModel):
"""The open-time missing-files check, pending until the user answers it.
`scanning` while the search is still running; afterwards `volumes` is empty
when there is nothing to ask — every volume resolves, the platform does not
take part, or the user has already answered for this open.
"""
scanning: bool = False
volumes: list[RelocationVolume] = []
class RelocationApplied(BaseModel):
mappings: int
tracks: int # how many tracks the applied mappings resolve