From a9b4169ee2b1b423fd8866e6bf082033ff82f5d8 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 18 Dec 2013 16:20:40 -0800 Subject: [PATCH] zero out month & day when missing from match --- beets/autotag/__init__.py | 22 ++++++++++++++-------- docs/changelog.rst | 2 ++ test/test_autotag.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/beets/autotag/__init__.py b/beets/autotag/__init__.py index b63232794..6b0c1185c 100644 --- a/beets/autotag/__init__.py +++ b/beets/autotag/__init__.py @@ -184,14 +184,20 @@ def apply_metadata(album_info, mapping): for suffix in 'year', 'month', 'day': key = prefix + suffix - value = getattr(album_info, key) - if value: - setattr(item, key, value) - if config['original_date']: - # If we're using original release date for both - # fields, set item.year = info.original_year, - # etc. - setattr(item, suffix, value) + value = getattr(album_info, key) or 0 + + # If we don't even have a year, apply nothing. + if suffix == 'year' and not value: + break + + # Otherwise, set the fetched value (or 0 for the month + # and day if not available). + item[key] = value + + # If we're using original release date for both fields, + # also set item.year = info.original_year, etc. + if config['original_date']: + item[suffix] = value # Title. item.title = track_info.title diff --git a/docs/changelog.rst b/docs/changelog.rst index e311096c8..b58803972 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -67,6 +67,8 @@ Little fixes: equivalent to the empty string). * :doc:`/plugins/lastgenre`: Fix a conflict between canonicalization and multiple genres. +* When a match has a year but not a month or day, the autotagger now "zeros + out" the month and day fields after applying the year. .. _Acoustic Attributes: http://developer.echonest.com/acoustic-attributes.html .. _MPD: http://mpd.wikia.com/wiki/Music_Player_Daemon_Wiki diff --git a/test/test_autotag.py b/test/test_autotag.py index 138bae7d6..1490da42a 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -821,6 +821,41 @@ class ApplyTest(_common.TestCase, ApplyTestUtil): self.assertEqual(self.items[1].albumartist_sort, 'albumArtistSort') self.assertEqual(self.items[1].artist_sort, 'albumArtistSort') + def test_full_date_applied(self): + my_info = copy.deepcopy(self.info) + my_info.year = 2013 + my_info.month = 12 + my_info.day = 18 + self._apply(info=my_info) + + self.assertEqual(self.items[0].year, 2013) + self.assertEqual(self.items[0].month, 12) + self.assertEqual(self.items[0].day, 18) + + def test_date_only_zeros_month_and_day(self): + self.items = [] + self.items.append(Item(year=1, month=2, day=3)) + self.items.append(Item(year=4, month=5, day=6)) + + my_info = copy.deepcopy(self.info) + my_info.year = 2013 + self._apply(info=my_info) + + self.assertEqual(self.items[0].year, 2013) + self.assertEqual(self.items[0].month, 0) + self.assertEqual(self.items[0].day, 0) + + def test_missing_date_applies_nothing(self): + self.items = [] + self.items.append(Item(year=1, month=2, day=3)) + self.items.append(Item(year=4, month=5, day=6)) + + self._apply() + + self.assertEqual(self.items[0].year, 1) + self.assertEqual(self.items[0].month, 2) + self.assertEqual(self.items[0].day, 3) + class ApplyCompilationTest(_common.TestCase, ApplyTestUtil): def setUp(self): super(ApplyCompilationTest, self).setUp()