mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-23 02:24:12 +01:00
Updated to handle some EnvironmentErrors.
This commit is contained in:
parent
fa8f39a725
commit
75f1eb4eee
@ -108,17 +108,20 @@ class FlatfileDunnoDB(DunnoDBInterface):
|
||||
return L
|
||||
|
||||
def __init__(self):
|
||||
self.dbs = ircutils.IrcDict()
|
||||
|
||||
self.filenames = sets.Set()
|
||||
def _getDb(self, channel):
|
||||
if channel not in self.dbs:
|
||||
# Why cache? It gains very little.
|
||||
filename = plugins.makeChannelFilename(channel, 'Dunno.db')
|
||||
self.dbs[channel] = self.DunnoDB(filename)
|
||||
return self.dbs[channel]
|
||||
self.filenames.add(filename)
|
||||
return self.DunnoDB(filename)
|
||||
|
||||
def close(self):
|
||||
for db in self.dbs.values():
|
||||
for filename in self.filenames:
|
||||
try:
|
||||
db = self.DunnoDB(filename)
|
||||
db.close()
|
||||
except EnvironmentError:
|
||||
pass
|
||||
|
||||
def add(self, channel, dunno, by, at):
|
||||
db = self._getDb(channel)
|
||||
@ -142,8 +145,12 @@ class FlatfileDunnoDB(DunnoDBInterface):
|
||||
|
||||
def random(self, channel):
|
||||
db = self._getDb(channel)
|
||||
(id, (at, by, dunno)) = random.choice(db.records())
|
||||
x = random.choice(db.records())
|
||||
if x:
|
||||
(id, (at, by, dunno)) = x
|
||||
return (id, dunno)
|
||||
else:
|
||||
return None
|
||||
|
||||
def search(self, channel, p):
|
||||
L = []
|
||||
@ -154,8 +161,11 @@ class FlatfileDunnoDB(DunnoDBInterface):
|
||||
return L
|
||||
|
||||
def size(self, channel):
|
||||
try:
|
||||
db = self._getDb(channel)
|
||||
return itertools.ilen(db.records())
|
||||
except EnvironmentError, e:
|
||||
return 0
|
||||
|
||||
|
||||
DunnoDB = FlatfileDunnoDB
|
||||
@ -178,8 +188,9 @@ class Dunno(callbacks.Privmsg):
|
||||
def invalidCommand(self, irc, msg, tokens):
|
||||
channel = msg.args[0]
|
||||
if ircutils.isChannel(channel):
|
||||
dunno = self.db.random(channel)[1]
|
||||
dunno = self.db.random(channel)
|
||||
if dunno is not None:
|
||||
dunno = dunno[1]
|
||||
prefixName = self.registryValue('prefixNick', channel)
|
||||
dunno = plugins.standardSubstitute(irc, msg, dunno)
|
||||
irc.reply(dunno, prefixName=prefixName)
|
||||
|
Loading…
Reference in New Issue
Block a user