From 429193229893e4755ec8208591f19a4fe6ac1e8e Mon Sep 17 00:00:00 2001 From: Ross Ashley Date: Thu, 26 Sep 2013 10:39:32 -0400 Subject: [PATCH 1/5] Added ability to store comma delimited lists of genres using lastfm and the local genre.txt file in the usual way. Using this, a song might now have a genre of 'House, IDM, Dance', instead of just 'House'. --- beetsplug/lastgenre/__init__.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 4e5f7bc2d..2c19786bb 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -49,6 +49,10 @@ PYLAST_EXCEPTIONS = ( # Core genre identification routine. +def _split_genre_string(genre): + genres = genre.split(',') + return genres + def _tags_for(obj): """Given a pylast entity (album or track), returns a list of tag names for that entity. Returns an empty list if the entity is @@ -82,10 +86,25 @@ def _find_allowed(genres): """Return the first string in the sequence `genres` that is present in the genre whitelist or None if no genre is suitable. """ + allowed_genres = [] + allowed_genres_copy = [] for genre in list(genres): if _is_allowed(genre): - return genre.title() # Title case. - return None + allowed_genres.append(genre.title()) + +# pdb.set_trace() + + for genre in allowed_genres[:-1]: + genre = genre + ', ' + allowed_genres_copy.append(genre) + else: + if allowed_genres: + allowed_genres_copy.append(allowed_genres[-1]) + + genre_str = '' + return genre_str.join(allowed_genres_copy) +# return genre.title() # Title case. +# return None def _strings_to_genre(tags): """Given a list of strings, return a genre. Returns the first string @@ -107,7 +126,7 @@ def _strings_to_genre(tags): def fetch_genre(lastfm_obj): """Return the genre for a pylast entity or None if no suitable genre - can be found. + can be found. Ex. 'Electronic, House, Dance' """ return _strings_to_genre(_tags_for(lastfm_obj)) From 514fa6cf0c51fde0dd48adcaea7bcb113484fe50 Mon Sep 17 00:00:00 2001 From: Ross Ashley Date: Thu, 26 Sep 2013 15:18:58 -0400 Subject: [PATCH 2/5] Removed some unused and commented out code. --- beetsplug/lastgenre/__init__.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 2c19786bb..eba06bc56 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -49,10 +49,6 @@ PYLAST_EXCEPTIONS = ( # Core genre identification routine. -def _split_genre_string(genre): - genres = genre.split(',') - return genres - def _tags_for(obj): """Given a pylast entity (album or track), returns a list of tag names for that entity. Returns an empty list if the entity is @@ -92,8 +88,6 @@ def _find_allowed(genres): if _is_allowed(genre): allowed_genres.append(genre.title()) -# pdb.set_trace() - for genre in allowed_genres[:-1]: genre = genre + ', ' allowed_genres_copy.append(genre) @@ -103,8 +97,6 @@ def _find_allowed(genres): genre_str = '' return genre_str.join(allowed_genres_copy) -# return genre.title() # Title case. -# return None def _strings_to_genre(tags): """Given a list of strings, return a genre. Returns the first string From 7c0d828a2ee318cb2b06b13e1a64c1a401343ec6 Mon Sep 17 00:00:00 2001 From: Ross Ashley Date: Fri, 27 Sep 2013 10:08:09 -0400 Subject: [PATCH 3/5] Replaced unnecessary code with one line. --- beetsplug/lastgenre/__init__.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index eba06bc56..b4b7dc783 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -79,25 +79,18 @@ def _is_allowed(genre): return False def _find_allowed(genres): - """Return the first string in the sequence `genres` that is present - in the genre whitelist or None if no genre is suitable. + """Return a string composed of comma delimited genres in the sequence + `genres` that is present in the genre whitelist or an empty string + if no genre is suitable. """ allowed_genres = [] - allowed_genres_copy = [] + for genre in list(genres): if _is_allowed(genre): allowed_genres.append(genre.title()) - for genre in allowed_genres[:-1]: - genre = genre + ', ' - allowed_genres_copy.append(genre) - else: - if allowed_genres: - allowed_genres_copy.append(allowed_genres[-1]) - - genre_str = '' - return genre_str.join(allowed_genres_copy) - + return ','.join(allowed_genres) + def _strings_to_genre(tags): """Given a list of strings, return a genre. Returns the first string that is present in the genre whitelist (or the canonicalization From 8525983791911b233443821e10688b695edb5229 Mon Sep 17 00:00:00 2001 From: Ross Ashley Date: Thu, 3 Oct 2013 11:39:33 -0400 Subject: [PATCH 4/5] For the comma delimited lastgenre addition, the old way is default. There is a new option called multiple_genres which defaults to False but when set to True, the comma delimited list of genres is returned by lastgenre. So, if you want a comma delimited list, it is lastgenre: multiple_genres: True --- beetsplug/lastgenre/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index b4b7dc783..c8a9d5440 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -28,6 +28,7 @@ import logging import pylast import os import yaml +import pdb from beets import plugins from beets import ui @@ -79,17 +80,26 @@ def _is_allowed(genre): return False def _find_allowed(genres): - """Return a string composed of comma delimited genres in the sequence - `genres` that is present in the genre whitelist or an empty string - if no genre is suitable. + """If multiple_genres is set to True, return a string composed of + comma delimited genres in the sequence `genres` that is present + in the genre whitelist or an empty string if no genre is suitable + or if multiple_genres is set to its default value of False, return + the original argument. """ allowed_genres = [] + pdb.set_trace() + for genre in list(genres): if _is_allowed(genre): allowed_genres.append(genre.title()) + if not options['multiple_genres']: + return genre.title() - return ','.join(allowed_genres) + if options['multiple_genres']: + return ','.join(allowed_genres) + + return def _strings_to_genre(tags): """Given a list of strings, return a genre. Returns the first string @@ -194,6 +204,7 @@ def fetch_track_genre(obj): options = { 'whitelist': None, + 'multiple_genres': False, 'branches': None, 'c14n': False, } @@ -203,6 +214,7 @@ class LastGenrePlugin(plugins.BeetsPlugin): self.config.add({ 'whitelist': os.path.join(os.path.dirname(__file__), 'genres.txt'), + 'multiple_genres': False, 'fallback': None, 'canonical': None, 'source': 'album', @@ -237,6 +249,8 @@ class LastGenrePlugin(plugins.BeetsPlugin): options['branches'] = branches options['c14n'] = True + options['multiple_genres'] = self.config['multiple_genres'].get() + @property def sources(self): """A tuple of allowed genre sources. May contain 'track', From 7a7d009671ced6b29169f0415f120345bb9c0818 Mon Sep 17 00:00:00 2001 From: Ross Ashley Date: Thu, 3 Oct 2013 15:15:18 -0400 Subject: [PATCH 5/5] Removed pdb stuff and cleeaned up description. --- beetsplug/lastgenre/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index c8a9d5440..8bc5b3b01 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -28,7 +28,6 @@ import logging import pylast import os import yaml -import pdb from beets import plugins from beets import ui @@ -84,12 +83,10 @@ def _find_allowed(genres): comma delimited genres in the sequence `genres` that is present in the genre whitelist or an empty string if no genre is suitable or if multiple_genres is set to its default value of False, return - the original argument. + the first genre found. """ allowed_genres = [] - pdb.set_trace() - for genre in list(genres): if _is_allowed(genre): allowed_genres.append(genre.title())