black: Reformat skipping string normalization.
This commit is contained in:
parent
5580c68ae5
commit
fba21bb00d
178 changed files with 6562 additions and 4469 deletions
|
@ -54,6 +54,7 @@ setuptools_info = dict(
|
|||
|
||||
try:
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
package_info.update(setuptools_info)
|
||||
package_info['packages'] = find_packages(exclude=['tests'])
|
||||
|
||||
|
@ -67,11 +68,13 @@ except ImportError:
|
|||
try:
|
||||
module = import_module(module_name) # type: Any
|
||||
if version is not None:
|
||||
assert(LooseVersion(module.__version__) >= LooseVersion(version))
|
||||
assert LooseVersion(module.__version__) >= LooseVersion(version)
|
||||
except (ImportError, AssertionError):
|
||||
if version is not None:
|
||||
print("{name}>={version} is not installed.".format(
|
||||
name=module_name, version=version), file=sys.stderr)
|
||||
print(
|
||||
"{name}>={version} is not installed.".format(name=module_name, version=version),
|
||||
file=sys.stderr,
|
||||
)
|
||||
else:
|
||||
print("{name} is not installed.".format(name=module_name), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
|
|
@ -9,7 +9,6 @@ from zulip_botserver import server
|
|||
|
||||
|
||||
class BotServerTestCase(TestCase):
|
||||
|
||||
def setUp(self) -> None:
|
||||
server.app.testing = True
|
||||
self.app = server.app.test_client()
|
||||
|
@ -31,8 +30,12 @@ class BotServerTestCase(TestCase):
|
|||
bots_lib_modules = server.load_lib_modules(available_bots)
|
||||
server.app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
||||
if bot_handlers is None:
|
||||
bot_handlers = server.load_bot_handlers(available_bots, bots_config, third_party_bot_conf)
|
||||
message_handlers = server.init_message_handlers(available_bots, bots_lib_modules, bot_handlers)
|
||||
bot_handlers = server.load_bot_handlers(
|
||||
available_bots, bots_config, third_party_bot_conf
|
||||
)
|
||||
message_handlers = server.init_message_handlers(
|
||||
available_bots, bots_lib_modules, bot_handlers
|
||||
)
|
||||
server.app.config["BOT_HANDLERS"] = bot_handlers
|
||||
server.app.config["MESSAGE_HANDLERS"] = message_handlers
|
||||
|
||||
|
|
|
@ -35,14 +35,18 @@ class BotServerTests(BotServerTestCase):
|
|||
'token': 'abcd1234',
|
||||
}
|
||||
}
|
||||
self.assert_bot_server_response(available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234'),
|
||||
expected_response="beep boop",
|
||||
check_success=True)
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
),
|
||||
expected_response="beep boop",
|
||||
check_success=True,
|
||||
)
|
||||
|
||||
def test_successful_request_from_two_bots(self) -> None:
|
||||
available_bots = ['helloworld', 'help']
|
||||
|
@ -58,16 +62,20 @@ class BotServerTests(BotServerTestCase):
|
|||
'key': '123456789qwertyuiop',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
}
|
||||
},
|
||||
}
|
||||
self.assert_bot_server_response(available_bots=available_bots,
|
||||
event=dict(message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234'),
|
||||
expected_response="beep boop",
|
||||
bots_config=bots_config,
|
||||
check_success=True)
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
),
|
||||
expected_response="beep boop",
|
||||
bots_config=bots_config,
|
||||
check_success=True,
|
||||
)
|
||||
|
||||
def test_request_for_unkown_bot(self) -> None:
|
||||
bots_config = {
|
||||
|
@ -78,11 +86,12 @@ class BotServerTests(BotServerTestCase):
|
|||
'token': 'abcd1234',
|
||||
},
|
||||
}
|
||||
self.assert_bot_server_response(available_bots=['helloworld'],
|
||||
event=dict(message={'content': "test message"},
|
||||
bot_email='unknown-bot@zulip.com'),
|
||||
bots_config=bots_config,
|
||||
check_success=False)
|
||||
self.assert_bot_server_response(
|
||||
available_bots=['helloworld'],
|
||||
event=dict(message={'content': "test message"}, bot_email='unknown-bot@zulip.com'),
|
||||
bots_config=bots_config,
|
||||
check_success=False,
|
||||
)
|
||||
|
||||
def test_wrong_bot_token(self) -> None:
|
||||
available_bots = ['helloworld']
|
||||
|
@ -94,17 +103,23 @@ class BotServerTests(BotServerTestCase):
|
|||
'token': 'abcd1234',
|
||||
}
|
||||
}
|
||||
self.assert_bot_server_response(available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='wrongtoken'),
|
||||
check_success=False)
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
bots_config=bots_config,
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='wrongtoken',
|
||||
),
|
||||
check_success=False,
|
||||
)
|
||||
|
||||
@mock.patch('logging.error')
|
||||
@mock.patch('zulip_bots.lib.StateHandler')
|
||||
def test_wrong_bot_credentials(self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock) -> None:
|
||||
def test_wrong_bot_credentials(
|
||||
self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock
|
||||
) -> None:
|
||||
available_bots = ['nonexistent-bot']
|
||||
bots_config = {
|
||||
'nonexistent-bot': {
|
||||
|
@ -117,16 +132,21 @@ class BotServerTests(BotServerTestCase):
|
|||
# This works, but mypy still complains:
|
||||
# error: No overload variant of "assertRaisesRegexp" of "TestCase" matches argument types
|
||||
# [def (*args: builtins.object, **kwargs: builtins.object) -> builtins.SystemExit, builtins.str]
|
||||
with self.assertRaisesRegexp(SystemExit,
|
||||
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
|
||||
'sure you have set up the botserverrc file correctly.'):
|
||||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
|
||||
'sure you have set up the botserverrc file correctly.',
|
||||
):
|
||||
self.assert_bot_server_response(
|
||||
available_bots=available_bots,
|
||||
event=dict(message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234'),
|
||||
bots_config=bots_config)
|
||||
event=dict(
|
||||
message={'content': "@**test** test message"},
|
||||
bot_email='helloworld-bot@zulip.com',
|
||||
trigger='mention',
|
||||
token='abcd1234',
|
||||
),
|
||||
bots_config=bots_config,
|
||||
)
|
||||
|
||||
@mock.patch('sys.argv', ['zulip-botserver', '--config-file', '/foo/bar/baz.conf'])
|
||||
def test_argument_parsing_defaults(self) -> None:
|
||||
|
@ -163,7 +183,9 @@ class BotServerTests(BotServerTestCase):
|
|||
assert server.read_config_from_env_vars("giphy") == {'giphy': bots_config['giphy']}
|
||||
|
||||
# Specified bot doesn't exist; should read the first section of the config.
|
||||
assert server.read_config_from_env_vars("redefined_bot") == {'redefined_bot': bots_config['hello_world']}
|
||||
assert server.read_config_from_env_vars("redefined_bot") == {
|
||||
'redefined_bot': bots_config['hello_world']
|
||||
}
|
||||
|
||||
def test_read_config_file(self) -> None:
|
||||
with self.assertRaises(IOError):
|
||||
|
@ -184,7 +206,7 @@ class BotServerTests(BotServerTestCase):
|
|||
'key': 'value2',
|
||||
'site': 'http://localhost',
|
||||
'token': 'abcd1234',
|
||||
}
|
||||
},
|
||||
}
|
||||
assert json.dumps(bot_conf1, sort_keys=True) == json.dumps(expected_config1, sort_keys=True)
|
||||
|
||||
|
@ -223,24 +245,35 @@ class BotServerTests(BotServerTestCase):
|
|||
assert module == helloworld
|
||||
|
||||
# load valid file path
|
||||
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')).as_posix()
|
||||
path = Path(
|
||||
root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')
|
||||
).as_posix()
|
||||
module = server.load_lib_modules([path])[path]
|
||||
assert module.__name__ == 'custom_bot_module'
|
||||
assert module.__file__ == path
|
||||
assert isinstance(module, ModuleType)
|
||||
|
||||
# load invalid module name
|
||||
with self.assertRaisesRegexp(SystemExit,
|
||||
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.'):
|
||||
module = server.load_lib_modules(['botserver-test-case-random-bot'])['botserver-test-case-random-bot']
|
||||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "botserver-test-case-random-bot" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.',
|
||||
):
|
||||
module = server.load_lib_modules(['botserver-test-case-random-bot'])[
|
||||
'botserver-test-case-random-bot'
|
||||
]
|
||||
|
||||
# load invalid file path
|
||||
with self.assertRaisesRegexp(SystemExit,
|
||||
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir)):
|
||||
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')).as_posix()
|
||||
with self.assertRaisesRegexp(
|
||||
SystemExit,
|
||||
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
||||
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir),
|
||||
):
|
||||
path = Path(
|
||||
root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')
|
||||
).as_posix()
|
||||
module = server.load_lib_modules([path])[path]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -10,40 +10,43 @@ def parse_args() -> argparse.Namespace:
|
|||
mutually_exclusive_args = parser.add_mutually_exclusive_group(required=True)
|
||||
# config-file or use-env-vars made mutually exclusive to prevent conflicts
|
||||
mutually_exclusive_args.add_argument(
|
||||
'--config-file', '-c',
|
||||
'--config-file',
|
||||
'-c',
|
||||
action='store',
|
||||
help='Config file for the Botserver. Use your `botserverrc` for multiple bots or'
|
||||
'`zuliprc` for a single bot.'
|
||||
'`zuliprc` for a single bot.',
|
||||
)
|
||||
mutually_exclusive_args.add_argument(
|
||||
'--use-env-vars', '-e',
|
||||
'--use-env-vars',
|
||||
'-e',
|
||||
action='store_true',
|
||||
help='Load configuration from JSON in ZULIP_BOTSERVER_CONFIG environment variable.'
|
||||
help='Load configuration from JSON in ZULIP_BOTSERVER_CONFIG environment variable.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--bot-config-file',
|
||||
action='store',
|
||||
default=None,
|
||||
help='Config file for bots. Only needed when one of '
|
||||
'the bots you want to run requires a config file.'
|
||||
'the bots you want to run requires a config file.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--bot-name', '-b',
|
||||
'--bot-name',
|
||||
'-b',
|
||||
action='store',
|
||||
help='Run a single bot BOT_NAME. Use this option to run the Botserver '
|
||||
'with a `zuliprc` config file.'
|
||||
'with a `zuliprc` config file.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hostname',
|
||||
action='store',
|
||||
default="127.0.0.1",
|
||||
help='Address on which you want to run the Botserver. (default: %(default)s)'
|
||||
help='Address on which you want to run the Botserver. (default: %(default)s)',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--port',
|
||||
action='store',
|
||||
default=5002,
|
||||
type=int,
|
||||
help='Port on which you want to run the Botserver. (default: %(default)d)'
|
||||
help='Port on which you want to run the Botserver. (default: %(default)d)',
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
|
|
@ -30,12 +30,15 @@ def read_config_section(parser: configparser.ConfigParser, section: str) -> Dict
|
|||
}
|
||||
return section_info
|
||||
|
||||
|
||||
def read_config_from_env_vars(bot_name: Optional[str] = None) -> Dict[str, Dict[str, str]]:
|
||||
bots_config = {} # type: Dict[str, Dict[str, str]]
|
||||
json_config = os.environ.get('ZULIP_BOTSERVER_CONFIG')
|
||||
|
||||
if json_config is None:
|
||||
raise OSError("Could not read environment variable 'ZULIP_BOTSERVER_CONFIG': Variable not set.")
|
||||
raise OSError(
|
||||
"Could not read environment variable 'ZULIP_BOTSERVER_CONFIG': Variable not set."
|
||||
)
|
||||
|
||||
# Load JSON-formatted environment variable; use OrderedDict to
|
||||
# preserve ordering on Python 3.6 and below.
|
||||
|
@ -51,26 +54,34 @@ def read_config_from_env_vars(bot_name: Optional[str] = None) -> Dict[str, Dict[
|
|||
first_bot_name = list(env_config.keys())[0]
|
||||
bots_config[bot_name] = env_config[first_bot_name]
|
||||
logging.warning(
|
||||
"First bot name in the config list was changed from '{}' to '{}'".format(first_bot_name, bot_name)
|
||||
"First bot name in the config list was changed from '{}' to '{}'".format(
|
||||
first_bot_name, bot_name
|
||||
)
|
||||
)
|
||||
else:
|
||||
bots_config = dict(env_config)
|
||||
return bots_config
|
||||
|
||||
def read_config_file(config_file_path: str, bot_name: Optional[str] = None) -> Dict[str, Dict[str, str]]:
|
||||
|
||||
def read_config_file(
|
||||
config_file_path: str, bot_name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, str]]:
|
||||
parser = parse_config_file(config_file_path)
|
||||
|
||||
bots_config = {} # type: Dict[str, Dict[str, str]]
|
||||
if bot_name is None:
|
||||
bots_config = {section: read_config_section(parser, section)
|
||||
for section in parser.sections()}
|
||||
bots_config = {
|
||||
section: read_config_section(parser, section) for section in parser.sections()
|
||||
}
|
||||
return bots_config
|
||||
|
||||
logging.warning("Single bot mode is enabled")
|
||||
if len(parser.sections()) == 0:
|
||||
sys.exit("Error: Your Botserver config file `{0}` does not contain any sections!\n"
|
||||
"You need to write the name of the bot you want to run in the "
|
||||
"section header of `{0}`.".format(config_file_path))
|
||||
sys.exit(
|
||||
"Error: Your Botserver config file `{0}` does not contain any sections!\n"
|
||||
"You need to write the name of the bot you want to run in the "
|
||||
"section header of `{0}`.".format(config_file_path)
|
||||
)
|
||||
|
||||
if bot_name in parser.sections():
|
||||
bot_section = bot_name
|
||||
|
@ -80,7 +91,9 @@ def read_config_file(config_file_path: str, bot_name: Optional[str] = None) -> D
|
|||
bot_section = parser.sections()[0]
|
||||
bots_config[bot_name] = read_config_section(parser, bot_section)
|
||||
logging.warning(
|
||||
"First bot name in the config list was changed from '{}' to '{}'".format(bot_section, bot_name)
|
||||
"First bot name in the config list was changed from '{}' to '{}'".format(
|
||||
bot_section, bot_name
|
||||
)
|
||||
)
|
||||
ignored_sections = parser.sections()[1:]
|
||||
|
||||
|
@ -98,6 +111,7 @@ def parse_config_file(config_file_path: str) -> configparser.ConfigParser:
|
|||
parser.read(config_file_path)
|
||||
return parser
|
||||
|
||||
|
||||
# TODO: Could we use the function from the bots library for this instead?
|
||||
def load_module_from_file(file_path: str) -> ModuleType:
|
||||
# Wrapper around importutil; see https://stackoverflow.com/a/67692/3909240.
|
||||
|
@ -107,6 +121,7 @@ def load_module_from_file(file_path: str) -> ModuleType:
|
|||
spec.loader.exec_module(lib_module)
|
||||
return lib_module
|
||||
|
||||
|
||||
def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]:
|
||||
bots_lib_module = {}
|
||||
for bot in available_bots:
|
||||
|
@ -118,10 +133,14 @@ def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]:
|
|||
lib_module = import_module(module_name)
|
||||
bots_lib_module[bot] = lib_module
|
||||
except ImportError:
|
||||
error_message = ("Error: Bot \"{}\" doesn't exist. Please make sure "
|
||||
"you have set up the botserverrc file correctly.\n".format(bot))
|
||||
error_message = (
|
||||
"Error: Bot \"{}\" doesn't exist. Please make sure "
|
||||
"you have set up the botserverrc file correctly.\n".format(bot)
|
||||
)
|
||||
if bot == "api":
|
||||
error_message += "Did you forget to specify the bot you want to run with -b <botname> ?"
|
||||
error_message += (
|
||||
"Did you forget to specify the bot you want to run with -b <botname> ?"
|
||||
)
|
||||
sys.exit(error_message)
|
||||
return bots_lib_module
|
||||
|
||||
|
@ -133,15 +152,14 @@ def load_bot_handlers(
|
|||
) -> Dict[str, lib.ExternalBotHandler]:
|
||||
bot_handlers = {}
|
||||
for bot in available_bots:
|
||||
client = Client(email=bots_config[bot]["email"],
|
||||
api_key=bots_config[bot]["key"],
|
||||
site=bots_config[bot]["site"])
|
||||
client = Client(
|
||||
email=bots_config[bot]["email"],
|
||||
api_key=bots_config[bot]["key"],
|
||||
site=bots_config[bot]["site"],
|
||||
)
|
||||
bot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bots', bot)
|
||||
bot_handler = lib.ExternalBotHandler(
|
||||
client,
|
||||
bot_dir,
|
||||
bot_details={},
|
||||
bot_config_parser=third_party_bot_conf
|
||||
client, bot_dir, bot_details={}, bot_config_parser=third_party_bot_conf
|
||||
)
|
||||
|
||||
bot_handlers[bot] = bot_handler
|
||||
|
@ -176,13 +194,17 @@ def handle_bot() -> str:
|
|||
bot_config = config
|
||||
break
|
||||
else:
|
||||
raise BadRequest("Cannot find a bot with email {} in the Botserver "
|
||||
"configuration file. Do the emails in your botserverrc "
|
||||
"match the bot emails on the server?".format(event['bot_email']))
|
||||
raise BadRequest(
|
||||
"Cannot find a bot with email {} in the Botserver "
|
||||
"configuration file. Do the emails in your botserverrc "
|
||||
"match the bot emails on the server?".format(event['bot_email'])
|
||||
)
|
||||
if bot_config['token'] != event['token']:
|
||||
raise Unauthorized("Request token does not match token found for bot {} in the "
|
||||
"Botserver configuration file. Do the outgoing webhooks in "
|
||||
"Zulip point to the right Botserver?".format(event['bot_email']))
|
||||
raise Unauthorized(
|
||||
"Request token does not match token found for bot {} in the "
|
||||
"Botserver configuration file. Do the outgoing webhooks in "
|
||||
"Zulip point to the right Botserver?".format(event['bot_email'])
|
||||
)
|
||||
app.config.get("BOTS_LIB_MODULES", {})[bot]
|
||||
bot_handler = app.config.get("BOT_HANDLERS", {})[bot]
|
||||
message_handler = app.config.get("MESSAGE_HANDLERS", {})[bot]
|
||||
|
@ -213,17 +235,24 @@ def main() -> None:
|
|||
try:
|
||||
bots_config = read_config_file(options.config_file, options.bot_name)
|
||||
except MissingSectionHeaderError:
|
||||
sys.exit("Error: Your Botserver config file `{0}` contains an empty section header!\n"
|
||||
"You need to write the names of the bots you want to run in the "
|
||||
"section headers of `{0}`.".format(options.config_file))
|
||||
sys.exit(
|
||||
"Error: Your Botserver config file `{0}` contains an empty section header!\n"
|
||||
"You need to write the names of the bots you want to run in the "
|
||||
"section headers of `{0}`.".format(options.config_file)
|
||||
)
|
||||
except NoOptionError as e:
|
||||
sys.exit("Error: Your Botserver config file `{0}` has a missing option `{1}` in section `{2}`!\n"
|
||||
"You need to add option `{1}` with appropriate value in section `{2}` of `{0}`"
|
||||
.format(options.config_file, e.option, e.section))
|
||||
sys.exit(
|
||||
"Error: Your Botserver config file `{0}` has a missing option `{1}` in section `{2}`!\n"
|
||||
"You need to add option `{1}` with appropriate value in section `{2}` of `{0}`".format(
|
||||
options.config_file, e.option, e.section
|
||||
)
|
||||
)
|
||||
|
||||
available_bots = list(bots_config.keys())
|
||||
bots_lib_modules = load_lib_modules(available_bots)
|
||||
third_party_bot_conf = parse_config_file(options.bot_config_file) if options.bot_config_file is not None else None
|
||||
third_party_bot_conf = (
|
||||
parse_config_file(options.bot_config_file) if options.bot_config_file is not None else None
|
||||
)
|
||||
bot_handlers = load_bot_handlers(available_bots, bots_config, third_party_bot_conf)
|
||||
message_handlers = init_message_handlers(available_bots, bots_lib_modules, bot_handlers)
|
||||
app.config["BOTS_LIB_MODULES"] = bots_lib_modules
|
||||
|
@ -231,5 +260,6 @@ def main() -> None:
|
|||
app.config["MESSAGE_HANDLERS"] = message_handlers
|
||||
app.run(host=options.hostname, port=int(options.port), debug=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue