commands.wrap update

This commit is contained in:
James Vega 2004-10-16 03:39:42 +00:00
parent 38ee295f8e
commit 1f35ff36d4
3 changed files with 31 additions and 32 deletions

View File

@ -51,7 +51,7 @@ import supybot.registry as registry
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import wrap from supybot.commands import *
import supybot.ircmsgs as ircmsgs import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins import supybot.plugins as plugins
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
@ -263,21 +263,20 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
else: else:
return '%s: %s' % (t, '; '.join(results)) return '%s: %s' % (t, '; '.join(results))
def lucky(self, irc, msg, args): def lucky(self, irc, msg, args, text):
"""<search> """<search>
Does a google search, but only returns the first result. Does a google search, but only returns the first result.
""" """
if not args: data = search(self.log, text)
raise callbacks.ArgumentError
data = search(self.log, args)
if data.results: if data.results:
url = data.results[0].URL url = data.results[0].URL
irc.reply(url) irc.reply(url)
else: else:
irc.reply('Google found nothing.') irc.reply('Google found nothing.')
lucky = wrap(lucky, [many('text')])
def google(self, irc, msg, args): def google(self, irc, msg, args, optlist, text):
"""<search> [--{language,restrict}=<value>] [--{notsafe,similar}] """<search> [--{language,restrict}=<value>] [--{notsafe,similar}]
Searches google.com for the given string. As many results as can fit Searches google.com for the given string. As many results as can fit
@ -286,8 +285,6 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
Google not to filter similar results. --notsafe allows possibly Google not to filter similar results. --notsafe allows possibly
work-unsafe results. work-unsafe results.
""" """
(optlist, rest) = getopt.getopt(args, '', ['language=', 'restrict=',
'notsafe', 'similar'])
kwargs = {} kwargs = {}
if self.registryValue('safeSearch', channel=msg.args[0]): if self.registryValue('safeSearch', channel=msg.args[0]):
kwargs['safeSearch'] = 1 kwargs['safeSearch'] = 1
@ -295,16 +292,14 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
if lang: if lang:
kwargs['language'] = lang kwargs['language'] = lang
for (option, argument) in optlist: for (option, argument) in optlist:
if option == '--notsafe': if option == 'notsafe':
kwargs['safeSearch'] = False kwargs['safeSearch'] = False
elif option == '--similar': elif option == 'similar':
kwargs['filter'] = False kwargs['filter'] = False
else: else:
kwargs[option[2:]] = argument kwargs[option[2:]] = argument
if not rest:
raise callbacks.ArgumentError
try: try:
data = search(self.log, rest, **kwargs) data = search(self.log, text, **kwargs)
except google.NoLicenseKey, e: except google.NoLicenseKey, e:
irc.error('You must have a free Google web services license key ' irc.error('You must have a free Google web services license key '
'in order to use this command. You can get one at ' 'in order to use this command. You can get one at '
@ -315,25 +310,26 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
bold = self.registryValue('bold', msg.args[0]) bold = self.registryValue('bold', msg.args[0])
max = self.registryValue('maximumResults', msg.args[0]) max = self.registryValue('maximumResults', msg.args[0])
irc.reply(self.formatData(data, bold=bold, max=max)) irc.reply(self.formatData(data, bold=bold, max=max))
google = wrap(google, [getopts({'language':'text', 'restrict':'text',
'notsafe':'', 'similar':''}),
many('text')])
def metagoogle(self, irc, msg, args): def metagoogle(self, irc, msg, args, optlist, text):
"""<search> [--(language,restrict)=<value>] [--{similar,notsafe}] """<search> [--(language,restrict)=<value>] [--{similar,notsafe}]
Searches google and gives all the interesting meta information about Searches google and gives all the interesting meta information about
the search. See the help for the google command for a detailed the search. See the help for the google command for a detailed
description of the parameters. description of the parameters.
""" """
(optlist, rest) = getopt.getopt(args, '', ['language=', 'restrict=',
'notsafe', 'similar'])
kwargs = {'language': 'lang_en', 'safeSearch': 1} kwargs = {'language': 'lang_en', 'safeSearch': 1}
for option, argument in optlist: for option, argument in optlist:
if option == '--notsafe': if option == 'notsafe':
kwargs['safeSearch'] = False kwargs['safeSearch'] = False
elif option == '--similar': elif option == 'similar':
kwargs['filter'] = False kwargs['filter'] = False
else: else:
kwargs[option[2:]] = argument kwargs[option[2:]] = argument
data = search(self.log, rest, **kwargs) data = search(self.log, text, **kwargs)
meta = data.meta meta = data.meta
categories = [d['fullViewableName'] for d in meta.directoryCategories] categories = [d['fullViewableName'] for d in meta.directoryCategories]
categories = [utils.dqrepr(s.replace('_', ' ')) for s in categories] categories = [utils.dqrepr(s.replace('_', ' ')) for s in categories]
@ -348,14 +344,17 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
meta.searchTime, meta.searchTime,
categories and ' Categories include %s.' % categories) categories and ' Categories include %s.' % categories)
irc.reply(s) irc.reply(s)
metagoogle = wrap(metagoogle, [getopts({'language':'text',
'restrict':'text',
'notsafe':'', 'similar':''}),
many('text')])
_cacheUrlRe = re.compile('<code>([^<]+)</code>') _cacheUrlRe = re.compile('<code>([^<]+)</code>')
def cache(self, irc, msg, args): def cache(self, irc, msg, args, url):
"""<url> """<url>
Returns a link to the cached version of <url> if it is available. Returns a link to the cached version of <url> if it is available.
""" """
url = privmsgs.getArgs(args)
html = google.doGetCachedPage(url) html = google.doGetCachedPage(url)
m = self._cacheUrlRe.search(html) m = self._cacheUrlRe.search(html)
if m is not None: if m is not None:
@ -364,6 +363,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
irc.reply(url) irc.reply(url)
else: else:
irc.error('Google seems to have no cache for that site.') irc.error('Google seems to have no cache for that site.')
cache = wrap(cache, ['url'])
def fight(self, irc, msg, args): def fight(self, irc, msg, args):
"""<search string> <search string> [<search string> ...] """<search string> <search string> [<search string> ...]
@ -385,12 +385,11 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
s = ', '.join(['%s: %s' % (format(s), i) for (i, s) in results]) s = ', '.join(['%s: %s' % (format(s), i) for (i, s) in results])
irc.reply(s) irc.reply(s)
def spell(self, irc, msg, args): def spell(self, irc, msg, args, word):
"""<word> """<word>
Returns Google's spelling recommendation for <word>. Returns Google's spelling recommendation for <word>.
""" """
word = privmsgs.getArgs(args)
result = google.doSpellingSuggestion(word) result = google.doSpellingSuggestion(word)
if result: if result:
irc.reply(result) irc.reply(result)
@ -399,6 +398,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
'the word you gave is spelled right; it could also ' 'the word you gave is spelled right; it could also '
'mean that its spelling was too whacked out even for ' 'mean that its spelling was too whacked out even for '
'Google to figure out.') 'Google to figure out.')
spell = wrap(spell, ['text'])
def stats(self, irc, msg, args): def stats(self, irc, msg, args):
"""takes no arguments """takes no arguments
@ -413,6 +413,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
'%s in the past 24 hours. ' '%s in the past 24 hours. '
'Google has spent %s seconds searching for me.' % 'Google has spent %s seconds searching for me.' %
(utils.nItems('search', searches), recent, time)) (utils.nItems('search', searches), recent, time))
stats = wrap(stats)
def googleSnarfer(self, irc, msg, match): def googleSnarfer(self, irc, msg, match):
r"^google\s+(.*)$" r"^google\s+(.*)$"
@ -465,12 +466,11 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
_calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I) _calcSupRe = re.compile(r'<sup>(.*?)</sup>', re.I)
_calcFontRe = re.compile(r'<font size=-2>(.*?)</font>') _calcFontRe = re.compile(r'<font size=-2>(.*?)</font>')
_calcTimesRe = re.compile(r'&times;') _calcTimesRe = re.compile(r'&times;')
def calc(self, irc, msg, args): def calc(self, irc, msg, args, expr):
"""<expression> """<expression>
Uses Google's calculator to calculate the value of <expression>. Uses Google's calculator to calculate the value of <expression>.
""" """
expr = privmsgs.getArgs(args)
expr = expr.replace('+', '%2B') expr = expr.replace('+', '%2B')
expr = expr.replace(' ', '+') expr = expr.replace(' ', '+')
url = r'http://google.com/search?q=%s' % expr url = r'http://google.com/search?q=%s' % expr
@ -484,7 +484,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp):
irc.reply(s) irc.reply(s)
else: else:
irc.reply('Google\'s calculator didn\'t come up with anything.') irc.reply('Google\'s calculator didn\'t come up with anything.')
calc = wrap(calc, ['text'])
Class = Google Class = Google

