mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 11:09:23 +01:00
Refactoring, added set, topic, and insert.
This commit is contained in:
parent
07757282f2
commit
18dce894a5
164
plugins/Topic.py
164
plugins/Topic.py
@ -87,7 +87,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
env = {'topic': topic}
|
env = {'topic': topic}
|
||||||
return plugins.standardSubstitute(irc, msg, formatter, env)
|
return plugins.standardSubstitute(irc, msg, formatter, env)
|
||||||
|
|
||||||
def _sendTopic(self, irc, channel, topics):
|
def _sendTopics(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()]
|
||||||
self.lastTopics[channel] = topics
|
self.lastTopics[channel] = topics
|
||||||
newTopic = self._joinTopic(channel, topics)
|
newTopic = self._joinTopic(channel, topics)
|
||||||
@ -101,7 +101,29 @@ class Topic(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def add(self, irc, msg, args, channel):
|
def _topicNumber(self, irc, n, topics=None):
|
||||||
|
try:
|
||||||
|
n = int(n)
|
||||||
|
if not n:
|
||||||
|
raise ValueError
|
||||||
|
if n > 0:
|
||||||
|
n -= 1
|
||||||
|
if topics is not None:
|
||||||
|
topics[n]
|
||||||
|
return n
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
irc.error('That\'s not a valid topic number.', Raise=True)
|
||||||
|
|
||||||
|
def topic(self, irc, msg, args, channel):
|
||||||
|
"""[<channel>] <topic>
|
||||||
|
|
||||||
|
Sets the topic of <channel> to <topic>.
|
||||||
|
"""
|
||||||
|
topic = privmsgs.getArgs(args)
|
||||||
|
irc.queueMsg(ircmsgs.topic(channel, topic))
|
||||||
|
topic = privmsgs.channel(topic)
|
||||||
|
|
||||||
|
def add(self, irc, msg, args, channel, insert=False):
|
||||||
"""[<channel>] <topic>
|
"""[<channel>] <topic>
|
||||||
|
|
||||||
Adds <topic> to the topics for <channel>. <channel> is only necessary
|
Adds <topic> to the topics for <channel>. <channel> is only necessary
|
||||||
@ -114,12 +136,24 @@ class Topic(callbacks.Privmsg):
|
|||||||
s = 'You can\'t have %s in your topic' % separator
|
s = 'You can\'t have %s in your topic' % separator
|
||||||
irc.error(s)
|
irc.error(s)
|
||||||
return
|
return
|
||||||
currentTopic = irc.state.getTopic(channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
formattedTopic = self._formatTopic(irc, msg, channel, topic)
|
formattedTopic = self._formatTopic(irc, msg, channel, topic)
|
||||||
# Empties are removed by _sendTopic.
|
if insert:
|
||||||
self._sendTopic(irc, channel, [currentTopic, formattedTopic])
|
topics.insert(0, formattedTopic)
|
||||||
|
else:
|
||||||
|
topics.append(formattedTopic)
|
||||||
|
self._sendTopics(irc, channel, topics)
|
||||||
add = privmsgs.channel(add)
|
add = privmsgs.channel(add)
|
||||||
|
|
||||||
|
def insert(self, irc, msg, args):
|
||||||
|
"""[<channel>] <topic>
|
||||||
|
|
||||||
|
Adds <topic> to the topics for <channel> at the beginning of the topics
|
||||||
|
currently on <channel>. <channel> is only necessary if the message
|
||||||
|
isn't sent in the channel itself.
|
||||||
|
"""
|
||||||
|
self.add(irc, msg, args, insert=True)
|
||||||
|
|
||||||
def shuffle(self, irc, msg, args, channel):
|
def shuffle(self, irc, msg, args, channel):
|
||||||
"""[<channel>]
|
"""[<channel>]
|
||||||
|
|
||||||
@ -138,7 +172,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
original = topics[:]
|
original = topics[:]
|
||||||
while topics == original:
|
while topics == original:
|
||||||
random.shuffle(topics)
|
random.shuffle(topics)
|
||||||
self._sendTopic(irc, channel, topics)
|
self._sendTopics(irc, channel, topics)
|
||||||
shuffle = privmsgs.channel(shuffle)
|
shuffle = privmsgs.channel(shuffle)
|
||||||
|
|
||||||
def reorder(self, irc, msg, args, channel):
|
def reorder(self, irc, msg, args, channel):
|
||||||
@ -159,30 +193,15 @@ class Topic(callbacks.Privmsg):
|
|||||||
irc.error('All topic numbers must be specified.')
|
irc.error('All topic numbers must be specified.')
|
||||||
return
|
return
|
||||||
order = privmsgs.getArgs(args, required=num)
|
order = privmsgs.getArgs(args, required=num)
|
||||||
if topics:
|
for i,p in enumerate(order):
|
||||||
for i,p in enumerate(order):
|
order[i] = self._topicNumber(irc, p, topics=topics)
|
||||||
try:
|
if order[i] < 0:
|
||||||
p = int(p)
|
order[i] += num
|
||||||
if p > 0:
|
if sorted(order) != range(num):
|
||||||
order[i] = p - 1
|
irc.error('Duplicate topic numbers cannot be specified.')
|
||||||
elif p == 0:
|
return
|
||||||
irc.error('0 is not a valid topic number.')
|
newtopics = [topics[i] for i in order]
|
||||||
return
|
self._sendTopics(irc, channel, newtopics)
|
||||||
else:
|
|
||||||
order[i] = num + p
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The positions must be valid integers.')
|
|
||||||
return
|
|
||||||
if sorted(order) != range(num):
|
|
||||||
irc.error('Duplicate topic numbers cannot be specified.')
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
newtopics = [topics[i] for i in order]
|
|
||||||
self._sendTopic(irc, channel, newtopics)
|
|
||||||
except IndexError:
|
|
||||||
irc.error('An invalid topic number was specified.')
|
|
||||||
else:
|
|
||||||
irc.error('There are no topics to reorder.')
|
|
||||||
reorder = privmsgs.channel(reorder)
|
reorder = privmsgs.channel(reorder)
|
||||||
|
|
||||||
def list(self, irc, msg, args, channel):
|
def list(self, irc, msg, args, channel):
|
||||||
@ -208,22 +227,10 @@ class Topic(callbacks.Privmsg):
|
|||||||
isn't sent in the channel itself.
|
isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
number = privmsgs.getArgs(args)
|
number = privmsgs.getArgs(args)
|
||||||
try:
|
|
||||||
number = int(number)
|
|
||||||
if number > 0:
|
|
||||||
number -= 1
|
|
||||||
elif number == 0:
|
|
||||||
irc.error('That\'s not a valid topic number.')
|
|
||||||
return
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The argument must be a valid integer.')
|
|
||||||
return
|
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if topics:
|
if topics:
|
||||||
try:
|
number = self._topicNumber(irc, number, topics=topics)
|
||||||
irc.reply(topics[number])
|
irc.reply(topics[number])
|
||||||
except IndexError:
|
|
||||||
irc.error('That\'s not a valid topic.')
|
|
||||||
else:
|
else:
|
||||||
irc.error('There are no topics to get.')
|
irc.error('There are no topics to get.')
|
||||||
get = privmsgs.channel(get)
|
get = privmsgs.channel(get)
|
||||||
@ -239,36 +246,35 @@ class Topic(callbacks.Privmsg):
|
|||||||
"""
|
"""
|
||||||
self._canChangeTopic(irc, channel)
|
self._canChangeTopic(irc, channel)
|
||||||
(number, regexp) = privmsgs.getArgs(args, required=2)
|
(number, regexp) = privmsgs.getArgs(args, required=2)
|
||||||
try:
|
|
||||||
number = int(number)
|
|
||||||
if number > 0:
|
|
||||||
number -= 1
|
|
||||||
elif number == 0:
|
|
||||||
irc.error('That\'s not a valid topic number.')
|
|
||||||
return
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The <number> argument must be a number.')
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
replacer = utils.perlReToReplacer(regexp)
|
|
||||||
except ValueError, e:
|
|
||||||
irc.error('The regexp wasn\'t valid: %s' % e.args[0])
|
|
||||||
return
|
|
||||||
except re.error, e:
|
|
||||||
irc.error(utils.exnToString(e))
|
|
||||||
return
|
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if not topics:
|
if not topics:
|
||||||
irc.error('There are no topics to change.')
|
irc.error('There are no topics to change.')
|
||||||
return
|
return
|
||||||
topic = topics.pop(number)
|
number = self._topicNumber(irc, number, topics=topics)
|
||||||
newTopic = replacer(topic)
|
try:
|
||||||
if number < 0:
|
replacer = utils.perlReToReplacer(regexp)
|
||||||
number = len(topics)+1+number
|
except ValueError, e:
|
||||||
topics.insert(number, newTopic)
|
irc.error('The regexp wasn\'t valid: %s' % e)
|
||||||
self._sendTopic(irc, channel, topics)
|
return
|
||||||
|
topics[number] = replacer(topics[number])
|
||||||
|
self._sendTopics(irc, channel, topics)
|
||||||
change = privmsgs.channel(change)
|
change = privmsgs.channel(change)
|
||||||
|
|
||||||
|
def set(self, irc, msg, args, channel):
|
||||||
|
"""[<channel>] <number> <topic>
|
||||||
|
|
||||||
|
Sets the topic <number> to be <text>. <channel> is only necessary if
|
||||||
|
the message isn't sent in the channel itself.
|
||||||
|
"""
|
||||||
|
self._canChangeTopic(irc, channel)
|
||||||
|
(i, topic) = privmsgs.getArgs(args, required=2)
|
||||||
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
|
i = self._topicNumber(irc, i, topics=topics)
|
||||||
|
topic = self._formatTopic(irc, msg, channel, topic)
|
||||||
|
topics[i] = topic
|
||||||
|
self._sendTopics(irc, channel, topics)
|
||||||
|
set = privmsgs.channel(set)
|
||||||
|
|
||||||
def remove(self, irc, msg, args, channel):
|
def remove(self, irc, msg, args, channel):
|
||||||
"""[<channel>] <number>
|
"""[<channel>] <number>
|
||||||
|
|
||||||
@ -278,23 +284,11 @@ class Topic(callbacks.Privmsg):
|
|||||||
necessary if the message isn't sent in the channel itself.
|
necessary if the message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
self._canChangeTopic(irc, channel)
|
self._canChangeTopic(irc, channel)
|
||||||
try:
|
i = privmsgs.getArgs(args)
|
||||||
number = int(privmsgs.getArgs(args))
|
|
||||||
if number > 0:
|
|
||||||
number -= 1
|
|
||||||
elif number == 0:
|
|
||||||
irc.error('That\'s not a valid topic number.')
|
|
||||||
return
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The argument must be a number.')
|
|
||||||
return
|
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
try:
|
i = self._topicNumber(irc, i, topics=topics)
|
||||||
topic = topics.pop(number)
|
topic = topics.pop(i)
|
||||||
except IndexError:
|
self._sendTopics(irc, channel, topics)
|
||||||
irc.error('That\'s not a valid topic number.')
|
|
||||||
return
|
|
||||||
self._sendTopic(irc, channel, topics)
|
|
||||||
remove = privmsgs.channel(remove)
|
remove = privmsgs.channel(remove)
|
||||||
|
|
||||||
def lock(self, irc, msg, args, channel):
|
def lock(self, irc, msg, args, channel):
|
||||||
@ -333,7 +327,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error('I haven\'t yet set the topic in %s.' % channel)
|
irc.error('I haven\'t yet set the topic in %s.' % channel)
|
||||||
return
|
return
|
||||||
self._sendTopic(irc, channel, topics)
|
self._sendTopics(irc, channel, topics)
|
||||||
restore = privmsgs.channel(restore)
|
restore = privmsgs.channel(restore)
|
||||||
|
|
||||||
Class = Topic
|
Class = Topic
|
||||||
|
@ -111,6 +111,11 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
_ = self.getMsg('topic add baz')
|
_ = self.getMsg('topic add baz')
|
||||||
self.assertRegexp('topic list', '1: foo .* 2: bar .* and 3: baz')
|
self.assertRegexp('topic list', '1: foo .* 2: bar .* and 3: baz')
|
||||||
|
|
||||||
|
def testSet(self):
|
||||||
|
_ = self.getMsg('topic add foo')
|
||||||
|
self.assertRegexp('topic set -1 bar', 'bar')
|
||||||
|
self.assertNotRegexp('topic set -1 baz', 'bar')
|
||||||
|
|
||||||
|
|
||||||
# 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