-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.py
464 lines (366 loc) · 16.9 KB
/
gui.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# -*- coding: utf-8 -*-
"""
Graphical user interface for speed test.
Hopefully useful.
"""
# tcl/tk -- used for building GUI
# messagebox -- neat little popup window
try:
import tkinter as tk
import tkinter.messagebox as messagebox
except ImportError:
import Tkinter as tk
import TkMessageBox as messagebox
# for the Resnet button
import webbrowser
# for running the speedtester thread in the background
import threading
# recordng time
import time
# makes the test display easier to read
from pyspeedtest import pretty_speed
# showing errors
import traceback
# import the rest of the code
from main import test_once
from uploadclient import upload
from analytics import run_analytics
from autoupdate import has_update, download_update
from settings import REC_FILE, LOCATION, FREQ, VERBOSITY, FORCE_SERVER, \
ANALYZE_FILE, ANALYTICS_REC_FILE, STANDARDS_ENABLE, \
STANDARD_PING, STANDARD_UP, STANDARD_DOWN, UPLOAD_URL, \
UPLOAD_PORT, parser
# background thread for running speed tests
class SpeedTesterThread(threading.Thread):
"""
Background thread for running speed tests.
"""
def __init__(self, handler):
"""
Instantiate thread handler.
"""
threading.Thread.__init__(self)
# used to pass the last result to the GUI handler for analysis/display
self.last_result = {'ping': 0, 'up': 0, 'down': 0}
# GUI tells us whether to stop or not
self.stoprequest = threading.Event()
# GUI handler itself
self.handler = handler
def run(self):
"""
Run the thread (or don't)
"""
# tell the user we're running
self.handler.thread_status.config(text="Thread status: alive")
# if the stop request is set, we want to stop. so, run while it's not
while not self.stoprequest.isSet():
# tell user we're testing
self.handler.thread_status.config(text="Thread status: testing")
# run the speed test
newline, time_diff, self.last_result = test_once(
self.handler.location_entry.get())
# tell the user we're now outputting the results
self.handler.thread_status.config(
text="Thread status: writing results")
# write the results to the specified file
with open(REC_FILE, 'a') as record:
record.write(newline)
# tell the handler we're done so it can update the display
self.handler.update_statistics()
# check again for stop request here -- otherwise, we'll wait
# to the next test unnecessarily
if not self.stoprequest.isSet():
self.handler.thread_status.config(text="Thread status: waiting")
time.sleep(time_diff)
self.handler.status_label.config(text="Status: stopped")
self.handler.thread_status.config(text="Thread status: dead")
def join(self, timeout=None):
# set the stop request so next time the test starts/stops we'll exit
self.stoprequest.set()
super(SpeedTesterThread, self).join(timeout)
class SpeedTesterGUI(object):
"""
Speed tester GUI object
"""
def __init__(self):
"""
Instantiate the GUI menu. Runs automatically.
"""
# root window
self.root = tk.Tk()
# check for an update. if there is, prompt user to download
try:
update_available = has_update()
except Exception:
update_available = False # can't update with no connection!
if update_available:
want_update = messagebox.askyesno("Update",
"An update has been detected." +
" Would you like to download?")
if want_update == 'yes':
download_update()
# set defaults
self.location = "-- ENTER LOCATION --"
self.lasttest = {'ping': 0, 'up': 0, 'down': 0}
self.avg = {'ping': 0, 'up': 0, 'down': 0}
self.ntests = 0
# instantiate the speed tester background thread
self.thread = SpeedTesterThread(self)
# build and start the GUI
self.init_gui()
try:
self.root.mainloop()
except Exception as exc:
messagebox.showerror("PySpeedTest Broke",
exc.message)
def show_error(self, *args):
err = traceback.format_exception(*args)
messagebox.showerror('Exception',err)
def update_statistics(self):
"""
Updates the statistics display on the GUI menu.
Responsible for keeping track of rolling averages and last tests.
Called directly by the tester thread.
"""
self.lasttest = self.thread.last_result
self.ntests += 1
self.avg['ping'] = self.avg['ping'] + ((self.lasttest['ping'] -
self.avg['ping']) /
self.ntests)
self.avg['down'] = self.avg['down'] + ((self.lasttest['down'] -
self.avg['down']) /
self.ntests)
self.avg['up'] = self.avg['up'] + ((self.lasttest['up'] -
self.avg['up']) /
self.ntests)
# create new format strings from computed averages/last test
# \u2191 is an up arrow
# \u2193 is a down arrow
last_str = "Last: {ping}ms / {up}\u2191 / {down}\u2193".format(
ping=self.lasttest['ping'],
up=pretty_speed(self.lasttest['up']),
down=pretty_speed(self.lasttest['down']))
avg_str = "Avg: {ping}ms / {up}\u2191 / {down}\u2193".format(
ping=round(self.avg['ping'], 2),
up=pretty_speed(self.avg['up']),
down=pretty_speed(self.avg['down']))
# apply changes
self.last_test_label.config(text=last_str)
self.avg_test_label.config(text=avg_str)
def start(self):
"""
Start the tester thread.
https://github.com/mishaturnbull/PySpeedTest/issues/4
"""
self.lasttest = {'ping': 0, 'up': 0, 'down': 0}
self.avg = {'ping': 0, 'up': 0, 'down': 0}
self.ntests = 0
self.status_label.config(text="Status: running")
self.thread.start()
def stop(self):
"""
Stop the tester thread.
Should not cause the program to hang.
"""
self.status_label.config(text="Status: stopping")
self.thread.join(0)
def make_analysis_file(self):
"""
Create the statistical analysis file.
"""
run_analytics()
def upload_data(self):
"""
Upload data to the server.
Requires a network connection (duh).
"""
upload() # easy enough :)
def edit_config(self):
"""
Configuration edit menu.
Shouldn't all be in one function, but oh well.
"""
cfgmen = tk.Toplevel(self.root)
cfgmen.wm_title("Configuration")
def set_vars():
"""
The 'Apply' button.
https://github.com/mishaturnbull/PySpeedTest/issues/5
"""
parser['Speedtester']['rec_file'] = entry_recfile.get()
parser['Speedtester']['location'] = entry_location.get()
parser['Speedtester']['freq'] = entry_freq.get()
parser['Speedtester']['verbosity'] = entry_verbosity.get()
parser['Speedtester']['force_server'] = entry_server.get()
parser['Analytics']['analyze_file'] = entry_afile.get()
parser['Analytics']['analytics_rec_file'] = entry_arecfile.get()
parser['Analytics']['standards_enable'] = str(bool(standvar.get()))
parser['Analytics']['standard_ping'] = entry_stan_ping.get()
parser['Analytics']['standard_up'] = entry_stan_up.get()
parser['Analytics']['standard_down'] = entry_stan_down.get()
parser['Upload']['url'] = entry_upload_url.get()
parser['Upload']['port'] = int(entry_upload_port.get())
with open("config.ini", 'w') as configfile:
parser.write(configfile)
def refresh():
"""
The 'Defaults' button.
"""
entry_recfile.delete(0, 'end')
entry_recfile.insert(0, REC_FILE)
# Set the location field to be the value of the location field
# in the main menu
entry_location.delete(0, 'end')
entry_location.insert(0, self.location_entry.get())
entry_freq.delete(0, 'end')
entry_freq.insert(0, FREQ)
entry_verbosity.delete(0, 'end')
entry_verbosity.insert(0, VERBOSITY)
entry_server.delete(0, 'end')
entry_server.insert(0, str(FORCE_SERVER))
entry_afile.delete(0, 'end')
entry_afile.insert(0, str(ANALYZE_FILE))
entry_arecfile.delete(0, 'end')
entry_arecfile.insert(0, ANALYTICS_REC_FILE)
standvar.set(int(bool(STANDARDS_ENABLE)))
_updopt()
entry_stan_ping.delete(0, 'end')
entry_stan_ping.insert(0, STANDARD_PING)
entry_stan_up.delete(0, 'end')
entry_stan_up.insert(0, STANDARD_UP)
entry_stan_down.delete(0, 'end')
entry_stan_down.insert(0, STANDARD_DOWN)
entry_upload_url.delete(0, 'end')
entry_upload_url.insert(0, UPLOAD_URL)
entry_upload_port.delete(0, 'end')
entry_upload_port.insert(0, str(UPLOAD_PORT))
setbutton = tk.Button(cfgmen, text="Apply", command=set_vars)
setbutton.grid(row=0, column=0, sticky=tk.W)
refreshbutton = tk.Button(cfgmen, text="Refresh", command=refresh)
refreshbutton.grid(row=0, column=1, sticky=tk.E)
label_sec_speedtest = tk.Label(cfgmen,
text="===== SPEEDTESTER SETTINGS =====")
label_sec_speedtest.grid(row=1, column=0, columnspan=2)
label_recfile = tk.Label(cfgmen, text="Record file name:")
label_recfile.grid(row=2, column=0, sticky=tk.W)
entry_recfile = tk.Entry(cfgmen, width=40)
entry_recfile.grid(row=2, column=1, sticky=tk.W)
# TODO: resize location entry size to appropriate length
label_location = tk.Label(cfgmen, text="Recording location:")
label_location.grid(row=3, column=0, sticky=tk.W)
entry_location = tk.Entry(cfgmen, width=40)
entry_location.grid(row=3, column=1, sticky=tk.W)
label_freq = tk.Label(cfgmen, text="Testing frequency (min):")
label_freq.grid(row=4, column=0, sticky=tk.W)
entry_freq = tk.Entry(cfgmen, width=10)
entry_freq.grid(row=4, column=1, sticky=tk.W)
label_verbosity = tk.Label(cfgmen, text="Verbosity: (0-3):")
label_verbosity.grid(row=5, column=0, sticky=tk.W)
entry_verbosity = tk.Entry(cfgmen, width=2)
entry_verbosity.grid(row=5, column=1, sticky=tk.W)
label_server = tk.Label(cfgmen, text="Force speedtest server:")
label_server.grid(row=6, column=0, sticky=tk.W)
entry_server = tk.Entry(cfgmen, width=40)
entry_server.grid(row=6, column=1, sticky=tk.W)
label_sec_analytics = tk.Label(cfgmen,
text="===== ANALYTICS SETTINGS =====")
label_sec_analytics.grid(row=7, column=0, columnspan=2)
label_afile = tk.Label(cfgmen, text="Analysis file:")
label_afile.grid(row=8, column=0, sticky=tk.W)
entry_afile = tk.Entry(cfgmen, width=40)
entry_afile.grid(row=8, column=1, sticky=tk.W)
label_arecfile = tk.Label(cfgmen, text="Output file:")
label_arecfile.grid(row=9, column=0, sticky=tk.W)
entry_arecfile = tk.Entry(cfgmen, width=40)
entry_arecfile.grid(row=9, column=1, sticky=tk.W)
def _updopt():
"""
Standards enable/disable toggle greyout fields shortcut.
"""
if standvar.get() != 0:
entry_stan_ping.config(state=tk.NORMAL)
entry_stan_up.config(state=tk.NORMAL)
entry_stan_down.config(state=tk.NORMAL)
else:
entry_stan_ping.config(state=tk.DISABLED)
entry_stan_up.config(state=tk.DISABLED)
entry_stan_down.config(state=tk.DISABLED)
standvar = tk.IntVar()
label_standards = tk.Label(cfgmen, text="Standards:")
label_standards.grid(row=10, column=0, sticky=tk.W)
button_standards = tk.Checkbutton(cfgmen, text="Enable",
variable=standvar,
command=_updopt)
button_standards.grid(row=10, column=1, sticky=tk.W)
label_stan_ping = tk.Label(cfgmen, text="Ping standard:")
label_stan_ping.grid(row=11, column=0, sticky=tk.W)
entry_stan_ping = tk.Entry(cfgmen, width=5)
entry_stan_ping.grid(row=11, column=1, sticky=tk.W)
label_stan_up = tk.Label(cfgmen, text="Upload standard (Mbps):")
label_stan_up.grid(row=12, column=0, sticky=tk.W)
entry_stan_up = tk.Entry(cfgmen, width=6)
entry_stan_up.grid(row=12, column=1, sticky=tk.W)
label_stan_down = tk.Label(cfgmen, text="Download standard (Mbps):")
label_stan_down.grid(row=13, column=0, sticky=tk.W)
entry_stan_down = tk.Entry(cfgmen, width=6)
entry_stan_down.grid(row=13, column=1, sticky=tk.W)
label_sec_upload = tk.Label(cfgmen, text="===== UPLOAD SETTINGS =====")
label_sec_upload.grid(row=14, column=0, columnspan=2, sticky=tk.W)
label_upload_url = tk.Label(cfgmen, text="Upload URL:")
label_upload_url.grid(row=15, column=0, sticky=tk.W)
entry_upload_url = tk.Entry(cfgmen, width=40)
entry_upload_url.grid(row=15, column=1, sticky=tk.W)
label_upload_port = tk.Label(cfgmen, text="Upload port:")
label_upload_port.grid(row=16, column=0, sticky=tk.W)
entry_upload_port = tk.Entry(cfgmen, width=7)
entry_upload_port.grid(row=16, column=1, sticky=tk.W)
_updopt()
refresh()
def resnet(self):
"""
Open your web browser to the ResNet support ticket page.
"""
# If only ResNet would've made this page easier to find,
# this option wouldn't be needed.
webbrowser.open("http://und.edu/web-support/request.cfm")
# here we go... avoid this code. it's bad.
def init_gui(self):
"""
Make it all look pretty (not)
"""
self.root.title("Internet Speed Tester")
self.status_label = tk.Label(self.root, text="Status: stopped")
self.status_label.grid(row=1, column=0, sticky=tk.W)
self.start_button = tk.Button(self.root, text="Start",
command=self.start)
self.start_button.grid(row=1, column=1, sticky=tk.E)
self.stop_button = tk.Button(self.root, text="Stop",
command=self.stop)
self.stop_button.grid(row=1, column=2, sticky=tk.W)
self.location_label = tk.Label(self.root, text="Location:")
self.location_label.grid(row=2, column=0, sticky=tk.W)
self.location_entry = tk.Entry(self.root, width=17)
self.location_entry.grid(row=2, column=1, columnspan=2,
sticky=tk.W)
self.location_entry.insert(0, LOCATION)
self.makefile_button = tk.Button(self.root, text="Create analysis file",
command=self.make_analysis_file)
self.makefile_button.grid(row=3, column=0, columnspan=2,
sticky=tk.W)
self.upload_button = tk.Button(self.root, text="Upload",
command=self.upload_data)
self.upload_button.grid(row=3, column=1, sticky=tk.W)
self.thread_status = tk.Label(self.root, text="Thread status:")
self.thread_status.grid(row=4, column=0, columnspan=3, sticky=tk.W)
self.last_test_label = tk.Label(self.root, text="Last: ")
self.last_test_label.grid(row=5, column=0, columnspan=3, sticky=tk.W)
self.avg_test_label = tk.Label(self.root, text="Avg.: ")
self.avg_test_label.grid(row=6, column=0, columnspan=3, sticky=tk.W)
self.config_button = tk.Button(self.root, text="Edit configuration",
command=self.edit_config)
self.config_button.grid(row=7, column=0, sticky=tk.W)
self.resnet_button = tk.Button(self.root, text="ResNet",
command=self.resnet)
self.resnet_button.grid(row=7, column=1, sticky=tk.W)
if __name__ == '__main__':
stg = SpeedTesterGUI()