def test_fix(self):
        validator = ReleaseValidator(get_lastfmcache())

        release = create_test_release(release_title="mezzanine", date="")
        fixed = validator.fix(release)
        assert fixed != release

        mockito.unstub()
    def test_lastfm_fix_track_title_lowercase(self):
        validator = ReleaseValidator(get_lastfmcache())

        release = create_test_release()
        next(iter(release.tracks.values())).track_title = "angel"
        fixed = validator.fix(release)
        assert fixed != release

        mockito.unstub()
Esempio n. 3
0
def fix_releases(validator: ReleaseValidator, release_dirs: Iterator[str],
                 args: argparse.Namespace, dest_folder: str,
                 invalid_folder: str, duplicate_folder: str) -> None:
    """Fix releases found in the scan directory"""

    unique_releases = set()

    for curr_dir in release_dirs:
        if not can_lock_path(curr_dir):
            logging.getLogger(__name__).error(
                "Could not lock directory {0}".format(curr_dir))
            continue

        audio, non_audio, unreadable = load_directory(curr_dir)

        release = Release(audio, guess_category_from_path(curr_dir),
                          guess_source_from_path(curr_dir))

        fixed = validator.fix(release, os.path.split(curr_dir)[1])

        if not args.dry_run:
            for x in fixed.tracks:
                if fixed.tracks[x] != release.tracks[x] or fixed.tracks[
                        x].always_write:
                    cleartag.write_tags(os.path.join(curr_dir, x),
                                        fixed.tracks[x])

        # rename files
        rename_files(fixed, curr_dir, args.dry_run)

        new_tracks = OrderedDict()
        for x in fixed.tracks:
            correct_filename = fixed.tracks[x].get_filename(fixed.is_va())
            if correct_filename:
                new_tracks[correct_filename] = fixed.tracks[x]
            else:
                new_tracks[x] = fixed.tracks[x]
        fixed.tracks = new_tracks

        # calculate violations before and after fixing
        codec_short = not args.full_codec_names
        old_violations = validator.validate(release)
        validate_folder_name(release, old_violations,
                             os.path.split(curr_dir)[1], False,
                             args.group_by_category, codec_short)
        add_unreadable_files(old_violations, unreadable)

        violations = validator.validate(fixed)
        validate_folder_name(fixed, violations,
                             os.path.split(curr_dir)[1], True,
                             args.group_by_category, codec_short)
        add_unreadable_files(violations, unreadable)

        if len(violations) == 0:
            moved_dir = move_rename_folder(fixed, unique_releases, curr_dir,
                                           dest_folder, duplicate_folder, args)
        else:
            moved_dir = move_invalid_folder(curr_dir, invalid_folder,
                                            violations, args.move_invalid)

        enforce_max_path(moved_dir)

        print("{0} violations: {1}".format(
            format_violations_str(old_violations, violations), moved_dir))

        if args.show_violations:
            if old_violations:
                print("Before")
                print_list(old_violations)

            if violations:
                print("After:")
                print_list(violations)