diff --git a/plugins/beetslabels.py b/plugins/beetslabels.py index 950acad..da47950 100644 --- a/plugins/beetslabels.py +++ b/plugins/beetslabels.py @@ -10,14 +10,16 @@ import optparse import json +LABELS_FIELD_NAME = "mylabels" + def do_modify_labels(lib, objs, label, action): changed = [] for obj in objs: labels = {} if action != 2: - if "mylabels" in obj: - labels = json.loads(obj["mylabels"]) + if LABELS_FIELD_NAME in obj: + labels = json.loads(obj[LABELS_FIELD_NAME]) split = label.split(":") @@ -30,7 +32,7 @@ def do_modify_labels(lib, objs, label, action): del labels[label] obj_mods = { - "mylabels": json.dumps(labels) + LABELS_FIELD_NAME: json.dumps(labels) } if print_and_modify(obj, obj_mods, []) and obj not in changed: changed.append(obj) @@ -85,8 +87,8 @@ def modify_labels(lib, opts, args): if actnum == 3: for obj in items: - if "mylabels" in obj: - labels = json.loads(obj["mylabels"]) + if LABELS_FIELD_NAME in obj: + labels = json.loads(obj[LABELS_FIELD_NAME]) labelstr = ";".join([f"{key}:{labels[key]}" for key in labels.keys()]) print_(f"{obj.title}: {labelstr}") else: @@ -129,16 +131,16 @@ def transfer_labels(lib, opts, args): print_(f"Dest: {dest.artist} - {dest.title}") print_("") - if "mylabels" not in source or not source["mylabels"]: + if LABELS_FIELD_NAME not in source or not source[LABELS_FIELD_NAME]: print_("Source has no labels to transfer.") return - labels = json.loads(source["mylabels"]) + labels = json.loads(source[LABELS_FIELD_NAME]) labelstr = ";".join([f"{key}:{labels[key]}" for key in labels.keys()]) print_(f"Labels to transfer: {labelstr}") - if "mylabels" in dest and dest["mylabels"]: - dest_labels = json.loads(dest["mylabels"]) + if LABELS_FIELD_NAME in dest and dest[LABELS_FIELD_NAME]: + dest_labels = json.loads(dest[LABELS_FIELD_NAME]) dest_labelstr = ";".join([f"{key}:{dest_labels[key]}" for key in dest_labels.keys()]) print_(f"WARNING: Destination already has labels: {dest_labelstr}") print_("These will be overwritten!") @@ -148,7 +150,7 @@ def transfer_labels(lib, opts, args): return with lib.transaction(): - dest["mylabels"] = source["mylabels"] + dest[LABELS_FIELD_NAME] = source[LABELS_FIELD_NAME] dest.try_sync(True, False, False) print_("Transfer complete.") @@ -158,7 +160,7 @@ labels_command.func = modify_labels class HasLabelQuery(FieldQuery): def __init__(self, _, pattern: str, __): - super().__init__("mylabels", pattern, False) + super().__init__(LABELS_FIELD_NAME, pattern, False) @classmethod def value_match(self, pattern, jsonstr):