diff --git a/beets/library.py b/beets/library.py index cfbf6b7ad..9448d234c 100644 --- a/beets/library.py +++ b/beets/library.py @@ -703,11 +703,13 @@ class Library(BaseLibrary): def __init__(self, path='library.blb', directory='~/Music', path_format='$artist/$album/$track $title', + art_filename='cover', item_fields=ITEM_FIELDS, album_fields=ALBUM_FIELDS): self.path = path self.directory = directory self.path_format = path_format + self.art_filename = art_filename self.conn = sqlite3.connect(self.path) self.conn.row_factory = sqlite3.Row @@ -786,8 +788,8 @@ class Library(BaseLibrary): libpath = self.directory subpath_tmpl = Template(self.path_format) - # 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) @@ -814,6 +816,15 @@ class Library(BaseLibrary): return _normpath(os.path.join(libpath, subpath)) + def art_path(self, item, image): + """Returns a path to the destination for the album art image + for the item's album. `image` is the path of the image that + will be moved there (used for its extension). + """ + item_dir = os.path.dirname(self.destination(item)) + _, ext = os.path.splitext(image) + dest = os.path.join(item_dir, self.art_filename + ext) + return dest # Main interface. diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index e504d187f..0f53595d4 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -31,6 +31,7 @@ CONFIG_FILE = os.path.expanduser('~/.beetsconfig') DEFAULT_LIBRARY = '~/.beetsmusic.blb' DEFAULT_DIRECTORY = '~/Music' DEFAULT_PATH_FORMAT = '$artist/$album/$track $title' +DEFAULT_ART_FILENAME = 'cover' # UI exception. Commands should throw this in order to display @@ -369,9 +370,12 @@ def main(): config_val(config, 'beets', 'directory', DEFAULT_DIRECTORY) path_format = options.path_format or \ config_val(config, 'beets', 'path_format', DEFAULT_PATH_FORMAT) + art_filename = \ + config_val(config, 'beets', 'art_filename', DEFAULT_ART_FILENAME) lib = library.Library(os.path.expanduser(libpath), directory, - path_format) + path_format, + art_filename) # Invoke the subcommand. try: diff --git a/test/rsrc/test.blb b/test/rsrc/test.blb index bdbf362bc..1559b449b 100644 Binary files a/test/rsrc/test.blb and b/test/rsrc/test.blb differ diff --git a/test/test_db.py b/test/test_db.py index 3cd386827..d353ba425 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -247,6 +247,21 @@ class DestinationTest(unittest.TestCase): p = beets.library._sanitize_path(u':', 'Darwin') self.assertEqual(p, u'-') +class ArtDestinationTest(unittest.TestCase): + def setUp(self): + self.lib = beets.library.Library(':memory:') + self.i = item() + self.lib.art_filename = 'artimage' + + def test_art_filename_respects_setting(self): + art = self.lib.art_path(self.i, 'something.jpg') + self.assert_('/artimage.jpg' in art) + + def test_art_path_in_item_dir(self): + art = self.lib.art_path(self.i, 'something.jpg') + track = self.lib.destination(self.i) + self.assertEqual(os.path.dirname(art), os.path.dirname(track)) + class MigrationTest(unittest.TestCase): """Tests the ability to change the database schema between versions. @@ -399,4 +414,3 @@ def suite(): if __name__ == '__main__': unittest.main(defaultTest='suite') -