[gedit-plugins/translate] Use settings



commit efabfc01242843f34fd74a017a67d979f7f72ab3
Author: Jordi Mas <jmas softcatala org>
Date:   Sun Jul 23 23:19:25 2017 +0200

    Use settings

 plugins/translate/Makefile.am                   |    3 +-
 plugins/translate/translate/__init__.py         |   23 ++++------
 plugins/translate/translate/preferences.py      |   29 ++++-------
 plugins/translate/translate/services/service.py |    4 ++
 plugins/translate/translate/settings.py         |   57 +++++++++++++++++++++++
 5 files changed, 83 insertions(+), 33 deletions(-)
---
diff --git a/plugins/translate/Makefile.am b/plugins/translate/Makefile.am
index 0974270..2812be8 100644
--- a/plugins/translate/Makefile.am
+++ b/plugins/translate/Makefile.am
@@ -3,7 +3,8 @@ plugins_translatedir = $(plugindir)/translate
 plugins_translate_PYTHON = \
     plugins/translate/translate/__init__.py    \
        plugins/translate/translate/preferences.py      \
-       plugins/translate/translate/translateview.py 
+       plugins/translate/translate/translateview.py \
+       plugins/translate/translate/settings.py
 
 plugins_servicesdir = $(plugindir)/translate/services
 dist_plugins_services_DATA = plugins/translate/translate/services/apertium.py \
diff --git a/plugins/translate/translate/__init__.py b/plugins/translate/translate/__init__.py
index 5885203..8fbb4ae 100644
--- a/plugins/translate/translate/__init__.py
+++ b/plugins/translate/translate/__init__.py
@@ -27,6 +27,7 @@ from .services.services import Services
 from .services.apertium import Apertium
 from .translateview import TranslateView
 from .preferences import Preferences
+from .settings import Settings
 import gettext
 from gpdefs import *
 
@@ -101,17 +102,11 @@ class TranslateWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGt
     
 class TranslateViewActivatable(GObject.Object, Gedit.ViewActivatable):
 
-    TRANSLATE_KEY_BASE = 'org.gnome.gedit.plugins.translate'
-    OUTPUT_TO_DOCUMENT = 'output-to-document'
-    LANGUAGE_PAIR = 'language-pair'
-    SERVICE = 'service'
-    API_KEY = 'api-key'
-
     view = GObject.Property(type=Gedit.View)
 
     def __init__(self):
         GObject.Object.__init__(self)
-        self._settings = Gio.Settings.new(self.TRANSLATE_KEY_BASE)
+        self._settings = Settings()
 
     def do_activate(self):
         self.view.translate_view_activatable = self
@@ -121,11 +116,11 @@ class TranslateViewActivatable(GObject.Object, Gedit.ViewActivatable):
         delattr(self.view, "translate_view_activatable")
 
     def _get_language_pair_name(self):
-        language_pair = self._settings.get_string(self.LANGUAGE_PAIR)
+        language_pair = self._settings.get_language_pair()
         languages = language_pair.split('|')
 
-        apertium = Apertium()
-        return apertium.get_language_pair_name(languages[0], languages[1])
+        service = self._get_translation_service()
+        return service.get_language_pair_name(languages[0], languages[1])
 
     def populate_popup(self, view, popup):
         if not isinstance(popup, Gtk.MenuShell):
@@ -161,10 +156,10 @@ class TranslateViewActivatable(GObject.Object, Gedit.ViewActivatable):
         return start is not None and end is not None
 
     def _get_translation_service(self):
-        service_id = self._settings.get_uint(self.SERVICE)
+        service_id = self._settings.get_service()
         service = Services.get(service_id)
         if service.has_api_key() is True:
-            key = self._settings.get_string(self.API_KEY)
+            key = self._settings.get_apikey()
             service.set_api_key(key)
 
         return service
@@ -172,12 +167,12 @@ class TranslateViewActivatable(GObject.Object, Gedit.ViewActivatable):
     def translate_text(self, document, start, end):
         doc = self.view.get_buffer()
         text = doc.get_text(start, end, False)
-        language_pair = self._settings.get_string(self.LANGUAGE_PAIR)
+        language_pair = self._settings.get_language_pair()
       
         service = self._get_translation_service()
         translated = service.translate_text(text, language_pair)
 
-        if self._settings.get_boolean(self.OUTPUT_TO_DOCUMENT):
+        if self._settings.get_output_document():
             doc.insert(start, translated)
         else:
             g_console.write(translated)
diff --git a/plugins/translate/translate/preferences.py b/plugins/translate/translate/preferences.py
index 4da4f22..6287e25 100644
--- a/plugins/translate/translate/preferences.py
+++ b/plugins/translate/translate/preferences.py
@@ -22,15 +22,11 @@ from gi.repository import Gio, Gtk
 import gettext
 from gpdefs import *
 from .services.services import Services
+from .settings import Settings
 
 
 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
 
@@ -39,7 +35,7 @@ class Preferences(object):
         self._language_codes = language_codes
         self._language_names = language_names
 
