mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-09 02:54:13 +01:00
Conditional: Add command @iferror. Closes GH-1114.
This commit is contained in:
parent
534fb5c78f
commit
5f361ef180
@ -35,6 +35,7 @@ import supybot.ircutils as ircutils
|
|||||||
import supybot.callbacks as callbacks
|
import supybot.callbacks as callbacks
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import collections
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from supybot.i18n import PluginInternationalization
|
from supybot.i18n import PluginInternationalization
|
||||||
@ -297,6 +298,45 @@ class Conditional(callbacks.Plugin):
|
|||||||
else:
|
else:
|
||||||
irc.reply('false')
|
irc.reply('false')
|
||||||
nle = wrap(nle, ['float', 'float'])
|
nle = wrap(nle, ['float', 'float'])
|
||||||
|
|
||||||
|
def iferror(self, irc, msg, args, testcommand, ifcommand, elsecommand):
|
||||||
|
"""<testcommand> <ifcommand> <elsecommand>
|
||||||
|
|
||||||
|
Runs <ifcommand> if <testcommand> raises an error, runs <elsecommand>
|
||||||
|
otherwise.
|
||||||
|
"""
|
||||||
|
tokens = callbacks.tokenize(testcommand)
|
||||||
|
InvalidCommand = collections.namedtuple('InvalidCommand',
|
||||||
|
'command')
|
||||||
|
replies = []
|
||||||
|
errors = []
|
||||||
|
class ErrorReportingProxy(self.Proxy):
|
||||||
|
def reply(self2, s, *args, **kwargs):
|
||||||
|
replies.append(s)
|
||||||
|
def error(self2, s, Raise=False, *args, **kwargs):
|
||||||
|
errors.append(s)
|
||||||
|
if Raise:
|
||||||
|
raise ArgumentError
|
||||||
|
def _callInvalidCommands(self2):
|
||||||
|
errors.append(InvalidCommand(self2.args))
|
||||||
|
def evalArgs(self2):
|
||||||
|
# We don't want the replies in the nested command to
|
||||||
|
# be stored here.
|
||||||
|
super(ErrorReportingProxy, self2).evalArgs(withClass=self.Proxy)
|
||||||
|
|
||||||
|
try:
|
||||||
|
ErrorReportingProxy(irc.irc, msg, tokens)
|
||||||
|
except callbacks.ArgumentError as e:
|
||||||
|
pass
|
||||||
|
# TODO: do something with the results
|
||||||
|
if errors:
|
||||||
|
tokens = callbacks.tokenize(ifcommand)
|
||||||
|
self.Proxy(irc, msg, tokens)
|
||||||
|
else:
|
||||||
|
tokens = callbacks.tokenize(elsecommand)
|
||||||
|
self.Proxy(irc, msg, tokens)
|
||||||
|
iferror = wrap(iferror, ['something', 'something', 'something'])
|
||||||
|
|
||||||
Condition = internationalizeDocstring(Conditional)
|
Condition = internationalizeDocstring(Conditional)
|
||||||
|
|
||||||
Class = Conditional
|
Class = Conditional
|
||||||
|
@ -145,5 +145,28 @@ class ConditionalTestCase(PluginTestCase):
|
|||||||
self.assertRegexp('nle 4 3', 'false')
|
self.assertRegexp('nle 4 3', 'false')
|
||||||
self.assertError('nle 3 4 5')
|
self.assertError('nle 3 4 5')
|
||||||
self.assertError('nle 1 bla')
|
self.assertError('nle 1 bla')
|
||||||
|
|
||||||
|
def testIferror(self):
|
||||||
|
self.assertResponse(
|
||||||
|
'iferror "echo hi" "foo" "bar"',
|
||||||
|
'Error: "bar" is not a valid command.')
|
||||||
|
self.assertResponse(
|
||||||
|
'iferror "echo hi" "echo 1 error" "echo 0 errors"',
|
||||||
|
'0 errors')
|
||||||
|
self.assertResponse(
|
||||||
|
'iferror "foobarbaz" "echo 1 error" "echo 0 errors"',
|
||||||
|
'1 error')
|
||||||
|
self.assertResponse(
|
||||||
|
'iferror "help foobarbaz" "echo 1 error" "echo 0 errors"',
|
||||||
|
'1 error')
|
||||||
|
self.assertResponse(
|
||||||
|
r'iferror "iferror \"help foobarbaz\" \"echo hi\" \"foobarbaz\"" '
|
||||||
|
r'"foo" "bar"',
|
||||||
|
'Error: "bar" is not a valid command.')
|
||||||
|
self.assertResponse(
|
||||||
|
r'iferror "iferror \"help foobarbaz\" \"echo hi\" \"foobarbaz\"" '
|
||||||
|
r'"echo 0 errors" "echo 1 error"',
|
||||||
|
'1 error')
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
@ -659,7 +659,7 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
|
|||||||
else:
|
else:
|
||||||
self.prefixNick = conf.supybot.reply.withNickPrefix()
|
self.prefixNick = conf.supybot.reply.withNickPrefix()
|
||||||
|
|
||||||
def evalArgs(self):
|
def evalArgs(self, withClass=None):
|
||||||
while self.counter < len(self.args):
|
while self.counter < len(self.args):
|
||||||
self.repliedTo = False
|
self.repliedTo = False
|
||||||
if isinstance(self.args[self.counter], minisix.string_types):
|
if isinstance(self.args[self.counter], minisix.string_types):
|
||||||
@ -676,8 +676,9 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
|
|||||||
# evaluating its args, it will call our reply method, which
|
# evaluating its args, it will call our reply method, which
|
||||||
# will subsequently call this function again, and we'll
|
# will subsequently call this function again, and we'll
|
||||||
# pick up where we left off via self.counter.
|
# pick up where we left off via self.counter.
|
||||||
self.__class__(self, self.msg,
|
cls = withClass or self.__class__
|
||||||
self.args[self.counter], nested=self.nested+1)
|
cls(self, self.msg, self.args[self.counter],
|
||||||
|
nested=self.nested+1)
|
||||||
# We have to return here because the new NestedCommandsIrcProxy
|
# We have to return here because the new NestedCommandsIrcProxy
|
||||||
# might not have called our reply method instantly, since
|
# might not have called our reply method instantly, since
|
||||||
# its command might be threaded. So (obviously) we can't
|
# its command might be threaded. So (obviously) we can't
|
||||||
|
Loading…
Reference in New Issue
Block a user