Voteserv: refactor DB handling to use Supybot's flushers

Closes #19. This removes the voteimport and voteexport commands, since
DB exporting is now handled by the core like it should be.
This commit is contained in:
James Lu 2014-12-11 18:30:00 -08:00
parent 5450bf9e99
commit d0f1640694

View File

@ -36,6 +36,7 @@ import supybot.plugins as plugins
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.ircdb as ircdb import supybot.ircdb as ircdb
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
import supybot.world as world
try: try:
from supybot.i18n import PluginInternationalization from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('Voteserv') _ = PluginInternationalization('Voteserv')
@ -56,34 +57,36 @@ class Voteserv(callbacks.Plugin):
self.__parent = super(Voteserv, self) self.__parent = super(Voteserv, self)
self.__parent.__init__(irc) self.__parent.__init__(irc)
self.vfilename = conf.supybot.directories.data.dirize("votes.db") self.vfilename = conf.supybot.directories.data.dirize("votes.db")
self.loadVoteDB()
world.flushers.append(self.exportVoteDB)
def loadVoteDB(self):
self.log.debug("Voteserv: loading votes database from "+self.vfilename)
try: try:
with open(self.vfilename, "r") as f: with open(self.vfilename, "r") as f:
self.votedb = json.load(f) self.votedb = json.load(f)
except IOError: except IOError:
self.log.debug("Voteserv: failed to load votes database %s" self.log.error("Voteserv: failed to load votes database %s"
", creating a new one..." % self.vfilename) ", creating a new one in memory..." % self.vfilename)
self.votedb = {} self.votedb = {}
except ValueError: except ValueError:
self.log.warning("Voteserv: Invalid JSON found in votes database " self.log.error("Voteserv: Invalid JSON found in votes database "
"%s, replacing it with a new one!" % self.vfilename) "%s, creating a new one in memory..." % self.vfilename)
self.votedb = {}
def loadVoteDB(self):
self.log.debug("Voteserv: loading votes database "+self.vfilename)
with open(self.vfilename, "r") as f:
self.votedb = json.load(f)
def exportVoteDB(self): def exportVoteDB(self):
self.log.debug("Voteserv: exporting votes database "+self.vfilename) self.log.debug("Voteserv: exporting votes database to "+self.vfilename)
with open(self.vfilename, 'w') as f: with open(self.vfilename, 'w') as f:
json.dump(self.votedb, f, indent=4, separators=(',', ': ')) json.dump(self.votedb, f, indent=4, separators=(',', ': '))
f.write("\n") f.write("\n")
def die(self): def die(self):
self.__parent.die()
try: try:
self.exportVoteDB() self.exportVoteDB()
except IOError as e: except IOError as e:
self.log.error("Failed to export votes database: " + str(e)) self.log.error("Failed to export votes database: " + str(e))
world.flushers.remove(self.exportVoteDB)
self.__parent.die()
def _lazyhostmask(self, host): def _lazyhostmask(self, host):
return "*!"+host.split("!",1)[1] return "*!"+host.split("!",1)[1]
@ -118,32 +121,6 @@ class Voteserv(callbacks.Plugin):
self.votedb[action].append(self._lazyhostmask(msg.prefix)) self.votedb[action].append(self._lazyhostmask(msg.prefix))
vote = wrap(vote, ['text']) vote = wrap(vote, ['text'])
def voteexport(self, irc, msg, args):
"""takes no arguments.
Exports votes stored in memory to file: data/votes.db
This is also done automatically when the plugin is unloaded or
reloaded."""
try:
self.exportVoteDB()
except IOError as e:
irc.error("IOError caught exporting DB: "+str(e))
else:
irc.replySuccess()
voteexport = wrap(voteexport, ['admin'])
def voteimport(self, irc, msg, args):
"""takes no arguments.
Imports the vote database for the current network."""
try:
self.loadVoteDB()
except IOError as e:
irc.error("IOError caught importing DB: "+str(e))
else:
irc.replySuccess()
voteimport = wrap(voteimport, ['admin'])
def voteclear(self, irc, msg, args): def voteclear(self, irc, msg, args):
"""takes no arguments. """takes no arguments.