-        self._settings = Gio.Settings.new(self.TRANSLATE_KEY_BASE)
+        self._settings = Settings()
         self._ui_path = os.path.join(datadir, 'ui', 'preferences.ui')
         self._ui = Gtk.Builder()
         self._ui.set_translation_domain(GETTEXT_PACKAGE)
@@ -52,7 +48,7 @@ class Preferences(object):
 
     def _init_api_entry(self):
 
-        service_id = self._settings.get_uint(self.SERVICE)
+        service_id = self._settings.get_service()
         service = Services.get(service_id)
         if service.has_api_key() is True:
             self._update_api_key_ui(True)
@@ -70,7 +66,7 @@ class Preferences(object):
             self._apikey= Gtk.Entry(expand=True)
 
             self._apikey.connect('changed', self._changed_apikey)
-            key = self._settings.get_string(self.API_KEY)
+            key = self._settings.get_apikey()
             self._apikey.set_text(key)
 
             apibox.add(self._apilabel)
@@ -86,7 +82,7 @@ class Preferences(object):
         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)
+        active = self._settings.get_output_document()
 
         if active:
             self._radio_samedoc.set_active(active)
@@ -106,7 +102,7 @@ class Preferences(object):
             model.append((services[service_id], service_id))
 
         self._services.set_model(model)
-        service_id = self._settings.get_uint(self.SERVICE)
+        service_id = self._settings.get_service()
         self._services.set_active(service_id)
         self._services.connect('changed', self._changed_services)
 
@@ -121,7 +117,7 @@ class Preferences(object):
         self._languages.set_model(self._model)
         self._languages.connect('changed', self._changed_lang_pair)
 
-        selected = self._settings.get_string(self.LANGUAGE_PAIR)
+        selected = self._settings.get_language_pair()
         index = self._get_index(selected)
         self._languages.set_active(index)
 
@@ -151,8 +147,7 @@ class Preferences(object):
         index = combobox.get_active()
         if index > -1:
             item = model[index]
-            self._settings.set_string(self.LANGUAGE_PAIR, item[Preferences.LANG_CODE])
-        return
+            self._settings.set_language_pair(item[Preferences.LANG_CODE])
 
     def _changed_services(self, combobox):
         model = combobox.get_model()
@@ -160,18 +155,16 @@ class Preferences(object):
         if index > -1:
             item = model[index]
             service_id = item[1]
-            self._settings.set_uint(self.SERVICE, service_id)
+            self._settings.set_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
+        self._settings.set_apikey(text)
 
     def _radio_samedoc_callback(self, widget, data=None):
-        self._settings.set_boolean(self.OUTPUT_TO_DOCUMENT, widget.get_active())
+        self._settings.set_output_document(widget.get_active())
 
     def configure_widget(self):
         self._ui.connect_signals(self)
diff --git a/plugins/translate/translate/services/service.py b/plugins/translate/translate/services/service.py
index 27ad804..a4bd8b9 100644
--- a/plugins/translate/translate/services/service.py
+++ b/plugins/translate/translate/services/service.py
@@ -34,6 +34,10 @@ class Service(metaclass=ABCMeta):
         pass
 
     @abstractmethod
+    def get_language_pair_name(self):
+        pass
+
+    @abstractmethod
     def has_api_key(self):
         pass
     
diff --git a/plugins/translate/translate/settings.py b/plugins/translate/translate/settings.py
new file mode 100644
index 0000000..e3f7cc2
--- /dev/null
+++ b/plugins/translate/translate/settings.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+#
+#  Copyrignt (C) 2017 Jordi Mas <jmas softcatala org>
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
+#  Boston, MA 02110-1301, USA.
+
+from gi.repository import Gio
+
+
+class Settings():
+
+    TRANSLATE_KEY_BASE = 'org.gnome.gedit.plugins.translate'
+    OUTPUT_TO_DOCUMENT = 'output-to-document'
+    LANGUAGE_PAIR = 'language-pair'
+    SERVICE = 'service'
+    API_KEY = 'api-key'
+
+    def __init__(self):
+        self._settings = Gio.Settings.new(self.TRANSLATE_KEY_BASE)
+
+    def get_language_pair(self):
+        return self._settings.get_string(self.LANGUAGE_PAIR)
+
+    def get_service(self):
+        return self._settings.get_uint(self.SERVICE)
+
+    def get_apikey(self):
+        return self._settings.get_string(self.API_KEY)
+
+    def get_output_document(self):
+        return self._settings.get_boolean(self.OUTPUT_TO_DOCUMENT)
+
+    def set_language_pair(self, language_pair):
+        self._settings.set_string(self.LANGUAGE_PAIR, language_pair)
+
+    def set_service(self, service_id):
+        self._settings.set_uint(self.SERVICE, service_id)
+
+    def set_apikey(self, text):
+        self._settings.set_string(self.API_KEY, text)
+
+    def set_output_document(self, document):
+        self._settings.set_boolean(self.OUTPUT_TO_DOCUMENT, document)
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]