Fixed spell problems; stopped caching spell fds (it's not called often enough to care, or go through the trouble).

This commit is contained in:
Jeremy Fincher 2003-11-15 04:25:28 +00:00
parent a8f31360e6
commit 28d371aeca
1 changed files with 21 additions and 21 deletions

View File

@ -97,10 +97,10 @@ def progstats():
sys.version.translate(string.ascii, '\r\n')) sys.version.translate(string.ascii, '\r\n'))
return response return response
class TimeoutError(Exception): class TimeoutError(IOError):
pass pass
def pipeReadline(fd, timeout=0.5): def pipeReadline(fd, timeout=0.75):
(r, _, _) = select.select([fd], [], [], timeout) (r, _, _) = select.select([fd], [], [], timeout)
if r: if r:
return r[0].readline() return r[0].readline()
@ -113,19 +113,12 @@ class Unix(callbacks.Privmsg):
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
# Initialize a file descriptor for the spell module. # Initialize a file descriptor for the spell module.
spellCmd = utils.findBinaryInPath('aspell') self.spellCmd = utils.findBinaryInPath('aspell')
if not spellCmd: if not self.spellCmd:
spellCmd = utils.findBinaryInPath('ispell') self.spellCmd = utils.findBinaryInPath('ispell')
(self._spellRead, self._spellWrite) = popen2.popen4([spellCmd, '-a'],0)
self._spellRead.readline() # Ignore the banner.
self.fortuneCmd = utils.findBinaryInPath('fortune') self.fortuneCmd = utils.findBinaryInPath('fortune')
self.wtfCmd = utils.findBinaryInPath('wtf') self.wtfCmd = utils.findBinaryInPath('wtf')
def die(self):
# close the filehandles
for h in (self._spellRead, self._spellWrite):
h.close()
def errno(self, irc, msg, args): def errno(self, irc, msg, args):
"""<error number or code> """<error number or code>
@ -187,16 +180,23 @@ class Unix(callbacks.Privmsg):
if ' ' in word: if ' ' in word:
irc.error(msg, 'Spaces aren\'t allowed in the word.') irc.error(msg, 'Spaces aren\'t allowed in the word.')
return return
self._spellWrite.write(word)
self._spellWrite.write('\n')
try: try:
line = pipeReadline(self._spellRead) (r, w) = popen2.popen4([self.spellCmd, '-a'])
# aspell puts extra whitespace, ignore it r.readline() # Banner.
while line == '\n': w.write(word)
line = pipeReadline(self._spellRead) w.write('\n')
except TimeoutError: w.flush()
irc.error(msg, 'The spell command didn\'t return usefully.') try:
return line = pipeReadline(r)
# aspell puts extra whitespace, ignore it
while line == '\n':
line = pipeReadline(r)
except TimeoutError:
irc.error(msg, 'The spell command timed out.')
return
finally:
r.close()
w.close()
# parse the output # parse the output
if line[0] in '*+': if line[0] in '*+':
resp = '"%s" may be spelled correctly.' % word resp = '"%s" may be spelled correctly.' % word