Ejemplo n.º 1
0
 def add_store(self, filename, store, source_lang, target_lang, callback=None):
     request = RESTRequest(
         self.base_url + "/%s/%s/store" % (source_lang, target_lang), filename, "POST", json.dumps(store)
     )
     self.add(request)
     if callback:
         request.connect("http-success", lambda widget, response: callback(widget, widget.id, json.loads(response)))
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace('_', '-')
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug('language pair not supported: %s => %s' % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from':    source_lang,
                'to':      target_lang,
            }

            req = RESTRequest(real_url, '', method='GET', data=urllib.urlencode(''), headers=None)
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
Ejemplo n.º 3
0
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        pair = (self.source_lang, self.target_lang)
        if pair not in self.language_pairs:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit("match-found", query_str, self.cache[query_str])
        else:
            values = {
                "appId": self.appid,
                "q": query_str,
                "langpair": "%s|%s" % (self.source_lang, self.target_lang),
                "markUnknown": "no",
                "format": "html",
            }
            req = RESTRequest(
                self.url_translate + "?" + urllib.urlencode(values),
                "",
                method="GET",
                data=urllib.urlencode(""),
                headers=None,
            )
            self.client.add(req)
            req.connect("http-success", lambda req, response: self.got_translation(response, query_str))
Ejemplo n.º 4
0
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang,
                                           self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang,
                                           self.target_lang).replace('_', '-')
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug('language pair not supported: %s => %s' %
                          (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from': source_lang,
                'to': target_lang,
            }

            req = RESTRequest(real_url,
                              '',
                              method='GET',
                              data=urllib.urlencode(''),
                              headers=None)
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))
Ejemplo n.º 5
0
 def forget_store(self, store, callback=None):
     request = RESTRequest(self.base_url + "/store", store.filename,
                           "DELETE")
     self.add(request)
     if callback:
         request.connect(
             "http-success", lambda widget, response: callback(
                 widget, widget.id, json.loads(response)))
Ejemplo n.º 6
0
 def translate_unit(self, unit_source, source_lang, target_lang, callback=None):
     """suggest translations from TM"""
     request = RESTRequest(
         self.base_url + "/%s/%s/unit" % (source_lang, target_lang), unit_source, "GET", user_agent=self.user_agent
     )
     self.add(request)
     if callback:
         request.connect("http-success", lambda widget, response: callback(widget, widget.id, json.loads(response)))
 def lang_negotiate(self, language, callback):
     #Open-Tran uses codes such as pt_br, and zh_cn
     opentran_lang = language.lower().replace('-', '_').replace('@', '_')
     request_body = xmlrpclib.dumps((opentran_lang,), "supported")
     request = RESTRequest(
         self.url, language, "POST", request_body)
     request.curl.setopt(pycurl.URL, self.url)
     self.add(request)
     request.connect("http-success", callback)
Ejemplo n.º 8
0
 def update_unit(self, unit, source_lang, target_lang, callback=None):
     request = RESTRequest(
             self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
             unit['source'], "POST", json.dumps(unit))
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 9
0
 def forget_unit(self, unit_source, source_lang, target_lang, callback=None):
     request = RESTRequest(
         self.base_url + "/%s/%s/unit" % (source_lang, target_lang, self.user_agent),
         unit_source,
         "DELETE",
         user_agent=self.user_agent,
     )
     self.add(request)
     if callback:
         request.connect("http-success", lambda widget, response: callback(widget, widget.id, json.loads(response)))
Ejemplo n.º 10
0
 def get_store_stats(self, store, callback=None):
     request = RESTRequest(self.base_url + "/store",
                           store.filename,
                           "GET",
                           user_agent=self.user_agent)
     self.add(request)
     if callback:
         request.connect(
             "http-success", lambda widget, response: callback(
                 widget, widget.id, json.loads(response)))
Ejemplo n.º 11
0
 def get_store_stats(self, store, callback=None):
     request = RESTRequest(
             self.base_url + "/store",
             store.filename, "GET")
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 12
0
 def add_store(self, filename, store, source_lang, target_lang, callback=None):
     request = RESTRequest(
             self.base_url + "/%s/%s/store" % (source_lang, target_lang),
             filename, "POST", json.dumps(store))
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 13
0
 def update_unit(self, unit, source_lang, target_lang, callback=None):
     request = RESTRequest(
             self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
             unit['source'], "POST", json.dumps(unit),
             user_agent=self.user_agent)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 14
