3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

core: implement module loading from user-defined directories

Closes #350.
This commit is contained in:
James Lu 2017-03-08 22:30:32 -08:00
parent 805a0502d2
commit f6d9765f87
4 changed files with 27 additions and 3 deletions

View File

@ -106,6 +106,8 @@ def _rehash():
# TODO: update file loggers here too.
utils.resetModuleDirs()
for network, sdata in new_conf['servers'].items():
# Connect any new networks or disconnected networks if they aren't already.
if (network not in world.networkobjects) or (not world.networkobjects[network].connection_thread.is_alive()):

View File

@ -54,6 +54,11 @@ bot:
# extra service bots.
#spawn_services: true
# Defines extra directories to look up plugins and protocol modules in.
# Environment variables (e.g. $HOME) and home folders (~) are expanded here, in that order.
#plugin_dirs: ["~/my-plugins", "~/pylink-contrib-modules/plugins"]
#protocol_dirs: ["~/pylink-contrib-modules/protocols"]
login:
# NOTE: for users migrating from PyLink < 1.1, the old login:user/login:password settings
# have been deprecated. We strongly recommend migrating to the new "accounts:" block below, as

1
pylink
View File

@ -62,6 +62,7 @@ if __name__ == '__main__':
# Import plugins first globally, because they can listen for events
# that happen before the connection phase.
to_load = conf.conf['plugins']
utils.resetModuleDirs()
# Here, we override the module lookup and import the plugins
# dynamically depending on which were configured.
for plugin in to_load:

View File

@ -14,11 +14,12 @@ import argparse
from .log import log
from . import world, conf
# This is just so protocols and plugins are importable.
# Load the protocol and plugin packages.
from pylinkirc import protocols, plugins
PLUGIN_PREFIX = 'pylinkirc.plugins.'
PROTOCOL_PREFIX = 'pylinkirc.protocols.'
PLUGIN_PREFIX = plugins.__name__ + '.'
PROTOCOL_PREFIX = protocols.__name__ + '.'
NORMALIZEWHITESPACE_RE = re.compile(r'\s+')
class NotAuthorizedError(Exception):
@ -147,6 +148,21 @@ def applyModes(irc, target, changedmodes):
log.warning("(%s) utils.applyModes is deprecated. Use irc.applyModes() instead!", irc.name)
return irc.applyModes(target, changedmodes)
def expandpath(path):
"""
Returns a path expanded with environment variables and home folders (~) expanded, in that order."""
return os.path.expanduser(os.path.expandvars(path))
def resetModuleDirs():
"""
(Re)sets custom protocol module and plugin directories to the ones specified in the config.
"""
# Note: This assumes that the first element of the package path is the default one.
plugins.__path__ = [plugins.__path__[0]] + [expandpath(path) for path in conf.conf['bot'].get('plugin_dirs', [])]
log.debug('resetModuleDirs: new pylinkirc.plugins.__path__: %s', plugins.__path__)
protocols.__path__ = [protocols.__path__[0]] + [expandpath(path) for path in conf.conf['bot'].get('protocol_dirs', [])]
log.debug('resetModuleDirs: new pylinkirc.protocols.__path__: %s', protocols.__path__)
def loadPlugin(name):
"""
Imports and returns the requested plugin.