From 0bfa0d153c5dd01336efb1cd0330c4a5a3e1405b Mon Sep 17 00:00:00 2001 From: James Vega Date: Sun, 11 Jul 2010 09:48:16 -0400 Subject: [PATCH] ShrinkUrl: Add serviceRotation config. When configured, the outFilter and shrinkSnarfer use cycle through serviceRotation's list of services. Otherwise, the default service is used. Closes: deb#539858 Signed-off-by: James Vega --- plugins/ShrinkUrl/config.py | 28 ++++++++++++++++++++++++++-- plugins/ShrinkUrl/plugin.py | 16 +++++++++++++--- plugins/ShrinkUrl/test.py | 30 +++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/plugins/ShrinkUrl/config.py b/plugins/ShrinkUrl/config.py index f03f88a0f..fa5429510 100644 --- a/plugins/ShrinkUrl/config.py +++ b/plugins/ShrinkUrl/config.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2005, Jeremiah Fincher -# Copyright (c) 2009, James Vega +# Copyright (c) 2009-2010, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -40,8 +40,30 @@ def configure(advanced): conf.supybot.plugins.ShrinkUrl.shrinkSnarfer.setValue(True) class ShrinkService(registry.OnlySomeStrings): + """Valid values include 'ln', 'tiny', 'xrl', and 'x0'.""" validStrings = ('ln', 'tiny', 'xrl', 'x0') +class ShrinkCycle(registry.SpaceSeparatedListOfStrings): + """Valid values include 'ln', 'tiny', 'xrl', and 'x0'.""" + Value = ShrinkService + + def __init__(self, *args, **kwargs): + super(ShrinkCycle, self).__init__(*args, **kwargs) + self.lastIndex = -1 + + def setValue(self, v): + super(ShrinkCycle, self).setValue(v) + self.lastIndex = -1 + + def getService(self): + L = self() + if L: + self.lastIndex = (self.lastIndex + 1) % len(L) + return L[self.lastIndex] + raise ValueError, \ + 'No services have been configured for rotation. ' \ + 'See conf.supybot.plugins.ShrinkUrl.serviceRotation.' + ShrinkUrl = conf.registerPlugin('ShrinkUrl') conf.registerChannelValue(ShrinkUrl, 'shrinkSnarfer', registry.Boolean(False, """Determines whether the @@ -70,6 +92,8 @@ conf.registerChannelValue(ShrinkUrl, 'default', conf.registerGlobalValue(ShrinkUrl, 'bold', registry.Boolean(True, """Determines whether this plugin will bold certain portions of its replies.""")) - +conf.registerChannelValue(ShrinkUrl, 'serviceRotation', + ShrinkCycle([], """If set to a non-empty value, specifies the list of + services to rotate through for the shrinkSnarfer and outFilter.""")) # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/ShrinkUrl/plugin.py b/plugins/ShrinkUrl/plugin.py index e47ae88cd..d162c27dc 100644 --- a/plugins/ShrinkUrl/plugin.py +++ b/plugins/ShrinkUrl/plugin.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2002-2004, Jeremiah Fincher -# Copyright (c) 2009, James Vega +# Copyright (c) 2009-2010, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -86,7 +86,12 @@ class ShrinkUrl(callbacks.PluginRegexp): for m in utils.web.httpUrlRe.finditer(text): url = m.group(1) if len(url) > self.registryValue('minimumLength', channel): - cmd = self.registryValue('default', channel).capitalize() + try: + cmd = self.registryValue('serviceRotation', + channel, value=False) + cmd = cmd.getService().capitalize() + except ValueError: + cmd = self.registryValue('default', channel).capitalize() try: shortUrl = getattr(self, '_get%sUrl' % cmd)(url) text = text.replace(url, shortUrl) @@ -117,7 +122,12 @@ class ShrinkUrl(callbacks.PluginRegexp): self.log.debug('Matched nonSnarfingRegexp: %u', url) return minlen = self.registryValue('minimumLength', channel) - cmd = self.registryValue('default', channel).capitalize() + try: + cmd = self.registryValue('serviceRotation', + channel, value=False) + cmd = cmd.getService().capitalize() + except ValueError: + cmd = self.registryValue('default', channel).capitalize() if len(url) >= minlen: try: shorturl = getattr(self, '_get%sUrl' % cmd)(url) diff --git a/plugins/ShrinkUrl/test.py b/plugins/ShrinkUrl/test.py index bae344175..caf606668 100644 --- a/plugins/ShrinkUrl/test.py +++ b/plugins/ShrinkUrl/test.py @@ -1,6 +1,6 @@ ### # Copyright (c) 2002-2004, Jeremiah Fincher -# Copyright (c) 2009, James Vega +# Copyright (c) 2009-2010, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -48,15 +48,31 @@ class ShrinkUrlTestCase(ChannelPluginTestCase): } if network: def testShrink(self): + for (service, testdata) in self.tests.iteritems(): + for (url, shrunkurl) in testdata: + self.assertRegexp('shrinkurl %s %s' % (service, url), + shrunkurl) + + def testShrinkCycle(self): + cycle = conf.supybot.plugins.ShrinkUrl.serviceRotation snarfer = conf.supybot.plugins.ShrinkUrl.shrinkSnarfer - orig = snarfer() + origcycle = cycle() + origsnarfer = snarfer() try: - for (service, testdata) in self.tests.iteritems(): - for (url, shrunkurl) in testdata: - self.assertRegexp('shrinkurl %s %s' % (service, url), - shrunkurl) + self.assertNotError( + 'config plugins.ShrinkUrl.serviceRotation ln x0') + self.assertError( + 'config plugins.ShrinkUrl.serviceRotation ln x1') + snarfer.setValue(True) + self.assertSnarfRegexp(self.udUrl, r'%s.* \(at' % + self.tests['ln'][1][1]) + self.assertSnarfRegexp(self.udUrl, r'%s.* \(at' % + self.tests['x0'][1][1]) + self.assertSnarfRegexp(self.udUrl, r'%s.* \(at' % + self.tests['ln'][1][1]) finally: - snarfer.setValue(orig) + cycle.setValue(origcycle) + snarfer.setValue(origsnarfer) def _snarf(self, service): shrink = conf.supybot.plugins.ShrinkUrl