Exemplo n.º 1
0
def report_errors(errors):
    for error in errors:
        print('{}: {}; message: {}'.format(
            color('Error', 'white', 'on_red'),
            color(error.__class__.__name__, 'red'),
            error.msg
        ))
Exemplo n.º 2
0
def report_errors(errors: typing.Sequence):
    for error in errors:
        print("{}: {}; message: {}".format(
            color("Error", "white", "on_red"),
            color(error.__class__.__name__, "red"),
            error.msg,
        ))
Exemplo n.º 3
0
def report_errors(errors: typing.Sequence):
    for error in errors:
        print('{}: {}; message: {}'.format(
            color('Error', 'white', 'on_red'),
            color(error.__class__.__name__, 'red'),
            error.msg
        ))
Exemplo n.º 4
0
def rename_filename(source: str) -> Meta:
    args = get_args()

    meta = Meta(source)
    meta_values = meta.interface.export_to_dict()
    target_filename = apply_format_string(meta_values)

    if args.rename_skip:
        skips = args.rename_skip.split(",")
        for skip in skips:
            if not meta_values[skip]:
                print(
                    color("Field “{}” is empty! Skipping".format(skip), "red"))
                return meta

    if args.rename_target:
        target_base = os.path.abspath(args.rename_target)
    else:
        target_base = os.getcwd()

    target = os.path.join(target_base, target_filename + "." + meta.extension)

    if os.path.exists(target):
        target_format = target.replace(".mscx", "{}.mscx")
        i = ""
        while os.path.exists(target_format.format(i)):
            target = target_format.format(i)
            if get_checksum(source) == get_checksum(target):
                print(
                    color(
                        "The file “{}” with the same checksum (sha1) "
                        "already exists in the target path “{}”!".format(
                            source, target),
                        "red",
                    ))
                return meta
            if i == "":
                i = 1
            i += 1

        target = target_format.format(i)

    show(source, target)

    if not args.general_dry_run:

        create_dir(target)
        # Invalid cross-device link:
        # os.rename(source, target)
        shutil.move(source, target)

    return meta
Exemplo n.º 5
0
    def show(self, pre, post):
        args = get_args()

        fields = list(self.interface.fields)

        if args.general_verbose < 1:
            fields.remove('readonly_abspath')
            fields.remove('readonly_dirname')
            fields.remove('readonly_extension')
            fields.remove('readonly_filename')
            fields.remove('readonly_relpath')

        if args.general_verbose < 2:
            fields.remove('readonly_relpath_backup')

        for field in fields:
            if (args.general_verbose == 0 and (pre[field] or post[field])) or \
               args.general_verbose > 0:

                if re.match(r'^combined_', field):
                    field_color = 'green'
                elif re.match(r'^metatag_', field):
                    field_color = 'blue'
                elif re.match(r'^readonly_', field):
                    field_color = 'red'
                elif re.match(r'^vbox_', field):
                    field_color = 'cyan'
                else:
                    field_color = 'white'

                line = []
                if pre[field]:
                    line.append('“{}”'.format(pre[field]))

                if pre[field] != post[field]:
                    line.append('->')
                    line.append(color('“{}”'.format(post[field]), 'yellow'))

                print('{}: {}'.format(color(field, field_color),
                                      ' '.join(line)))
Exemplo n.º 6
0
    def show(self, pre, post):
        args = get_args()

        fields = list(self.interface.fields)

        if args.general_verbose < 1:
            fields.remove("readonly_abspath")
            fields.remove("readonly_dirname")
            fields.remove("readonly_extension")
            fields.remove("readonly_filename")
            fields.remove("readonly_relpath")

        if args.general_verbose < 2:
            fields.remove("readonly_relpath_backup")

        for field in fields:
            if (args.general_verbose == 0 and
                (pre[field] or post[field])) or args.general_verbose > 0:

                if re.match(r"^combined_", field):
                    field_color = "green"
                elif re.match(r"^metatag_", field):
                    field_color = "blue"
                elif re.match(r"^readonly_", field):
                    field_color = "red"
                elif re.match(r"^vbox_", field):
                    field_color = "cyan"
                else:
                    field_color = "white"

                line = []
                if pre[field]:
                    line.append("“{}”".format(pre[field]))

                if pre[field] != post[field]:
                    line.append("->")
                    line.append(color("“{}”".format(post[field]), "yellow"))

                print("{}: {}".format(color(field, field_color),
                                      " ".join(line)))
Exemplo n.º 7
0
    def show(self, pre, post):
        args = get_settings('args')

        fields = list(self.interface.fields)

        if args.general_verbose < 1:
            fields.remove('readonly_abspath')
            fields.remove('readonly_dirname')
            fields.remove('readonly_extension')
            fields.remove('readonly_filename')
            fields.remove('readonly_relpath')

        if args.general_verbose < 2:
            fields.remove('readonly_relpath_backup')

        for field in fields:
            if (args.general_verbose == 0 and (pre[field] or post[field])) or \
               args.general_verbose > 0:

                if re.match(r'^combined_',  field):
                    field_color = 'green'
                elif re.match(r'^metatag_',  field):
                    field_color = 'blue'
                elif re.match(r'^readonly_',  field):
                    field_color = 'red'
                elif re.match(r'^vbox_',  field):
                    field_color = 'cyan'
                else:
                    field_color = 'white'

                line = []
                if pre[field]:
                    line.append('“{}”'.format(pre[field]))

                if pre[field] != post[field]:
                    line.append('->')
                    line.append(color('“{}”'.format(post[field]), 'yellow'))

                print('{}: {}'.format(color(field, field_color),
                                      ' '.join(line)))
