From b954c5e30dc5568429157c8ab9998feebb2cccf0 Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Mon, 10 May 2021 04:55:03 +0200 Subject: [PATCH] TODO: interactive rebase to actual cool named commits --- mediafile.py | 48 ++++++++++++++++++++++++++++++++++-------- test/test_mediafile.py | 24 +++++++++++++++------ 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/mediafile.py b/mediafile.py index dacd519..8e8b105 100644 --- a/mediafile.py +++ b/mediafile.py @@ -889,6 +889,29 @@ def delete(self, mutagen_file): del mutagen_file[frame.HashKey] +class MP3ListDescStorageStyle(MP3DescStorageStyle, ListStorageStyle): + def __init__(self, desc=u'', key='TXXX', **kwargs): + super(MP3ListDescStorageStyle, self).__init__(desc=desc, key=key, + **kwargs) + + def fetch(self, mutagen_file): + for frame in mutagen_file.tags.getall(self.key): + if frame.desc.lower() == self.description.lower(): + return frame.text + return [] + + def store(self, mutagen_file, values): + self.delete(mutagen_file) + frame = mutagen.id3.Frames[self.key]( + desc=self.description, + text=values, + encoding=mutagen.id3.Encoding.UTF8, + ) + if self.id3_lang: + frame.lang = self.id3_lang + mutagen_file.tags.add(frame) + + class MP3SlashPackStorageStyle(MP3StorageStyle): """Store value as part of pair that is serialized as a slash- separated string. @@ -1644,7 +1667,7 @@ def update(self, dict): ASFStorageStyle('Author'), ) artists = ListMediaField( - # MP3ListDescStorageStyle(desc='ARTISTS'), + MP3ListDescStorageStyle(desc=u'ARTISTS'), MP4ListStorageStyle('----:com.apple.iTunes:ARTISTS'), ListStorageStyle('ARTISTS'), ASFStorageStyle('WM/ARTISTS'), @@ -1769,6 +1792,12 @@ def update(self, dict): StorageStyle('ALBUMARTIST'), ASFStorageStyle('WM/AlbumArtist'), ) + albumartists = ListMediaField( + MP3ListDescStorageStyle(desc=u'ALBUMARTISTS'), + MP4ListStorageStyle('----:com.apple.iTunes:ALBUMARTISTS'), + ListStorageStyle('ALBUMARTISTS'), + ASFStorageStyle('WM/AlbumArtists'), + ) albumtype = MediaField( MP3DescStorageStyle(u'MusicBrainz Album Type'), MP4StorageStyle('----:com.apple.iTunes:MusicBrainz Album Type'), @@ -1945,16 +1974,17 @@ def update(self, dict): StorageStyle('MUSICBRAINZ_ALBUMID'), ASFStorageStyle('MusicBrainz/Album Id'), ) - mb_artistid = MediaField( - MP3DescStorageStyle(u'MusicBrainz Artist Id'), - MP4StorageStyle('----:com.apple.iTunes:MusicBrainz Artist Id'), - StorageStyle('MUSICBRAINZ_ARTISTID'), + mb_artistid = ListMediaField( + MP3ListDescStorageStyle(u'MusicBrainz Artist Id'), + MP4ListStorageStyle('----:com.apple.iTunes:MusicBrainz Artist Id'), + ListStorageStyle('MUSICBRAINZ_ARTISTID'), ASFStorageStyle('MusicBrainz/Artist Id'), ) - mb_albumartistid = MediaField( - MP3DescStorageStyle(u'MusicBrainz Album Artist Id'), - MP4StorageStyle('----:com.apple.iTunes:MusicBrainz Album Artist Id'), - StorageStyle('MUSICBRAINZ_ALBUMARTISTID'), + mb_albumartistid = ListMediaField( + MP3ListDescStorageStyle(u'MusicBrainz Album Artist Id'), + MP4ListStorageStyle( + '----:com.apple.iTunes:MusicBrainz Album Artist Id'), + ListStorageStyle('MUSICBRAINZ_ALBUMARTISTID'), ASFStorageStyle('MusicBrainz/Album Artist Id'), ) mb_releasegroupid = MediaField( diff --git a/test/test_mediafile.py b/test/test_mediafile.py index 2fa04fa..2dd4bdf 100644 --- a/test/test_mediafile.py +++ b/test/test_mediafile.py @@ -340,7 +340,7 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin, 'mb_trackid': '8b882575-08a5-4452-a7a7-cbb8a1531f9e', 'mb_releasetrackid': 'c29f3a57-b439-46fd-a2e2-93776b1371e0', 'mb_albumid': '9e873859-8aa4-4790-b985-5a953e8ef628', - 'mb_artistid': '7cf0ea9d-86b9-4dad-ba9e-2355a64899ea', + 'mb_artistid': ['7cf0ea9d-86b9-4dad-ba9e-2355a64899ea'], 'art': None, 'label': u'the label', } @@ -348,7 +348,6 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin, tag_fields = [ 'title', 'artist', - 'artists' 'album', 'genre', 'lyricist', @@ -373,7 +372,6 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin, 'mb_releasetrackid', 'mb_workid', 'mb_albumid', - 'mb_artistid', 'art', 'label', 'rg_track_peak', @@ -383,7 +381,6 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin, 'r128_track_gain', 'r128_album_gain', 'albumartist', - 'mb_albumartistid', 'artist_sort', 'albumartist_sort', 'acoustid_fingerprint', @@ -452,7 +449,11 @@ def test_read_full(self): def test_read_empty(self): mediafile = self._mediafile_fixture('empty') for field in self.tag_fields: - self.assertIsNone(getattr(mediafile, field)) + value = getattr(mediafile, field) + if isinstance(value, list): + assert len(value) == 0 + else: + self.assertIsNone(value) def test_write_empty(self): mediafile = self._mediafile_fixture('empty') @@ -623,7 +624,11 @@ def test_delete_tag(self): mediafile = MediaFile(mediafile.path) for key in keys: - self.assertIsNone(getattr(mediafile, key)) + value = getattr(mediafile, key) + if isinstance(value, list): + assert len(value) == 0 + else: + self.assertIsNone(value) def test_delete_packed_total(self): mediafile = self._mediafile_fixture('full') @@ -708,6 +713,10 @@ def _generate_tags(self, base=None): for key in ['disc', 'disctotal', 'track', 'tracktotal', 'bpm']: tags[key] = 1 + for key in ['artists', 'albumartists', 'mb_artistid', + 'mb_albumartistid']: + tags[key] = ['multival', 'test'] + tags['art'] = self.jpg_data tags['comp'] = True @@ -979,7 +988,8 @@ def test_properties_from_readable_fields(self): def test_known_fields(self): fields = list(ReadWriteTestBase.tag_fields) - fields.extend(('encoder', 'images', 'genres', 'albumtype')) + fields.extend(('encoder', 'images', 'genres', 'albumtype', 'artists', + 'albumartists', 'mb_artistid', 'mb_albumartistid')) assertCountEqual(self, MediaFile.fields(), fields) def test_fields_in_readable_fields(self):