dunlp commited on
Commit
06626f0
1 Parent(s): 09d275f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -76
app.py CHANGED
@@ -1,88 +1,73 @@
1
- import gradio as gr
2
- import zipfile
3
- import io
4
  import re
 
5
 
6
- def process_files(phase, objecten_file_dict, template_files_data):
7
- try:
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
- # Extract object codes
29
- for k in objs:
30
- objs[k] = [re.search(regObject, o).group(0) for o in objs[k] if re.search(regObject, o)]
 
 
31
 
32
- # Create an in-memory ZIP file
33
- zip_buffer = io.BytesIO()
34
- with zipfile.ZipFile(zip_buffer, 'w') as zf:
35
- for k in objs:
36
- regPhase = ''
37
- if k == '(i)SAT':
38
- regPhase = r'sat'
39
- elif k == 'iFAT':
40
- regPhase = r'fat'
41
 
42
- # Filter template files for this phase
43
- phase_templates = []
44
- for file_dict in template_files_data:
45
- filename = file_dict['name']
46
- if re.search(regPhase, filename, re.IGNORECASE):
47
- phase_templates.append(file_dict)
 
 
 
48
 
49
- if not phase_templates:
50
- error_msg = f"Phase {k} has no templates."
51
- return None, error_msg
 
 
 
52
 
53
- for o in objs[k]:
54
- folder_path = f"{o}/"
55
- for file_dict in phase_templates:
56
- template_filename = file_dict['name']
57
- # Adjust filename if needed
58
- if re.search(r"hut_\d{4}[a-zA-Z]{2}", template_filename, re.IGNORECASE):
59
- adjusted_filename = template_filename[:4] + o + template_filename[10:]
60
- else:
61
- adjusted_filename = template_filename
62
 
63
- file_content = file_dict['data']
64
- file_path = folder_path + adjusted_filename
65
- zf.writestr(file_path, file_content)
66
- zip_buffer.seek(0)
67
- return zip_buffer, None
 
 
 
 
 
68
  except Exception as e:
69
- return None, str(e)
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- phase_options = ['iFAT', '(i)SAT', 'All']
72
 
73
- interface = gr.Interface(
74
- fn=process_files,
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
- interface.launch()
 
 
 
 
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()