Spaces:
Sleeping
Sleeping
Gnanashri Kota Venkatesh commited on
Refactor file input handling and brand analysis
Browse filesRefactor file processing logic to handle flat structures by moving items to a dedicated input subfolder. Simplify brand analysis handling within the button action.
tab5.py
CHANGED
|
@@ -38,15 +38,29 @@ def brand_renamer_tool():
|
|
| 38 |
if not st.session_state.processing_complete:
|
| 39 |
with st.spinner("Extracting and analyzing files..."):
|
| 40 |
temp_dir = extract_zip_to_temp(uploaded_file)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
items = os.listdir(temp_dir)
|
|
|
|
|
|
|
| 44 |
if len(items) == 1 and os.path.isdir(os.path.join(temp_dir, items[0])):
|
|
|
|
| 45 |
input_folder = Path(temp_dir) / items[0]
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
output_folder = Path(temp_dir) / "output"
|
| 48 |
output_folder.mkdir(exist_ok=True)
|
| 49 |
-
|
| 50 |
marken_set, _, _ = analyze_files_by_filename(input_folder)
|
| 51 |
|
| 52 |
if not marken_set:
|
|
@@ -63,18 +77,14 @@ def brand_renamer_tool():
|
|
| 63 |
index=0
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if st.button("Process Files and Generate Reports", type="primary"):
|
| 67 |
-
# Re-analyze to ensure we have ALL brands before processing
|
| 68 |
-
marken_set, _, _ = analyze_files_by_filename(input_folder)
|
| 69 |
-
|
| 70 |
-
# CREATE marken_index HERE, inside the button handler with ALL found brands
|
| 71 |
-
marken_index = {erste_marke: "01"}
|
| 72 |
-
aktuelle_nummer = 2
|
| 73 |
-
for marke in sorted(marken_set):
|
| 74 |
-
if marke != erste_marke:
|
| 75 |
-
marken_index[marke] = f"{aktuelle_nummer:02d}"
|
| 76 |
-
aktuelle_nummer += 1
|
| 77 |
-
|
| 78 |
with st.spinner("Processing files..."):
|
| 79 |
# Process and rename files
|
| 80 |
renamed_files, file_to_factorgroup = process_files(
|
|
|
|
| 38 |
if not st.session_state.processing_complete:
|
| 39 |
with st.spinner("Extracting and analyzing files..."):
|
| 40 |
temp_dir = extract_zip_to_temp(uploaded_file)
|
| 41 |
+
|
| 42 |
+
# Determine the actual input folder
|
| 43 |
+
items = [item for item in os.listdir(temp_dir) if not item.startswith('.')]
|
| 44 |
+
|
| 45 |
+
# ✅ ROOT CAUSE FIX: If flat structure, create a dedicated input subfolder
|
| 46 |
if len(items) == 1 and os.path.isdir(os.path.join(temp_dir, items[0])):
|
| 47 |
+
# Single root folder - use it as input
|
| 48 |
input_folder = Path(temp_dir) / items[0]
|
| 49 |
+
else:
|
| 50 |
+
# Flat structure - move everything to an input subfolder
|
| 51 |
+
input_folder = Path(temp_dir) / "input"
|
| 52 |
+
input_folder.mkdir(exist_ok=True)
|
| 53 |
+
|
| 54 |
+
# Move all items to input folder
|
| 55 |
+
for item in items:
|
| 56 |
+
src = Path(temp_dir) / item
|
| 57 |
+
dst = input_folder / item
|
| 58 |
+
shutil.move(str(src), str(dst))
|
| 59 |
+
|
| 60 |
+
# Now create output folder at temp_dir level (guaranteed to be separate)
|
| 61 |
output_folder = Path(temp_dir) / "output"
|
| 62 |
output_folder.mkdir(exist_ok=True)
|
| 63 |
+
|
| 64 |
marken_set, _, _ = analyze_files_by_filename(input_folder)
|
| 65 |
|
| 66 |
if not marken_set:
|
|
|
|
| 77 |
index=0
|
| 78 |
)
|
| 79 |
|
| 80 |
+
marken_index = {erste_marke: "01"}
|
| 81 |
+
aktuelle_nummer = 2
|
| 82 |
+
for marke in sorted(marken_set):
|
| 83 |
+
if marke != erste_marke:
|
| 84 |
+
marken_index[marke] = f"{aktuelle_nummer:02d}"
|
| 85 |
+
aktuelle_nummer += 1
|
| 86 |
+
|
| 87 |
if st.button("Process Files and Generate Reports", type="primary"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with st.spinner("Processing files..."):
|
| 89 |
# Process and rename files
|
| 90 |
renamed_files, file_to_factorgroup = process_files(
|