def foobar(input_file, file_list, option, buffer_size, count): """ Sample method showing some usage examples. """ # First of all, check if the paramters have the correct type pv.param_type_list([[input_file, "input file", str], [file_list, "file list", list], [option, "option", str], [buffer_size, "buffer size", int], [count, "count", int]]) # Ensure that 'input_file' exists pv.path(input_file, "input file", True, True) # Ensure that 'input_file' is not identical with any from 'file_list' pv.compfile(input_file, "input file", file_list) # Ensure that the file name of 'input_file' does not contain any wildcards # and also that it does not contain brackets or exlamation marks pv.string(input_file, "input file", False, ['(', ')', '!']) # Ensure 'option' is one of the options from the list pv.compstr(option, "option", ["print", "read", "write"]) # Ensure that 'buffer_size' is a postive integer between 1 and 4096 pv.intrange(buffer_size, "buffer size", 1, 4096, False) # Finally, ensure that 'count' is either zero or a positive integer pv.intvalue(count, "count", True, True, False) print("Parameter validation successful.")
def replace(directory, file_ext, mode, spaces=8, recursive=False, overwrite=False, verbose=True): """ Method to perform the replacement process. """ pv.path(directory, "input", False, True) pv.string(file_ext, "file extension", False, None) mode = mode.lower() pv.compstr(mode, "mode", ["spaces", "tabs"]) pv.intvalue(spaces, "spaces", True, False, False) directory = os.path.abspath(directory) spaces = int(spaces) num = 1 if verbose: print "\nGathering files to process. Please wait.\n" list_files = common.get_files(directory, file_ext, recursive) just = len(str(len(list_files))) for file_input in list_files: if verbose: print "Processing file %s of %s: '%s'" % \ (str(num).rjust(just, " "), str(len(list_files)), file_input) num += 1 if overwrite: __replace_file(file_input, mode, spaces) else: __replace_copy(file_input, mode, spaces) if verbose: print "\nFinished.\n"
def modify_names(directory, action, position, input_string, replace_string=None, recursive=False, exclude=None, pattern=None, ignore_case=True, regex_syntax=False, report_file=None, ignore_symlinks=False, strip_chars=None): """ Modify the base name of files by adding, removing or replacing a user-defined string. """ pv.path(directory, "given", False, True) pv.compstr(action, "action", ["add", "remove", "replace"]) pv.compstr(position, "position", ["any", "prefix", "suffix"]) pv.string(input_string, "input string", False, common.get_invalid_chars()) action = action.lower() position = position.lower() directory = os.path.abspath(directory) if not directory.endswith(os.path.sep): directory += os.path.sep if report_file == None: simulate = False else: pv.path(report_file, "report", True, False) report_file = os.path.abspath(report_file) simulate = True if not replace_string == None: if not action == "replace": raise Exception("The replace string argument can only be used " \ "together with the action 'replace'.") else: pv.string(replace_string, "string False", False, common.get_invalid_chars()) if action == "add" and position == "any": raise Exception("The position 'any' cannot be used together with " \ "the action 'add'.") if len(input_string) == 0: raise Exception("The input string must not be empty.") else: pv.string(input_string, "input string", False, common.get_invalid_chars()) if not strip_chars == None: pv.string(strip_chars, "strip chars string", False, common.get_invalid_chars()) time_start = dt.now() list_content = [] list_excluded = [] list_renamed = [] list_skipped = [] regex = None if not pattern == None: regex = common.compile_regex(pattern, ignore_case, regex_syntax) list_content, list_excluded = \ common.get_files(directory, recursive, ignore_case, regex, exclude, ignore_symlinks) for item in list_content: list_files = item[1] __modify_names(list_files, list_renamed, list_skipped, action, position, input_string, replace_string, strip_chars) if simulate: explicit = None if exclude == None: exclude = False explicit = False elif exclude: explicit = False else: explicit = True list_header = [] list_header.append("Nomen File Name Modifier simulation report") list_header.append(["Report file name:", report_file]) list_header.append(["Directory:", directory]) list_header.append(["Recursive:", recursive]) list_header.append(["Ignore symlinks:", ignore_symlinks]) list_header.append(["Action to perform:", action.capitalize()]) list_header.append(["Position:", position.capitalize()]) list_header.append(["Input string:", "\"" + input_string + "\" " \ "(without double quotes)"]) if not replace_string == None: list_header.append(["Replace string:", "\"" + replace_string + \ "\" (without double quotes)"]) if strip_chars == None: list_header.append(["Strip chars:", "None"]) else: list_header.append(["Strip chars:", "\"" + strip_chars + "\" " \ "(without double quotes)"]) list_header.append(["Exclude files:", exclude]) list_header.append(["Explicit files:", explicit]) list_header.append(["Pattern:", pattern]) list_header.append(["Ignore case:", ignore_case]) list_header.append(["Regex syntax:", regex_syntax]) common.report(report_file, list_header, list_renamed, list_excluded, list_skipped, time_start) else: common.rename(list_renamed)
def rename_files(directory, rename_mode, separator=" ", recursive=False, padding=0, exclude=None, pattern=None, ignore_case=True, regex_syntax=False, report_file=None, ignore_symlinks=False, ignore_file_ext=False, custom_name=None, step=1, order_by=None): """ Rename the base name of files based on the name of the directory where they are stored in and add a numeric ID. """ pv.path(directory, "given", False, True) pv.compstr(rename_mode, "rename mode", ["fill-gaps", "increase", "keep-order", "rename-new"]) pv.intrange(padding, "padding", 0, 12, True) pv.string(separator, "seperator", False, common.get_invalid_chars()) pv.intvalue(step, "step", True, False, False) if not order_by == None: pv.compstr(order_by, "order by", ["accessed", "created", "modified"]) if not rename_mode == "keep-order": raise Exception("The order-by argument can only be used in " \ "combination with keep-order mode.") step = int(step) rename_mode = rename_mode.lower() directory = os.path.abspath(directory) if not directory.endswith(os.path.sep): directory += os.path.sep if report_file == None: simulate = False else: pv.path(report_file, "report", True, False) report_file = os.path.abspath(report_file) simulate = True if not custom_name == None: pv.string(custom_name, "custom file name", False, common.get_invalid_chars()) time_start = dt.now() list_content = [] list_excluded = [] list_renamed = [] list_skipped = [] regex = None if not pattern == None: regex = common.compile_regex(pattern, ignore_case, regex_syntax) list_content, list_excluded = \ common.get_files(directory, recursive, ignore_case, regex, exclude, ignore_symlinks, order_by) for item in list_content: list_files = item[1] if rename_mode == "fill-gaps": list_renamed, list_skipped = \ __rename_files_fill(list_files, list_renamed, list_skipped, separator, padding, True, ignore_file_ext, custom_name, step) elif rename_mode == "rename-new": list_renamed, list_skipped = \ __rename_files_fill(list_files, list_renamed, list_skipped, separator, padding, False, ignore_file_ext, custom_name, step) elif rename_mode == "keep-order": list_renamed, list_skipped = \ __rename_files_keep_order(list_files, list_renamed, list_skipped, separator, padding, ignore_file_ext, custom_name, step, order_by) else: raise Exception("An invalid rename mode was given.") if simulate: if padding == 0: padding = "Set automatically" else: padding = str(padding) explicit = None if exclude == None: exclude = False explicit = False elif exclude: explicit = False else: explicit = True if order_by == "accessed": order_by = "Access time" elif order_by == "created": order_by = "Creation time" elif order_by == "modified": order_by = "Modification time" else: order_by = "False" list_header = [] list_header.append("Nomen File Renamer simulation report") list_header.append(["Report file name:", report_file]) list_header.append(["Directory:", directory]) list_header.append(["Recursive:", recursive]) list_header.append(["Ignore symlinks:", ignore_symlinks]) list_header.append(["Rename mode:", rename_mode.capitalize()]) list_header.append(["Order by time:", order_by]) list_header.append(["Separator:", "\"" + separator + "\" " \ "(without double quotes)"]) list_header.append(["Numeric padding:", padding]) list_header.append(["Step size:", step]) list_header.append(["Exclude files:", exclude]) list_header.append(["Explicit files:", explicit]) list_header.append(["Pattern:", pattern]) list_header.append(["Ignore case:", ignore_case]) list_header.append(["Regex syntax:", regex_syntax]) common.report(report_file, list_header, list_renamed, list_excluded, list_skipped, time_start) else: common.rename(list_renamed)
import os import paval as pv import re import statcase from datetime import datetime as dt def convert_case(directory, case, conflict_mode, recursive=False, cfg_lower=None, cfg_mixed=None, cfg_title=None, cfg_upper=None, report_file=None, ignore_symlinks=False): """ Convert the case of the base name of files. """ pv.path(directory, "given", False, True) pv.compstr(case, "case", ["lower", "title", "upper", "config"]) pv.compstr(conflict_mode, "conflict mode", ["rename", "skip"]) case = case.lower() conflict_mode = conflict_mode.lower() directory = os.path.abspath(directory) if not directory.endswith(os.path.sep): directory += os.path.sep if report_file == None: simulate = False else: pv.path(report_file, "report", True, False) report_file = os.path.abspath(report_file) simulate = True time_start = dt.now()
def rename_extensions(directory, conflict_mode, extension, extension_target, recursive=False, ignore_case=True, report_file=None, ignore_symlinks=False): """ Rename the file extensions in the given directory and all of its sub-directories (if requested). """ pv.path(directory, "given", False, True) pv.compstr(conflict_mode, "conflict mode", ["rename", "skip"]) pv.string(extension, "extension", False, common.get_invalid_chars()) pv.string(extension_target, "target extension", False, common.get_invalid_chars()) conflict_mode = conflict_mode.lower() directory = os.path.abspath(directory) if not directory.endswith(os.path.sep): directory += os.path.sep if report_file == None: simulate = False else: pv.path(report_file, "report", True, False) report_file = os.path.abspath(report_file) simulate = True time_start = dt.now() list_content = [] list_excluded = [] list_extensions = [] list_renamed = [] list_skipped = [] if ";" in extension: while (";" * 2) in extension: extension = extension.replace((";" * 2), ";") list_temp = extension.split(";") for extension in list_temp: if not extension == "": list_extensions.append(extension) if len(list_extensions) == 0: raise Exception("The given extension list does not contain any " \ "extensions.") else: list_extensions.append(extension) pattern = "" for extension in list_extensions: pattern += "(.*\." + str(extension) + "$)|" pattern = pattern.rstrip("|") if ignore_case: regex = re.compile(pattern, re.IGNORECASE) else: regex = re.compile(pattern) list_content, list_excluded = \ common.get_files(directory, recursive, ignore_case, regex, False, ignore_symlinks) for item in list_content: list_files = item[1] list_renamed, list_skipped = \ __rename_extensions(list_files, list_extensions, list_renamed, list_skipped, conflict_mode, extension_target) if simulate: list_header = [] list_header.append("Nomen Extension Renamer simulation report") list_header.append(["Report file name:", report_file]) list_header.append(["Directory:", directory]) list_header.append(["Recursive:", recursive]) list_header.append(["Ignore symlinks:", ignore_symlinks]) list_header.append(["Conflict mode:", conflict_mode.capitalize()]) list_header.append(["Extensions:", extension]) list_header.append(["Target extension:", extension_target]) list_header.append(["Ignore case:", ignore_case]) common.report(report_file, list_header, list_renamed, list_excluded, list_skipped, time_start) else: common.rename(list_renamed)