def copy_album_art(self, album, dest_dir, path_formats, pretend=False): """Copies the associated cover art of the album. Album must have at least one track. """ if not album or not album.artpath: return album_item = album.items().get() # Album shouldn't be empty. if not album_item: return # Get the destination of the first item (track) of the album, we use # this function to format the path accordingly to path_formats. dest = album_item.destination(basedir=dest_dir, path_formats=path_formats) # Remove item from the path. dest = os.path.join(*util.components(dest)[:-1]) dest = album.art_destination(album.artpath, item_dir=dest) if album.artpath == dest: return if not pretend: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info("Skipping {0} (target file exists)", util.displayable_path(album.artpath)) return if pretend: self._log.info("cp {0} {1}", util.displayable_path(album.artpath), util.displayable_path(dest)) else: self._log.info("Copying cover art to {0}", util.displayable_path(dest)) util.copy(album.artpath, dest)
def copy_album_art(self, album, dest_dir, path_formats, pretend=False): """Copies or converts the associated cover art of the album. Album must have at least one track. """ if not album or not album.artpath: return album_item = album.items().get() # Album shouldn't be empty. if not album_item: return # Get the destination of the first item (track) of the album, we use # this function to format the path accordingly to path_formats. dest = album_item.destination(basedir=dest_dir, path_formats=path_formats) # Remove item from the path. dest = os.path.join(*util.components(dest)[:-1]) dest = album.art_destination(album.artpath, item_dir=dest) if album.artpath == dest: return if not pretend: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info(u"Skipping {0} (target file exists)", util.displayable_path(album.artpath)) return # Decide whether we need to resize the cover-art image. resize = False maxwidth = None if self.config["album_art_maxwidth"]: maxwidth = self.config["album_art_maxwidth"].get(int) size = ArtResizer.shared.get_size(album.artpath) self._log.debug("image size: {}", size) if size: resize = size[0] > maxwidth else: self._log.warning(u"Could not get size of image (please see " u"documentation for dependencies).") # Either copy or resize (while copying) the image. if resize: self._log.info( u"Resizing cover art from {0} to {1}", util.displayable_path(album.artpath), util.displayable_path(dest) ) if not pretend: ArtResizer.shared.resize(maxwidth, album.artpath, dest) else: if pretend: self._log.info(u"cp {0} {1}", util.displayable_path(album.artpath), util.displayable_path(dest)) else: self._log.info( u"Copying cover art to {0}", util.displayable_path(album.artpath), util.displayable_path(dest) ) util.copy(album.artpath, dest)
def libtree(lib): """Generates a filesystem-like directory tree for the files contained in `lib`. Filesystem nodes are (files, dirs) named tuples in which both components are dictionaries. The first maps filenames to Item ids. The second maps directory names to child node tuples. """ root = Node({}, {}) for item in lib.items(): dest = item.destination(fragment=True) parts = util.components(dest) _insert(root, parts, item.id) return root
def libtree(lib): """Generates a filesystem-like directory tree for the files contained in `lib`. Filesystem nodes are (files, dirs) named tuples in which both components are dictionaries. The first maps filenames to Item ids. The second maps directory names to child node tuples. """ root = Node({}, {}) for item in lib.items(): dest = lib.destination(item, fragment=True) parts = util.components(dest) _insert(root, parts, item.id) return root
def copy_album_art(album, dest_dir, path_formats, pretend=False): """Copies the associated cover art of the album. Album must have at least one track. """ if not album or not album.artpath: return album_item = album.items().get() # Album shouldn't be empty. if not album_item: return # Get the destination of the first item (track) of the album, we use this # function to format the path accordingly to path_formats. dest = album_item.destination(basedir=dest_dir, path_formats=path_formats) # Remove item from the path. dest = os.path.join(*util.components(dest)[:-1]) dest = album.art_destination(album.artpath, item_dir=dest) if album.artpath == dest: return if not pretend: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(album.artpath) )) return if pretend: log.info(u'cp {0} {1}'.format( util.displayable_path(album.artpath), util.displayable_path(dest), )) else: log.info(u'Copying cover art to {0}'.format( util.displayable_path(dest))) util.copy(album.artpath, dest)
def test_components_works_on_relative(self): p = 'a/b/c' a = ['a', 'b', 'c'] self.assertEqual(util.components(p), a)
def test_components_works_on_dir(self): p = '/a/b/c/' a = ['/', 'a', 'b', 'c'] self.assertEqual(util.components(p), a)
def copy_album_art(self, album, dest_dir, path_formats, pretend=False, link=False, hardlink=False): """Copies or converts the associated cover art of the album. Album must have at least one track. """ if not album or not album.artpath: return album_item = album.items().get() # Album shouldn't be empty. if not album_item: return # Get the destination of the first item (track) of the album, we use # this function to format the path accordingly to path_formats. dest = album_item.destination(basedir=dest_dir, path_formats=path_formats) # Remove item from the path. dest = os.path.join(*util.components(dest)[:-1]) dest = album.art_destination(album.artpath, item_dir=dest) if album.artpath == dest: return if not pretend: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info(u'Skipping {0} (target file exists)', util.displayable_path(album.artpath)) return # Decide whether we need to resize the cover-art image. resize = False maxwidth = None if self.config['album_art_maxwidth']: maxwidth = self.config['album_art_maxwidth'].get(int) size = ArtResizer.shared.get_size(album.artpath) self._log.debug('image size: {}', size) if size: resize = size[0] > maxwidth else: self._log.warning(u'Could not get size of image (please see ' u'documentation for dependencies).') # Either copy or resize (while copying) the image. if resize: self._log.info(u'Resizing cover art from {0} to {1}', util.displayable_path(album.artpath), util.displayable_path(dest)) if not pretend: ArtResizer.shared.resize(maxwidth, album.artpath, dest) else: if pretend: msg = 'ln' if hardlink else ('ln -s' if link else 'cp') self._log.info(u'{2} {0} {1}', util.displayable_path(album.artpath), util.displayable_path(dest), msg) else: msg = 'Hardlinking' if hardlink \ else ('Linking' if link else 'Copying') self._log.info(u'{2} cover art from {0} to {1}', util.displayable_path(album.artpath), util.displayable_path(dest), msg) if hardlink: util.hardlink(album.artpath, dest) elif link: util.link(album.artpath, dest) else: util.copy(album.artpath, dest)