zulip_botserver: Eliminate the bots_config global variable.

It's easy to just pass it around to the one function that needs it.
This commit is contained in:
Tim Abbott 2018-05-15 12:27:55 -07:00
parent 1e5c0fec1f
commit 5ef224f2a2
2 changed files with 10 additions and 8 deletions

View file

@ -21,9 +21,8 @@ class BotServerTestCase(TestCase):
) -> None: ) -> None:
if available_bots is not None: if available_bots is not None:
server.available_bots = available_bots server.available_bots = available_bots
server.bots_config = bots_config # type: ignore # monkey-patching
server.load_lib_modules() server.load_lib_modules()
server.load_bot_handlers() server.load_bot_handlers(bots_config)
response = self.app.post(payload_url, data=json.dumps(message)) response = self.app.post(payload_url, data=json.dumps(message))

View file

@ -6,31 +6,32 @@ import argparse
from flask import Flask, request from flask import Flask, request
from importlib import import_module 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 werkzeug.exceptions import BadRequest
from zulip import Client from zulip import Client
from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.custom_exceptions import ConfigValidationError
from zulip_bots.lib import ExternalBotHandler, StateHandler from zulip_bots.lib import ExternalBotHandler, StateHandler
bots_config = {} # type: Dict[str, Mapping[str, str]]
available_bots = [] # type: List[str] available_bots = [] # type: List[str]
bots_lib_module = {} # type: Dict[str, Any] bots_lib_module = {} # type: Dict[str, Any]
bot_handlers = {} # type: Dict[str, ExternalBotHandler] 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)) config_file_path = os.path.abspath(os.path.expanduser(config_file_path))
if not os.path.isfile(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)) raise IOError("Could not read config file {}: File not found.".format(config_file_path))
parser = configparser.ConfigParser() parser = configparser.ConfigParser()
parser.read(config_file_path) parser.read(config_file_path)
bots_config = {}
for section in parser.sections(): for section in parser.sections():
bots_config[section] = { bots_config[section] = {
"email": parser.get(section, 'email'), "email": parser.get(section, 'email'),
"key": parser.get(section, 'key'), "key": parser.get(section, 'key'),
"site": parser.get(section, 'site'), "site": parser.get(section, 'site'),
} }
return bots_config
def load_lib_modules() -> None: def load_lib_modules() -> None:
for bot in available_bots: for bot in available_bots:
@ -44,7 +45,9 @@ def load_lib_modules() -> None:
"file correctly.\n".format(bot) "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: for bot in available_bots:
client = Client(email=bots_config[bot]["email"], client = Client(email=bots_config[bot]["email"],
api_key=bots_config[bot]["key"], api_key=bots_config[bot]["key"],
@ -132,11 +135,11 @@ def parse_args() -> argparse.Namespace:
def main() -> None: def main() -> None:
options = parse_args() options = parse_args()
read_config_file(options.config_file) bots_config = read_config_file(options.config_file)
global available_bots global available_bots
available_bots = list(bots_config.keys()) available_bots = list(bots_config.keys())
load_lib_modules() load_lib_modules()
load_bot_handlers() load_bot_handlers(bots_config)
app.run(host=options.hostname, port=int(options.port), debug=True) app.run(host=options.hostname, port=int(options.port), debug=True)
if __name__ == '__main__': if __name__ == '__main__':