bot_server: Reuse import_module_from_source to load bot modules from paths.

This removes the need to have `load_module_from_file`.
This commit is contained in:
PIG208 2021-07-22 11:31:50 +08:00
parent a87303beb1
commit 4fd29baf2b

View file

@ -1,8 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import configparser import configparser
import importlib.abc
import importlib.util
import json import json
import logging import logging
import os import os
@ -18,6 +16,7 @@ from werkzeug.exceptions import BadRequest, Unauthorized
from zulip import Client from zulip import Client
from zulip_bots import lib from zulip_bots import lib
from zulip_bots.finder import import_module_from_source
from zulip_botserver.input_parameters import parse_args from zulip_botserver.input_parameters import parse_args
@ -112,22 +111,12 @@ def parse_config_file(config_file_path: str) -> configparser.ConfigParser:
return parser 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.
spec = importlib.util.spec_from_file_location("custom_bot_module", file_path)
lib_module = importlib.util.module_from_spec(spec)
assert isinstance(spec.loader, importlib.abc.Loader)
spec.loader.exec_module(lib_module)
return lib_module
def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]: def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]:
bots_lib_module = {} bots_lib_module = {}
for bot in available_bots: for bot in available_bots:
try: try:
if bot.endswith(".py") and os.path.isfile(bot): if bot.endswith(".py") and os.path.isfile(bot):
lib_module = load_module_from_file(bot) lib_module = import_module_from_source(bot, "custom_bot_module")
else: else:
module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot) module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot)
lib_module = import_module(module_name) lib_module = import_module(module_name)