prettified ui and other minor niceties

This commit is contained in:
Thaddeus Hughes
2025-09-17 22:43:09 -05:00
parent 9fe87ea605
commit 9581579b2b
3 changed files with 41 additions and 50 deletions

View File

@@ -1,4 +1,4 @@
gross_weight,gross_time,tare_weight,tare_time,net GROSS_WEIGHT,GROSS_TIME,TARE_WEIGHT,TARE_TIME,NET
0,06:12 PM 09/17/25,3780,06:13 PM 09/17/25,-3780 0,09/17/2025 06:12 PM,3780,09/17/2025 06:13 PM,-3780
3780,10:18 PM 09/17/25,3640,10:19 PM 09/17/25,140 3780,09/17/2025 10:18 PM,3640,09/17/2025 10:19 PM,140
1640,10:19 PM 09/17/25,0,10:20 PM 09/17/25,1640 1640,09/17/2025 10:19 PM,0,09/17/2025 10:20 PM,1640
1 gross_weight GROSS_WEIGHT gross_time GROSS_TIME tare_weight TARE_WEIGHT tare_time TARE_TIME net NET
2 0 0 06:12 PM 09/17/25 09/17/2025 06:12 PM 3780 3780 06:13 PM 09/17/25 09/17/2025 06:13 PM -3780 -3780
3 3780 3780 10:18 PM 09/17/25 09/17/2025 10:18 PM 3640 3640 10:19 PM 09/17/25 09/17/2025 10:19 PM 140 140
4 1640 1640 10:19 PM 09/17/25 09/17/2025 10:19 PM 0 0 10:20 PM 09/17/25 09/17/2025 10:20 PM 1640 1640

View File

@@ -1,7 +1,7 @@
weight,timestamp WEIGHT,TIMESTAMP
0,06:12 PM 09/17/25 0,09/17/2025 06:12 PM
3780,06:13 PM 09/17/25 3780,09/17/2025 06:13 PM
3780,10:18 PM 09/17/25 3780,09/17/2025 10:18 PM
3640,10:19 PM 09/17/25 3640,09/17/2025 10:19 PM
1640,10:19 PM 09/17/25 1640,09/17/2025 10:19 PM
0,10:20 PM 09/17/25 0,09/17/2025 10:20 PM
1 weight WEIGHT timestamp TIMESTAMP
2 0 0 06:12 PM 09/17/25 09/17/2025 06:12 PM
3 3780 3780 06:13 PM 09/17/25 09/17/2025 06:13 PM
4 3780 3780 10:18 PM 09/17/25 09/17/2025 10:18 PM
5 3640 3640 10:19 PM 09/17/25 09/17/2025 10:19 PM
6 1640 1640 10:19 PM 09/17/25 09/17/2025 10:19 PM
7 0 0 10:20 PM 09/17/25 09/17/2025 10:20 PM

69
main.py
View File

