zulip_bots: Only cache storage.get(). Don't cache put().
MODIFY
zulip_bots/zulip_bots/bots/incrementor/incrementor.py
zulip_bots/zulip_bots/bots/incrementor/incrementor.py
@@ -5,7 +5,6 @@ class IncrementorHandler(object):
META = {
'name': 'Incrementor',
'description': 'Example bot to test the update_message() function.',
- 'uses_storage': True,
}
def usage(self):

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

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

MODIFY
zulip_bots/zulip_bots/lib.py
zulip_bots/zulip_bots/lib.py
@@ -64,14 +64,15 @@ class StateHandler(object):
response = self._client.get_storage()
if response['result'] == 'success':
self.state_ = response['state']
- self._modified_entries = set() # type: Set[Text]
else:
raise StateHandlerError("Error initializing state: {}".format(str(response)))
def put(self, key, value):
# type: (Text, Text) -> None
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):
# type: (Text) -> Text
@@ -81,16 +82,6 @@ class StateHandler(object):
# type: (Text) -> bool
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):
def __init__(self, client, root_dir, bot_details={}):
# type: (Client, str, Dict[str, Any]) -> None
@@ -117,7 +108,7 @@ class ExternalBotHandler(object):
self._client = client
self._root_dir = root_dir
self.bot_details = bot_details
- self._storage = StateHandler(client) if self.bot_details.get('uses_storage', False) else None
+ self._storage = StateHandler(client)
try:
self.user_id = user_profile['user_id']
self.full_name = user_profile['full_name']
@@ -130,15 +121,6 @@ class ExternalBotHandler(object):
@property
def storage(self):
# 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
def send_message(self, message):
@@ -279,7 +261,6 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
message=message,
bot_handler=restricted_client
)
- restricted_client.storage._save()
signal.signal(signal.SIGINT, exit_gracefully)

MODIFY
zulip_bots/zulip_bots/test_lib.py
zulip_bots/zulip_bots/test_lib.py
@@ -47,6 +47,7 @@ class BotTestCaseBase(TestCase):
self.mock_bot_handler = self.MockClass(None, None)
self.mock_client = MagicMock()
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.send_message.return_value = {'id': 42}
self.mock_bot_handler.send_reply.return_value = {'id': 42}

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