mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 20:59:27 +01:00
This should work somewhat.
This commit is contained in:
parent
5589ffdea3
commit
95877b2d53
@ -64,37 +64,55 @@ class Probability(registry.Float):
|
|||||||
else:
|
else:
|
||||||
super(Probability, self).setValue(v)
|
super(Probability, self).setValue(v)
|
||||||
|
|
||||||
|
class Observers(registry.SpaceSeparatedListOfStrings):
|
||||||
|
List = callbacks.CanonicalNameSet
|
||||||
|
|
||||||
|
|
||||||
|
class ActiveObservers(registry.SpaceSeparatedListOfStrings):
|
||||||
|
String = callbacks.canonicalName
|
||||||
|
|
||||||
conf.registerPlugin('Observer')
|
conf.registerPlugin('Observer')
|
||||||
# XXX These both need to be CanonicalStrings.
|
# XXX These both need to be CanonicalStrings.
|
||||||
conf.registerGlobalValue(conf.supybot.plugins.Observer, 'observers',
|
conf.registerGlobalValue(conf.supybot.plugins.Observer, 'observers',
|
||||||
registry.SpaceSeparatedSetOfStrings([], """Determines what observers are
|
Observers([], """Determines what observers are available.""",
|
||||||
available.""", orderAlphabetically=True))
|
orderAlphabetically=True))
|
||||||
conf.registerChannelValue(conf.supybot.plugins.Observer.observers, 'active',
|
conf.registerChannelValue(conf.supybot.plugins.Observer.observers, 'active',
|
||||||
registry.SpaceSeparatedListOfStrings([], """Determines what observers are
|
ActiveObservers([], """Determines what observers are
|
||||||
active on a channel."""))
|
active on a channel."""))
|
||||||
|
|
||||||
|
|
||||||
def registerObserver(name, regexpString=None,
|
def registerObserver(name, regexpString='',
|
||||||
commandString=None, probability=1.0):
|
commandString='', probability=1.0):
|
||||||
g = conf.registerGlobalValue(conf.supybot.plugins.Observer.observers,
|
g = conf.registerGlobalValue(conf.supybot.plugins.Observer.observers,
|
||||||
name, registry.Regexp(regexpString, """Determines what regexp must
|
name, registry.Regexp(regexpString, """Determines what regexp must
|
||||||
match for this observer to be executed."""))
|
match for this observer to be executed."""))
|
||||||
if regexpString is not None:
|
if regexpString:
|
||||||
g.set(regexpString) # This is in case it's been registered.
|
g.set(regexpString) # This is in case it's been registered.
|
||||||
conf.registerGlobalValue(g, 'command', registry.String('', """Determines
|
conf.registerGlobalValue(g, 'command', registry.String('', """Determines
|
||||||
what command will be run when this observer is executed."""))
|
what command will be run when this observer is executed."""))
|
||||||
|
if commandString:
|
||||||
|
g.command.setValue(commandString)
|
||||||
conf.registerGlobalValue(g, 'probability', Probability(probability, """
|
conf.registerGlobalValue(g, 'probability', Probability(probability, """
|
||||||
Determines what the probability of executing this observer is if it
|
Determines what the probability of executing this observer is if it
|
||||||
matches."""))
|
matches."""))
|
||||||
|
g.probability.setValue(probability)
|
||||||
conf.supybot.plugins.Observer.observers().add(name)
|
conf.supybot.plugins.Observer.observers().add(name)
|
||||||
return g
|
return g
|
||||||
|
|
||||||
|
|
||||||
class Observer(callbacks.Privmsg):
|
class Observer(callbacks.Privmsg):
|
||||||
|
commandCalled = False
|
||||||
def _isValidObserverName(self, name):
|
def _isValidObserverName(self, name):
|
||||||
return name != 'active' and registry.isValidRegistryName(name)
|
return name != 'active' and registry.isValidRegistryName(name)
|
||||||
|
|
||||||
|
def callCommand(self, *args, **kwargs):
|
||||||
|
self.commandCalled = True
|
||||||
|
super(Observer, self).callCommand(*args, **kwargs)
|
||||||
|
|
||||||
def doPrivmsg(self, irc, msg):
|
def doPrivmsg(self, irc, msg):
|
||||||
|
if self.commandCalled:
|
||||||
|
self.commandCalled = False
|
||||||
|
return
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
Owner = irc.getCallback('Owner')
|
Owner = irc.getCallback('Owner')
|
||||||
observers = self.registryValue('observers')
|
observers = self.registryValue('observers')
|
||||||
@ -116,7 +134,7 @@ class Observer(callbacks.Privmsg):
|
|||||||
groups.insert(0, m.group(0))
|
groups.insert(0, m.group(0))
|
||||||
for (i, group) in enumerate(groups):
|
for (i, group) in enumerate(groups):
|
||||||
command = command.replace('$%s' % i, group)
|
command = command.replace('$%s' % i, group)
|
||||||
tokens = callbacks.tokenize(text, channel=channel)
|
tokens = callbacks.tokenize(command, channel=channel)
|
||||||
Owner.processTokens(irc, msg, tokens)
|
Owner.processTokens(irc, msg, tokens)
|
||||||
|
|
||||||
def list(self, irc, msg, args):
|
def list(self, irc, msg, args):
|
||||||
@ -142,7 +160,7 @@ class Observer(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
irc.reply('There were no relevant observers.')
|
irc.reply('There were no relevant observers.')
|
||||||
|
|
||||||
def enable(self, irc, msg, args):
|
def enable(self, irc, msg, args, channel):
|
||||||
"""[<channel>] <name>
|
"""[<channel>] <name>
|
||||||
|
|
||||||
Enables the observer <name> in <channel>. <channel> is only
|
Enables the observer <name> in <channel>. <channel> is only
|
||||||
@ -151,7 +169,7 @@ class Observer(callbacks.Privmsg):
|
|||||||
name = privmsgs.getArgs(args)
|
name = privmsgs.getArgs(args)
|
||||||
if name not in self.registryValue('observers'):
|
if name not in self.registryValue('observers'):
|
||||||
irc.error('There is no observer %s.' % name, Raise=True)
|
irc.error('There is no observer %s.' % name, Raise=True)
|
||||||
self.registryValue('observers.active', channel).add(name)
|
self.registryValue('observers.active', channel).append(name)
|
||||||
irc.replySuccess()
|
irc.replySuccess()
|
||||||
enable = privmsgs.checkChannelCapability(enable, 'op')
|
enable = privmsgs.checkChannelCapability(enable, 'op')
|
||||||
|
|
||||||
@ -165,7 +183,7 @@ class Observer(callbacks.Privmsg):
|
|||||||
try:
|
try:
|
||||||
self.registryValue('observers.active', channel).remove(name)
|
self.registryValue('observers.active', channel).remove(name)
|
||||||
irc.replySuccess()
|
irc.replySuccess()
|
||||||
except KeyError:
|
except (KeyError, ValueError):
|
||||||
irc.error('The observer %s was not active on %s.' % (name,channel))
|
irc.error('The observer %s was not active on %s.' % (name,channel))
|
||||||
disable = privmsgs.checkChannelCapability(disable, 'op')
|
disable = privmsgs.checkChannelCapability(disable, 'op')
|
||||||
|
|
||||||
|
58
test/test_Observer.py
Normal file
58
test/test_Observer.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
###
|
||||||
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
###
|
||||||
|
|
||||||
|
from testsupport import *
|
||||||
|
|
||||||
|
class ObserverTestCase(ChannelPluginTestCase):
|
||||||
|
plugins = ('Observer', 'Utilities')
|
||||||
|
config = {'reply.whenNotCommand': False}
|
||||||
|
def testAdd(self):
|
||||||
|
self.assertNotError('add foo m/foo/i echo I saw foo.')
|
||||||
|
self.assertNoResponse('blah blah blah', 1)
|
||||||
|
self.assertNotError('observer enable foo')
|
||||||
|
self.assertNoResponse('blah blah blah', 1)
|
||||||
|
self.assertResponse('I love to foo!', 'I saw foo.')
|
||||||
|
self.assertResponse('Foo you!', 'I saw foo.')
|
||||||
|
self.assertResponse('foobar', 'I saw foo.')
|
||||||
|
self.assertNotError('observer disable foo')
|
||||||
|
|
||||||
|
def testGroups(self):
|
||||||
|
self.assertNotError('add digits m/(\d+)/ echo $1')
|
||||||
|
self.assertNoResponse('asdfkjaf', 1)
|
||||||
|
self.assertNotError('observer enable digits')
|
||||||
|
self.assertNoResponse('asdfkjaf', 1)
|
||||||
|
self.assertResponse('abc -- easy as 123', '123')
|
||||||
|
self.assertResponse('testing, 1 2 3' , '1')
|
||||||
|
self.assertNotError('observer disable digits')
|
||||||
|
|
||||||
|
|
||||||
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user