Skip to content

Commit

Permalink
Add functionality to config menu
Browse files Browse the repository at this point in the history
- Minor big fix in main.py
- Switch to using .ini config file and ConfigParser class in settings.py
  • Loading branch information
mishaturnbull committed Jan 24, 2018
1 parent ff79c5b commit c685d16
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 113 deletions.
20 changes: 20 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Speedtester]
rec_file = speed_record.ilog
location = McVey 306B
freq = 0.5
verbosity = 3
force_server = None

[Analytics]
analyze_file = None
analytics_rec_file = report.txt
standards_enable = False
standard_ping = 0.0
standard_down = 0.0
standard_up = 0.0

[CSV]
csv_input_file = None
csv_output_file = Internet_speed_record.csv
csv_clear_infile = False

111 changes: 97 additions & 14 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from pyspeedtest import pretty_speed

from main import test_once
from settings import * # yeah I know this is bad.
from settings import REC_FILE, LOCATION, FREQ, VERBOSITY, FORCE_SERVER, \
ANALYZE_FILE, ANALYTICS_REC_FILE, STANDARDS_ENABLE, \
STANDARD_PING, STANDARD_UP, STANDARD_DOWN, parser

class SpeedTesterThread(threading.Thread):

Expand Down Expand Up @@ -63,20 +65,23 @@ def update_statistics(self):
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['ping']) /
self.ntests)
self.avg['down'] = self.avg['down'] + ((self.lasttest['down'] -
self.avg['down']) / self.ntests)
self.avg['down']) /
self.ntests)
self.avg['up'] = self.avg['up'] + ((self.lasttest['up'] -
self.avg['up']) / self.ntests)
self.avg['up']) /
self.ntests)

last_str = "Last: Ping: {ping}ms / {up}\u2191 / {down}\u2193".format(
ping=self.lasttest['ping'],
up=pretty_speed(self.lasttest['up']),
down=pretty_speed(self.lasttest['down']))
ping=self.lasttest['ping'],
up=pretty_speed(self.lasttest['up']),
down=pretty_speed(self.lasttest['down']))
avg_str = "Avg: Ping: {ping}ms / {up}\u2191 / {down}\u2193".format(
ping=self.avg['ping'],
up=pretty_speed(self.avg['up']),
down=pretty_speed(self.avg['down']))
ping=self.avg['ping'],
up=pretty_speed(self.avg['up']),
down=pretty_speed(self.avg['down']))

self.last_test_label.config(text=last_str)
self.avg_test_label.config(text=avg_str)
Expand All @@ -103,10 +108,55 @@ def edit_config(self):
cfgmen.wm_title("Configuration")

def set_vars():
pass
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()

with open("config.ini", 'w') as configfile:
parser.write(configfile)

def refresh():
pass
entry_recfile.delete(0, 'end')
entry_recfile.insert(0, REC_FILE)

entry_location.delete(0, 'end')
entry_location.insert(0, LOCATION)

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)


setbutton = tk.Button(cfgmen, text="Apply", command=set_vars)
setbutton.grid(row=0, column=0, sticky=tk.W)
Expand Down Expand Up @@ -158,7 +208,40 @@ def refresh():
entry_arecfile = tk.Entry(cfgmen, width=40)
entry_arecfile.grid(row=9, column=1, sticky=tk.W)


def _updopt():
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)

_updopt()


def resnet(self):
Expand Down Expand Up @@ -192,7 +275,7 @@ def init_gui(self):
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)
command=self.upload_data)
self.upload_button.grid(row=3, column=1, sticky=tk.W)

self.last_test_label = tk.Label(self.root, text="Last: ")
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_once():
except Exception as exc:
dprint(1, "It didn't work! Joining error line...")
newline = ", ".join([curtime, LOCATION, exc.__repr__()]) + '\n'
ping = down = upload_speed = 0

time_b = datetime.datetime.now()

