From 0d869340ecc41d9f0b2e85280f9d3fb31198338f Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 18 Feb 2011 12:17:22 -0800 Subject: [PATCH] fixes for album artist inference - Inference must be enabled explicitly with the "infer_aa" flag. It does not happen transparently. - Infer both artist and artist ID. - Fixed a bug where only the database row was using the inferred data, not the returned data structure. - Added tests. --- beets/library.py | 19 +++++++++++++------ test/test_db.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/beets/library.py b/beets/library.py index 4267ee78a..5afaf9b70 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1080,18 +1080,25 @@ class Library(BaseLibrary): if record: return Album(self, dict(record)) - def add_album(self, items): + def add_album(self, items, infer_aa=False): """Create a new album in the database with metadata derived from its items. The items are added to the database if they - don't yet have an ID. Returns an Album object. + don't yet have an ID. Returns an Album object. If the + infer_aa flag is set, then the album artist field will be + guessed from artist fields when not present. """ # Set the metadata from the first item. #fixme: check for consensus? item_values = dict( (key, getattr(items[0], key)) for key in ALBUM_KEYS_ITEM) - if not item_values['albumartist']: - item_values['albumartist'] = getattr(items[0], 'artist') - + if infer_aa: + namemap = { + 'albumartist': 'artist', + 'mb_albumartistid': 'mb_artistid', + } + for field, itemfield in namemap.iteritems(): + if not item_values[field]: + item_values[field] = getattr(items[0], itemfield) sql = 'INSERT INTO albums (%s) VALUES (%s)' % \ (', '.join(ALBUM_KEYS_ITEM), @@ -1104,7 +1111,7 @@ class Library(BaseLibrary): record = {} for key in ALBUM_KEYS: if key in ALBUM_KEYS_ITEM: - record[key] = getattr(items[0], key) + record[key] = item_values[key] else: # Non-item fields default to None. record[key] = None diff --git a/test/test_db.py b/test/test_db.py index aa8bd0c8f..1afd67e0a 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -445,6 +445,27 @@ class AlbumInfoTest(unittest.TestCase): self.assertEqual(ai.album, self.i.album) self.assertEqual(ai.year, self.i.year) + def test_infer_aa_gets_artist_and_id(self): + i = item() + i.albumartist = '' + i.mb_albumartistid = '' + ai = self.lib.add_album((i,), infer_aa=True) + self.assertEqual(ai.albumartist, i.artist) + self.assertEqual(ai.mb_albumartistid, i.mb_artistid) + + def test_no_infer_aa_sets_blank_artist_and_id(self): + i = item() + i.albumartist = '' + i.mb_albumartistid = '' + ai = self.lib.add_album((i,), infer_aa=False) + self.assertEqual(ai.albumartist, '') + self.assertEqual(ai.mb_albumartistid, '') + + def test_infer_aa_lets_album_values_override(self): + ai = self.lib.add_album((self.i,), infer_aa=True) + self.assertEqual(ai.albumartist, self.i.albumartist) + self.assertEqual(ai.mb_albumartistid, self.i.mb_albumartistid) + def test_albuminfo_stores_art(self): ai = self.lib.get_album(self.i) ai.artpath = '/my/great/art'