diff --git a/modules/test/test_translate.py b/modules/test/test_translate.py deleted file mode 100644 index 0fee5e4..0000000 --- a/modules/test/test_translate.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -test_translate.py - tests for the translation module -author: mutantmonkey -""" - -import re -import unittest -from mock import MagicMock, Mock -from modules.translate import translate, tr, tr2, mangle - - -class TestTranslation(unittest.TestCase): - def setUp(self): - self.phenny = MagicMock() - - def test_translate(self): - out = translate("plomo o plata", input='es') - self.assertEqual(('lead or silver', 'es'), out) - - def test_tr(self): - input = Mock(groups=lambda: ('fr', 'en', 'mon chien')) - tr(self.phenny, input) - - self.assertIn("my dog", self.phenny.reply.call_args[0][0]) - - def test_tr2(self): - input = Mock(group=lambda x: 'Estoy bien') - tr2(self.phenny, input) - - self.assertIn("'m fine", self.phenny.reply.call_args[0][0]) - - def test_translate_bracketnull(self): - input = Mock(group=lambda x: 'Moja je lebdjelica puna jegulja') - tr2(self.phenny, input) - - self.assertIn("My hovercraft is full of eels", - self.phenny.reply.call_args[0][0]) - - def test_mangle(self): - input = Mock(group=lambda x: 'Mangle this phrase!') - mangle(self.phenny, input) - - self.phenny.reply.assert_not_called_with('ERRORS SRY') diff --git a/modules/translate.py b/modules/translate.py deleted file mode 100644 index 3dee81a..0000000 --- a/modules/translate.py +++ /dev/null @@ -1,135 +0,0 @@ -#!/usr/bin/env python -# coding=utf-8 -""" -translate.py - Phenny Translation Module -Copyright 2008, Sean B. Palmer, inamidst.com -Licensed under the Eiffel Forum License 2. - -http://inamidst.com/phenny/ -""" - -import re -import json -import web - -def translate(text, input='auto', output='en'): - raw = False - if output.endswith('-raw'): - output = output[:-4] - raw = True - - #opener = urllib.request.build_opener() - #opener.addheaders = [( - # 'User-Agent', 'Mozilla/5.0' + - # '(X11; U; Linux i686)' + - # 'Gecko/20071127 Firefox/2.0.0.11' - #)] - input = web.quote(input) - output = web.quote(output.encode('utf-8')) - text = web.quote(text.encode('utf-8')) - - result = web.get('https://translate.google.com/translate_a/t?' + - ('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) + - ('&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text=%s' % text)) - - while ',,' in result: - result = result.replace(',,', ',null,') - result = result.replace('[,', '[null,') - data = json.loads(result) - - if raw: - return str(data), 'en-raw' - - try: language = data[2] # -2][0][0] - except: language = '?' - - return ''.join(x[0] for x in data[0]), language - -def tr(phenny, context): - """Translates a phrase, with an optional language hint.""" - input, output, phrase = context.groups() - - phrase = phrase - - if (len(phrase) > 350) and (not context.admin): - return phenny.reply('Phrase must be under 350 characters.') - - input = input or 'auto' - output = (output or 'en') - - if input != output: - msg, input = translate(phrase, input, output) - if msg: - msg = web.decode(msg) # msg.replace(''', "'") - msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) - else: msg = 'The %s to %s translation failed, sorry!' % (input, output) - - phenny.reply(msg) - else: phenny.reply('Language guessing failed, so try suggesting one!') - -tr.rule = ('$nick', r'(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$') -tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?' -tr.priority = 'low' - -def tr2(phenny, input): - """Translates a phrase, with an optional language hint.""" - command = input.group(2) - if not command: - phenny.reply("Please provide a phrase to translate") - return - - def langcode(p): - return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha() - - args = ['auto', 'en'] - - for i in range(2): - if not ' ' in command: break - prefix, cmd = command.split(' ', 1) - if langcode(prefix): - args[i] = prefix[1:] - command = cmd - #phrase = command.encode('utf-8') - phrase = command - - if (len(phrase) > 350) and (not input.admin): - return phenny.reply('Phrase must be under 350 characters.') - - src, dest = args - if src != dest: - msg, src = translate(phrase, src, dest) - #if isinstance(msg, str): - # msg = msg.decode('utf-8') - if msg: - msg = web.decode(msg) # msg.replace(''', "'") - msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest) - else: msg = 'The %s to %s translation failed, sorry!' % (src, dest) - - phenny.reply(msg) - else: phenny.reply('Language guessing failed, so try suggesting one!') - -tr2.commands = ['tr'] -tr2.priority = 'low' - -def mangle(phenny, input): - phrase = input.group(2) - for lang in ['fr', 'de', 'es', 'it', 'ja']: - backup = phrase - phrase, inputlang = translate(phrase, 'en', lang) - if not phrase: - phrase = backup - break - __import__('time').sleep(0.5) - - backup = phrase - phrase, inputlang = translate(phrase, lang, 'en') - if not phrase: - phrase = backup - break - __import__('time').sleep(0.5) - - phenny.reply(phrase or 'ERRORS SRY') -mangle.commands = ['mangle'] - -if __name__ == '__main__': - print(__doc__.strip())