Added change.

This commit is contained in:
Jeremy Fincher 2003-10-30 04:36:40 +00:00
parent af32b12745
commit 06ce1bcb94
2 changed files with 45 additions and 0 deletions

View File

@ -351,6 +351,44 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg):
utils.nItems(counter, 'factoid'), factoids) utils.nItems(counter, 'factoid'), factoids)
irc.reply(msg, s) irc.reply(msg, s)
def change(self, irc, msg, args):
"""[<channel>] <key> <number> <regexp>
Changes the factoid #<number> associated with <key> according to
<regexp>.
"""
channel = privmsgs.getChannel(msg, args)
(key, number, regexp) = privmsgs.getArgs(args, needed=3)
try:
replacer = utils.perlReToReplacer(regexp)
except ValueError, e:
irc.error(msg, 'Invalid regexp: %s' % e)
return
try:
number = int(number)
if number <= 0:
raise ValueError
except ValueError:
irc.error(msg, 'Invalid key id.')
return
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT factoids.id, factoids.fact
FROM keys, factoids
WHERE keys.key LIKE %s AND
keys.id=factoids.key_id""", key)
if cursor.rowcount == 0:
irc.error(msg, 'I couldn\'t find any key %r' % key)
return
elif cursor.rowcount < number:
irc.error(msg, 'That\'s not a valid key id.')
return
(id, fact) = cursor.fetchall()[number-1]
newfact = replacer(fact)
cursor.execute("UPDATE factoids SET fact=%s WHERE id=%s", newfact, id)
db.commit()
irc.reply(msg, conf.replySuccess)
_sqlTrans = string.maketrans('*?', '%_') _sqlTrans = string.maketrans('*?', '%_')
def search(self, irc, msg, args): def search(self, irc, msg, args):
"""[<channel>] [--{regexp,exact}=<value>] [<glob>] """[<channel>] [--{regexp,exact}=<value>] [<glob>]

View File

@ -76,6 +76,13 @@ if sqlite is not None:
self.assertError('learn foo bar baz') # No 'as' self.assertError('learn foo bar baz') # No 'as'
self.assertError('learn foo bar') # No 'as' self.assertError('learn foo bar') # No 'as'
def testChangeFactoid(self):
self.assertNotError('learn foo as bar')
self.assertNotError('change foo 1 s/bar/baz/')
self.assertRegexp('whatis foo', 'baz')
self.assertError('change foo 2 s/bar/baz/')
self.assertError('change foo 0 s/bar/baz/')
def testSearchFactoids(self): def testSearchFactoids(self):
self.assertNotError('learn jemfinch as my primary author') self.assertNotError('learn jemfinch as my primary author')
self.assertNotError('learn strike as a cool guy working on me') self.assertNotError('learn strike as a cool guy working on me')