Moved dict stuff to its own plugin.

This commit is contained in:
Jeremy Fincher 2003-09-05 20:22:43 +00:00
parent e59f076f48
commit 7501b3a625
4 changed files with 202 additions and 59 deletions

146
plugins/Dict.py Normal file
View File

@ -0,0 +1,146 @@
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Commands that use the dictd protocol to snag stuff off a server.
"""
from baseplugin import *
import random
import dictclient
import conf
import debug
import utils
import privmsgs
import callbacks
def configure(onStart, afterConnect, advanced):
# This will be called by setup.py to configure this module. onStart and
# afterConnect are both lists. Append to onStart the commands you would
# like to be run when the bot is started; append to afterConnect the
# commands you would like to be run when the bot has finished connecting.
from questions import expect, anything, something, yn
onStart.append('load Dict')
print 'The default dictd server is dict.org.'
if yn('Would you like to specify a dictd server?') == 'y':
server = something('What server?')
onStart.append('dictserver %s' % server)
example = utils.wrapLines("""
<jemfinch> @dict socket
<supybot> jemfinch: foldoc, wn, and web1913 responded, 1 shown: wn: socket n 1: a bony hollow into which a structure fits 2: receptacle where something (a pipe or probe or end of a bone) is inserted 3: a receptacle into which an electric device can be inserted
<jemfinch> @dict foldoc socket
<supybot> jemfinch: Chopped: foldoc: socket <networking> The {Berkeley Unix} mechansim for creating a virtual connection between processes. Sockets interface {Unix}'s {standard I/O} with its {network} communication facilities. They can be of two types, stream (bi-directional) or {datagram} (fixed length destination-addressed messages). The socket library function socket() creates a communications end-point or socket and ret
""")
class Dict(callbacks.Privmsg):
threaded = True
dictServer = 'dict.org'
def __init__(self):
self.setDictServer(self.dictServer)
callbacks.Privmsg.__init__(self)
def setDictServer(self, server):
conn = dictclient.Connection(server)
self.dictdbs = sets.Set(conn.getdbdescs())
self.dictServer = server
def dictserver(self, irc, msg, args):
"""[<dictd server>]
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 dictionaries(self, irc, msg, args):
"""takes no arguments.
Returns the dictionaries valid for the dict command.
"""
irc.reply(msg, utils.commaAndify(self.dictdbs))
def randomdictionary(self, irc, msg, args):
"""takes no arguments.
Returns a random valid dictionary.
"""
irc.reply(msg, random.sample(list(self.dictdbs), 1)[0])
def dict(self, irc, msg, args):
"""[<dictionary>] <word>
Looks up the definition of <word> on dict.org's dictd server. If a
a dictionary is specified and the definition is too long, snips it to
an appropriate length.
"""
if args[0] in self.dictdbs:
dictionary = args.pop(0)
else:
dictionary = '*'
word = privmsgs.getArgs(args)
conn = dictclient.Connection(self.dictServer)
definitions = conn.define(dictionary, word)
dbs = sets.Set()
if not definitions:
irc.reply(msg, 'No definition for %r could be found.' % word)
return
L = []
for d in definitions:
dbs.add(ircutils.bold(d.getdb().getname()))
(db, s) = (d.getdb().getname(), d.getdefstr())
db = ircutils.bold(db)
s = utils.normalizeWhitespace(s).rstrip(';.,')
L.append('%s: %s' % (db, s))
utils.sortBy(len, L)
originalFirst = L[0]
ircutils.shrinkList(L, '; ')
if not L:
irc.reply(msg, '%s \x02<snip>\x0f' % originalFirst[:400])
elif dictionary == '*':
s = '%s responded, %s shown: %s' % \
(utils.commaAndify(dbs), len(L), '; '.join(L))
irc.reply(msg, s)
else:
irc.reply('; '.join(L))
Class = Dict
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -63,8 +63,6 @@ sys.stdout = StringIO()
import this
sys.stdout = sys.__stdout__
import dictclient
#import conf
import debug
import utils
@ -153,25 +151,18 @@ example = utils.wrapLines("""
<supybot> 204.152.189.116
<jemfinch> @zen
<supybot> jemfinch: Beautiful is better than ugly.
<jemfinch> @dict socket
<supybot> jemfinch: foldoc, wn, and web1913 responded, 1 shown: wn: socket n 1: a bony hollow into which a structure fits 2: receptacle where something (a pipe or probe or end of a bone) is inserted 3: a receptacle into which an electric device can be inserted
<jemfinch> @dict foldoc socket
<supybot> jemfinch: Chopped: foldoc: socket <networking> The {Berkeley Unix} mechansim for creating a virtual connection between processes. Sockets interface {Unix}'s {standard I/O} with its {network} communication facilities. They can be of two types, stream (bi-directional) or {datagram} (fixed length destination-addressed messages). The socket library function socket() creates a communications end-point or socket and ret
<jemfinch> @whois ohio-state.edu
<supybot> jemfinch: ohio-state.edu <http://www.educause.edu/edudomain> is active; registered 18-aug-1987, updated 19-aug-2003, expires 18-aug-2004.
""")
class FunCommands(callbacks.Privmsg):
priority = 98 # Really just to test.
dictServer = 'dict.org'
def __init__(self):
callbacks.Privmsg.__init__(self)
self.sentMsgs = 0
self.recvdMsgs = 0
self.sentBytes = 0
self.recvdBytes = 0
conn = dictclient.Connection(self.dictServer)
self.dictdbs = sets.Set(conn.getdbdescs().iterkeys())
def inFilter(self, irc, msg):
self.recvdMsgs += 1
@ -852,50 +843,6 @@ class FunCommands(callbacks.Privmsg):
irc.error(msg, 'I couldn\'t find such a domain.')
whois = privmsgs.thread(whois)
def dictionaries(self, irc, msg, args):
"""takes no arguments.
Returns the dictionaries valid for the dict command.
"""
irc.reply(msg, utils.commaAndify(self.dictdbs))
def dict(self, irc, msg, args):
"""[<dictionary>] <word>
Looks up the definition of <word> on dict.org's dictd server. If a
a dictionary is specified and the definition is too long, snips it to
an appropriate length.
"""
if args[0] in self.dictdbs:
dictionary = args.pop(0)
else:
dictionary = '*'
word = privmsgs.getArgs(args)
conn = dictclient.Connection(self.dictServer)
definitions = conn.define(dictionary, word)
dbs = sets.Set()
if not definitions:
irc.reply(msg, 'No definition for %r could be found.' % word)
return
L = []
for d in definitions:
dbs.add(ircutils.bold(d.getdb().getname()))
(db, s) = (d.getdb().getname(), d.getdefstr())
db = ircutils.bold(db)
s = utils.normalizeWhitespace(s).rstrip(';.,')
L.append('%s: %s' % (db, s))
utils.sortBy(len, L)
originalFirst = L[0]
ircutils.shrinkList(L, '; ')
if not L:
irc.reply(msg, '%s \x02<snip>\x0f' % originalFirst[:400])
elif dictionary == '*':
s = '%s responded, %s shown: %s' % \
(utils.commaAndify(dbs), len(L), '; '.join(L))
irc.reply(msg, s)
else:
irc.reply('; '.join(L))
dict = privmsgs.thread(dict)
Class = FunCommands

56
test/test_Dict.py Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
from test import *
class DictTestCase(PluginTestCase):
plugins = ('Dict', 'MiscCommands')
def testHelps(self):
self.assertNotError('list Dict')
self.assertNotError('help dict')
self.assertNotError('morehelp dict')
self.assertNotError('help dictionaries')
self.assertNotError('morehelp dictionaries')
self.assertNotError('help randomdictionary')
self.assertNotError('morehelp randomdictionary')
def testDict(self):
self.assertNotError('dict slash')
self.assertNotRegexp('dict web1913 slash', 'foldoc')
def testDictionaries(self):
self.assertNotError('dictionaries')
def testRandomDictionary(self):
self.assertNotError('randomdictionary')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -103,12 +103,6 @@ class FunCommandsTest(PluginTestCase):
self.assertNotError('whois ohio-state.edu')
self.assertError('whois slashdot.org')
def testDict(self):
self.assertNotError('dict slash')
self.assertNotRegexp('dict web1913 slash', 'foldoc')
def testDictionaries(self):
self.assertNotError('dictionaries')