several fixes in the new ICapTranslate capability, module and application

This commit is contained in:
Romain Bignon 2012-04-01 19:32:05 +02:00
commit a8166a4719
5 changed files with 96 additions and 78 deletions

View file

@ -18,21 +18,32 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
"backend for http://translate.google.com" "backend for http://translate.google.com"
from weboob.capabilities.translate import ICapTranslate
from weboob.tools.backend import BaseBackend, BackendConfig from weboob.capabilities.translate import ICapTranslate, Translation, TranslationFail
from weboob.tools.backend import BaseBackend
from .browser import GoogleTranslateBrowser from .browser import GoogleTranslateBrowser
__all__ = ['GoogleTranslateBackend'] __all__ = ['GoogleTranslateBackend']
class GoogleTranslateBackend(BaseBackend, ICapTranslate): class GoogleTranslateBackend(BaseBackend, ICapTranslate):
MAINTAINER = 'Lucien Loiseau' MAINTAINER = 'Lucien Loiseau'
EMAIL = 'loiseau.lucien@gmail.com' EMAIL = 'loiseau.lucien@gmail.com'
VERSION = '0.c' VERSION = '0.c'
LICENSE = 'AGPLv3+' LICENSE = 'AGPLv3+'
STORAGE = {'seen': {}}
NAME = 'googletranslate' NAME = 'googletranslate'
DESCRIPTION = u'Google translation web service' DESCRIPTION = u'Google translation web service'
BROWSER = GoogleTranslateBrowser BROWSER = GoogleTranslateBrowser
def translate(self, lan_from, lan_to, text): def translate(self, lan_from, lan_to, text):
return self.browser.translate(lan_from, lan_to, text) translation = Translation(0)
translation.lang_src = lan_from
translation.lang_dst = lan_to
translation.text = self.browser.translate(lan_from, lan_to, text)
if translation.text is None:
raise TranslationFail()
return translation

View file

@ -18,16 +18,16 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound, BrowserIncorrectPassword import urllib
from weboob.tools.browser.decorators import id2url, check_url
from weboob.capabilities.translate import TranslationFail from weboob.tools.browser import BaseBrowser
from .pages import TranslatePage from .pages import TranslatePage
import urllib
__all__ = ['GoogleTranslateBrowser'] __all__ = ['GoogleTranslateBrowser']
class GoogleTranslateBrowser(BaseBrowser): class GoogleTranslateBrowser(BaseBrowser):
DOMAIN = 'translate.google.com' DOMAIN = 'translate.google.com'
ENCODING = 'UTF-8' ENCODING = 'UTF-8'
@ -43,22 +43,17 @@ class GoogleTranslateBrowser(BaseBrowser):
""" """
translate 'text' from 'source' language to 'to' language translate 'text' from 'source' language to 'to' language
""" """
try: d = {
d = { 'sl': source.encode('utf-8'),
'sl': source, 'tl': to.encode('utf-8'),
'tl': to, 'js': 'n',
'js': 'n', 'prev': '_t',
'prev': '_t', 'hl': 'en',
'hl': 'en', 'ie': 'UTF-8',
'ie': 'UTF-8', 'layout': '2',
'layout': '2', 'eotf': '1',
'eotf': '1', 'text': text.encode('utf-8'),
'text': text, }
} self.location('http://'+self.DOMAIN, urllib.urlencode(d))
self.location('http://'+self.DOMAIN, urllib.urlencode(d)) translation = self.page.get_translation()
translation = self.page.get_translation() return translation
return translation
except TranslationFail:
return "no translation available"

View file

@ -17,15 +17,17 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from datetime import date
from weboob.tools.browser import BasePage from weboob.tools.browser import BasePage
__all__ = ['TranslatePage']
class TranslatePage(BasePage): class TranslatePage(BasePage):
def get_translation(self): def get_translation(self):
for span in self.document.getiterator('span'): boxes = self.parser.select(self.document.getroot(), 'span#result_box', 1).findall('span')
if (span.attrib.get('id', '') == 'result_box'): if len(boxes) == 0:
for children in span.getchildren(): return None
return children.text
return u'\n'.join([unicode(box.text) for box in boxes])

