From 06ce1bcb940d3c5b8eda2220caf202dca7ccb104 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 30 Oct 2003 04:36:40 +0000 Subject: [PATCH] Added change. --- plugins/Factoids.py | 38 ++++++++++++++++++++++++++++++++++++++ test/test_Factoids.py | 7 +++++++ 2 files changed, 45 insertions(+) diff --git a/plugins/Factoids.py b/plugins/Factoids.py index 856c244ee..b1807e5ba 100644 --- a/plugins/Factoids.py +++ b/plugins/Factoids.py @@ -351,6 +351,44 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg): utils.nItems(counter, 'factoid'), factoids) irc.reply(msg, s) + def change(self, irc, msg, args): + """[] + + Changes the factoid # associated with according to + . + """ + 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('*?', '%_') def search(self, irc, msg, args): """[] [--{regexp,exact}=] [] diff --git a/test/test_Factoids.py b/test/test_Factoids.py index e7ca071a1..b2ad3c82f 100644 --- a/test/test_Factoids.py +++ b/test/test_Factoids.py @@ -76,6 +76,13 @@ if sqlite is not None: self.assertError('learn foo bar baz') # 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): self.assertNotError('learn jemfinch as my primary author') self.assertNotError('learn strike as a cool guy working on me')