From 5ef224f2a23b80bce658984171037bc959b45cc8 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 15 May 2018 12:27:55 -0700 Subject: [PATCH] zulip_botserver: Eliminate the bots_config global variable. It's easy to just pass it around to the one function that needs it. --- zulip_botserver/tests/server_test_lib.py | 3 +-- zulip_botserver/zulip_botserver/server.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/zulip_botserver/tests/server_test_lib.py b/zulip_botserver/tests/server_test_lib.py index b649f08..5ae1aa3 100644 --- a/zulip_botserver/tests/server_test_lib.py +++ b/zulip_botserver/tests/server_test_lib.py @@ -21,9 +21,8 @@ class BotServerTestCase(TestCase): ) -> None: if available_bots is not None: server.available_bots = available_bots - server.bots_config = bots_config # type: ignore # monkey-patching server.load_lib_modules() - server.load_bot_handlers() + server.load_bot_handlers(bots_config) response = self.app.post(payload_url, data=json.dumps(message)) diff --git a/zulip_botserver/zulip_botserver/server.py b/zulip_botserver/zulip_botserver/server.py index ed716ba..dc96832 100644 --- a/zulip_botserver/zulip_botserver/server.py +++ b/zulip_botserver/zulip_botserver/server.py @@ -6,31 +6,32 @@ import argparse from flask import Flask, request from importlib import import_module -from typing import Any, Dict, Mapping, Union, List, Optional +from typing import Any, Dict, Union, List, Optional from werkzeug.exceptions import BadRequest from zulip import Client from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.lib import ExternalBotHandler, StateHandler -bots_config = {} # type: Dict[str, Mapping[str, str]] available_bots = [] # type: List[str] bots_lib_module = {} # type: Dict[str, Any] bot_handlers = {} # type: Dict[str, ExternalBotHandler] -def read_config_file(config_file_path: str) -> None: +def read_config_file(config_file_path: str) -> Dict[str, Dict[str, str]]: config_file_path = os.path.abspath(os.path.expanduser(config_file_path)) if not os.path.isfile(config_file_path): raise IOError("Could not read config file {}: File not found.".format(config_file_path)) parser = configparser.ConfigParser() parser.read(config_file_path) + bots_config = {} for section in parser.sections(): bots_config[section] = { "email": parser.get(section, 'email'), "key": parser.get(section, 'key'), "site": parser.get(section, 'site'), } + return bots_config def load_lib_modules() -> None: for bot in available_bots: @@ -44,7 +45,9 @@ def load_lib_modules() -> None: "file correctly.\n".format(bot) ) -def load_bot_handlers() -> Optional[BadRequest]: +def load_bot_handlers( + bots_config: Dict[str, Dict[str, str]], +) -> Optional[BadRequest]: for bot in available_bots: client = Client(email=bots_config[bot]["email"], api_key=bots_config[bot]["key"], @@ -132,11 +135,11 @@ def parse_args() -> argparse.Namespace: def main() -> None: options = parse_args() - read_config_file(options.config_file) + bots_config = read_config_file(options.config_file) global available_bots available_bots = list(bots_config.keys()) load_lib_modules() - load_bot_handlers() + load_bot_handlers(bots_config) app.run(host=options.hostname, port=int(options.port), debug=True) if __name__ == '__main__':