From f26716f2b84d2d737ddfbe06b573b010bb99898d Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 29 Dec 2015 18:00:05 +0100 Subject: [PATCH] Utilities: Add @let. --- plugins/Utilities/plugin.py | 24 ++++++++++++++++++++++++ plugins/Utilities/test.py | 10 +++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/plugins/Utilities/plugin.py b/plugins/Utilities/plugin.py index e645d1bfe..6ad2d984d 100644 --- a/plugins/Utilities/plugin.py +++ b/plugins/Utilities/plugin.py @@ -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): + """ = in + + Defines to be equal to in the + and runs the . + '=' 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 diff --git a/plugins/Utilities/test.py b/plugins/Utilities/test.py index 1d1be9d3a..8a213f54c 100644 --- a/plugins/Utilities/test.py +++ b/plugins/Utilities/test.py @@ -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: