Esempio n. 1
0
 def logAfterTranslation(self, before, toTranslate):
     if self.scaleMtLogs:
         after = datetime.now()
         tInfo = TranslationInfo(self)
         key = getKey(tInfo.key)
         scaleMtLog(self.get_status(), after - before, tInfo, key,
                    len(toTranslate))
Esempio n. 2
0
    def logAfterTranslation(self, before, length):
        after = datetime.now()
        if self.scaleMtLogs:
            tInfo = TranslationInfo(self)
            key = getKey(tInfo.key)
            scaleMtLog(self.get_status(), after-before, tInfo, key, length)

        if self.get_status() == 200:
            oldest = self.stats['timing'][0][0] if self.stats['timing'] else datetime.now()
            if datetime.now() - oldest > self.STAT_PERIOD_MAX_AGE:
                self.stats['timing'].pop(0)
            self.stats['timing'].append(
                (before, after, length))
Esempio n. 3
0
    def logAfterTranslation(self, before, length):
        after = datetime.now()
        if self.scaleMtLogs:
            tInfo = TranslationInfo(self)
            key = getKey(tInfo.key)
            scaleMtLog(self.get_status(), after-before, tInfo, key, length)

        if self.get_status() == 200:
            oldest = self.stats['timing'][0][0] if self.stats['timing'] else datetime.now()
            if datetime.now() - oldest > self.STAT_PERIOD_MAX_AGE:
                self.stats['timing'].pop(0)
            self.stats['timing'].append(
                (before, after, length))
Esempio n. 4
0
    def get(self):
        toTranslate = self.get_argument('q')
        markUnknown = self.get_argument('markUnknown', default='yes') in ['yes', 'true', '1']

        try:
            l1, l2 = map(toAlpha3Code, self.get_argument('langpair').split('|'))
        except ValueError:
            self.send_error(400, explanation='That pair is invalid, use e.g. eng|spa')

            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after-before, tInfo, key, len(toTranslate))

            return

        def handleTranslation():
            if self.get_status() != 200:
                self.send_error(self.get_status())
                return
            if hasattr(self, 'res'):
                self.noteUnknownTokens('-'.join((l1, l2)), self.res)
                self.res = self.res if markUnknown else self.stripUnknownMarks(self.res)
                self.sendResponse({
                    'responseData': {'translatedText': self.res},
                   'responseDetails': None,
                   'responseStatus': 200
                })
                return
            if hasattr(self, 'redir'):
                self.redirect(self.redir)
                return
            logging.error('handleTranslation reached a thought-to-be-unreachable line')
            self.send_error(500)

        if '%s-%s' % (l1, l2) in self.pairs:
            self.start_worker(handleTranslation, toTranslate, l1, l2)
            self.notePairUsage((l1, l2))
            self.cleanPairs()
        else:
            self.send_error(400, explanation='That pair is not installed')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after-before, tInfo, key, len(toTranslate))
Esempio n. 5
0
    def get(self):

        try:
            l1, l2 = self.get_argument('langpair').split('|')
        except ValueError:
            self.send_error(
                400, explanation='That pair is invalid, use e.g. eng|spa')
        if '%s-%s' % (l1, l2) in self.pairs:
            #            mode = "%s-%s" % (l1, l2)

            url = self.get_argument('url')
            data = urllib.request.urlopen(url).read()
            if chardet:
                encoding = chardet.detect(data)["encoding"]
            else:
                encoding = 'utf-8'
            text = data.decode(encoding)
            text = text.replace(
                'href="/',
                'href="{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url)))
            text = re.sub(
                r'a([^>]+)href=[\'"]?([^\'" >]+)',
                'a \\1 href="#" onclick=\'window.parent.translateLink("\\2");\'',
                text)

            pipeline = self.getPipeline(l1, l2)
            translated = yield pipeline.translate(text, nosplit=True)
            self.sendResponse({
                'responseData': {
                    'translatedPage': translated
                },
                'responseDetails': None,
                'responseStatus': 200
            })
            self.cleanPairs()
        else:
            self.send_error(400, explanation='That pair is not installed')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after - before, tInfo, key, len(toTranslate))
