mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 07:01:33 +00:00
Project-wide refactor of things named Component* to EventHandler*
This commit is contained in:
parent
acaf8899cc
commit
62a12c605c
@ -27,8 +27,8 @@ ENV PATH="$PYENV_ROOT/bin:$PATH"
|
|||||||
ENV TFW_APP_DIR="/srv/app"
|
ENV TFW_APP_DIR="/srv/app"
|
||||||
COPY src/app ${TFW_APP_DIR}
|
COPY src/app ${TFW_APP_DIR}
|
||||||
|
|
||||||
ENV TFW_COMPONENTS_DIR="/opt/components"
|
ENV TFW_EVENT_HANDLERS_DIR="/opt/event_handlers"
|
||||||
COPY src/components ${TFW_COMPONENTS_DIR}
|
COPY src/event_handlers ${TFW_EVENT_HANDLERS_DIR}
|
||||||
|
|
||||||
# Copy config
|
# Copy config
|
||||||
ENV TFW_CONFIG_DIR="/usr/local/lib/"
|
ENV TFW_CONFIG_DIR="/usr/local/lib/"
|
||||||
|
@ -31,6 +31,6 @@ PYTHONPATH="../../lib/" python app.py
|
|||||||
in one, and
|
in one, and
|
||||||
```
|
```
|
||||||
cd src/components/
|
cd src/components/
|
||||||
PYTHONPATH="../../lib/" python component_example.py
|
PYTHONPATH="../../lib/" python event_handler_example.py
|
||||||
```
|
```
|
||||||
in the other.
|
in the other.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from transitions import Machine
|
from transitions import Machine
|
||||||
|
|
||||||
import component_connector
|
import event_handler_connector
|
||||||
|
|
||||||
|
|
||||||
class Buttons:
|
class Buttons:
|
||||||
@ -22,7 +22,7 @@ class Buttons:
|
|||||||
|
|
||||||
def forward_message(self, event_data):
|
def forward_message(self, event_data):
|
||||||
message = event_data.kwargs.get('message')
|
message = event_data.kwargs.get('message')
|
||||||
component_connector.send_message(message)
|
event_handler_connector.send_message(message)
|
||||||
|
|
||||||
|
|
||||||
fsm = Buttons()
|
fsm = Buttons()
|
@ -5,7 +5,7 @@ from util import parse_anchor_from_message
|
|||||||
|
|
||||||
# from buttons import fsm
|
# from buttons import fsm
|
||||||
from sql_injection_fsm import fsm
|
from sql_injection_fsm import fsm
|
||||||
import component_connector
|
import event_handler_connector
|
||||||
|
|
||||||
|
|
||||||
class ZMQWebSocketHandler(WebSocketHandler):
|
class ZMQWebSocketHandler(WebSocketHandler):
|
||||||
@ -18,14 +18,14 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
logging.debug('Received on pull socket: {}'.format(data.decode()))
|
logging.debug('Received on pull socket: {}'.format(data.decode()))
|
||||||
self.write_message(data.decode())
|
self.write_message(data.decode())
|
||||||
|
|
||||||
component_connector.register_callback(zmq_callback)
|
event_handler_connector.register_callback(zmq_callback)
|
||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
logging.debug('Received on WebSocket: {}'.format(message))
|
logging.debug('Received on WebSocket: {}'.format(message))
|
||||||
fsm.trigger(parse_anchor_from_message(message), message=message)
|
fsm.trigger(parse_anchor_from_message(message), message=message)
|
||||||
|
|
||||||
def send_message(self, message: str, anchor: str = None):
|
def send_message(self, message: str, anchor: str = None):
|
||||||
component_connector.send_message(message, anchor)
|
event_handler_connector.send_message(message, anchor)
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
pass
|
pass
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from transitions import Machine
|
from transitions import Machine
|
||||||
|
|
||||||
import component_connector
|
import event_handler_connector
|
||||||
|
|
||||||
|
|
||||||
class SQLInjectionFSM:
|
class SQLInjectionFSM:
|
||||||
@ -26,7 +26,7 @@ class SQLInjectionFSM:
|
|||||||
|
|
||||||
def forward_message(self, event_data):
|
def forward_message(self, event_data):
|
||||||
message = event_data.kwargs.get('message')
|
message = event_data.kwargs.get('message')
|
||||||
component_connector.send_message(message)
|
event_handler_connector.send_message(message)
|
||||||
|
|
||||||
|
|
||||||
fsm = SQLInjectionFSM()
|
fsm = SQLInjectionFSM()
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from component_base import ComponentBase
|
from event_handler_base import EventHandlerBase
|
||||||
|
|
||||||
|
|
||||||
class Component(ComponentBase):
|
class EventHandler(EventHandlerBase):
|
||||||
def __init__(self, anchor, event_handler, zmq_context=None):
|
def __init__(self, anchor, event_handler_function, zmq_context=None):
|
||||||
super().__init__(anchor, event_handler, zmq_context)
|
super().__init__(anchor, event_handler_function, zmq_context)
|
||||||
|
|
||||||
def wrapper(msg_parts, handler):
|
def wrapper(msg_parts, handler):
|
||||||
anchor, message = msg_parts
|
anchor, message = msg_parts
|
||||||
@ -15,6 +15,4 @@ class Component(ComponentBase):
|
|||||||
encoded_response = json.dumps(response).encode('utf-8')
|
encoded_response = json.dumps(response).encode('utf-8')
|
||||||
self.zmq_push_socket.send_multipart([anchor, encoded_response])
|
self.zmq_push_socket.send_multipart([anchor, encoded_response])
|
||||||
|
|
||||||
self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler))
|
self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler_function))
|
||||||
|
|
||||||
|
|
@ -8,10 +8,10 @@ from config import PUBLISHER_PORT, RECEIVER_PORT
|
|||||||
ioloop.install()
|
ioloop.install()
|
||||||
|
|
||||||
|
|
||||||
class ComponentBase:
|
class EventHandlerBase:
|
||||||
def __init__(self, anchor, event_handler, zmq_context=None):
|
def __init__(self, anchor, event_handler_function, zmq_context=None):
|
||||||
self.anchor = anchor
|
self.anchor = anchor
|
||||||
self.event_handler = event_handler
|
self.event_handler_function = event_handler_function
|
||||||
self.zmq_context = zmq_context or zmq.Context.instance()
|
self.zmq_context = zmq_context or zmq.Context.instance()
|
||||||
self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB)
|
self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB)
|
||||||
self.subscriptions = {self.anchor}
|
self.subscriptions = {self.anchor}
|
@ -2,8 +2,8 @@ import codecs
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
import source_code
|
import source_code
|
||||||
from component import Component
|
from event_handler import EventHandler
|
||||||
from stateful_component import StatefulComponent
|
from stateful_event_handler import StatefulEventHandler
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
|
|
||||||
from login_component import authorize_login
|
from login_component import authorize_login
|
||||||
@ -70,17 +70,17 @@ def login_handler(data, component):
|
|||||||
yield ('anchor_login', '# Login page\n' + response)
|
yield ('anchor_login', '# Login page\n' + response)
|
||||||
|
|
||||||
|
|
||||||
def source_code_handler(data, component):
|
def source_code_handler(data, event_handler):
|
||||||
component.unsubscribe(data['anchor'])
|
event_handler.unsubscribe(data['anchor'])
|
||||||
yield (data['anchor'],
|
yield (data['anchor'],
|
||||||
source_code.get_source_code(authorize_login, strip_comments=True))
|
source_code.get_source_code(authorize_login, strip_comments=True))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
anchor_a = Component('anchor_a', change_case_handler)
|
anchor_a = EventHandler('anchor_a', change_case_handler)
|
||||||
anchor_b = Component('anchor_b', rot13_handler)
|
anchor_b = EventHandler('anchor_b', rot13_handler)
|
||||||
anchor_c = Component('anchor_c', reverse_handler)
|
anchor_c = EventHandler('anchor_c', reverse_handler)
|
||||||
anchor_login = StatefulComponent('anchor_login', login_handler)
|
anchor_login = StatefulEventHandler('anchor_login', login_handler)
|
||||||
anchor_webide = StatefulComponent('anchor_webide', source_code_handler)
|
anchor_webide = StatefulEventHandler('anchor_webide', source_code_handler)
|
||||||
IOLoop.instance().start()
|
IOLoop.instance().start()
|
||||||
|
|
@ -3,10 +3,10 @@ from functools import partial
|
|||||||
|
|
||||||
import zmq
|
import zmq
|
||||||
|
|
||||||
from component_base import ComponentBase
|
from event_handler_base import EventHandlerBase
|
||||||
|
|
||||||
|
|
||||||
class StatefulComponent(ComponentBase):
|
class StatefulEventHandler(EventHandlerBase):
|
||||||
def __init__(self, anchor, event_handler, zmq_context=None):
|
def __init__(self, anchor, event_handler, zmq_context=None):
|
||||||
super().__init__(anchor, event_handler, zmq_context)
|
super().__init__(anchor, event_handler, zmq_context)
|
||||||
self.generator = None
|
self.generator = None
|
@ -17,7 +17,7 @@ serverurl=unix:///tmp/supervisor.sock
|
|||||||
directory=%(ENV_TFW_APP_DIR)s
|
directory=%(ENV_TFW_APP_DIR)s
|
||||||
command=env python app.py
|
command=env python app.py
|
||||||
|
|
||||||
[program:component_example]
|
[program:event_handler_example]
|
||||||
directory=%(ENV_TFW_COMPONENTS_DIR)s
|
directory=%(ENV_TFW_EVENT_HANDLERS_DIR)s
|
||||||
command=env python component_example.py
|
command=env python event_handler_example.py
|
||||||
autorestart=true
|
autorestart=true
|
||||||
|
Loading…
Reference in New Issue
Block a user