diff --git a/beets/library.py b/beets/library.py index 8b2c862b4..8388ff170 100644 --- a/beets/library.py +++ b/beets/library.py @@ -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, diff --git a/test/test_db.py b/test/test_db.py index 333d3c237..912696c81 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -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__)