Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ import zipfile
|
|
| 3 |
import io
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
def process_files(phase,
|
| 7 |
try:
|
| 8 |
# Read and process the 'objecten.txt' file
|
| 9 |
-
content =
|
| 10 |
t = content.strip().split('\n\n')
|
| 11 |
phases = ["iFAT", "(i)SAT"]
|
| 12 |
objs = {p: [] for p in phases}
|
|
@@ -40,23 +40,27 @@ def process_files(phase, objecten_file, template_files):
|
|
| 40 |
regPhase = r'fat'
|
| 41 |
|
| 42 |
# Filter template files for this phase
|
| 43 |
-
phase_templates = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if not phase_templates:
|
| 45 |
error_msg = f"Phase {k} has no templates."
|
| 46 |
return None, error_msg
|
| 47 |
|
| 48 |
for o in objs[k]:
|
| 49 |
folder_path = f"{o}/"
|
| 50 |
-
for
|
| 51 |
-
template_filename =
|
| 52 |
# Adjust filename if needed
|
| 53 |
if re.search(r"hut_\d{4}[a-zA-Z]{2}", template_filename, re.IGNORECASE):
|
| 54 |
adjusted_filename = template_filename[:4] + o + template_filename[10:]
|
| 55 |
else:
|
| 56 |
adjusted_filename = template_filename
|
| 57 |
|
| 58 |
-
file_content =
|
| 59 |
-
f.seek(0) # Reset file pointer
|
| 60 |
file_path = folder_path + adjusted_filename
|
| 61 |
zf.writestr(file_path, file_content)
|
| 62 |
zip_buffer.seek(0)
|
|
@@ -70,8 +74,8 @@ interface = gr.Interface(
|
|
| 70 |
fn=process_files,
|
| 71 |
inputs=[
|
| 72 |
gr.Dropdown(choices=phase_options, label="Select Phase"),
|
| 73 |
-
gr.File(label="Upload 'objecten.txt' File", type='
|
| 74 |
-
gr.File(label="Upload Template Files", type='
|
| 75 |
],
|
| 76 |
outputs=[
|
| 77 |
gr.File(label="Download ZIP File"),
|
|
|
|
| 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}
|
|
|
|
| 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)
|
|
|
|
| 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"),
|