View File

@ -57,7 +57,7 @@ finally:
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import wrap from supybot.commands import *
import supybot.webutils as webutils import supybot.webutils as webutils
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs import supybot.privmsgs as privmsgs
@ -87,7 +87,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp):
callBefore = ['URL'] callBefore = ['URL']
regexps = ['aspnRecipes'] regexps = ['aspnRecipes']
modulechars = string.ascii_letters + string.digits + '_.' modulechars = string.ascii_letters + string.digits + '_.'
def pydoc(self, irc, msg, args): def pydoc(self, irc, msg, args, name):
"""<python function> """<python function>
Returns the __doc__ string for a given Python function. Returns the __doc__ string for a given Python function.
@ -112,7 +112,6 @@ class Python(callbacks.PrivmsgCommandAndRegexp):
else: else:
return None return None
return newmodule return newmodule
name = privmsgs.getArgs(args)
if name.translate(string.ascii, self.modulechars) != '': if name.translate(string.ascii, self.modulechars) != '':
irc.error('That\'s not a valid module or function name.') irc.error('That\'s not a valid module or function name.')
return return
@ -159,6 +158,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp):
irc.error('That function has no documentation.') irc.error('That function has no documentation.')
else: else:
irc.error('No function or module %s exists.' % name) irc.error('No function or module %s exists.' % name)
pydoc = wrap(pydoc, ['somethingWithoutSpaces'])
_these = [str(s) for s in this.s.decode('rot13').splitlines() if s] _these = [str(s) for s in this.s.decode('rot13').splitlines() if s]
_these.pop(0) # Initial line (The Zen of Python...) _these.pop(0) # Initial line (The Zen of Python...)
@ -168,6 +168,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp):
Returns one of the zen of Python statements. Returns one of the zen of Python statements.
""" """
irc.reply(random.choice(self._these)) irc.reply(random.choice(self._these))
zen = wrap(zen)
_title = re.compile(r'<b>(Title):</b>&nbsp;(.*)', re.I) _title = re.compile(r'<b>(Title):</b>&nbsp;(.*)', re.I)
_submit = re.compile(r'<b>(Submitter):</b>&nbsp;(.*)', re.I) _submit = re.compile(r'<b>(Submitter):</b>&nbsp;(.*)', re.I)

View File

@ -47,7 +47,7 @@ import supybot.dbi as dbi
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
import supybot.ircmsgs as ircmsgs import supybot.ircmsgs as ircmsgs
from supybot.commands import wrap from supybot.commands import *
import supybot.webutils as webutils import supybot.webutils as webutils
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.registry as registry import supybot.registry as registry
@ -223,8 +223,6 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
_tinyRe = re.compile(r'<blockquote><b>(http://tinyurl\.com/\w+)</b>') _tinyRe = re.compile(r'<blockquote><b>(http://tinyurl\.com/\w+)</b>')
def _getTinyUrl(self, url): def _getTinyUrl(self, url):
# XXX This should use a database, eventually, especially once we write
# the outFilter.
try: try:
return self.db.getTiny(url) return self.db.getTiny(url)
except KeyError: except KeyError: