Adrian Sampson  committed e538d78

docs & style for #92

We already have a nice plurality() function in beets.util to calculate the most
popular value of something, so I reused that.

  • Participants
  • Parent commits 00e279f
  • Branches default

Comments (0)

Files changed (2)

File beetsplug/lastgenre/

 from beets import plugins
 from beets import ui
-from beets.util import normpath
+from beets.util import normpath, plurality
 from beets import config
 from beets import library
             elif obj.albumartist != 'Various Artists':
                 result = fetch_album_artist_genre(obj)
-                # For 'Various Artists' pick the most used common tag as
-                # album_tag
-                artist_genres = {}
+                # For "Various Artists", pick the most popular track genre.
+                item_genres = []
                 for item in obj.items():
-                    tmp_genre = None
+                    item_genre = None
                     if 'track' in self.sources:
-                        tmp_genre = fetch_track_genre(item)
-                    if not tmp_genre:
-                        tmp_genre = fetch_artist_genre(item)
-                    if tmp_genre:
-                        artist_genres[tmp_genre] = \
-                            artist_genres.get(tmp_genre, 0) + 1
-                if len(artist_genres) > 0:
-                    result = filter(lambda k:artist_genres[k] ==
-                        max(artist_genres.values()), artist_genres)[0]
+                        item_genre = fetch_track_genre(item)
+                    if not item_genre:
+                        item_genre = fetch_artist_genre(item)
+                    if item_genre:
+                        item_genres.append(item_genre)
+                if item_genres:
+                    result, _ = plurality(item_genres)
             if result:
                 return result, 'artist'
                 for item in album.items():
+                    # If we're using track-level sources, also look up each
+                    # track on the album.
                     if 'track' in self.sources:
-                        # Fetch track genre only if source is 'track'
                         item.genre, src = self._get_genre(item)
               'genre for track {0} - {1} ({2}): {3}'.format(
                             item.artist, item.title, src, item.genre
                     if write:
             album.genre, src = self._get_genre(album)
             log.debug(u'added album genre ({0}): {1}'.format(
                   src, album.genre))
-            for item in album.items():
-                if 'track' in self.sources:
+            if 'track' in self.sources:
+                for item in album.items():
                     item.genre, src = self._get_genre(item)
                     log.debug(u'added item genre ({0}): {1}'.format(
                           src, item.genre))

File docs/plugins/lastgenre.rst

 Genre Source
-When looking up genres for albums or individual tracks, you may prefer to use
-a genre tag from the *track* or *artist* instead of the individual entity. To
-do so, set the ``source`` configuration value to "track" or "artist", like so::
+When looking up genres for albums or individual tracks, you can choose whether
+to use tags on the album, the artist, or the track. For example, you
+might want all the albums for a certain artist to carry the same genre. Set the
+``source`` configuration value to "album", "track", or "artist", like so::
         source: artist
+The default is "album". When set to "track", the plugin will fetch *both*
+album-level and track-level genres for your music when importing albums.
 Running Manually