zulip_bots: Only cache storage.get(). Don't cache put().

This commit is contained in:
Robert Hönig 2017-11-24 19:37:13 +01:00 committed by showell
parent c264f7ef12
commit ea6ddb2312
5 changed files with 5 additions and 26 deletions

View file

@ -5,7 +5,6 @@ class IncrementorHandler(object):
META = { META = {
'name': 'Incrementor', 'name': 'Incrementor',
'description': 'Example bot to test the update_message() function.', 'description': 'Example bot to test the update_message() function.',
'uses_storage': True,
} }
def usage(self): def usage(self):

View file

@ -270,7 +270,6 @@ class ticTacToeHandler(object):
META = { META = {
'name': 'TicTacToe', 'name': 'TicTacToe',
'description': 'Lets you play Tic-tac-toe against a computer.', 'description': 'Lets you play Tic-tac-toe against a computer.',
'uses_storage': True,
} }
def usage(self): def usage(self):

View file

@ -7,7 +7,6 @@ class VirtualFsHandler(object):
META = { META = {
'name': 'VirtualFs', 'name': 'VirtualFs',
'description': 'Provides a simple, permanent file system to store and retrieve strings.', 'description': 'Provides a simple, permanent file system to store and retrieve strings.',
'uses_storage': True,
} }
def usage(self): def usage(self):

View file

@ -64,14 +64,15 @@ class StateHandler(object):
response = self._client.get_storage() response = self._client.get_storage()
if response['result'] == 'success': if response['result'] == 'success':
self.state_ = response['state'] self.state_ = response['state']
self._modified_entries = set() # type: Set[Text]
else: else:
raise StateHandlerError("Error initializing state: {}".format(str(response))) raise StateHandlerError("Error initializing state: {}".format(str(response)))
def put(self, key, value): def put(self, key, value):
# type: (Text, Text) -> None # type: (Text, Text) -> None
self.state_[key] = self.marshal(value) self.state_[key] = self.marshal(value)
self._modified_entries.add(key) response = self._client.update_storage({'state': {key: self.state_[key]}})
if response['result'] != 'success':
raise StateHandlerError("Error updating state: {}".format(str(response)))
def get(self, key): def get(self, key):
# type: (Text) -> Text # type: (Text) -> Text
@ -81,16 +82,6 @@ class StateHandler(object):
# type: (Text) -> bool # type: (Text) -> bool
return key in self.state_ return key in self.state_
def _save(self):
# type: () -> None
state_update = {'state': {key: self.state_[key] for key in self._modified_entries}}
if state_update:
response = self._client.update_storage(state_update)
if response['result'] == 'success':
self._modified_entries.clear()
else:
raise StateHandlerError("Error updating state: {}".format(str(response)))
class ExternalBotHandler(object): class ExternalBotHandler(object):
def __init__(self, client, root_dir, bot_details={}): def __init__(self, client, root_dir, bot_details={}):
# type: (Client, str, Dict[str, Any]) -> None # type: (Client, str, Dict[str, Any]) -> None
@ -117,7 +108,7 @@ class ExternalBotHandler(object):
self._client = client self._client = client
self._root_dir = root_dir self._root_dir = root_dir
self.bot_details = bot_details self.bot_details = bot_details
self._storage = StateHandler(client) if self.bot_details.get('uses_storage', False) else None self._storage = StateHandler(client)
try: try:
self.user_id = user_profile['user_id'] self.user_id = user_profile['user_id']
self.full_name = user_profile['full_name'] self.full_name = user_profile['full_name']
@ -130,15 +121,6 @@ class ExternalBotHandler(object):
@property @property
def storage(self): def storage(self):
# type: () -> StateHandler # type: () -> StateHandler
if not self._storage:
raise AttributeError("""Bot tried to access storage, but has not enabled
storage access. To enable storage access, add
META = {
'uses_storage': True,
}
to your bot handler class. Check out the incrementor
bot for an example on how to do this.
""")
return self._storage return self._storage
def send_message(self, message): def send_message(self, message):
@ -279,7 +261,6 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
message=message, message=message,
bot_handler=restricted_client bot_handler=restricted_client
) )
restricted_client.storage._save()
signal.signal(signal.SIGINT, exit_gracefully) signal.signal(signal.SIGINT, exit_gracefully)

View file

@ -47,6 +47,7 @@ class BotTestCaseBase(TestCase):
self.mock_bot_handler = self.MockClass(None, None) self.mock_bot_handler = self.MockClass(None, None)
self.mock_client = MagicMock() self.mock_client = MagicMock()
self.mock_client.get_storage.return_value = {'result': 'success', 'state': {}} self.mock_client.get_storage.return_value = {'result': 'success', 'state': {}}
self.mock_client.update_storage.return_value = {'result': 'success'}
self.mock_bot_handler.storage = StateHandler(self.mock_client) self.mock_bot_handler.storage = StateHandler(self.mock_client)
self.mock_bot_handler.send_message.return_value = {'id': 42} self.mock_bot_handler.send_message.return_value = {'id': 42}
self.mock_bot_handler.send_reply.return_value = {'id': 42} self.mock_bot_handler.send_reply.return_value = {'id': 42}