Merge pull request #153 from fishyboteso/wip/keyboard-interup

handling keyboard interupt and exiting main thread safely
This commit is contained in:
Adam Saudagar 2023-03-07 00:08:25 +05:30 committed by GitHub
commit bc491c8cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 63 deletions

View File

@ -2,10 +2,11 @@ import logging
import sys import sys
import fishy import fishy
from fishy.gui import GUI, splash, update_dialog, check_eula from fishy.gui import GUI, update_dialog, check_eula
from fishy import helper, web from fishy import helper, web
from fishy.engine.common.event_handler import EngineEventHandler from fishy.engine.common.event_handler import EngineEventHandler
from fishy.gui.log_config import GuiLogger from fishy.gui.log_config import GuiLogger
from fishy.gui.splash import Splash
from fishy.helper import hotkey from fishy.helper import hotkey
from fishy.helper.active_poll import active from fishy.helper.active_poll import active
from fishy.helper.config import config from fishy.helper.config import config
@ -39,6 +40,12 @@ def initialize():
helper.install_required_addons() helper.install_required_addons()
def on_gui_load(gui, splash, logger):
splash.finish()
update_dialog.check_update(gui)
logger.connect(gui)
def main(): def main():
print("launching please wait...") print("launching please wait...")
@ -50,34 +57,37 @@ def main():
if not check_eula(): if not check_eula():
return return
finish_splash = splash.start()
logger = GuiLogger()
config.start_backup_scheduler()
active.init()
hotkey.init()
def on_gui_load():
finish_splash()
update_dialog.check_update(gui)
logger.connect(gui)
bot = EngineEventHandler(lambda: gui) bot = EngineEventHandler(lambda: gui)
gui = GUI(lambda: bot, on_gui_load) gui = GUI(lambda: bot, lambda: on_gui_load(gui, splash, logger))
logger = GuiLogger()
hotkey.init()
active.init()
hotkey.start() try:
config.init()
if not check_eula():
return
logging.info(f"Fishybot v{fishy.__version__}") logging.info(f"Fishybot v{fishy.__version__}")
initialize()
gui.start() splash = Splash().start()
active.start() config.start_backup_scheduler()
bot.start_event_handler() # main thread loop initialize()
hotkey.stop() hotkey.start()
active.stop() gui.start()
config.stop() active.start()
bot.stop()
bot.start_event_handler() # main thread loop
except KeyboardInterrupt:
print("caught KeyboardInterrupt, Stopping main thread")
finally:
gui.stop()
hotkey.stop()
active.stop()
config.stop()
bot.stop()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -69,6 +69,9 @@ class GUI:
def create(self): def create(self):
main_gui._create(self) main_gui._create(self)
def stop(self):
self._destroyed = True
def start(self): def start(self):
self._thread.start() self._thread.start()

View File

@ -10,54 +10,54 @@ from fishy.helper import helper
from fishy.helper.config import config from fishy.helper.config import config
def show(win_loc, q): class Splash:
logging.debug("started splash process") def __init__(self):
dim = (300, 200) self.q = Queue()
top = tk.Tk() self.process = Process(name=Splash.__name__, target=self.show, args=(config.get("win_loc"), self.q,))
top.overrideredirect(True) def finish(self):
top.lift() self.q.put("stop")
top.attributes('-topmost', True)
top.title("Loading...") def start(self):
top.resizable(False, False) self.process.start()
top.iconbitmap(helper.manifest_file('icon.ico')) return self
canvas = tk.Canvas(top, width=dim[0], height=dim[1], bg='white') def show(self, win_loc, q):
canvas.pack() logging.debug("started splash process")
top.image = Image.open(helper.manifest_file('fishybot_logo.png')).resize(dim) dim = (300, 200)
top.image = ImageTk.PhotoImage(top.image) top = tk.Tk()
canvas.create_image(0, 0, anchor=tk.NW, image=top.image)
# Position splash at the center of the main window top.overrideredirect(True)
top.lift()
top.attributes('-topmost', True)
default_loc = (str(top.winfo_reqwidth()) + "+" + str(top.winfo_reqheight()) + "+" + "0" + "0") top.title("Loading...")
loc = (win_loc or default_loc).split(":")[-1].split("+")[1:] top.resizable(False, False)
top.geometry("{}x{}+{}+{}".format(dim[0], dim[1], int(loc[0]) + int(dim[0] / 2), int(loc[1]) + int(dim[1] / 2))) top.iconbitmap(helper.manifest_file('icon.ico'))
def waiting(): canvas = tk.Canvas(top, width=dim[0], height=dim[1], bg='white')
q.get() canvas.pack()
time.sleep(0.2) top.image = Image.open(helper.manifest_file('fishybot_logo.png')).resize(dim)
running[0] = False top.image = ImageTk.PhotoImage(top.image)
Thread(target=waiting).start() canvas.create_image(0, 0, anchor=tk.NW, image=top.image)
running = [True] # Position splash at the center of the main window
while running[0]:
top.update()
time.sleep(0.1)
top.destroy() default_loc = (str(top.winfo_reqwidth()) + "+" + str(top.winfo_reqheight()) + "+" + "0" + "0")
logging.debug("ended splash process") loc = (win_loc or default_loc).split(":")[-1].split("+")[1:]
top.geometry("{}x{}+{}+{}".format(dim[0], dim[1], int(loc[0]) + int(dim[0] / 2), int(loc[1]) + int(dim[1] / 2)))
def waiting():
q.get()
time.sleep(0.2)
running[0] = False
def create_finish(q): Thread(target=waiting).start()
def finish():
q.put("stop")
return finish running = [True]
while running[0]:
top.update()
time.sleep(0.1)
top.destroy()
def start(): logging.debug("ended splash process")
q = Queue()
Process(target=show, args=(config.get("win_loc"), q,)).start()
return create_finish(q)

View File

@ -14,12 +14,12 @@ class active:
return return
active._scheduler = EventScheduler() active._scheduler = EventScheduler()
active._scheduler.start()
logging.debug("active scheduler initialized") logging.debug("active scheduler initialized")
@staticmethod @staticmethod
def start(): def start():
web.ping() web.ping()
active._scheduler.start()
active._scheduler.enter_recurring(60, 1, web.ping) active._scheduler.enter_recurring(60, 1, web.ping)
logging.debug("active scheduler started") logging.debug("active scheduler started")