Replace Factoids.factoidPrefix with Factoids.format

Factoids.format makes use of registry.TemplatedString so the user can specify
excactly how they want the factoid reply to be formed.

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
James Vega 2009-10-15 22:37:59 -04:00
parent 5af38453a8
commit 4c9efc044b
3 changed files with 25 additions and 11 deletions

View File

@ -4,6 +4,10 @@ utils.str.perlVariableSubstitute is deprecated in favor of using Python's
string.Template directly. perlVariableSubstitute will be removed in a future
release.
Factoids' config variable supybot.plugins.Factoids.factoidPrefix has been
replaced by supybot.plugins.Factoids.format, which allows the user to
determine exactly how replies to Factoid queries are formatted.
Version 0.83.4.1

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2009, James Vega
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -38,6 +39,10 @@ def configure(advanced):
from supybot.questions import expect, anything, something, yn
conf.registerPlugin('Factoids', True)
class FactoidFormat(registry.TemplatedString):
"""Value must include $value, otherwise the factoid's value would be left
out."""
requiredTemplates = ['value']
Factoids = conf.registerPlugin('Factoids')
conf.registerChannelValue(Factoids, 'learnSeparator',
@ -53,9 +58,10 @@ conf.registerChannelValue(Factoids, 'replyWhenInvalidCommand',
registry.Boolean(True, """Determines whether the bot will reply to invalid
commands by searching for a factoid; basically making the whatis
unnecessary when you want all factoids for a given key."""))
conf.registerChannelValue(Factoids, 'factoidPrefix',
registry.StringWithSpaceOnRight('could be ', """Determines the string that
factoids will be introduced by."""))
conf.registerChannelValue(Factoids, 'format',
FactoidFormat('$key could be $value.', """Determines the format of
the response given when a factoid's value is requested. All the standard
substitutes apply, in addition to "$key" for the factoid's key and "$value"
for the factoid's value."""))
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -164,7 +164,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
LIMIT 20""", key)
return [t[0] for t in cursor.fetchall()]
def _replyFactoids(self, irc, channel, key, factoids,
def _replyFactoids(self, irc, msg, key, factoids,
number=0, error=True):
if factoids:
if number:
@ -174,17 +174,21 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
irc.error('That\'s not a valid number for that key.')
return
else:
intro = self.registryValue('factoidPrefix', channel)
prefix = format('%q %s', key, intro)
env = {'key': key}
def prefixer(v):
env['value'] = v
formatter = self.registryValue('format', msg.args[0])
return ircutils.standardSubstitute(irc, msg,
formatter, env)
if len(factoids) == 1:
irc.reply(prefix + factoids[0])
irc.reply(prefixer(factoids[0]))
else:
factoidsS = []
counter = 1
for factoid in factoids:
factoidsS.append(format('(#%i) %s', counter, factoid))
counter += 1
irc.replies(factoidsS, prefixer=prefix,
irc.replies(factoidsS, prefixer=prefixer,
joiner=', or ', onlyPrefixFirst=True)
elif error:
irc.error('No factoid matches that key.')
@ -195,7 +199,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if self.registryValue('replyWhenInvalidCommand', channel):
key = ' '.join(tokens)
factoids = self._lookupFactoid(channel, key)
self._replyFactoids(irc, channel, key, factoids, error=False)
self._replyFactoids(irc, msg, key, factoids, error=False)
def whatis(self, irc, msg, args, channel, words):
"""[<channel>] <key> [<number>]
@ -212,7 +216,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
irc.errorInvalid('key id')
key = ' '.join(words)
factoids = self._lookupFactoid(channel, key)
self._replyFactoids(irc, channel, key, factoids, number)
self._replyFactoids(irc, msg, key, factoids, number)
whatis = wrap(whatis, ['channel', many('something')])
def lock(self, irc, msg, args, channel, key):