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/>.
"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
__all__ = ['GoogleTranslateBackend']
class GoogleTranslateBackend(BaseBackend, ICapTranslate):
MAINTAINER = 'Lucien Loiseau'
EMAIL = 'loiseau.lucien@gmail.com'
VERSION = '0.c'
LICENSE = 'AGPLv3+'
STORAGE = {'seen': {}}
NAME = 'googletranslate'
DESCRIPTION = u'Google translation web service'
BROWSER = GoogleTranslateBrowser
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/>.
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound, BrowserIncorrectPassword
from weboob.tools.browser.decorators import id2url, check_url
from weboob.capabilities.translate import TranslationFail
import urllib
from weboob.tools.browser import BaseBrowser
from .pages import TranslatePage
import urllib
__all__ = ['GoogleTranslateBrowser']
class GoogleTranslateBrowser(BaseBrowser):
DOMAIN = 'translate.google.com'
ENCODING = 'UTF-8'
@ -43,22 +43,17 @@ class GoogleTranslateBrowser(BaseBrowser):
"""
translate 'text' from 'source' language to 'to' language
"""
try:
d = {
'sl': source,
'tl': to,
'js': 'n',
'prev': '_t',
'hl': 'en',
'ie': 'UTF-8',
'layout': '2',
'eotf': '1',
'text': text,
}
self.location('http://'+self.DOMAIN, urllib.urlencode(d))
translation = self.page.get_translation()
return translation
except TranslationFail:
return "no translation available"
d = {
'sl': source.encode('utf-8'),
'tl': to.encode('utf-8'),
'js': 'n',
'prev': '_t',
'hl': 'en',
'ie': 'UTF-8',
'layout': '2',
'eotf': '1',
'text': text.encode('utf-8'),
}
self.location('http://'+self.DOMAIN, urllib.urlencode(d))
translation = self.page.get_translation()
return translation

View file

@ -17,15 +17,17 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from datetime import date
from weboob.tools.browser import BasePage
__all__ = ['TranslatePage']
class TranslatePage(BasePage):
def get_translation(self):
for span in self.document.getiterator('span'):
if (span.attrib.get('id', '') == 'result_box'):
for children in span.getchildren():
return children.text
boxes = self.parser.select(self.document.getroot(), 'span#result_box', 1).findall('span')
if len(boxes) == 0:
return None
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/>.
import sys
import os
import sys
import codecs
import locale
from weboob.capabilities.translate import ICapTranslate
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
__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):
APPNAME = 'translaboob'
VERSION = '0.c'
COPYRIGHT = 'Copyright(C) 2012 Lucien Loiseau'
DESCRIPTION = 'Console application to translate text from one language to another'
CAPS = ICapTranslate
def main(self, argv):
return ReplApplication.main(self, argv)
EXTRA_FORMATTERS = {'translation': TranslationFormatter,
'xmltrans': XmlTranslationFormatter,
}
COMMANDS_FORMATTERS = {'translate': 'translation',
}
def do_translate(self, line):
lan_from, lan_to, text = self.parse_command_args(line, 3, 1)
"""
translate <FROM> <TO> <TEXT>
translate from one language to another,
<FROM> : source language
<TO> : destination language
<TEXT> : language to translate, standart input if - is given
translate FROM TO [TEXT]
Translate from one language to another.
* FROM : source language
* 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 == '-':
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):
print "<translation "+backend.name+">"
print translation
print "</translation>"
self.format(translation)

View file

@ -18,10 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from datetime import date, datetime
from .base import CapBaseObject, Field, StringField, DateField, DecimalField, IntField
from .collection import ICapCollection
from .base import IBaseCap, CapBaseObject, StringField
__all__ = ['TranslationFail', 'ICapTranslate']
@ -36,21 +33,27 @@ class TranslationFail(Exception):
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
"""
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 destination_language: language to translate the request into
:param request: the sentence to be translated
:rtype: Translation
"""
raise TranslationFail()
raise NotImplementedError()