-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
209 lines (187 loc) · 12.3 KB
/
main.py
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
import webbrowser
from scripts import attachment_config, email_config_send, msg_config, preview, user_messages, THEME, FONT, FONT_L, FONT_XL, URL_GITHUB
import PySimpleGUI as sg # pip install PySimpleGUI
def get_info() -> None:
'''Displays the info window.'''
info_layout = [
[sg.Push(), sg.Text("~~EnMasse E-mail~~", font= FONT_XL), sg.Push()],
[sg.Push(), sg.Text("Send E-mails en masse using a template.", font= FONT_L), sg.Push()],
[sg.Push(), sg.Text("Version 1.0", font= FONT), sg.Push()],
[sg.HorizontalSeparator()],
[sg.Push(), sg.T("Github Repository", key= "-GITHUB_URL-", enable_events= True, tooltip= URL_GITHUB, text_color= "Blue", background_color= "Grey", font= FONT + ("underline", )), sg.Push()],
[sg.Push(), sg.OK(), sg.Push()]
]
info_window = sg.Window("Info", info_layout, enable_close_attempted_event= True, font= FONT, modal= True, icon= "icon.ico")
while True:
event, _ = info_window.read()
match event:
case sg.WINDOW_CLOSE_ATTEMPTED_EVENT | "OK":
break
case "-GITHUB_URL-":
webbrowser.open(URL_GITHUB)
info_window.close()
def get_main_layout() -> list[list[sg.Element]]:
'''Returns the layout to use for the main window.'''
message_configuration_layout = msg_config.get_msg_config_layout()
attachment_configuration_layout = attachment_config.get_attachment_config_layout()
preview_layout = preview.get_preview_layout("-PREVIEW-", (30, 10))
email_config_send_layout = email_config_send.get_email_config_send_layout()
layout = [
[sg.TabGroup(
[
[sg.Tab("Message configuration", message_configuration_layout),
sg.Tab("Attachment configuration", attachment_configuration_layout),
sg.Tab("E-mail configuration and Send", email_config_send_layout),
sg.Tab("Preview", preview_layout)
]
], expand_x= True, expand_y= True)
],
[sg.Button("Reset"), sg.Push(), sg.Button("Info")]
]
return layout
def main():
'''Main function.'''
sg.theme(THEME)
#print(sg.theme_text_color())
window = sg.Window("EnMasse E-mail", get_main_layout(), font= FONT, icon= "icon.ico", return_keyboard_events= True, enable_close_attempted_event= True, resizable= True, finalize= True)
window.set_min_size((1080, 710))
data_df = None # Will be the dataframe containing the replacement data.
placeholders = []
template_text = ""
total_emails_to_send = 0 # Will be determined by the amount of rows in data dataframe.
attachments_valid = False
preview_index = 0
preview_live= False
preview_element = None
msg_config.expand_scrollable_column("-PAIR_COLUMN-", window)
while True:
event, values = window.read()
match event:
case sg.WINDOW_CLOSE_ATTEMPTED_EVENT | sg.WIN_CLOSED: # | "Escape:27":
break
case "Reset":
window.close()
main()
case "Info":
get_info()
# Message configuration events
case "-BROWSE_TEMPLATE-":
msg_config.browse_template_event(window)
case "-BROWSE_DATA-":
msg_config.browse_data_event(window)
case "-GENERATE_PAIRS-":
if not values["-TEMPLATE_PATH-"] or not values["-DATA_PATH-"]: # Error handling starts here
user_messages.one_line_error_handler("Please ensure you have selected the template and data files.")
elif not window.Element("-SHEET_NAMES-").Disabled and not values["-SHEET_NAMES-"]:
user_messages.one_line_error_handler("Please select the excel sheet.")
else:
try:
data_df = msg_config.load_data(values)
total_emails_to_send = len(data_df.index)
template_text = msg_config.load_template(values)
placeholders = msg_config.get_placeholders(data_df, placeholders, template_text, values, window)
window.Element("-GENERATE_PAIRS-").update(disabled= True) # If generate pairs button is pressed again, the resulting pairs have broken element keys for some reason, need to test more. If there is a need to correct something, reset.
window.Element("-BROWSE_TEMPLATE-").update(disabled= True)
window.Element("-BROWSE_DATA-").update(disabled= True)
except Exception as e:
user_messages.multiline_error_handler(["Unable to load file(s):", f"{type(e).__name__}: {str(e)}"])
#window['-PAIR_COLUMN-'].contents_changed() # Appears to not have any effect on the scrollable column, calling after event handling.
# Attachment configuration events
case "-BROWSE_ATTACHMENT_FILES-":
attachment_config.browse_attachment_files_event(window)
case "-BROWSE_ATTACHMENTS_DIRECTORY-":
attachment_config.browse_attachment_directory_event(window)
case "Validate and preview":
if values["-SEPARATE_ATTACHMENTS-"]:
if data_df is None: # Error handling starts here.
user_messages.one_line_error_handler("Please generate the pairs in the first tab.")
elif not values["-ATTACHMENTS_DIRECTORY-"]:
user_messages.one_line_error_handler("Please specify the dirctory containing the attachment files.")
elif not values["-ATTACHMENT_FILENAMES_COLUMN-"]:
user_messages.one_line_error_handler("Please select a data column.")
else:
data_attachments_filenames = attachment_config.get_data_attachments_filenames(data_df, total_emails_to_send, values)
directory_filenames = attachment_config.get_directory_attachments_filenames(values)
attachments_valid = attachment_config.attachment_validation(data_attachments_filenames, directory_filenames)
attachment_config.attachment_preview(data_attachments_filenames, directory_filenames, window)
# Preview events
case "-SHOW_PREVIEW-":
missing_pairs = False
for placeholder in placeholders:
if not values[("-DATA-", placeholder)]:
missing_pairs = True
break
if data_df is None: # Error handling starts here.
user_messages.one_line_error_handler("Please generate the pairs in the first tab.")
elif values["-SUBJECT_ALL-"] and not values["-SUBJECT-"]:
user_messages.one_line_error_handler("Please specify the subject.")
elif values["-SUBJECT_FROM_DATA-"] and not values["-SUBJECT_COLUMN-"]:
user_messages.one_line_error_handler("Please specify the subject data column.")
elif missing_pairs:
user_messages.one_line_error_handler("Please ensure that all placeholders have been paired with a data column.")
elif not values["-RECIPIENT_EMAIL_ADDRESS-"]:
user_messages.one_line_error_handler("Please specify the column containing the recipient e-mail address(es).")
else:
try:
preview_index, preview_element, preview_live = preview.show_preview_event(data_df, placeholders, template_text, values, window)
window.Element("-ROW_TO_JUMP-").update(2, values= [row for row in range(2, total_emails_to_send + 2)])
except Exception as e: # Error while processing the html.
preview_live = False
user_messages.multiline_error_handler(["Unable to show preview due to error:", f"{type(e).__name__}: {str(e)}"])
case "-NEXT-" | "Right:39":
if (preview_index < total_emails_to_send - 1) and preview_live:
preview_index = preview.next_preview_event(data_df, placeholders, preview_element, preview_index, template_text, values, window)
case "-PREVIOUS-" | "Left:37":
if preview_index > 0 and preview_live:
preview_index = preview.previous_preview_event(data_df, placeholders, preview_element, preview_index, template_text, values, window)
case "-JUMP_ROW-":
if preview_live:
preview_index = preview.jump_to_row_event(data_df, placeholders, preview_element, template_text, values, window)
# E-mail configuration and send events
case "-EMAIL_SERVICE-":
service = values[event]
email_config_send.set_server_port(service, window)
case "-PORT-":
port = values["-PORT-"]
if len(port) > 3:
window.Element("-PORT-").update(port[:3])
case "Setup and Send":
missing_pairs = False
for placeholder in placeholders:
if not values[("-DATA-", placeholder)]:
missing_pairs = True
break
if data_df is None: # Error handling starts here.
user_messages.one_line_error_handler("Please generate the pairs in the first tab.")
elif values["-SUBJECT_ALL-"] and not values["-SUBJECT-"]:
user_messages.one_line_error_handler("PLease specify a subject.")
elif values["-SUBJECT_FROM_DATA-"] and not values["-SUBJECT_COLUMN-"]:
user_messages.one_line_error_handler("Please specify the subject data column.")
elif missing_pairs:
user_messages.one_line_error_handler("Please ensure that all placeholders have been paired with a data column.")
elif values["-SAME_ATTACHMENTS-"] and not values["-SAME_ATTACHMENT_FILES-"]:
user_messages.one_line_error_handler("Please specify the attachment file(s).")
elif values["-SEPARATE_ATTACHMENTS-"] and not values["-ATTACHMENTS_DIRECTORY-"]:
user_messages.one_line_error_handler("Please specify the attachment file directory.")
elif values["-SEPARATE_ATTACHMENTS-"] and not values["-ATTACHMENT_FILENAMES_COLUMN-"]:
user_messages.one_line_error_handler("Please specify the attachment data column.")
elif values["-SEPARATE_ATTACHMENTS-"] and not attachments_valid:
user_messages.one_line_error_handler("Please validate the attachments and ensure that all required files exist in the directory.")
elif not values["-SERVER-"] or not values["-PORT-"]:
user_messages.one_line_error_handler("Please specify the e-mail server and port.")
elif not values["-RECIPIENT_EMAIL_ADDRESS-"]:
user_messages.one_line_error_handler("Please specify the data column containing the recipient e-mail address(es).")
elif values["-INCLUDE_CC-"] and not values["-CC_EMAIL_ADDRESS-"]:
user_messages.one_line_error_handler("Please specify the data column containing the CC e-mail address(es).")
elif values["-INCLUDE_BCC-"] and not values["-BCC_EMAIL_ADDRESS-"]:
user_messages.one_line_error_handler("Please specify the data column containing the BCC e-mail address(es).")
elif values["-SET_ALIAS-"] and not values["-ALIAS-"]:
user_messages.one_line_error_handler("Please specify your alias.")
else:
window.Element("Setup and Send").update(disabled = True)
email_config_send.setup_and_send_event(data_df, placeholders, template_text, total_emails_to_send, values, window)
window.Element("Setup and Send").update(disabled = False)
window.Element('-PAIR_COLUMN-').contents_changed() # Inefficient to call after every event, but does not work in "msg_config.generate_pairs_event" function or after calling it in event handling.
window.close()
if __name__ == "__main__":
main()