diff --git a/tfw/components/frontend/__init__.py b/tfw/components/frontend/__init__.py index a0bf733..c952158 100644 --- a/tfw/components/frontend/__init__.py +++ b/tfw/components/frontend/__init__.py @@ -3,3 +3,4 @@ from .frontend_proxy_handler import FrontendProxyHandler from .frontend_ready_handler import FrontendReadyHandler from .message_queue_handler import MessageQueueHandler from .message_sender import MessageSender +from .frontend_config_handler import FrontendConfigHandler diff --git a/tfw/components/frontend/frontend_config_handler/__init__.py b/tfw/components/frontend/frontend_config_handler/__init__.py new file mode 100644 index 0000000..86c5b0a --- /dev/null +++ b/tfw/components/frontend/frontend_config_handler/__init__.py @@ -0,0 +1 @@ +from .frontend_config_handler import FrontendConfigHandler diff --git a/tfw/components/frontend/frontend_config_handler/frontend_config_handler.py b/tfw/components/frontend/frontend_config_handler/frontend_config_handler.py new file mode 100644 index 0000000..4c71097 --- /dev/null +++ b/tfw/components/frontend/frontend_config_handler/frontend_config_handler.py @@ -0,0 +1,36 @@ +from yaml import safe_load + +from tfw.internals.networking import Scope + + +class FrontendConfigHandler: + keys = ['frontend.ready'] + + def __init__(self, config_path): + self._config_path = config_path + + def handle_event(self, _, connector): + # pylint: disable=no-self-use + for message in self._config_messages: + connector.send_message(message) + connector.send_message({'key': 'frontend.ready'}, scope=Scope.WEBSOCKET) + + @property + def _config_messages(self): + config = safe_load(self._config_str) + return [ + self._create_config_message(k, v) + for k, v in config.items() + ] + + @property + def _config_str(self): + with open(self._config_path, 'r') as ifile: + return ifile.read() + + @staticmethod + def _create_config_message(key, value): + return { + 'key': f'frontend.{key}', + **value + } diff --git a/tfw/components/frontend/frontend_config_handler/test_frontend_config_handler.py b/tfw/components/frontend/frontend_config_handler/test_frontend_config_handler.py new file mode 100644 index 0000000..007e5f3 --- /dev/null +++ b/tfw/components/frontend/frontend_config_handler/test_frontend_config_handler.py @@ -0,0 +1,40 @@ +from textwrap import dedent + +from .frontend_config_handler import FrontendConfigHandler + + +class MockFrontendConfigHandler(FrontendConfigHandler): + @property + def _config_str(self): + return dedent(''' + cat: + someConfigKey: lel + otherStuff: 42 + foo: + hey: yaaay + ''') + + +class MockConnector: + def __init__(self): + self.messages = [] + + def send_message(self, message, **_): + self.messages.append(message) + + +def test_frontend_config_handler(): + connector = MockConnector() + handler = MockFrontendConfigHandler('') + + handler.handle_event({'key': 'frontend.ready'}, connector) + assert connector.messages[0] == { + 'key': 'frontend.cat', + 'someConfigKey': 'lel', + 'otherStuff': 42 + } + assert connector.messages[1] == { + 'key': 'frontend.foo', + 'hey': 'yaaay' + } + assert connector.messages[-1] == {'key': 'frontend.ready'}