GPT
Browse files
app.py
CHANGED
|
@@ -1,171 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import gradio as gr
|
| 4 |
-
from shutil import copy
|
| 5 |
-
from tkinter import Tk
|
| 6 |
-
from tkinter.filedialog import askdirectory
|
| 7 |
-
from os import makedirs, listdir
|
| 8 |
-
from os.path import isdir, isfile, join, realpath
|
| 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 |
-
return
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
case "iFAT":
|
| 59 |
-
regPhase = r"fat"
|
| 60 |
-
|
| 61 |
-
files = [f for f in templatefiles if re.search(regPhase, f, re.IGNORECASE)]
|
| 62 |
-
if len(files) == 0:
|
| 63 |
-
print(f"Phase {k} has no templates")
|
| 64 |
-
continue
|
| 65 |
-
|
| 66 |
-
for o in objs[k]:
|
| 67 |
-
targetLocation = join(root, o)
|
| 68 |
-
tlFiles = getFilesWith(targetLocation, regPhase)
|
| 69 |
-
|
| 70 |
-
if tlFiles:
|
| 71 |
-
print(f"{k} files already exist in: {targetLocation}")
|
| 72 |
-
continue
|
| 73 |
-
|
| 74 |
-
for f in files:
|
| 75 |
-
templatepath = join(templatesDir, f)
|
| 76 |
-
targetpath = targetLocation
|
| 77 |
-
if re.search(r"hut_\d{4}[a-zA-Z]{2}", f, re.IGNORECASE):
|
| 78 |
-
targetpath = join(targetLocation, f[:4] + o + f[10:])
|
| 79 |
-
copy(templatepath, targetpath)
|
| 80 |
-
|
| 81 |
-
# Function to get objects per phase
|
| 82 |
-
def getObjectsPerPhase(phase: str = "All"):
|
| 83 |
-
with open("./objecten.txt", "r") as f:
|
| 84 |
-
t = f.read().split("\n\n")
|
| 85 |
-
|
| 86 |
-
objs = {p: [] for p in phases}
|
| 87 |
-
if phase in phases:
|
| 88 |
-
objs = {phase: []}
|
| 89 |
-
|
| 90 |
-
regObject = r"\d{4}[a-zA-Z]{2}"
|
| 91 |
-
for g in t:
|
| 92 |
-
ls = g.split("\n")
|
| 93 |
-
k = ls[0]
|
| 94 |
-
if k in objs:
|
| 95 |
-
objs[k] = ls[1::]
|
| 96 |
-
else:
|
| 97 |
-
print(f"Key [{k}] is not recognized")
|
| 98 |
-
|
| 99 |
-
objs = {k: objs[k] for k in objs if objs[k]}
|
| 100 |
-
|
| 101 |
-
for k in objs:
|
| 102 |
-
for i, o in enumerate(objs[k]):
|
| 103 |
-
m = re.search(regObject, o)
|
| 104 |
-
if not m:
|
| 105 |
-
continue
|
| 106 |
-
objs[k][i] = m.group(0)
|
| 107 |
-
|
| 108 |
-
return objs
|
| 109 |
-
|
| 110 |
-
# Function to copy and paste templates
|
| 111 |
-
def copyPasteTemplates(root: str, phase: str, templatesDir: str):
|
| 112 |
-
objs = getObjectsPerPhase(phase)
|
| 113 |
-
objectslist = list(set([o for p in [objs[k] for k in objs] for o in p]))
|
| 114 |
-
|
| 115 |
-
createNewFolders([join(root, o) for o in objectslist])
|
| 116 |
-
print("Directories created")
|
| 117 |
-
|
| 118 |
-
createNewTemplates(objs, templatesDir, regTemplate, root)
|
| 119 |
-
print("Templates ready")
|
| 120 |
-
|
| 121 |
-
# --- Main Gradio App ---
|
| 122 |
-
|
| 123 |
-
# Main function to run steps
|
| 124 |
-
def run_steps(phase, root_dir, templates_dir):
|
| 125 |
-
testphase = phase
|
| 126 |
-
rootDir = root_dir
|
| 127 |
-
templatesDir = templates_dir
|
| 128 |
-
|
| 129 |
-
# Run folder creation process
|
| 130 |
-
if not validate_directory(rootDir):
|
| 131 |
-
return f"Error: Root directory '{rootDir}' does not exist."
|
| 132 |
-
if not validate_directory(templatesDir):
|
| 133 |
-
return f"Error: Templates directory '{templatesDir}' does not exist."
|
| 134 |
-
|
| 135 |
-
copyPasteTemplates(rootDir, testphase, templatesDir)
|
| 136 |
-
return "Folders created successfully!"
|
| 137 |
-
|
| 138 |
-
# Gradio interface
|
| 139 |
-
def validate_and_run(phase, root_dir, templates_dir):
|
| 140 |
-
if not root_dir or not templates_dir:
|
| 141 |
-
return "Error: Please provide both the root and templates directory paths."
|
| 142 |
-
|
| 143 |
-
return run_steps(phase, root_dir, templates_dir)
|
| 144 |
-
|
| 145 |
-
# Functions to trigger folder selection for Gradio
|
| 146 |
-
def select_root_directory():
|
| 147 |
-
return select_folder()
|
| 148 |
-
|
| 149 |
-
def select_templates_directory():
|
| 150 |
-
return select_folder()
|
| 151 |
-
|
| 152 |
-
# Gradio app components
|
| 153 |
-
with gr.Blocks() as app:
|
| 154 |
-
gr.Markdown("# Folder Creation Tool")
|
| 155 |
-
|
| 156 |
-
phase_input = gr.Dropdown(choices=list(inputPhases.values()), label="Select Phase")
|
| 157 |
-
|
| 158 |
-
root_dir_input = gr.Textbox(label="Root Directory", placeholder="Select the root directory")
|
| 159 |
-
root_dir_button = gr.Button("Select Root Directory")
|
| 160 |
-
root_dir_button.click(fn=select_root_directory, outputs=root_dir_input)
|
| 161 |
-
|
| 162 |
-
templates_dir_input = gr.Textbox(label="Templates Directory", placeholder="Select the templates directory")
|
| 163 |
-
templates_dir_button = gr.Button("Select Templates Directory")
|
| 164 |
-
templates_dir_button.click(fn=select_templates_directory, outputs=templates_dir_input)
|
| 165 |
-
|
| 166 |
-
create_button = gr.Button("Create Folders")
|
| 167 |
-
output = gr.Textbox(label="Output")
|
| 168 |
-
|
| 169 |
-
create_button.click(validate_and_run, inputs=[phase_input, root_dir_input, templates_dir_input], outputs=output)
|
| 170 |
-
|
| 171 |
-
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def process(phase, target_dir, template_dir, object_file):
|
| 6 |
+
try:
|
| 7 |
+
if not os.path.isdir(target_dir):
|
| 8 |
+
return "Error: Target directory does not exist."
|
| 9 |
+
if not os.path.isdir(template_dir):
|
| 10 |
+
return "Error: Template directory does not exist."
|
| 11 |
+
|
| 12 |
+
# Read object names from the txt file
|
| 13 |
+
object_names = object_file.read().decode('utf-8').splitlines()
|
| 14 |
+
|
| 15 |
+
for object_name in object_names:
|
| 16 |
+
if not object_name.strip():
|
| 17 |
+
continue # Skip empty lines
|
| 18 |
+
# Create a directory for the object under target_dir
|
| 19 |
+
object_dir = os.path.join(target_dir, f"{phase}_{object_name}")
|
| 20 |
+
os.makedirs(object_dir, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# Copy files from template_dir to object_dir
|
| 23 |
+
for filename in os.listdir(template_dir):
|
| 24 |
+
template_file = os.path.join(template_dir, filename)
|
| 25 |
+
if os.path.isfile(template_file):
|
| 26 |
+
# Get the file extension
|
| 27 |
+
name, ext = os.path.splitext(filename)
|
| 28 |
+
# Create new filename with object name appended
|
| 29 |
+
new_filename = f"{name}_{object_name}{ext}"
|
| 30 |
+
new_file = os.path.join(object_dir, new_filename)
|
| 31 |
+
shutil.copy(template_file, new_file)
|
| 32 |
+
return f"Created directories and files for {len(object_names)} objects."
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error: {str(e)}"
|
| 35 |
+
|
| 36 |
+
phase_dropdown = gr.Dropdown(choices=['FAT', 'iFAT', 'iSAT'], label='Select Phase')
|
| 37 |
+
|
| 38 |
+
target_dir_input = gr.Textbox(label='Target Directory Path', placeholder='Enter target directory path (must exist)')
|
| 39 |
+
|
| 40 |
+
template_dir_input = gr.Textbox(label='Template Directory Path', placeholder='Enter template directory path (must exist)')
|
| 41 |
+
|
| 42 |
+
object_file_input = gr.File(label='Object Names File (txt)', file_types=['.txt'])
|
| 43 |
+
|
| 44 |
+
output_text = gr.Textbox(label='Output')
|
| 45 |
+
|
| 46 |
+
gr.Interface(
|
| 47 |
+
fn=process,
|
| 48 |
+
inputs=[phase_dropdown, target_dir_input, template_dir_input, object_file_input],
|
| 49 |
+
outputs=output_text,
|
| 50 |
+
title='Directory and File Creator',
|
| 51 |
+
description='Enter the required information and upload the object names txt file. Ensure the directory paths are valid.'
|
| 52 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|