mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 08:39:17 +01:00
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.
This commit is contained in:
parent
775a48eb28
commit
3176b83cd7
2 changed files with 22 additions and 4 deletions
|
|
@ -466,6 +466,11 @@ class Model(object):
|
||||||
|
|
||||||
return cls._type(key).parse(string)
|
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.
|
# Database controller and supporting interfaces.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,27 @@ def flatten(obj, fields):
|
||||||
"""Represent `obj`, a `dbcore.Model` object, as a dictionary for
|
"""Represent `obj`, a `dbcore.Model` object, as a dictionary for
|
||||||
serialization. Only include the given `fields` if provided;
|
serialization. Only include the given `fields` if provided;
|
||||||
otherwise, include everything.
|
otherwise, include everything.
|
||||||
|
|
||||||
|
The resulting dictionary's keys are all human-readable strings.
|
||||||
"""
|
"""
|
||||||
d = dict(obj)
|
d = dict(obj.formatted())
|
||||||
if fields:
|
if fields:
|
||||||
return {k: v for k, v in d.items() if k in fields}
|
return {k: v for k, v in d.items() if k in fields}
|
||||||
else:
|
else:
|
||||||
return d
|
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):
|
class EditPlugin(plugins.BeetsPlugin):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -233,14 +246,14 @@ class EditPlugin(plugins.BeetsPlugin):
|
||||||
forbidden = False
|
forbidden = False
|
||||||
for key in ignore_fields:
|
for key in ignore_fields:
|
||||||
if old_dict.get(key) != new_dict.get(key):
|
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
|
forbidden = True
|
||||||
break
|
break
|
||||||
if forbidden:
|
if forbidden:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
obj = obj_by_id[old_dict['id']]
|
id = int(old_dict['id'])
|
||||||
obj.update(new_dict)
|
apply(obj_by_id[id], new_dict)
|
||||||
|
|
||||||
def save_write(self, objs):
|
def save_write(self, objs):
|
||||||
"""Save a list of updated Model objects to the database.
|
"""Save a list of updated Model objects to the database.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue