Skip to content

Commit 80d6727

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

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

zulip_bots/zulip_bots/finder.py

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

3333

34-
def import_module_from_zulip_bot_registry(name: str) -> Any:
34+
def import_module_from_zulip_bot_registry(name: str) -> Tuple[str, Any]:
3535
registered_bots = entrypoints.get_group_all("zulip_bots.registry")
3636
matching_bots = [bot for bot in registered_bots if bot.name == name]
3737

3838
if len(matching_bots) == 1: # Unique matching entrypoint
39-
return matching_bots[0].load()
39+
bot = matching_bots[0]
40+
if bot.distro is not None:
41+
return "{}: {}".format(bot.distro.name, bot.distro.version), bot.load()
42+
else:
43+
print(bot)
44+
return "editable package: {}".format(bot.module_name), bot.load()
4045

4146
if len(matching_bots) > 1:
4247
raise DuplicateRegisteredBotName(name)
4348

44-
return None # no matches in registry
49+
return "", None # no matches in registry
4550

4651

4752
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
@@ -132,16 +132,18 @@ def main() -> None:
132132
)
133133
print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list))
134134
sys.exit(1)
135+
bot_source = "source"
135136
else:
136137
lib_module = finder.import_module_by_name(args.bot)
137138
if lib_module:
138139
bot_name = lib_module.__name__
140+
bot_source = "named module"
139141
if args.provision:
140142
print("ERROR: Could not load bot's module for '{}'. Exiting now.")
141143
sys.exit(1)
142144
else:
143145
try:
144-
lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
146+
bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
145147
except finder.DuplicateRegisteredBotName:
146148
print(
147149
"ERROR: Found duplicate entries for bot name in zulip bot registry. Exiting now."
@@ -170,6 +172,7 @@ def main() -> None:
170172
bot_config_file=args.bot_config_file,
171173
quiet=args.quiet,
172174
bot_name=bot_name,
175+
bot_source=bot_source,
173176
)
174177
except NoBotConfigException:
175178
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 = entrypoints.EntryPoint("packaged_bot", "module_name", None)
20+
packaged_bot_distro = entrypoints.Distribution("packaged-bot-source", "1.0.0")
21+
packaged_bot_entrypoint = entrypoints.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)