From 43d3805baddad984b4d62498668671e3d2dd6722 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:42:51 +0530 Subject: [PATCH 01/13] Add _folksonomy_tags property to MetadataItem Class --- picard/item.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/picard/item.py b/picard/item.py index 8874eadda7..7f8f393104 100644 --- a/picard/item.py +++ b/picard/item.py @@ -200,6 +200,7 @@ def __init__(self, obj_id=None): self.iter_children_items_metadata_ignore_attrs = {} self.suspend_metadata_images_update = IgnoreUpdatesContext() self._genres = Counter() + self._folksonomy_tags = Counter() @property def tagger(self): @@ -314,14 +315,21 @@ def set_genre_inc_params(inc, config=None): require_authentication = False config = config or get_config() if config.setting['use_genres']: - use_folksonomy = config.setting['folksonomy_tags'] if config.setting['only_my_genres']: require_authentication = True - inc |= {'user-tags'} if use_folksonomy else {'user-genres'} + inc |= {'user-tags', 'user-genres'} else: - inc |= {'tags'} if use_folksonomy else {'genres'} + inc |= {'tags', 'genres'} return require_authentication + @property + def folksonomy_tags(self): + return self._folksonomy_tags + + def add_folksonomy_tag(self, name, count): + if name: + self._folksonomy_tags[name] += count + class FileListItem(MetadataItem): From a3c9b7f1fd3a191a96d16e9a52844c632024afa7 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:46:55 +0530 Subject: [PATCH 02/13] Load tags to _folksonomy_tags and genres to _genres --- picard/mbjson.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/picard/mbjson.py b/picard/mbjson.py index be8380b9da..999520e0fb 100644 --- a/picard/mbjson.py +++ b/picard/mbjson.py @@ -636,14 +636,16 @@ def add_secondary_release_types(node, m): def add_genres_from_node(node, obj): if obj is None: return - if 'genres' in node: - add_genres(node['genres'], obj) - if 'tags' in node: - add_genres(node['tags'], obj) - if 'user-genres' in node: - add_user_genres(node['user-genres'], obj) - if 'user-tags' in node: - add_user_genres(node['user-tags'], obj) + config = get_config() + if config.setting['use_genres']: + if 'tags' in node: + add_tags(node['tags'], obj) + if 'user-tags' in node: + add_user_tags(node['user-tags'], obj) + if 'genres' in node: + add_genres(node['genres'], obj) + if 'user-genres' in node: + add_user_genres(node['user-genres'], obj) def add_genres(node, obj): @@ -656,6 +658,16 @@ def add_user_genres(node, obj): obj.add_genre(tag['name'], 1) +def add_tags(node, obj): + for tag in node: + obj.add_folksonomy_tag(tag['name'], tag['count']) + + +def add_user_tags(node, obj): + for tag in node: + obj.add_folksonomy_tag(tag['name'], 1) + + def add_isrcs_to_metadata(node, metadata): for isrc in node: metadata.add('isrc', isrc) From a05a1e853ab7f8d04e4b79ebb96ace91d4815806 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:50:31 +0530 Subject: [PATCH 03/13] Write _genres and _folksonomy_tags into file --- picard/track.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/picard/track.py b/picard/track.py index fbaae45601..230b85e470 100644 --- a/picard/track.py +++ b/picard/track.py @@ -313,6 +313,8 @@ def _customize_metadata(self): tm['~silence'] = '1' if config.setting['use_genres']: + self.add_folksonomy_tags() + self.add_genres() self._convert_folksonomy_tags_to_genre() # Convert Unicode punctuation @@ -368,6 +370,18 @@ def _convert_folksonomy_tags_to_genre(self): join_with=config.setting['join_genres'] ) + def add_folksonomy_tags(self): + config = get_config() + tags = Counter(self._folksonomy_tags) + tags += self.album._folksonomy_tags + self.metadata['_folksonomy_tags'] = self._genres_to_metadata(tags, join_with=config.setting['join_genres']) + + def add_genres(self): + config = get_config() + _genres = Counter(self._genres) + _genres += self.album._genres + self.metadata['_genres'] = self._genres_to_metadata(_genres, join_with=config.setting['join_genres']) + class NonAlbumTrack(Track): From 2785839952218a72d5f9542a064db168c9bfdb09 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:50:57 +0530 Subject: [PATCH 04/13] Check 'folksonomy_tag' setting and fill genre The check has been moved here from picard/mbjson.py as we are loading _genres and _folksonomy_tags regardless of user setting. Thus for filling in the actual genre tag, we are checking the user preference. --- picard/track.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/picard/track.py b/picard/track.py index 230b85e470..24b7de557b 100644 --- a/picard/track.py +++ b/picard/track.py @@ -348,9 +348,13 @@ def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=No def _convert_folksonomy_tags_to_genre(self): config = get_config() - # Combine release and track genres genres = Counter(self.genres) - genres += self.album.genres + use_folksonomy = config.setting['folksonomy_tags'] + if use_folksonomy: + genres += self.album._folksonomy_tags + else: + genres += self.album._genres + # Combine release and track genres if self.album.release_group: genres += self.album.release_group.genres if not genres and config.setting['artists_genres']: From cf267f6dde6abb34784c0d4eed14e7af0dc8e674 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:49:03 +0530 Subject: [PATCH 05/13] Move the method to follow add_genre --- picard/item.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/picard/item.py b/picard/item.py index 7f8f393104..a40f0d6fe3 100644 --- a/picard/item.py +++ b/picard/item.py @@ -310,6 +310,14 @@ def add_genre(self, name, count): if name: self._genres[name] += count + @property + def folksonomy_tags(self): + return self._folksonomy_tags + + def add_folksonomy_tag(self, name, count): + if name: + self._folksonomy_tags[name] += count + @staticmethod def set_genre_inc_params(inc, config=None): require_authentication = False @@ -322,14 +330,6 @@ def set_genre_inc_params(inc, config=None): inc |= {'tags', 'genres'} return require_authentication - @property - def folksonomy_tags(self): - return self._folksonomy_tags - - def add_folksonomy_tag(self, name, count): - if name: - self._folksonomy_tags[name] += count - class FileListItem(MetadataItem): From 47a139ce83364325358ee58fe62404d99047c7ec Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:50:45 +0530 Subject: [PATCH 06/13] Remove the check for 'use_genre' --- picard/mbjson.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/picard/mbjson.py b/picard/mbjson.py index 999520e0fb..7dbbaf810c 100644 --- a/picard/mbjson.py +++ b/picard/mbjson.py @@ -636,16 +636,14 @@ def add_secondary_release_types(node, m): def add_genres_from_node(node, obj): if obj is None: return - config = get_config() - if config.setting['use_genres']: - if 'tags' in node: - add_tags(node['tags'], obj) - if 'user-tags' in node: - add_user_tags(node['user-tags'], obj) - if 'genres' in node: - add_genres(node['genres'], obj) - if 'user-genres' in node: - add_user_genres(node['user-genres'], obj) + if 'tags' in node: + add_tags(node['tags'], obj) + if 'user-tags' in node: + add_user_tags(node['user-tags'], obj) + if 'genres' in node: + add_genres(node['genres'], obj) + if 'user-genres' in node: + add_user_genres(node['user-genres'], obj) def add_genres(node, obj): From d543ec0b1277b5e69111195cf19b604dcd789f8a Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:52:16 +0530 Subject: [PATCH 07/13] Move repeated code to _add_tags, remove filter --- picard/track.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/picard/track.py b/picard/track.py index 24b7de557b..be48bc4c89 100644 --- a/picard/track.py +++ b/picard/track.py @@ -313,8 +313,9 @@ def _customize_metadata(self): tm['~silence'] = '1' if config.setting['use_genres']: - self.add_folksonomy_tags() - self.add_genres() + config = get_config() + self._add_folksonomy_tags(config) + self._add_genres(config) self._convert_folksonomy_tags_to_genre() # Convert Unicode punctuation @@ -374,17 +375,19 @@ def _convert_folksonomy_tags_to_genre(self): join_with=config.setting['join_genres'] ) - def add_folksonomy_tags(self): - config = get_config() + def _add_tags(self, tags, name, config=None): + config = config or get_config() + self.metadata[name] = tags.keys() + + def _add_folksonomy_tags(self, config=None): tags = Counter(self._folksonomy_tags) tags += self.album._folksonomy_tags - self.metadata['_folksonomy_tags'] = self._genres_to_metadata(tags, join_with=config.setting['join_genres']) + self._add_tags(tags, '_folksonomy_tags', config) - def add_genres(self): - config = get_config() - _genres = Counter(self._genres) - _genres += self.album._genres - self.metadata['_genres'] = self._genres_to_metadata(_genres, join_with=config.setting['join_genres']) + def _add_genres(self, config=None): + genre = Counter(self._genres) + genre += self.album._genres + self._add_tags(genre, '_genres', config) class NonAlbumTrack(Track): From c82f01ca34850b4db23b0a0ebb3f80b42dff07eb Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Tue, 22 Oct 2024 04:31:10 +0530 Subject: [PATCH 08/13] Fix tests Check for _genres and _folksonomy_tags individually as both are filled in separately now. --- test/test_mbjson.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/test/test_mbjson.py b/test/test_mbjson.py index f466fc5450..6bbecfdc01 100644 --- a/test/test_mbjson.py +++ b/test/test_mbjson.py @@ -135,11 +135,14 @@ def test_release(self): self.assertEqual(m['~release_seriesid'], '7421b602-a413-4151-bcf4-d831debc3f27') self.assertEqual(m['~release_seriescomment'], 'Pink Floyed special editions') self.assertEqual(m['~release_seriesnumber'], '') - self.assertEqual(a.genres, { - 'genre1': 6, 'genre2': 3, - 'tag1': 6, 'tag2': 3}) + self.assertEqual(a._genres, { + 'genre1': 6, 'genre2': 3 + }) + self.assertEqual(a._folksonomy_tags, { + 'tag1': 6, 'tag2': 3 + }) for artist in a._album_artists: - self.assertEqual(artist.genres, { + self.assertEqual(artist._folksonomy_tags, { 'british': 2, 'progressive rock': 10}) @@ -166,11 +169,14 @@ def test_release_without_release_relationships(self): self.assertEqual(m['~albumartists_sort'], 'Pink Floyd') self.assertEqual(m['~releaselanguage'], 'eng') self.assertEqual(m.getall('~releasecountries'), ['GB', 'NZ']) - self.assertEqual(a.genres, { - 'genre1': 6, 'genre2': 3, - 'tag1': 6, 'tag2': 3}) + self.assertEqual(a._genres, { + 'genre1': 6, 'genre2': 3 + }) + self.assertEqual(a._folksonomy_tags, { + 'tag1': 6, 'tag2': 3 + }) for artist in a._album_artists: - self.assertEqual(artist.genres, { + self.assertEqual(artist._folksonomy_tags, { 'british': 2, 'progressive rock': 10}) @@ -293,11 +299,11 @@ def test_recording(self): self.assertEqual(m['~video'], '') self.assertNotIn('originaldate', m) self.assertNotIn('originalyear', m) - self.assertEqual(t.genres, { + self.assertEqual(t._folksonomy_tags, { 'blue-eyed soul': 1, 'pop': 3}) for artist in t._track_artists: - self.assertEqual(artist.genres, { + self.assertEqual(artist._folksonomy_tags, { 'dance-pop': 1, 'guitarist': 0}) @@ -770,7 +776,7 @@ def test_release_group(self): self.assertEqual(m['releasetype'], 'album') self.assertEqual(m['~primaryreleasetype'], 'album') self.assertEqual(m['~releasegroup'], 'The Dark Side of the Moon') - self.assertEqual(r.genres, {'test2': 3, 'test': 6}) + self.assertEqual(r._folksonomy_tags, {'test2': 3, 'test': 6}) class NullReleaseGroupTest(MBJSONTest): From 5b50798ba07dd7274b31a9262133c720c4550f22 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:45:59 +0530 Subject: [PATCH 09/13] removing unneeded config variables --- picard/track.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/picard/track.py b/picard/track.py index be48bc4c89..1e4f6197e6 100644 --- a/picard/track.py +++ b/picard/track.py @@ -313,9 +313,8 @@ def _customize_metadata(self): tm['~silence'] = '1' if config.setting['use_genres']: - config = get_config() - self._add_folksonomy_tags(config) - self._add_genres(config) + self._add_folksonomy_tags() + self._add_genres() self._convert_folksonomy_tags_to_genre() # Convert Unicode punctuation @@ -375,19 +374,18 @@ def _convert_folksonomy_tags_to_genre(self): join_with=config.setting['join_genres'] ) - def _add_tags(self, tags, name, config=None): - config = config or get_config() + def _add_tags(self, tags, name): self.metadata[name] = tags.keys() - def _add_folksonomy_tags(self, config=None): + def _add_folksonomy_tags(self): tags = Counter(self._folksonomy_tags) tags += self.album._folksonomy_tags - self._add_tags(tags, '_folksonomy_tags', config) + self._add_tags(tags, '_folksonomy_tags') - def _add_genres(self, config=None): + def _add_genres(self): genre = Counter(self._genres) genre += self.album._genres - self._add_tags(genre, '_genres', config) + self._add_tags(genre, '_genres') class NonAlbumTrack(Track): From 58286b778343fd23f02df682ca3ee2eea42044c6 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:25:29 +0530 Subject: [PATCH 10/13] Add function to remove genres from tags This function would remove the tags that are present in genre. --- picard/track.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/picard/track.py b/picard/track.py index 1e4f6197e6..28c06d066e 100644 --- a/picard/track.py +++ b/picard/track.py @@ -313,9 +313,10 @@ def _customize_metadata(self): tm['~silence'] = '1' if config.setting['use_genres']: + self._convert_folksonomy_tags_to_genre() + self._delete_genres_from_tags() self._add_folksonomy_tags() self._add_genres() - self._convert_folksonomy_tags_to_genre() # Convert Unicode punctuation if config.setting['convert_punctuation']: @@ -387,6 +388,10 @@ def _add_genres(self): genre += self.album._genres self._add_tags(genre, '_genres') + def _delete_genres_from_tags(self): + for genres in self.album.genres: + del self.album._folksonomy_tags[genres] + class NonAlbumTrack(Track): From 3f0a795d8bbceba4d239480a9d38d64cc1bd237e Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Sun, 17 Nov 2024 23:28:51 +0530 Subject: [PATCH 11/13] Change genres to genre --- picard/track.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/picard/track.py b/picard/track.py index 28c06d066e..f31d63c5fa 100644 --- a/picard/track.py +++ b/picard/track.py @@ -389,8 +389,8 @@ def _add_genres(self): self._add_tags(genre, '_genres') def _delete_genres_from_tags(self): - for genres in self.album.genres: - del self.album._folksonomy_tags[genres] + for genre in self.album.genres: + del self.album._folksonomy_tags[genre] class NonAlbumTrack(Track): From bf8b6b7d82f8b1cc150fa9129f8886bf72e7d3b5 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Mon, 30 Dec 2024 22:41:28 +0400 Subject: [PATCH 12/13] Rename _genres and _folksonomy_tags to genre and folksonomy_tags --- picard/track.py | 14 +++++++------- test/test_mbjson.py | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/picard/track.py b/picard/track.py index f31d63c5fa..efef149065 100644 --- a/picard/track.py +++ b/picard/track.py @@ -352,9 +352,9 @@ def _convert_folksonomy_tags_to_genre(self): genres = Counter(self.genres) use_folksonomy = config.setting['folksonomy_tags'] if use_folksonomy: - genres += self.album._folksonomy_tags + genres += self.album.folksonomy_tags else: - genres += self.album._genres + genres += self.album.genres # Combine release and track genres if self.album.release_group: genres += self.album.release_group.genres @@ -379,18 +379,18 @@ def _add_tags(self, tags, name): self.metadata[name] = tags.keys() def _add_folksonomy_tags(self): - tags = Counter(self._folksonomy_tags) - tags += self.album._folksonomy_tags + tags = Counter(self.folksonomy_tags) + tags += self.album.folksonomy_tags self._add_tags(tags, '_folksonomy_tags') def _add_genres(self): - genre = Counter(self._genres) - genre += self.album._genres + genre = Counter(self.genres) + genre += self.album.genres self._add_tags(genre, '_genres') def _delete_genres_from_tags(self): for genre in self.album.genres: - del self.album._folksonomy_tags[genre] + del self.album.folksonomy_tags[genre] class NonAlbumTrack(Track): diff --git a/test/test_mbjson.py b/test/test_mbjson.py index 6bbecfdc01..7756c9d776 100644 --- a/test/test_mbjson.py +++ b/test/test_mbjson.py @@ -169,14 +169,14 @@ def test_release_without_release_relationships(self): self.assertEqual(m['~albumartists_sort'], 'Pink Floyd') self.assertEqual(m['~releaselanguage'], 'eng') self.assertEqual(m.getall('~releasecountries'), ['GB', 'NZ']) - self.assertEqual(a._genres, { + self.assertEqual(a.genres, { 'genre1': 6, 'genre2': 3 }) - self.assertEqual(a._folksonomy_tags, { + self.assertEqual(a.folksonomy_tags, { 'tag1': 6, 'tag2': 3 }) for artist in a._album_artists: - self.assertEqual(artist._folksonomy_tags, { + self.assertEqual(artist.folksonomy_tags, { 'british': 2, 'progressive rock': 10}) @@ -299,11 +299,11 @@ def test_recording(self): self.assertEqual(m['~video'], '') self.assertNotIn('originaldate', m) self.assertNotIn('originalyear', m) - self.assertEqual(t._folksonomy_tags, { + self.assertEqual(t.folksonomy_tags, { 'blue-eyed soul': 1, 'pop': 3}) for artist in t._track_artists: - self.assertEqual(artist._folksonomy_tags, { + self.assertEqual(artist.folksonomy_tags, { 'dance-pop': 1, 'guitarist': 0}) @@ -776,7 +776,7 @@ def test_release_group(self): self.assertEqual(m['releasetype'], 'album') self.assertEqual(m['~primaryreleasetype'], 'album') self.assertEqual(m['~releasegroup'], 'The Dark Side of the Moon') - self.assertEqual(r._folksonomy_tags, {'test2': 3, 'test': 6}) + self.assertEqual(r.folksonomy_tags, {'test2': 3, 'test': 6}) class NullReleaseGroupTest(MBJSONTest): From 539e104fb2deec4b10c87359b09020ba51b88e84 Mon Sep 17 00:00:00 2001 From: Rakim Middya <103818757+rakim-0@users.noreply.github.com> Date: Mon, 30 Dec 2024 22:51:27 +0400 Subject: [PATCH 13/13] Move delete_genre_from_tag from track.py to album.py --- picard/album.py | 4 ++++ picard/track.py | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/picard/album.py b/picard/album.py index 89100701ce..23d2053505 100644 --- a/picard/album.py +++ b/picard/album.py @@ -856,6 +856,10 @@ def children_metadata_items(self): yield from track.files yield from self.unmatched_files.files + def delete_genres_from_tags(self): + for genre in self.genres: + del self.folksonomy_tags[genre] + class NatAlbum(Album): diff --git a/picard/track.py b/picard/track.py index efef149065..d288c86fa1 100644 --- a/picard/track.py +++ b/picard/track.py @@ -314,7 +314,7 @@ def _customize_metadata(self): if config.setting['use_genres']: self._convert_folksonomy_tags_to_genre() - self._delete_genres_from_tags() + self.album.delete_genres_from_tags() self._add_folksonomy_tags() self._add_genres() @@ -388,10 +388,6 @@ def _add_genres(self): genre += self.album.genres self._add_tags(genre, '_genres') - def _delete_genres_from_tags(self): - for genre in self.album.genres: - del self.album.folksonomy_tags[genre] - class NonAlbumTrack(Track):