New implementation of formatting; dropped unformatting entirely.

This commit is contained in:
Jeremy Fincher 2004-08-21 07:30:39 +00:00
parent 247f8f4cc6
commit 0e9a7a7057
2 changed files with 18 additions and 49 deletions

View File

@ -55,10 +55,12 @@ conf.registerPlugin('Topic')
conf.registerChannelValue(conf.supybot.plugins.Topic, 'separator',
registry.StringSurroundedBySpaces(' || ', """Determines what separator is
used between individually added topics in the channel topic."""))
conf.registerChannelValue(conf.supybot.plugins.Topic, 'format',
registry.String('$topic ($nick)', """Determines what format is used to add
topics in the topic. All the standard substitutes apply, in addiction to
"$topic" for the topic itself."""))
class Topic(callbacks.Privmsg):
topicFormatter = '%s (%s)'
topicUnformatter = re.compile('(.*) \((\S+)\)')
def __init__(self):
callbacks.Privmsg.__init__(self)
self.lastTopics = ircutils.IrcDict()
@ -71,19 +73,10 @@ class Topic(callbacks.Privmsg):
separator = self.registryValue('separator', channel)
return separator.join(topics)
def _unformatTopic(self, channel, topic):
m = self.topicUnformatter.match(topic)
if m:
return (m.group(1), m.group(2))
else:
return (topic, '')
def _formatTopic(self, channel, msg, topic):
try:
name = ircdb.users.getUser(msg.prefix).name
except KeyError:
name = msg.nick
return self.topicFormatter % (topic, name)
def _formatTopic(self, irc, msg, channel, topic):
formatter = self.registryValue('format', channel)
env = {'topic': topic}
return plugins.standardSubstitute(irc, msg, formatter, env)
def _sendTopic(self, irc, channel, topics):
topics = [s for s in topics if s and not s.isspace()]
@ -104,7 +97,7 @@ class Topic(callbacks.Privmsg):
irc.error(s)
return
currentTopic = irc.state.getTopic(channel)
formattedTopic = self._formatTopic(channel, msg, topic)
formattedTopic = self._formatTopic(irc, msg, channel, topic)
# Empties are removed by _sendTopic.
self._sendTopic(irc, channel, [currentTopic, formattedTopic])
add = privmsgs.channel(add)
@ -182,7 +175,6 @@ class Topic(callbacks.Privmsg):
topics = self._splitTopic(irc.state.getTopic(channel), channel)
L = []
for (i, t) in enumerate(topics):
(t, _) = self._unformatTopic(channel, t)
L.append('%s: %s' % (i+1, utils.ellipsisify(t, 30)))
s = utils.commaAndify(L)
irc.reply(s)
@ -209,7 +201,7 @@ class Topic(callbacks.Privmsg):
topics = self._splitTopic(irc.state.getTopic(channel), channel)
if topics:
try:
irc.reply(self._unformatTopic(channel, topics[number])[0])
irc.reply(topics[number])
except IndexError:
irc.error('That\'s not a valid topic.')
else:
@ -249,16 +241,7 @@ class Topic(callbacks.Privmsg):
irc.error('There are no topics to change.')
return
topic = topics.pop(number)
(topic, name) = self._unformatTopic(channel, topic)
try:
senderName = ircdb.users.getUser(msg.prefix).name
except KeyError:
senderName = msg.nick
if name and name != senderName and \
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error('You can only modify your own topics.')
return
newTopic = self.topicFormatter % (replacer(topic), name)
newTopic = replacer(topic)
if number < 0:
number = len(topics)+1+number
topics.insert(number, newTopic)
@ -289,15 +272,6 @@ class Topic(callbacks.Privmsg):
except IndexError:
irc.error('That\'s not a valid topic number.')
return
(topic, name) = self._unformatTopic(channel, topic)
try:
username = ircdb.users.getUser(msg.prefix).name
except KeyError:
username = msg.nick
if name and name != username and \
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error('You can only remove your own topics.')
return
self._sendTopic(irc, channel, topics)
remove = privmsgs.channel(remove)

View File

@ -33,7 +33,7 @@ from testsupport import *
class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('Topic',)
def testTopicRemove(self):
def testRemove(self):
self.assertError('topic remove 1')
_ = self.getMsg('topic add foo')
_ = self.getMsg('topic add bar')
@ -44,20 +44,15 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
self.assertNotError('topic remove 1')
self.assertError('topic remove 1')
def testTopicGet(self):
def testGet(self):
self.assertError('topic get 1')
_ = self.getMsg('topic add foo')
_ = self.getMsg('topic add bar')
_ = self.getMsg('topic add baz')
self.assertRegexp('topic get 1', '^foo')
self.assertNotRegexp('topic get 1', self.nick)
self.assertRegexp('topic get 2', '^bar')
self.assertNotRegexp('topic get 2', self.nick)
self.assertRegexp('topic get 3', '^baz')
self.assertNotRegexp('topic get 3', self.nick)
self.assertError('topic get 0')
def testTopicAdd(self):
def testAdd(self):
m = self.getMsg('topic add foo')
self.assertEqual(m.command, 'TOPIC')
self.assertEqual(m.args[0], self.channel)
@ -66,7 +61,7 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
self.assertEqual(m.command, 'TOPIC')
self.assertEqual(m.args[0], self.channel)
def testTopicChange(self):
def testChange(self):
_ = self.getMsg('topic add foo')
_ = self.getMsg('topic add bar')
_ = self.getMsg('topic add baz')
@ -110,11 +105,11 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
def testList(self):
_ = self.getMsg('topic add foo')
self.assertResponse('topic list', '1: foo')
self.assertRegexp('topic list', '1: foo')
_ = self.getMsg('topic add bar')
self.assertResponse('topic list', '1: foo and 2: bar')
self.assertRegexp('topic list', '1: foo .*2: bar')
_ = self.getMsg('topic add baz')
self.assertResponse('topic list', '1: foo, 2: bar, and 3: baz')
self.assertRegexp('topic list', '1: foo .* 2: bar .* and 3: baz')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: