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 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.engine.common.event_handler import EngineEventHandler
from fishy.gui.log_config import GuiLogger
from fishy.gui.splash import Splash
from fishy.helper import hotkey
from fishy.helper.active_poll import active
from fishy.helper.config import config
@ -39,6 +40,12 @@ def initialize():
helper.install_required_addons()
def on_gui_load(gui, splash, logger):
splash.finish()
update_dialog.check_update(gui)
logger.connect(gui)
def main():
print("launching please wait...")
@ -50,34 +57,37 @@ def main():
if not check_eula():
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)
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__}")
initialize()
logging.info(f"Fishybot v{fishy.__version__}")
gui.start()
active.start()
splash = Splash().start()
config.start_backup_scheduler()
bot.start_event_handler() # main thread loop
initialize()
hotkey.stop()
active.stop()
config.stop()
bot.stop()
hotkey.start()
gui.start()
active.start()
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__":

View File

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

View File

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

View File

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