Ejemplo n.º 1
0
def main(argv):
    parser = argparse.ArgumentParser(description=HELP)
    parser.add_argument('attribute')
    parser.add_argument('value')
    parser.add_argument('files', nargs='+')
    parser.add_argument('--search',
                        help='only replace, if the old value matches')
    parser.add_argument('--dry-run', action='store_true')
    args = parser.parse_args(argv)

    for path in args.files:
        with open(path) as f:
            text = f.read()
            try:
                value = get_attribute(text, args.attribute)
            except:
                value = None
            if args.search and value != args.search:
                continue
            if value == args.value:
                continue
            lines = set_attribute(text.splitlines(), args.attribute,
                                  args.value)

        if not args.dry_run:
            with open(path, 'w') as f:
                f.writelines(lines)

        print(path)
def get_linked_files(txt_path):
    songdir = os.path.dirname(txt_path)

    try:
        with open(txt_path) as f:
            text = f.read()
    except UnicodeDecodeError:
        return

    if '#TITLE' in text:
        yield txt_path

    for attribute in ('VIDEO', 'MP3', 'COVER', 'BACKGROUND'):
        with suppress(KeyError):
            yield os.path.join(songdir, get_attribute(text, attribute))
Ejemplo n.º 3
0
def main(argv):
    parser = argparse.ArgumentParser(description=HELP)
    parser.add_argument('attribute')
    parser.add_argument('files', nargs='+')
    parser.add_argument('--no-filename', action='store_true', help='just print the value, not the file path.')
    args = parser.parse_args(argv)

    for path in args.files:
        with open(path) as f:
            try:
                value = get_attribute(f.read(), args.attribute)
            except KeyError:
                continue

            if args.no_filename:
                print(f'{value}')
            else:
                print(f'{value}\t{path}')
def _get_attr_path(text, path, attr):
    attr_path = get_attribute(text, attr)
    songdir = os.path.dirname(path)
    attr_path = os.path.join(songdir, attr_path)
    return attr_path
 def has_attr(text, path):
     try:
         if not get_attribute(text, attr):
             yield f'attribute {attr} empty'
     except KeyError:
         yield f'attribute {attr} missing'
Ejemplo n.º 6
0
def has_working_cover(songdir, text):
    with suppress(KeyError):
        if os.path.exists(songdir + '/' + get_attribute(text, 'COVER')):
            return True

    return False
def fix_file_links(path, keep_missing_files, dry_run=False, verbose=False):
    print(path)

    song_dir = os.path.dirname(path)
    song_files = os.listdir(song_dir)
    song_files = {force_ascii(s): s for s in song_files}

    renamed = {}

    with open(path) as f:
        text = f.read()

    try:
        artisttitle = get_artisttitle(text)
        artisttitle = artisttitle.replace('/', ' ')
    except KeyError:
        return

    new_path = song_dir + '/' + artisttitle + '.txt'
    if new_path != path:
        print(f'rename {new_path}')

        if not dry_run:
            os.rename(path, new_path)

    lines = text.splitlines(True)

    for attr in ('MP3', 'VIDEO', 'COVER', 'BACKGROUND'):
        try:
            attr_path = get_attribute(text, attr)
            attr_path_ascii = force_ascii(attr_path)
        except KeyError:
            continue

        if verbose:
            print(f'processing {attr}: {attr_path_ascii}')
            print(f'candiates: ' + ' '.join(f[0] for f in song_files.items()))

        candidates = [
            original for ascii, original in song_files.items()
            if ascii.lower() == attr_path_ascii.lower()
        ]

        if not candidates:
            if keep_missing_files:
                print(f"keep {attr}, no file found")
            else:
                print(f"remove {attr}, no file found")
                lines = set_attribute(lines, attr, None)
            continue

        if attr_path == attr_path_ascii:
            print(f"ignore {attr}, ascii")
            continue

        old_attr_name = candidates[0]
        attr_ext = os.path.splitext(old_attr_name)[1]
        new_attr_name = artisttitle + ' ' + attr + attr_ext

        if old_attr_name == new_attr_name:
            print(f"ignore {attr}, no change")
            continue

        if old_attr_name not in renamed:
            print(f"rewrite {attr}: {old_attr_name} => {new_attr_name}")
            if not dry_run:
                os.rename(song_dir + '/' + old_attr_name,
                          song_dir + '/' + new_attr_name)
            renamed[old_attr_name] = new_attr_name
        else:
            new_attr_name = renamed[old_attr_name]
            print(
                f"rewrite {attr} (double-ref): {old_attr_name} => {new_attr_name}"
            )

        lines = set_attribute(lines, attr, new_attr_name)

    if not dry_run:
        with open(new_path, 'w') as f:
            f.writelines(lines)