-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_utils.py
More file actions
371 lines (344 loc) · 17.3 KB
/
export_utils.py
File metadata and controls
371 lines (344 loc) · 17.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import sys
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from utils.config import get_logger
import json
from datetime import datetime
from PIL import Image as PILImage
import matplotlib.pyplot as plt
logger = get_logger("export_utils")
EXPORT_UTILS_VERSION = "2025-03-30_v5"
# Determine the base directory for bundled resources
if getattr(sys, 'frozen', False):
BASE_DIR = sys._MEIPASS
else:
BASE_DIR = r"C:\Users\travism\source\repos\GuthPumpRegistry"
# Define config paths
if getattr(sys, 'frozen', False):
# Use AppData for persistent config in installed app
CONFIG_DIR = os.path.join(os.getenv('APPDATA'), "GuthPumpRegistry")
os.makedirs(CONFIG_DIR, exist_ok=True)
CONFIG_PATH = os.path.join(CONFIG_DIR, "config.json")
DEFAULT_CONFIG_PATH = os.path.join(BASE_DIR, "config.json")
else:
CONFIG_PATH = os.path.join(BASE_DIR, "config.json")
DEFAULT_CONFIG_PATH = CONFIG_PATH
LOGO_PATH = os.path.join(BASE_DIR, "assets", "logo.png")
def load_config():
"""Load configuration from config.json, creating it with defaults if missing."""
# Check user-specific config first (writable location)
if os.path.exists(CONFIG_PATH):
try:
with open(CONFIG_PATH, "r") as f:
config = json.load(f)
logger.info(f"Loaded user config from {CONFIG_PATH}")
except Exception as e:
logger.error(f"Failed to load user config from {CONFIG_PATH}: {str(e)}")
config = {}
else:
# Fall back to bundled default config
if os.path.exists(DEFAULT_CONFIG_PATH):
try:
with open(DEFAULT_CONFIG_PATH, "r") as f:
config = json.load(f)
logger.info(f"Loaded default config from {DEFAULT_CONFIG_PATH}")
except Exception as e:
logger.error(f"Failed to load default config from {DEFAULT_CONFIG_PATH}: {str(e)}")
config = {}
else:
config = {}
logger.warning(f"No config found at {CONFIG_PATH} or {DEFAULT_CONFIG_PATH}")
# Ensure defaults are applied for required keys
config.setdefault("document_dirs", {
"certificate": os.path.join(BASE_DIR, "certificates")
})
config.setdefault("email_settings", {
"smtp_host": "smtp.gmail.com",
"smtp_port": "587",
"smtp_username": "",
"smtp_password": "",
"sender_email": "",
"use_tls": True
})
# If user config didn’t exist, create it with defaults
if not os.path.exists(CONFIG_PATH):
try:
with open(CONFIG_PATH, "w") as f:
json.dump(config, f, indent=4)
logger.info(f"Created default config file at {CONFIG_PATH}")
except Exception as e:
logger.error(f"Failed to create default config at {CONFIG_PATH}: {str(e)}")
return config
def generate_test_graph(test_data, output_path="temp_graph.png"):
"""Generate a graph of test data (amperage and pressure)."""
try:
plt.figure(figsize=(6, 3))
# Filter out empty entries and convert to float, using test numbers as x-axis
time = [i + 1 for i, x in enumerate(test_data["amperage"]) if x.strip()]
amperage = [float(x) for x in test_data["amperage"] if x.strip()]
pressure = [float(x) for x in test_data["pressure"] if x.strip()]
if amperage:
plt.plot(time, amperage, label="Amperage (A)", color="blue", marker="o")
if pressure:
plt.plot(time, pressure, label="Pressure (bar)", color="red", marker="o")
plt.xlabel("Test Number")
plt.ylabel("Value")
plt.title("Pump Test Results")
plt.legend()
plt.grid(True)
plt.savefig(output_path, dpi=100, bbox_inches="tight")
plt.close()
logger.info(f"Generated test graph at {output_path}")
return output_path
except Exception as e:
logger.error(f"Failed to generate test graph: {str(e)}")
return None
def send_email(to_email, subject, greeting, body_content, footer="", *attachment_paths):
"""Send an email with multiple optional attachments using SMTP settings from config.json."""
config = load_config()
email_settings = config.get("email_settings", {})
smtp_server = email_settings.get("smtp_host", "")
smtp_port = int(email_settings.get("smtp_port", 587))
sender_email = email_settings.get("sender_email", "")
smtp_username = email_settings.get("smtp_username", "")
smtp_password = email_settings.get("smtp_password", "")
use_tls = email_settings.get("use_tls", True)
if not all([smtp_server, smtp_port, sender_email]):
logger.error("Required email settings (smtp_host, smtp_port, sender_email) not found in config.json")
raise ValueError("Required email settings not set in config.json")
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = to_email
msg['Subject'] = subject
body = f"""
<html>
<body style="font-family: Arial, sans-serif; color: #333; line-height: 1.6;">
<div style="max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #ddd; border-radius: 5px;">
<div style="text-align: center;">
<img src="file://{LOGO_PATH}" alt="Guth Logo" style="max-width: 150px; height: auto;" />
</div>
<h2 style="color: #2c3e50; text-align: center;">{subject}</h2>
<p>{greeting}</p>
{body_content}
<p style="margin-top: 20px;">{footer}</p>
<hr style="border: 0; border-top: 1px solid #ddd; margin: 20px 0;">
<p style="font-size: 12px; color: #777; text-align: center;">© Guth South Africa | Version {EXPORT_UTILS_VERSION}</p>
</div>
</body>
</html>
"""
msg.attach(MIMEText(body, 'html'))
for attachment_path in attachment_paths:
if os.path.exists(attachment_path):
try:
with open(attachment_path, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype="pdf")
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment_path))
msg.attach(attachment)
logger.info(f"Attached file to email: {attachment_path}")
except Exception as e:
logger.error(f"Failed to attach {attachment_path}: {str(e)}")
else:
logger.warning(f"Attachment not found: {attachment_path}")
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
if use_tls:
server.starttls()
if smtp_username and smtp_password:
server.login(smtp_username, smtp_password)
server.sendmail(sender_email, to_email, msg.as_string())
logger.info(f"Email sent to {to_email} with subject '{subject}' and {len(attachment_paths)} attachments")
except Exception as e:
logger.error(f"Failed to send email to {to_email}: {str(e)}")
raise
def generate_pdf_notification(serial_number, data, title="Pump Assembly Notification", output_path=None):
"""Generate a PDF document with pump details, BOM items, and test data graph if applicable."""
config = load_config()
if output_path is None:
output_dir = config["document_dirs"].get("certificate", os.path.join(BASE_DIR, "certificates"))
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"{serial_number}_{title.replace(' ', '_')}.pdf")
doc = SimpleDocTemplate(output_path, pagesize=A4, rightMargin=36, leftMargin=36, topMargin=36, bottomMargin=36)
styles = getSampleStyleSheet()
title_style = styles["Heading1"]
title_style.alignment = 1
heading2_style = styles["Heading2"]
normal_style = styles["Normal"]
cell_style = ParagraphStyle(name="CellStyle", fontSize=10, leading=12, wordWrap="CJK", alignment=0)
elements = []
if os.path.exists(LOGO_PATH):
try:
with PILImage.open(LOGO_PATH) as img:
orig_width, orig_height = img.size
logo_width = min(orig_width * 0.5, 120)
logo_height = min(orig_height * 0.5, 60)
logo = Image(LOGO_PATH, width=logo_width, height=logo_height)
logo.hAlign = 'RIGHT'
elements.append(logo)
elements.append(Spacer(1, 12))
except Exception as e:
logger.warning(f"Failed to load logo for PDF: {str(e)}")
elements.append(Paragraph(title, title_style))
elements.append(Spacer(1, 12))
# Add test data graph if present
if "flowrate" in data and "pressure" in data and "amperage" in data:
test_data = {
"flowrate": data["flowrate"],
"pressure": data["pressure"],
"amperage": data["amperage"]
}
graph_path = generate_test_graph(test_data)
if graph_path and os.path.exists(graph_path):
elements.append(Paragraph("Test Data Graph", heading2_style))
elements.append(Image(graph_path, width=6*inch, height=3*inch))
elements.append(Spacer(1, 12))
if "bom_items" in data:
elements.append(Paragraph("Please use this checklist to pull the required items for the pump assembly.", normal_style))
elements.append(Spacer(1, 12))
bom_items = data["bom_items"]
if not isinstance(bom_items, (list, tuple)) or not bom_items:
elements.append(Paragraph("Invalid BOM data received. Please check the pump configuration.", normal_style))
else:
table_data = [["Part Code", "Item", "Quantity", "Check"]]
for item in bom_items:
part_name = item.get("part_name", "N/A") if isinstance(item, dict) else str(item)
part_code = item.get("part_code", "N/A") if isinstance(item, dict) else "N/A"
quantity = str(item.get("quantity", "1")) if isinstance(item, dict) else "1"
table_data.append([Paragraph(part_code, cell_style), Paragraph(part_name, cell_style), quantity, "[ ]"])
table = Table(table_data, colWidths=[2.5*inch, 2*inch, 1*inch, 1*inch])
table.setStyle(TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("FONTSIZE", (0, 0), (-1, -1), 10),
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
("BACKGROUND", (0, 1), (-1, -1), colors.beige),
("GRID", (0, 0), (-1, -1), 1, colors.black),
("VALIGN", (0, 0), (-1, -1), "TOP")
]))
elements.append(table)
else:
table_data = [["Field", "Value"]]
for key, value in data.items():
# Use display versions for table if available, otherwise raw value
if key in ["flowrate", "pressure", "amperage"] and f"{key}_display" in data:
display_key = f"{key}_display"
value_paragraph = Paragraph(str(data[display_key]).replace("\n", "<br/>"), cell_style)
table_data.append([key.replace("_", " ").title(), value_paragraph])
elif key != "bom_items" and value and not key.endswith("_display"):
value_paragraph = Paragraph(str(value).replace("\n", "<br/>"), cell_style)
table_data.append([key.replace("_", " ").title(), value_paragraph])
table = Table(table_data, colWidths=[2.5*inch, 4.5*inch])
table.setStyle(TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("FONTSIZE", (0, 0), (-1, -1), 10),
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
("BACKGROUND", (0, 1), (-1, -1), colors.beige),
("GRID", (0, 0), (-1, -1), 1, colors.black),
("VALIGN", (0, 0), (-1, -1), "TOP")
]))
elements.append(table)
elements.append(Spacer(1, 12))
footer_style = ParagraphStyle(name="Footer", fontSize=10, alignment=1, textColor=colors.grey)
generated_on = data.get("generated_on", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
elements.append(Paragraph(f"Generated on {generated_on} | Version {EXPORT_UTILS_VERSION}", footer_style))
try:
doc.build(elements)
logger.info(f"Generated PDF at {output_path}")
return output_path
except Exception as e:
logger.error(f"Failed to generate PDF for {serial_number}: {str(e)}")
elements.append(Paragraph(f"Error generating PDF: {str(e)}", normal_style))
doc.build(elements)
return output_path
def generate_pump_details_table(data):
"""Generate an HTML table for pump details."""
try:
rows = "".join(f"""
<tr>
<td style="padding: 8px; border: 1px solid #ddd; background-color: #f9f9f9; font-weight: bold;">{key.replace('_', ' ').title()}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{value}</td>
</tr>
""" for key, value in data.items() if value and not key.endswith("_display"))
return f"""
<table style="border-collapse: collapse; width: 100%; max-width: 600px; margin: 10px 0; font-family: Arial, sans-serif;">
<tbody>{rows}</tbody>
</table>
"""
except Exception as e:
logger.error(f"Failed to generate pump details table: {str(e)}")
return "<p>Error generating pump details table.</p>"
def generate_bom_table(bom_items):
"""Generate an HTML table for BOM items."""
if not bom_items:
return "<p>No BOM items available.</p>"
try:
rows = "".join(f"""
<tr>
<td style="padding: 8px; border: 1px solid #ddd;">{item.get('part_name', 'N/A')}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{item.get('part_code', 'N/A')}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{item.get('quantity', 'N/A')}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{item.get('pulled', 'N/A')}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{item.get('reason', 'N/A')}</td>
</tr>
""" for item in bom_items)
return f"""
<h3 style="color: #34495e;">Bill of Materials</h3>
<table style="border-collapse: collapse; width: 100%; max-width: 600px; margin-bottom: 20px;">
<thead>
<tr style="background-color: #f4f4f4;">
<th style="padding: 8px; border: 1px solid #ddd;">Part Name</th>
<th style="padding: 8px; border: 1px solid #ddd;">Part Code</th>
<th style="padding: 8px; border: 1px solid #ddd;">Quantity</th>
<th style="padding: 8px; border: 1px solid #ddd;">Pulled</th>
<th style="padding: 8px; border: 1px solid #ddd;">Reason</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
"""
except Exception as e:
logger.error(f"Failed to generate BOM table: {str(e)}")
return "<p>Error generating BOM table.</p>"
def generate_test_data_table(test_data):
"""Generate an HTML table for test data."""
if not test_data or not all(key in test_data for key in ["flowrate", "pressure", "amperage"]):
return "<p>No valid test data available.</p>"
try:
rows = "".join(f"""
<tr>
<td style="padding: 8px; border: 1px solid #ddd;">Test {i+1}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{test_data['flowrate'][i] if i < len(test_data['flowrate']) else ''}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{test_data['pressure'][i] if i < len(test_data['pressure']) else ''}</td>
<td style="padding: 8px; border: 1px solid #ddd;">{test_data['amperage'][i] if i < len(test_data['amperage']) else ''}</td>
</tr>
""" for i in range(5))
return f"""
<h3 style="color: #34495e;">Test Summary</h3>
<table style="border-collapse: collapse; width: 100%; max-width: 600px; margin-bottom: 20px;">
<thead>
<tr style="background-color: #f4f4f4;">
<th style="padding: 8px; border: 1px solid #ddd;">Test</th>
<th style="padding: 8px; border: 1px solid #ddd;">Flowrate (l/h)</th>
<th style="padding: 8px; border: 1px solid #ddd;">Pressure (bar)</th>
<th style="padding: 8px; border: 1px solid #ddd;">Amperage (A)</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
"""
except Exception as e:
logger.error(f"Failed to generate test data table: {str(e)}")
return "<p>Error generating test data table.</p>"