From 1f35ff36d47af4b7be0b80ddaeb411880c48bfaf Mon Sep 17 00:00:00 2001 From: James Vega Date: Sat, 16 Oct 2004 03:39:42 +0000 Subject: [PATCH] commands.wrap update --- plugins/Google.py | 52 ++++++++++++++++++++++---------------------- plugins/Python.py | 7 +++--- plugins/ShrinkUrl.py | 4 +--- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/plugins/Google.py b/plugins/Google.py index e8630b0f9..d4e10fdd7 100644 --- a/plugins/Google.py +++ b/plugins/Google.py @@ -51,7 +51,7 @@ import supybot.registry as registry import supybot.conf as conf import supybot.utils as utils -from supybot.commands import wrap +from supybot.commands import * import supybot.ircmsgs as ircmsgs import supybot.plugins as plugins import supybot.ircutils as ircutils @@ -263,21 +263,20 @@ class Google(callbacks.PrivmsgCommandAndRegexp): else: return '%s: %s' % (t, '; '.join(results)) - def lucky(self, irc, msg, args): + def lucky(self, irc, msg, args, text): """ Does a google search, but only returns the first result. """ - if not args: - raise callbacks.ArgumentError - data = search(self.log, args) + data = search(self.log, text) if data.results: url = data.results[0].URL irc.reply(url) else: irc.reply('Google found nothing.') + lucky = wrap(lucky, [many('text')]) - def google(self, irc, msg, args): + def google(self, irc, msg, args, optlist, text): """ [--{language,restrict}=] [--{notsafe,similar}] 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 work-unsafe results. """ - (optlist, rest) = getopt.getopt(args, '', ['language=', 'restrict=', - 'notsafe', 'similar']) kwargs = {} if self.registryValue('safeSearch', channel=msg.args[0]): kwargs['safeSearch'] = 1 @@ -295,16 +292,14 @@ class Google(callbacks.PrivmsgCommandAndRegexp): if lang: kwargs['language'] = lang for (option, argument) in optlist: - if option == '--notsafe': + if option == 'notsafe': kwargs['safeSearch'] = False - elif option == '--similar': + elif option == 'similar': kwargs['filter'] = False else: kwargs[option[2:]] = argument - if not rest: - raise callbacks.ArgumentError try: - data = search(self.log, rest, **kwargs) + data = search(self.log, text, **kwargs) except google.NoLicenseKey, e: irc.error('You must have a free Google web services license key ' '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]) max = self.registryValue('maximumResults', msg.args[0]) 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): """ [--(language,restrict)=] [--{similar,notsafe}] Searches google and gives all the interesting meta information about the search. See the help for the google command for a detailed description of the parameters. """ - (optlist, rest) = getopt.getopt(args, '', ['language=', 'restrict=', - 'notsafe', 'similar']) kwargs = {'language': 'lang_en', 'safeSearch': 1} for option, argument in optlist: - if option == '--notsafe': + if option == 'notsafe': kwargs['safeSearch'] = False - elif option == '--similar': + elif option == 'similar': kwargs['filter'] = False else: kwargs[option[2:]] = argument - data = search(self.log, rest, **kwargs) + data = search(self.log, text, **kwargs) meta = data.meta categories = [d['fullViewableName'] for d in meta.directoryCategories] categories = [utils.dqrepr(s.replace('_', ' ')) for s in categories] @@ -348,14 +344,17 @@ class Google(callbacks.PrivmsgCommandAndRegexp): meta.searchTime, categories and ' Categories include %s.' % categories) irc.reply(s) + metagoogle = wrap(metagoogle, [getopts({'language':'text', + 'restrict':'text', + 'notsafe':'', 'similar':''}), + many('text')]) _cacheUrlRe = re.compile('([^<]+)') - def cache(self, irc, msg, args): + def cache(self, irc, msg, args, url): """ Returns a link to the cached version of if it is available. """ - url = privmsgs.getArgs(args) html = google.doGetCachedPage(url) m = self._cacheUrlRe.search(html) if m is not None: @@ -364,6 +363,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp): irc.reply(url) else: irc.error('Google seems to have no cache for that site.') + cache = wrap(cache, ['url']) def fight(self, irc, msg, args): """ [ ...] @@ -385,12 +385,11 @@ class Google(callbacks.PrivmsgCommandAndRegexp): s = ', '.join(['%s: %s' % (format(s), i) for (i, s) in results]) irc.reply(s) - def spell(self, irc, msg, args): + def spell(self, irc, msg, args, word): """ Returns Google's spelling recommendation for . """ - word = privmsgs.getArgs(args) result = google.doSpellingSuggestion(word) if result: irc.reply(result) @@ -399,6 +398,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp): 'the word you gave is spelled right; it could also ' 'mean that its spelling was too whacked out even for ' 'Google to figure out.') + spell = wrap(spell, ['text']) def stats(self, irc, msg, args): """takes no arguments @@ -413,6 +413,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp): '%s in the past 24 hours. ' 'Google has spent %s seconds searching for me.' % (utils.nItems('search', searches), recent, time)) + stats = wrap(stats) def googleSnarfer(self, irc, msg, match): r"^google\s+(.*)$" @@ -465,12 +466,11 @@ class Google(callbacks.PrivmsgCommandAndRegexp): _calcSupRe = re.compile(r'(.*?)', re.I) _calcFontRe = re.compile(r'(.*?)') _calcTimesRe = re.compile(r'×') - def calc(self, irc, msg, args): + def calc(self, irc, msg, args, expr): """ Uses Google's calculator to calculate the value of . """ - expr = privmsgs.getArgs(args) expr = expr.replace('+', '%2B') expr = expr.replace(' ', '+') url = r'http://google.com/search?q=%s' % expr @@ -484,7 +484,7 @@ class Google(callbacks.PrivmsgCommandAndRegexp): irc.reply(s) else: irc.reply('Google\'s calculator didn\'t come up with anything.') - + calc = wrap(calc, ['text']) Class = Google diff --git a/plugins/Python.py b/plugins/Python.py index 3c458f60f..a44ce8219 100644 --- a/plugins/Python.py +++ b/plugins/Python.py @@ -57,7 +57,7 @@ finally: import supybot.conf as conf import supybot.utils as utils -from supybot.commands import wrap +from supybot.commands import * import supybot.webutils as webutils import supybot.ircutils as ircutils import supybot.privmsgs as privmsgs @@ -87,7 +87,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp): callBefore = ['URL'] regexps = ['aspnRecipes'] modulechars = string.ascii_letters + string.digits + '_.' - def pydoc(self, irc, msg, args): + def pydoc(self, irc, msg, args, name): """ Returns the __doc__ string for a given Python function. @@ -112,7 +112,6 @@ class Python(callbacks.PrivmsgCommandAndRegexp): else: return None return newmodule - name = privmsgs.getArgs(args) if name.translate(string.ascii, self.modulechars) != '': irc.error('That\'s not a valid module or function name.') return @@ -159,6 +158,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp): irc.error('That function has no documentation.') else: 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.pop(0) # Initial line (The Zen of Python...) @@ -168,6 +168,7 @@ class Python(callbacks.PrivmsgCommandAndRegexp): Returns one of the zen of Python statements. """ irc.reply(random.choice(self._these)) + zen = wrap(zen) _title = re.compile(r'(Title): (.*)', re.I) _submit = re.compile(r'(Submitter): (.*)', re.I) diff --git a/plugins/ShrinkUrl.py b/plugins/ShrinkUrl.py index 16054df06..2511e71a5 100644 --- a/plugins/ShrinkUrl.py +++ b/plugins/ShrinkUrl.py @@ -47,7 +47,7 @@ import supybot.dbi as dbi import supybot.conf as conf import supybot.utils as utils import supybot.ircmsgs as ircmsgs -from supybot.commands import wrap +from supybot.commands import * import supybot.webutils as webutils import supybot.ircutils as ircutils import supybot.registry as registry @@ -223,8 +223,6 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp): _tinyRe = re.compile(r'
(http://tinyurl\.com/\w+)') def _getTinyUrl(self, url): - # XXX This should use a database, eventually, especially once we write - # the outFilter. try: return self.db.getTiny(url) except KeyError: