def test_get_cover_many_prefer_embedded(self): # embed one cover, move one to the other dir MP3File(self.file1).set_image(EmbeddedImage.from_path(self.cover1)) os.unlink(self.cover1) self.external_cover = os.path.join(self.dir2, "cover.png") shutil.move(self.cover2, self.external_cover) # move one audio file in each dir shutil.move(self.file1, self.dir1) self.file1 = os.path.join(self.dir1, os.path.basename(self.file1)) shutil.move(self.file2, self.dir2) self.file2 = os.path.join(self.dir2, os.path.basename(self.file2)) song1 = MP3File(self.file1) song2 = MP3File(self.file2) # each should find a cover self.failUnless(self.is_embedded(self.manager.get_cover(song1))) self.failIf(self.is_embedded(self.manager.get_cover(song2))) cover_for = self.manager.get_cover_many # both settings should search both songs before giving up config.set("albumart", "prefer_embedded", True) self.failUnless(self.is_embedded(cover_for([song1, song2]))) self.failUnless(self.is_embedded(cover_for([song2, song1]))) config.set("albumart", "prefer_embedded", False) self.failIf(self.is_embedded(cover_for([song1, song2]))) self.failIf(self.is_embedded(cover_for([song2, song1])))
def test_acquire_prefer_embedded(self): # embed one cover... MP3File(self.file1).set_image(EmbeddedImage.from_path(self.cover1)) os.unlink(self.cover1) self.external_cover = os.path.join(self.dir1, "cover.png") # ...and save a different cover externally shutil.copy(self.cover2, self.external_cover) shutil.move(self.file1, self.dir1) self.file1 = os.path.join(self.dir1, os.path.basename(self.file1)) both_song = MP3File(self.file1) results = [] def acquire(song): def cb(source, result): results.append(result) self.manager.acquire_cover(cb, None, song) def result_was_embedded(): return self.is_embedded(results.pop()) config.set("albumart", "prefer_embedded", True) acquire(both_song) self.failUnless(result_was_embedded(), "Embedded image expected due to prefs") config.set("albumart", "prefer_embedded", False) acquire(both_song) self.failIf(result_was_embedded(), "Got an embedded image despite prefs")
def _execute(self, options, args): if len(args) < 2: raise CommandError(_("Not enough arguments")) image_path = args[0] paths = args[1:] image = EmbeddedImage.from_path(image_path) if not image: raise CommandError(_("Failed to load image file: %r") % image_path) songs = [self.load_song(p) for p in paths] for song in songs: if not song.can_change_images: raise CommandError( _("Image editing not supported for %(file_name)s " "(%(file_format)s)") % { "file_name": song("~filename"), "file_format": song("~format") }) for song in songs: try: song.set_image(image) except AudioFileError as e: raise CommandError(e)
def test_from_path(self): image = EmbeddedImage.from_path(self.filename) self.assertTrue(image) self.assertEqual(image.file.name, self.filename) self.assertEqual(image.mime_type, "image/png") self.assertEqual(image.width, 150) self.assertEqual(image.height, 10) self.assertEqual(image.color_depth, 8)
def test_from_path_empty(self): h, empty = mkstemp() os.close(h) try: image = EmbeddedImage.from_path(empty) self.assertFalse(image) finally: os.remove(empty)
def __set_image(self, menu_item, songs): win = WritingWindow(self.plugin_window, len(songs)) win.show() for song in songs: if song.can_change_images: fileobj = app.cover_manager.get_cover(song) if fileobj: path = fileobj.name image = EmbeddedImage.from_path(path) if image: try: song.set_image(image) except AudioFileError: util.print_exc() if win.step(): break win.destroy() self.plugin_finish()
def test_main(self): # embedd one cover, move one to the other dir MP3File(self.file1).set_image(EmbeddedImage.from_path(self.cover1)) os.unlink(self.cover1) dest = os.path.join(self.dir2, "cover.png") shutil.move(self.cover2, dest) self.cover2 = dest # move one audio file in each dir shutil.move(self.file1, self.dir1) self.file1 = os.path.join(self.dir1, os.path.basename(self.file1)) shutil.move(self.file2, self.dir2) self.file2 = os.path.join(self.dir2, os.path.basename(self.file2)) song1 = MP3File(self.file1) song2 = MP3File(self.file2) def is_embedded(fileobj): return not path_equal(fileobj.name, self.cover2, True) # each should find a cover self.assertTrue(is_embedded(self.manager.get_cover(song1))) self.assertTrue(not is_embedded(self.manager.get_cover(song2))) # both settings should search both songs before giving up config.set("albumart", "prefer_embedded", True) self.assertTrue( is_embedded(self.manager.get_cover_many([song1, song2]))) self.assertTrue( is_embedded(self.manager.get_cover_many([song2, song1]))) config.set("albumart", "prefer_embedded", False) self.assertTrue( not is_embedded(self.manager.get_cover_many([song1, song2]))) self.assertTrue( not is_embedded(self.manager.get_cover_many([song2, song1])))
def _execute(self, options, args): if len(args) < 2: raise CommandError(_("Not enough arguments")) image_path = args[0] paths = args[1:] image = EmbeddedImage.from_path(image_path) if not image: raise CommandError(_("Failed to load image file: %r") % image_path) songs = [self.load_song(p) for p in paths] for song in songs: if not song.can_change_images: raise CommandError( _("Image editing not supported for %(file_name)s " "(%(file_format)s)") % { "file_name": song("~filename"), "file_format": song("~format") }) for song in songs: song.set_image(image)
def test_get_extensions(self): image = EmbeddedImage.from_path(self.filename) self.assertTrue("png" in image.extensions)
def test_not_an_image(self): path = get_data_path('test-2.wma') image = EmbeddedImage.from_path(path) self.assertFalse(image)
def test_from_path_bogus(self): image = EmbeddedImage.from_path(self.filename + "nope") self.assertFalse(image)
def test_repr(self): image = EmbeddedImage.from_path(self.filename) repr(image)
def test_set_image_noent(self): os.remove(self.filename) image = EmbeddedImage(None, "image/png") self.assertRaises(AudioFileError, self.song.set_image, image)
def test_not_an_image(self): path = os.path.join(DATA_DIR, 'test-2.wma') image = EmbeddedImage.from_path(path) self.assertFalse(image)