Esempio n. 6
0
    def get(self):
        toTranslate = self.get_argument('q')
        markUnknown = self.get_argument('markUnknown',
                                        default='yes') in ['yes', 'true', '1']

        try:
            l1, l2 = map(toAlpha3Code,
                         self.get_argument('langpair').split('|'))
        except ValueError:
            self.send_error(
                400, explanation='That pair is invalid, use e.g. eng|spa')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after - before, tInfo, key, len(toTranslate))
            return

        if '%s-%s' % (l1, l2) in self.pairs:
            before = self.logBeforeTranslation()
            pipeline = self.getPipeline(l1, l2)
            self.notePairUsage((l1, l2))
            translated = yield pipeline.translate(toTranslate)
            self.logAfterTranslation(before, toTranslate)
            self.sendResponse({
                'responseData': {
                    'translatedText':
                    self.maybeStripMarks(markUnknown, l1, l2, translated)
                },
                'responseDetails': None,
                'responseStatus': 200
            })
            self.cleanPairs()
        else:
            self.send_error(400, explanation='That pair is not installed')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after - before, tInfo, key, len(toTranslate))
Esempio n. 7
0
    def get(self):
        toTranslate = self.get_argument('q')
        markUnknown = self.get_argument('markUnknown', default='yes') in ['yes', 'true', '1']

        try:
            l1, l2 = map(toAlpha3Code, self.get_argument('langpair').split('|'))
        except ValueError:
            self.send_error(400, explanation='That pair is invalid, use e.g. eng|spa')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after-before, tInfo, key, len(toTranslate))
            return

        if '%s-%s' % (l1, l2) in self.pairs:
            before = self.logBeforeTranslation()
            pipeline = self.getPipeline(l1, l2)
            self.notePairUsage((l1, l2))
            translated = yield pipeline.translate(toTranslate)
            self.logAfterTranslation(before, toTranslate)
            self.sendResponse({
                'responseData': {
                    'translatedText': self.maybeStripMarks(markUnknown, l1, l2, translated)
                },
                'responseDetails': None,
                'responseStatus': 200
            })
            self.cleanPairs()
        else:
            self.send_error(400, explanation='That pair is not installed')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after-before, tInfo, key, len(toTranslate))
Esempio n. 8
0
    def get(self):

        try:
            l1, l2 = self.get_argument('langpair').split('|')
        except ValueError:
            self.send_error(400, explanation='That pair is invalid, use e.g. eng|spa')
        if '%s-%s' % (l1, l2) in self.pairs:
#            mode = "%s-%s" % (l1, l2)

            url = self.get_argument('url')
            data = urllib.request.urlopen(url).read()
            if chardet:
                encoding = chardet.detect(data)["encoding"]
            else:
                encoding = 'utf-8'
            text = data.decode(encoding)   
            text = text.replace('href="/',  'href="{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url)))
            text = re.sub(r'a([^>]+)href=[\'"]?([^\'" >]+)', 'a \\1 href="#" onclick=\'window.parent.translateLink("\\2");\'', text)

            pipeline = self.getPipeline(l1, l2)
            translated = yield pipeline.translate(text, nosplit=True)
            self.sendResponse({
                'responseData': {
                    'translatedPage': translated
                },
                'responseDetails': None,
                'responseStatus': 200
            })
            self.cleanPairs()
        else:
            self.send_error(400, explanation='That pair is not installed')
            if self.scaleMtLogs:
                before = datetime.now()
                tInfo = TranslationInfo(self)
                key = getKey(tInfo.key)
                after = datetime.now()
                scaleMtLog(400, after-before, tInfo, key, len(toTranslate))
Esempio n. 9
0
 def logAfterTranslation(self, before, toTranslate):
     if self.scaleMtLogs:
         after = datetime.now()
         tInfo = TranslationInfo(self)
         key = getKey(tInfo.key)
         scaleMtLog(self.get_status(), after-before, tInfo, key, len(toTranslate))