From 1384d1976fa984b6b254c93677363f67944c0c7a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 9 May 2010 21:49:15 +0200 Subject: Made everything much more error resistant --- modules/__init__.py | 21 +++++++++++++++++++++ modules/base.py | 24 ------------------------ modules/credits.py | 2 +- modules/ddate.py | 2 +- modules/help.py | 6 +++--- modules/log.py | 12 ++++++------ modules/mensa.py | 2 +- modules/mysql.py | 20 +++++++++++++++++--- modules/topic.py | 2 +- 9 files changed, 51 insertions(+), 40 deletions(-) delete mode 100644 modules/base.py (limited to 'modules') diff --git a/modules/__init__.py b/modules/__init__.py index e69de29..49a460a 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -0,0 +1,21 @@ +class ModuleBase: + def __init__(self, manager): + self.manager = manager + + def commands(self): + return [] + + def helptexts(self): + return [] + + def groupchat(self, room, nick, text, handler): + pass + + def join(self, room, nick, show, status, handler): + pass + + def leave(self, room, nick, show, status, handler): + pass + + def topic(self, room, nick, text, handler): + pass diff --git a/modules/base.py b/modules/base.py deleted file mode 100644 index b839e9b..0000000 --- a/modules/base.py +++ /dev/null @@ -1,24 +0,0 @@ -class ModuleBase: - def __init__(self, manager): - self.manager = manager - - def commands(self): - return [] - - def helptexts(self): - return [] - - def message(self, message_type, message_from, message_subject, message_body, reply): - pass - - def groupchat(self, room, nick, text, handler): - pass - - def join(self, room, nick, show, status, handler): - pass - - def leave(self, room, nick, show, status, handler): - pass - - def topic(self, room, nick, text, handler): - pass diff --git a/modules/credits.py b/modules/credits.py index 29c00f7..d6f0f66 100644 --- a/modules/credits.py +++ b/modules/credits.py @@ -1,4 +1,4 @@ -from base import ModuleBase +from . import ModuleBase import re class Module(ModuleBase): diff --git a/modules/ddate.py b/modules/ddate.py index 5c6fd77..e687d76 100644 --- a/modules/ddate.py +++ b/modules/ddate.py @@ -1,4 +1,4 @@ -from base import ModuleBase +from . import ModuleBase import re, subprocess class Module(ModuleBase): diff --git a/modules/help.py b/modules/help.py index ed3e4d3..99f05a4 100644 --- a/modules/help.py +++ b/modules/help.py @@ -1,4 +1,4 @@ -from base import ModuleBase +from . import ModuleBase import re class Module(ModuleBase): @@ -12,8 +12,8 @@ class Module(ModuleBase): if not re.match(r'!help(?:\W|\Z)', text): return - commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules.itervalues(), []) + commands = self.manager.commands() helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '') - helptexts = '\n'.join(reduce(lambda l, mod: l + mod.helptexts(), self.manager.modules.itervalues(), [])) + helptexts = '\n'.join(self.manager.helptexts()) handler.reply(('Befehle:\n' + helpstring + '\n' + helptexts).strip()) diff --git a/modules/log.py b/modules/log.py index 04ea721..ff8093e 100644 --- a/modules/log.py +++ b/modules/log.py @@ -1,29 +1,29 @@ -from base import ModuleBase +from . import ModuleBase class Module(ModuleBase): def __init__(self, manager): ModuleBase.__init__(self, manager) - self.db = manager.get('mysql').db + self.mysql = manager.get('mysql') def helptexts(self): return ['Chatlogs werden auch erstellt.'] def groupchat(self, room, nick, text, handler): - cursor = self.db.cursor() + cursor = self.mysql.cursor() cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("message", NOW(), %s, %s, %s)', (room, nick, text)) cursor.close() def join(self, room, nick, show, status, handler): - cursor = self.db.cursor() + cursor = self.mysql.cursor() cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("join", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) cursor.close() def leave(self, room, nick, show, status, handler): - cursor = self.db.cursor() + cursor = self.mysql.cursor() cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `show`, `text`) VALUES ("leave", NOW(), %s, %s, %s, %s)', (room, nick, show, status)) cursor.close() def topic(self, room, nick, text, handler): - cursor = self.db.cursor() + cursor = self.mysql.cursor() cursor.execute('INSERT INTO log (`type`, `time`, `room`, `nick`, `text`) VALUES ("topic", NOW(), %s, %s, %s)', (room, nick, text)) cursor.close() diff --git a/modules/mensa.py b/modules/mensa.py index 2dd71dc..96f2275 100644 --- a/modules/mensa.py +++ b/modules/mensa.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from base import ModuleBase +from . import ModuleBase import re from datetime import datetime, date, timedelta, time import urllib diff --git a/modules/mysql.py b/modules/mysql.py index 2db2817..5ae9e93 100644 --- a/modules/mysql.py +++ b/modules/mysql.py @@ -1,10 +1,24 @@ -from base import ModuleBase +from . import ModuleBase import MySQLdb class Module(ModuleBase): def __init__(self, manager): ModuleBase.__init__(self, manager) - conf = manager.config['mysql'] + self.conf = manager.config['mysql'] - self.db = MySQLdb.connect(host = conf['host'], user = conf['user'], passwd = conf['passwd'], db = conf['db'], use_unicode = True, charset = 'utf8') + self.db = False + self._connect() + + def _connect(self): + if self.db: + self.db.close() + self.db = MySQLdb.connect(host = self.conf['host'], user = self.conf['user'], passwd = self.conf['passwd'], db = self.conf['db'], use_unicode = True, charset = 'utf8') + + def cursor(self): + try: + self.db.ping() + return self.db.cursor() + except MySQLdb.OperationalError: + self._connect() + return self.db.cursor() diff --git a/modules/topic.py b/modules/topic.py index 81e1802..d878a91 100644 --- a/modules/topic.py +++ b/modules/topic.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from base import ModuleBase +from . import ModuleBase import re class Module(ModuleBase): -- cgit v1.2.3