File size: 2,620 Bytes
184f2d6 06626f0 a996c52 06626f0 5fa3d68 06626f0 5fa3d68 06626f0 184f2d6 06626f0 09d275f 06626f0 184f2d6 06626f0 184f2d6 06626f0 184f2d6 06626f0 5fa3d68 06626f0 5fa3d68 06626f0 5fa3d68 06626f0 |
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 |
import re
import gradio as gr
# Simulating an in-memory virtual filesystem for directories and files
virtual_filesystem = {}
def create_virtual_directory(path):
if path not in virtual_filesystem:
virtual_filesystem[path] = {}
return f"Directory '{path}' created."
return f"Directory '{path}' already exists."
def create_new_templates(objs, templatesDir, regTemplate, root):
result = []
if templatesDir not in virtual_filesystem:
return f"Error: Template directory '{templatesDir}' not found."
template_files = [f for f in virtual_filesystem[templatesDir] if re.search(regTemplate, f, re.IGNORECASE)]
if not template_files:
return f"No templates matching '{regTemplate}' found in {templatesDir}."
for phase in objs:
phase_files = [f for f in template_files if re.search(phase.lower(), f, re.IGNORECASE)]
if not phase_files:
result.append(f"No templates for phase '{phase}' found.")
continue
for obj in objs[phase]:
target_dir = f"{root}/{obj}"
create_virtual_directory(target_dir)
result.append(f"Copied templates for {obj}.")
return "\n".join(result)
# Simulated phase selection
def get_objects_per_phase(phase="All"):
objects = {
"iFAT": ["001ABC", "002DEF"],
"(i)SAT": ["003GHI", "004JKL"],
}
return objects if phase == "All" else {phase: objects.get(phase, [])}
def process_folders_and_templates(root_dir, phase, templates_dir):
try:
# Create directories based on phase objects
objects = get_objects_per_phase(phase)
for obj_list in objects.values():
for obj in obj_list:
create_virtual_directory(f"{root_dir}/{obj}")
# Copy template files
return create_new_templates(objects, templates_dir, r'template', root_dir)
except Exception as e:
return f"Error occurred: {str(e)}"
# Gradio Interface
def gradio_interface(root_dir, phase, templates_dir):
return process_folders_and_templates(root_dir, phase, templates_dir)
# Gradio App
with gr.Blocks() as app:
gr.Markdown("# Folder and Template Processor")
root_dir = gr.Textbox(label="Root Directory")
phase = gr.Dropdown(choices=["iFAT", "(i)SAT", "All"], label="Phase Selection")
templates_dir = gr.Textbox(label="Templates Directory")
output = gr.Textbox(label="Output")
submit_button = gr.Button("Create Folders and Copy Templates")
submit_button.click(gradio_interface, inputs=[root_dir, phase, templates_dir], outputs=output)
app.launch()
|