Utilities: Add @let.

This commit is contained in:
Valentin Lorentz 2015-12-29 18:00:05 +01:00
parent f7943a6697
commit f26716f2b8
2 changed files with 33 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import types
import random
from supybot.commands import *
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring
@ -153,6 +154,29 @@ class Utilities(callbacks.Plugin):
self.Proxy(irc, msg, allTokens)
apply = wrap(apply, ['something', many('something')])
def let(self, irc, msg, args, var_name, _, value, __, command):
"""<variable> = <value> in <command>
Defines <variable> to be equal to <value> in the <command>
and runs the <command>.
'=' and 'in' can be omitted."""
if msg.reply_env and var_name in msg.reply_env:
# For security reason (eg. a Sudo-like plugin), we don't want
# to make it possible to override stuff like $nick.
irc.error(_('Cannot set a variable that already exists.'),
Raise=True)
fake_msg = ircmsgs.IrcMsg(msg=msg)
if fake_msg.reply_env is None:
fake_msg.reply_env = {}
fake_msg.reply_env[var_name] = value
tokens = callbacks.tokenize(command)
self.Proxy(irc, fake_msg, tokens)
let = wrap(let, [
'something', optional(('literal', ['='])), 'something',
optional(('literal', ['in'])), 'text'])
Class = Utilities

View File

@ -33,7 +33,7 @@ from supybot.utils.minisix import u
from supybot.test import *
class UtilitiesTestCase(PluginTestCase):
plugins = ('Utilities', 'String')
plugins = ('Math', 'Utilities', 'String')
def testIgnore(self):
self.assertNoResponse('utilities ignore foo bar baz', 1)
self.assertError('utilities ignore [re m/foo bar]')
@ -80,4 +80,12 @@ class UtilitiesTestCase(PluginTestCase):
self.assertResponse('countargs a "b c"', '2')
self.assertResponse('countargs', '0')
def testLet(self):
self.assertResponse('let x = 42 in echo foo $x bar', 'foo 42 bar')
self.assertResponse(
'let y = 21 in "'
'let x = [math calc 2*[echo $y]] in '
'echo foo $x bar"',
'foo 42 bar')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: