bots: Move import_module_from_source to finder.py.
This commit is contained in:
parent
36566c26c1
commit
d053528925
23
zulip_bots/zulip_bots/finder.py
Normal file
23
zulip_bots/zulip_bots/finder.py
Normal file
|
@ -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
|
|
@ -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 <bot_name> --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:
|
||||
|
|
|
@ -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__))
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue