testfiles / app.py
dunlp's picture
Update app.py
06626f0 verified
raw
history blame
2.62 kB
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()