dunlp commited on
Commit
8609612
1 Parent(s): 1130bd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -4,7 +4,7 @@ import io
4
  import re
5
  import traceback
6
 
7
- def process_files(phase, objecten_file_dict, template_files_data):
8
  try:
9
  # Read and process the 'objecten.txt' file
10
  content = objecten_file_dict['data'].decode('utf-8')
@@ -30,9 +30,14 @@ def process_files(phase, objecten_file_dict, template_files_data):
30
  for k in objs:
31
  objs[k] = [re.search(regObject, o).group(0) for o in objs[k] if re.search(regObject, o)]
32
 
33
- # Create an in-memory ZIP file
34
- zip_buffer = io.BytesIO()
35
- with zipfile.ZipFile(zip_buffer, 'w') as zf:
 
 
 
 
 
36
  for k in objs:
37
  regPhase = ''
38
  if k == '(i)SAT':
@@ -42,10 +47,9 @@ def process_files(phase, objecten_file_dict, template_files_data):
42
 
43
  # Filter template files for this phase
44
  phase_templates = []
45
- for file_dict in template_files_data:
46
- filename = file_dict['name']
47
  if re.search(regPhase, filename, re.IGNORECASE):
48
- phase_templates.append(file_dict)
49
 
50
  if not phase_templates:
51
  error_msg = f"Phase {k} has no templates."
@@ -53,19 +57,17 @@ def process_files(phase, objecten_file_dict, template_files_data):
53
 
54
  for o in objs[k]:
55
  folder_path = f"{o}/"
56
- for file_dict in phase_templates:
57
- template_filename = file_dict['name']
58
  # Adjust filename if needed
59
  if re.search(r"hut_\d{4}[a-zA-Z]{2}", template_filename, re.IGNORECASE):
60
  adjusted_filename = template_filename[:4] + o + template_filename[10:]
61
  else:
62
  adjusted_filename = template_filename
63
 
64
- file_content = file_dict['data']
65
  file_path = folder_path + adjusted_filename
66
  zf.writestr(file_path, file_content)
67
- zip_buffer.seek(0)
68
- return zip_buffer, None
69
  except Exception as e:
70
  # Capture the full traceback
71
  error_msg = traceback.format_exc()
@@ -78,14 +80,14 @@ interface = gr.Interface(
78
  inputs=[
79
  gr.Dropdown(choices=phase_options, label="Select Phase"),
80
  gr.File(label="Upload 'objecten.txt' File", type='binary'),
81
- gr.File(label="Upload Template Files", type='binary', multiple=True)
82
  ],
83
  outputs=[
84
  gr.File(label="Download ZIP File"),
85
- gr.Textbox(label="Error Log", lines=15)
86
  ],
87
  title="Template Folder Generator",
88
- description="Upload 'objecten.txt' and template files to generate folders and files."
89
  )
90
 
91
  interface.launch()
 
4
  import re
5
  import traceback
6
 
7
+ def process_files(phase, objecten_file_dict, templates_zip_file_dict):
8
  try:
9
  # Read and process the 'objecten.txt' file
10
  content = objecten_file_dict['data'].decode('utf-8')
 
30
  for k in objs:
31
  objs[k] = [re.search(regObject, o).group(0) for o in objs[k] if re.search(regObject, o)]
32
 
33
+ # Read template files from the uploaded ZIP file
34
+ templates_zip_data = templates_zip_file_dict['data']
35
+ with zipfile.ZipFile(io.BytesIO(templates_zip_data), 'r') as zip_ref:
36
+ template_files = {info.filename: zip_ref.read(info.filename) for info in zip_ref.infolist()}
37
+
38
+ # Create an in-memory ZIP file for the output
39
+ output_zip_buffer = io.BytesIO()
40
+ with zipfile.ZipFile(output_zip_buffer, 'w') as zf:
41
  for k in objs:
42
  regPhase = ''
43
  if k == '(i)SAT':
 
47
 
48
  # Filter template files for this phase
49
  phase_templates = []
50
+ for filename, file_data in template_files.items():
 
51
  if re.search(regPhase, filename, re.IGNORECASE):
52
+ phase_templates.append((filename, file_data))
53
 
54
  if not phase_templates:
55
  error_msg = f"Phase {k} has no templates."
 
57
 
58
  for o in objs[k]:
59
  folder_path = f"{o}/"
60
+ for template_filename, file_content in phase_templates:
 
61
  # Adjust filename if needed
62
  if re.search(r"hut_\d{4}[a-zA-Z]{2}", template_filename, re.IGNORECASE):
63
  adjusted_filename = template_filename[:4] + o + template_filename[10:]
64
  else:
65
  adjusted_filename = template_filename
66
 
 
67
  file_path = folder_path + adjusted_filename
68
  zf.writestr(file_path, file_content)
69
+ output_zip_buffer.seek(0)
70
+ return output_zip_buffer, None
71
  except Exception as e:
72
  # Capture the full traceback
73
  error_msg = traceback.format_exc()
 
80
  inputs=[
81
  gr.Dropdown(choices=phase_options, label="Select Phase"),
82
  gr.File(label="Upload 'objecten.txt' File", type='binary'),
83
+ gr.File(label="Upload Templates ZIP File", type='binary')
84
  ],
85
  outputs=[
86
  gr.File(label="Download ZIP File"),
87
+ gr.Code(label="Error Log", language="python")
88
  ],
89
  title="Template Folder Generator",
90
+ description="Upload 'objecten.txt' and a ZIP file containing template files to generate folders and files."
91
  )
92
 
93
  interface.launch()