diff --git a/plugins/Dict.py b/plugins/Dict.py index 8358eea58..44670906a 100644 --- a/plugins/Dict.py +++ b/plugins/Dict.py @@ -44,6 +44,7 @@ import dictclient import conf import debug import utils +import plugins import ircutils import privmsgs import callbacks @@ -61,47 +62,46 @@ def configure(onStart, afterConnect, advanced): server = something('What server?') onStart.append('dictserver %s' % server) -class Dict(callbacks.Privmsg): +replyTimeout = 'Timeout on the dictd server.' +class Dict(callbacks.Privmsg, plugins.Configurable): threaded = True - dictServer = 'dict.org' + configurables = plugins.ConfigurableDictionary( + [('server', plugins.ConfigurableStrType, 'dict.org', + """Determines what server the bot will connect to to receive + definitions from."""),] + ) def __init__(self): - self.setDictServer(self.dictServer) callbacks.Privmsg.__init__(self) + plugins.Configurable.__init__(self) - def setDictServer(self, server): - self.dictServer = server - try: - conn = dictclient.Connection(server, timeout=3) - self.dictdbs = sets.Set(conn.getdbdescs()) - except socket.timeout: - debug.msg('Timeout on server %s' % server) - self.dictdbs = sets.Set([]) - - def dictserver(self, irc, msg, args): - """[] - - Sets the dictd server the plugin should use. - """ - server = privmsgs.getArgs(args) - try: - self.setDictServer(server) - irc.reply(msg, conf.replySuccess) - except Exception, e: - irc.error(msg, debug.exnToString(e)) + def die(self): + callbacks.Privmsg.die(self) + plugins.Configurable.die(self) def dictionaries(self, irc, msg, args): """takes no arguments. Returns the dictionaries valid for the dict command. """ - irc.reply(msg, utils.commaAndify(self.dictdbs)) + try: + conn = dictclient.Connection(self.configurables.get('server')) + dbs = conf.getdbdescs() + dbs.sort() + irc.reply(msg, utils.commaAndify(dbs)) + except socket.timeout: + irc.error(msg, replyTimeout) def random(self, irc, msg, args): """takes no arguments. Returns a random valid dictionary. """ - irc.reply(msg, random.choice(list(self.dictdbs))) + try: + conn = dictclient.Connection(self.configurables.get('server')) + dbs = conf.getdbdescs() + irc.reply(msg, random.choice(dbs)) + except socket.timeout: + irc.error(msg, replyTimeout) def dict(self, irc, msg, args): """[] @@ -111,13 +111,12 @@ class Dict(callbacks.Privmsg): if not args: raise callbacks.ArgumentError try: - conn = dictclient.Connection(self.dictServer) + conn = dictclient.Connection(self.configurables.get('server')) except socket.timeout: irc.error(msg, 'Timeout on the dict server.') return - if not self.dictdbs: - self.dictdbs = sets.Set(conn.getdbdescs()) - if args[0] in self.dictdbs: + dbs = sets.Set(conn.getdbdescs()) + if args[0] in dbs: dictionary = args.pop(0) else: dictionary = '*' diff --git a/test/test_Dictionary.py b/test/test_Dict.py similarity index 95% rename from test/test_Dictionary.py rename to test/test_Dict.py index 0a5e16f95..7b40b4ced 100644 --- a/test/test_Dictionary.py +++ b/test/test_Dict.py @@ -31,8 +31,8 @@ from test import * -class DictionaryTestCase(PluginTestCase, PluginDocumentation): - plugins = ('Dictionary', 'Misc') +class DictTestCase(PluginTestCase, PluginDocumentation): + plugins = ('Dict', 'Misc') def testDict(self): self.assertNotError('dict slash') self.assertNotRegexp('dict web1913 slash', 'foldoc')