Skip to content

Commit 4fd29ba

Browse files
committed
bot_server: Reuse import_module_from_source to load bot modules from paths.
This removes the need to have `load_module_from_file`.
1 parent a87303b commit 4fd29ba

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

zulip_botserver/zulip_botserver/server.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env python3
22

33
import configparser
4-
import importlib.abc
5-
import importlib.util
64
import json
75
import logging
86
import os
@@ -18,6 +16,7 @@
1816

1917
from zulip import Client
2018
from zulip_bots import lib
19+
from zulip_bots.finder import import_module_from_source
2120
from zulip_botserver.input_parameters import parse_args
2221

2322

@@ -112,22 +111,12 @@ def parse_config_file(config_file_path: str) -> configparser.ConfigParser:
112111
return parser
113112

114113

115-
# TODO: Could we use the function from the bots library for this instead?
116-
def load_module_from_file(file_path: str) -> ModuleType:
117-
# Wrapper around importutil; see https://stackoverflow.com/a/67692/3909240.
118-
spec = importlib.util.spec_from_file_location("custom_bot_module", file_path)
119-
lib_module = importlib.util.module_from_spec(spec)
120-
assert isinstance(spec.loader, importlib.abc.Loader)
121-
spec.loader.exec_module(lib_module)
122-
return lib_module
123-
124-
125114
def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]:
126115
bots_lib_module = {}
127116
for bot in available_bots:
128117
try:
129118
if bot.endswith(".py") and os.path.isfile(bot):
130-
lib_module = load_module_from_file(bot)
119+
lib_module = import_module_from_source(bot, "custom_bot_module")
131120
else:
132121
module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot)
133122
lib_module = import_module(module_name)

0 commit comments

Comments
 (0)