reworked engine parent class to impement common things in both the engine

This commit is contained in:
Adam Saudagar 2022-01-31 23:18:51 +05:30
parent b79a7ed076
commit 0f35faa59f

View File

@ -1,19 +1,24 @@
import logging
import traceback
import typing
from abc import ABC, abstractmethod
from threading import Thread
from typing import Callable
from playsound import playsound
from fishy.gui.funcs import GUIFuncsMock
from fishy.helper import helper
if typing.TYPE_CHECKING:
from fishy.gui import GUI
class IEngine(ABC):
class IEngine:
def __init__(self, gui_ref: 'Callable[[], GUI]'):
self.get_gui = gui_ref
self.start = False
# 0 - off, 1 - running, 2 - quitting
self.state = 0
self.window = None
self.thread = None
@ -24,10 +29,35 @@ class IEngine(ABC):
return self.get_gui().funcs
@abstractmethod
def toggle_start(self):
...
if self.state == 1:
self.turn_off()
elif self.state == 0:
self.turn_on()
def turn_on(self):
self.state = 1
playsound(helper.manifest_file("beep.wav"), False)
self.thread = Thread(target=self._crash_safe)
self.thread.start()
def turn_off(self):
"""
this method only signals the thread to close using start flag,
its the responsibility of the thread to shut turn itself off
"""
self.state = 2
helper.playsound_multiple(helper.manifest_file("beep.wav"))
# noinspection PyBroadException
def _crash_safe(self):
self.gui.bot_started(True)
try:
self.run()
except Exception:
traceback.print_exc()
self.state = 0
self.gui.bot_started(False)
@abstractmethod
def run(self):
...
raise NotImplementedError