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