Skip to content

Commit a788f50

Browse files
neiljpPIG208
authored andcommitted
bots: Indicate source of bot (from source/module/registry) upon startup.
Amend tests to include new parameter.
1 parent a232d12 commit a788f50

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

zulip_bots/zulip_bots/finder.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ class DuplicateRegisteredBotName(Exception):
3030
pass
3131

3232

33-
def import_module_from_zulip_bot_registry(name: str) -> Any:
33+
def import_module_from_zulip_bot_registry(name: str) -> Tuple[str, Any]:
3434
registered_bots = importlib.metadata.entry_points()["zulip_bots.registry"]
3535
matching_bots = [bot for bot in registered_bots if bot.name == name]
3636

3737
if len(matching_bots) == 1: # Unique matching entrypoint
38-
return matching_bots[0].load()
38+
"""We expect external bots to be registered using entry_points in the
39+
group "zulip_bots.registry", where the name of the entry point should
40+
match the name of the module containing the bot handler and the value
41+
of it should be the package containing the bot handler module.
42+
43+
"""
44+
bot = matching_bots[0]
45+
bot_name = bot.name
46+
bot_module = bot.load()
47+
bot_version = bot_module.__version__
48+
49+
if bot_version is not None:
50+
return f"{bot_name}: {bot_version}", bot_module
51+
else:
52+
return f"editable package: {bot_name}", bot_module
3953

4054
if len(matching_bots) > 1:
4155
raise DuplicateRegisteredBotName(name)
4256

43-
return None # no matches in registry
57+
return "", None # no matches in registry
4458

4559

4660
def resolve_bot_path(name: str) -> Optional[Tuple[Path, str]]:

zulip_bots/zulip_bots/lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ def run_message_handler_for_bot(
443443
config_file: str,
444444
bot_config_file: str,
445445
bot_name: str,
446+
bot_source: str,
446447
) -> Any:
447448
"""
448449
lib_module is of type Any, since it can contain any bot's
@@ -473,7 +474,7 @@ def run_message_handler_for_bot(
473474
message_handler = prepare_message_handler(bot_name, restricted_client, lib_module)
474475

475476
if not quiet:
476-
print("Running {} Bot:".format(bot_details["name"]))
477+
print("Running {} Bot (from {}):".format(bot_details["name"], bot_source))
477478
if bot_details["description"] != "":
478479
print("\n\t{}".format(bot_details["description"]))
479480
if hasattr(message_handler, "usage"):

zulip_bots/zulip_bots/run.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def main() -> None:
118118

119119
if args.registry:
120120
try:
121-
lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
121+
bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
122122
except finder.DuplicateRegisteredBotName:
123123
print("ERROR: Found duplicate entries for bot name in zulip bot registry. Exiting now.")
124124
sys.exit(1)
@@ -148,10 +148,12 @@ def main() -> None:
148148
)
149149
print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list))
150150
sys.exit(1)
151+
bot_source = "source"
151152
else:
152153
lib_module = finder.import_module_by_name(args.bot)
153154
if lib_module:
154155
bot_name = lib_module.__name__
156+
bot_source = "named module"
155157
if args.provision:
156158
print("ERROR: Could not load bot's module for '{}'. Exiting now.")
157159
sys.exit(1)
@@ -176,6 +178,7 @@ def main() -> None:
176178
bot_config_file=args.bot_config_file,
177179
quiet=args.quiet,
178180
bot_name=bot_name,
181+
bot_source=bot_source,
179182
)
180183
except NoBotConfigException:
181184
print(

zulip_bots/zulip_bots/tests/test_lib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def test_message(message, flags):
197197
config_file=None,
198198
bot_config_file=None,
199199
bot_name="testbot",
200+
bot_source="bot code location",
200201
)
201202

202203
def test_upload_file(self):

zulip_bots/zulip_bots/tests/test_run.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class TestDefaultArguments(TestCase):
1717

1818
our_dir = os.path.dirname(__file__)
1919
path_to_bot = os.path.abspath(os.path.join(our_dir, "../bots/giphy/giphy.py"))
20-
packaged_bot_entrypoint = EntryPoint("packaged_bot", "module_name", None)
20+
packaged_bot_distro = Distribution("packaged-bot-source", "1.0.0")
21+
packaged_bot_entrypoint = EntryPoint(
22+
"packaged_bot", "module_name", None, distro=packaged_bot_distro
23+
)
2124

2225
@patch("sys.argv", ["zulip-run-bot", "giphy", "--config-file", "/foo/bar/baz.conf"])
2326
@patch("zulip_bots.run.run_message_handler_for_bot")
@@ -32,6 +35,7 @@ def test_argument_parsing_with_bot_name(
3235
config_file="/foo/bar/baz.conf",
3336
bot_config_file=None,
3437
lib_module=mock.ANY,
38+
bot_source="source",
3539
quiet=False,
3640
)
3741

@@ -48,6 +52,7 @@ def test_argument_parsing_with_bot_path(
4852
config_file="/foo/bar/baz.conf",
4953
bot_config_file=None,
5054
lib_module=mock.ANY,
55+
bot_source="source",
5156
quiet=False,
5257
)
5358

@@ -69,6 +74,7 @@ def test_argument_parsing_with_zulip_bot_registry(
6974
config_file="/foo/bar/baz.conf",
7075
bot_config_file=None,
7176
lib_module=mock.ANY,
77+
bot_source="packaged-bot-source: 1.0.0",
7278
quiet=False,
7379
)
7480

0 commit comments

Comments
 (0)