From 5ca9ec0771fcfb2ad956780f88914b8562088192 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 23 Jul 2017 13:18:50 -0700 Subject: [PATCH] bots: Switch VirtualFS to use state_handler.state() contextmanager. --- .../zulip_bots/bots/virtual_fs/virtual_fs.py | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) 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 8cc5d76..f85cbd2 100644 --- a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py +++ b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py @@ -11,26 +11,23 @@ class VirtualFsHandler(object): command = message['content'] if command == "": command = "help" - sender = message['sender_email'] - state = state_handler.get_state() - if state is None: - state = {} + sender = message['sender_email'] recipient = message['display_recipient'] if isinstance(recipient, list): # If not a stream, then hash on list of emails recipient = " ".join([x['email'] for x in recipient]) - if recipient not in state: - state[recipient] = fs_new() - fs = state[recipient] - if sender not in fs['user_paths']: - fs['user_paths'][sender] = '/' - fs, msg = fs_command(fs, sender, command) - prependix = '{}:\n'.format(sender) - msg = prependix + msg - state[recipient] = fs - state_handler.set_state(state) + with state_handler.state({}) as state: + if recipient not in state: + state[recipient] = fs_new() + fs = state[recipient] + if sender not in fs['user_paths']: + fs['user_paths'][sender] = '/' + fs, msg = fs_command(fs, sender, command) + prependix = '{}:\n'.format(sender) + msg = prependix + msg + state[recipient] = fs bot_handler.send_reply(message, msg)