From ff65666ac81e4749a12a525ba2191af657adf87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Sat, 30 Dec 2017 21:40:24 +0100 Subject: [PATCH] Add quit() function to ExternalBotHandler. bot_handler.quit() should be used whenever a bot wishes to terminate. This allows a flexible reaction suited to the bot's environment: For external bots, sys.exit() will be called, whereas for embedded bots, different code can be executed. --- zulip_bots/zulip_bots/lib.py | 4 ++++ zulip_bots/zulip_bots/test_lib.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 243b1ab..c2d2c11 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -210,6 +210,10 @@ class ExternalBotHandler(object): raise PermissionError("Cannot open file \"{}\". Bots may only access " "files in their local directory.".format(abs_filepath)) + def quit(self, message = ""): + # type: (str) -> None + sys.exit(message) + def extract_query_without_mention(message, client): # type: (Dict[str, Any], ExternalBotHandler) -> str """ diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index ae9be4d..64b77f4 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -45,6 +45,13 @@ class StubBotHandler: # type: (Dict[str, Any]) -> None self.message_server.update(message) + class BotQuitException(Exception): + pass + + def quit(self, message = ""): + # type: (str) -> None + raise self.BotQuitException() + def get_config_info(self, bot_name, optional=False): # type: (str, bool) -> Dict[str, Any] return None