configurable album art path construction

This commit is contained in:
Adrian Sampson 2010-07-13 22:00:42 -07:00
parent f1870b7941
commit 5bb064a860
4 changed files with 33 additions and 4 deletions

View file

@ -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.

View file

@ -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:

Binary file not shown.

View file

@ -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')