mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-23 10:34:19 +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
|
return L
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.dbs = ircutils.IrcDict()
|
self.filenames = sets.Set()
|
||||||
|
|
||||||
def _getDb(self, channel):
|
def _getDb(self, channel):
|
||||||
if channel not in self.dbs:
|
# Why cache? It gains very little.
|
||||||
filename = plugins.makeChannelFilename(channel, 'Dunno.db')
|
filename = plugins.makeChannelFilename(channel, 'Dunno.db')
|
||||||
self.dbs[channel] = self.DunnoDB(filename)
|
self.filenames.add(filename)
|
||||||
return self.dbs[channel]
|
return self.DunnoDB(filename)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
for db in self.dbs.values():
|
for filename in self.filenames:
|
||||||
|
try:
|
||||||
|
db = self.DunnoDB(filename)
|
||||||
db.close()
|
db.close()
|
||||||
|
except EnvironmentError:
|
||||||
|
pass
|
||||||
|
|
||||||
def add(self, channel, dunno, by, at):
|
def add(self, channel, dunno, by, at):
|
||||||
db = self._getDb(channel)
|
db = self._getDb(channel)
|
||||||
@ -142,8 +145,12 @@ class FlatfileDunnoDB(DunnoDBInterface):
|
|||||||
|
|
||||||
def random(self, channel):
|
def random(self, channel):
|
||||||
db = self._getDb(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)
|
return (id, dunno)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def search(self, channel, p):
|
def search(self, channel, p):
|
||||||
L = []
|
L = []
|
||||||
@ -154,8 +161,11 @@ class FlatfileDunnoDB(DunnoDBInterface):
|
|||||||
return L
|
return L
|
||||||
|
|
||||||
def size(self, channel):
|
def size(self, channel):
|
||||||
|
try:
|
||||||
db = self._getDb(channel)
|
db = self._getDb(channel)
|
||||||
return itertools.ilen(db.records())
|
return itertools.ilen(db.records())
|
||||||
|
except EnvironmentError, e:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
DunnoDB = FlatfileDunnoDB
|
DunnoDB = FlatfileDunnoDB
|
||||||
@ -178,8 +188,9 @@ class Dunno(callbacks.Privmsg):
|
|||||||
def invalidCommand(self, irc, msg, tokens):
|
def invalidCommand(self, irc, msg, tokens):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
if ircutils.isChannel(channel):
|
if ircutils.isChannel(channel):
|
||||||
dunno = self.db.random(channel)[1]
|
dunno = self.db.random(channel)
|
||||||
if dunno is not None:
|
if dunno is not None:
|
||||||
|
dunno = dunno[1]
|
||||||
prefixName = self.registryValue('prefixNick', channel)
|
prefixName = self.registryValue('prefixNick', channel)
|
||||||
dunno = plugins.standardSubstitute(irc, msg, dunno)
|
dunno = plugins.standardSubstitute(irc, msg, dunno)
|
||||||
irc.reply(dunno, prefixName=prefixName)
|
irc.reply(dunno, prefixName=prefixName)
|
||||||
|
Loading…
Reference in New Issue
Block a user