diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 1500538d3..81c2fadb1 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -39,11 +39,19 @@ except AttributeError: # Classes used to represent candidate options. -class Map(dict): +class AttrDict(dict): """ - Example: - m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, - sports=['Soccer']) + Dictionary with flexible attributes + to get an tag value: + value = info.tag + or value = info[tag] + or value = info.get(tag) + or value = getattr(info, tag) + all raise AttributeError when info doesn't have tag + to set a tag value: + info.tag = value + or info[tag] = value + or setattr(info, tag, value) """ def __getattr__(self, attr): @@ -59,7 +67,7 @@ class Map(dict): return id(self) -class AlbumInfo(Map): +class AlbumInfo(AttrDict): """Describes a canonical release that may be used to match a release in the library. Consists of these data members: @@ -117,8 +125,7 @@ class AlbumInfo(Map): self.discogs_albumid = discogs_albumid self.discogs_labelid = discogs_labelid self.discogs_artistid = discogs_artistid - for arg in kwargs: - self.__setattr__(arg, kwargs[arg]) + self.update(kwargs) # Work around a bug in python-musicbrainz-ngs that causes some # strings to be bytes rather than Unicode. @@ -142,7 +149,7 @@ class AlbumInfo(Map): track.decode(codec) -class TrackInfo(Map): +class TrackInfo(AttrDict): """Describes a canonical track present on a release. Appears as part of an AlbumInfo's ``tracks`` list. Consists of these data members: @@ -189,8 +196,7 @@ class TrackInfo(Map): self.bpm = bpm self.initial_key = initial_key self.genre = genre - for arg in kwargs: - self.__setattr__(arg, kwargs[arg]) + self.update(kwargs) # As above, work around a bug in python-musicbrainz-ngs. def decode(self, codec='utf-8'): diff --git a/test/test_autotag.py b/test/test_autotag.py index af74b5c8f..3532a80ec 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -754,36 +754,10 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): def test_album_artist_overrides_empty_track_artist(self): # make a deepcopy of self.info trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - medium=1, - medium_index=1, - medium_total=1, - index=1, - artist_credit='trackArtistCredit', - artist_sort='trackArtistSort', - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - medium=2, - medium_index=1, - index=2, - medium_total=1, - )) - my_info = AlbumInfo( - tracks=trackinfo, - artist=u'artistNew', - album=u'albumNew', - album_id='7edb51cb-77d6-4416-a23c-3a8c2994a2c7', - artist_id='a6623d39-2d8e-4f70-8242-0a9553b91e50', - artist_credit=u'albumArtistCredit', - artist_sort=u'albumArtistSort', - albumtype=u'album', - va=False, - mediums=2, - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + my_info = AlbumInfo(self.info) + my_info.tracks = trackinfo self._apply(info=my_info) self.assertEqual(self.items[0].artist, 'artistNew') self.assertEqual(self.items[1].artist, 'artistNew') @@ -791,36 +765,10 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): def test_album_artist_overridden_by_nonempty_track_artist(self): # make a deepcopy of self.info trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - medium=1, - medium_index=1, - medium_total=1, - index=1, - artist_credit='trackArtistCredit', - artist_sort='trackArtistSort', - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - medium=2, - medium_index=1, - index=2, - medium_total=1, - )) - my_info = AlbumInfo( - tracks=trackinfo, - artist=u'artistNew', - album=u'albumNew', - album_id='7edb51cb-77d6-4416-a23c-3a8c2994a2c7', - artist_id='a6623d39-2d8e-4f70-8242-0a9553b91e50', - artist_credit=u'albumArtistCredit', - artist_sort=u'albumArtistSort', - albumtype=u'album', - va=False, - mediums=2, - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + my_info = AlbumInfo(self.info) + my_info.tracks = trackinfo my_info.tracks[0].artist = 'artist1!' my_info.tracks[1].artist = 'artist2!' self._apply(info=my_info) @@ -844,36 +792,10 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): def test_full_date_applied(self): # make a deepcopy of self.info trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - medium=1, - medium_index=1, - medium_total=1, - index=1, - artist_credit='trackArtistCredit', - artist_sort='trackArtistSort', - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - medium=2, - medium_index=1, - index=2, - medium_total=1, - )) - my_info = AlbumInfo( - tracks=trackinfo, - artist=u'artistNew', - album=u'albumNew', - album_id='7edb51cb-77d6-4416-a23c-3a8c2994a2c7', - artist_id='a6623d39-2d8e-4f70-8242-0a9553b91e50', - artist_credit=u'albumArtistCredit', - artist_sort=u'albumArtistSort', - albumtype=u'album', - va=False, - mediums=2, - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + my_info = AlbumInfo(self.info) + my_info.tracks = trackinfo my_info.year = 2013 my_info.month = 12 my_info.day = 18 @@ -890,36 +812,10 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): # make a deepcopy of self.info trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - medium=1, - medium_index=1, - medium_total=1, - index=1, - artist_credit='trackArtistCredit', - artist_sort='trackArtistSort', - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - medium=2, - medium_index=1, - index=2, - medium_total=1, - )) - my_info = AlbumInfo( - tracks=trackinfo, - artist=u'artistNew', - album=u'albumNew', - album_id='7edb51cb-77d6-4416-a23c-3a8c2994a2c7', - artist_id='a6623d39-2d8e-4f70-8242-0a9553b91e50', - artist_credit=u'albumArtistCredit', - artist_sort=u'albumArtistSort', - albumtype=u'album', - va=False, - mediums=2, - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + my_info = AlbumInfo(self.info) + my_info.tracks = trackinfo my_info.year = 2013 self._apply(info=my_info) @@ -941,36 +837,10 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): def test_data_source_applied(self): # make a deepcopy of self.info trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - medium=1, - medium_index=1, - medium_total=1, - index=1, - artist_credit='trackArtistCredit', - artist_sort='trackArtistSort', - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - medium=2, - medium_index=1, - index=2, - medium_total=1, - )) - my_info = AlbumInfo( - tracks=trackinfo, - artist=u'artistNew', - album=u'albumNew', - album_id='7edb51cb-77d6-4416-a23c-3a8c2994a2c7', - artist_id='a6623d39-2d8e-4f70-8242-0a9553b91e50', - artist_credit=u'albumArtistCredit', - artist_sort=u'albumArtistSort', - albumtype=u'album', - va=False, - mediums=2, - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + my_info = AlbumInfo(self.info) + my_info.tracks = trackinfo my_info.data_source = 'MusicBrainz' self._apply(info=my_info) @@ -981,7 +851,6 @@ class ApplyCompilationTest(_common.TestCase, ApplyTestUtil): def setUp(self): super(ApplyCompilationTest, self).setUp() - # make a deepcopy of self.info self.items = [] self.items.append(Item({})) self.items.append(Item({})) @@ -1035,29 +904,10 @@ class ApplyCompilationTest(_common.TestCase, ApplyTestUtil): def test_va_flag_sets_comp(self): # make a deepcopy of self.info trackinfo = [] - trackinfo = [] - trackinfo.append(TrackInfo( - title=u'oneNew', - track_id=u'dfa939ec-118c-4d0f-84a0-60f3d1e6522c', - artist=u'artistOneNew', - artist_id=u'a05686fc-9db2-4c23-b99e-77f5db3e5282', - index=1, - )) - trackinfo.append(TrackInfo( - title=u'twoNew', - track_id=u'40130ed1-a27c-42fd-a328-1ebefb6caef4', - artist=u'artistTwoNew', - artist_id=u'80b3cf5e-18fe-4c59-98c7-e5bb87210710', - index=2, - )) - va_info = AlbumInfo( - tracks=trackinfo, - artist=u'variousNew', - album=u'albumNew', - album_id='3b69ea40-39b8-487f-8818-04b6eff8c21a', - artist_id='89ad4ac3-39f7-470e-963a-56509c546377', - albumtype=u'compilation', - ) + trackinfo.append(TrackInfo(self.info.tracks[0])) + trackinfo.append(TrackInfo(self.info.tracks[1])) + va_info = AlbumInfo(self.info) + va_info.tracks = trackinfo va_info.va = True self._apply(info=va_info) self.assertTrue(self.items[0].comp)