Пример #1
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
Пример #2
0
def prepare_fields(fields: dict) -> dict:
    args = get_args()
    out = {}
    for field, value in fields.items():
        if value:
            if args.rename_alphanum:
                value = alphanum(value)
                value = value.strip()
            if args.rename_ascii:
                value = asciify(value)
            if args.rename_no_whitespace:
                value = nowhitespace(value)
            value = value.strip()
            value = value.replace("/", "-")
        out[field] = value
    return out
Пример #3
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)))
Пример #4
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)))
Пример #5
0
def apply_format_string(fields: dict) -> str:
    args = get_args()
    fields = prepare_fields(fields)
    name = tmep.parse(args.rename_format, fields)
    return name
Пример #6
0
"""Test submodule “utils.py”."""

import unittest
from unittest import mock

from mscxyz.utils import get_args, get_mscore_bin, mscore

args = get_args()
args.general_executable = None


class TestFunctionGetMscoreBin(unittest.TestCase):
    @mock.patch("mscxyz.utils.get_args")
    @mock.patch("platform.system")
    @mock.patch("os.path.exists")
    @mock.patch("subprocess.check_output")
    def test_output(self, check_output, exists, system, get_args):
        get_args.return_value = args
        system.return_value = "Linux"
        exists.return_value = True
        path = bytes("/usr/local/bin/mscore\n".encode("utf-8"))
        check_output.return_value = path
        output = get_mscore_bin()
        self.assertEqual(output, "/usr/local/bin/mscore")


class TestFunctionMscore(unittest.TestCase):
    @mock.patch("mscxyz.utils.get_mscore_bin")
    @mock.patch("subprocess.Popen")
    def test_function(self, popen, get_mscore_bin):
        get_mscore_bin.return_value = "/bin/mscore"