Quelques optimisations pour réduire les requêtes chez Google.
authorProgfou <jean-christophe.andre@auf.org>
Mon, 28 Sep 2009 00:17:04 +0000 (07:17 +0700)
committerProgfou <jean-christophe.andre@auf.org>
Mon, 28 Sep 2009 00:17:04 +0000 (07:17 +0700)
pygtranslator/pygtranslator.py

index ae77618..69f2974 100755 (executable)
@@ -18,7 +18,7 @@ pygtk.require('2.0')
 import gtk, gobject
 
 def DEBUG(message):
-    #print "\x1b[31;1mDEBUG\x1b[m:", message
+    print "\x1b[31;1mDEBUG\x1b[m:", message
     return
 
 class XmppTranslator(Thread):
@@ -26,9 +26,11 @@ class XmppTranslator(Thread):
     def __init__(self, jid, password):
         Thread.__init__(self)
         self.setDaemon(True)
+        self._quit = False
+
         self._input_queue = Queue(0)
         self._output_queue = Queue(0)
-        self._quit = False
+        self._last_text_to_translate = {}
 
         self._jid = xmpp.JID(jid)
         self._password = password
@@ -84,6 +86,11 @@ class XmppTranslator(Thread):
             self._client.Process(1)
 
     def request_translation(self, translation_path, text):
+        text = text.strip().replace('\r\n','\n')
+        # skip consecutive identical requests
+        if text == self._last_text_to_translate.get(translation_path):
+            return
+        self._last_text_to_translate[translation_path] = text
         # remove any pending translation job
         while not self._input_queue.empty():
             self._input_queue.get()
@@ -112,7 +119,7 @@ class GtkTranslator(object):
         self.window.set_title(u"PygTranslator")
         self.window.set_border_width(0)
         self.window.connect("destroy", lambda w: gtk.main_quit())
-        self.window.connect("delete_event", self.delete_event)
+        self.window.connect("delete_event", self._delete_event)
 
         vbox = gtk.VBox(True, 0)
         vbox.show()
@@ -125,7 +132,8 @@ class GtkTranslator(object):
         scroll.add(textview)
         scroll.show()
         frame = gtk.Frame(u"Texte en français")
-        frame.connect("key_release_event", self.key_release_event, 'fr')
+        frame.connect("key_release_event", self._text_change, 'fr')
+        frame.connect("button_release_event", self._text_change, 'fr')
         frame.add(scroll)
         frame.show()
         vbox.pack_start(frame, True, True, 0)
@@ -139,7 +147,8 @@ class GtkTranslator(object):
         scroll.add(textview)
         scroll.show()
         frame = gtk.Frame(u"Text in English")
-        frame.connect("key_release_event", self.key_release_event, 'en')
+        frame.connect("key_release_event", self._text_change, 'en')
+        frame.connect("button_release_event", self._text_change, 'en')
         frame.add(scroll)
         frame.show()
         vbox.pack_start(frame, True, True, 0)
@@ -148,25 +157,19 @@ class GtkTranslator(object):
         self.window.add(vbox)
         self.window.show()
 
-    def run(self):
-        DEBUG("running graphic interface")
-        gobject.timeout_add(100, self.process_translation_result)
-        gtk.gdk.threads_init()
-        gtk.main()
-
-    def delete_event(self, widget, event, data=None):
+    def _delete_event(self, widget, event, data=None):
         DEBUG("caught delete event")
         gtk.main_quit()
         return False
 
-    def key_release_event(self, widget, event, lang):
+    def _text_change(self, widget, event, lang):
         start, end = self._textbuf[lang].get_bounds()
         text = self._textbuf[lang].get_text(start, end)
         other_lang = (set(self._textbuf.keys()) - set((lang,))).pop()
         translation_path = lang + '2' + other_lang
         self._translator.request_translation(translation_path, text)
 
-    def process_translation_result(self):
+    def _process_translation_result(self):
         result = {}
         # FIXME: to transform into an iterator...
         while True:
@@ -182,7 +185,13 @@ class GtkTranslator(object):
             self._translator.translation_done()
         for lang, text in result.items():
             self._textbuf[lang].set_text(text)
-        gobject.timeout_add(100, self.process_translation_result)
+        gobject.timeout_add(300, self._process_translation_result)
+
+    def run(self):
+        DEBUG("running graphic interface")
+        gobject.timeout_add(300, self._process_translation_result)
+        gtk.gdk.threads_init()
+        gtk.main()
 
 if __name__ == "__main__":
     import sys