3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-24 03:29:28 +01:00

Move rehash into core (Closes #178)

This commit is contained in:
James Lu 2016-04-09 09:54:04 -07:00
parent 92c600b7d3
commit 8f32727a3f
2 changed files with 50 additions and 48 deletions

View File

@ -7,6 +7,8 @@ import sys
import signal
import utils
import conf
import classes
from log import log
import world
@ -332,6 +334,54 @@ def reload(irc, source, args):
if unload(irc, source, args):
load(irc, source, args)
@utils.add_cmd
def rehash(irc, source, args):
"""takes no arguments.
Reloads the configuration file for PyLink, (dis)connecting added/removed networks.
Plugins must be manually reloaded."""
utils.checkAuthenticated(irc, source, allowOper=False)
old_conf = conf.conf.copy()
fname = conf.fname
try:
new_conf = conf.loadConf(fname, errors_fatal=False)
except Exception as e: # Something went wrong, abort.
log.exception("Error REHASH'ing config: ")
irc.reply("Error loading configuration file: %s: %s" % (type(e).__name__, e))
return
else:
new_conf = conf.validateConf(new_conf)
conf.conf = new_conf
for network, ircobj in world.networkobjects.copy().items():
# Server was removed from the config file, disconnect them.
log.debug('(%s) rehash: checking if %r is in new conf still.', irc.name, network)
if network not in new_conf['servers']:
log.debug('(%s) rehash: removing connection to %r (removed from config).', irc.name, network)
# Disable autoconnect first.
ircobj.serverdata['autoconnect'] = -1
ircobj.disconnect()
del world.networkobjects[network]
else:
ircobj.conf = new_conf
ircobj.serverdata = new_conf['servers'][network]
ircobj.botdata = new_conf['bot']
# Clear the IRC object's channel loggers and replace them with
# new ones by re-running logSetup().
while ircobj.loghandlers:
log.removeHandler(ircobj.loghandlers.pop())
ircobj.logSetup()
# TODO: update file loggers here too.
for network, sdata in new_conf['servers'].items():
# New server was added. Connect them if not already connected.
if network not in world.networkobjects:
proto = utils.getProtocolModule(sdata['protocol'])
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
irc.reply("Done.")
def main(irc=None):
# This is a global sanity check, to make sure the protocol module is doing
# its job.

View File

@ -5,10 +5,8 @@ from time import ctime
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import utils
import conf
from log import log
import world
import classes
@utils.add_cmd
def status(irc, source, args):
@ -173,52 +171,6 @@ def echo(irc, source, args):
Echoes the text given."""
irc.reply(' '.join(args))
@utils.add_cmd
def rehash(irc, source, args):
"""takes no arguments.
Reloads the configuration file for PyLink, (dis)connecting added/removed networks.
Plugins must be manually reloaded."""
utils.checkAuthenticated(irc, source, allowOper=False)
old_conf = conf.conf.copy()
fname = conf.fname
try:
new_conf = conf.loadConf(fname, errors_fatal=False)
except Exception as e: # Something went wrong, abort.
log.exception("Error REHASH'ing config: ")
irc.reply("Error loading configuration file: %s: %s" % (type(e).__name__, e))
return
else:
new_conf = conf.validateConf(new_conf)
conf.conf = new_conf
for network, ircobj in world.networkobjects.copy().items():
# Server was removed from the config file, disconnect them.
log.debug('(%s) rehash: checking if %r is in new conf still.', irc.name, network)
if network not in new_conf['servers']:
log.debug('(%s) rehash: removing connection to %r (removed from config).', irc.name, network)
# Disable autoconnect first.
ircobj.serverdata['autoconnect'] = -1
ircobj.disconnect()
del world.networkobjects[network]
else:
ircobj.conf = new_conf
ircobj.serverdata = new_conf['servers'][network]
ircobj.botdata = new_conf['bot']
# Clear the IRC object's channel loggers and replace them with
# new ones by re-running logSetup().
while ircobj.loghandlers:
log.removeHandler(ircobj.loghandlers.pop())
ircobj.logSetup()
for network, sdata in new_conf['servers'].items():
# New server was added. Connect them if not already connected.
if network not in world.networkobjects:
proto = utils.getProtocolModule(sdata['protocol'])
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
irc.reply("Done.")
loglevels = {'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'ERROR': 40, 'CRITICAL': 50}
@utils.add_cmd
def loglevel(irc, source, args):