Esempio n. 1
0
def read_option(file_path, section, option, fallback=None):
    """
        Parse config file and read out the value of a certain option.
    """
    try:
        # For details see the notice in the header
        import paval as pv

        pv.path(file_path, "config", True, True)
        pv.string(section, "section string")
        pv.string(option, "option string")
    except NameError:
        pass

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    value = ""
    try:
        value = c.get(section, option)
    except ConfigParser.NoSectionError:
        if fallback:
            return str(fallback)
        else:
            raise Exception("This section does not exist in the given " \
                            "config file.")
    except ConfigParser.NoOptionError:
        if fallback:
            return str(fallback)
        else:
            raise Exception("This option does not exist in the given " \
                            "section.")

    return str(value)
Esempio n. 2
0
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.")
Esempio n. 3
0
def remove_section(file_path, section):
    """
        Remove a certain section from the parameter file.
    """
    pv.path(file_path, "parameter", True, True)
    pv.string(section, "section string")

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    c.remove_section(section)
    with open(file_path, 'w') as fh_parameter:
        c.write(fh_parameter)
Esempio n. 4
0
def replace_chars(config_file,
                  string="",
                  remove_spaces=False,
                  remove_chars=False,
                  number=1,
                  sort_length=False):
    """
        Replace characters in a string based on the given config file.
    """
    pv.string(string, "string to modify")
    pv.intrange(number, "number of strings", 1, None)
    try:
        pv.path(config_file, "config", True, True)
    except:
        config_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        config_file = \
            os.path.join(config_dir, "cfg", os.path.basename(config_file))
        pv.path(config_file, "config", True, True)

    config_file = os.path.abspath(config_file)
    number = int(number)

    if remove_spaces:
        string = string.replace(" ", "")

    if remove_chars:
        chars = __read_option(config_file, "Remove", "Characters")
        chars = chars.replace(" ", "")

        for char in chars:
            string = string.replace(char, "")

    dict_chars = {}
    for char in string:
        if char in ("[", "]", "=", ";", ","):
            continue

        value = __read_option(config_file, "Replace", char.upper())
        if char not in dict_chars:
            replace_chars = value.strip(", ").replace(" ", "").split(",")
            dict_chars.update({char: replace_chars})

    output = []
    for n in range(number):
        string_new = __transform(string, output, dict_chars)
        output.append(string_new)

    if sort_length:
        output = sorted(output, key=len, reverse=True)

    return output
Esempio n. 5
0
def indent(directory,
           file_ext,
           spaces=4,
           padding=12,
           left_justify=False,
           recursive=False,
           overwrite=False,
           verbose=False):
    """
        Method to perform the indentation process.
    """
    pv.path(directory, "input", False, True)
    pv.string(file_ext, "file extension", False, None)
    pv.intvalue(spaces, "spaces", True, False, False)
    pv.intvalue(padding, "padding", True, False, False)

    directory = os.path.abspath(directory)
    spaces = int(spaces)
    padding = int(padding)
    num = 1

    if verbose:
        print "\nGathering files to process. Please wait.\n"

    list_files = common.get_files(directory, file_ext, recursive)

    if len(list_files) == 0:
        if verbose:
            print "No files to process.\n"
            return

    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:
            __indent_file(file_input, spaces, padding, left_justify)
        else:
            __indent_copy(file_input, spaces, padding, left_justify)

    if verbose:
        print "\nFinished.\n"
Esempio n. 6
0
def write_option(file_path, section, option, value="", new_file=False):
    """
        Write an option into a parameter file.
    """
    if not new_file:
        pv.path(file_path, "parameter", True, True)
    pv.string(section, "section string")
    pv.string(option, "option string")

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    if not section in c.sections():
        c.add_section(section)

    c.set(section, option, value)
    with open(file_path, 'w') as fh_parameter:
        c.write(fh_parameter)
