-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainUI.py
391 lines (324 loc) · 13.3 KB
/
MainUI.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Author: Quinn James (qj@quinnjam.es)
#
# The main UI for the graphical driver application. Interfaces with the
# command-line utility to generate an output spreadsheet.
#
# More details about this project can be found in the README file or at:
# https://github.com/qcjames53/AJM-RouteSummaries
from tkinter import messagebox
import tkinter
from tkinter.filedialog import asksaveasfilename
from tkinter.filedialog import askopenfilename
from datetime import date, datetime
import webbrowser
import traceback
import os
from TemplateGeneratorRideChecks import createTemplateRideChecks
from TemplateGeneratorBusStop import createTemplateBusStop
from RouteSummaryGenerator import generateSummary
from WorkbookUpdateUtility import convertFormat, convertValues
from Log import Log
# Constants
UPDATE_AFTER_LOG = True
DEFAULT_WINDOW_WIDTH = 1200
DEFAULT_WINDOW_HEIGHT = 500
REPO_URL = "https://github.com/qcjames53/AJM-RouteSummaries"
DEFUALT_RIDECHECKS_PREFIX = "ridechecks"
DEFAULT_BUS_STOP_PREFIX = "busstop"
DEFAULT_ROUTE_SUMMARY_PREFIX = "summary"
def getDateString():
"""
Helper function for getting the date as a string.
@returns An ISO 8601 formatted date as a string.
"""
return str(date.today())
def getDateTimeString():
"""
Helper function for getting the datetime as a string.
@returns An ISO 8601 formatted datetime as a string.
"""
return str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
class MainWindow(tkinter.Frame):
"""
A class that defines the main window of the GUI application for the route
summary generator. Extends tkinter.Frame.
"""
def __init__(self, master=None):
"""
Initialize the main window of the GUI application.
@param master The parent window of the main window. Defaults to None
"""
# Non-tkinter member variables
self.ride_checks_filepath = None
self.bus_stop_filepath = None
# Handle instantiation
tkinter.Frame.__init__(self, master)
self.master = master
self.master.option_add('*tearOff', False)
# Create the options menu bar
menu = tkinter.Menu(self.master)
self.master.config(menu=menu)
# Create the file menu
fileMenu = tkinter.Menu(menu)
selectMenu = tkinter.Menu(fileMenu)
selectMenu.add_command(label="Ride Checks",
command=self.setRideChecks)
selectMenu.add_command(label="Bus Stop",
command=self.setBusStop)
fileMenu.add_cascade(label="Select Input File", menu=selectMenu)
fileMenu.add_command(label="Exit", command=self.exitProgram)
menu.add_cascade(label="File", menu=fileMenu)
# Create Utility Menu
templateMenu = tkinter.Menu(menu)
generateMenu = tkinter.Menu(templateMenu)
generateMenu.add_command(label="Ride Checks",
command=self.createRideChecks)
generateMenu.add_command(label="Bus Stop",
command=self.createBusStop)
templateMenu.add_cascade(label="Generate Template", menu=generateMenu)
templateMenu.add_command(label="Old Format Conversion",
command=self.convertOldFormat)
templateMenu.add_command(label="Clear Displayed Log",
command=self.clearLog)
menu.add_cascade(label="Utility", menu=templateMenu)
# Create Run Button
menu.add_command(label="Run", command=self.runRouteSummary)
# Create Help Menu
helpMenu = tkinter.Menu(menu)
helpMenu.add_command(label="Open Documentation",
command=self.openRepository)
menu.add_cascade(label="Help", menu=helpMenu)
# Create a scrolling debug/message log on the GUI application
self.log_text = tkinter.Text(self.master)
scroll_bar = tkinter.Scrollbar(self.master, command=self.log_text.yview)
self.log_text.config(yscrollcommand=scroll_bar.set)
scroll_bar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
self.log_text.pack(side=tkinter.LEFT, expand=True, fill=tkinter.BOTH)
# Create the log
self.log = Log(self.applicationMessage)
self.log.logGeneral("Application launched")
def openRepository(self):
"""
Opens the repository for the RouteSummaryGenerator project.
"""
webbrowser.open(REPO_URL, new=1)
def exitProgram(self):
"""
Exits the GUI program safely.
"""
exit()
def applicationMessage(self, message):
"""
Alerts the user with a provided message
@param message A string to display to the user
"""
self.log_text.insert(tkinter.END, "\n" + message)
self.log_text.yview_scroll(1, "units")
if UPDATE_AFTER_LOG:
self.update()
def clearLog(self):
"""
Clears the log messages
"""
self.log_text.delete("1.0","end")
self.update()
def createRideChecks(self):
"""
Opens a save-as dialog and creates a ride checks template at the
provided location.
"""
# Get default filename
default_name = DEFUALT_RIDECHECKS_PREFIX + getDateString() + ".xlsx"
# Save-As dialog
save_filepath = asksaveasfilename(
title="Save Ride Checks Workbook",
initialfile=default_name,
defaultextension=".xlsx",
filetypes=[("Excel Workbook", "*.xlsx")
])
# Check for 'cancel', if true return
if save_filepath is None or save_filepath == "":
return
# Write the file, alert the user
createTemplateRideChecks(self.log, save_filepath)
self.log.logGeneral("Successfully created the ride check template"\
+ " '" + save_filepath + "'")
def createBusStop(self):
"""
Opens a save-as dialog and creates a bus stop template at the
provided location.
"""
# Get default filename
default_name = DEFAULT_BUS_STOP_PREFIX + getDateString() + ".xlsx"
# Save-As dialog
save_filepath = asksaveasfilename(
title="Save Bus Stop Workbook",
initialfile=default_name,
defaultextension=".xlsx",
filetypes=[("Excel Workbook", "*.xlsx")]
)
# Check for 'cancel', if true return
if save_filepath is None or save_filepath == "":
return
# Write the file, alert the user
createTemplateBusStop(self.log, save_filepath)
self.log.logGeneral("Successfully created the bus stop template"\
+ " '" + save_filepath + "'")
def convertOldFormat(self):
"""
Asks the user to select a document to convert. Tries to update this
document to the new document format (.xls to .xlsx, replaces numberical
dates and times with Excel dates and times, etc.)
"""
# Ask the user to select the old workbook
self.log.logGeneral("Selecting old-format workbook file...")
filepath = askopenfilename(
title="Select Old-format Workbook",
filetypes=[
("Excel Workbook", "*.xls *.xlsx"),
]
)
# If cancel was selected, set None and return
if filepath is None or filepath == "":
self.log.logGeneral("File selection was cancelled by the user.")
return
self.log.logGeneral("Selected old-format file '" + \
filepath + "'")
# Ask where to save the generated sheet
# Get default filename & extension
basename = os.path.splitext(os.path.basename(filepath))
filename = basename[0]
extension = basename[1]
default_name = filename + ".xlsx"
# Save-As dialog
save_filepath = asksaveasfilename(
title="Save Ride Checks Workbook",
initialfile=default_name,
defaultextension=".xlsx",
filetypes=[("Excel Workbook", "*.xlsx")]
)
# Check for 'cancel', if true return
if save_filepath is None or save_filepath == "":
self.log.logGeneral("File selection was cancelled by the user.")
return
# Update to .xlsx if neccesary
if extension == ".xls":
self.log.logGeneral("Updating Excel 97-2003 .xls document...")
self.update()
try:
convertFormat(self.log, filepath, save_filepath)
except:
self.log.logFailure("Workbook file format update failed.")
print(traceback.format_exc())
return
# Update the internal values
self.log.logGeneral("Updating workbook values to current format.")
self.update()
try:
if extension == ".xls":
convertValues(self.log, save_filepath)
else:
convertValues(self.log, filepath, save_filepath=save_filepath)
except:
self.log.logFailure("Workbook value update failed.")
print(traceback.format_exc())
return
def setRideChecks(self):
"""
Asks the user to select a ride checks workbook. Sets the ride checks
member variable to match. Sets to None if cancelled.
"""
# Ask the user for the ride checks workbook
filepath = askopenfilename(
title="Select Ride Checks Workbook",
filetypes=[("Excel Workbook", "*.xlsx")]
)
# If cancel was selected, set None and return
if filepath is None or filepath == "":
self.ride_checks_filepath = None
return
# Set the member variable to the selected filepath, alert the user
self.ride_checks_filepath = filepath
self.log.logGeneral("Selected ride checks file '" + \
self.ride_checks_filepath + "'")
def setBusStop(self):
"""
Asks the user to select a bus stop workbook. Sets the bus stop
member variable to match. Sets to None if cancelled.
"""
# Ask the user for the ride checks workbook
filepath = askopenfilename(
title="Select Bus Stop Workbook",
filetypes=[("Excel Workbook", "*.xlsx")]
)
# If cancel was selected, set None and return
if filepath is None or filepath == "":
self.bus_stop_filepath = None
return
# Set the member variable to the selected filepath, alert the user
self.bus_stop_filepath = filepath
self.log.logGeneral("Selected bus stop file '" + \
self.bus_stop_filepath + "'")
def runRouteSummary(self):
"""
Runs the route summary generator with the selected filepaths. Will
run setRideCheck and/or setBusStop if the respective filepaths have
not been set.
"""
# Get the respective sheet filepaths if they haven't been selected
if self.ride_checks_filepath is None:
self.setRideChecks()
# check a valid filepath was submitted. If not, exit.
if self.ride_checks_filepath is None:
self.log.logGeneral("Route summary generation was cancelled by the user.")
return
if self.bus_stop_filepath is None:
self.setBusStop()
# check a valid filepath was submitted. If not, exit.
if self.bus_stop_filepath is None:
self.log.logGeneral("Route summary generation was cancelled by the user.")
return
# Ask where to save the generated sheet
# Get default filename
default_name = DEFAULT_ROUTE_SUMMARY_PREFIX + getDateString() + ".xlsx"
# Save-As dialog
save_filepath = asksaveasfilename(
title="Save Route Summary Workbook",
initialfile=default_name,
defaultextension=".xlsx",
filetypes=[("Excel Workbook", "*.xlsx")]
)
# Check for 'cancel', if true return
if save_filepath is None or save_filepath == "":
self.log.logGeneral("Route summary generation was cancelled by the user.")
return
# Run the RouteSummaryGenerator utility and log
self.log.logGeneral("Generating route summary...")
self.update()
try:
summary_generation_result = generateSummary(self.log, \
self.ride_checks_filepath, self.bus_stop_filepath, \
save_filepath)
# Console message for the correct result
if(summary_generation_result == 0):
self.log.logGeneral("Successfully created the route summary '"\
+ save_filepath + "'")
elif(summary_generation_result == 1):
self.log.logError("Major error enconutered while generating summary.")
elif(summary_generation_result == 2):
self.log.logFailure("Output workbook '" + save_filepath +\
"' could not be created.")
else:
self.log.logError("Unspecified error")
except:
self.log.logFailure("Summary generation failed.")
print(traceback.format_exc())
# Reset the selected filepaths
self.bus_stop_filepath = None
self.ride_checks_filepath = None
# Main program start
root = tkinter.Tk()
app = MainWindow(root)
root.wm_title("Route Summaries Utility")
root.geometry(str(DEFAULT_WINDOW_WIDTH) + "x" + str(DEFAULT_WINDOW_HEIGHT))
root.mainloop()