From ea6ddb2312b557020e77152efb73f6514105732e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Fri, 24 Nov 2017 19:37:13 +0100 Subject: [PATCH] zulip_bots: Only cache storage.get(). Don't cache put(). --- .../bots/incrementor/incrementor.py | 1 - .../zulip_bots/bots/tictactoe/tictactoe.py | 1 - .../zulip_bots/bots/virtual_fs/virtual_fs.py | 1 - zulip_bots/zulip_bots/lib.py | 27 +++---------------- zulip_bots/zulip_bots/test_lib.py | 1 + 5 files changed, 5 insertions(+), 26 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py b/zulip_bots/zulip_bots/bots/incrementor/incrementor.py index 461379d..dbdcb3d 100644 --- a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py +++ b/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): diff --git a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py b/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py index 7c58f4d..d9c70a4 100644 --- a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py +++ b/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): diff --git a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py index b6be581..22bacfe 100644 --- a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py +++ b/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): diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index cf289da..b7a2720 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/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) diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 0f5ca58..e6d877a 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/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}