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|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Main function to run steps
|
| 38 |
def run_steps(phase, root_dir, templates_dir):
|
| 39 |
-
global testphase, rootDir, templatesDir
|
| 40 |
testphase = phase
|
| 41 |
rootDir = root_dir
|
| 42 |
templatesDir = templates_dir
|
| 43 |
|
| 44 |
# Run folder creation process
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Gradio interface
|
| 49 |
def validate_and_run(phase, root_dir, templates_dir):
|
| 50 |
-
# Check for empty inputs
|
| 51 |
if not root_dir or not templates_dir:
|
| 52 |
return "Error: Please provide both the root and templates directory paths."
|
| 53 |
|
| 54 |
-
# Run the main function with inputs
|
| 55 |
return run_steps(phase, root_dir, templates_dir)
|
| 56 |
|
| 57 |
# Gradio app components
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
from shutil import copy
|
| 4 |
import gradio as gr
|
| 5 |
+
from os import makedirs, listdir
|
| 6 |
+
from os.path import isdir, isfile, join, realpath
|
| 7 |
+
from tkinter import Tk
|
| 8 |
+
from tkinter.filedialog import askdirectory
|
| 9 |
|
| 10 |
+
# --- Constants ---
|
| 11 |
+
phases = ["iFAT", "(i)SAT"]
|
| 12 |
+
inputPhases = {**{i: k for i, k in enumerate(phases)}, **{len(phases): "All"}}
|
| 13 |
+
exitinput = {"no", "n", "0"}
|
| 14 |
+
regTemplate = r'template'
|
| 15 |
|
| 16 |
+
# --- Helper Functions ---
|
| 17 |
+
|
| 18 |
+
# Function to print available options
|
| 19 |
+
def printOptions():
|
| 20 |
+
print("\nchoose one of the following options;\n")
|
| 21 |
+
for key in inputPhases:
|
| 22 |
+
print("[%d] %s" % (key, inputPhases[key]))
|
| 23 |
+
print()
|
| 24 |
+
|
| 25 |
+
# Function to get phase input
|
| 26 |
+
def validatedPhaseInput():
|
| 27 |
+
inputPhase = None
|
| 28 |
+
while inputPhase is None:
|
| 29 |
+
printOptions()
|
| 30 |
+
inputPhase = input()
|
| 31 |
+
|
| 32 |
+
if inputPhase.isnumeric():
|
| 33 |
+
inputPhase = int(inputPhase)
|
| 34 |
+
if inputPhase not in range(len(inputPhases)):
|
| 35 |
+
print("\n", inputPhase, "is not a valid option")
|
| 36 |
+
inputPhase = None
|
| 37 |
+
else:
|
| 38 |
+
return inputPhases[inputPhase]
|
| 39 |
+
else:
|
| 40 |
+
inputPhase = inputPhase.lower()
|
| 41 |
+
if inputPhase not in phases:
|
| 42 |
+
print("\n", inputPhase, "is not a valid option")
|
| 43 |
+
inputPhase = None
|
| 44 |
+
else:
|
| 45 |
+
return inputPhases[inputPhase]
|
| 46 |
+
|
| 47 |
+
print("Something went wrong, please consult the maintainer of the codebase.")
|
| 48 |
+
return inputPhase
|
| 49 |
+
|
| 50 |
+
# Function to validate if a directory exists
|
| 51 |
def validate_directory(directory_path):
|
| 52 |
+
return os.path.isdir(directory_path)
|
| 53 |
+
|
| 54 |
+
# Function to get list of files matching a pattern
|
| 55 |
+
def getFilesWith(path: str, reg: str):
|
| 56 |
+
if not isdir(path):
|
| 57 |
+
print(f"{path} is not a valid path")
|
| 58 |
+
return None
|
| 59 |
+
content = listdir(path)
|
| 60 |
+
if len(content) == 0:
|
| 61 |
+
print(f"{path} has no content")
|
| 62 |
+
return None
|
| 63 |
+
files = [f for f in content if isfile(join(path, f)) and re.search(reg, f, re.IGNORECASE)]
|
| 64 |
+
if len(files) == 0:
|
| 65 |
+
print(f"{path} contains no {reg}")
|
| 66 |
+
return None
|
| 67 |
+
return files
|
| 68 |
+
|
| 69 |
+
# Function to create new folders
|
| 70 |
+
def createNewFolders(dirs: list):
|
| 71 |
+
for d in dirs:
|
| 72 |
+
if not isdir(d):
|
| 73 |
+
makedirs(d)
|
| 74 |
+
else:
|
| 75 |
+
print(f"Directory already exists: {d}")
|
| 76 |
+
|
| 77 |
+
# Function to create new templates
|
| 78 |
+
def createNewTemplates(objs, templatesDir, regTemplate, root):
|
| 79 |
+
templatefiles = getFilesWith(templatesDir, regTemplate)
|
| 80 |
+
for k in objs:
|
| 81 |
+
regPhase = r""
|
| 82 |
+
match k:
|
| 83 |
+
case "(i)SAT":
|
| 84 |
+
regPhase = r"sat"
|
| 85 |
+
case "iFAT":
|
| 86 |
+
regPhase = r"fat"
|
| 87 |
+
|
| 88 |
+
files = [f for f in templatefiles if re.search(regPhase, f, re.IGNORECASE)]
|
| 89 |
+
if len(files) == 0:
|
| 90 |
+
print(f"Phase {k} has no templates")
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
for o in objs[k]:
|
| 94 |
+
targetLocation = join(root, o)
|
| 95 |
+
tlFiles = getFilesWith(targetLocation, regPhase)
|
| 96 |
+
|
| 97 |
+
if tlFiles:
|
| 98 |
+
print(f"{k} files already exist in: {targetLocation}")
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
for f in files:
|
| 102 |
+
templatepath = join(templatesDir, f)
|
| 103 |
+
targetpath = targetLocation
|
| 104 |
+
if re.search(r"hut_\d{4}[a-zA-Z]{2}", f, re.IGNORECASE):
|
| 105 |
+
targetpath = join(targetLocation, f[:4] + o + f[10:])
|
| 106 |
+
copy(templatepath, targetpath)
|
| 107 |
+
|
| 108 |
+
# Function to get objects per phase
|
| 109 |
+
def getObjectsPerPhase(phase: str = "All"):
|
| 110 |
+
with open("./objecten.txt", "r") as f:
|
| 111 |
+
t = f.read().split("\n\n")
|
| 112 |
+
|
| 113 |
+
objs = {p: [] for p in phases}
|
| 114 |
+
if phase in phases:
|
| 115 |
+
objs = {phase: []}
|
| 116 |
+
|
| 117 |
+
regObject = r"\d{4}[a-zA-Z]{2}"
|
| 118 |
+
for g in t:
|
| 119 |
+
ls = g.split("\n")
|
| 120 |
+
k = ls[0]
|
| 121 |
+
if k in objs:
|
| 122 |
+
objs[k] = ls[1::]
|
| 123 |
+
else:
|
| 124 |
+
print(f"Key [{k}] is not recognized")
|
| 125 |
+
|
| 126 |
+
objs = {k: objs[k] for k in objs if objs[k]}
|
| 127 |
+
|
| 128 |
+
for k in objs:
|
| 129 |
+
for i, o in enumerate(objs[k]):
|
| 130 |
+
m = re.search(regObject, o)
|
| 131 |
+
if not m:
|
| 132 |
+
continue
|
| 133 |
+
objs[k][i] = m.group(0)
|
| 134 |
+
|
| 135 |
+
return objs
|
| 136 |
+
|
| 137 |
+
# Function to copy and paste templates
|
| 138 |
+
def copyPasteTemplates(root: str, phase: str, templatesDir: str):
|
| 139 |
+
objs = getObjectsPerPhase(phase)
|
| 140 |
+
objectslist = list(set([o for p in [objs[k] for k in objs] for o in p]))
|
| 141 |
|
| 142 |
+
createNewFolders([join(root, o) for o in objectslist])
|
| 143 |
+
print("Directories created")
|
| 144 |
+
|
| 145 |
+
createNewTemplates(objs, templatesDir, regTemplate, root)
|
| 146 |
+
print("Templates ready")
|
| 147 |
+
|
| 148 |
+
# --- Main Gradio App ---
|
| 149 |
|
| 150 |
# Main function to run steps
|
| 151 |
def run_steps(phase, root_dir, templates_dir):
|
|
|
|
| 152 |
testphase = phase
|
| 153 |
rootDir = root_dir
|
| 154 |
templatesDir = templates_dir
|
| 155 |
|
| 156 |
# Run folder creation process
|
| 157 |
+
if not validate_directory(rootDir):
|
| 158 |
+
return f"Error: Root directory '{rootDir}' does not exist."
|
| 159 |
+
if not validate_directory(templatesDir):
|
| 160 |
+
return f"Error: Templates directory '{templatesDir}' does not exist."
|
| 161 |
+
|
| 162 |
+
copyPasteTemplates(rootDir, testphase, templatesDir)
|
| 163 |
+
return "Folders created successfully!"
|
| 164 |
|
| 165 |
# Gradio interface
|
| 166 |
def validate_and_run(phase, root_dir, templates_dir):
|
|
|
|
| 167 |
if not root_dir or not templates_dir:
|
| 168 |
return "Error: Please provide both the root and templates directory paths."
|
| 169 |
|
|
|
|
| 170 |
return run_steps(phase, root_dir, templates_dir)
|
| 171 |
|
| 172 |
# Gradio app components
|