Ejemplo n.º 1
0
def main():
    from core import clap
    from core import common
    from core import replace as r

    try:
        p = clap.Parser()
    except Exception as e:
        print("%s: error: %s" % (os.path.basename(sys.argv[0]), e))
        sys.exit(1)

    p.set_description("Replace tabs inside of files with a user-defined "
                      "amount of spaces and vice versa.")
    p.set_epilog("Further information and usage examples can be found inside "
                 "the documentation file for this script.")

    # Required arguments
    p.add_avalue("-d", "--directory", "directory that contains the files " \
                 "to process", "directory", None, True)
    p.add_avalue("-f", "--file-extension", "file extension of the files to " \
                 "process", "file_ext", None, True)
    p.add_predef("-m", "--indentation-mode", "indentation method to use",
                 "mode", ["spaces", "tabs"], True)
    p.add_avalue("-s", "--spaces", "amount of spaces used to replace a tab " \
                 "or to get replaced by a tab (depending on the "
                 "indentation mode)", "spaces", None, True)

    # Optional arguments
    p.add_switch(None, "--confirm", "skip the confirmation prompt and " \
                 "instantly process the data (in case the '--overwrite' " \
                 "argument was given)", "confirm", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--overwrite", "overwrite the input file with the " \
                 "processed data (instead of creating an output file with " \
                 "the same name and the additional suffix '.tasp')",
                 "overwrite", True, False)
    p.add_switch("-r", "--recursive", "process the given directory " \
                 "recursively", "recursive", True, False)
    p.add_switch(None, "--verbose", "use verbose mode (print details while " \
                 "processing data)", "verbose", True, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print(r.get_version())
        sys.exit(0)

    args = p.parse_args()
    try:
        if args.overwrite:
            if not args.confirm:
                if not common.confirm_notice():
                    sys.exit(0)
        else:
            if args.confirm:
                p.error("The '--confirm' argument only makes sense in " \
                        "combination with the '--overwrite' argument.")

        r.replace(args.directory, args.file_ext, args.mode, args.spaces,
                  args.recursive, args.overwrite, args.verbose)
    except Exception as e:
        p.error(e)
Ejemplo n.º 2
0
def main():
    from core import clap
    from core import common
    from core import indent as i

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Consistently indent messed up files (especially " \
                      "config files) using a user-defined amount of spaces.")
    p.set_epilog("Further information and usage examples can be found inside "
                 "the documentation file for this script.")

    # Required arguments
    p.add_avalue("-d", "--directory", "directory that contains the files " \
                 "to process", "directory", None, True)
    p.add_avalue("-f", "--file-extension", "file extension of the files to " \
                 "process", "file_ext", None, True)
    p.add_avalue("-p", "--padding", "padding (in spaces) used to indent " \
                 "each item inside a line", "padding", None, True)
    p.add_avalue("-s", "--spaces", "amount of leading spaces for each " \
                 "indented line", "spaces", None, True)

    # Optional arguments
    p.add_switch(None, "--confirm", "skip the confirmation prompt and " \
                 "instantly process the data (in case the '--overwrite' " \
                 "argument was given)", "confirm", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch("-l", "--left-justify", "left-justify the line if it only " \
                 "consists of a single character", "left_justify", True,
                 False)
    p.add_switch(None, "--overwrite", "overwrite the input file with the " \
                 "processed data (instead of creating an output file with " \
                 "the same name and the additional suffix '.tasp')",
                 "overwrite", True, False)
    p.add_switch("-r", "--recursive", "process the given directory " \
                 "recursively", "recursive", True, False)
    p.add_switch(None, "--verbose", "use verbose mode (print details while " \
                 "processing data)", "verbose", True, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print i.get_version()
        sys.exit(0)

    args = p.parse_args()
    try:
        if args.overwrite:
            if not args.confirm:
                if not common.confirm_notice():
                    sys.exit(0)
        else:
            if args.confirm:
                p.error("The '--confirm' argument only makes sense in " \
                        "combination with the '--overwrite' argument.")

        i.indent(args.directory, args.file_ext, args.spaces, args.padding,
                 args.left_justify, args.recursive, args.overwrite,
                 args.verbose)
    except Exception as e:
        p.error(e)
Ejemplo n.º 3
0
def main():
    from core import clap
    from core import common
    from core import extren

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Rename (and adjust) differently spelled file " \
                      "extensions of the same file type file within a " \
                      "directory and (if requested) in all of its sub-" \
                      "directories.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Required arguments
    p.add_avalue("-d", "--directory", "directory that contains the files " \
                 "to process", "directory", None, True)
    p.add_avalue("-e", "--extension", "extension to rename (case-" \
                 "sensitive, multiple extensions separated via semicolon)",
                 "extension", None, True)
    p.add_predef("-m", "--conflict-mode", "conflict mode (in case of " \
                 "duplicate file names)", "conflict_mode", ["rename", "skip"],
                 True)
    p.add_avalue("-t", "--target-extension", "target extension (case-" \
                 "sensitive)", "extension_target", None, True)

    # Optional arguments
    p.add_switch("-c", "--case-sensitive", "do not ignore the case of the " \
                 "given extension list", "case", False, False)
    p.add_switch(None, "--confirm", "skip the confirmation prompt and " \
                 "instantly rename files", "confirm", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--ignore-symlinks", "ignore symbolic links",
                 "ignore_symlinks", True, False)
    p.add_switch("-r", "--recursive", "process the given directory " \
                 "recursively", "recursive", True, False)
    p.add_avalue(None, "--simulate", "simulate the rename process and " \
                 "write the details into a report file", "report_file", None,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print extren.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.confirm and not args.report_file == None:
        p.error("The confirm and the simulate argument cannot be given at " \
                "the same time.")

    try:
        if not args.confirm and args.report_file == None:
            if not common.confirm_notice():
                sys.exit(0)

        extren.rename_extensions(args.directory, args.conflict_mode,
                                 args.extension, args.extension_target,
                                 args.recursive, args.case, args.report_file,
                                 args.ignore_symlinks)
    except Exception as e:
        p.error(e)
Ejemplo n.º 4
0
def main():
    from core import clap
    from core import common
    from core import fileren

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Rename the base name of files within a directory " \
                      "and (if requested) in all of its sub-directories " \
                      "based on the name of the directory where the files " \
                      "are stored in and add a unique numeric ID.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Required arguments
    p.add_avalue("-d", "--directory", "directory that contains the files " \
                 "to process", "directory", None, True)
    p.add_predef("-m", "--rename-mode", "rename mode to use", "rename_mode",
                 ["fill-gaps", "keep-order", "rename-new"], True)

    # Optional arguments
    p.add_switch("-c", "--case-sensitive", "do not ignore the case of the " \
                 "given exclude or explicit pattern", "case", False, False)
    p.add_switch(None, "--confirm", "skip the confirmation prompt and " \
                 "instantly rename files", "confirm", True, False)
    p.add_avalue(None, "--custom-name", "custom file name (instead of the " \
                 "directory name where the files are stored in)",
                 "custom_name", None, False)
    p.add_avalue(None, "--exclude", "pattern to exclude certain files " \
                 "(case-insensitive, multiple patterns separated via " \
                 "semicolon)", "exclude_pattern", None, False)
    p.add_avalue(None, "--explicit", "explicit pattern to only process " \
                 "certain files (case-insensitive, multiple patterns " \
                 "separated via semicolon)", "explicit_pattern", None, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--ignore-file-ext", "ignore file extensions when " \
                 "numerating files", "ignore_file_ext", True, False)
    p.add_switch(None, "--ignore-symlinks", "ignore symbolic links",
                 "ignore_symlinks", True, False)
    p.add_predef("-o", "--order-by", "order files by last accessed, " \
                 "created or modified date", "order_by", ["accessed",
                 "created", "modified"], False)
    p.add_avalue("-p", "--padding", "set a user-defined numeric padding " \
                 "(if no user-defined padding value is given, it will be " \
                 "set automatically based on the amount of files per " \
                 "directory)", "padding", 0, False)
    p.add_switch("-r", "--recursive", "process the given directory " \
                 "recursively", "recursive", True, False)
    p.add_switch(None, "--regex", "use regex syntax for the exclude or " \
                 "explicit pattern instead of just asterisk wildcards and " \
                 "semicolon separators (for details see the section " \
                 "'Regular expression operations' inside the official " \
                 "Python documentation)", "regex_syntax", True, False)
    p.add_avalue("-s", "--separator", "use a user-defined character or " \
                 "string as a separator between the directory name and the " \
                 "unique numeric ID", "separator", " ", False)
    p.add_avalue(None, "--simulate", "simulate the rename process and " \
                 "write the details into a report file", "report_file", None,
                 False)
    p.add_avalue(None, "--step", "steps between each numeric ID", "step", 1,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print fileren.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.confirm and not args.report_file == None:
        p.error("The confirm and the simulate argument cannot be given at " \
                "the same time.")
    if args.exclude_pattern and args.explicit_pattern:
        p.error("The exclude and the explicit pattern argument cannot be " \
                "given at the same time.")

    try:
        if not args.confirm and args.report_file == None:
            if not common.confirm_notice():
                sys.exit(0)

        if args.exclude_pattern:
            pattern = args.exclude_pattern
            exclude = True
        elif args.explicit_pattern:
            exclude = False
            pattern = args.explicit_pattern
        else:
            exclude = None
            pattern = None

        fileren.rename_files(args.directory, args.rename_mode, args.separator,
                             args.recursive, args.padding, exclude, pattern,
                             args.case, args.regex_syntax, args.report_file,
                             args.ignore_symlinks, args.ignore_file_ext,
                             args.custom_name, args.step, args.order_by)
    except Exception as e:
        p.error(e)
Ejemplo n.º 5
0
def main():
    from core import clap
    from core import common
    from core import fileren

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Modify the base name of files by adding, removing " \
                      "or replacing a user-defined string.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Required arguments
    p.add_predef("-a", "--action", "action to perform", "action",
                 ["add", "remove", "replace"], True)
    p.add_avalue("-d", "--directory", "directory that contains the files " \
                 "to process", "directory", None, True)
    p.add_predef("-p", "--position", "position where to perform the action",
                 "position", ["any", "prefix", "suffix"], True)
    p.add_avalue("-s", "--string", "input string to perform the action " \
                 "with (case-sensitive)", "input_string", None, True)

    # Optional arguments
    p.add_switch("-c", "--case-sensitive", "do not ignore the case of the " \
                 "given exclude or explicit pattern", "case", False, False)
    p.add_switch(None, "--confirm", "skip the confirmation prompt and " \
                 "instantly rename files", "confirm", True, False)
    p.add_avalue(None, "--exclude", "pattern to exclude certain files " \
                 "(case-insensitive, multiple patterns separated via " \
                 "semicolon)", "exclude_pattern", None, False)
    p.add_avalue(None, "--explicit", "explicit pattern to only process " \
                 "certain files (case-insensitive, multiple patterns " \
                 "separated via semicolon)", "explicit_pattern", None, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--ignore-symlinks", "ignore symbolic links",
                 "ignore_symlinks", True, False)
    p.add_switch("-r", "--recursive", "process the given directory " \
                 "recursively", "recursive", True, False)
    p.add_switch(None, "--regex", "use regex syntax for the exclude or " \
                 "explicit pattern instead of just asterisk wildcards and " \
                 "semicolon separators (for details see the section " \
                 "'Regular expression operations' inside the official " \
                 "Python documentation)", "regex_syntax", True, False)
    p.add_avalue(None, "--replace-string", "string to replace the input" \
                 "string with (when using the action 'replace')",
                 "replace_string", None, False)
    p.add_avalue(None, "--simulate", "simulate the rename process and " \
                 "write the details into a report file", "report_file", None,
                 False)
    p.add_avalue(None, "--strip", "remove certain leading and trailing " \
                 "characters from the base name", "strip_chars", None, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print fileren.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.confirm and not args.report_file == None:
        p.error("The confirm and the simulate argument cannot be given at " \
                "the same time.")
    if args.exclude_pattern and args.explicit_pattern:
        p.error("The exclude and the explicit pattern argument cannot be " \
                "given at the same time.")

    try:
        if not args.confirm and args.report_file == None:
            if not common.confirm_notice():
                sys.exit(0)

        if args.exclude_pattern:
            pattern = args.exclude_pattern
            exclude = True
        elif args.explicit_pattern:
            exclude = False
            pattern = args.explicit_pattern
        else:
            exclude = None
            pattern = None

        fileren.modify_names(args.directory, args.action, args.position,
                             args.input_string, args.replace_string,
                             args.recursive, exclude, pattern, args.case,
                             args.regex_syntax, args.report_file,
                             args.ignore_symlinks, args.strip_chars)
    except Exception as e:
        p.error(e)
Ejemplo n.º 6
0
def main():
    from core import clap
    from core import common
    from core import fileren

    try:
        p = clap.Parser()
    except Exception as e:
        print("%s: error: %s" % (os.path.basename(sys.argv[0]), e))
        sys.exit(1)

    p.set_description("Convert the case of the base name of all files "
                      "inside a directory and (if requested) in all of its "
                      "sub-directories.")
    p.set_epilog("Further information and usage examples can be found "
                 "inside the documentation file for this script.")

    # Required arguments
    p.add_predef("-c", "--case", "target case of the base name", "case",
                 ["lower", "title", "upper", "config"], True)
    p.add_avalue("-d", "--directory", "directory that contains the files "
                 "to process", "directory", None, True)
    p.add_predef("-m", "--conflict-mode", "conflict mode (in case of "
                 "duplicate file names)", "conflict_mode", ["rename", "skip"],
                 True)

    # Optional arguments
    p.add_avalue(
        None, "--cfg-lower", "path to the config file for strings "
        "which should always be lowercase inside the file "
        "name", "cfg_lower", None, False)
    p.add_avalue(
        None, "--cfg-mixed", "path to the config file for strings "
        "which should always be mixed case inside the "
        "file name", "cfg_mixed", None, False)
    p.add_avalue(
        None, "--cfg-title", "path to the config file for strings "
        "which should always be title case inside the "
        "file name", "cfg_title", None, False)
    p.add_avalue(
        None, "--cfg-upper", "path to the config file for strings "
        "which should always be uppercase inside the file "
        "name", "cfg_upper", None, False)
    p.add_switch(None, "--confirm", "skip the confirmation prompt and "
                 "instantly rename files", "confirm", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--ignore-symlinks", "ignore symbolic links",
                 "ignore_symlinks", True, False)
    p.add_switch("-r", "--recursive", "process the given directory "
                 "recursively", "recursive", True, False)
    p.add_avalue(
        None, "--simulate", "simulate the rename process and "
        "write the details into a report file", "report_file", None, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print(fileren.get_version())
        sys.exit(0)

    args = p.parse_args()
    if args.confirm and args.report_file is not None:
        p.error("The confirm and the simulate argument cannot be given at "
                "the same time.")

    try:
        if not args.confirm and args.report_file is None:
            if not common.confirm_notice():
                sys.exit(0)

        fileren.convert_case(args.directory, args.case, args.conflict_mode,
                             args.recursive, args.cfg_lower, args.cfg_mixed,
                             args.cfg_title, args.cfg_upper, args.report_file,
                             args.ignore_symlinks)
    except Exception as e:
        p.error(e)