From 898b4bd24e5fcf1cd2054d4faf4ef438b1fed102 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 6 Aug 2010 10:36:17 -0700 Subject: [PATCH] destination now uses album values when available When computing track destination paths, we now look for album-level values when they're available. This has the effect of making albums go into a single directory even when their tracks have heterogeneous metadata. We will need to revisit this once we start explicitly supporting non-album tracks. --- beets/library.py | 18 ++++++++++++++---- test/test_db.py | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) 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.