File size: 5,934 Bytes
805d72c
 
 
a996c52
805d72c
 
 
 
a996c52
805d72c
 
 
 
 
a996c52
805d72c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a996c52
805d72c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a996c52
805d72c
 
 
 
 
 
 
a996c52
 
 
 
 
 
 
 
805d72c
 
 
 
 
 
 
a996c52
 
 
 
 
 
 
 
 
 
 
 
f5e08f1
a996c52
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import re
from shutil import copy
import gradio as gr
from os import makedirs, listdir
from os.path import isdir, isfile, join, realpath
from tkinter import Tk
from tkinter.filedialog import askdirectory

# --- Constants ---
phases = ["iFAT", "(i)SAT"]
inputPhases = {**{i: k for i, k in enumerate(phases)}, **{len(phases): "All"}}
exitinput = {"no", "n", "0"}
regTemplate = r'template'

# --- Helper Functions ---

# Function to print available options
def printOptions():
    print("\nchoose one of the following options;\n")
    for key in inputPhases:
        print("[%d] %s" % (key, inputPhases[key]))
    print()

# Function to get phase input
def validatedPhaseInput():
    inputPhase = None
    while inputPhase is None:
        printOptions()
        inputPhase = input()

        if inputPhase.isnumeric():
            inputPhase = int(inputPhase)
            if inputPhase not in range(len(inputPhases)):
                print("\n", inputPhase, "is not a valid option")
                inputPhase = None
            else:
                return inputPhases[inputPhase]
        else:
            inputPhase = inputPhase.lower()
            if inputPhase not in phases:
                print("\n", inputPhase, "is not a valid option")
                inputPhase = None
            else:
                return inputPhases[inputPhase]

    print("Something went wrong, please consult the maintainer of the codebase.")
    return inputPhase

# Function to validate if a directory exists
def validate_directory(directory_path):
    return os.path.isdir(directory_path)

# Function to get list of files matching a pattern
def getFilesWith(path: str, reg: str):
    if not isdir(path):
        print(f"{path} is not a valid path")
        return None
    content = listdir(path)
    if len(content) == 0:
        print(f"{path} has no content")
        return None
    files = [f for f in content if isfile(join(path, f)) and re.search(reg, f, re.IGNORECASE)]
    if len(files) == 0:
        print(f"{path} contains no {reg}")
        return None
    return files

# Function to create new folders
def createNewFolders(dirs: list):
    for d in dirs:
        if not isdir(d):
            makedirs(d)
        else:
            print(f"Directory already exists: {d}")

# Function to create new templates
def createNewTemplates(objs, templatesDir, regTemplate, root):
    templatefiles = getFilesWith(templatesDir, regTemplate)
    for k in objs:
        regPhase = r""
        match k:
            case "(i)SAT":
                regPhase = r"sat"
            case "iFAT":
                regPhase = r"fat"

        files = [f for f in templatefiles if re.search(regPhase, f, re.IGNORECASE)]
        if len(files) == 0:
            print(f"Phase {k} has no templates")
            continue

        for o in objs[k]:
            targetLocation = join(root, o)
            tlFiles = getFilesWith(targetLocation, regPhase)

            if tlFiles:
                print(f"{k} files already exist in: {targetLocation}")
                continue

            for f in files:
                templatepath = join(templatesDir, f)
                targetpath = targetLocation
                if re.search(r"hut_\d{4}[a-zA-Z]{2}", f, re.IGNORECASE):
                    targetpath = join(targetLocation, f[:4] + o + f[10:])
                copy(templatepath, targetpath)

# Function to get objects per phase
def getObjectsPerPhase(phase: str = "All"):
    with open("./objecten.txt", "r") as f:
        t = f.read().split("\n\n")

    objs = {p: [] for p in phases}
    if phase in phases:
        objs = {phase: []}

    regObject = r"\d{4}[a-zA-Z]{2}"
    for g in t:
        ls = g.split("\n")
        k = ls[0]
        if k in objs:
            objs[k] = ls[1::]
        else:
            print(f"Key [{k}] is not recognized")

    objs = {k: objs[k] for k in objs if objs[k]}

    for k in objs:
        for i, o in enumerate(objs[k]):
            m = re.search(regObject, o)
            if not m:
                continue
            objs[k][i] = m.group(0)

    return objs

# Function to copy and paste templates
def copyPasteTemplates(root: str, phase: str, templatesDir: str):
    objs = getObjectsPerPhase(phase)
    objectslist = list(set([o for p in [objs[k] for k in objs] for o in p]))
    
    createNewFolders([join(root, o) for o in objectslist])
    print("Directories created")
    
    createNewTemplates(objs, templatesDir, regTemplate, root)
    print("Templates ready")

# --- Main Gradio App ---

# Main function to run steps
def run_steps(phase, root_dir, templates_dir):
    testphase = phase
    rootDir = root_dir
    templatesDir = templates_dir

    # Run folder creation process
    if not validate_directory(rootDir):
        return f"Error: Root directory '{rootDir}' does not exist."
    if not validate_directory(templatesDir):
        return f"Error: Templates directory '{templatesDir}' does not exist."

    copyPasteTemplates(rootDir, testphase, templatesDir)
    return "Folders created successfully!"

# Gradio interface
def validate_and_run(phase, root_dir, templates_dir):
    if not root_dir or not templates_dir:
        return "Error: Please provide both the root and templates directory paths."
    
    return run_steps(phase, root_dir, templates_dir)

# Gradio app components
with gr.Blocks() as app:
    gr.Markdown("# Folder Creation Tool")
    
    phase_input = gr.Dropdown(choices=list(inputPhases.values()), label="Select Phase")
    root_dir_input = gr.Textbox(label="Root Directory", placeholder="Enter the root directory path")
    templates_dir_input = gr.Textbox(label="Templates Directory", placeholder="Enter the templates directory path")
    
    create_button = gr.Button("Create Folders")
    output = gr.Textbox(label="Output")
    
    create_button.click(validate_and_run, inputs=[phase_input, root_dir_input, templates_dir_input], outputs=output)

app.launch()