Add --raw option to factoids.whatis, which disables variable substitution on the factoid.

also add test for this.
This commit is contained in:
Daniel Folkinshteyn 2010-04-28 15:27:08 -04:00
parent 976ad82d69
commit e4c51ef517
2 changed files with 25 additions and 13 deletions

View File

@ -239,12 +239,17 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
db.commit() db.commit()
def _replyFactoids(self, irc, msg, key, channel, factoids, def _replyFactoids(self, irc, msg, key, channel, factoids,
number=0, error=True): number=0, error=True, raw=False):
def format_fact(text):
if raw:
return text
else:
return ircutils.standardSubstitute(irc, msg, text)
if factoids: if factoids:
if number: if number:
try: try:
irc.reply(ircutils.standardSubstitute(irc, msg, irc.reply(format_fact(factoids[number-1][0]))
factoids[number-1][0]))
self._updateRank(channel, [factoids[number-1]]) self._updateRank(channel, [factoids[number-1]])
except IndexError: except IndexError:
irc.error('That\'s not a valid number for that key.') irc.error('That\'s not a valid number for that key.')
@ -257,15 +262,13 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
return ircutils.standardSubstitute(irc, msg, return ircutils.standardSubstitute(irc, msg,
formatter, env) formatter, env)
if len(factoids) == 1: if len(factoids) == 1:
irc.reply(ircutils.standardSubstitute(irc, msg, irc.reply(format_fact(prefixer(factoids[0][0])))
prefixer(factoids[0][0])))
else: else:
factoidsS = [] factoidsS = []
counter = 1 counter = 1
for factoid in factoids: for factoid in factoids:
factoidsS.append(format('(#%i) %s', counter, factoidsS.append(format('(#%i) %s', counter,
ircutils.standardSubstitute(irc, msg, format_fact(factoid[0])))
factoid[0])))
counter += 1 counter += 1
irc.replies(factoidsS, prefixer=prefixer, irc.replies(factoidsS, prefixer=prefixer,
joiner=', or ', onlyPrefixFirst=True) joiner=', or ', onlyPrefixFirst=True)
@ -294,13 +297,19 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
else: else:
self._replyApproximateFactoids(irc, msg, channel, key, error=False) self._replyApproximateFactoids(irc, msg, channel, key, error=False)
def whatis(self, irc, msg, args, channel, words): def whatis(self, irc, msg, args, channel, optlist, words):
"""[<channel>] <key> [<number>] """[<channel>] [--raw] <key> [<number>]
Looks up the value of <key> in the factoid database. If given a Looks up the value of <key> in the factoid database. If given a
number, will return only that exact factoid. <channel> is only number, will return only that exact factoid. If '--raw' option is
necessary if the message isn't sent in the channel itself. given, no variable substitution will take place on the factoid.
<channel> is only necessary if the message isn't sent in the channel
itself.
""" """
raw = False
for (option, arg) in optlist:
if option == 'raw':
raw = True
number = None number = None
if len(words) > 1: if len(words) > 1:
if words[-1].isdigit(): if words[-1].isdigit():
@ -310,10 +319,12 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
key = ' '.join(words) key = ' '.join(words)
factoids = self._lookupFactoid(channel, key) factoids = self._lookupFactoid(channel, key)
if factoids: if factoids:
self._replyFactoids(irc, msg, key, channel, factoids, number) self._replyFactoids(irc, msg, key, channel, factoids, number, raw=raw)
else: else:
self._replyApproximateFactoids(irc, msg, channel, key) self._replyApproximateFactoids(irc, msg, channel, key)
whatis = wrap(whatis, ['channel', many('something')]) whatis = wrap(whatis, ['channel',
getopts({'raw': '',}),
many('something')])
def alias(self, irc, msg, args, channel, oldkey, newkey, number): def alias(self, irc, msg, args, channel, oldkey, newkey, number):
"""[<channel>] <oldkey> <newkey> [<number>] """[<channel>] <oldkey> <newkey> [<number>]

View File

@ -177,6 +177,7 @@ class FactoidsTestCase(ChannelPluginTestCase):
def testStandardSubstitute(self): def testStandardSubstitute(self):
self.assertNotError('learn foo as this is $channel, and hour is $hour') self.assertNotError('learn foo as this is $channel, and hour is $hour')
self.assertRegexp('whatis foo', 'this is #test, and hour is \d{1,2}') self.assertRegexp('whatis foo', 'this is #test, and hour is \d{1,2}')
self.assertRegexp('whatis --raw foo', 'this is \$channel, and hour is \$hour')
self.assertNotError('learn bar as this is $$channel escaped') self.assertNotError('learn bar as this is $$channel escaped')
self.assertRegexp('whatis bar', 'this is \$channel') self.assertRegexp('whatis bar', 'this is \$channel')
self.assertNotError('learn bar as this is $minute') self.assertNotError('learn bar as this is $minute')