diff --git a/zulip_bots/zulip_bots/finder.py b/zulip_bots/zulip_bots/finder.py new file mode 100644 index 0000000..a79f76b --- /dev/null +++ b/zulip_bots/zulip_bots/finder.py @@ -0,0 +1,23 @@ +import sys +from os.path import basename, splitext +from typing import Any, Optional, Text + +def import_module_from_source(path: Text, name: Optional[Text]=None) -> Any: + if not name: + name = splitext(basename(path))[0] + + # importlib.util.module_from_spec is supported from Python3.5 + py_version = sys.version_info + if py_version.major < 3 or (py_version.major == 3 and py_version.minor < 5): + import imp + module = imp.load_source(name, path) + else: + import importlib.util + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + loader = spec.loader + if loader is None: + return None + loader.exec_module(module) + + return module diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index aa2082d..7a48239 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -6,39 +6,15 @@ import sys import os from os.path import basename, splitext -from typing import Any, Optional, Text - from zulip_bots.lib import ( run_message_handler_for_bot, NoBotConfigException, ) - +from zulip_bots import finder from zulip_bots.provision import provision_bot current_dir = os.path.dirname(os.path.abspath(__file__)) - -def import_module_from_source(path: Text, name: Optional[Text]=None) -> Any: - if not name: - name = splitext(basename(path))[0] - - # importlib.util.module_from_spec is supported from Python3.5 - py_version = sys.version_info - if py_version.major < 3 or (py_version.major == 3 and py_version.minor < 5): - import imp - module = imp.load_source(name, path) - else: - import importlib.util - spec = importlib.util.spec_from_file_location(name, path) - module = importlib.util.module_from_spec(spec) - loader = spec.loader - if loader is None: - return None - loader.exec_module(module) - - return module - - def parse_args() -> argparse.Namespace: usage = ''' zulip-run-bot --config-file ~/zuliprc @@ -115,7 +91,7 @@ def main() -> None: provision_bot(os.path.dirname(bot_path), args.force) try: - lib_module = import_module_from_source(bot_path, bot_name) + lib_module = finder.import_module_from_source(bot_path, bot_name) except ImportError as e: req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt") with open(req_path) as fp: diff --git a/zulip_bots/zulip_bots/terminal.py b/zulip_bots/zulip_bots/terminal.py index 501b6b9..414331b 100644 --- a/zulip_bots/zulip_bots/terminal.py +++ b/zulip_bots/zulip_bots/terminal.py @@ -3,7 +3,7 @@ import os import sys import argparse -from zulip_bots.run import import_module_from_source +from zulip_bots.finder import import_module_from_source from zulip_bots.simple_lib import TerminalBotHandler current_dir = os.path.dirname(os.path.abspath(__file__)) diff --git a/zulip_bots/zulip_bots/tests/test_run.py b/zulip_bots/zulip_bots/tests/test_run.py index 326e99a..3343fe6 100644 --- a/zulip_bots/zulip_bots/tests/test_run.py +++ b/zulip_bots/zulip_bots/tests/test_run.py @@ -59,7 +59,7 @@ class TestDefaultArguments(TestCase): def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier, bot_dir_path): # type: (str, str) -> None with patch('sys.argv', ['zulip-run-bot', bot_qualifier, '--config-file', '/path/to/config']): - with patch('zulip_bots.run.import_module_from_source', return_value=mock.Mock()): + with patch('zulip_bots.finder.import_module_from_source', return_value=mock.Mock()): with patch('zulip_bots.run.run_message_handler_for_bot'): with patch('zulip_bots.run.exit_gracefully_if_zulip_config_file_does_not_exist'): zulip_bots.run.main()