0
 def forget_unit(self, unit_source, source_lang, target_lang, callback=None):
     request = RESTRequest(
             self.base_url + "/%s/%s/unit" % (source_lang, target_lang, self.user_agent),
             unit_source, "DELETE",
             user_agent=self.user_agent)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 15
0
 def add_unit(self, unit, source_lang, target_lang, callback=None):
     request = RESTRequest(
         self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
         unit["source"],
         "PUT",
         json.dumps(unit),
         user_agent=self.user_agent,
     )
     self.add(request)
     if callback:
         request.connect("http-success", lambda widget, response: callback(widget, widget.id, json.loads(response)))
Ejemplo n.º 16
0
 def upload_store(self, store, source_lang, target_lang, callback=None):
     data = str(store)
     request = RESTRequest(
             self.base_url + "/%s/%s/store" % (source_lang, target_lang),
             store.filename, "PUT", data)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 17
0
 def translate_unit(self, unit_source, source_lang, target_lang, callback=None):
     """suggest translations from TM"""
     request = RESTRequest(
             self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
             unit_source, "GET",
             user_agent=self.user_agent)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 18
0
 def upload_store(self, store, source_lang, target_lang, callback=None):
     data = str(store)
     request = RESTRequest(
             self.base_url + "/%s/%s/store" % (source_lang, target_lang),
             store.filename, "PUT", data,
             user_agent=self.user_agent)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 19
0
 def __init__(self, internal_name, controller):
     self.internal_name = internal_name
     super(TMModel, self).__init__(controller)
     self.load_config()
     if not self.config['api_key']:
         self._disable_all(
             "An API key is needed to use the Google Translate plugin")
         return
     self.client = HTTPClient()
     self._languages = set()
     langreq = RESTRequest(self.url_getlanguages % self.config, '')
     self.client.add(langreq)
     langreq.connect('http-success',
                     lambda langreq, response: self.got_languages(response))
Ejemplo n.º 20
0
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.language_pairs = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getpairs = "%(url)s/listPairs?appId=%(appid)s" % {"url": self.url, "appid": self.config["appid"]}
        self.url_translate = "%(url)s/translate" % {"url": self.url}
        self.appid = self.config["appid"]
        langreq = RESTRequest(self.url_getpairs, "", method="GET", data=urllib.urlencode(""), headers=None)
        self.client.add(langreq)
        langreq.connect("http-success", lambda langreq, response: self.got_language_pairs(response))

        super(TMModel, self).__init__(controller)
Ejemplo n.º 21
0
 def __init__(self, internal_name, controller):
     self.internal_name = internal_name
     super(TMModel, self).__init__(controller)
     self.load_config()
     if not self.config['api_key']:
         self._disable_all("An API key is needed to use the Google Translate plugin")
         return
     self.client = HTTPClient()
     self._languages = set()
     langreq = RESTRequest(self.url_getlanguages % self.config, '')
     self.client.add(langreq)
     langreq.connect(
         'http-success',
         lambda langreq, response: self.got_languages(response)
     )
Ejemplo n.º 22
0
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.language_pairs = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getpairs = "%(url)s/listPairs?appId=%(appid)s" % {"url": self.url, "appid": self.config["appid"]}
        self.url_translate = "%(url)s/translate" % {"url": self.url}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getpairs, '')
        self.client.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_language_pairs(response)
        )

        super(TMModel, self).__init__(controller)
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.languages = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getlanguages = "%(url)s/GetLanguages?appId=%(appid)s" % {"url": self.config['url'], "appid": self.config["appid"]}
        self.url_translate = "%(url)s/Translate" % {"url": self.config['url']}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getlanguages, '', method='GET', data=urllib.urlencode(''), headers=None)
        self.client.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_languages(response)
        )

        super(TMModel, self).__init__(controller)
Ejemplo n.º 24
0
 def translate_unit(self, unit_source, source_lang, target_lang, callback=None, params=None):
     """suggest translations from TM"""
     request = RESTRequest(
             self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
             unit_source, "GET",
             user_agent=self.user_agent,
             params=params,
     )
     # TM requests have to finish quickly to be useful. This also helps to
     # avoid buildup in case of network failure
     request.curl.setopt(pycurl.TIMEOUT, 30)
     self.add(request)
     if callback:
         request.connect(
             "http-success",
             lambda widget, response: callback(widget, widget.id, json.loads(response))
         )
