This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.py
130 lines (105 loc) · 3.99 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
# gui.py
# by Pon Pon
# Provide GUI functionality to the script. Note that much of this code was taken
# from https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Multithreaded_Logging.py
import logging
import threading
from queue import Queue, Empty
import PySimpleGUIQt as sg
import helper_functions
from main import main, addMission, removeMission, getMissions
class ThreadedApp(threading.Thread):
def __init__(self):
super().__init__()
self.daemon = True
self.kill_queue = Queue()
self._stop_event = threading.Event()
def run(self):
# Note: In production, we want the system to mask any exceptions instead of crashing.
try:
main()
except Exception as e:
logging.error("Error: {}".format(e))
raise # Reraise the error to prevent masking
def stop(self):
self._stop_event.set()
class QueueHandler(logging.Handler):
def __init__(self, log_queue):
super().__init__()
self.log_queue = log_queue
def emit(self, record):
self.log_queue.put(record)
def updateMissions(updateWindows):
i = 0
missionList = ""
missionArray = []
for mission in getMissions():
i = i+1
missionList += str(i) +". " + str(mission[1] + "\n")
missionArray.append(mission[1])
if updateWindows:
window["missionList"].update(missionList)
window["removalDropdown"].update(values=missionArray)
return [missionList, missionArray]
if __name__ == "__main__":
try:
helper_functions.module_setup()
except NotImplementedError as e:
logging.error(e)
output = sg.Output(key="output")
layout = [
[sg.Button("Start")],
[sg.Button("Quit")],
[sg.Text("Add missions to check for:")],
[sg.InputText("Mission detection text...", key="missionDetection"), sg.InputText("Mission label...", key="missionType"), sg.Button("Add mission", size=[25,1])],
[sg.Text(updateMissions(False)[0], key="missionList")],
[sg.Combo(values=updateMissions(False)[1], key="removalDropdown"), sg.Button("Remove mission", size=[25,1])],
[output]
]
window = sg.Window(title="Baking Bot", layout=layout, size=(900,300))
# Setup logging and start app
logger = logging.getLogger()
log_queue = Queue()
queue_handler = QueueHandler(log_queue)
queue_handler.setLevel(level=logging.INFO)
logger.addHandler(queue_handler)
threadedApp = ThreadedApp()
appStarted = False
# Mainloop
while True:
event, values = window.read(timeout=100)
if event == "Start":
if not appStarted:
logging.info("Starting script")
threadedApp.start()
window["Start"].update(disabled=True)
appStarted = True
if event in ["Quit", sg.WIN_CLOSED]:
if appStarted:
logging.info("Stopping script")
threadedApp.stop()
appStarted = False
break
if event == "Add mission":
logging.info("Added {} to the list".format(values["missionType"]))
#Add mission to list (missions_needed) in main.py
addMission(values["missionDetection"].upper(), values["missionType"])
#Update list in gui
updateMissions(True)
if event == "Remove mission":
for mission in getMissions():
if mission[1] == values["removalDropdown"]:
#Remove mission from list
removeMission(mission)
#Update GUI after removing mission check
updateMissions(True)
logging.info("Removed {} from the list".format(mission[1]))
# Poll queue
try:
record = log_queue.get(block=False)
except Empty:
pass
else:
msg = queue_handler.format(record)
output.write(msg.strip("\r\n")+'\n')
window.close()