From 3176b83cd76807cfc48773cc282f0ee021a62c70 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 19 Nov 2015 15:25:49 -0800 Subject: [PATCH] Use type-based formatting and parsing All editable values are now strings and are parsed from strings. This closely matches the behavior of the `modify` command, for example. --- beets/dbcore/db.py | 5 +++++ beetsplug/edit.py | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index df796a134..b5da1c89e 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -466,6 +466,11 @@ class Model(object): return cls._type(key).parse(string) + def set_parse(self, key, string): + """Set the object's key to a value represented by a string. + """ + self[key] = self._parse(key, string) + # Database controller and supporting interfaces. diff --git a/beetsplug/edit.py b/beetsplug/edit.py index 719fbd032..2798c162c 100644 --- a/beetsplug/edit.py +++ b/beetsplug/edit.py @@ -55,14 +55,27 @@ def flatten(obj, fields): """Represent `obj`, a `dbcore.Model` object, as a dictionary for serialization. Only include the given `fields` if provided; otherwise, include everything. + + The resulting dictionary's keys are all human-readable strings. """ - d = dict(obj) + d = dict(obj.formatted()) if fields: return {k: v for k, v in d.items() if k in fields} else: return d +def apply(obj, data): + """Set the fields of a `dbcore.Model` object according to a + dictionary. + + This is the opposite of `flatten`. The `data` dictionary should have + strings as values. + """ + for key, value in data.items(): + obj.set_parse(key, value) + + class EditPlugin(plugins.BeetsPlugin): def __init__(self): @@ -233,14 +246,14 @@ class EditPlugin(plugins.BeetsPlugin): forbidden = False for key in ignore_fields: if old_dict.get(key) != new_dict.get(key): - self._log.warn('ignoring object where {} changed', key) + self._log.warn('ignoring object whose {} changed', key) forbidden = True break if forbidden: continue - obj = obj_by_id[old_dict['id']] - obj.update(new_dict) + id = int(old_dict['id']) + apply(obj_by_id[id], new_dict) def save_write(self, objs): """Save a list of updated Model objects to the database.