From 3b2ef3d88fc7de307ba41e5a651cef4a49417983 Mon Sep 17 00:00:00 2001 From: James Vega Date: Fri, 17 Dec 2004 05:18:21 +0000 Subject: [PATCH] Add plugins.getChannel --- plugins/MoobotFactoids.py | 19 ++++--------- plugins/Seen.py | 5 +--- plugins/WordStats.py | 7 ++--- plugins/__init__.py | 11 +++++--- test/test_WordStats.py | 56 +++++++++++++++++++-------------------- 5 files changed, 46 insertions(+), 52 deletions(-) diff --git a/plugins/MoobotFactoids.py b/plugins/MoobotFactoids.py index 7a1ec4e51..c68c3636b 100644 --- a/plugins/MoobotFactoids.py +++ b/plugins/MoobotFactoids.py @@ -320,15 +320,6 @@ class SqliteMoobotDB(object): MoobotDB = plugins.DB('MoobotFactoids', {'sqlite': SqliteMoobotDB}) -## We define our own getChannel so we can take advantage of the channeldb -# wrapper from non-command methods. This means that the bot will use -# supybot.databases.plugins.channelSpecific.channel as the default channel -# when the user is talking to the bot privately. -def getChannel(irc, msg, args=()): - spec = Spec(['channeldb']) - state = spec(irc, msg, args) - return state.channel - class MoobotFactoids(callbacks.Privmsg): callBefore = ['Dunno'] def __init__(self): @@ -369,7 +360,7 @@ class MoobotFactoids(callbacks.Privmsg): else: key = ' '.join(tokens) key = self._sanitizeKey(key) - channel = getChannel(irc, msg) + channel = plugins.getChannel(msg.args[0]) fact = self.db.getFactoid(channel, key) if fact: self.db.updateRequest(channel, key, msg.prefix) @@ -427,7 +418,7 @@ class MoobotFactoids(callbacks.Privmsg): def addFactoid(self, irc, msg, tokens): # First, check and see if the entire message matches a factoid key - channel = getChannel(irc, msg) + channel = plugins.getChannel(msg.args[0]) id = self._getUserId(irc, msg.prefix) (key, fact) = self._getKeyAndFactoid(tokens) # Check and make sure it's not in the DB already @@ -440,7 +431,7 @@ class MoobotFactoids(callbacks.Privmsg): id = self._getUserId(irc, msg.prefix) (key, regexp) = map(' '.join, utils.itersplit('=~'.__eq__, tokens, maxsplit=1)) - channel = getChannel(irc, msg) + channel = plugins.getChannel(msg.args[0]) # Check and make sure it's in the DB fact = self._getFactoid(irc, channel, key) self._checkNotLocked(irc, channel, key) @@ -461,7 +452,7 @@ class MoobotFactoids(callbacks.Privmsg): isAlso = pairs.index(['is', 'also']) key = ' '.join(tokens[:isAlso]) new_text = ' '.join(tokens[isAlso+2:]) - channel = getChannel(irc, msg) + channel = plugins.getChannel(msg.args[0]) fact = self._getFactoid(irc, channel, key) self._checkNotLocked(irc, channel, key) # It's fair game if we get to here @@ -472,7 +463,7 @@ class MoobotFactoids(callbacks.Privmsg): def replaceFactoid(self, irc, msg, tokens): # Must be registered! - channel = getChannel(irc, msg) + channel = plugins.getChannel(msg.args[0]) id = self._getUserId(irc, msg.prefix) del tokens[0] # remove the "no," (key, fact) = self._getKeyAndFactoid(tokens) diff --git a/plugins/Seen.py b/plugins/Seen.py index 3ea2dc367..79ef96f2b 100644 --- a/plugins/Seen.py +++ b/plugins/Seen.py @@ -125,10 +125,7 @@ class Seen(callbacks.Privmsg): def doPrivmsg(self, irc, msg): if irc.isChannel(msg.args[0]): said = ircmsgs.prettyPrint(msg) - channel = msg.args[0] - channelSpecific = conf.supybot.databases.plugins.channelSpecific - if not conf.get(channelSpecific, channel): - channel = conf.get(channelSpecific.channel, channel) + channel = plugins.getChannel(msg.args[0]) self.db.update(channel, msg.nick, said) try: id = ircdb.users.getUserId(msg.prefix) diff --git a/plugins/WordStats.py b/plugins/WordStats.py index 19934eef3..1667b99e9 100644 --- a/plugins/WordStats.py +++ b/plugins/WordStats.py @@ -150,6 +150,7 @@ class WordStatsDB(plugins.ChannelUserDB): (channel, text) = msg.args if not ircutils.isChannel(channel): return + channel = plugins.getChannel(channel) text = text.strip().lower() if not text: return @@ -217,7 +218,7 @@ class WordStats(callbacks.Privmsg): return self.db.addWord(channel, word) irc.replySuccess() - add = wrap(add, ['channel', 'somethingWithoutSpaces']) + add = wrap(add, ['channeldb', 'somethingWithoutSpaces']) def remove(self, irc, msg, args, channel, word): """[] @@ -235,7 +236,7 @@ class WordStats(callbacks.Privmsg): 'on.' % utils.quoted(word)) else: irc.error('I am not currently keeping any word stats.') - remove = wrap(remove, ['channel', 'somethingWithoutSpaces']) + remove = wrap(remove, ['channeldb', 'somethingWithoutSpaces']) def wordstats(self, irc, msg, args, channel, user, word): """[] [] [] @@ -319,7 +320,7 @@ class WordStats(callbacks.Privmsg): except KeyError: irc.error('I have no wordstats for %s.' % user.name) wordstats = wrap(wordstats, - ['channel', + ['channeldb', optional('otherUser'), additional('somethingWithoutSpaces')]) diff --git a/plugins/__init__.py b/plugins/__init__.py index bbbf61f37..853fa7bfb 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -101,7 +101,7 @@ class NoSuitableDatabase(Exception): def __init__(self, suitable): self.suitable = suitable self.suitable.sort() - + def __str__(self): return 'No suitable databases were found. Suitable databases ' \ 'include %s. If you have one of these databases installed, ' \ @@ -169,6 +169,11 @@ def makeChannelFilename(filename, channel=None, dirname=None): os.makedirs(dirname) return os.path.join(dirname, filename) +def getChannel(channel): + channelSpecific = conf.supybot.databases.plugins.channelSpecific + if not conf.get(channelSpecific, channel): + channel = conf.get(channelSpecific.channel, channel) + return channel # XXX This shouldn't be a mixin. This should be contained by classes that # want such behavior. But at this point, it wouldn't gain much for us @@ -251,7 +256,7 @@ class DbiChannelDB(object): db = self._getDb(channel) return getattr(db, attr)(*args, **kwargs) return _getDbAndDispatcher - + # XXX This should eventually be gotten rid of in favor of some dbi thing. At # the very least, it ought to get an interface much closer to dbi.DB. @@ -402,7 +407,7 @@ class ChannelIdDatabasePlugin(callbacks.Privmsg): if ircdb.checkCapability(msg.prefix, cap): return True irc.errorNoCapability(cap) - + def addValidator(self, irc, text): """This should irc.error or raise an exception if text is invalid.""" pass diff --git a/test/test_WordStats.py b/test/test_WordStats.py index 4369a3f4d..dd038e657 100644 --- a/test/test_WordStats.py +++ b/test/test_WordStats.py @@ -54,11 +54,11 @@ class WordStatsTestCase(ChannelPluginTestCase): self.assertNotError('add lol') self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', prefix=self.prefix)) - self.assertResponse('wordstats foo', '\'lol\': 2') + self.assertRegexp('wordstats foo', r'[\'"]lol[\'"]: 2') self.assertNotError('add moo') self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'moo', prefix=self.prefix)) - self.assertResponse('wordstats foo', '\'lol\': 2 and \'moo\': 2') + self.assertRegexp('wordstats foo', r'lol[\'"]: 2 and [\'"]moo[\'"]: 2') def testWordStatsWord(self): userPrefix1 = 'moo!bar@baz'; userNick1 = 'moo' @@ -77,12 +77,12 @@ class WordStatsTestCase(ChannelPluginTestCase): self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', prefix=userPrefix1)) self.assertRegexp('wordstats lol', - '2.*%s: 5.*foo: 2' % userNick1) + r'2.*%s: 5.*foo: 2' % userNick1) for i in range(10): self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', prefix=userPrefix2)) self.assertRegexp('wordstats lol', - '3.*%s: 10.*%s: 5.*foo: 3' % + r'3.*%s: 10.*%s: 5.*foo: 3' % (userNick2, userNick1)) # Check for the extra-swanky stuff too # (note: to do so we must make sure they don't appear in the list, @@ -91,50 +91,50 @@ class WordStatsTestCase(ChannelPluginTestCase): orig = conf.supybot.plugins.WordStats.rankingDisplay() conf.supybot.plugins.WordStats.rankingDisplay.setValue(2) self.assertRegexp('wordstats lol', - 'total.*19 \'lol\'s.*%s: 10.*%s: 5.*' - 'ranked 3 out of 3 \'lol\'ers' % \ + r'total.*19 [\'"]lol[\'"]s.*%s: 10.*%s: 5.*' + r'ranked 3 out of 3 [\'"]lol[\'"]ers' % \ (userNick2, userNick1)) finally: conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig) def testWordStatsUserWord(self): self.assertNotError('add lol') - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 1 time.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 1 time.') self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', prefix=self.prefix)) - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 3 times.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 3 times.') # Now check for case-insensitivity self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'LOL', prefix=self.prefix)) - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 5 times.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 5 times.') # Check and make sure actions get nabbed too self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', prefix=self.prefix)) - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 7 times.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 7 times.') # Check and make sure it handles two words in one message self.assertNotError('add heh') self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol heh', prefix=self.prefix)) - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 9 times.') - self.assertResponse('wordstats foo heh', - 'foo has said \'heh\' 2 times.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 9 times.') + self.assertRegexp('wordstats foo heh', + r'foo has said [\'"]heh[\'"] 2 times.') # It should ignore punctuation around words self.irc.feedMsg(ircmsgs.privmsg(self.channel,'lol, I said "heh"', prefix=self.prefix)) - self.assertResponse('wordstats foo lol', - 'foo has said \'lol\' 11 times.') - self.assertResponse('wordstats foo heh', - 'foo has said \'heh\' 4 times.') + self.assertRegexp('wordstats foo lol', + r'foo has said [\'"]lol[\'"] 11 times.') + self.assertRegexp('wordstats foo heh', + r'foo has said [\'"]heh[\'"] 4 times.') def testAddword(self): self.assertError('add lol!') self.assertNotError('add lolz0r') - self.assertRegexp('wordstats lolz0r', r'1 \'lolz0r\' seen') + self.assertRegexp('wordstats lolz0r', r'1 [\'"]lolz0r[\'"] seen') def testRemoveword(self): # Using a word that's also the name of a user isn't smart since that @@ -142,13 +142,13 @@ class WordStatsTestCase(ChannelPluginTestCase): #self.assertError('wordstats remove foo') self.assertError('wordstats remove blarg') self.assertNotError('wordstats add blarg') - self.assertRegexp('wordstats blarg', r'1 \'blarg\' seen') - self.assertRegexp('wordstats blarg', r'2 \'blarg\'s seen') + self.assertRegexp('wordstats blarg', r'1 [\'"]blarg[\'"] seen') + self.assertRegexp('wordstats blarg', r'2 [\'"]blarg[\'"]s seen') self.assertNotError('wordstats remove blarg') self.assertRegexp('wordstats blarg', r'doesn\'t look like a word I') # Verify that we aren't keeping results from before self.assertNotError('add blarg') - self.assertRegexp('wordstats blarg', r'1 \'blarg\' seen') + self.assertRegexp('wordstats blarg', r'1 [\'"]blarg[\'"] seen') def testWordStatsRankingDisplay(self): self.assertNotError('add lol') @@ -171,8 +171,8 @@ class WordStatsTestCase(ChannelPluginTestCase): prefix=users[i][0])) # Make sure it shows the top 5 self.assertRegexp('wordstats lol', - 'Top 5 \'lol\'ers.*foo9: 9.*foo8: 8.*' - 'foo7: 7.*foo6: 6.*foo5: 5') + r'Top 5 [\'"]lol[\'"]ers.*foo9: 9.*foo8: 8.*' + r'foo7: 7.*foo6: 6.*foo5: 5') finally: conf.supybot.plugins.WordStats.rankingDisplay.setValue(orig)