From 54205998391b284801dba4cd1d595a591f23282a Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 28 Jul 2015 22:27:43 -0700 Subject: [PATCH] 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. --- beets/dbcore/db.py | 7 +++++++ beets/library.py | 18 ------------------ beets/ui/commands.py | 10 ++-------- beetsplug/duplicates.py | 10 +++++++--- docs/changelog.rst | 2 ++ 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 274e7af64..df796a134 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -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): diff --git a/beets/library.py b/beets/library.py index 9d11b166c..85c6e1b40 100644 --- a/beets/library.py +++ b/beets/library.py @@ -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. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index bbdbbcf96..c499d36d8 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -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( diff --git a/beetsplug/duplicates.py b/beetsplug/duplicates.py index 908f6a94c..6249f2278 100644 --- a/beetsplug/duplicates.py +++ b/beetsplug/duplicates.py @@ -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: diff --git a/docs/changelog.rst b/docs/changelog.rst index 80dec7b9f..aea740c1e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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