Ejemplo n.º 25
0
    def translate_unit(self, unit_source, callback=None):
        if self.source_lang is None or self.target_lang is None:
            return

        if not self._languages:
            # for some reason we don't (yet) have supported languages
            return

        query_str = unit_source
        request = RESTRequest(self.url_translate % (self.source_lang, self.target_lang), id=query_str)
        self.add(request)
        def call_callback(request, response):
            return callback(
                request, request.id, self.format_suggestions(request.id, response)
            )

        if callback:
            request.connect("http-success", call_callback)
    def translate_unit(self, unit_source, callback=None):
        if self.source_lang is None or self.target_lang is None:
            return

        if not self._languages:
            # for some reason we don't (yet) have supported languages
            return

        query_str = unit_source
        request = RESTRequest(self.url_translate % (self.source_lang, self.target_lang), id=query_str, method='GET', \
                data=urllib.urlencode(''))
        self.add(request)
        def call_callback(request, response):
            return callback(
                request, request.id, self.format_suggestions(request.id, response)
            )

        if callback:
            request.connect("http-success", call_callback)
    def translate_unit(self, unit_source, callback=None):
        if self.source_lang is None or self.target_lang is None:
            return
        if isinstance(unit_source, unicode):
            unit_source = unit_source.encode("utf-8")

        request_body = xmlrpclib.dumps(
            (unit_source, self.source_lang, self.target_lang), "suggest2"
        )
        request = RESTRequest(
            self.url, unit_source, "POST", request_body
        )
        request.curl.setopt(pycurl.URL, self.url)
        self.add(request)
        def call_callback(widget, response):
            return callback(
                widget, widget.id, self.format_suggestions(widget.id, response)
            )

        if callback:
            request.connect("http-success", call_callback)
Ejemplo n.º 28
0
    def __init__(self, max_candidates=3, min_similarity=75, max_length=1000):
        gobject.GObject.__init__(self)
        HTTPClient.__init__(self)

        self.max_candidates = max_candidates
        self.min_similarity = min_similarity
        self.comparer = LevenshteinComparer(max_length)
        self.last_suggestions = []  # used by the open-tran terminology backend

        self._languages = set()

        self.source_lang = None
        self.target_lang = None
        #detect supported language

        self.url_getlanguages = 'http://open-tran.eu/json/supported'
        self.url_translate = 'http://%s.%s.open-tran.eu/json/suggest'
        langreq = RESTRequest(self.url_getlanguages, id='')
        self.add(langreq)
        langreq.connect('http-success',
                        lambda langreq, response: self.got_languages(response))
Ejemplo n.º 29
0
    def __init__(self, internal_name, controller):
        self.internal_name = internal_name
        self.languages = []
        self.load_config()

        self.client = HTTPClient()
        self.url_getlanguages = "%(url)s/GetLanguages?appId=%(appid)s" % {
            "url": self.config['url'],
            "appid": self.config["appid"]
        }
        self.url_translate = "%(url)s/Translate" % {"url": self.config['url']}
        self.appid = self.config['appid']
        langreq = RESTRequest(self.url_getlanguages,
                              '',
                              method='GET',
                              data=urllib.urlencode(''),
                              headers=None)
        self.client.add(langreq)
        langreq.connect('http-success',
                        lambda langreq, response: self.got_languages(response))

        super(TMModel, self).__init__(controller)
Ejemplo n.º 30
0
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        if self.source_lang not in self.languages or self.target_lang not in self.languages:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'text': query_str,
                'from': self.source_lang,
                'to': self.target_lang
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '', method='GET', \
                    data=urllib.urlencode(''), headers=None)
            self.client.add(req)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))
Ejemplo n.º 31
0
 def translate_unit(self,
                    unit_source,
                    source_lang,
                    target_lang,
                    callback=None,
                    params=None):
     """suggest translations from TM"""
     request = RESTRequest(
         self.base_url + "/%s/%s/unit" % (source_lang, target_lang),
         unit_source,
         "GET",
         user_agent=self.user_agent,
         params=params,
     )
     # TM requests have to finish quickly to be useful. This also helps to
     # avoid buildup in case of network failure
     request.curl.setopt(pycurl.TIMEOUT, 30)
     self.add(request)
     if callback:
         request.connect(
             "http-success", lambda widget, response: callback(
                 widget, widget.id, json.loads(response)))
