def set_art(self, path, copy=True): """Sets the album's cover art to the image at the given path. The image is copied (or moved) into place, replacing any existing art. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.remove(oldart) artdest = util.unique_path(artdest) if copy: util.copy(path, artdest) else: util.move(path, artdest) self.artpath = artdest
def set_art(self, path, copy=True): """Sets the album's cover art to the image at the given path. The image is copied (or moved) into place, replacing any existing art. Sends an 'art_set' event with `self` as the sole argument. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.remove(oldart) artdest = util.unique_path(artdest) if copy: util.copy(path, artdest) else: util.move(path, artdest) self.artpath = artdest plugins.send('art_set', album=self)
def move_file(self, dest, copy=False, link=False): """Moves or copies the item's file, updating the path value if the move succeeds. If a file exists at ``dest``, then it is slightly modified to be unique. """ if not util.samefile(self.path, dest): dest = util.unique_path(dest) if copy: util.copy(self.path, dest) plugins.send("item_copied", item=self, source=self.path, destination=dest) elif link: util.link(self.path, dest) plugins.send("item_linked", item=self, source=self.path, destination=dest) else: plugins.send("before_item_moved", item=self, source=self.path, destination=dest) util.move(self.path, dest) plugins.send("item_moved", item=self, source=self.path, destination=dest) # Either copying or moving succeeded, so update the stored path. self.path = dest
def fetch_art(config): """A coroutine that fetches and applies album art for albums where appropriate. """ lib = _reopen_lib(config.lib) task = None while True: task = yield task if task.should_skip(): continue if task.should_fetch_art(): artpath = beets.autotag.art.art_for_album(task.info, task.path) # Save the art if any was found. if artpath: try: album = lib.get_album(task.album_id) album.set_art(artpath) if config.delete and not util.samefile(artpath, album.artpath): # Delete the original file after it's imported. os.remove(artpath) finally: lib.save(False)
def fetch_art(config): """A coroutine that fetches and applies album art for albums where appropriate. """ lib = _reopen_lib(config.lib) task = None while True: task = yield task if task.should_skip(): continue if task.should_fetch_art(): artpath = beets.autotag.art.art_for_album(task.info, task.path) # Save the art if any was found. if artpath: try: album = lib.get_album(task.album_id) album.set_art(artpath) if config.delete and not util.samefile( artpath, album.artpath): # Delete the original file after it's imported. os.remove(artpath) finally: lib.save(False)
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. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.soft_remove(oldart) util.copy(path, artdest) self.artpath = artdest
def item_change_actions(self, item, path, dest): """ Returns the necessary actions for items that were previously in the external collection, but might require metadata updates. """ actions = [] if not path == dest: # The path of the link itself changed actions.append(self.MOVE) elif not util.samefile(path, item.path): # link target changed actions.append(self.MOVE) return actions
def move(self, dest, copy=False): """Moves or copies the item's file, updating the path value if the move succeeds. If a file exists at ``dest``, then it is slightly modified to be unique. """ if not util.samefile(self.path, dest): dest = util.unique_path(dest) if copy: util.copy(self.path, dest) else: util.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. self.path = dest
def matched_item_action(self, item): path = self.get_path(item) actions = [] if path and os.path.isfile(syspath(path)): dest = self.destination(item) if not util.samefile(path, dest): actions.extend([self.MOVE, self.WRITE]) elif (os.path.getmtime(syspath(dest)) < os.path.getmtime(syspath(item.path))): actions.append(self.WRITE) album = item.get_album() if (album and album.artpath and (os.path.getmtime(syspath(path)) < os.path.getmtime(syspath(album.artpath)))): actions.append(self.EMBED_ART) else: actions.append(self.ADD) return (item, actions)
def item_change_actions(self, item, path, dest): """ Returns the necessary actions for items that were previously in the external collection, but might require metadata updates. """ actions = [] if not util.samefile(path, dest): actions.append(self.MOVE) item_mtime_alt = os.path.getmtime(syspath(path)) if (item_mtime_alt < os.path.getmtime(syspath(item.path))): actions.append(self.WRITE) album = item.get_album() if album: if (album.artpath and os.path.isfile(syspath(album.artpath)) and (item_mtime_alt < os.path.getmtime(syspath(album.artpath)))): actions.append(self.SYNC_ART) return actions
def move(self, library, copy=False, in_album=False, basedir=None): """Move the item to its designated location within the library directory (provided by destination()). Subdirectories are created as needed. If the operation succeeds, the item's path field is updated to reflect the new location. If copy is True, moving the file is copied rather than moved. If in_album is True, then the track is treated as part of an album even if it does not yet have an album_id associated with it. (This allows items to be moved before they are added to the database, a performance optimization.) basedir overrides the library base directory for the destination. Passes on appropriate exceptions if directories cannot be created or moving/copying fails. Note that one should almost certainly call store() and library.save() after this method in order to keep on-disk data consistent. """ dest = library.destination(self, in_album=in_album, basedir=basedir) # Create necessary ancestry for the move. util.mkdirall(dest) if not samefile(self.path, dest): if copy: util.copy(self.path, dest) else: util.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. old_path = self.path self.path = dest # Prune vacated directory. if not copy: util.prune_dirs(os.path.dirname(old_path), library.directory)
def matched_item_action(self, item): path = self.get_path(item) if path and os.path.isfile(syspath(path)): dest = self.destination(item) _, path_ext = os.path.splitext(path) _, dest_ext = os.path.splitext(dest) if not path_ext == dest_ext: # formats config option changed return (item, [self.REMOVE, self.ADD]) actions = [] if not util.samefile(path, dest): actions.append(self.MOVE) item_mtime_alt = os.path.getmtime(syspath(path)) if (item_mtime_alt < os.path.getmtime(syspath(item.path))): actions.append(self.WRITE) album = item.get_album() if album: if (album.artpath and os.path.isfile(syspath(album.artpath)) and (item_mtime_alt < os.path.getmtime( syspath(album.artpath)))): actions.append(self.SYNC_ART) return (item, actions) else: return (item, [self.ADD])