@@ -3,6 +3,7 @@ import csv
import tkinter as tk import tkinter as tk
from tkinter import filedialog, messagebox from tkinter import filedialog, messagebox
import os import os
from datetime import datetime
def parse_logs(log_text): def parse_logs(log_text):
lines = log_text.splitlines() lines = log_text.splitlines()
@@ -20,12 +21,19 @@ def parse_logs(log_text):
match = re.match(r'^GROSS\s+(\d+)\s*lb$', line) match = re.match(r'^GROSS\s+(\d+)\s*lb$', line)
if match: if match:
weight = int(match.group(1)) weight = int(match.group(1))
i += 2 i += 2 # Skip to timestamp line
if i < len(lines): if i < len(lines):
ts_line = lines[i].strip() ts_line = lines[i].strip()
if re.match(r'^\d{2}:\d{2} (AM|PM) \d{2}/\d{2}/\d{2}$', ts_line): if re.match(r'^\d{2}:\d{2} (AM|PM) \d{2}/\d{2}/\d{2}$', ts_line):
pairs.append((weight, ts_line)) # Convert timestamp to Excel-friendly 12-hour format
i += 1 try:
dt = datetime.strptime(ts_line, '%I:%M %p %m/%d/%y')
excel_ts = dt.strftime('%m/%d/%Y %I:%M %p')
pairs.append((weight, excel_ts))
except ValueError:
i += 1
continue
i += 1 # Move to next weight entry
else: else:
i += 1 i += 1
else: else:
@@ -36,14 +44,14 @@ def parse_logs(log_text):
def write_csv1(pairs, filename): def write_csv1(pairs, filename):
with open(filename, 'w', newline='') as f: with open(filename, 'w', newline='') as f:
writer = csv.writer(f) writer = csv.writer(f)
writer.writerow(['weight', 'timestamp']) writer.writerow(['WEIGHT', 'TIMESTAMP'])
for weight, timestamp in pairs: for weight, timestamp in pairs:
writer.writerow([weight, timestamp]) writer.writerow([weight, timestamp])
def write_csv2(pairs, filename): def write_csv2(pairs, filename):
with open(filename, 'w', newline='') as f: with open(filename, 'w', newline='') as f:
writer = csv.writer(f) writer = csv.writer(f)
writer.writerow(['gross_weight', 'gross_time', 'tare_weight', 'tare_time', 'net']) writer.writerow(['GROSS_WEIGHT', 'GROSS_TIME', 'TARE_WEIGHT', 'TARE_TIME', 'NET'])
for j in range(0, len(pairs), 2): for j in range(0, len(pairs), 2):
if j + 1 < len(pairs): if j + 1 < len(pairs):
gross_weight, gross_time = pairs[j] gross_weight, gross_time = pairs[j]
@@ -55,60 +63,43 @@ def select_input_file():
filename = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) filename = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
if filename: if filename:
input_file_var.set(filename) input_file_var.set(filename)
# Auto-populate output file names
base = os.path.splitext(filename)[0]
output_file1_var.set(f"{base}.sequential.csv")
output_file2_var.set(f"{base}.joined.csv")
def select_output_file1():
filename = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV files", "*.csv"), ("All files", "*.*")])
if filename:
output_file1_var.set(filename)
def select_output_file2():
filename = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV files", "*.csv"), ("All files", "*.*")])
if filename:
output_file2_var.set(filename)
def process_files(): def process_files():
input_file = input_file_var.get() input_file = input_file_var.get()
output_file1 = output_file1_var.get()
output_file2 = output_file2_var.get()
if not input_file or not output_file1 or not output_file2: if not input_file:
messagebox.showerror("Error", "Please select all input and output files.") messagebox.showerror("Error", "Please select an input file.")
return return
# Auto-generate output filenames
base = os.path.splitext(input_file)[0]
output_file1 = f"{base}.sequential.csv"
output_file2 = f"{base}.joined.csv"
try: try:
with open(input_file, 'r') as f: with open(input_file, 'r') as f:
log_text = f.read() log_text = f.read()
pairs = parse_logs(log_text) pairs = parse_logs(log_text)
write_csv1(pairs, output_file1) write_csv1(pairs, output_file1)
write_csv2(pairs, output_file2) write_csv2(pairs, output_file2)
messagebox.showinfo("Success", "CSV files generated successfully!") messagebox.showinfo("Success", f"CSV files generated:\n{output_file1}\n{output_file2}")
except Exception as e: except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}") messagebox.showerror("Error", f"An error occurred: {str(e)}")
root = tk.Tk() root = tk.Tk()
root.title("Log to CSV Converter") root.title("Log to CSV Converter")
root.geometry("500x500") root.geometry("500x120")
input_file_var = tk.StringVar() input_file_var = tk.StringVar()
output_file1_var = tk.StringVar()
output_file2_var = tk.StringVar()
tk.Label(root, text="Select Input Log File:").pack(pady=5) # Input file selection
tk.Entry(root, textvariable=input_file_var, width=50).pack(pady=5) input_frame = tk.Frame(root)
tk.Button(root, text="Browse Input", command=select_input_file).pack(pady=5) input_frame.pack(pady=5, padx=10, fill='x')
tk.Label(input_frame, text="Select Input Log File:").pack(side='left')
tk.Entry(input_frame, textvariable=input_file_var, width=40).pack(side='left', padx=5)
tk.Button(input_frame, text="Browse Input", command=select_input_file).pack(side='left')
tk.Label(root, text="Select Output CSV (Sequential):").pack(pady=5) # Convert button
tk.Entry(root, textvariable=output_file1_var, width=50).pack(pady=5) tk.Button(root, text="CONVERT TO CSV", command=process_files, font=("Arial", 14), relief="raised", bg="#4CAF50", fg="white").pack(pady=20, padx=10, fill='x')
tk.Button(root, text="Browse Sequential Output", command=select_output_file1).pack(pady=5)
tk.Label(root, text="Select Output CSV (Joined):").pack(pady=5)
tk.Entry(root, textvariable=output_file2_var, width=50).pack(pady=5)
tk.Button(root, text="Browse Joined Output", command=select_output_file2).pack(pady=5)
tk.Button(root, text="Convert to CSV", command=process_files).pack(pady=20)
root.mainloop() root.mainloop()