fix field inference w/ null first item (closes #14 on GitHub)

When a partial match is found, its first item (task.items[0]) may be None, and
_infer_album_fields would crash in this case. This solution walks through the
items list and finds the first non-None item.
This commit is contained in:
Adrian Sampson 2011-12-16 16:45:50 -08:00
parent 621b3d4bb7
commit 08b539a80e
3 changed files with 21 additions and 6 deletions

View file

@ -170,18 +170,25 @@ def _infer_album_fields(task):
elif task.choice_flag == action.APPLY:
# Applying autotagged metadata. Just get AA from the first
# item.
if not task.items[0].albumartist:
changes['albumartist'] = task.items[0].artist
if not task.items[0].mb_albumartistid:
changes['mb_albumartistid'] = task.items[0].mb_artistid
for item in task.items:
if item is not None:
first_item = item
break
else:
assert False, "all items are None"
if not first_item.albumartist:
changes['albumartist'] = first_item.artist
if not first_item.mb_albumartistid:
changes['mb_albumartistid'] = first_item.mb_artistid
else:
assert False
# Apply new metadata.
for item in task.items:
for k, v in changes.iteritems():
setattr(item, k, v)
if item is not None:
for k, v in changes.iteritems():
setattr(item, k, v)
def _open_state():
"""Reads the state file, returning a dictionary."""

View file

@ -13,6 +13,7 @@ This release focuses on making beets' path formatting vastly more powerful.
functions via a plugin, see :ref:`writing-plugins`.
* Fix an incompatibility in BPD with libmpc (the library that powers mpc and
ncmpc).
* Fix a crash when importing a partial match whose first track was missing.
1.0b11 (December 12, 2011)
--------------------------

View file

@ -541,6 +541,13 @@ class InferAlbumDataTest(unittest.TestCase):
self._infer()
self.assertFalse(self.items[0].comp)
def test_first_item_null_apply(self):
self.items[0] = None
self.task.set_choice(({}, self.items)) # APPLY
self._infer()
self.assertFalse(self.items[1].comp)
self.assertEqual(self.items[1].albumartist, self.items[2].artist)
class DuplicateCheckTest(unittest.TestCase):
def setUp(self):
self.lib = library.Library(':memory:')