mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 02:21:32 +00:00
Rename anchor -> key globally ¯\_(ツ)_/¯
This commit is contained in:
parent
654ddacb74
commit
31ba6cb623
@ -24,9 +24,9 @@ class WebideReloadEventHandler(FileSystemEventHandler):
|
|||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
if self._paused: return
|
if self._paused: return
|
||||||
log.debug(event)
|
log.debug(event)
|
||||||
anchor = 'anchor_webide'
|
key = 'webide'
|
||||||
self.uplink.send(anchor, {'anchor': anchor,
|
self.uplink.send(key, {'key': key,
|
||||||
'data': {'command': 'reload'}})
|
'data': {'command': 'reload'}})
|
||||||
|
|
||||||
|
|
||||||
class DirectoryMonitor:
|
class DirectoryMonitor:
|
||||||
|
@ -44,8 +44,8 @@ class FileManager:
|
|||||||
|
|
||||||
|
|
||||||
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
||||||
def __init__(self, anchor, directory, process_name, selected_file=None):
|
def __init__(self, key, directory, process_name, selected_file=None):
|
||||||
super().__init__(anchor)
|
super().__init__(key)
|
||||||
self.filemanager = FileManager(directory, selected_file=selected_file)
|
self.filemanager = FileManager(directory, selected_file=selected_file)
|
||||||
self.process_name = process_name
|
self.process_name = process_name
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
|||||||
data['filename'] = self.filemanager.filename
|
data['filename'] = self.filemanager.filename
|
||||||
data['files'] = self.filemanager.files
|
data['files'] = self.filemanager.files
|
||||||
|
|
||||||
def handle_event(self, anchor, data_json):
|
def handle_event(self, key, data_json):
|
||||||
data = data_json['data']
|
data = data_json['data']
|
||||||
data_json['data'] = self.commands[data['command']](data)
|
data_json['data'] = self.commands[data['command']](data)
|
||||||
self.attach_fileinfo(data)
|
self.attach_fileinfo(data)
|
||||||
|
@ -6,12 +6,12 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
|
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
|
||||||
def __init__(self, anchor, process_name):
|
def __init__(self, key, process_name):
|
||||||
super().__init__(anchor)
|
super().__init__(key)
|
||||||
self.working_directory = tfwenv.TERMINADO_DIR
|
self.working_directory = tfwenv.TERMINADO_DIR
|
||||||
self.process_name = process_name
|
self.process_name = process_name
|
||||||
self.start_process()
|
self.start_process()
|
||||||
|
|
||||||
def handle_event(self, anchor, data_json):
|
def handle_event(self, key, data_json):
|
||||||
log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor))
|
log.debug('TerminadoEventHandler received event for key {}'.format(key))
|
||||||
# TODO: wat do?
|
# TODO: wat do?
|
||||||
|
@ -14,25 +14,25 @@ def cenator():
|
|||||||
|
|
||||||
|
|
||||||
class EventHandlerBase:
|
class EventHandlerBase:
|
||||||
def __init__(self, anchor):
|
def __init__(self, key):
|
||||||
self.server_connector = ServerConnector()
|
self.server_connector = ServerConnector()
|
||||||
self.anchor = anchor
|
self.key = key
|
||||||
self.subscriptions = set()
|
self.subscriptions = set()
|
||||||
self.subscribe(self.anchor)
|
self.subscribe(self.key)
|
||||||
self.subscribe('reset')
|
self.subscribe('reset')
|
||||||
self.server_connector.register_callback(self.event_handler_callback)
|
self.server_connector.register_callback(self.event_handler_callback)
|
||||||
self.cenerator = cycle(cenator())
|
self.cenerator = cycle(cenator())
|
||||||
|
|
||||||
def event_handler_callback(self, msg_parts):
|
def event_handler_callback(self, msg_parts):
|
||||||
anchor, message = deserialize_all(*msg_parts)
|
key, message = deserialize_all(*msg_parts)
|
||||||
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(anchor, message) if anchor != 'reset' else self.handle_reset(message)
|
response = self.handle_event(key, message) if key != 'reset' else self.handle_reset(message)
|
||||||
if response is None: return
|
if response is None: return
|
||||||
self.server_connector.send(anchor, response)
|
self.server_connector.send(key, response)
|
||||||
|
|
||||||
def handle_event(self, anchor, data_json):
|
def handle_event(self, key, data_json):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def handle_reset(self, data_json):
|
def handle_reset(self, data_json):
|
||||||
@ -41,26 +41,26 @@ class EventHandlerBase:
|
|||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def message_other(self, anchor, data):
|
def message_other(self, key, data):
|
||||||
message = {
|
message = {
|
||||||
'anchor': anchor,
|
'key': key,
|
||||||
'data': data
|
'data': data
|
||||||
}
|
}
|
||||||
self.server_connector.send(anchor, message)
|
self.server_connector.send(key, message)
|
||||||
|
|
||||||
def subscribe(self, anchor):
|
def subscribe(self, key):
|
||||||
if anchor not in self.subscriptions:
|
if key not in self.subscriptions:
|
||||||
self.subscriptions.add(anchor)
|
self.subscriptions.add(key)
|
||||||
self.server_connector.subscribe(anchor)
|
self.server_connector.subscribe(key)
|
||||||
|
|
||||||
def unsubscribe(self, anchor):
|
def unsubscribe(self, key):
|
||||||
try:
|
try:
|
||||||
self.subscriptions.remove(anchor)
|
self.subscriptions.remove(key)
|
||||||
self.server_connector.unsubscribe(anchor)
|
self.server_connector.unsubscribe(key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def unsubscribe_all(self):
|
def unsubscribe_all(self):
|
||||||
for sub in self.subscriptions:
|
for sub in self.subscriptions:
|
||||||
self.server_connector.unsubscribe(anchor=sub)
|
self.server_connector.unsubscribe(key=sub)
|
||||||
self.subscriptions.clear()
|
self.subscriptions.clear()
|
||||||
|
@ -4,9 +4,9 @@ from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
|||||||
|
|
||||||
|
|
||||||
class MessageSender:
|
class MessageSender:
|
||||||
def __init__(self, custom_anchor: str = None):
|
def __init__(self, custom_key: str = None):
|
||||||
self.server_connector = ServerUplinkConnector()
|
self.server_connector = ServerUplinkConnector()
|
||||||
self.anchor = custom_anchor or 'message'
|
self.key = custom_key or 'message'
|
||||||
|
|
||||||
def send(self, originator, message):
|
def send(self, originator, message):
|
||||||
data = {
|
data = {
|
||||||
@ -15,7 +15,7 @@ class MessageSender:
|
|||||||
'message': message
|
'message': message
|
||||||
}
|
}
|
||||||
response = {
|
response = {
|
||||||
'anchor': self.anchor,
|
'key': self.key,
|
||||||
'data': data
|
'data': data
|
||||||
}
|
}
|
||||||
self.server_connector.send(self.anchor, response)
|
self.server_connector.send(self.key, response)
|
||||||
|
@ -25,8 +25,8 @@ class ServerUplinkConnector(ZMQConnectorBase):
|
|||||||
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
|
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
|
||||||
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
|
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
|
||||||
|
|
||||||
def send(self, anchor, response):
|
def send(self, key, response):
|
||||||
self._zmq_push_socket.send_multipart(serialize_all(anchor, response))
|
self._zmq_push_socket.send_multipart(serialize_all(key, response))
|
||||||
|
|
||||||
|
|
||||||
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
||||||
|
@ -34,7 +34,7 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
|
|||||||
def register_callback(self, callback):
|
def register_callback(self, callback):
|
||||||
self._zmq_pull_stream.on_recv(callback)
|
self._zmq_pull_stream.on_recv(callback)
|
||||||
|
|
||||||
def send_message(self, message: dict, anchor: str = None):
|
def send_message(self, message: dict, key: str = None):
|
||||||
if not anchor:
|
if not key:
|
||||||
anchor = message['anchor']
|
key = message['key']
|
||||||
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message))
|
self._zmq_pub_socket.send_multipart(serialize_all(key, message))
|
||||||
|
@ -23,7 +23,7 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def zmq_callback(msg_parts):
|
def zmq_callback(msg_parts):
|
||||||
anchor, data = deserialize_all(*msg_parts)
|
key, data = deserialize_all(*msg_parts)
|
||||||
log.debug('Received on pull socket: {}'.format(data))
|
log.debug('Received on pull socket: {}'.format(data))
|
||||||
for instance in ZMQWebSocketHandler.instances:
|
for instance in ZMQWebSocketHandler.instances:
|
||||||
instance.write_message(data)
|
instance.write_message(data)
|
||||||
@ -32,8 +32,8 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
log.debug('Received on WebSocket: {}'.format(message))
|
log.debug('Received on WebSocket: {}'.format(message))
|
||||||
self.send_message(*self.make_response(json.loads(message)))
|
self.send_message(*self.make_response(json.loads(message)))
|
||||||
|
|
||||||
def send_message(self, message: dict, anchor: str = None):
|
def send_message(self, message: dict, key: str = None):
|
||||||
self._event_handler_connector.send_message(message, anchor)
|
self._event_handler_connector.send_message(message, key)
|
||||||
|
|
||||||
# much secure, very cors, wow
|
# much secure, very cors, wow
|
||||||
def check_origin(self, origin):
|
def check_origin(self, origin):
|
||||||
@ -53,10 +53,10 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
|||||||
self._event_handler_connector.send_message(message)
|
self._event_handler_connector.send_message(message)
|
||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
self.fsm.trigger(message['anchor'], message=message)
|
self.fsm.trigger(message['key'], message=message)
|
||||||
anchor = 'FSMUpdate'
|
key = 'FSMUpdate'
|
||||||
response = {'anchor': anchor,
|
response = {'key': key,
|
||||||
'data': {'current_state': self.fsm.state,
|
'data': {'current_state': self.fsm.state,
|
||||||
'valid_transitions':
|
'valid_transitions':
|
||||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
||||||
return response, anchor
|
return response, key
|
||||||
|
@ -6,8 +6,8 @@ from tfw.config import tfwenv
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
eventhandlers = {SourceCodeEventHandler('anchor_webide', tfwenv.WEBIDE_WD, 'login'),
|
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'),
|
||||||
TerminadoEventHandler('anchor_terminado', 'terminado')}
|
TerminadoEventHandler('terminado', 'terminado')}
|
||||||
try:
|
try:
|
||||||
IOLoop.instance().start()
|
IOLoop.instance().start()
|
||||||
finally:
|
finally:
|
||||||
|
@ -12,13 +12,13 @@ class SQLInjectionFSM(FSMBase):
|
|||||||
'end',
|
'end',
|
||||||
]
|
]
|
||||||
transitions = [
|
transitions = [
|
||||||
{'trigger': 'anchor_webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this
|
{'trigger': 'webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this
|
||||||
{'trigger': 'anchor_webide', 'source': 'start', 'dest': 'stripped_code'},
|
{'trigger': 'webide', 'source': 'start', 'dest': 'stripped_code'},
|
||||||
{'trigger': 'anchor_login', 'source': 'stripped_code', 'dest': 'sql'},
|
{'trigger': 'login', 'source': 'stripped_code', 'dest': 'sql'},
|
||||||
{'trigger': 'anchor_logger', 'source': 'sql', 'dest': 'commented_code'},
|
{'trigger': 'logger', 'source': 'sql', 'dest': 'commented_code'},
|
||||||
{'trigger': 'anchor_webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'},
|
{'trigger': 'webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'},
|
||||||
{'trigger': 'anchor_logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'},
|
{'trigger': 'logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'},
|
||||||
{'trigger': 'anchor_logger', 'source': 'sql_output', 'dest': 'end'},
|
{'trigger': 'logger', 'source': 'sql_output', 'dest': 'end'},
|
||||||
{'trigger': 'reset', 'source': 'end', 'dest': 'start'},
|
{'trigger': 'reset', 'source': 'end', 'dest': 'start'},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user