Expand Down
121 changes: 22 additions & 99 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,25 @@
@author: Misha
"""

################ MAIN PROGRAM SETTINGS ################

######## Name of file to output to:
# No matter the extension, the program writes pure text data and the file can
# be opened and read with any text editor
REC_FILE = "speed_record_room.ilog"

######## Location to record at:
# The program supports recording speeds at multiple locations (e.g. 'Outside
# room 301' or 'Atrium'). Set the location here. For prettier outputs,
# don't set a location longer than the guide below.
# Guide: "-----------------"
LOCATION = "McVey 306B"

######## Speed test frequency:
# This value specifies how many minutes should elapse between speed tests.
# It is specified in minutes and can be set as a decimal (e.g. 2 is every 2
# minutes, 0.5 is every 30 seconds). If a test takes longer than `FREQ`
# seconds, the program will start the next test immediately after the first is
# complete. This behavior will continue until the program catches up to its
# specified testing interval.
FREQ = 0.5

######## Verbosity:
# This value specifies how talkative the program should be. It scales from
# 0 (run silently with no output) to 3 (tell me everything). Integers only.
#
# Well I mean decimals *should* work but why would you want do that?
VERBOSITY = 3

######## Speedtest server:
# If (and only if) you would like to specify a specific speedtest server,
# set the next value to the string format of the url of the server.
# For example, "speedtest.midco.net".
# If you would like to have the program (pyspeedtest) determine the best server
# automatically, leave the value as None.
FORCE_SERVER = None

################ ANALYTICS PROGRAM SETTINGS ################

######## Name of file to analyze:
# This filename should ONLY be specified if the file you would like to perform
# analytics on is different from the filename specified in the main program's
# record file.
# As an example, if you have multiple record files and would like to analyze
# a different file than the one you most recently created, set this value to
# that filename.
ANALYZE_FILE = None

######## Name of file to output to:
# This is the name of the file that is output by the analytics program.
# It's usually a little easier to set it as a *.txt file.
ANALYTICS_REC_FILE = "report.txt"

######## Speed standards:
# The analytics program can analyze speeds and compare them against a set
# standard. An example of using this feature would be if your department
# needs a minimum of 20 Mbit/sec, but isn't getting that, the program will
# tell you what percent of all tests were at or above that standard.

#### Enable:
# Boolean value. Whether or not to include standards in the report.
STANDARDS_ENABLE = True

#### Ping time:
# Set a maximum requires ping time in milliseconds.
STANDARD_PING = 25.0

#### Download speed:
# Set a minimum download speed in megabit/second.
# FCC defines broadband internet as 25Mbit/s (as of late 2017)
STANDARD_DOWN = 20.0

#### Upload speed:
# Set a minimum upload speed in megabit/second.
# FCC defines broadband internet sa 3Mbit/s (as of late 2017)
STANDARD_UP = 20.0

################ CSV CONVERSION PROGRAM SETTINGS ################

######## Name of file to analyze:
# This filename should ONLY be specified if the file you would like to perform
# conversion on is different from the filename specified in the main program's
# record file.
# As an example, if you have multiple record files and would like to convert
# a different file than the one you most recently created, set this value to
# that filename.
CSV_INPUT_FILE = None

######## Name of file to output to:
# For obvious reasons, I would recommend setting the filetype here to .csv.
CSV_OUTPUT_FILE = "Internet_speed_record.csv"

######## Clear records file on completion:
# This value determines whether or not the program should clear the file
# specified by CSV_INPUT_FILE when the csv conversion and output completes.
# For my use, I left it on, ran the program, copied the data to Excel, and
# processed it there while never even stopping the main program.
CSV_CLEAR_INFILE = False
import configparser

parser = configparser.ConfigParser()
parser.read("config.ini")

REC_FILE = parser['Speedtester']['rec_file']
LOCATION = parser['Speedtester']['location']
FREQ = float(parser['Speedtester']['freq'])
VERBOSITY = int(parser['Speedtester']['verbosity'])
server = parser['Speedtester']['force_server']
FORCE_SERVER = None if server == 'None' else server

ANALYZE_FILE = parser['Analytics']['analyze_file']
ANALYTICS_REC_FILE = parser['Analytics']['analytics_rec_file']
STANDARDS_ENABLE = parser['Analytics']['standards_enable']
STANDARD_PING = float(parser['Analytics']['standard_ping'])
STANDARD_DOWN = float(parser['Analytics']['standard_down'])
STANDARD_UP = float(parser['Analytics']['standard_up'])

CSV_INPUT_FILE = parser['CSV']['csv_input_file']
CSV_OUTPUT_FILE = parser['CSV']['csv_output_file']
CSV_CLEAR_INFILE = parser['CSV']['csv_clear_infile']

0 comments on commit c685d16

Please sign in to comment.