def move_album(album, destination): """Move an Album to destination If the destination exists and is a file, FileExistsError will be raised. If the destination exists and is a folder, album will be placed inside of the folder. If the album is moving into a subpath of the existing path, the tracks will be moved (ie collection/artist -> collection/artist/album). """ if os.path.isdir(destination): album_folder = os.path.basename(album.path) destination_path = os.path.join(destination, album_folder) elif os.path.exists(destination): raise FileExistsError(("File {} cannot be moved to destination:" + "{} (Already Exists)").format(album.path, destination)) else: destination_path = destination if album.path in destination_path: if not os.path.isdir(destination_path): os.mkdir(destination_path) for track in [x.path for x in album]: shutil.move(track, destination_path) else: shutil.move(album.path, destination) controller.set_album_path(album, destination_path)
def rename_album(album, pattern=None): """Correct the folder of an album to reflect tags and a given pattern A pattern may be passed as an argument, but rename_album will use the configuration files to select one. The paths of any Tracks contained by the album will make the necessary changes to remain valid. """ if pattern is None: pattern = ALBUM_PATTERN supported_fields = album.supported_fields() fields = {field: getattr(album, field) for field in supported_fields} name = pattern.format(**fields) album_parent = parent(album.path) destination = os.path.join(album_parent, name) if not os.path.exists(destination): shutil.move(album.path, destination) else: raise FileExistsError("File exists: {}".format(destination)) controller.set_album_path(album, destination)
def reorganize_and_rename_collection(collection_root=COLLECTION_ROOT, organization_pattern=ORGANIZATION_PATTERN, album_pattern=ALBUM_PATTERN, track_pattern=TRACK_PATTERN, artist_pattern=ARTIST_PATTERN, include_only=None): if include_only: if isinstance(include_only, Album): collection = (include_only,) else: collection = include_only else: collection = controller.build_albums(collection_root, recursive=True) for album in collection: orig_path = album.path tempdir = tempfile.mkdtemp() dest_dir = os.path.join(tempdir, os.path.basename(album.path)) shutil.copytree(album.path, dest_dir) controller.set_album_path(album, dest_dir) dest_path = tempdir for index, catagory in enumerate(organization_pattern.split('/')): print(catagory) if catagory == 'ARTIST': dest_path = _name_to_pattern( album, dest_path, artist_pattern) elif catagory == 'ALBUM': dest_path = _name_to_pattern( album, dest_path, album_pattern) elif catagory == 'TRACK': rename_tracks(album, track_pattern) else: raise Exception( "{} is not a valid organization pattern token".format( catagory)) if index == 0: root_path = dest_path if not os.path.isdir(collection_root): os.mkdir(collection_root) shutil.move(root_path, collection_root) shutil.rmtree(tempdir) shutil.rmtree(orig_path)