mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Updated to include timeouts on the connection; changed name back to Dict.py.
This commit is contained in:
parent
bb231a19e5
commit
dd4c09e1a4
@ -20,21 +20,21 @@ import socket, re
|
||||
|
||||
version = '1.0'
|
||||
|
||||
def dequote(str):
|
||||
def dequote(s):
|
||||
"""Will remove single or double quotes from the start and end of a string
|
||||
and return the result."""
|
||||
quotechars = "'\""
|
||||
while len(str) and str[0] in quotechars:
|
||||
str = str[1:]
|
||||
while len(str) and str[-1] in quotechars:
|
||||
str = str[0:-1]
|
||||
return str
|
||||
while s and s[0] in quotechars:
|
||||
s = s[1:]
|
||||
while str and str[-1] in quotechars:
|
||||
s = s[0:-1]
|
||||
return s
|
||||
|
||||
def enquote(str):
|
||||
def enquote(s):
|
||||
"""This function will put a string in double quotes, properly
|
||||
escaping any existing double quotes with a backslash. It will
|
||||
return the result."""
|
||||
return '"' + str.replace('"', "\\\"") + '"'
|
||||
return '"' + s.replace('"', "\\\"") + '"'
|
||||
|
||||
class Connection:
|
||||
"""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)
|
||||
and a port (an int). The hostname defaults to localhost
|
||||
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.settimeout(timeout)
|
||||
self.sock.connect((hostname, port))
|
||||
self.rfile = self.sock.makefile("rt")
|
||||
self.wfile = self.sock.makefile("wt", 0)
|
||||
|
@ -37,6 +37,7 @@ import plugins
|
||||
|
||||
import sets
|
||||
import random
|
||||
import socket
|
||||
|
||||
import dictclient
|
||||
|
||||
@ -60,7 +61,7 @@ def configure(onStart, afterConnect, advanced):
|
||||
server = something('What server?')
|
||||
onStart.append('dictserver %s' % server)
|
||||
|
||||
class Dictionary(callbacks.Privmsg):
|
||||
class Dict(callbacks.Privmsg):
|
||||
threaded = True
|
||||
dictServer = 'dict.org'
|
||||
def __init__(self):
|
||||
@ -68,9 +69,13 @@ class Dictionary(callbacks.Privmsg):
|
||||
callbacks.Privmsg.__init__(self)
|
||||
|
||||
def setDictServer(self, server):
|
||||
conn = dictclient.Connection(server)
|
||||
self.dictdbs = sets.Set(conn.getdbdescs())
|
||||
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):
|
||||
"""[<dictd server>]
|
||||
@ -91,7 +96,7 @@ class Dictionary(callbacks.Privmsg):
|
||||
"""
|
||||
irc.reply(msg, utils.commaAndify(self.dictdbs))
|
||||
|
||||
def randomdictionary(self, irc, msg, args):
|
||||
def random(self, irc, msg, args):
|
||||
"""takes no arguments.
|
||||
|
||||
Returns a random valid dictionary.
|
||||
@ -101,18 +106,22 @@ class Dictionary(callbacks.Privmsg):
|
||||
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.
|
||||
Looks up the definition of <word> on dict.org's dictd server.
|
||||
"""
|
||||
if not args:
|
||||
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:
|
||||
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:
|
||||
@ -137,6 +146,7 @@ class Dictionary(callbacks.Privmsg):
|
||||
irc.reply(msg, s)
|
||||
|
||||
|
||||
Class = Dictionary
|
||||
Class = Dict
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
Loading…
Reference in New Issue
Block a user