diff --git a/beets/library.py b/beets/library.py index 3adea78e8..586f9c83b 100644 --- a/beets/library.py +++ b/beets/library.py @@ -805,13 +805,23 @@ class Library(BaseLibrary): item (i.e., where the file ought to be). """ subpath_tmpl = Template(self.path_format) + + # Get the item's Album if it has one. + album = self.get_album(item) - # build the mapping for substitution in the path template, beginning - # with the values from the database + # Build the mapping for substitution in the path template, + # beginning with the values from the database. mapping = {} for key in ITEM_KEYS_META: - value = getattr(item, key) - # sanitize the value for inclusion in a path: + # Get the values from either the item or its album. + if key in ALBUM_KEYS_ITEM and album is not None: + # From album. + value = getattr(album, key) + else: + # From Item. + value = getattr(item, key) + + # Sanitize the value for inclusion in a path: # replace / and leading . with _ if isinstance(value, basestring): value = value.replace(os.sep, '_') diff --git a/test/test_db.py b/test/test_db.py index c81b1c15a..964201a3a 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -254,6 +254,14 @@ class DestinationTest(unittest.TestCase): p = self.lib.destination(self.i) self.assert_('(FLAC)' in p) + def test_heterogeneous_album_gets_single_directory(self): + i1, i2 = item(), item() + self.lib.add_album([i1, i2]) + i1.year, i2.year = 2009, 2010 + self.lib.path_format = '$album ($year)/$track $title' + dest1, dest2 = self.lib.destination(i1), self.lib.destination(i2) + self.assertEqual(os.path.dirname(dest1), os.path.dirname(dest2)) + class MigrationTest(unittest.TestCase): """Tests the ability to change the database schema between versions.