mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 19:19:32 +01:00
Update to use the new-style DB abstraction.
This commit is contained in:
parent
d242c8ad81
commit
bc8a6b94d9
@ -81,21 +81,38 @@ conf.registerChannelValue(conf.supybot.plugins.Karma, 'allowUnaddressedKarma',
|
|||||||
increase/decrease karma without being addressed."""))
|
increase/decrease karma without being addressed."""))
|
||||||
|
|
||||||
class SqliteKarmaDB(object):
|
class SqliteKarmaDB(object):
|
||||||
|
def __init__(self, filename):
|
||||||
|
self.dbs = ircutils.IrcDict()
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
for db in self.dbs.itervalues():
|
||||||
|
db.close()
|
||||||
|
|
||||||
def _getDb(self, channel):
|
def _getDb(self, channel):
|
||||||
filename = plugins.makeChannelFilename('Karma.db', channel)
|
try:
|
||||||
|
import sqlite
|
||||||
|
except ImportError:
|
||||||
|
raise callbacks.Error, 'You need to have PySQLite installed to ' \
|
||||||
|
'use this plugin. Download it at ' \
|
||||||
|
'<http://pysqlite.sf.net/>'
|
||||||
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
||||||
|
if filename in self.dbs:
|
||||||
|
return self.dbs[filename]
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
db = sqlite.connect(filename)
|
self.dbs[filename] = sqlite.connect(filename)
|
||||||
else:
|
return self.dbs[filename]
|
||||||
db = sqlite.connect(filename)
|
db = sqlite.connect(filename)
|
||||||
cursor = db.cursor()
|
self.dbs[filename] = db
|
||||||
cursor.execute("""CREATE TABLE karma (
|
cursor = db.cursor()
|
||||||
id INTEGER PRIMARY KEY,
|
cursor.execute("""CREATE TABLE karma (
|
||||||
name TEXT,
|
id INTEGER PRIMARY KEY,
|
||||||
normalized TEXT UNIQUE ON CONFLICT IGNORE,
|
name TEXT,
|
||||||
added INTEGER,
|
normalized TEXT UNIQUE ON CONFLICT IGNORE,
|
||||||
subtracted INTEGER
|
added INTEGER,
|
||||||
)""")
|
subtracted INTEGER
|
||||||
db.commit()
|
)""")
|
||||||
|
db.commit()
|
||||||
def p(s1, s2):
|
def p(s1, s2):
|
||||||
return int(ircutils.nickEqual(s1, s2))
|
return int(ircutils.nickEqual(s1, s2))
|
||||||
db.create_function('nickeq', 2, p)
|
db.create_function('nickeq', 2, p)
|
||||||
@ -204,9 +221,8 @@ class SqliteKarmaDB(object):
|
|||||||
WHERE normalized=%s""", normalized)
|
WHERE normalized=%s""", normalized)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
KarmaDB = plugins.DB('Karma',
|
||||||
def KarmaDB():
|
{'sqlite': SqliteKarmaDB})
|
||||||
return SqliteKarmaDB()
|
|
||||||
|
|
||||||
class Karma(callbacks.Privmsg):
|
class Karma(callbacks.Privmsg):
|
||||||
callBefore = ('Factoids', 'MoobotFactoids', 'Infobot')
|
callBefore = ('Factoids', 'MoobotFactoids', 'Infobot')
|
||||||
@ -214,19 +230,22 @@ class Karma(callbacks.Privmsg):
|
|||||||
self.db = KarmaDB()
|
self.db = KarmaDB()
|
||||||
super(Karma, self).__init__()
|
super(Karma, self).__init__()
|
||||||
|
|
||||||
|
def die(self):
|
||||||
|
self.db.close()
|
||||||
|
|
||||||
def _normalizeThing(self, thing):
|
def _normalizeThing(self, thing):
|
||||||
assert thing
|
assert thing
|
||||||
if thing[0] == '(' and thing[-1] == ')':
|
if thing[0] == '(' and thing[-1] == ')':
|
||||||
thing = thing[1:-1]
|
thing = thing[1:-1]
|
||||||
return thing
|
return thing
|
||||||
|
|
||||||
def _respond(self, irc, channel):
|
def _respond(self, irc, channel):
|
||||||
if self.registryValue('response', channel):
|
if self.registryValue('response', channel):
|
||||||
irc.replySuccess()
|
irc.replySuccess()
|
||||||
else:
|
else:
|
||||||
irc.noReply()
|
irc.noReply()
|
||||||
assert irc.msg.repliedTo
|
assert irc.msg.repliedTo
|
||||||
|
|
||||||
def _doKarma(self, irc, channel, thing):
|
def _doKarma(self, irc, channel, thing):
|
||||||
assert thing[-2:] in ('++', '--')
|
assert thing[-2:] in ('++', '--')
|
||||||
if thing.endswith('++'):
|
if thing.endswith('++'):
|
||||||
@ -245,7 +264,7 @@ class Karma(callbacks.Privmsg):
|
|||||||
elif thing:
|
elif thing:
|
||||||
self.db.decrement(channel, self._normalizeThing(thing))
|
self.db.decrement(channel, self._normalizeThing(thing))
|
||||||
self._respond(irc, channel)
|
self._respond(irc, channel)
|
||||||
|
|
||||||
def tokenizedCommand(self, irc, msg, tokens):
|
def tokenizedCommand(self, irc, msg, tokens):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
if not ircutils.isChannel(channel):
|
if not ircutils.isChannel(channel):
|
||||||
@ -263,7 +282,7 @@ class Karma(callbacks.Privmsg):
|
|||||||
thing = msg.args[1].rstrip()
|
thing = msg.args[1].rstrip()
|
||||||
if thing[-2:] in ('++', '--'):
|
if thing[-2:] in ('++', '--'):
|
||||||
self._doKarma(irc, channel, thing)
|
self._doKarma(irc, channel, thing)
|
||||||
|
|
||||||
def karma(self, irc, msg, args):
|
def karma(self, irc, msg, args):
|
||||||
"""[<channel>] [<thing> [<thing> ...]]
|
"""[<channel>] [<thing> [<thing> ...]]
|
||||||
|
|
||||||
|
@ -79,8 +79,9 @@ class NewsRecord(object):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
class SqliteNewsDB(object):
|
class SqliteNewsDB(object):
|
||||||
def __init__(self):
|
def __init__(self, filename):
|
||||||
self.dbs = ircutils.IrcDict()
|
self.dbs = ircutils.IrcDict()
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
for db in self.dbs.itervalues():
|
for db in self.dbs.itervalues():
|
||||||
@ -93,7 +94,7 @@ class SqliteNewsDB(object):
|
|||||||
raise callbacks.Error, 'You need to have PySQLite installed to ' \
|
raise callbacks.Error, 'You need to have PySQLite installed to ' \
|
||||||
'use this plugin. Download it at ' \
|
'use this plugin. Download it at ' \
|
||||||
'<http://pysqlite.sf.net/>'
|
'<http://pysqlite.sf.net/>'
|
||||||
filename = plugins.makeChannelFilename(channel, 'News.db')
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
||||||
if filename in self.dbs:
|
if filename in self.dbs:
|
||||||
return self.dbs[filename]
|
return self.dbs[filename]
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
@ -170,8 +171,8 @@ class SqliteNewsDB(object):
|
|||||||
cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""",
|
cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""",
|
||||||
newSubject, newItem, id)
|
newSubject, newItem, id)
|
||||||
|
|
||||||
def NewsDB():
|
NewsDB = plugins.DB('News',
|
||||||
return SqliteNewsDB()
|
{'sqlite': SqliteNewsDB})
|
||||||
|
|
||||||
class News(callbacks.Privmsg):
|
class News(callbacks.Privmsg):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user