mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 08:51:32 +00:00
Implement builtin handler for configuring frontend
This commit is contained in:
parent
ffc06a64ee
commit
bb5c000b1d
@ -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
|
||||
|
@ -0,0 +1 @@
|
||||
from .frontend_config_handler import FrontendConfigHandler
|
@ -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
|
||||
}
|
@ -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'}
|
Loading…
Reference in New Issue
Block a user