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 <jamessan@users.sourceforge.net>
(cherry picked from commit 0bfa0d153c)

Signed-off-by: Daniel Folkinshteyn <nanotube@users.sourceforge.net>
This commit is contained in:
James Vega 2010-07-11 09:48:16 -04:00 committed by Daniel Folkinshteyn
parent a9e2fc7bed
commit 6e33df49ab
3 changed files with 62 additions and 12 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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