69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
import threading
|
|
from typing import Dict
|
|
|
|
from .abstract_plugin import AbstractCommand, AbstractPlugin, AbstractCommandCompiler
|
|
import logging
|
|
from threading import Event
|
|
|
|
|
|
#
|
|
# I have to admit, the abort() solution is not the most robust thing I've ever designed
|
|
# But for this simple scenario, it's good enough
|
|
#
|
|
|
|
class WaitCommand(AbstractCommand):
|
|
|
|
def __init__(self, logger: logging.Logger, shared_event: threading.Event):
|
|
self._shared_event = shared_event
|
|
self._logger = logger
|
|
self._aborted = False
|
|
|
|
def execute(self):
|
|
self._logger.debug(f"Waiting for user interaction...")
|
|
self._aborted = False # Have to reset, because of looping
|
|
self._shared_event.clear()
|
|
self._shared_event.wait()
|
|
|
|
if self._aborted:
|
|
self._logger.warning("Waiting for interaction aborted externally!")
|
|
else:
|
|
self._logger.debug(f"User interaction received!")
|
|
|
|
def abort(self):
|
|
self._aborted = True
|
|
self._shared_event.set() # <- force the event.wait to return
|
|
|
|
def describe(self) -> dict:
|
|
return {
|
|
"command": "wait"
|
|
}
|
|
|
|
|
|
class WaitCompiler(AbstractCommandCompiler):
|
|
|
|
def __init__(self, logger: logging.Logger, shared_event: threading.Event):
|
|
self._logger = logger
|
|
self._shared_event = shared_event
|
|
|
|
def compile(self) -> AbstractCommand:
|
|
return WaitCommand(self._logger, self._shared_event)
|
|
|
|
|
|
class WaitPlugin(AbstractPlugin):
|
|
plugin_name = "wait"
|
|
|
|
def __init__(self):
|
|
self._logger = logging.getLogger("plugin").getChild("wait")
|
|
self._shared_event = threading.Event()
|
|
|
|
def load_compilers(self) -> Dict[str, AbstractCommandCompiler]:
|
|
return {
|
|
"wait": WaitCompiler(self._logger, self._shared_event)
|
|
}
|
|
|
|
def cont(self): # <- magic
|
|
self._shared_event.set()
|
|
|
|
def close(self):
|
|
pass
|