From bb964a7c479e0e070e010d8374385c45a7099ead Mon Sep 17 00:00:00 2001 From: Simon Chopin Date: Tue, 22 Nov 2011 12:51:37 +0100 Subject: [PATCH] autotag: Fill the blanks when ordering incomplete album In the function order_items, instead of automatically reject the canonical candidate if it has more tracks, the function still tries to find matches for the tracks amongst the items, and otherwise uses None to fill the void in order to keep the information about the track numbers --- beets/autotag/match.py | 7 ++++--- test/test_autotag.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/beets/autotag/match.py b/beets/autotag/match.py index bc6a506f6..d6566c2dd 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -174,8 +174,9 @@ def order_items(items, trackinfo): information. This always produces a result if the numbers of tracks match. """ - # Make sure lengths match. - if len(items) != len(trackinfo): + # Make sure lengths match: If there is less items, it might just be that + # there is some tracks missing. + if len(items) > len(trackinfo): return None # Construct the cost matrix. @@ -190,7 +191,7 @@ def order_items(items, trackinfo): matching = Munkres().compute(costs) # Order items based on the matching. - ordered_items = [None]*len(items) + ordered_items = [None]*len(trackinfo) for cur_idx, canon_idx in matching: ordered_items[canon_idx] = items[cur_idx] return ordered_items diff --git a/test/test_autotag.py b/test/test_autotag.py index 0b5ccc535..c8fd3dca2 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -251,11 +251,24 @@ class OrderingTest(unittest.TestCase): items = [] items.append(self.item('one', 1)) items.append(self.item('two', 2)) + items.append(self.item('three', 3)) + items.append(self.item('four',4)) trackinfo = [] trackinfo.append(TrackInfo('one', None)) ordered = match.order_items(items, trackinfo) self.assertEqual(ordered, None) + def test_order_works_with_missing_tracks(self) + items = [] + items.append(self.item('one', 1)) + items.append(self.item('two', 2)) + trackinfo = [] + trackinfo.append(TrackInfo('one', None)) + ordered = match.order_items(items, trackinfo) + self.assertEqual(ordered[0].title, 'one') + self.assertEqual(ordered[1].title, 'two') + self.assertEqual(ordered[2], None) + def test_order_corrects_when_track_names_are_entirely_wrong(self): # A real-world test case contributed by a user. def item(i, length):