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 rename_track(track, pattern): supported_fields = track.supported_fields() fields = {field: getattr(track, field) for field in supported_fields} name = pattern.format(**fields) + extension(track.path) destination = os.path.join(parent(track.path), name) shutil.move(track.path, destination) track.path = destination
def test_parent_arg(): assert library.parent(path_folder_1, 2) == '/path'
import os import shutil import tempfile import ConfigParser from r3tagger import controller, FileExistsError from r3tagger.model.album import Album from r3tagger.model.track import Track from r3tagger.library import parent, extension # Config loading parent_dir = parent(os.path.dirname(__file__)) config_file = os.path.join(parent_dir, 'r3tagger.cfg') config = ConfigParser.RawConfigParser() config.read(config_file) TRACK_PATTERN = config.get('Main', 'track-pattern') ALBUM_PATTERN = config.get('Main', 'album-pattern') COLLECTION_ROOT = config.get('Main', 'collection-root') ORGANIZATION_PATTERN = config.get('Main', 'organization-pattern') # TODO: Move to config file ARTIST_PATTERN = '{artist}' def rename_tracks(target, pattern=TRACK_PATTERN): """Correct the file name of a Track to reflect tags and a given pattern Either Albums or Tracks are acceptable arguments to pass. A pattern may be passed as an argument, but rename_tracks will use the configuration files to select one.
def test_parent(): result = '/path/to' assert library.parent(path_folder_1) == result assert library.parent(path_folder_2) == result