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

pylink: use sys.path instead of imp library hacks

This commit is contained in:
James Lu 2015-09-27 10:44:52 -07:00
parent a903f97507
commit cf2ba4b492

22
pylink
View File

@ -4,9 +4,12 @@ import imp
import os import os
import sys import sys
# This must be done before conf imports, so we get the real conf instead of testing one. # cd into the PyLink root directory first...
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
import world import world
# This must be set before conf imports, so we get the real conf instead of testing one.
world.testing = False world.testing = False
import conf import conf
@ -19,7 +22,7 @@ if __name__ == '__main__':
if conf.conf['login']['password'] == 'changeme': if conf.conf['login']['password'] == 'changeme':
log.critical("You have not set the login details correctly! Exiting...") log.critical("You have not set the login details correctly! Exiting...")
sys.exit(2) sys.exit(2)
protocols_folder = [os.path.join(os.getcwd(), 'protocols')] protocols_folder = os.path.join(os.getcwd(), 'protocols')
# Write a PID file. # Write a PID file.
with open('%s.pid' % conf.confname, 'w') as f: with open('%s.pid' % conf.confname, 'w') as f:
@ -28,14 +31,11 @@ if __name__ == '__main__':
# Import plugins first globally, because they can listen for events # Import plugins first globally, because they can listen for events
# that happen before the connection phase. # that happen before the connection phase.
to_load = conf.conf['plugins'] to_load = conf.conf['plugins']
plugins_folder = [os.path.join(os.getcwd(), 'plugins')] plugins_folder = os.path.join(os.getcwd(), 'plugins')
# Here, we override the module lookup and import the plugins sys.path.extend([plugins_folder, protocols_folder])
# dynamically depending on which were configured.
for plugin in to_load: for plugin in to_load:
try: try:
moduleinfo = imp.find_module(plugin, plugins_folder) world.plugins[plugin] = pl = __import__(plugin)
pl = imp.load_source(plugin, moduleinfo[1])
world.plugins[plugin] = pl
except ImportError as e: except ImportError as e:
if str(e) == ('No module named %r' % plugin): if str(e) == ('No module named %r' % plugin):
log.error('Failed to load plugin %r: The plugin could not be found.', plugin) log.error('Failed to load plugin %r: The plugin could not be found.', plugin)
@ -49,8 +49,7 @@ if __name__ == '__main__':
for network in conf.conf['servers']: for network in conf.conf['servers']:
protoname = conf.conf['servers'][network]['protocol'] protoname = conf.conf['servers'][network]['protocol']
try: try:
moduleinfo = imp.find_module(protoname, protocols_folder) proto = __import__(protoname)
proto = imp.load_source(protoname, moduleinfo[1])
except ImportError as e: except ImportError as e:
if str(e) == ('No module named %r' % protoname): if str(e) == ('No module named %r' % protoname):
log.critical('Failed to load protocol module %r: The file could not be found.', protoname) log.critical('Failed to load protocol module %r: The file could not be found.', protoname)
@ -58,6 +57,7 @@ if __name__ == '__main__':
log.critical('Failed to load protocol module: ImportError: %s', protoname, str(e)) log.critical('Failed to load protocol module: ImportError: %s', protoname, str(e))
sys.exit(2) sys.exit(2)
else: else:
world.networkobjects[network] = classes.Irc(network, proto) world.networkobjects[network] = irc = classes.Irc(network, proto)
irc.plugins_folder = plugins_folder
world.started.set() world.started.set()
log.info("loaded plugins: %s", world.plugins) log.info("loaded plugins: %s", world.plugins)