From 3fa57731831e7494aef65c529507852cea4ebf7c Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 16 Nov 2014 12:32:43 -0800 Subject: [PATCH] Updates to SupyMisc - SupyMisc: rename config variable repeat.max -> maxLen - Add more tests for SupyMisc - Move 'colors' command from Randomness to SupyMisc --- Randomness/plugin.py | 12 ------------ SupyMisc/config.py | 9 +++------ SupyMisc/plugin.py | 27 ++++++++++++++++++++++++--- SupyMisc/test.py | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/Randomness/plugin.py b/Randomness/plugin.py index 88cfe36..9fa20eb 100644 --- a/Randomness/plugin.py +++ b/Randomness/plugin.py @@ -205,18 +205,6 @@ class Randomness(callbacks.Plugin): irc.reply(self._attack(user), action=True) attack = wrap(attack, ['text']) - def colors(self, irc, msg, args): - """takes no arguments. - - Replies with a display of IRC colour codes.""" - s = ("\x03,00 \x0F\x0300 00\x0F \x03,01 \x0F\x0301 01\x0F \x03,02 \x0F\x0302 02\x0F \x03,03 " - "\x0F\x0303 03\x0F \x03,04 \x0F\x0304 04\x0F \x03,05 \x0F\x0305 05\x0F \x03,06 \x0F\x0306" - " 06\x0F \x03,07 \x0F\x0307 07\x0F \x03,08 \x0F\x0308 08\x0F \x03,09 \x0F\x0309 09\x0F " - "\x03,10 \x0F\x0310 10\x0F \x03,11 \x0F\x0311 11\x0F \x03,12 \x0F\x0312 12\x0F \x03,13 " - "\x0F\x0313 13\x0F \x03,14 \x0F\x0314 14\x0F \x03,15 \x0F\x0315 15\x0F") - irc.reply(s) - colors = wrap(colors) - Class = Randomness diff --git a/SupyMisc/config.py b/SupyMisc/config.py index 5c1d89e..eb9d579 100644 --- a/SupyMisc/config.py +++ b/SupyMisc/config.py @@ -48,13 +48,10 @@ def configure(advanced): SupyMisc = conf.registerPlugin('SupyMisc') -# This is where your configuration variables (if any) should go. For example: -# conf.registerGlobalValue(SupyMisc, 'someConfigVariableName', -# registry.Boolean(False, _("""Help for someConfigVariableName."""))) -conf.registerGroup(SupyMisc, 'repeat') -conf.registerGlobalValue(SupyMisc.repeat, 'max', - registry.PositiveInteger(100, _("""The maximum value someone can use with the repeat command."""))) +conf.registerGlobalValue(SupyMisc, 'maxLen', + registry.PositiveInteger(100, _("""The maximum value/lengths someone + can use with the repeat and mreplace commands."""))) # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/SupyMisc/plugin.py b/SupyMisc/plugin.py index 47b0247..d3bab3f 100644 --- a/SupyMisc/plugin.py +++ b/SupyMisc/plugin.py @@ -28,6 +28,10 @@ ### import random +try: + from itertools import izip +except ImportError: + izip = zip import supybot.conf as conf import supybot.utils as utils @@ -68,7 +72,7 @@ class SupyMisc(callbacks.Plugin): Returns repeated times. must be a positive integer. To keep leading and trailing spaces, it is recommended to quote the argument " like this ". """ - maxN = self.registryValue("repeat.max") + maxN = self.registryValue("maxLen") if num <= maxN: irc.reply(text * num) else: @@ -97,13 +101,30 @@ class SupyMisc(callbacks.Plugin): Replaces all instances of with in (from left to right). Essentially an alternative for Supybot's format.translate, but with support for substrings of different lengths.""" - if len(good) != len(bad): + maxLen = self.registryValue("maxLen") + lbad, lgood = len(good), len(bad) + if lbad > maxLen or lgood > maxLen: + irc.error("Too many substrings given. Current maximum: {}" \ + .format(maxN), Raise=True) + if lbad != lgood: irc.error(" must be the same length as ", Raise=True) - for pair in itertools.izip(bad, good): + for pair in izip(bad, good): text = text.replace(pair[0], pair[1]) irc.reply(text) mreplace = wrap(mreplace, [commalist('something'), commalist('something'), 'text']) + def colors(self, irc, msg, args): + """takes no arguments. + + Replies with a display of IRC colour codes.""" + s = ("\x03,00 \x0F\x0300 00\x0F \x03,01 \x0F\x0301 01\x0F \x03,02 \x0F\x0302 02\x0F \x03,03 " + "\x0F\x0303 03\x0F \x03,04 \x0F\x0304 04\x0F \x03,05 \x0F\x0305 05\x0F \x03,06 \x0F\x0306" + " 06\x0F \x03,07 \x0F\x0307 07\x0F \x03,08 \x0F\x0308 08\x0F \x03,09 \x0F\x0309 09\x0F " + "\x03,10 \x0F\x0310 10\x0F \x03,11 \x0F\x0311 11\x0F \x03,12 \x0F\x0312 12\x0F \x03,13 " + "\x0F\x0313 13\x0F \x03,14 \x0F\x0314 14\x0F \x03,15 \x0F\x0315 15\x0F") + irc.reply(s) + colors = wrap(colors) + def tld(self, irc, msg, args, text): """ diff --git a/SupyMisc/test.py b/SupyMisc/test.py index 4a7e0cd..8a2209f 100644 --- a/SupyMisc/test.py +++ b/SupyMisc/test.py @@ -34,6 +34,10 @@ from supybot.test import * class SupyMiscTestCase(PluginTestCase): plugins = ('SupyMisc',) + def setUp(self): + PluginTestCase.setUp(self) + self.prefix = 'foo!bar@baz.not' + def testTld(self): self.assertNotError('tld .com') @@ -47,4 +51,17 @@ class SupyMiscTestCase(PluginTestCase): from codecs import unicode_escape_decode as u self.assertNotError('tld '+u('\u7f51\u7edc')[0]) + def testColorwheel(self): + self.assertRegexp('colors', '.*\x03.*') + + def testHostFetchers(self): + self.assertResponse('me', 'foo') + self.assertResponse('getident', 'bar') + self.assertResponse('gethost', 'baz.not') + self.assertResponse('botnick', self.nick) + + def testmreplace(self): + self.assertResponse('mreplace hi,there hello,ok hmm, hi there everyone', + 'hmm, hello ok everyone') + # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: