Implement triggered and triggerless EventHandler base classes

This commit is contained in:
Kristóf Tóth 2018-02-23 14:07:41 +01:00
parent fd1cb100ad
commit 940e74d6da
3 changed files with 27 additions and 5 deletions

View File

@ -2,7 +2,7 @@ from os.path import isfile, join, relpath
from glob import glob from glob import glob
from tfw.components.mixins import SupervisorMixin from tfw.components.mixins import SupervisorMixin
from tfw.event_handler_base import EventHandlerBase from tfw.event_handler_base import TriggerlessEventHandler
from tfw.components.directory_monitor import DirectoryMonitor from tfw.components.directory_monitor import DirectoryMonitor
from tfw.config.logs import logging from tfw.config.logs import logging
@ -43,7 +43,7 @@ class FileManager:
return relpath(self._filepath(filename), start=self._workdir) return relpath(self._filepath(filename), start=self._workdir)
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
def __init__(self, key, directory, process_name, selected_file=None): def __init__(self, key, directory, process_name, selected_file=None):
super().__init__(key) super().__init__(key)
self.filemanager = FileManager(directory, selected_file=selected_file) self.filemanager = FileManager(directory, selected_file=selected_file)

View File

@ -1,11 +1,11 @@
from tfw.event_handler_base import EventHandlerBase from tfw.event_handler_base import TriggerlessEventHandler
from tfw.components.mixins import SupervisorMixin from tfw.components.mixins import SupervisorMixin
from tfw.config import tfwenv from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin):
def __init__(self, key, process_name): def __init__(self, key, process_name):
super().__init__(key) super().__init__(key)
self.working_directory = tfwenv.TERMINADO_DIR self.working_directory = tfwenv.TERMINADO_DIR

View File

@ -28,10 +28,17 @@ class EventHandlerBase:
from .message_sender import MessageSender from .message_sender import MessageSender
ms = MessageSender() ms = MessageSender()
ms.send('JOHN CENA', next(self.cenerator)) ms.send('JOHN CENA', next(self.cenerator))
response = self.handle_event(key, message) if key != 'reset' else self.handle_reset(message) response = self.dispatch_handling(key, message)
if response is None: return if response is None: return
self.server_connector.send(key, response) self.server_connector.send(key, response)
def dispatch_handling(self, key, message):
raise NotImplementedError
def _dispatch_handling(self, key, message):
if key != 'reset': return self.handle_event(key, message)
else: return self.handle_reset(message)
def handle_event(self, key, data_json): def handle_event(self, key, data_json):
raise NotImplementedError raise NotImplementedError
@ -64,3 +71,18 @@ class EventHandlerBase:
for sub in self.subscriptions: for sub in self.subscriptions:
self.server_connector.unsubscribe(key=sub) self.server_connector.unsubscribe(key=sub)
self.subscriptions.clear() self.subscriptions.clear()
class TriggerlessEventHandler(EventHandlerBase):
def dispatch_handling(self, key, message):
return self._dispatch_handling(key, message)
class TriggeredEventHandler(EventHandlerBase):
def __init__(self, key, trigger):
super().__init__(key)
self.trigger = trigger
def dispatch_handling(self, key, message):
if message.get('trigger') == self.trigger:
return self._dispatch_handling(key, message)