New all_keys() method on dbcore.Model

Rather than the ad-hoc one on library classes. This also avoids some confusing
duplication in the `beet fields` output, at the cost of turning off the plugin
distinction.
This commit is contained in:
Adrian Sampson 2015-07-28 22:27:43 -07:00
parent 41256898d2
commit 5420599839
5 changed files with 18 additions and 29 deletions

View file

@ -271,6 +271,13 @@ class Model(object):
else:
return base_keys
@classmethod
def all_keys(self):
"""Get a list of available keys for objects of this type.
Includes fixed and computed fields.
"""
return list(self._fields) + self._getters().keys()
# Act like a dictionary.
def update(self, values):

View file

@ -479,15 +479,6 @@ class Item(LibModel):
i.mtime = i.current_mtime() # Initial mtime.
return i
@classmethod
def get_fields(cls):
"""Returns Item fields available for queries and format strings."""
plugin_fields = []
for plugin in plugins.find_plugins():
plugin_fields += plugin.template_fields.keys()
return (cls._fields.keys() + cls._getters().keys() +
cls._types.keys()), plugin_fields
def __setitem__(self, key, value):
"""Set the item's value for a standard field or a flexattr.
"""
@ -910,15 +901,6 @@ class Album(LibModel):
getters['albumtotal'] = Album._albumtotal
return getters
@classmethod
def get_fields(cls):
"""Returns Album fields available for queries and format strings."""
plugin_fields = []
for plugin in plugins.find_plugins():
plugin_fields += plugin.album_template_fields.keys()
return (cls._fields.keys() + cls._getters().keys() +
cls._types.keys()), plugin_fields
def items(self):
"""Returns an iterable over the items associated with this
album.

View file

@ -82,17 +82,11 @@ def fields_func(lib, opts, args):
names.sort()
print_(" " + "\n ".join(names))
fs, pfs = library.Item.get_fields()
print_("Item fields:")
_print_rows(fs)
print_("Template fields from plugins:")
_print_rows(pfs)
_print_rows(library.Item.all_keys())
fs, pfs = library.Album.get_fields()
print_("Album fields:")
_print_rows(fs)
print_("Template fields from plugins:")
_print_rows(pfs)
_print_rows(library.Album.all_keys())
fields_cmd = ui.Subcommand(

View file

@ -237,9 +237,13 @@ class DuplicatesPlugin(BeetsPlugin):
return counts
def _order(self, objs, tiebreak=None):
"""Return objs sorted by descending order of fields in tiebreak dict.
"""Return the objects (Items or Albums) sorted by descending
order of priority.
Default ordering is based on attribute completeness.
If provided, the `tiebreak` dict indicates the field to use to
prioritize the objects. Otherwise, the objects are placed in
order of "completeness": objects with more non-null fields come
first.
"""
if tiebreak:
kind = 'items' if all(isinstance(o, Item)
@ -248,7 +252,7 @@ class DuplicatesPlugin(BeetsPlugin):
else:
kind = Item if all(isinstance(o, Item) for o in objs) else Album
if kind is Item:
fields = [f for sublist in kind.get_fields() for f in sublist]
fields = kind.all_keys()
key = lambda x: len([(a, getattr(x, a, None)) for a in fields
if getattr(x, a, None) not in (None, '')])
else:

View file

@ -79,6 +79,8 @@ Fixes:
bug`_. Thanks to :user:`nathdwek`. :bug:`1545`
* Fix the :ref:`group_albums` importer mode so it can handle when files are
not already in order by album. :bug:`1550`
* The ``fields`` command no longer separates built-in fields from
plugin-provided ones. This distinction was becoming increasingly unreliable.
.. _Python bug: http://bugs.python.org/issue16512