zulip_bots: Rename state_handler to storage.

This commit is contained in:
derAnfaenger 2017-10-23 12:24:41 +02:00
parent eb6982e670
commit e331426c64
7 changed files with 21 additions and 21 deletions

View file

@ -12,7 +12,7 @@ class IncrementorHandler(object):
''' '''
def handle_message(self, message, bot_handler): def handle_message(self, message, bot_handler):
with bot_handler.state_handler.state({'number': 0, 'message_id': None}) as state: with bot_handler.storage.state({'number': 0, 'message_id': None}) as state:
state['number'] += 1 state['number'] += 1
if state['message_id'] is None: if state['message_id'] is None:
result = bot_handler.send_reply(message, str(state['number'])) result = bot_handler.send_reply(message, str(state['number']))

View file

@ -23,11 +23,11 @@ class TestIncrementorBot(BotTestCase):
'sender_email': 'foo_sender@zulip.com', 'sender_email': 'foo_sender@zulip.com',
}, },
] ]
state_handler = StateHandler() storage = StateHandler()
self.assert_bot_response(dict(messages[0], content=""), {'content': "1"}, self.assert_bot_response(dict(messages[0], content=""), {'content': "1"},
'send_reply', state_handler) 'send_reply', storage)
# Last test commented out since we don't have update_message # Last test commented out since we don't have update_message
# support in the test framework yet. # support in the test framework yet.
# self.assert_bot_response(dict(messages[0], content=""), {'message_id': 5, 'content': "2"}, # self.assert_bot_response(dict(messages[0], content=""), {'message_id': 5, 'content': "2"},
# 'update_message', state_handler) # 'update_message', storage)

View file

@ -281,7 +281,7 @@ class ticTacToeHandler(object):
command += val command += val
original_sender = message['sender_email'] original_sender = message['sender_email']
with bot_handler.state_handler.state({}) as mydict: with bot_handler.storage.state({}) as mydict:
user_game = mydict.get(original_sender) user_game = mydict.get(original_sender)
if (not user_game) and command == "new": if (not user_game) and command == "new":
user_game = TicTacToeGame(copy.deepcopy(initial_board)) user_game = TicTacToeGame(copy.deepcopy(initial_board))

View file

@ -45,5 +45,5 @@ class TestVirtualFsBot(BotTestCase):
("cd /home", "foo_sender@zulip.com:\nCurrent path: /home/"), ("cd /home", "foo_sender@zulip.com:\nCurrent path: /home/"),
] ]
state_handler = StateHandler() storage = StateHandler()
self.check_expected_responses(expected, state_handler = state_handler) self.check_expected_responses(expected, storage = storage)

View file

@ -18,7 +18,7 @@ class VirtualFsHandler(object):
if isinstance(recipient, list): # If not a stream, then hash on list of emails if isinstance(recipient, list): # If not a stream, then hash on list of emails
recipient = " ".join([x['email'] for x in recipient]) recipient = " ".join([x['email'] for x in recipient])
with bot_handler.state_handler.state({}) as state: with bot_handler.storage.state({}) as state:
if recipient not in state: if recipient not in state:
state[recipient] = fs_new() state[recipient] = fs_new()
fs = state[recipient] fs = state[recipient]

View file

@ -79,7 +79,7 @@ class ExternalBotHandler(object):
self._rate_limit = RateLimit(20, 5) self._rate_limit = RateLimit(20, 5)
self._client = client self._client = client
self._root_dir = root_dir self._root_dir = root_dir
self.state_handler = StateHandler() self.storage = StateHandler()
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']

View file

@ -56,7 +56,7 @@ class BotTestCase(TestCase):
def check_expected_responses(self, expectations, expected_method='send_reply', def check_expected_responses(self, expectations, expected_method='send_reply',
email="foo_sender@zulip.com", recipient="foo", subject="foo", email="foo_sender@zulip.com", recipient="foo", subject="foo",
sender_id=0, sender_full_name="Foo Bar", type="all", sender_id=0, sender_full_name="Foo Bar", type="all",
state_handler=None): storage=None):
# type: (Union[Sequence[Tuple[str, Any]], Dict[str, Any]], str, str, str, str, int, str, str, Optional[StateHandler]) -> None # type: (Union[Sequence[Tuple[str, Any]], Dict[str, Any]], str, str, str, str, int, str, str, Optional[StateHandler]) -> None
# To test send_message, Any would be a Dict type, # To test send_message, Any would be a Dict type,
# to test send_reply, Any would be a str type. # to test send_reply, Any would be a str type.
@ -82,12 +82,12 @@ class BotTestCase(TestCase):
trigger_messages.append(stream_message_dict) trigger_messages.append(stream_message_dict)
for trigger_message in trigger_messages: for trigger_message in trigger_messages:
if state_handler is None: if storage is None:
current_state_handler = None current_storage = None
else: else:
# A new (copy of) the state_handler is used for each trigger message. # A new (copy of) the StateHandler is used for each trigger message.
# This avoids type="all" failing if state is created in the first iteration. # This avoids type="all" failing if state is created in the first iteration.
current_state_handler = deepcopy(state_handler) current_storage = deepcopy(storage)
for m, r in expected: for m, r in expected:
# For calls with send_reply, r is a string (the content of a message), # For calls with send_reply, r is a string (the content of a message),
@ -97,15 +97,15 @@ class BotTestCase(TestCase):
response = {'content': r} if expected_method == 'send_reply' else r response = {'content': r} if expected_method == 'send_reply' else r
self.assert_bot_response(message=message, response=response, self.assert_bot_response(message=message, response=response,
expected_method=expected_method, expected_method=expected_method,
state_handler=current_state_handler) storage=current_storage)
def call_request(self, message, expected_method, response, state_handler): def call_request(self, message, expected_method, response, storage):
# type: (Dict[str, Any], str, Dict[str, Any], Optional[StateHandler]) -> None # type: (Dict[str, Any], str, Dict[str, Any], Optional[StateHandler]) -> None
if state_handler is None: if storage is None:
state_handler = StateHandler() storage = StateHandler()
# Send message to the concerned bot # Send message to the concerned bot
mock_bot_handler = self.MockClass(None, None) mock_bot_handler = self.MockClass(None, None)
mock_bot_handler.state_handler = state_handler mock_bot_handler.storage = storage
self.message_handler.handle_message(message, mock_bot_handler) self.message_handler.handle_message(message, mock_bot_handler)
# Check if the bot is sending a message via `send_message` function. # Check if the bot is sending a message via `send_message` function.
@ -159,8 +159,8 @@ class BotTestCase(TestCase):
else: else:
mock_get.assert_called_with(http_request['api_url']) mock_get.assert_called_with(http_request['api_url'])
def assert_bot_response(self, message, response, expected_method, state_handler = None): def assert_bot_response(self, message, response, expected_method, storage = None):
# type: (Dict[str, Any], Dict[str, Any], str, Optional[StateHandler]) -> None # type: (Dict[str, Any], Dict[str, Any], str, Optional[StateHandler]) -> None
# Strictly speaking, this function is not needed anymore, # Strictly speaking, this function is not needed anymore,
# kept for now for legacy reasons. # kept for now for legacy reasons.
self.call_request(message, expected_method, response, state_handler) self.call_request(message, expected_method, response, storage)