mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-25 04:02:46 +01:00
Small refactoring (the calm before the storm, so to speak) to start putting some more config stuff in conf.py.
This commit is contained in:
parent
52fab17e92
commit
f98d1f9db5
66
src/bot.py
66
src/bot.py
@ -37,7 +37,6 @@ from fix import *
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import email
|
|
||||||
import getopt
|
import getopt
|
||||||
#import pprint
|
#import pprint
|
||||||
|
|
||||||
@ -68,15 +67,13 @@ class ConfigurationDict(dict):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def processConfigFile(filename):
|
def handleConfigFile(filename):
|
||||||
import debug
|
import debug
|
||||||
import irclib
|
import irclib
|
||||||
import ircmsgs
|
import ircmsgs
|
||||||
import drivers
|
import drivers
|
||||||
import ircutils
|
import ircutils
|
||||||
import privmsgs
|
import privmsgs
|
||||||
def reportConfigError(filename, msg):
|
|
||||||
debug.recoverableError('%s: %s' % (filename, msg))
|
|
||||||
class ConfigAfter376(irclib.IrcCallback):
|
class ConfigAfter376(irclib.IrcCallback):
|
||||||
public = False
|
public = False
|
||||||
def __init__(self, commands):
|
def __init__(self, commands):
|
||||||
@ -91,63 +88,21 @@ def processConfigFile(filename):
|
|||||||
irc.queueMsg(msg)
|
irc.queueMsg(msg)
|
||||||
|
|
||||||
do377 = do376
|
do377 = do376
|
||||||
try:
|
nick = conf.config['nick']
|
||||||
fd = file(filename)
|
user = conf.config['user']
|
||||||
m = email.message_from_file(fd)
|
ident = conf.config['ident']
|
||||||
d = ConfigurationDict(m.items())
|
prefix = ircutils.joinHostmask(nick, ident, 'host')
|
||||||
if 'nick' not in d:
|
password = conf.config['password']
|
||||||
reportConfigError(filename, 'No nick defined.')
|
|
||||||
return
|
|
||||||
if 'server' not in d:
|
|
||||||
reportConfigError(filename, 'No server defined.')
|
|
||||||
return
|
|
||||||
nick = d['nick']
|
|
||||||
server = d['server']
|
|
||||||
password = d['password']
|
|
||||||
if ':' in server:
|
|
||||||
(server, port) = server.split(':', 1)
|
|
||||||
try:
|
|
||||||
server = (server, int(port))
|
|
||||||
except ValueError:
|
|
||||||
reportConfigError(filename, 'Server has invalid port.')
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
server = (server, 6667)
|
|
||||||
user = d['user'] or nick
|
|
||||||
ident = d['ident'] or nick
|
|
||||||
irc = irclib.Irc(nick, user, ident, password)
|
irc = irclib.Irc(nick, user, ident, password)
|
||||||
for Class in privmsgs.standardPrivmsgModules:
|
for Class in privmsgs.standardPrivmsgModules:
|
||||||
irc.addCallback(Class())
|
irc.addCallback(Class())
|
||||||
world.startup = True
|
world.startup = True
|
||||||
text = m.get_payload()
|
for line in conf.config['onStart']:
|
||||||
while 1:
|
|
||||||
# Gotta remove all extra newlines.
|
|
||||||
newtext = text.replace('\n\n\n', '\n\n')
|
|
||||||
if newtext == text:
|
|
||||||
text = newtext
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
text = newtext
|
|
||||||
lines = text.splitlines()
|
|
||||||
#debug.printf(pprint.pformat(lines))
|
|
||||||
(startup, after376) = tuple(itersplit(lines,lambda s: not s, True))
|
|
||||||
#debug.printf('startup: %r' % startup)
|
|
||||||
#debug.printf('after376: %r' % after376)
|
|
||||||
prefix = ircutils.joinHostmask(nick, ident, 'host')
|
|
||||||
for line in filter(None, startup):
|
|
||||||
if not line.startswith('#'):
|
|
||||||
irc.feedMsg(ircmsgs.privmsg(irc.nick, line, prefix=prefix))
|
irc.feedMsg(ircmsgs.privmsg(irc.nick, line, prefix=prefix))
|
||||||
irc.reset()
|
irc.reset()
|
||||||
world.startup = False
|
world.startup = False
|
||||||
irc.addCallback(ConfigAfter376(after376))
|
irc.addCallback(ConfigAfter376(conf.config['afterConnect']))
|
||||||
driver = drivers.newDriver(server, irc)
|
driver = drivers.newDriver(conf.config['server'], irc)
|
||||||
except IOError, e:
|
|
||||||
reportConfigError(filename, e)
|
|
||||||
except email.Errors.HeaderParseError, e:
|
|
||||||
s = str(e)
|
|
||||||
problem = s[s.rfind('`')+1:-2]
|
|
||||||
msg = 'Invalid configuration format: %s' % problem
|
|
||||||
reportConfigError(filename, msg)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
(optlist, filenames) = getopt.getopt(sys.argv[1:], 'Opc:')
|
(optlist, filenames) = getopt.getopt(sys.argv[1:], 'Opc:')
|
||||||
@ -166,7 +121,8 @@ def main():
|
|||||||
import drivers
|
import drivers
|
||||||
import schedule
|
import schedule
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
processConfigFile(filename)
|
conf.processConfig(filename)
|
||||||
|
handleConfigFile(filename)
|
||||||
schedule.addPeriodicEvent(world.upkeep, 300)
|
schedule.addPeriodicEvent(world.upkeep, 300)
|
||||||
try:
|
try:
|
||||||
while world.ircs:
|
while world.ircs:
|
||||||
|
74
src/conf.py
74
src/conf.py
@ -187,7 +187,75 @@ driverModule = 'asyncoreDrivers'
|
|||||||
###############################
|
###############################
|
||||||
###############################
|
###############################
|
||||||
###############################
|
###############################
|
||||||
|
class ConfigurationDict(dict):
|
||||||
|
def __init__(self, L=()):
|
||||||
|
L = [(key.lower(), value) for (key, value) in L]
|
||||||
|
dict.__init__(self, L)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
dict.__setitem__(self, key.lower(), value)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
try:
|
||||||
|
return dict.__getitem__(self, key.lower())
|
||||||
|
except KeyError:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
return dict.__contains__(self, key.lower())
|
||||||
|
|
||||||
|
config = ConfigurationDict()
|
||||||
|
|
||||||
|
def processConfig(filename):
|
||||||
|
import debug
|
||||||
|
import email
|
||||||
|
def reportConfigError(filename, msg):
|
||||||
|
debug.unrecoverableError('%s: %s' % (filename, msg))
|
||||||
|
try:
|
||||||
|
fd = file(filename)
|
||||||
|
m = email.message_from_file(fd)
|
||||||
|
for (k, v) in m.items():
|
||||||
|
config[k] = v
|
||||||
|
if 'nick' not in config:
|
||||||
|
reportConfigError(filename, 'No nick defined.')
|
||||||
|
if 'server' not in config:
|
||||||
|
reportConfigError(filename, 'No server defined.')
|
||||||
|
if 'user' not in config:
|
||||||
|
config['user'] = config['nick']
|
||||||
|
if 'ident' not in config:
|
||||||
|
config['ident'] = config['nick']
|
||||||
|
server = config['server']
|
||||||
|
if ':' in server:
|
||||||
|
(server, port) = server.split(':', 1)
|
||||||
|
try:
|
||||||
|
server = (server, int(port))
|
||||||
|
except ValueError:
|
||||||
|
reportConfigError(filename, 'Server has an invalid port.')
|
||||||
|
else:
|
||||||
|
server = (server, 6667)
|
||||||
|
config['server'] = server
|
||||||
|
text = m.get_payload()
|
||||||
|
while 1:
|
||||||
|
# Gotta remove all extra newlines.
|
||||||
|
newtext = text.replace('\n\n\n', '\n\n')
|
||||||
|
if newtext == text:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
text = newtext
|
||||||
|
lines = text.splitlines()
|
||||||
|
# There are 2 newlines separating the commands to be run on startup
|
||||||
|
# from the commands to be run after connecting. This separates them
|
||||||
|
# based on those newlines.
|
||||||
|
(onStart, afterConnect) = tuple(itersplit(lines,lambda s: not s,True))
|
||||||
|
config['onStart'] = [s for s in onStart if s and s[0] != '#']
|
||||||
|
config['afterConnect'] = [s for s in afterConnect if s and s[0] != '#']
|
||||||
|
|
||||||
|
except IOError, e:
|
||||||
|
reportConfigError(filename, e)
|
||||||
|
except email.Errors.HeaderParseError, e:
|
||||||
|
s = str(e)
|
||||||
|
problem = s[s.rfind('`')+1:-2]
|
||||||
|
msg = 'Invalid configuration format: %s' % problem
|
||||||
|
reportConfigError(filename, msg)
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user