2019-07-05 13:25:59 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from contextlib import suppress
|
|
|
|
|
|
|
|
|
|
|
|
class MessageStorage(ABC):
|
|
|
|
def __init__(self):
|
|
|
|
self._messages = []
|
|
|
|
|
|
|
|
def save_message(self, message):
|
|
|
|
with suppress(KeyError, AttributeError):
|
|
|
|
if self._filter_message(message):
|
|
|
|
self._messages.extend(self._transform_message(message))
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _filter_message(self, message):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _transform_message(self, message): # pylint: disable=no-self-use
|
|
|
|
yield message
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._messages.clear()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def messages(self):
|
|
|
|
yield from self._messages
|
|
|
|
|
|
|
|
|
|
|
|
class FrontendMessageStorage(MessageStorage):
|
|
|
|
def _filter_message(self, message):
|
2019-08-08 13:05:37 +00:00
|
|
|
return message['key'].startswith((
|
|
|
|
'console.write',
|
2019-08-12 09:57:54 +00:00
|
|
|
'message.send',
|
2019-08-08 13:05:37 +00:00
|
|
|
'ide.read'
|
|
|
|
))
|
2019-07-05 13:25:59 +00:00
|
|
|
|
|
|
|
def _transform_message(self, message):
|
2019-08-08 13:05:37 +00:00
|
|
|
transformations = {
|
|
|
|
'ide.read': self._delete_ide_content
|
|
|
|
}
|
|
|
|
if message['key'] in transformations:
|
|
|
|
yield from transformations[message['key']](message)
|
2019-08-09 08:59:27 +00:00
|
|
|
else:
|
|
|
|
yield message
|
2019-08-08 13:05:37 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _delete_ide_content(message):
|
|
|
|
del message['content']
|
|
|
|
yield message
|