-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
235 lines (193 loc) · 9.08 KB
/
app.py
File metadata and controls
235 lines (193 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import tkinter as tk
import os
from datetime import datetime
from tkinter import messagebox
from utils import (
select_single_file,
select_multiple_files,
select_folder,
get_csv_files_from_folder,
CSVProcessor
)
# Get current year-month format
current_ym = datetime.now().strftime("%Y-%m")
class CSVProcessorApp:
def __init__(self, root):
self.root = root
self.root.title("EasyNodePro.com CSV Processor")
self.root.geometry("600x500")
# File path variables
self.selected_files = []
self.mapping_path = tk.StringVar()
self.selection_mode = tk.StringVar(value="folder")
self.process_folder_path = ""
# Initialize processor
self.processor = CSVProcessor()
# Create UI elements
self.create_ui()
# Auto-detect mapping file
self.auto_detect_mapping()
# Auto-load process folder
self.auto_load_process_folder()
def create_ui(self):
"""Create the user interface"""
# Selection mode frame
mode_frame = tk.Frame(self.root)
mode_frame.pack(pady=10)
tk.Label(mode_frame, text="Selection Mode:", font=('Arial', 12, 'bold')).pack()
# Radio buttons for selection mode
tk.Radiobutton(mode_frame, text="Process Folder (Default)",
variable=self.selection_mode, value="folder",
command=self.clear_selection).pack(anchor='w')
tk.Radiobutton(mode_frame, text="Single File",
variable=self.selection_mode, value="single",
command=self.clear_selection).pack(anchor='w')
tk.Radiobutton(mode_frame, text="Multiple Files",
variable=self.selection_mode, value="multiple",
command=self.clear_selection).pack(anchor='w')
# File selection frame
file_frame = tk.Frame(self.root)
file_frame.pack(pady=10, fill='x', padx=20)
tk.Label(file_frame, text="Selected Files:", font=('Arial', 12)).pack(anchor='w')
# Scrollable text area for selected files
self.file_text = tk.Text(file_frame, height=6, width=70,
wrap=tk.WORD, state='disabled')
scrollbar = tk.Scrollbar(file_frame, orient="vertical", command=self.file_text.yview)
self.file_text.configure(yscrollcommand=scrollbar.set)
self.file_text.pack(side='left', fill='both', expand=True)
scrollbar.pack(side='right', fill='y')
# Selection button
tk.Button(self.root, text="Select Files",
command=self.select_files, width=20, height=2,
bg="lightgreen").pack(pady=10)
# Mapping file section
mapping_frame = tk.Frame(self.root)
mapping_frame.pack(pady=10, fill='x', padx=20)
tk.Label(mapping_frame, text="Find/Replace Mapping File:", font=('Arial', 12)).pack(anchor='w')
tk.Entry(mapping_frame, textvariable=self.mapping_path, width=60).pack(fill='x', pady=5)
tk.Button(mapping_frame, text="Select Mapping File",
command=self.select_mapping_file, width=20).pack()
# Process button
tk.Button(self.root, text="Process CSV Files", command=self.process_csv,
width=20, height=3, bg="lightblue", font=('Arial', 12, 'bold')).pack(pady=20)
# Status label
self.status_label = tk.Label(self.root, text="", fg="green", font=('Arial', 10))
self.status_label.pack(pady=5)
def auto_detect_mapping(self):
"""Check for mapping.csv in the same directory as app.py"""
script_dir = os.path.dirname(os.path.abspath(__file__))
mapping_path = os.path.join(script_dir, 'mapping.csv')
if os.path.exists(mapping_path):
self.mapping_path.set(mapping_path)
self.status_label.config(text="Auto-detected mapping.csv in same folder as app.py")
else:
# Check for example file and suggest copying it
example_path = os.path.join(script_dir, 'mapping.csv.example')
if os.path.exists(example_path):
self.status_label.config(
text="No mapping.csv found. Copy mapping.csv.example to mapping.csv to get started.",
fg="orange"
)
def auto_load_process_folder(self):
"""Auto-load the process folder if it exists"""
script_dir = os.path.dirname(os.path.abspath(__file__))
process_folder = os.path.join(script_dir, 'process')
if os.path.exists(process_folder) and os.path.isdir(process_folder):
self.process_folder_path = process_folder
# Get all CSV files from folder (including extracted from ZIPs)
csv_files = get_csv_files_from_folder(process_folder, extract_zips=True)
if csv_files:
self.selected_files = csv_files
self.update_file_display()
self.status_label.config(
text=f"Auto-loaded {len(csv_files)} file(s) from process folder",
fg="green"
)
else:
self.status_label.config(
text="Process folder exists but no CSV or ZIP files found",
fg="orange"
)
def clear_selection(self):
"""Clear the current file selection"""
self.selected_files = []
self.update_file_display()
def update_file_display(self):
"""Update the display of selected files"""
self.file_text.config(state='normal')
self.file_text.delete('1.0', tk.END)
if not self.selected_files:
self.file_text.insert('1.0', "No files selected")
else:
for i, file_path in enumerate(self.selected_files, 1):
self.file_text.insert(tk.END, f"{i}. {file_path}\n")
self.file_text.config(state='disabled')
def select_files(self):
"""Handle file selection based on mode"""
mode = self.selection_mode.get()
if mode == "folder":
folder_path = select_folder()
if folder_path:
self.process_folder_path = folder_path
# Get all CSV files from folder (including extracted from ZIPs)
csv_files = get_csv_files_from_folder(folder_path, extract_zips=True)
if csv_files:
self.selected_files = csv_files
else:
messagebox.showwarning("Warning", "No CSV or ZIP files found in the selected folder.")
self.selected_files = []
elif mode == "single":
file_path = select_single_file()
if file_path:
self.selected_files = [file_path]
elif mode == "multiple":
file_paths = select_multiple_files()
if file_paths:
self.selected_files = list(file_paths)
self.update_file_display()
def select_mapping_file(self):
"""Select the mapping file"""
file_path = select_single_file()
if file_path:
self.mapping_path.set(file_path)
def process_csv(self):
"""Process the selected CSV files individually"""
if not self.selected_files:
messagebox.showerror("Error", "Please select CSV file(s) to process.")
return
mapping_path = self.mapping_path.get()
# Show processing status
self.status_label.config(text="Processing files individually...", fg="orange")
self.root.update()
try:
# Process files individually (each file gets its own output folder)
success, message, stats = self.processor.process_files_individually(
self.selected_files,
mapping_path,
current_ym
)
if success:
# Update status
self.status_label.config(text=message, fg="green")
# Show detailed success message with all output directories
output_dirs_text = "\n".join([f" - {d}" for d in stats['output_dirs']])
detail_msg = (
f"Processing Complete!\n\n"
f"Files processed: {stats['num_files_processed']}\n"
f"Total records: {stats['total_records']}\n"
f"Total chunks: {stats['num_chunks']}\n"
f"Replacements applied: {stats['replacements_applied']}\n\n"
f"Output directories:\n{output_dirs_text}"
)
messagebox.showinfo("Success", detail_msg)
else:
self.status_label.config(text="Processing completed with errors", fg="red")
messagebox.showerror("Error", message)
except Exception as e:
error_msg = f"An unexpected error occurred: {str(e)}"
self.status_label.config(text=error_msg, fg="red")
messagebox.showerror("Error", error_msg)
if __name__ == "__main__":
root = tk.Tk()
app = CSVProcessorApp(root)
root.mainloop()