Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,73 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import zipfile
|
| 3 |
-
import io
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Read and process the 'objecten.txt' file
|
| 9 |
-
content = objecten_file_dict['data'].decode('utf-8')
|
| 10 |
-
t = content.strip().split('\n\n')
|
| 11 |
-
phases = ["iFAT", "(i)SAT"]
|
| 12 |
-
objs = {p: [] for p in phases}
|
| 13 |
-
if phase in phases:
|
| 14 |
-
objs = {phase: []}
|
| 15 |
-
else:
|
| 16 |
-
objs = {p: [] for p in phases}
|
| 17 |
-
|
| 18 |
-
regObject = r"\d{4}[a-zA-Z]{2}"
|
| 19 |
-
for g in t:
|
| 20 |
-
ls = g.strip().split('\n')
|
| 21 |
-
k = ls[0]
|
| 22 |
-
if k in objs:
|
| 23 |
-
objs[k] = ls[1:]
|
| 24 |
-
else:
|
| 25 |
-
error_msg = f"Key [{k}] is not recognized."
|
| 26 |
-
return None, error_msg
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
regPhase = ''
|
| 37 |
-
if k == '(i)SAT':
|
| 38 |
-
regPhase = r'sat'
|
| 39 |
-
elif k == 'iFAT':
|
| 40 |
-
regPhase = r'fat'
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
else:
|
| 61 |
-
adjusted_filename = template_filename
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
inputs=[
|
| 76 |
-
gr.Dropdown(choices=phase_options, label="Select Phase"),
|
| 77 |
-
gr.File(label="Upload 'objecten.txt' File", type='binary'),
|
| 78 |
-
gr.File(label="Upload Template Files", type='binary', multiple=True)
|
| 79 |
-
],
|
| 80 |
-
outputs=[
|
| 81 |
-
gr.File(label="Download ZIP File"),
|
| 82 |
-
gr.Textbox(label="Error Message", lines=2)
|
| 83 |
-
],
|
| 84 |
-
title="Template Folder Generator",
|
| 85 |
-
description="Upload 'objecten.txt' and template files to generate folders and files."
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
+
# Simulating an in-memory virtual filesystem for directories and files
|
| 5 |
+
virtual_filesystem = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def create_virtual_directory(path):
|
| 8 |
+
if path not in virtual_filesystem:
|
| 9 |
+
virtual_filesystem[path] = {}
|
| 10 |
+
return f"Directory '{path}' created."
|
| 11 |
+
return f"Directory '{path}' already exists."
|
| 12 |
|
| 13 |
+
def create_new_templates(objs, templatesDir, regTemplate, root):
|
| 14 |
+
result = []
|
| 15 |
+
if templatesDir not in virtual_filesystem:
|
| 16 |
+
return f"Error: Template directory '{templatesDir}' not found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
template_files = [f for f in virtual_filesystem[templatesDir] if re.search(regTemplate, f, re.IGNORECASE)]
|
| 19 |
+
if not template_files:
|
| 20 |
+
return f"No templates matching '{regTemplate}' found in {templatesDir}."
|
| 21 |
+
|
| 22 |
+
for phase in objs:
|
| 23 |
+
phase_files = [f for f in template_files if re.search(phase.lower(), f, re.IGNORECASE)]
|
| 24 |
+
if not phase_files:
|
| 25 |
+
result.append(f"No templates for phase '{phase}' found.")
|
| 26 |
+
continue
|
| 27 |
|
| 28 |
+
for obj in objs[phase]:
|
| 29 |
+
target_dir = f"{root}/{obj}"
|
| 30 |
+
create_virtual_directory(target_dir)
|
| 31 |
+
result.append(f"Copied templates for {obj}.")
|
| 32 |
+
|
| 33 |
+
return "\n".join(result)
|
| 34 |
|
| 35 |
+
# Simulated phase selection
|
| 36 |
+
def get_objects_per_phase(phase="All"):
|
| 37 |
+
objects = {
|
| 38 |
+
"iFAT": ["001ABC", "002DEF"],
|
| 39 |
+
"(i)SAT": ["003GHI", "004JKL"],
|
| 40 |
+
}
|
| 41 |
+
return objects if phase == "All" else {phase: objects.get(phase, [])}
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def process_folders_and_templates(root_dir, phase, templates_dir):
|
| 44 |
+
try:
|
| 45 |
+
# Create directories based on phase objects
|
| 46 |
+
objects = get_objects_per_phase(phase)
|
| 47 |
+
for obj_list in objects.values():
|
| 48 |
+
for obj in obj_list:
|
| 49 |
+
create_virtual_directory(f"{root_dir}/{obj}")
|
| 50 |
+
|
| 51 |
+
# Copy template files
|
| 52 |
+
return create_new_templates(objects, templates_dir, r'template', root_dir)
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"Error occurred: {str(e)}"
|
| 55 |
+
|
| 56 |
+
# Gradio Interface
|
| 57 |
+
def gradio_interface(root_dir, phase, templates_dir):
|
| 58 |
+
return process_folders_and_templates(root_dir, phase, templates_dir)
|
| 59 |
+
|
| 60 |
+
# Gradio App
|
| 61 |
+
with gr.Blocks() as app:
|
| 62 |
+
gr.Markdown("# Folder and Template Processor")
|
| 63 |
+
|
| 64 |
+
root_dir = gr.Textbox(label="Root Directory")
|
| 65 |
+
phase = gr.Dropdown(choices=["iFAT", "(i)SAT", "All"], label="Phase Selection")
|
| 66 |
+
templates_dir = gr.Textbox(label="Templates Directory")
|
| 67 |
|
| 68 |
+
output = gr.Textbox(label="Output")
|
| 69 |
|
| 70 |
+
submit_button = gr.Button("Create Folders and Copy Templates")
|
| 71 |
+
submit_button.click(gradio_interface, inputs=[root_dir, phase, templates_dir], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
app.launch()
|