Updated to include timeouts on the connection; changed name back to Dict.py.

This commit is contained in:
Jeremy Fincher 2003-11-12 00:46:26 +00:00
parent bb231a19e5
commit dd4c09e1a4
2 changed files with 29 additions and 18 deletions

View File

@ -20,21 +20,21 @@ import socket, re
version = '1.0' version = '1.0'
def dequote(str): def dequote(s):
"""Will remove single or double quotes from the start and end of a string """Will remove single or double quotes from the start and end of a string
and return the result.""" and return the result."""
quotechars = "'\"" quotechars = "'\""
while len(str) and str[0] in quotechars: while s and s[0] in quotechars:
str = str[1:] s = s[1:]
while len(str) and str[-1] in quotechars: while str and str[-1] in quotechars:
str = str[0:-1] s = s[0:-1]
return str return s
def enquote(str): def enquote(s):
"""This function will put a string in double quotes, properly """This function will put a string in double quotes, properly
escaping any existing double quotes with a backslash. It will escaping any existing double quotes with a backslash. It will
return the result.""" return the result."""
return '"' + str.replace('"', "\\\"") + '"' return '"' + s.replace('"', "\\\"") + '"'
class Connection: class Connection:
"""This class is used to establish a connection to a database server. """This class is used to establish a connection to a database server.
@ -42,8 +42,9 @@ class Connection:
Instantiating it takes two optional arguments: a hostname (a string) Instantiating it takes two optional arguments: a hostname (a string)
and a port (an int). The hostname defaults to localhost and a port (an int). The hostname defaults to localhost
and the port to 2628, the port specified in RFC.""" and the port to 2628, the port specified in RFC."""
def __init__(self, hostname = 'localhost', port = 2628): def __init__(self, hostname='localhost', port=2628, timeout=10):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(timeout)
self.sock.connect((hostname, port)) self.sock.connect((hostname, port))
self.rfile = self.sock.makefile("rt") self.rfile = self.sock.makefile("rt")
self.wfile = self.sock.makefile("wt", 0) self.wfile = self.sock.makefile("wt", 0)

View File

@ -37,6 +37,7 @@ import plugins
import sets import sets
import random import random
import socket
import dictclient import dictclient
@ -60,7 +61,7 @@ def configure(onStart, afterConnect, advanced):
server = something('What server?') server = something('What server?')
onStart.append('dictserver %s' % server) onStart.append('dictserver %s' % server)
class Dictionary(callbacks.Privmsg): class Dict(callbacks.Privmsg):
threaded = True threaded = True
dictServer = 'dict.org' dictServer = 'dict.org'
def __init__(self): def __init__(self):
@ -68,9 +69,13 @@ class Dictionary(callbacks.Privmsg):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
def setDictServer(self, server): def setDictServer(self, server):
conn = dictclient.Connection(server)
self.dictdbs = sets.Set(conn.getdbdescs())
self.dictServer = 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): def dictserver(self, irc, msg, args):
"""[<dictd server>] """[<dictd server>]
@ -91,7 +96,7 @@ class Dictionary(callbacks.Privmsg):
""" """
irc.reply(msg, utils.commaAndify(self.dictdbs)) irc.reply(msg, utils.commaAndify(self.dictdbs))
def randomdictionary(self, irc, msg, args): def random(self, irc, msg, args):
"""takes no arguments. """takes no arguments.
Returns a random valid dictionary. Returns a random valid dictionary.
@ -101,18 +106,22 @@ class Dictionary(callbacks.Privmsg):
def dict(self, irc, msg, args): def dict(self, irc, msg, args):
"""[<dictionary>] <word> """[<dictionary>] <word>
Looks up the definition of <word> on dict.org's dictd server. If a Looks up the definition of <word> on dict.org's dictd server.
a dictionary is specified and the definition is too long, snips it to
an appropriate length.
""" """
if not args: if not args:
raise callbacks.ArgumentError raise callbacks.ArgumentError
try:
conn = dictclient.Connection(self.dictServer)
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: if args[0] in self.dictdbs:
dictionary = args.pop(0) dictionary = args.pop(0)
else: else:
dictionary = '*' dictionary = '*'
word = privmsgs.getArgs(args) word = privmsgs.getArgs(args)
conn = dictclient.Connection(self.dictServer)
definitions = conn.define(dictionary, word) definitions = conn.define(dictionary, word)
dbs = sets.Set() dbs = sets.Set()
if not definitions: if not definitions:
@ -137,6 +146,7 @@ class Dictionary(callbacks.Privmsg):
irc.reply(msg, s) irc.reply(msg, s)
Class = Dictionary Class = Dict
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: