Adrian Sampson avatar 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.

Comments (0)

Files changed (2)

beetsplug/lastgenre/__init__.py

 
 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)
             else:
-                # 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)
                         lib.store(item)
                         log.info(u'genre for track {0} - {1} ({2}): {3}'.format(
                             item.artist, item.title, src, item.genre
                         ))
+
                     if write:
                         item.write()
 
             album.genre, src = self._get_genre(album)
             log.debug(u'added last.fm 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 last.fm item genre ({0}): {1}'.format(
                           src, item.genre))

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 Last.fm 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::
 
     lastgenre:
         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
 ----------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.