mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-30 06:49:24 +01:00
Convert Google to using the registry which required adding PositiveInteger
Class to the registry.
This commit is contained in:
parent
bb97cb3884
commit
a8c3d67cfc
@ -48,6 +48,8 @@ import xml.sax
|
|||||||
import SOAP
|
import SOAP
|
||||||
import google
|
import google
|
||||||
|
|
||||||
|
import registry
|
||||||
|
|
||||||
import conf
|
import conf
|
||||||
import utils
|
import utils
|
||||||
import ircmsgs
|
import ircmsgs
|
||||||
@ -56,7 +58,6 @@ import ircutils
|
|||||||
import privmsgs
|
import privmsgs
|
||||||
import callbacks
|
import callbacks
|
||||||
import structures
|
import structures
|
||||||
import configurable
|
|
||||||
|
|
||||||
def configure(onStart, afterConnect, advanced):
|
def configure(onStart, afterConnect, advanced):
|
||||||
from questions import expect, anything, something, yn
|
from questions import expect, anything, something, yn
|
||||||
@ -72,7 +73,7 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
break
|
break
|
||||||
if key:
|
if key:
|
||||||
onStart.append('load Google')
|
onStart.append('load Google')
|
||||||
onStart.append('google licensekey %s' % key)
|
conf.supybot.plugins.Google.licenseKey.set(key)
|
||||||
print 'The Google plugin has the functionality to watch for URLs'
|
print 'The Google plugin has the functionality to watch for URLs'
|
||||||
print 'that match a specific pattern (we call this a snarfer).'
|
print 'that match a specific pattern (we call this a snarfer).'
|
||||||
print 'When supybot sees such a URL, he will parse the web page'
|
print 'When supybot sees such a URL, he will parse the web page'
|
||||||
@ -83,10 +84,10 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
print
|
print
|
||||||
if yn('Do you want the Google Groups link snarfer enabled by '
|
if yn('Do you want the Google Groups link snarfer enabled by '
|
||||||
'default?') == 'y':
|
'default?') == 'y':
|
||||||
onStart.append('Google config groups-snarfer on')
|
conf.supybot.plugins.Google.groupsSnarfer.set(True)
|
||||||
if yn('Do you want the Google search snarfer enabled by default?') \
|
if yn('Do you want the Google search snarfer enabled by default?') \
|
||||||
== 'y':
|
== 'y':
|
||||||
onStart.append('Google config search-snarfer on')
|
conf.supybot.plugins.Google.searchSnarfer.set(True)
|
||||||
if 'load Alias' not in onStart:
|
if 'load Alias' not in onStart:
|
||||||
print 'Google depends on the Alias module for some extra commands.'
|
print 'Google depends on the Alias module for some extra commands.'
|
||||||
if yn('Would you like to load the Alias module now?') == 'y':
|
if yn('Would you like to load the Alias module now?') == 'y':
|
||||||
@ -131,33 +132,35 @@ def search(log, *args, **kwargs):
|
|||||||
raise callbacks.Error, 'Google returned an unparseable response. ' \
|
raise callbacks.Error, 'Google returned an unparseable response. ' \
|
||||||
'The full traceback has been logged.'
|
'The full traceback has been logged.'
|
||||||
|
|
||||||
class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
|
conf.registerPlugin('Google')
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'groupsSnarfer',
|
||||||
|
registry.Boolean(False, """Determines whether the groups snarfer is
|
||||||
|
enabled. If so, URLs at groups.google.com will be snarfed and their
|
||||||
|
group/title messaged to the channel."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'searchSnarfer',
|
||||||
|
registry.Boolean(False, """Determines whether the search snarfer is
|
||||||
|
enabled. If so, messages (even unaddressed ones) beginning with the word
|
||||||
|
'google' will result in the first URL Google returns being sent to the
|
||||||
|
channel."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'bold',
|
||||||
|
registry.Boolean(True, """Determines whether results are bolded."""))
|
||||||
|
conf.registerChannelValue(conf.supybot.plugins.Google, 'maximum-results',
|
||||||
|
registry.PositiveInteger(10, """Determines the maximum number of results
|
||||||
|
returned from the google command."""))
|
||||||
|
conf.registerGlobalValue(conf.supybot.plugins.Google, 'licenseKey',
|
||||||
|
registry.String('', """Sets the Google license key for using Google's Web
|
||||||
|
Services API. This is necessary before you can do any searching with this
|
||||||
|
module."""))
|
||||||
|
class Google(callbacks.PrivmsgCommandAndRegexp):
|
||||||
threaded = True
|
threaded = True
|
||||||
regexps = sets.Set(['googleSnarfer', 'googleGroups'])
|
regexps = sets.Set(['googleSnarfer', 'googleGroups'])
|
||||||
configurables = configurable.Dictionary(
|
|
||||||
[('groups-snarfer', configurable.BoolType, False,
|
|
||||||
"""Determines whether the groups snarfer is enabled. If so, URLs at
|
|
||||||
groups.google.com will be snarfed and their group/title messaged to
|
|
||||||
the channel."""),
|
|
||||||
('search-snarfer', configurable.BoolType, False,
|
|
||||||
"""Determines whether the search snarfer is enabled. If so, messages
|
|
||||||
(even unaddressed ones) beginning with the word 'google' will result
|
|
||||||
in the first URL Google returns being sent to the channel."""),
|
|
||||||
('bold', configurable.BoolType, True,
|
|
||||||
"""Determines whether results are bolded."""),
|
|
||||||
('maximum-results', configurable.PositiveIntType, 10,
|
|
||||||
"""Determines the maximum number of results returned from the
|
|
||||||
google command."""),]
|
|
||||||
)
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
configurable.Mixin.__init__(self)
|
|
||||||
callbacks.PrivmsgCommandAndRegexp.__init__(self)
|
callbacks.PrivmsgCommandAndRegexp.__init__(self)
|
||||||
self.total = 0
|
self.total = 0
|
||||||
self.totalTime = 0
|
self.totalTime = 0
|
||||||
self.last24hours = structures.queue()
|
self.last24hours = structures.queue()
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
configurable.Mixin.die(self)
|
|
||||||
callbacks.PrivmsgCommandAndRegexp.die(self)
|
callbacks.PrivmsgCommandAndRegexp.die(self)
|
||||||
|
|
||||||
def formatData(self, data, bold=True, max=0):
|
def formatData(self, data, bold=True, max=0):
|
||||||
@ -181,20 +184,6 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
|
|||||||
else:
|
else:
|
||||||
return '%s: %s' % (time, '; '.join(results))
|
return '%s: %s' % (time, '; '.join(results))
|
||||||
|
|
||||||
def licensekey(self, irc, msg, args):
|
|
||||||
"""<key>
|
|
||||||
|
|
||||||
Sets the Google license key for using Google's Web Services API. This
|
|
||||||
is necessary before you can do any searching with this module.
|
|
||||||
"""
|
|
||||||
key = privmsgs.getArgs(args)
|
|
||||||
if len(key) != 32:
|
|
||||||
irc.error('That doesn\'t seem to be a valid license key.')
|
|
||||||
return
|
|
||||||
google.setLicense(key)
|
|
||||||
irc.replySuccess()
|
|
||||||
licensekey = privmsgs.checkCapability(licensekey, 'admin')
|
|
||||||
|
|
||||||
def google(self, irc, msg, args):
|
def google(self, irc, msg, args):
|
||||||
"""<search> [--{language,restrict}=<value>] [--{notsafe,similar}]
|
"""<search> [--{language,restrict}=<value>] [--{notsafe,similar}]
|
||||||
|
|
||||||
@ -215,9 +204,13 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
|
|||||||
else:
|
else:
|
||||||
kwargs[option[2:]] = argument
|
kwargs[option[2:]] = argument
|
||||||
searchString = privmsgs.getArgs(rest)
|
searchString = privmsgs.getArgs(rest)
|
||||||
|
try:
|
||||||
data = search(self.log, searchString, **kwargs)
|
data = search(self.log, searchString, **kwargs)
|
||||||
bold = self.configurables.get('bold', msg.args[0])
|
except google.NoLicenseKey, e:
|
||||||
max = self.configurables.get('maximum-results', msg.args[0])
|
irc.error(str(e))
|
||||||
|
return
|
||||||
|
bold = conf.supybot.plugins.Google.bold()
|
||||||
|
max = conf.supybot.plugins.Google.get('maximum-results')
|
||||||
irc.reply(self.formatData(data, bold=bold, max=max))
|
irc.reply(self.formatData(data, bold=bold, max=max))
|
||||||
|
|
||||||
def metagoogle(self, irc, msg, args):
|
def metagoogle(self, irc, msg, args):
|
||||||
@ -297,7 +290,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
|
|||||||
|
|
||||||
def googleSnarfer(self, irc, msg, match):
|
def googleSnarfer(self, irc, msg, match):
|
||||||
r"^google\s+(.*)$"
|
r"^google\s+(.*)$"
|
||||||
if not self.configurables.get('search-snarfer', channel=msg.args[0]):
|
if not conf.supybot.plugins.Google.searchSnarfer():
|
||||||
return
|
return
|
||||||
searchString = match.group(1)
|
searchString = match.group(1)
|
||||||
try:
|
try:
|
||||||
@ -317,7 +310,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp, configurable.Mixin):
|
|||||||
_ggPlainGroup = re.compile(r'Newsgroups: (.*)')
|
_ggPlainGroup = re.compile(r'Newsgroups: (.*)')
|
||||||
def googleGroups(self, irc, msg, match):
|
def googleGroups(self, irc, msg, match):
|
||||||
r"http://groups.google.com/[^\s]+"
|
r"http://groups.google.com/[^\s]+"
|
||||||
if not self.configurables.get('groups-snarfer', channel=msg.args[0]):
|
if not conf.supybot.plugins.Google.groupsSnarfer():
|
||||||
return
|
return
|
||||||
request = urllib2.Request(match.group(0), headers= \
|
request = urllib2.Request(match.group(0), headers= \
|
||||||
{'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'})
|
{'User-agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'})
|
||||||
|
@ -114,6 +114,15 @@ class Integer(Value):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, 'Value must be an integer.'
|
raise InvalidRegistryValue, 'Value must be an integer.'
|
||||||
|
|
||||||
|
class PositiveInteger(Value):
|
||||||
|
def set(self, s):
|
||||||
|
try:
|
||||||
|
self.value = int(s)
|
||||||
|
if self.value < 0:
|
||||||
|
raise InvalidRegistryValue, 'Value must be a positive integer.'
|
||||||
|
except ValueError:
|
||||||
|
raise InvalidRegistryValue, 'Value must be a positive integer.'
|
||||||
|
|
||||||
class Float(Value):
|
class Float(Value):
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
try:
|
try:
|
||||||
|
@ -35,12 +35,12 @@ class GoogleTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
plugins = ('Google',)
|
plugins = ('Google',)
|
||||||
if network:
|
if network:
|
||||||
def testNoNoLicenseKeyError(self):
|
def testNoNoLicenseKeyError(self):
|
||||||
self.assertNotError('google config groups-snarfer on')
|
conf.supybot.plugins.Google.groupsSnarfer.set('on')
|
||||||
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'google blah'))
|
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'google blah'))
|
||||||
self.assertNoResponse(' ')
|
self.assertNoResponse(' ')
|
||||||
|
|
||||||
def testGroupsSnarfer(self):
|
def testGroupsSnarfer(self):
|
||||||
self.assertNotError('google config groups-snarfer on')
|
conf.supybot.plugins.Google.groupsSnarfer.set('on')
|
||||||
self.assertRegexp('http://groups.google.com/groups?dq=&hl=en&'
|
self.assertRegexp('http://groups.google.com/groups?dq=&hl=en&'
|
||||||
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
||||||
'0310132012.738e22fc%40posting.google.com',
|
'0310132012.738e22fc%40posting.google.com',
|
||||||
@ -65,21 +65,20 @@ class GoogleTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
r'comp\.lang\.python.*What exactly are bound')
|
r'comp\.lang\.python.*What exactly are bound')
|
||||||
|
|
||||||
def testConfig(self):
|
def testConfig(self):
|
||||||
self.assertNotError('google config groups-snarfer off')
|
conf.supybot.plugins.Google.groupsSnarfer.set('off')
|
||||||
self.assertNoResponse('http://groups.google.com/groups?dq=&hl=en&'
|
self.assertNoResponse('http://groups.google.com/groups?dq=&hl=en&'
|
||||||
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
||||||
'0310132012.738e22fc%40posting.google.com')
|
'0310132012.738e22fc%40posting.google.com')
|
||||||
self.assertNotError('google config groups-snarfer on')
|
conf.supybot.plugins.Google.groupsSnarfer.set('on')
|
||||||
self.assertNotError('http://groups.google.com/groups?dq=&hl=en&'
|
self.assertNotError('http://groups.google.com/groups?dq=&hl=en&'
|
||||||
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
'lr=lang_en&ie=UTF-8&oe=UTF-8&selm=698f09f8.'
|
||||||
'0310132012.738e22fc%40posting.google.com')
|
'0310132012.738e22fc%40posting.google.com')
|
||||||
|
|
||||||
def testInvalidKeyCaught(self):
|
def testInvalidKeyCaught(self):
|
||||||
self.assertNotError(
|
conf.supybot.plugins.Google.licenseKey.set(
|
||||||
'google licensekey abcdefghijklmnopqrstuvwxyz123456')
|
'abcdefghijklmnopqrstuvwxyz123456')
|
||||||
self.assertNotRegexp('google foobar', 'faultType')
|
self.assertNotRegexp('google foobar', 'faultType')
|
||||||
self.assertNotRegexp('google foobar', 'SOAP')
|
self.assertNotRegexp('google foobar', 'SOAP')
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user