View file

@ -18,54 +18,61 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import sys
import codecs
import locale
from weboob.capabilities.translate import ICapTranslate from weboob.capabilities.translate import ICapTranslate
from weboob.tools.application.repl import ReplApplication from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
__all__ = ['Translaboob'] __all__ = ['Translaboob']
class TranslationFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'text')
def flush(self):
pass
def format_dict(self, item):
backend = item['id'].split('@', 1)[1]
result = u'%s* %s%s\n\t%s' % (self.BOLD, backend, self.NC, item['text'].replace('\n', '\n\t'))
return result
class XmlTranslationFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'lang_src', 'lang_dst', 'text')
def flush(self):
pass
def format_dict(self, item):
backend = item['id'].split('@', 1)[1]
return u'<translation %s>\n%s\n</translation>' % (backend, item['text'])
class Translaboob(ReplApplication): class Translaboob(ReplApplication):
APPNAME = 'translaboob' APPNAME = 'translaboob'
VERSION = '0.c' VERSION = '0.c'
COPYRIGHT = 'Copyright(C) 2012 Lucien Loiseau' COPYRIGHT = 'Copyright(C) 2012 Lucien Loiseau'
DESCRIPTION = 'Console application to translate text from one language to another' DESCRIPTION = 'Console application to translate text from one language to another'
CAPS = ICapTranslate CAPS = ICapTranslate
EXTRA_FORMATTERS = {'translation': TranslationFormatter,
def main(self, argv): 'xmltrans': XmlTranslationFormatter,
return ReplApplication.main(self, argv) }
COMMANDS_FORMATTERS = {'translate': 'translation',
}
def do_translate(self, line): def do_translate(self, line):
lan_from, lan_to, text = self.parse_command_args(line, 3, 1)
""" """
translate <FROM> <TO> <TEXT> translate FROM TO [TEXT]
translate from one language to another,
<FROM> : source language Translate from one language to another.
<TO> : destination language * FROM : source language
<TEXT> : language to translate, standart input if - is given * TO : destination language
* TEXT : language to translate, standart input if - is given
""" """
lan_from, lan_to, text = self.parse_command_args(line, 3, 2)
if not text or text == '-': if not text or text == '-':
text = self.acquire_input() text = self.acquire_input()
print "from : "+lan_from+" to : "+lan_to
print "<source>"
print text
print "</source>"
for backend, translation in self.do('translate', lan_from, lan_to, text): for backend, translation in self.do('translate', lan_from, lan_to, text):
print "<translation "+backend.name+">" self.format(translation)
print translation
print "</translation>"

View file

@ -18,10 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from datetime import date, datetime from .base import IBaseCap, CapBaseObject, StringField
from .base import CapBaseObject, Field, StringField, DateField, DecimalField, IntField
from .collection import ICapCollection
__all__ = ['TranslationFail', 'ICapTranslate'] __all__ = ['TranslationFail', 'ICapTranslate']
@ -36,21 +33,27 @@ class TranslationFail(Exception):
Exception.__init__(self, msg) Exception.__init__(self, msg)
class ICapTranslate(ICapCollection): class Translation(CapBaseObject):
"""
Translation.
"""
lang_src = StringField('Source language')
lang_dst = StringField('Destination language')
text = StringField('Translation')
class ICapTranslate(IBaseCap):
""" """
Capability of online translation website to translate word or sentence Capability of online translation website to translate word or sentence
""" """
def translate(self, source_language, destination_language, request): def translate(self, source_language, destination_language, request):
""" """
perfom a translation Perfom a translation.
:param source_language: language in which the request is written :param source_language: language in which the request is written
:param destination_language: language to translate the request into :param destination_language: language to translate the request into
:param request: the sentence to be translated :param request: the sentence to be translated
:rtype: Translation
""" """
raise TranslationFail() raise NotImplementedError()