<commit_before># Copyright (c) 2016, <NAME> (<EMAIL>)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the project.

import os
import imghdr

__author__ = '<NAME>'


# TODO add start and end
# TODO add logfile
# TODO move start script to another file
# TODO add commandline support - add cmdline parser and optional reading of JSON argument file
# TODO add tests

class Main:
    def run(self):
        """
        Script used to batch rename files in a directory tree. At the moment only files in the
        first layer of subfolders will be taken into account.
        Files will be renamed in the following fashion:
        '[Subdirectory name]_[specified batch name]_[progressing index]__[part of the previous name after separator]'.
        The index will be reset for each subfolder, the starting index can be defined.
        It can be defined whether all files should be renamed, the renaming can also be restricted to
        specified file types only.
        Subdirectories can be excluded by name.
        """
        # Define the path of the main directory from which to rename files in folders.
        main_dir_path = 'D:\\_Chaos\\Bilder\\201510_Zentralasien\\testRename\\'
        # Define a base string that will be inserted into the renaming string.
        main_name = 'Zentralasien_'
        # Define at which number the index for renaming multiple files in each folder should start.
        batch_start_index = 1
        # Separator string that separates the part of the existing filename that will be
        # replaced with the part of the existing filename that will be kept for the renaming string.
        name_separator = '__'
        # Define if all files encountered in a directory should be renamed
        # or if only a defined subset should be subjected to renaming.
        rename_all_file_types = True
        # Define which types of files should be renamed.
        rename_file_types = ['JPG', 'JPEG', 'PNG']
        # Define which directories within the main directory should be excluded all together.
        # This is case sensitive.
        exclude_dirs = ['excludeMe', '20150926_meToo']

        for path, dirs, files in os.walk(main_dir_path):
            if path != main_dir_path:

                # TODO add better check for exclude_dirs at this point
                do_exclude = False
                for curr_dir in exclude_dirs:
                    if path.__contains__(curr_dir):
                        print("[Info] Directory " + curr_dir + " will be excluded")
                        do_exclude = True

                if not do_exclude:
                    print("[Info] Process directory " + path)
                    i = batch_start_index
                    for f in files:
                        original_file = os.path.join(path, f)

                        if rename_all_file_types or rename_file_types.__contains__(str(imghdr.what(original_file)).upper()):
                            if not f.__contains__(name_separator):
                                # TODO integrate filename in a nicer way into the print string
                                print("[Warning] File \'" + f + "\' does not contain separator")
                            else:
                                split_parts = f.split(name_separator)
                                file_name = os.path.basename(path) + main_name + "{:03d}".format(i)
                                file_name += name_separator + split_parts[1]
                                formatted_name = os.path.join(path, file_name)
                                #print("[Info] Rename to " + formatted_name)
                                os.rename(original_file, formatted_name)
                                i += 1<commit_msg>add use directory name conditional<commit_after># Copyright (c) 2016, <NAME> (<EMAIL>)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the project.

import os
import imghdr

__author__ = '<NAME>'


# TODO add start and end
# TODO add logfile
# TODO add commandline support - add cmdline parser and optional reading of JSON argument file
# TODO add tests

class Main:
    def run(self):
        """
        Script used to batch rename files in a directory tree. At the moment only files in the
        first layer of subfolders will be taken into account.
        Files will be renamed in the following fashion:
        '[Subdirectory name]_[specified batch name]_[progressing index]__[part of the previous name after separator]'.
        The index will be reset for each subfolder, the starting index can be defined.
        It can be defined whether all files should be renamed, the renaming can also be restricted to
        specified file types only.
        Subdirectories can be excluded by name.
        """
        # Define the path of the main directory from which to rename files in folders.
        main_dir_path = 'D:\\_Chaos\\Bilder\\201510_Zentralasien\\testRename\\'
        # Define a base string that will be inserted into the renaming string.
        main_name = 'Zentralasien_'
        # Use the name of the directory when renaming a file.
        add_directory_name = True
        # Separator string that separates the part of the existing filename that will be
        # replaced with the part of the existing filename that will be kept for the renaming string.
        name_separator = '__'
        # White space separator.
        ws_separator = '_'
        # Define at which number the index for renaming multiple files in each folder should start.
        batch_start_index = 1
        # Define if all files encountered in a directory should be renamed
        # or if only a defined subset should be subjected to renaming.
        rename_all_file_types = True
        # Define which types of files should be renamed.
        rename_file_types = ['JPG', 'JPEG', 'PNG']
        # Define which directories within the main directory should be excluded all together.
        # This is case sensitive.
        exclude_dirs = ['excludeMe', '20150926_meToo']

        for path, dirs, files in os.walk(main_dir_path):
            if path != main_dir_path:

                # TODO add better check for exclude_dirs at this point
                do_exclude = False
                for curr_dir in exclude_dirs:
                    if path.__contains__(curr_dir):
                        print("[Info] Directory " + curr_dir + " will be excluded")
                        do_exclude = True

                if not do_exclude:
                    print("[Info] Process directory " + path)
                    i = batch_start_index
                    for f in files:
                        original_file = os.path.join(path, f)

                        if rename_all_file_types or rename_file_types.__contains__(str(imghdr.what(original_file)).upper()):
                            if not f.__contains__(name_separator):
                                # TODO integrate filename in a nicer way into the print string
                                print("[Warning] File \'" + f + "\' does not contain separator")
                            else:
                                split_parts = f.split(name_separator)

                                file_name = ""
                                if add_directory_name:
                                    file_name = os.path.basename(path)

                                file_name = file_name + main_name + "{:03d}".format(i)
                                file_name += name_separator + split_parts[1]
                                formatted_name = os.path.join(path, file_name)
                                print("[Info] Rename to " + formatted_name)
                                #os.rename(original_file, formatted_name)
                                i += 1
{'fraction_non_alphanumeric': 0.061462205700123916, 'fraction_numerical': 0.005700123915737299, 'mean_word_length': 1.8559801840056618, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '266453', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1924, 'n_words': 807}