mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
New implementation of formatting; dropped unformatting entirely.
This commit is contained in:
parent
247f8f4cc6
commit
0e9a7a7057
@ -55,10 +55,12 @@ conf.registerPlugin('Topic')
|
|||||||
conf.registerChannelValue(conf.supybot.plugins.Topic, 'separator',
|
conf.registerChannelValue(conf.supybot.plugins.Topic, 'separator',
|
||||||
registry.StringSurroundedBySpaces(' || ', """Determines what separator is
|
registry.StringSurroundedBySpaces(' || ', """Determines what separator is
|
||||||
used between individually added topics in the channel topic."""))
|
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):
|
class Topic(callbacks.Privmsg):
|
||||||
topicFormatter = '%s (%s)'
|
|
||||||
topicUnformatter = re.compile('(.*) \((\S+)\)')
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
callbacks.Privmsg.__init__(self)
|
callbacks.Privmsg.__init__(self)
|
||||||
self.lastTopics = ircutils.IrcDict()
|
self.lastTopics = ircutils.IrcDict()
|
||||||
@ -71,19 +73,10 @@ class Topic(callbacks.Privmsg):
|
|||||||
separator = self.registryValue('separator', channel)
|
separator = self.registryValue('separator', channel)
|
||||||
return separator.join(topics)
|
return separator.join(topics)
|
||||||
|
|
||||||
def _unformatTopic(self, channel, topic):
|
def _formatTopic(self, irc, msg, channel, topic):
|
||||||
m = self.topicUnformatter.match(topic)
|
formatter = self.registryValue('format', channel)
|
||||||
if m:
|
env = {'topic': topic}
|
||||||
return (m.group(1), m.group(2))
|
return plugins.standardSubstitute(irc, msg, formatter, env)
|
||||||
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 _sendTopic(self, irc, channel, topics):
|
def _sendTopic(self, irc, channel, topics):
|
||||||
topics = [s for s in topics if s and not s.isspace()]
|
topics = [s for s in topics if s and not s.isspace()]
|
||||||
@ -104,7 +97,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
irc.error(s)
|
irc.error(s)
|
||||||
return
|
return
|
||||||
currentTopic = irc.state.getTopic(channel)
|
currentTopic = irc.state.getTopic(channel)
|
||||||
formattedTopic = self._formatTopic(channel, msg, topic)
|
formattedTopic = self._formatTopic(irc, msg, channel, topic)
|
||||||
# Empties are removed by _sendTopic.
|
# Empties are removed by _sendTopic.
|
||||||
self._sendTopic(irc, channel, [currentTopic, formattedTopic])
|
self._sendTopic(irc, channel, [currentTopic, formattedTopic])
|
||||||
add = privmsgs.channel(add)
|
add = privmsgs.channel(add)
|
||||||
@ -182,7 +175,6 @@ class Topic(callbacks.Privmsg):
|
|||||||
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
L = []
|
L = []
|
||||||
for (i, t) in enumerate(topics):
|
for (i, t) in enumerate(topics):
|
||||||
(t, _) = self._unformatTopic(channel, t)
|
|
||||||
L.append('%s: %s' % (i+1, utils.ellipsisify(t, 30)))
|
L.append('%s: %s' % (i+1, utils.ellipsisify(t, 30)))
|
||||||
s = utils.commaAndify(L)
|
s = utils.commaAndify(L)
|
||||||
irc.reply(s)
|
irc.reply(s)
|
||||||
@ -209,7 +201,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if topics:
|
if topics:
|
||||||
try:
|
try:
|
||||||
irc.reply(self._unformatTopic(channel, topics[number])[0])
|
irc.reply(topics[number])
|
||||||
except IndexError:
|
except IndexError:
|
||||||
irc.error('That\'s not a valid topic.')
|
irc.error('That\'s not a valid topic.')
|
||||||
else:
|
else:
|
||||||
@ -249,16 +241,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
irc.error('There are no topics to change.')
|
irc.error('There are no topics to change.')
|
||||||
return
|
return
|
||||||
topic = topics.pop(number)
|
topic = topics.pop(number)
|
||||||
(topic, name) = self._unformatTopic(channel, topic)
|
newTopic = replacer(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)
|
|
||||||
if number < 0:
|
if number < 0:
|
||||||
number = len(topics)+1+number
|
number = len(topics)+1+number
|
||||||
topics.insert(number, newTopic)
|
topics.insert(number, newTopic)
|
||||||
@ -289,15 +272,6 @@ class Topic(callbacks.Privmsg):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
irc.error('That\'s not a valid topic number.')
|
irc.error('That\'s not a valid topic number.')
|
||||||
return
|
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)
|
self._sendTopic(irc, channel, topics)
|
||||||
remove = privmsgs.channel(remove)
|
remove = privmsgs.channel(remove)
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ from testsupport import *
|
|||||||
|
|
||||||
class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
||||||
plugins = ('Topic',)
|
plugins = ('Topic',)
|
||||||
def testTopicRemove(self):
|
def testRemove(self):
|
||||||
self.assertError('topic remove 1')
|
self.assertError('topic remove 1')
|
||||||
_ = self.getMsg('topic add foo')
|
_ = self.getMsg('topic add foo')
|
||||||
_ = self.getMsg('topic add bar')
|
_ = self.getMsg('topic add bar')
|
||||||
@ -44,20 +44,15 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
self.assertNotError('topic remove 1')
|
self.assertNotError('topic remove 1')
|
||||||
self.assertError('topic remove 1')
|
self.assertError('topic remove 1')
|
||||||
|
|
||||||
def testTopicGet(self):
|
def testGet(self):
|
||||||
self.assertError('topic get 1')
|
self.assertError('topic get 1')
|
||||||
_ = self.getMsg('topic add foo')
|
_ = self.getMsg('topic add foo')
|
||||||
_ = self.getMsg('topic add bar')
|
_ = self.getMsg('topic add bar')
|
||||||
_ = self.getMsg('topic add baz')
|
_ = self.getMsg('topic add baz')
|
||||||
self.assertRegexp('topic get 1', '^foo')
|
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')
|
self.assertError('topic get 0')
|
||||||
|
|
||||||
def testTopicAdd(self):
|
def testAdd(self):
|
||||||
m = self.getMsg('topic add foo')
|
m = self.getMsg('topic add foo')
|
||||||
self.assertEqual(m.command, 'TOPIC')
|
self.assertEqual(m.command, 'TOPIC')
|
||||||
self.assertEqual(m.args[0], self.channel)
|
self.assertEqual(m.args[0], self.channel)
|
||||||
@ -66,7 +61,7 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
self.assertEqual(m.command, 'TOPIC')
|
self.assertEqual(m.command, 'TOPIC')
|
||||||
self.assertEqual(m.args[0], self.channel)
|
self.assertEqual(m.args[0], self.channel)
|
||||||
|
|
||||||
def testTopicChange(self):
|
def testChange(self):
|
||||||
_ = self.getMsg('topic add foo')
|
_ = self.getMsg('topic add foo')
|
||||||
_ = self.getMsg('topic add bar')
|
_ = self.getMsg('topic add bar')
|
||||||
_ = self.getMsg('topic add baz')
|
_ = self.getMsg('topic add baz')
|
||||||
@ -110,11 +105,11 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
|
|
||||||
def testList(self):
|
def testList(self):
|
||||||
_ = self.getMsg('topic add foo')
|
_ = self.getMsg('topic add foo')
|
||||||
self.assertResponse('topic list', '1: foo')
|
self.assertRegexp('topic list', '1: foo')
|
||||||
_ = self.getMsg('topic add bar')
|
_ = 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.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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
Reference in New Issue
Block a user