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) 2005, Jeremiah Fincher
# Copyright (c) 2009, James Vega # Copyright (c) 2009-2010, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # 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) conf.supybot.plugins.ShrinkUrl.shrinkSnarfer.setValue(True)
class ShrinkService(registry.OnlySomeStrings): class ShrinkService(registry.OnlySomeStrings):
"""Valid values include 'ln', 'tiny', 'xrl', and 'x0'."""
validStrings = ('ln', 'tiny', 'xrl', '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') ShrinkUrl = conf.registerPlugin('ShrinkUrl')
conf.registerChannelValue(ShrinkUrl, 'shrinkSnarfer', conf.registerChannelValue(ShrinkUrl, 'shrinkSnarfer',
registry.Boolean(False, """Determines whether the registry.Boolean(False, """Determines whether the
@ -70,6 +92,8 @@ conf.registerChannelValue(ShrinkUrl, 'default',
conf.registerGlobalValue(ShrinkUrl, 'bold', conf.registerGlobalValue(ShrinkUrl, 'bold',
registry.Boolean(True, """Determines whether this plugin will bold certain registry.Boolean(True, """Determines whether this plugin will bold certain
portions of its replies.""")) 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: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -1,6 +1,6 @@
### ###
# Copyright (c) 2002-2004, Jeremiah Fincher # Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2009, James Vega # Copyright (c) 2009-2010, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -86,6 +86,11 @@ class ShrinkUrl(callbacks.PluginRegexp):
for m in utils.web.httpUrlRe.finditer(text): for m in utils.web.httpUrlRe.finditer(text):
url = m.group(1) url = m.group(1)
if len(url) > self.registryValue('minimumLength', channel): if len(url) > self.registryValue('minimumLength', channel):
try:
cmd = self.registryValue('serviceRotation',
channel, value=False)
cmd = cmd.getService().capitalize()
except ValueError:
cmd = self.registryValue('default', channel).capitalize() cmd = self.registryValue('default', channel).capitalize()
try: try:
shortUrl = getattr(self, '_get%sUrl' % cmd)(url) shortUrl = getattr(self, '_get%sUrl' % cmd)(url)
@ -117,6 +122,11 @@ class ShrinkUrl(callbacks.PluginRegexp):
self.log.debug('Matched nonSnarfingRegexp: %u', url) self.log.debug('Matched nonSnarfingRegexp: %u', url)
return return
minlen = self.registryValue('minimumLength', channel) minlen = self.registryValue('minimumLength', channel)
try:
cmd = self.registryValue('serviceRotation',
channel, value=False)
cmd = cmd.getService().capitalize()
except ValueError:
cmd = self.registryValue('default', channel).capitalize() cmd = self.registryValue('default', channel).capitalize()
if len(url) >= minlen: if len(url) >= minlen:
try: try:

View File

@ -1,6 +1,6 @@
### ###
# Copyright (c) 2002-2004, Jeremiah Fincher # Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2009, James Vega # Copyright (c) 2009-2010, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -48,15 +48,31 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
} }
if network: if network:
def testShrink(self): def testShrink(self):
snarfer = conf.supybot.plugins.ShrinkUrl.shrinkSnarfer
orig = snarfer()
try:
for (service, testdata) in self.tests.iteritems(): for (service, testdata) in self.tests.iteritems():
for (url, shrunkurl) in testdata: for (url, shrunkurl) in testdata:
self.assertRegexp('shrinkurl %s %s' % (service, url), self.assertRegexp('shrinkurl %s %s' % (service, url),
shrunkurl) shrunkurl)
def testShrinkCycle(self):
cycle = conf.supybot.plugins.ShrinkUrl.serviceRotation
snarfer = conf.supybot.plugins.ShrinkUrl.shrinkSnarfer
origcycle = cycle()
origsnarfer = snarfer()
try:
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: finally:
snarfer.setValue(orig) cycle.setValue(origcycle)
snarfer.setValue(origsnarfer)
def _snarf(self, service): def _snarf(self, service):
shrink = conf.supybot.plugins.ShrinkUrl shrink = conf.supybot.plugins.ShrinkUrl