[gedit-plugins/translate] Allow configuration different services
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit-plugins/translate] Allow configuration different services
- Date: Thu, 20 Jul 2017 18:56:07 +0000 (UTC)
commit 04ddd6e1c6506dfb351d45f72e998e82f7284002
Author: Jordi Mas <jmas softcatala org>
Date: Thu Jul 20 20:55:50 2017 +0200
Allow configuration different services
plugins/translate/Makefile.am | 8 +-
...rg.gnome.gedit.plugins.translate.gschema.xml.in | 14 +++
plugins/translate/tests/testapertium.py | 10 +--
plugins/translate/translate/__init__.py | 2 +-
plugins/translate/translate/preferences.py | 98 ++++++++++++++++----
.../translate/{backends => services}/apertium.py | 16 ++-
.../translator.py => services/service.py} | 18 ++++-
.../translator.py => services/services.py} | 25 ++++-
plugins/translate/translate/ui/preferences.ui | 27 +-----
9 files changed, 153 insertions(+), 65 deletions(-)
---
diff --git a/plugins/translate/Makefile.am b/plugins/translate/Makefile.am
index badda07..0974270 100644
--- a/plugins/translate/Makefile.am
+++ b/plugins/translate/Makefile.am
@@ -5,9 +5,11 @@ plugins_translate_PYTHON = \
plugins/translate/translate/preferences.py \
plugins/translate/translate/translateview.py
-plugins_backendsdir = $(plugindir)/translate/backends
-dist_plugins_backends_DATA = plugins/translate/translate/backends/apertium.py \
- plugins/translate/translate/backends/translator.py
+plugins_servicesdir = $(plugindir)/translate/services
+dist_plugins_services_DATA = plugins/translate/translate/services/apertium.py \
+ plugins/translate/translate/services/service.py \
+ plugins/translate/translate/services/services.py \
+ plugins/translate/translate/services/yandex.py
plugins_preferences_uidir = $(GEDIT_PLUGINS_DATA_DIR)/translate/ui
dist_plugins_preferences_ui_DATA = plugins/translate/translate/ui/preferences.ui
diff --git a/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml.in
b/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml.in
index aadbf23..29b7068 100644
--- a/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml.in
+++ b/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml.in
@@ -14,5 +14,19 @@
Language pair used to translate from one language to another
</description>
</key>
+ <key type="s" name="api-key">
+ <default>''</default>
+ <summary>API key for remote web service</summary>
+ <description>
+ API key for remote web service
+ </description>
+ </key>
+ <key type="i" name="service">
+ <default>0</default>
+ <summary>Remote web service to use</summary>
+ <description>
+ Remote web service to use
+ </description>
+ </key>
</schema>
</schemalist>
diff --git a/plugins/translate/tests/testapertium.py b/plugins/translate/tests/testapertium.py
index 795c4e5..ea4f448 100644
--- a/plugins/translate/tests/testapertium.py
+++ b/plugins/translate/tests/testapertium.py
@@ -19,28 +19,24 @@
import unittest
-from backends.apertium import Apertium
+from services.apertium import Apertium
from unittest.mock import patch, MagicMock
-# See: https://stackoverflow.com/questions/32043035/python-3-urlopen-context-manager-mocking
class TestPagination(unittest.TestCase):
@patch('urllib.request.urlopen')
- def test_cm(self, mock_urlopen):
+ def test_translate_text(self, mock_urlopen):
cm = MagicMock()
cm.getcode.return_value = 200
cm.read.return_value = bytes('{"responseData": {"translatedText": "Hauries d\'haver-hi rebut una
c\u00f2pia"}, "responseDetails": null, "responseStatus": 200}', 'utf-8')
cm.__enter__.return_value = cm
mock_urlopen.return_value = cm
- apertium = Apertium('hola')
+ apertium = Apertium(False)
translated = apertium.translate_text('You should have received a copy', 'eng|cat')
self.assertEqual('Hauries d\'haver-hi rebut una còpia', translated)
mock_urlopen.assert_called_with("https://www.apertium.org/apy/translate?langpair=eng|cat&markUnknown=no&q=You+should+have+received+a+copy")
- #with urllib.request.urlopen('http://foo') as response:
- # self.assertEqual(response.getcode(), 200)
- # self.assertEqual(response.read(), 'contents') self.assertEquals(pagination.page, 2)
if __name__ == '__main__':
diff --git a/plugins/translate/translate/__init__.py b/plugins/translate/translate/__init__.py
index 47a05b0..e3bc169 100644
--- a/plugins/translate/translate/__init__.py
+++ b/plugins/translate/translate/__init__.py
@@ -23,7 +23,7 @@ gi.require_version('GtkSource', '3.0')
gi.require_version('PeasGtk', '1.0')
from gi.repository import GObject, Gio, Gtk, Gedit, PeasGtk
-from .backends.apertium import Apertium
+from .services.apertium import Apertium
from .translateview import TranslateView
from .preferences import Preferences
import gettext
diff --git a/plugins/translate/translate/preferences.py b/plugins/translate/translate/preferences.py
index 17b50e2..546322f 100644
--- a/plugins/translate/translate/preferences.py
+++ b/plugins/translate/translate/preferences.py
@@ -21,6 +21,7 @@ import os
from gi.repository import Gio, Gtk
import gettext
from gpdefs import *
+from .services.services import Services
class Preferences(object):
@@ -28,6 +29,8 @@ class Preferences(object):
TRANSLATE_KEY_BASE = 'org.gnome.gedit.plugins.translate'
OUTPUT_TO_DOCUMENT = 'output-to-document'
LANGUAGE_PAIR = 'language-pair'
+ API_KEY = 'api-key'
+ SERVICE = 'service'
LANG_NAME = 0
LANG_CODE = 1
@@ -42,13 +45,47 @@ class Preferences(object):
self._ui.set_translation_domain(GETTEXT_PACKAGE)
self._ui.add_from_file(self._ui_path)
- self.init_radiobuttons()
- self.init_combobox()
+ self._init_radiobuttons()
+ self._init_combobox_languages()
+ self._init_combobox_services()
+ self._init_api_entry()
- def init_radiobuttons(self):
+ def _init_api_entry(self):
+ service_id = self._settings.get_uint(self.SERVICE)
+ service = Services.get(service_id)
+ if service.has_api_key() is True:
+ self._update_api_key_ui(True)
+ return
+ else:
+ self._apikey = None
+ return
+
+ self._apikey.connect('changed', self._changed_apikey)
+ key = self._settings.get_string(self.API_KEY)
+ self._apikey.set_text(key)
+
+ def _update_api_key_ui(self, show):
+ apibox = self._ui.get_object('api_box')
+
+ print("_update_api_key_ui show:" + str(show))
+ if show is True:
+ self._apilabel = Gtk.Label("API Key")
+ self._apikey= Gtk.Entry(expand=True)
+
+ apibox.add(self._apilabel)
+ apibox.add(self._apikey)
+ apibox.show_all()
+ else:
+ apibox.remove(self._apilabel)
+ apibox.remove(self._apikey)
+ self._apilabel = None
+ self._apikey = None
+
+ def _init_radiobuttons(self):
self._radio_samedoc = self._ui.get_object('same_document')
self._output_window = self._ui.get_object('output_window')
+ self._radio_samedoc.connect("toggled", self._radio_samedoc_callback)
active = self._settings.get_boolean(self.OUTPUT_TO_DOCUMENT)
if active:
@@ -56,22 +93,39 @@ class Preferences(object):
else:
self._output_window.set_active(active is False)
- def init_combobox(self):
+ def _init_combobox_services(self):
+ self._services = self._ui.get_object('services')
+
+ cell = Gtk.CellRendererText()
+ self._services.pack_start(cell, 1)
+ self._services.add_attribute(cell, 'text', 0)
+ services = Services.get_names_and_ids()
+
+ model = Gtk.ListStore(str, int)
+ for service_id in services.keys():
+ model.append((services[service_id], service_id))
+
+ self._services.set_model(model)
+ service_id = self._settings.get_uint(self.SERVICE)
+ self._services.set_active(service_id)
+ self._services.connect('changed', self._changed_services)
+
+ def _init_combobox_languages(self):
self._languages = self._ui.get_object('languages')
cell = Gtk.CellRendererText()
self._languages.pack_start(cell, 1)
self._languages.add_attribute(cell, 'text', 0)
- self._model = self._get_stored_model()
+ self._model = self._get_languages_stored_model()
self._languages.set_model(self._model)
- self._languages.connect('changed', self.changed_cb)
+ self._languages.connect('changed', self._changed_lang_pair)
selected = self._settings.get_string(self.LANGUAGE_PAIR)
- index = self.get_index(selected)
+ index = self._get_index(selected)
self._languages.set_active(index)
- def _get_stored_model(self):
+ def _get_languages_stored_model(self):
sorted_language_names = set()
for i in range(len(self._language_names)):
@@ -86,13 +140,13 @@ class Preferences(object):
return model
- def get_index(self, selected):
+ def _get_index(self, selected):
for i in range(len(self._model)):
if self._model[i][Preferences.LANG_CODE] == selected:
return i
return -1
- def changed_cb(self, combobox):
+ def _changed_lang_pair(self, combobox):
model = combobox.get_model()
index = combobox.get_active()
if index > -1:
@@ -100,16 +154,26 @@ class Preferences(object):
self._settings.set_string(self.LANGUAGE_PAIR, item[Preferences.LANG_CODE])
return
- def set_output_to_doc(self, active):
- self._settings.set_boolean(self.OUTPUT_TO_DOCUMENT, active)
+ def _changed_services(self, combobox):
+ model = combobox.get_model()
+ index = combobox.get_active()
+ if index > -1:
+ item = model[index]
+ service_id = item[1]
+ self._settings.set_uint(self.SERVICE, service_id)
+ service = Services.get(service_id)
+ self._update_api_key_ui(service.has_api_key())
+ return
+
+ def _changed_apikey(self, text_entry):
+ text = text_entry.get_text()
+ self._settings.set_string(self.API_KEY, text)
+ return
- def radio_samedoc_callback(self, widget, data=None):
- self.set_output_to_doc(widget.get_active())
+ def _radio_samedoc_callback(self, widget, data=None):
+ self._settings.set_boolean(self.OUTPUT_TO_DOCUMENT, widget.get_active())
def configure_widget(self):
self._ui.connect_signals(self)
- self._radio_samedoc.connect("toggled", self.radio_samedoc_callback)
-
widget = self._ui.get_object('grid')
return widget
-
diff --git a/plugins/translate/translate/backends/apertium.py
b/plugins/translate/translate/services/apertium.py
similarity index 96%
rename from plugins/translate/translate/backends/apertium.py
rename to plugins/translate/translate/services/apertium.py
index 63a4f7f..c7e4ab5 100644
--- a/plugins/translate/translate/backends/apertium.py
+++ b/plugins/translate/translate/services/apertium.py
@@ -20,9 +20,9 @@
import urllib.request, urllib.parse, urllib.error
import json
import locale
-from .translator import Translator
+from .service import Service
-class Apertium(Translator):
+class Apertium(Service):
g_language_codes = []
g_language_names = []
@@ -41,11 +41,15 @@ class Apertium(Translator):
SERVER = "https://www.apertium.org/apy"
- def __init__(self):
- self._get_remote_language_names_and_pairs()
+ def __init__(self, init = True):
+ if init is True:
+ self._get_remote_language_names_and_pairs()
- def __init__(self, string):
- pass
+ def has_api_key(self):
+ return False
+
+ def set_api_key(self):
+ pass
def get_language_names(self):
if len(Apertium.g_language_codes) > 0 and len(Apertium.g_language_names) > 0:
diff --git a/plugins/translate/translate/backends/translator.py
b/plugins/translate/translate/services/service.py
similarity index 75%
copy from plugins/translate/translate/backends/translator.py
copy to plugins/translate/translate/services/service.py
index 99f36e1..7617b1a 100644
--- a/plugins/translate/translate/backends/translator.py
+++ b/plugins/translate/translate/services/service.py
@@ -19,9 +19,25 @@
from abc import ABCMeta, abstractmethod
-class Translator(metaclass=ABCMeta):
+class Service(metaclass=ABCMeta):
@abstractmethod
def translate_text(self, text, language_pair):
pass
+ @abstractmethod
+ def get_language_names(self):
+ pass
+
+ @abstractmethod
+ def get_language_codes(self):
+ pass
+
+ @abstractmethod
+ def has_api_key(self):
+ pass
+
+ @abstractmethod
+ def set_api_key(self):
+ pass
+
diff --git a/plugins/translate/translate/backends/translator.py
b/plugins/translate/translate/services/services.py
similarity index 62%
rename from plugins/translate/translate/backends/translator.py
rename to plugins/translate/translate/services/services.py
index 99f36e1..57f99c2 100644
--- a/plugins/translate/translate/backends/translator.py
+++ b/plugins/translate/translate/services/services.py
@@ -17,11 +17,26 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
-from abc import ABCMeta, abstractmethod
+from .apertium import Apertium
+from .yandex import Yandex
-class Translator(metaclass=ABCMeta):
- @abstractmethod
- def translate_text(self, text, language_pair):
- pass
+class Services():
+ SERVICES = {0: "Apertium", 1: "Yandex"}
+
+ @staticmethod
+ def get_name(service_id):
+ return Services.SERVICES[service_id]
+
+ @staticmethod
+ def get(service_id):
+ print(service_id)
+ if service_id == 0:
+ return Apertium()
+ elif service_id == 1:
+ return Yandex()
+
+ @staticmethod
+ def get_names_and_ids():
+ return Services.SERVICES
diff --git a/plugins/translate/translate/ui/preferences.ui b/plugins/translate/translate/ui/preferences.ui
index 6a16c07..96de721 100644
--- a/plugins/translate/translate/ui/preferences.ui
+++ b/plugins/translate/translate/ui/preferences.ui
@@ -111,32 +111,9 @@
</packing>
</child>
<child>
- <object class="GtkBox">
+ <object class="GtkBox" id ="api_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <child>
- <object class="GtkLabel" id="api_key">
- <property name="visible">True</property>
- <property name="can_focus">False</property>
- <property name="label" translatable="yes">API Key</property>
- </object>
- <packing>
- <property name="expand">False</property>
- <property name="fill">True</property>
- <property name="position">0</property>
- </packing>
- </child>
- <child>
- <object class="GtkEntry">
- <property name="visible">True</property>
- <property name="can_focus">True</property>
- </object>
- <packing>
- <property name="expand">True</property>
- <property name="fill">True</property>
- <property name="position">1</property>
- </packing>
- </child>
</object>
<packing>
<property name="left_attach">0</property>
@@ -160,7 +137,7 @@
</packing>
</child>
<child>
- <object class="GtkComboBox" id="service">
+ <object class="GtkComboBox" id="services">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]