Implemented usability fix (RFE #849691) where if listkeys returns only one

factoid key, it should just return the value.
This commit is contained in:
Daniel DiPaolo 2003-11-26 16:20:57 +00:00
parent d64f5314f8
commit 8dc24b2d50
2 changed files with 32 additions and 7 deletions

View File

@ -161,7 +161,7 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
db.close() db.close()
del db del db
def parseFactoid(self, irc, msg, fact): def _parseFactoid(self, irc, msg, fact):
type = "define" # Default is to just spit the factoid back as a type = "define" # Default is to just spit the factoid back as a
# definition of what the key is (i.e., "foo is bar") # definition of what the key is (i.e., "foo is bar")
newfact = pickOptions(fact) newfact = pickOptions(fact)
@ -186,6 +186,21 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
hostmask, int(time.time()), key) hostmask, int(time.time()), key)
db.commit() db.commit()
def randomfactoid(self, irc, msg, args):
"""<takes no arguments>
Displays a random factoid (along with its key) from the database.
"""
db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT fact, key FROM factoids
ORDER BY random() LIMIT 1""")
if cursor.rowcount == 0:
irc.error(msg, 'No factoids in the database.')
return
(fact, key) = cursor.fetchone()
irc.reply(msg, "%r is %r" % (key, fact))
def invalidCommand(self, irc, msg, tokens): def invalidCommand(self, irc, msg, tokens):
key = ' '.join(tokens) key = ' '.join(tokens)
key = key.rstrip('?!') key = key.rstrip('?!')
@ -203,7 +218,7 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
hostmask = msg.prefix hostmask = msg.prefix
self.updateFactoidRequest(key, hostmask) self.updateFactoidRequest(key, hostmask)
# Now actually get the factoid and respond accordingly # Now actually get the factoid and respond accordingly
(type, text) = self.parseFactoid(irc, msg, fact) (type, text) = self._parseFactoid(irc, msg, fact)
if type == "action": if type == "action":
irc.reply(msg, text, action=True) irc.reply(msg, text, action=True)
elif type == "reply": elif type == "reply":
@ -211,16 +226,22 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
elif type == "define": elif type == "define":
irc.reply(msg, "%s is %s" % (key, text), prefixName=False) irc.reply(msg, "%s is %s" % (key, text), prefixName=False)
else: else:
irc.error(msg, "Spurious type from parseFactoid.") irc.error(msg, "Spurious type from _parseFactoid.")
return True return True
def addFactoid(self, irc, msg, match): def addFactoid(self, irc, msg, match):
r"^(.+?)\s+(?:is|_is_)\s+(.+)" r"^(.+?)\s+(?:is|_is_)\s+(.+)"
# Check and see if there is a command that matches this that didn't
# get caught due to nesting
# cb = callbacks.findCallbackForCommand(irc, msg)
# if cb:
# irc.reply(msg, irc.getHelp(cb[0].config))
# return
# First, check and see if the entire message matches a factoid key # First, check and see if the entire message matches a factoid key
db = self.dbHandler.getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT * FROM factoids WHERE key LIKE %s""", key = match.group().rstrip('?! ')
match.group().rstrip('?! ')) cursor.execute("""SELECT * FROM factoids WHERE key LIKE %s""", key)
if cursor.rowcount != 0: if cursor.rowcount != 0:
self.invalidCommand(irc, msg, callbacks.tokenize(match.group())) self.invalidCommand(irc, msg, callbacks.tokenize(match.group()))
return return
@ -579,6 +600,10 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, "No keys matching %r found." % search) irc.reply(msg, "No keys matching %r found." % search)
return return
elif cursor.rowcount == 1:
key = cursor.fetchone()[0]
self.invalidCommand(irc, msg, [key])
return
keys = [repr(tup[0]) for tup in cursor.fetchall()] keys = [repr(tup[0]) for tup in cursor.fetchall()]
s = "Key search for %r (%s found): %s" % \ s = "Key search for %r (%s found): %s" % \
(search, len(keys), utils.commaAndify(keys)) (search, len(keys), utils.commaAndify(keys))

View File

@ -226,8 +226,8 @@ if sqlite is not None:
def testListkeys(self): def testListkeys(self):
self.assertResponse('listkeys %', 'No keys matching \'%\' found.') self.assertResponse('listkeys %', 'No keys matching \'%\' found.')
self.assertNotError('moo is <reply>moo') self.assertNotError('moo is <reply>moo')
self.assertResponse('listkeys moo', # If only one key, it should respond with the factoid
'Key search for \'moo\' (1 found): \'moo\'') self.assertResponse('listkeys moo', 'moo')
self.assertResponse('listkeys foo', 'No keys matching \'foo\' ' self.assertResponse('listkeys foo', 'No keys matching \'foo\' '
'found.') 'found.')
# Throw in a bunch more # Throw in a bunch more