Exemplo n.º 8
0
def show(old: str, new: str):
    print("{} -> {}".format(color(old, "yellow"), color(new, "green")))
Exemplo n.º 9
0
def execute(args: typing.Sequence = None):
    args = cli.parser.parse_args(args)
    config = parse_config_ini(args.general_config_file)
    if config:
        args = merge_config_into_args(config, args)
    set_args(args)

    if args.subcommand == "help":
        show_all_help(args)
        sys.exit()

    files = list_scores(path=args.path, glob=args.general_glob)

    for file in files:

        print("\n" + color(file, "red"))

        if args.general_backup:
            from mscxyz.score_file_classes import MscoreFile

            score = MscoreFile(file)
            score.backup()

        if args.subcommand == "clean":
            score = MscoreXmlTree(file)
            print(score.filename)
            score.clean()
            if args.clean_style:
                score.merge_style(styles=args.clean_style.name)
            score.save(mscore=args.general_mscore)

        elif args.subcommand == "lyrics":
            score = MscoreLyricsInterface(file)
            if args.lyrics_remap:
                score.remap(remap_string=args.lyrics_remap,
                            mscore=args.general_mscore)
            elif args.lyrics_fix:
                score.fix_lyrics(mscore=args.general_mscore)
            else:
                score.extract_lyrics(number=args.lyrics_extract,
                                     mscore=args.general_mscore)

        elif args.subcommand == "meta":
            score = Meta(file)
            if no_error(lxml.etree.XMLSyntaxError, score.errors):
                pre = score.interface.export_to_dict()
                if args.meta_clean:
                    score.clean(fields=args.meta_clean)
                if args.meta_json:
                    score.export_json()
                if args.meta_dist:
                    for a in args.meta_dist:
                        score.distribute_field(source_fields=a[0],
                                               format_string=a[1])
                if args.meta_set:
                    for a in args.meta_set:
                        score.set_field(destination_field=a[0],
                                        format_string=a[1])
                if args.meta_delete:
                    score.delete_duplicates()
                if args.meta_sync:
                    score.sync_fields()
                if args.meta_log:
                    score.write_to_log_file(args.meta_log[0], args.meta_log[1])
                post = score.interface.export_to_dict()
                score.show(pre, post)
            if not args.general_dry_run and not score.errors and pre != post:
                score.save(mscore=args.general_mscore)

        elif args.subcommand == "rename":
            score = rename_filename(file)

        elif args.subcommand == "export":
            from mscxyz.score_file_classes import MscoreFile

            score = MscoreFile(file)
            score.export(extension=args.export_extension)

        report_errors(score.errors)
Exemplo n.º 10
0
def execute(args=None):
    args = cli.parser.parse_args(args)
    set_settings('args', args)

    if args.subcommand == 'help':
        show_all_help(args)
        sys.exit()

    files = list_scores(path=args.path, glob=args.general_glob)

    for file in files:

        print('\n' + color(file, 'red'))

        if args.general_backup:
            from mscxyz.score_file_classes import ScoreFile
            score = ScoreFile(file)
            score.backup()

        if args.subcommand == 'clean':
            score = XMLTree(file)
            print(score.filename)
            score.clean()
            if args.clean_style:
                score.merge_style(styles=args.clean_style.name)
            score.save(mscore=args.general_mscore)

        elif args.subcommand == 'lyrics':
            score = Lyrics(file)
            if args.lyrics_remap:
                score.remap(remap_string=args.lyrics_remap,
                            mscore=args.general_mscore)
            elif args.lyrics_fix:
                score.fix_lyrics(mscore=args.general_mscore)
            else:
                score.extract_lyrics(number=args.lyrics_extract,
                                     mscore=args.general_mscore)

        elif args.subcommand == 'meta':
            score = Meta(file)
            if no_error(lxml.etree.XMLSyntaxError, score.errors):
                pre = score.interface.export_to_dict()
                if args.meta_clean:
                    score.clean(fields=args.meta_clean)
                if args.meta_json:
                    score.export_json()
                if args.meta_dist:
                    for a in args.meta_dist:
                        score.distribute_field(source_fields=a[0],
                                               format_string=a[1])
                if args.meta_set:
                    for a in args.meta_set:
                        score.set_field(destination_field=a[0],
                                        format_string=a[1])
                if args.meta_delete:
                    score.delete_duplicates()
                if args.meta_sync:
                    score.sync_fields()
                if args.meta_log:
                    score.write_to_log_file(args.meta_log[0], args.meta_log[1])
                post = score.interface.export_to_dict()
                score.show(pre, post)
            if not args.general_dry_run and not score.errors and pre != post:
                score.save(mscore=args.general_mscore)

        elif args.subcommand == 'rename':
            score = rename_filename(file)

        elif args.subcommand == 'export':
            from mscxyz.score_file_classes import ScoreFile
            score = ScoreFile(file)
            score.export(extension=args.export_extension)

        report_errors(score.errors)