mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 04:59:24 +01:00
Modularize our import hacks, make Irc() take a conf object again
This commit is contained in:
parent
9e07949730
commit
e84a2d1025
@ -9,7 +9,6 @@ import hashlib
|
||||
from copy import deepcopy
|
||||
|
||||
from log import log
|
||||
from conf import conf
|
||||
import world
|
||||
import utils
|
||||
|
||||
@ -63,7 +62,7 @@ class Irc():
|
||||
self.uplink = None
|
||||
self.start_ts = int(time.time())
|
||||
|
||||
def __init__(self, netname, proto):
|
||||
def __init__(self, netname, proto, conf):
|
||||
# Initialize some variables
|
||||
self.name = netname.lower()
|
||||
self.conf = conf
|
||||
|
1
conf.py
1
conf.py
@ -60,6 +60,7 @@ def loadConf(fname):
|
||||
if world.testing:
|
||||
conf = testconf
|
||||
confname = 'testconf'
|
||||
fname = None
|
||||
else:
|
||||
try:
|
||||
# Get the config name from the command line, falling back to config.yml
|
||||
|
@ -7,9 +7,10 @@ import gc
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import utils
|
||||
from conf import conf
|
||||
import conf
|
||||
from log import log
|
||||
import world
|
||||
import classes
|
||||
|
||||
@utils.add_cmd
|
||||
def status(irc, source, args):
|
||||
@ -37,8 +38,8 @@ def identify(irc, source, args):
|
||||
irc.msg(source, 'Error: Not enough arguments.')
|
||||
return
|
||||
# Usernames are case-insensitive, passwords are NOT.
|
||||
if username.lower() == conf['login']['user'].lower() and password == conf['login']['password']:
|
||||
realuser = conf['login']['user']
|
||||
if username.lower() == irc.conf['login']['user'].lower() and password == irc.conf['login']['password']:
|
||||
realuser = irc.conf['login']['user']
|
||||
irc.users[source].identified = realuser
|
||||
irc.msg(source, 'Successfully logged in as %s.' % realuser)
|
||||
log.info("(%s) Successful login to %r by %s.",
|
||||
@ -220,7 +221,7 @@ def load(irc, source, args):
|
||||
irc.msg(irc.called_by, "Error: %r is already loaded." % name)
|
||||
return
|
||||
try:
|
||||
world.plugins[name] = pl = __import__(name)
|
||||
world.plugins[name] = pl = utils.loadModuleFromFolder(name, world.plugins_folder)
|
||||
except ImportError as e:
|
||||
if str(e) == ('No module named %r' % name):
|
||||
log.exception('Failed to load plugin %r: The plugin could not be found.', name)
|
||||
|
21
pylink
21
pylink
@ -12,11 +12,11 @@ world.testing = False
|
||||
import conf
|
||||
from log import log
|
||||
import classes
|
||||
import utils
|
||||
import coreplugin
|
||||
|
||||
if __name__ == '__main__':
|
||||
log.info('PyLink %s starting...', world.version)
|
||||
protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
|
||||
|
||||
# Write a PID file.
|
||||
with open('%s.pid' % conf.confname, 'w') as f:
|
||||
@ -25,14 +25,11 @@ if __name__ == '__main__':
|
||||
# Import plugins first globally, because they can listen for events
|
||||
# that happen before the connection phase.
|
||||
to_load = conf.conf['plugins']
|
||||
plugins_folder = [os.path.join(os.getcwd(), 'plugins')]
|
||||
# Here, we override the module lookup and import the plugins
|
||||
# dynamically depending on which were configured.
|
||||
for plugin in to_load:
|
||||
try:
|
||||
moduleinfo = imp.find_module(plugin, plugins_folder)
|
||||
pl = imp.load_source(plugin, moduleinfo[1])
|
||||
world.plugins[plugin] = pl
|
||||
world.plugins[plugin] = pl = utils.loadModuleFromFolder(plugin, world.plugins_folder)
|
||||
except ImportError as e:
|
||||
if str(e) == ('No module named %r' % plugin):
|
||||
log.error('Failed to load plugin %r: The plugin could not be found.', plugin)
|
||||
@ -44,17 +41,7 @@ if __name__ == '__main__':
|
||||
pl.main()
|
||||
|
||||
for network in conf.conf['servers']:
|
||||
protoname = conf.conf['servers'][network]['protocol']
|
||||
try:
|
||||
moduleinfo = imp.find_module(protoname, protocols_folder)
|
||||
proto = imp.load_source(protoname, moduleinfo[1])
|
||||
except ImportError as e:
|
||||
if str(e) == ('No module named %r' % protoname):
|
||||
log.critical('Failed to load protocol module %r: The file could not be found.', protoname)
|
||||
else:
|
||||
log.critical('Failed to load protocol module: ImportError: %s', protoname, str(e))
|
||||
sys.exit(2)
|
||||
else:
|
||||
world.networkobjects[network] = classes.Irc(network, proto)
|
||||
proto = utils.getProtoModule(conf.conf['servers'][network]['protocol'])
|
||||
world.networkobjects[network] = classes.Irc(network, proto, conf.conf)
|
||||
world.started.set()
|
||||
log.info("loaded plugins: %s", world.plugins)
|
||||
|
11
utils.py
11
utils.py
@ -1,6 +1,7 @@
|
||||
import string
|
||||
import re
|
||||
import inspect
|
||||
import imp
|
||||
|
||||
from log import log
|
||||
import world
|
||||
@ -497,3 +498,13 @@ def getHostmask(irc, user):
|
||||
except AttributeError:
|
||||
host = '<unknown host>'
|
||||
return '%s!%s@%s' % (nick, ident, host)
|
||||
|
||||
def loadModuleFromFolder(name, folder):
|
||||
"""Attempts an import of name from a specific folder, returning the resulting module."""
|
||||
moduleinfo = imp.find_module(name, folder)
|
||||
m = imp.load_source(name, moduleinfo[1])
|
||||
return m
|
||||
|
||||
def getProtoModule(protoname):
|
||||
"""Imports and returns the protocol module requested."""
|
||||
return loadModuleFromFolder(protoname, world.protocols_folder)
|
||||
|
4
world.py
4
world.py
@ -3,6 +3,7 @@
|
||||
from collections import defaultdict
|
||||
import threading
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
# Global variable to indicate whether we're being ran directly, or imported
|
||||
# for a testcase.
|
||||
@ -18,6 +19,9 @@ plugins = {}
|
||||
whois_handlers = []
|
||||
started = threading.Event()
|
||||
|
||||
plugins_folder = [os.path.join(os.getcwd(), 'plugins')]
|
||||
protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
|
||||
|
||||
version = "<unknown>"
|
||||
source = "https://github.com/GLolol/PyLink" # CHANGE THIS IF YOU'RE FORKING!!
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user