diff options
-rwxr-xr-x | IRC/asybot.py | 26 | ||||
-rw-r--r-- | IRC/getconf.py | 24 | ||||
-rw-r--r-- | config.json | 28 | ||||
-rw-r--r-- | config.py | 24 |
4 files changed, 54 insertions, 48 deletions
diff --git a/IRC/asybot.py b/IRC/asybot.py index ceebe84..6d19fd0 100755 --- a/IRC/asybot.py +++ b/IRC/asybot.py @@ -19,13 +19,13 @@ from re import split, search, match from textwrap import TextWrapper import logging,logging.handlers from getconf import make_getconf -getconf = make_getconf('config.json') +getconf = make_getconf('config.py') log = logging.getLogger('asybot') hdlr = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON) formatter = logging.Formatter( '%(filename)s: %(levelname)s: %(message)s') hdlr.setFormatter(formatter) log.addHandler(hdlr) -logging.basicConfig(level = logging.DEBUG if getconf('main.debug') else logging.INFO) +logging.basicConfig(level = logging.DEBUG if getconf('debug') else logging.INFO) # s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g -- removes color codes @@ -33,14 +33,14 @@ logging.basicConfig(level = logging.DEBUG if getconf('main.debug') else logging. class asybot(asychat): def __init__(self): asychat.__init__(self) - self.server = getconf('irc.server') - self.port = getconf('irc.port') - self.channels = getconf('irc.channels') - self.realname = getconf('irc.nickname') - self.nickname = getconf('irc.nickname') - self.username = getconf('irc.nickname') - self.hostname = getconf('irc.nickname') - self.ircname = getconf('irc.nickname') + self.server = getconf('irc_server') + self.port = getconf('irc_port') + self.channels = getconf('irc_channels') + self.realname = getconf('irc_nickname') + self.nickname = getconf('irc_nickname') + self.username = getconf('irc_nickname') + self.hostname = getconf('irc_nickname') + self.ircname = getconf('irc_nickname') self.data = '' self.set_terminator('\r\n'.encode(encoding='UTF-8')) self.create_socket(AF_INET, SOCK_STREAM) @@ -52,8 +52,8 @@ class asybot(asychat): # When we don't receive data for alarm_timeout seconds then issue a # PING every hammer_interval seconds until kill_timeout seconds have # passed without a message. Any incoming message will reset alarm. - self.alarm_timeout = getconf('irc.alarm_timeout') - self.hammer_interval = getconf('irc.hammer_interval') + self.alarm_timeout = getconf('irc_alarm_timeout') + self.hammer_interval = getconf('irc_hammer_interval') self.kill_timeout = 360 signal(SIGALRM, lambda signum, frame: self.alarm_handler()) self.reset_alarm() @@ -125,7 +125,7 @@ class asybot(asychat): def ME(text): PRIVMSG(('ACTION ' + text + '').encode(encoding='UTF-8')) - for command in getconf('irc.commands'): + for command in getconf('irc_commands'): y = match(command['pattern'], rest) if y: self.execute_command(command, y, PRIVMSG, ME) diff --git a/IRC/getconf.py b/IRC/getconf.py index 5fdd1cd..d6e9f42 100644 --- a/IRC/getconf.py +++ b/IRC/getconf.py @@ -2,19 +2,29 @@ #getconf(key) -> value #oder error -import json +import imp +import os + def make_getconf(filename): + + config = load_config(filename) + def getconf(prop): prop_split = prop.split('.') string = '' - file = open(filename) - for line in file.readlines(): - string+=line - parsed = json.loads(string) - tmp = parsed + imp.reload(config) + tmp = config.__dict__ for pr in prop_split: tmp = tmp[pr] - return tmp + return getconf + + +def load_config(filename): + dirname = os.path.dirname(filename) + modname, ext = os.path.splitext(os.path.basename(filename)) + file, pathname, description = imp.find_module(modname, [ dirname ]) + return imp.load_module(modname, file, pathname, description) + diff --git a/config.json b/config.json deleted file mode 100644 index 7b84c55..0000000 --- a/config.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "main": { - "debug": true, - "name": "asybot" - }, - "irc": { - "alarm_timeout": 300, - "hammer_interval": 10, - "kill_timeout": 360, - "nickname": "asybot", - "server": "irc.freenode.org", - "port": 6667, - "channels": [ - "#krebs" - ], - "commands": [ - { "pattern": "^(?:asybot|\\*):\\s*caps\\s*$", "argv": [ "commands/caps" ] }, - { "pattern": "^(?:asybot|\\*):\\s*hello\\s*$", "argv": [ "commands/hello" ] }, - { "pattern": "^(?:asybot|\\*):\\s*reload\\s*$", "argv": [ "commands/reload" ] }, - { "pattern": "^(?:asybot|\\*):\\s*badcommand\\s*$", "argv": [ "commands/badcommand" ] }, - { "pattern": "^(?:asybot|\\*):\\s*rev\\s*$", "argv": [ "commands/rev" ] }, - { "pattern": "^(?:asybot|\\*):\\s*uptime\\s*$", "argv": [ "commands/uptime" ] }, - { "pattern": "^(?:asybot|\\*):\\s*nocommand\\s*$", "argv": [ "commands/nocommand" ] }, - { "pattern": "^.*\\basybot(?:\\b[^:].*)?$", "argv": [ "commands/say", "I'm famous" ] } - ] - } -} - diff --git a/config.py b/config.py new file mode 100644 index 0000000..92d97af --- /dev/null +++ b/config.py @@ -0,0 +1,24 @@ + +debug = True +name = 'kwasybot' + +irc_alarm_timeout = 300 +irc_hammer_interval = 10 +irc_kill_timeout = 360 +irc_nickname = name +irc_server = 'irc.freenode.org' +irc_port = 6667 +irc_channels = [ + '#krebs' +] + +irc_commands = [ + { 'pattern': '^(?:asybot|\\*):\\s*caps\\s*$', 'argv': [ 'commands/caps' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*hello\\s*$', 'argv': [ 'commands/hello' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*reload\\s*$', 'argv': [ 'commands/reload' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*badcommand\\s*$', 'argv': [ 'commands/badcommand' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*rev\\s*$', 'argv': [ 'commands/rev' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*uptime\\s*$', 'argv': [ 'commands/uptime' ] }, + { 'pattern': '^(?:asybot|\\*):\\s*nocommand\\s*$', 'argv': [ 'commands/nocommand' ] }, + { 'pattern': '^.*\\basybot(?:\\b[^:].*)?$', 'argv': [ 'commands/say', 'I\'m famous' ] } +] |