Ejemplo n.º 32
0
    def __init__(self, max_candidates=3, min_similarity=75, max_length=1000):
        gobject.GObject.__init__(self)
        HTTPClient.__init__(self)

        self.max_candidates = max_candidates
        self.min_similarity = min_similarity
        self.comparer = LevenshteinComparer(max_length)
        self.last_suggestions = None  # used by the open-tran terminology backend

        self._languages = set()

        self.source_lang = None
        self.target_lang = None
        #detect supported language

        self.url_getlanguages = 'http://open-tran.eu/json/supported'
        self.url_translate = 'http://%s.%s.open-tran.eu/json/suggest'
        langreq = RESTRequest(self.url_getlanguages, id='', method='GET', data=urllib.urlencode(''))
        self.add(langreq)
        langreq.connect(
            'http-success',
            lambda langreq, response: self.got_languages(response)
        )
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        if self.source_lang not in self.languages or self.target_lang not in self.languages:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'text': query_str,
                'from': self.source_lang,
                'to': self.target_lang
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '', method='GET', \
                    data=urllib.urlencode(''), headers=None)
            self.client.add(req)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says no more than 5000 characters
        query_str = query_str[:5000]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace("_", "-")
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace("_", "-")
        if source_lang not in _languages or target_lang not in _languages:
            logging.debug("language pair not supported: %s => %s" % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit("match-found", query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                "message": urllib.quote_plus(query_str.encode("utf-8")),
                "from": source_lang,
                "to": target_lang,
            }

            req = RESTRequest(real_url, "", method="GET", data=urllib.urlencode(""), headers=None)
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect("http-success", lambda req, response: self.got_translation(response, query_str))
Ejemplo n.º 35
0
    def query(self, tmcontroller, unit):
        """Send the query to the web service. The response is handled by means
        of a call-back because it happens asynchronously."""
        pair = (self.source_lang, self.target_lang)
        if pair not in self.language_pairs:
            return

        query_str = unit.source
        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            values = {
                'appId': self.appid,
                'q': query_str,
                'langpair': "%s|%s" % (self.source_lang, self.target_lang),
                'markUnknown': "no",
                'format': 'html',
            }
            req = RESTRequest(self.url_translate + "?" + urllib.urlencode(values), '')
            self.client.add(req)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
Ejemplo n.º 36
0
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says the whole URL must be less than "2K"
        # characters.
        query_str = query_str[:2000 - len(self.translate_url)]
        source_lang = code_translation.get(self.source_lang, self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang, self.target_lang).replace('_', '-')
        if source_lang not in self._languages or target_lang not in self._languages:
            logging.debug('language pair not supported: %s => %s' % (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'key':     self.config['api_key'],
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from':    source_lang,
                'to':      target_lang,
            }

            req = RESTRequest(real_url, '')
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success',
                lambda req, response: self.got_translation(response, query_str)
            )
            req.connect(
                'http-client-error',
                lambda req, response: self.got_error(response, query_str)
            )
            req.connect(
                'http-server-error',
                lambda req, response: self.got_error(response, query_str)
            )
Ejemplo n.º 37
0
    def query(self, tmcontroller, unit):
        query_str = unit.source
        # Google's Terms of Service says the whole URL must be less than "2K"
        # characters.
        query_str = query_str[:2000 - len(self.translate_url)]
        source_lang = code_translation.get(self.source_lang,
                                           self.source_lang).replace('_', '-')
        target_lang = code_translation.get(self.target_lang,
                                           self.target_lang).replace('_', '-')
        if source_lang not in self._languages or target_lang not in self._languages:
            logging.debug('language pair not supported: %s => %s' %
                          (source_lang, target_lang))
            return

        if self.cache.has_key(query_str):
            self.emit('match-found', query_str, self.cache[query_str])
        else:
            real_url = self.translate_url % {
                'key': self.config['api_key'],
                'message': urllib.quote_plus(query_str.encode('utf-8')),
                'from': source_lang,
                'to': target_lang,
            }

            req = RESTRequest(real_url, '')
            self.client.add(req)
            # Google's Terms of Service says we need a proper HTTP referrer
            req.curl.setopt(pycurl.REFERER, virtaal_referrer)
            req.connect(
                'http-success', lambda req, response: self.got_translation(
                    response, query_str))
            req.connect(
                'http-client-error',
                lambda req, response: self.got_error(response, query_str))
            req.connect(
                'http-server-error',
                lambda req, response: self.got_error(response, query_str))