Esempio n. 7
0
def compile_regex(string, ignore_case=True, regex_syntax=False):
    """
        Compile a regular expression from the given string.
    """
    pv.string(string, "regular expression", True, None)

    # Either use the professional way (regex syntax) or the simplified way
    # (only supports an asterisk as wildcard and semicolon as a seperator)
    if regex_syntax:
        pattern = ".*" + string + ".*"
    else:
        pattern = ""
        spec_chars = [
            "\\", ".", "^", "$", "+", "?", "{", "}", "[", "]", "|", "(", ")"
        ]

        for char in spec_chars:
            string = string.replace(char, "\\" + char)

        string = string.strip("*")
        string = string.strip(";")

        while ("*" * 2) in string:
            string = string.replace(("*" * 2), "*")

        while (";" * 2) in string:
            string = string.replace((";" * 2), ";")

        list_string = string.split(";")
        for string in list_string:
            if not string == "":
                pattern += "(.*" + string.replace("*", ".*") + ".*)|"
        pattern = pattern.rstrip("|")

        if pattern == "":
            raise Exception("The given string does not make sense this way.")

    if ignore_case:
        regex = re.compile(pattern, re.IGNORECASE)
    else:
        regex = re.compile(pattern)

    return regex
Esempio n. 8
0
def remove_section(file_path, section):
    """
        Remove a certain section from the config file.
    """
    try:
        # For details see the notice in the header
        import paval as pv

        pv.path(file_path, "config", True, True)
        pv.string(section, "section string")
    except NameError:
        pass

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    c.remove_section(section)
    with open(file_path, 'w') as fh_config:
        c.write(fh_config)
Esempio n. 9
0
def write_option(file_path, section, option, value="", new_section=False,
                 new_file=False, force=False):
    """
        Write an option into a config file.
    """
    try:
        # For details see the notice in the header
        import paval as pv

        if not force:
            if new_file:
                pv.path(file_path, "config", True, False)
                new_section = True
            else:
                pv.path(file_path, "config", True, True)
            pv.string(section, "section string")
            pv.string(option, "option string")
    except NameError:
        if new_file:
            new_section = True

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    if force:
        # This makes the parameters "new_section" and "new_file" obsolete, so
        # they will be ignored
        if not section in c.sections():
            c.add_section(section)
    else:
        if new_section:
            if section in c.sections():
                raise Exception("This section cannot be created, because " \
                                "it already exists.")
            c.add_section(section)
        else:
            if not section in c.sections():
                raise Exception("This section does not exist.")

    c.set(section, option, value)
    with open(file_path, 'w') as fh_config:
        c.write(fh_config)
Esempio n. 10
0
def read_option(file_path, section, option, fallback=None, empty=True):
    """
        Parse parameter file and read out the value of a certain option.
    """
    pv.path(file_path, "parameter", True, True)
    pv.string(section, "section string")
    pv.string(option, "option string")

    c = ConfigParser.RawConfigParser()
    c.read(file_path)

    value = ""
    try:
        value = c.get(section, option)
    except ConfigParser.NoSectionError:
        if fallback:
            return str(fallback)
        else:
            pass
    except ConfigParser.NoOptionError:
        if fallback:
            return str(fallback)
        else:
            pass

    try:
        int(value)
        return int(value)
    except:
        value = str(value)
        if len(value) > 0:
            return value
        else:
            if empty:
                return value
            else:
                if fallback == None:
                    return None
                else:
                    return str(fallback)
Esempio n. 11
0
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"
Esempio n. 12
0
def find_term(content_file, search_term, ignore_case=True, regex_syntax=False):
    """
        Search a content file for the given search term.
    """
    pv.path(content_file, "content file", True, True)
    pv.string(search_term, "search term", True, None)

    content_file = os.path.abspath(content_file)
    list_matches = []
    regex = common.compile_regex(search_term, ignore_case, regex_syntax)

    fh_content = open(content_file, "r")
    for line in fh_content:
        if ignore_case:
            if regex.match(line.lower()):
                list_matches.append(line.replace("\n", ""))
        else:
            if regex.match(line):
                list_matches.append(line.replace("\n", ""))

    fh_content.close()
    list_matches.sort()

    return list_matches
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
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)