Implement the 'sync' command fully, making the 'remotedb' variable channel specific. See @help sync

This commit is contained in:
Terence Simpson 2008-08-07 12:37:25 +01:00
parent 0a39ae7222
commit 24a296c2d8
2 changed files with 38 additions and 23 deletions

View File

@ -21,7 +21,7 @@ def configure(advanced):
Encyclopedia = conf.registerPlugin('Encyclopedia') Encyclopedia = conf.registerPlugin('Encyclopedia')
conf.registerChannelValue(Encyclopedia, 'database', conf.registerChannelValue(Encyclopedia, 'database',
registry.String('', 'Name of database to use')) registry.String('ubuntu', 'Name of database to use'))
conf.registerGlobalValue(Encyclopedia, 'packagelookup', conf.registerGlobalValue(Encyclopedia, 'packagelookup',
registry.Boolean(True, "Whether to look up packages")) registry.Boolean(True, "Whether to look up packages"))
conf.registerChannelValue(Encyclopedia, 'relaychannel', conf.registerChannelValue(Encyclopedia, 'relaychannel',
@ -40,5 +40,5 @@ conf.registerGlobalValue(Encyclopedia, 'aptdir',
registry.String('', 'Path to apt cache directory',private=True)) registry.String('', 'Path to apt cache directory',private=True))
conf.registerChannelValue(Encyclopedia, 'alert', conf.registerChannelValue(Encyclopedia, 'alert',
registry.String('ops','factoid name used for alerts')) registry.String('ops','factoid name used for alerts'))
conf.registerGlobalValue(Encyclopedia, 'remotedb', conf.registerChannelValue(Encyclopedia, 'remotedb',
registry.String('http://jussi01.com/ubuntu.db', 'Remote location of the master database')) registry.String('http://jussi01.com/ubuntu.db', 'Remote location of the master database'))

View File

@ -598,11 +598,12 @@ class Encyclopedia(callbacks.Plugin):
return "None found" return "None found"
return 'Found: %s' % ', '.join(sorted(ret.keys(), lambda x, y: cmp(ret[x], ret[y]))[:10]) return 'Found: %s' % ', '.join(sorted(ret.keys(), lambda x, y: cmp(ret[x], ret[y]))[:10])
def sync(self, irc, msg, args): def sync(self, irc, msg, args, channel):
"""takes no arguements """[<channel>]
Downloads a copy of the database from the remote server. Downloads a copy of the database from the remote server.
Set the server with the configuration variable supybot.plugins.Encyclopedia.remotedb. Set the server with the channel configuration variable supybot.plugins.Encyclopedia.remotedb.
If <channel> is not set it will default to the channel the command is given in or the global value
""" """
if not capab(msg.prefix, "owner"): if not capab(msg.prefix, "owner"):
irc.error("Sorry, you can't do that") irc.error("Sorry, you can't do that")
@ -610,42 +611,56 @@ class Encyclopedia(callbacks.Plugin):
def download_database(location, dpath): def download_database(location, dpath):
"""Download the database located at location to path dpath""" """Download the database located at location to path dpath"""
import urllib2 import urllib2
tmp_db = "%s%stmp" % (dpath, os.extsep)
fd = urllib2.urlopen(location) fd = urllib2.urlopen(location)
newDb = fd.read() fd2 = open(tmp_db,'w')
fd2.write(fd.read()) # Download to a temparary file
fd.close() fd.close()
fd2 = open(dpath,'w')
fd2.write(newDb)
fd2.close() fd2.close()
# Do some checking to make sure we have an SQLite database
fd2 = open(tmp_db, 'rb')
data = fd.read(47)
if data == '** This file contains an SQLite 2.1 database **': # OK, rename to dpath
os.rename(tmp_db, dpath)
else: # Remove the tmpparary file and raise an error
os.remove(tmp_db)
raise RuntimeError, "Downloaded file was not a SQLite 2.1 database"
def tryReload(): db = self.registryValue('database', channel)
try: rdb = self.registryValue('remotedb', channel)
sys.modules['Encyclopedia'].reloadPlugin() if not db:
except: if channel:
pass irc.error("I don't have a database set for %s" % channel)
return
# Having this configurable is nice, but could lead to errors in *my* code, irc.error("There is no global database set, use 'config supybot.plugins.Encyclopedia.database <database>' to set it")
# So I'll just assume it's always going to be set to 'ubuntu' return
# db = self.registryValue('database',channel) if not rdb:
db = 'ubuntu' if channel:
irc.error("I don't have a remote database set for %s" % channel)
return
irc.error("There is no global remote database set, use 'config supybot.plugins.Encyclopedia.remotedb <url>' ro set it")
return
dbpath = os.path.join(self.registryValue('datadir'), '%s.db' % db) dbpath = os.path.join(self.registryValue('datadir'), '%s.db' % db)
# We're moving files and downloading, lots can go wrong so use lots of try blocks. # We're moving files and downloading, lots can go wrong so use lots of try blocks.
try: try:
os.rename(dbpath, "%s.backup" % dbpath) os.rename(dbpath, "%s.backup" % dbpath)
except: except Exception, e:
self.log.error("Could not rename %s to %s.backup" % (dbpath, dbpath)) self.log.error("Could not rename %s to %s.backup" % (dbpath, dbpath))
self.log.error(utils.exnToString(e))
irc.error("Internal error, see log") irc.error("Internal error, see log")
return return
try: try:
# Downloading can take some time, let the user know we're doing something # Downloading can take some time, let the user know we're doing something
irc.reply("Attemting to download database", prefixNick=False) irc.reply("Attemting to download database", prefixNick=False)
download_database(self.registryValue('remotedb'), dbpath) download_database(rdb, dbpath)
irc.replySuccess() irc.replySuccess()
except: except Exception, e:
self.log.error("Could not download %s to %s" % (self.registryValue('remotedb'), dbpath)) self.log.error("Could not download %s to %s" % (rdb, dbpath))
self.log.error(utils.exnToString(e))
irc.error("Internal error, see log") irc.error("Internal error, see log")
os.rename("%s.backup" % dbpath, dbpath) os.rename("%s.backup" % dbpath, dbpath)
return return
sync = wrap(sync) sync = wrap(sync, [optional("somethingWithoutSpaces")])
Class = Encyclopedia Class = Encyclopedia