Skip to content

Commit 1df9b05

Browse files
committed
bots: Indicate source of bot (from source/module/registry) upon startup.
Amend tests to include new parameter.
1 parent 50b571e commit 1df9b05

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

zulip_bots/zulip_bots/finder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ def import_module_by_name(name: Text) -> Any:
3636
class DuplicateRegisteredBotName(Exception):
3737
pass
3838

39-
def import_module_from_zulip_bot_registry(name: str) -> Any:
39+
def import_module_from_zulip_bot_registry(name: str) -> Tuple[str, Any]:
4040
registered_bots = entrypoints.get_group_all('zulip_bots.registry')
4141
matching_bots = [bot for bot in registered_bots if bot.name == name]
4242

4343
if len(matching_bots) == 1: # Unique matching entrypoint
44-
return matching_bots[0].load()
44+
bot = matching_bots[0]
45+
return "{}: {}".format(bot.distro.name, bot.distro.version), bot.load()
4546

4647
if len(matching_bots) > 1:
4748
raise DuplicateRegisteredBotName(name)
4849

49-
return None # no matches in registry
50+
return "", None # no matches in registry
5051

5152
def resolve_bot_path(name: Text) -> Optional[Tuple[Text, Text]]:
5253
if os.path.isfile(name):

zulip_bots/zulip_bots/lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def run_message_handler_for_bot(
304304
config_file: str,
305305
bot_config_file: str,
306306
bot_name: str,
307+
bot_source: str,
307308
) -> Any:
308309
"""
309310
lib_module is of type Any, since it can contain any bot's
@@ -334,7 +335,7 @@ def run_message_handler_for_bot(
334335
message_handler = prepare_message_handler(bot_name, restricted_client, lib_module)
335336

336337
if not quiet:
337-
print("Running {} Bot:".format(bot_details['name']))
338+
print("Running {} Bot (from {}):".format(bot_details['name'], bot_source))
338339
if bot_details['description'] != "":
339340
print("\n\t{}".format(bot_details['description']))
340341
print(message_handler.usage())

zulip_bots/zulip_bots/run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,18 @@ def main() -> None:
123123
" zulip-run-bot {bot_name} --provision")
124124
print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list))
125125
sys.exit(1)
126+
bot_source = "source"
126127
else:
127128
lib_module = finder.import_module_by_name(args.bot)
128129
if lib_module:
129130
bot_name = lib_module.__name__
131+
bot_source = "named module"
130132
if args.provision:
131133
print("ERROR: Could not load bot's module for '{}'. Exiting now.")
132134
sys.exit(1)
133135
else:
134136
try:
135-
lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
137+
bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot)
136138
except finder.DuplicateRegisteredBotName as e:
137139
print("ERROR: Found duplicate entries for bot name in zulip bot registry. Exiting now.")
138140
sys.exit(1)
@@ -158,7 +160,8 @@ def main() -> None:
158160
config_file=args.config_file,
159161
bot_config_file=args.bot_config_file,
160162
quiet=args.quiet,
161-
bot_name=bot_name
163+
bot_name=bot_name,
164+
bot_source=bot_source,
162165
)
163166
except NoBotConfigException:
164167
print('''

zulip_bots/zulip_bots/tests/test_lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ def test_message(message, flags):
174174
quiet=True,
175175
config_file=None,
176176
bot_config_file=None,
177-
bot_name='testbot')
177+
bot_name='testbot',
178+
bot_source='bot code location')
178179

179180
def test_upload_file(self):
180181
client, handler = self._create_client_and_handler_for_file_upload()

zulip_bots/zulip_bots/tests/test_run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class TestDefaultArguments(TestCase):
1616

1717
our_dir = os.path.dirname(__file__)
1818
path_to_bot = os.path.abspath(os.path.join(our_dir, '../bots/giphy/giphy.py'))
19-
packaged_bot_entrypoint = entrypoints.EntryPoint("packaged_bot", "module_name", None)
19+
packaged_bot_distro = entrypoints.Distribution("packaged-bot-source", "1.0.0")
20+
packaged_bot_entrypoint = entrypoints.EntryPoint("packaged_bot", "module_name", None, distro=packaged_bot_distro)
2021

2122
@patch('sys.argv', ['zulip-run-bot', 'giphy', '--config-file', '/foo/bar/baz.conf'])
2223
@patch('zulip_bots.run.run_message_handler_for_bot')
@@ -28,6 +29,7 @@ def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot:
2829
config_file='/foo/bar/baz.conf',
2930
bot_config_file=None,
3031
lib_module=mock.ANY,
32+
bot_source='source',
3133
quiet=False)
3234

3335
@patch('sys.argv', ['zulip-run-bot', path_to_bot, '--config-file', '/foo/bar/baz.conf'])
@@ -41,6 +43,7 @@ def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot:
4143
config_file='/foo/bar/baz.conf',
4244
bot_config_file=None,
4345
lib_module=mock.ANY,
46+
bot_source='source',
4447
quiet=False)
4548

4649
@patch('sys.argv', ['zulip-run-bot', 'packaged_bot', '--config-file', '/foo/bar/baz.conf'])
@@ -56,6 +59,7 @@ def test_argument_parsing_with_zulip_bot_registry(self, mock_run_message_handler
5659
config_file='/foo/bar/baz.conf',
5760
bot_config_file=None,
5861
lib_module=mock.ANY,
62+
bot_source='packaged-bot-source: 1.0.0',
5963
quiet=False)
6064

6165
def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self) -> None:

0 commit comments

Comments
 (0)