mirror of
https://github.com/beetbox/beets.git
synced 2026-01-09 17:33:51 +01:00
add_art convenience function takes care of copying to destination
This commit is contained in:
parent
304e4d6708
commit
6a3c024005
2 changed files with 25 additions and 4 deletions
|
|
@ -976,6 +976,18 @@ class Library(BaseLibrary):
|
|||
_, ext = os.path.splitext(image)
|
||||
dest = os.path.join(item_dir, self._library.art_filename + ext)
|
||||
return dest
|
||||
|
||||
def set_art(self, path):
|
||||
"""Sets the album's cover art to the image at the given path.
|
||||
The image is copied into place, replacing any existing art.
|
||||
"""
|
||||
oldart = self.artpath
|
||||
artdest = self.art_destination(path)
|
||||
if oldart == artdest:
|
||||
os.unlink(oldart)
|
||||
|
||||
shutil.copy(path, artdest)
|
||||
self.artpath = artdest
|
||||
|
||||
def get_album(self, item_or_id):
|
||||
"""Given an album ID or an item associated with an album,
|
||||
|
|
|
|||
|
|
@ -438,15 +438,14 @@ class ArtFileTest(unittest.TestCase):
|
|||
self.lib.directory = self.libdir
|
||||
self.i = item()
|
||||
self.i.path = self.lib.destination(self.i)
|
||||
# Make a file.
|
||||
# Make a music file.
|
||||
beets.library._mkdirall(self.i.path)
|
||||
_touch(self.i.path)
|
||||
self.lib.add_album(self.i.artist, self.i.album, (self.i,))
|
||||
# Make an album.
|
||||
self.ai = self.lib.add_album(self.i.artist, self.i.album, (self.i,))
|
||||
# Make an art file too.
|
||||
self.art = self.lib.get_album(self.i).art_destination('something.jpg')
|
||||
_touch(self.art)
|
||||
# Make an album.
|
||||
self.ai = self.lib.get_album(self.i)
|
||||
self.ai.artpath = self.art
|
||||
def tearDown(self):
|
||||
if os.path.exists(self.libdir):
|
||||
|
|
@ -469,6 +468,16 @@ class ArtFileTest(unittest.TestCase):
|
|||
newart = self.lib.get_album(self.i).art_destination(self.art)
|
||||
self.assertTrue(os.path.exists(newart))
|
||||
|
||||
def test_setart_copies_image(self):
|
||||
newart = os.path.join(self.libdir, 'newart.jpg')
|
||||
_touch(newart)
|
||||
i2 = item()
|
||||
i2.artist = 'someArtist'
|
||||
ai = self.lib.add_album(i2.artist, i2.album, (i2,))
|
||||
self.assertEqual(ai.artpath, None)
|
||||
ai.set_art(newart)
|
||||
self.assertTrue(os.path.exists(ai.artpath))
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue