zulip_bots/run: Support importing bot modules by path.
The zulip-run-bot script now supports passing in a --path-to-bot option. This allows users to specify the path to the source file for their own custom bots, the first step towards being able to support out-of-tree bots. To run an existing bot in the zulip_bots package, just passing in the name of the bot should suffice.
This commit is contained in:
parent
0625c26c9a
commit
44f63992da
|
@ -7,10 +7,29 @@ import optparse
|
||||||
import sys
|
import sys
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
from os.path import basename, splitext
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
from zulip_bots.lib import run_message_handler_for_bot
|
from zulip_bots.lib import run_message_handler_for_bot
|
||||||
|
|
||||||
|
|
||||||
|
def import_module_from_source(path, name=None):
|
||||||
|
if name is None:
|
||||||
|
name = splitext(basename(path))[0]
|
||||||
|
|
||||||
|
if six.PY2:
|
||||||
|
import imp
|
||||||
|
module = imp.load_source(name, path)
|
||||||
|
return module
|
||||||
|
else:
|
||||||
|
import importlib.util
|
||||||
|
spec = importlib.util.spec_from_file_location(name, path)
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
return module
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
usage = '''
|
usage = '''
|
||||||
zulip-run-bot <bot_name>
|
zulip-run-bot <bot_name>
|
||||||
|
@ -33,12 +52,22 @@ def parse_args():
|
||||||
action='store',
|
action='store',
|
||||||
help='(alternate config file to ~/.zuliprc)')
|
help='(alternate config file to ~/.zuliprc)')
|
||||||
|
|
||||||
|
parser.add_option('--path-to-bot',
|
||||||
|
action='store',
|
||||||
|
help='path to the file with the bot handler class')
|
||||||
|
|
||||||
parser.add_option('--force',
|
parser.add_option('--force',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help='Try running the bot even if dependencies install fails.')
|
help='Try running the bot even if dependencies install fails.')
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
if not args:
|
|
||||||
parser.error('You must specify the name of the bot!')
|
if not args and not options.path_to_bot:
|
||||||
|
error_message = """
|
||||||
|
You must either specify the name of an existing bot or
|
||||||
|
specify a path to the file (--path-to-bot) that contains
|
||||||
|
the bot handler class.
|
||||||
|
"""
|
||||||
|
parser.error(error_message)
|
||||||
|
|
||||||
return (options, args)
|
return (options, args)
|
||||||
|
|
||||||
|
@ -46,9 +75,16 @@ def parse_args():
|
||||||
def main():
|
def main():
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
(options, args) = parse_args()
|
(options, args) = parse_args()
|
||||||
bot_name = args[0]
|
|
||||||
|
|
||||||
lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name))
|
bot_name = None
|
||||||
|
if args:
|
||||||
|
bot_name = args[0]
|
||||||
|
|
||||||
|
if options.path_to_bot:
|
||||||
|
lib_module = import_module_from_source(options.path_to_bot, name=bot_name)
|
||||||
|
else:
|
||||||
|
lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name))
|
||||||
|
|
||||||
if not options.quiet:
|
if not options.quiet:
|
||||||
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue