Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions library/rekordbox_dump_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,50 @@ json.dump(out, sys.stdout)
}
}
}

// The helper resolves a My Tag condition's value as rekordbox reads it: a
// signed 32-bit integer attribute whose bits are the tag ID, saturating past
// the signed range, reading malformed text as tag 0 and a leading number
// from anything else (link-spec smart-mytag-matrix, rekordbox 7.2.19).
func TestDumpHelperReadsMyTagValuesLikeRekordbox(t *testing.T) {
if _, err := exec.LookPath("python3"); err != nil {
t.Skip("python3 not available")
}
const script = `
import importlib.util, json, sys, types
sys.modules["sqlcipher3"] = types.ModuleType("sqlcipher3")
spec = importlib.util.spec_from_file_location("dump", "../tools/rekordbox_dump.py")
dump = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dump)
tags = {"0": "Zero", "1": "One", "2147483647": "Max", "2147483648": "High", "4294967295": "All"}
def tag(value):
xml = '<NODE Id="1" LogicalOperator="1"><CONDITION PropertyName="myTag" Operator="8" ValueUnit="" ValueLeft="%s" ValueRight=""/></NODE>' % value
return dump.parse_smartlist(xml, tags)["conditions"][0]["left"]
values = ["1", "2147483647", "2147483648", "4294967295", "4294967296", "-2147483648", "-1",
"-2147483649", " 001", "+1", " 1 ", "0x1", "1,2147483648", "wat", "", "12345"]
json.dump({v: tag(v) for v in values}, sys.stdout)
`
raw, err := exec.Command("python3", "-c", script).CombinedOutput()
if err != nil {
t.Fatalf("python3: %v\n%s", err, raw)
}
var got map[string]string
if err := json.Unmarshal(raw, &got); err != nil {
t.Fatalf("decode %s: %v", raw, err)
}
for value, want := range map[string]string{
"1": "One", "2147483647": "Max",
// Past the signed range saturates to its top, not the bits above it.
"2147483648": "Max", "4294967295": "Max", "4294967296": "Max",
// Negative values carry the high bit; underflow saturates to it.
"-2147483648": "High", "-1": "All", "-2147483649": "High",
" 001": "One", "+1": "One", " 1 ": "One",
"0x1": "Zero", "1,2147483648": "One", "wat": "Zero",
"": "", // blank stays blank and matches nothing
"12345": "12345", // no such tag
} {
if got[value] != want {
t.Errorf("ValueLeft %q resolved to %q, want %q", value, got[value], want)
}
}
}
21 changes: 13 additions & 8 deletions tools/rekordbox_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def open_db(db_path, provided_key):
def juce_int(s):
"""Read text as rekordbox does an integer attribute (JUCE getIntValue):
an optionally signed run of leading digits after any spaces, 0 if none,
so "1.5" is 1 and "wat" is 0."""
so "1.5" is 1, "0x1" is 0 and "wat" is 0, saturated to a signed 32-bit
int, so "2147483648" is 2147483647 (link-spec smart-mytag-matrix,
rekordbox 7.2.19)."""
m = re.match(r"\s*([+-]?\d+)", s or "")
return int(m.group(1)) if m else 0
if not m:
return 0
return max(-(1 << 31), min(int(m.group(1)), (1 << 31) - 1))


def db_int(v, bits=32, signed=True):
Expand Down Expand Up @@ -271,12 +275,13 @@ def parse_smartlist(xml_str, tags_by_id):
a = cnd.attrib
prop = a.get("PropertyName", "")
left = a.get("ValueLeft", "")
if prop == "myTag" and left:
try:
uid = str(int(left) & 0xFFFFFFFF) # signed int32 → unsigned key
left = tags_by_id.get(uid, left)
except ValueError:
pass
if prop == "myTag" and left.strip():
# rekordbox reads the tag ID as an integer attribute (juce_int),
# so a positive ID past the signed range saturates and malformed
# text is tag 0; the signed value's bits are djmdMyTag.ID. A blank
# value stays blank: it matches nothing either way.
uid = str(juce_int(left) & 0xFFFFFFFF)
left = tags_by_id.get(uid, uid)
conds.append({
"property": prop,
"operator": juce_int(a.get("Operator", "")),
Expand Down