From 088047aea8b5155c68cfbb49fcb625a1a14fa884 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 21 Aug 2003 12:25:35 +0000 Subject: [PATCH] Stylistic updates. --- others/babelfish.py | 12 +++-- plugins/Http.py | 111 ++++++++++++++++++++++---------------------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/others/babelfish.py b/others/babelfish.py index 0fbb319e2..5efc0b62a 100644 --- a/others/babelfish.py +++ b/others/babelfish.py @@ -124,8 +124,10 @@ def translate(phrase, from_lang, to_lang): html = response.read() for regex in __where: match = regex.search(html) - if match: break - if not match: raise BabelfishChangedError("Can't recognize translated string.") + if match: + break + if not match: + raise BabelfishChangedError("Can't recognize translated string.") return clean(match.group(1)) def babelize(phrase, from_language, through_language, limit = 12, callback = None): @@ -139,7 +141,8 @@ def babelize(phrase, from_language, through_language, limit = 12, callback = Non next = from_language for i in range(limit): phrase = translate(phrase, next, flip[next]) - if seen.has_key(phrase): break + if seen.has_key(phrase): + break seen[phrase] = 1 if callback: callback(phrase) @@ -149,7 +152,8 @@ def babelize(phrase, from_language, through_language, limit = 12, callback = Non if next != from_language: phrase = translate(phrase, next, flip[next]) results.append(phrase) - if not callback: return results + if not callback: + return results if __name__ == '__main__': import sys diff --git a/plugins/Http.py b/plugins/Http.py index 633639693..c9578c27d 100644 --- a/plugins/Http.py +++ b/plugins/Http.py @@ -42,10 +42,11 @@ import random import urllib import urllib2 +import babelfish + import utils import debug import privmsgs -import babelfish import callbacks import structures @@ -194,24 +195,6 @@ class Http(callbacks.Privmsg): search = urllib.unquote(search) s = 'There appears to be no definition for %s.' % search irc.reply(msg, s) - ''' - _zipcode = re.compile(r'Local Forecast for (.*), (.*?) ') - def zipcode(self, irc, msg, args): - """ - - Returns the city and state of a given US Zip code. - """ - zip = privmsgs.getArgs(args) - url = "http://www.weather.com/weather/local/%s?lswe=%s" % (zip, zip) - try: - html = urllib2.urlopen(url).read() - (city, state) = self._zipcode.search(html).groups() - irc.reply(msg, '%s, %s' % (city, state)) - except AttributeError: - irc.error(msg, 'the format of the page was odd.') - except urllib2.URLError: - irc.error(msg, 'Couldn\'t open search page.') - ''' _tempregex = re.compile('CLASS=obsTempTextA>(\d+)°F',\ @@ -269,7 +252,7 @@ class Http(callbacks.Privmsg): def acronym(self, irc, msg, args): """ - Displays the first acronym matches from acronymfinder.com + Displays acronym matches from acronymfinder.com """ acronym = privmsgs.getArgs(args) try: @@ -365,55 +348,71 @@ class Http(callbacks.Privmsg): irc.reply(msg, s) _abbrevs = utils.abbrev(map(str.lower, babelfish.available_languages)) + _abbrevs['de'] = 'german' + _abbrevs['fr'] = 'french' + _abbrevs['jp'] = 'japanese' + _abbrevs['ko'] = 'korean' + _abbrevs['es'] = 'spanish' + _abbrevs['pt'] = 'portuguese' + _abbrevs['it'] = 'italian' + _abbrevs['zh'] = 'chinese' + for language in babelfish.available_languages: + _abbrevs[language] = language def translate(self, irc, msg, args): - """ + """ - Returns the phrase translated to the new language. One of the - languages must be English. + Returns the text translated to the new language. """ - (flang, tlang, phrase) = privmsgs.getArgs(args, 3) - flang = str.lower(flang) - tlang = str.lower(tlang) - if self._abbrevs.has_key(flang): - flang = self._abbrevs[flang] - if self._abbrevs.has_key(tlang): - tlang = self._abbrevs[tlang] + (fromLang, toLang, text) = privmsgs.getArgs(args, needed=3) try: - trans = babelfish.translate(phrase, flang, tlang) - irc.reply(msg, trans) - except babelfish.LanguageNotAvailableError, e: - irc.reply(msg, 'Valid languages: %s.' %\ - ', '.join(babelfish.available_languages)) + fromLang = self._abbrevs[fromLang.lower()] + toLang = self._abbrevs[toLang.lower()] + translation = babelfish.translate(text, fromLang, toLang) + irc.reply(msg, translation) + except (KeyError, babelfish.LanguageNotAvailableError), e: + irc.error(msg, '%s is not a valid language. Valid languages ' \ + 'include %s' % \ + (e, utils.commaAndify(babelfish.available_languages))) except babelfish.BabelizerIOError, e: - irc.reply(msg, e.args[0]) + irc.error(msg, e) except babelfish.BabelfishChangedError, e: - irc.reply(msg, 'Babelfish has foiled our plans by changing their\ - format') + irc.error(msg, 'Babelfish has foiled our plans by changing its ' \ + 'webpage format') def babelize(self, irc, msg, args): - """ + """ - Returns the phrase translated to the new language. This is done 12 - times, or until the output doesn't change anymore. + Translates repeatedly between and + until it doesn't change anymore or 12 times, whichever is fewer. One + of the languages must be English. """ - (flang, tlang, phrase) = privmsgs.getArgs(args, 3) - flang = str.lower(flang) - tlang = str.lower(tlang) - if self._abbrevs.has_key(flang): - flang = self._abbrevs[flang] - if self._abbrevs.has_key(tlang): - tlang = self._abbrevs[tlang] + (fromLang, toLang, text) = privmsgs.getArgs(args, needed=3) try: - trans = babelfish.babelize(phrase, flang, tlang) - irc.reply(msg, trans[-1]) - except babelfish.LanguageNotAvailableError, e: - irc.reply(msg, 'Valid languages: %s.' %\ - ', '.join(babelfish.available_languages)) + fromLang = self._abbrevs[fromLang.lower()] + toLang = self._abbrevs[toLang.lower()] + translations = babelfish.babelize(text, fromLang, toLang) + irc.reply(msg, translations[-1]) + except (KeyError, babelfish.LanguageNotAvailableError), e: + irc.reply(msg, '%s is not a valid language. Valid languages ' \ + 'include %s' % \ + (e, utils.commaAndify(babelfish.available_languages))) except babelfish.BabelizerIOError, e: - irc.reply(msg, e.args[0]) + irc.reply(msg, e) except babelfish.BabelfishChangedError, e: - irc.reply(msg, 'Babelfish has foiled our plans by changing their\ - format') + irc.reply(msg, 'Babelfish has foiled our plans by changing its ' \ + 'webpage format') + + def randomlanguage(self, irc, msg, args): + """[] + + Returns a random language supported by babelfish. If + is provided, will include English in the list of possible languages. + """ + allowEnglish = privmsgs.getArgs(args, needed=0, optional=1) + language = random.sample(babelfish.available_languages, 1)[0] + while not allowEnglish and language == 'English': + language = random.sample(babelfish.available_languages, 1)[0] + irc.reply(msg, language) Class = Http