# File Renamer || Python Backend

# 11.8.2020 <NAME>


import os
import json

class Renamer():


    def __init__(self):
        
        self.options = {}
        self.path = ""


    def load_options(self,json_path):

        with open(json_path,encoding="UTF-8") as opt:
            self.options = json.load(opt)
            self.path = self.options["path"]
    

    def rename_the_path(self,path):

        # If options loaded.
        if self.options != {}:
            main_path = path
            file_names = [file_name for file_name in os.listdir(main_path)]
            file_extensions = []
            file_name_without_extensions = []
            for file_name in file_names:

                try:
                    file_ext = file_name.split(".")[1]
                    file_extensions.append(file_ext)
                    file_name_without_extensions.append(file_name.split(".")[0])

                except:
                    file_extensions.append("")
                    file_name_without_extensions.append(file_name)
            
            PATH_LEN = len(file_name_without_extensions)
            KEYWORD = self.options["keyword"]
            SEED = self.options["seed"]


            
            # Now we have main path, file names and file extensions, let's rename everything
            for  file_name,file_ext,seed in zip(file_name_without_extensions,file_extensions,range(SEED,PATH_LEN+SEED)):

                os.rename(main_path+file_name+"."+file_ext,main_path+str(KEYWORD)+str(seed)+"."+file_ext)




renamer = Renamer()

renamer.load_options(json_path="options.json")


renamer.rename_the_path(renamer.path)



# TODO - IN ORDER TO MAKE THIS SCRIPT MORE USEFUL ADD THE GUI WITH C# 

