Skip to content

Commit

Permalink
Added Gui, option to run only once and sheet error
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0ndaime authored Dec 22, 2020
1 parent 1155a09 commit 7e35c8d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 14 deletions.
106 changes: 106 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from tkinter import *
from tkinter import filedialog
import json


class Gui:
def __init__(self):
# Read config and initialize Variables for Gui
self.config = json.load(open('config.json', 'r'))
self.window = Tk()
self.window.title("Config")
self.window.geometry("700x120")
self.path = StringVar()
self.sheet_id = StringVar()
self.calculate_averages = IntVar()
self.run_once = IntVar()
self.polling_interval = IntVar()
self.runs_to_average = IntVar()

# Give startvalue to variables
self.calculate_averages.set(int(self.config["calculate_averages"]))
self.sheet_id.set(self.config["sheet_id"])
self.run_once.set(int(self.config["run_once"]))
self.polling_interval.set(int(self.config["polling_interval"]))
self.path.set(self.config["stats_path"])
self.runs_to_average.set(int(self.config["num_of_runs_to_average"]))


def browse_path(self):
self.path.set(filedialog.askdirectory(initialdir=self.path.get(), title="test"))

def finished(self):
self.config["stats_path"] = self.path.get()
if self.sheet_id.get().find("docs.google.com") != -1:
full_link = self.sheet_id.get()
id_temp = full_link[full_link.find("/d/") + 3:]
id_temp = id_temp[:id_temp.find("/")]
self.sheet_id.set(id_temp)
self.config["sheet_id"] = self.sheet_id.get()
self.config["calculate_averages"] = (self.calculate_averages.get() == 1)
self.config["run_once"] = (self.run_once.get() == 1)
self.config["num_of_runs_to_average"] = self.runs_to_average.get()
self.config["polling_interval"] = self.polling_interval.get()
with open("config.json", "w") as outfile:
json.dump(self.config, outfile, indent=4)
self.window.destroy()

def main(self):
# Gui for path
path_frame = Frame(self.window)
pre_path_label = Label(path_frame, text="Path: ")
browse_path_button = Button(path_frame, text="Browse", command=self.browse_path)
path_label = Label(path_frame, textvariable=self.path)
pre_path_label.pack(side="left")
path_label.pack(side="left")
browse_path_button.pack(side="right")

# Gui for sheetid
sheet_id_frame = Frame(self.window)
sheet_id_entry = Entry(sheet_id_frame, textvariable=self.sheet_id)
pre_sheet_id_label = Label(sheet_id_frame, text="Progresssheet: ")
pre_sheet_id_label.pack(side="left")
sheet_id_entry.pack(fill="x")

# Gui for advanced options
advanced_padding = 25
advanced_frame = Frame(self.window)
# Calculate Averages
calculate_averages_box = Checkbutton(advanced_frame, variable=self.calculate_averages,
text="Calculate Averages")
calculate_averages_box.pack(side="left", padx=advanced_padding)
# Run once
run_once_box = Checkbutton(advanced_frame, text="Run Once", variable=self.run_once)
run_once_box.pack(side="left", padx=advanced_padding)
# Polling interval
polling_interval_frame = Frame(advanced_frame)
polling_interval_label = Label(polling_interval_frame, text="Polling Interval")
polling_interval_entry = Entry(polling_interval_frame, textvariable=self.polling_interval, width=5)
polling_interval_entry.pack(side="left")
polling_interval_label.pack(side="left")
polling_interval_frame.pack(side="left", padx=advanced_padding)
# Runs to average
runs_to_average_frame = Frame(advanced_frame)
runs_to_average_entry = Entry(runs_to_average_frame, textvariable=self.runs_to_average, width=3)
runs_to_average_label = Label(runs_to_average_frame, text="Number of runs to average")
runs_to_average_entry.pack(side="left")
runs_to_average_label.pack(side="left")
runs_to_average_frame.pack(side="left", padx=advanced_padding)

# Finished button
finished_frame = Frame(self.window)
finished_button = Button(finished_frame, command=self.finished, text="Finish")
finished_button.pack()

# Pack all frames and run mainloop
path_frame.pack(fill="x")
sheet_id_frame.pack(fill="x")
advanced_label = Label(self.window, text="Advanced Settings")
advanced_label.pack()
advanced_frame.pack(fill="x")
finished_frame.pack(fill="x")
self.window.mainloop()


test = Gui()
test.main()
10 changes: 8 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from datetime import datetime
from errors import handle_error
from sheets import create_service, read_sheet_range, validate_sheet_range, write_to_cell
from gui import Gui
import sys


@dataclass
Expand Down Expand Up @@ -146,15 +148,19 @@ def init_versionblacklist():
return blacklist


gui = Gui()
config = json.load(open('config.json', 'r'))
sheet_api = create_service()
blacklist = init_versionblacklist()
scenarios = init_scenario_data(config, sheet_api)
stats = list(sorted(os.listdir(config['stats_path'])))

if config['update_sheet_on_startup']:
if config['run_once']:
print("run_once is active")
update(config, scenarios, stats, blacklist)
time.sleep(max(config['polling_interval'], 30))
print("Finished Updating, program will close in 3 seconds...")
time.sleep(3)
sys.exit()

while True:
new_stats = os.listdir(config['stats_path'])
Expand Down
27 changes: 15 additions & 12 deletions sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ def create_service():
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)

if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', scopes)
creds = flow.run_local_server(port=0)
try:
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', scopes)
creds = flow.run_local_server(port=0)

with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)

with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)

service = build('sheets', 'v4', credentials=creds)
return service.spreadsheets()
service = build('sheets', 'v4', credentials=creds)
return service.spreadsheets()
except HttpError as error:
handle_error('sheets_api', val=error._get_reason())

0 comments on commit 7e35c8d

Please sign in to comment.