[orca/new-settings] Some new methods added and removed to create a better module interface.



commit 30cc1036cb4b45ab66dad864799ae2ccf2b82e1f
Author: Juanje Ojeda <jojeda emergya es>
Date:   Sun Oct 24 22:48:33 2010 +0200

    Some new methods added and removed to create a better module interface.

 src/orca/settings_manager.py |  215 ++++++++++++++++++++++++++++++++----------
 1 files changed, 164 insertions(+), 51 deletions(-)
---
diff --git a/src/orca/settings_manager.py b/src/orca/settings_manager.py
index 72b9cdd..b5b9ea6 100644
--- a/src/orca/settings_manager.py
+++ b/src/orca/settings_manager.py
@@ -28,33 +28,177 @@ __copyright__ = "Copyright (c) 2010 Consorcio Fernando de los Rios."
 __license__   = "LGPL"
 
 
-class Singleton(object):
+class SettingsManager():
+    """Settings backend manager. This class manages orca user's settings
+    using different backends"""
+
     def __new__(cls, *args, **kwargs):
         if '__instance' not in vars(cls):
             cls.__instance = object.__new__(cls, *args, **kwargs)
         return cls.__instance
 
-class SettingsManager(Singleton):
-    """Settings backend manager. This class manages orca user's settings
-    using different backends"""
-
-    # At the moment, DEFAULT_BACKEND can be established here
-    # Possible values are: classic and gconf
-    DEFAULT_BACKEND = 'gconf_backend'
-    #DEFAULT_BACKEND = 'classic'
-
-    def __init__(self, backend = None):
+    def __init__(self, backend='gconf', profile='default'):
         """Initialize a SettingsManager Object.
         If backend isn't defined then uses default backend, in this
-        case gconf-backend"""
+        case gconf-backend.
+        backend parameter can use the follow values:
+        backend='gconf'
+        backend='classic'
+        backend='yaml'
+        """
 
-        self.userSettings = None
         self.backendModule = None
+        self._backend = None
+        self.profile = profile
+        self.backendName = backend
+        self.defaultGeneral = {}
+        self.defaultPronunciations = {}
+        self.defaultKeybindings = {}
+        self.profileGeneral = {}
+        self.profilePronunciations = {}
+        self.profileKeybindings = {}
+        self.general = {}
+        self.pronunciations = {}
+        self.keybindings = {}
+
+        if not self._loadBackend():
+            return False
 
-        if not backend:
-            self.backend = self.DEFAULT_BACKEND
-        else:
-            self.backend = backend
+        self._backend = self.backendModule.Backend()
+        self.defaultGeneral = self._getDefaultGeneral()
+        self.defaultPronunciations = self._getDefaultPronunciations()
+        self.defaultKeybindings = self._getDefaultKeybindings()
+        self.general = self.defaultGeneral
+        self.pronunciations = self.defaultPronunciations
+        self.keybindings = self.defaultKeybindings
+
+        self.setProfile(self.profile)
+        if self.isFirstStart():
+            self._createDefaults()
+
+    def _loadBackend(self):
+        """Load specific backend for manage user settings"""
+
+        try:
+            self.backendModule = __import__('backends.%s' % self.backendName, \
+                                            globals(),
+                                            locals(),
+                                            self.backendName)
+            print '\nImporting %s backend OK!' % self.backendName
+            return True
+        except:
+            print '\nUnable to load %s backend' % self.backendName
+            return False
+
+    def _createDefaults(self):
+        """Let the active backend to create the initial structure
+        for storing the settings and save the default ones from
+        orca.settings"""
+        self._backend.createGeneral(self.general,
+                                    self.pronunciations,
+                                    self.keybindings)
+
+    def _getGeneral(self):
+        """Get from the active backend the general settings for
+        the current profile"""
+        generalSettings = self._backend.getGeneral(self.profile)
+        self.self.profileGeneral = generalSettings
+
+    def _getPronunciations(self):
+        """Get from the active backend the pronunciations settings for
+        the current profile"""
+        pronunciationsSettings = self._backend.getPronunciations(self.profile)
+        self.self.profilePronunciations = pronunciationsSettings
+
+    def _getKeybindings(self):
+        """Get from the active backend the keybindings settings for
+        the current profile"""
+        keybindingsSettings = self._backend.getKeybindings(self.profile)
+        self.self.profileKeybindings = keybindingsSettings
+
+    def _loadProfileSettings(profile=None):
+        """Get from the active backend all the settings for the current
+        profile and store them in the object's attributes.
+        A profile can be passed as a parameter. This could be useful for
+        change from one profile to another."""
+        if profile is None:
+            profile = self.profile
+        self.profileGeneral = self._getGeneral(profile)
+        self.profilePronunciations = self._getPronunciations(profile)
+        self.profileKeybindings = self._getKeybindings(profile)
+
+    def _mergeSettings():
+        """Update the changed values on the profile settings
+        over the current and active settings"""
+        self.general.update(self.profileGeneral)
+        self.pronunciations.update(self.profilePronunciation)
+        self.keybindings.update(self.profileKeybindings)
+
+    def setProfile(profile='default'):
+        """Set a specific profile as the active one.
+        Also the settings from that profile will be loading
+        and updated the current settings with them."""
+        self.profile = profile
+        self._loadProfileSettings(profile)
+        self.mergeSettings()
+
+    def getGeneralSettings(self):
+        """Return the current general settings.
+        Those settings comes from updating the default settings
+        with the profiles' ones"""
+        return self.general
+
+    def getPronunciations(self):
+        """Return the current pronunciations settings.
+        Those settings comes from updating the default settings
+        with the profiles' ones"""
+        return self.pronunciations
+
+    def getKeybindings(self):
+        """Return the current keybindings settings.
+        Those settings comes from updating the default settings
+        with the profiles' ones"""
+        return self.keybindings
+
+    def _setProfileGeneral(self):
+        """Set the changed general settings from the defaults' ones
+        as the profile's."""
+        self.profileGeneral = {}
+        for key, value in self.general.items():
+            if value != self.defaultGeneral[key]:
+                self.profileGeneral[key] = value
+
+    def _setProfilePronunciations(self):
+        """Set the changed pronunciations settings from the defaults' ones
+        as the profile's."""
+        self.profilePronunciations = {}
+        for key, value in self.pronunciations.items():
+            if value != self.defaultPronunciations[key]:
+                self.profilePronunciations[key] = value
+
+    def _setProfileKeybindings(self):
+        """Set the changed keybindings settings from the defaults' ones
+        as the profile's."""
+        self.profileKeybindings = {}
+        for key, value in self.keybindings.items():
+            if value != self.defaultKeybindings[key]:
+                self.profileKeybindings[key] = value
+
+    def saveSettings(self):
+        """Let the active backend to store the default settings and
+        the profiles' ones."""
+        # The default settings should already exist but we save them anyway
+        # just in case the default settings have changed at orca.settings
+        self._backend.saveDefaultSettings(self.general,
+                                          self.pronunciations,
+                                          self.keybindings)
+        self._setProfileGeneral()
+        self._setProfilePronunciations()
+        self._setProfileKeybindings()
+        self._backend.saveProfileSettings(self.profile,
+                                          self.profileGeneral,
+                                          self.profilePronunciations,
+                                          self.profileKeybindings)
 
     def isClassic(self):
         """Is the backend the classic one?
@@ -69,49 +213,18 @@ class SettingsManager(Singleton):
         if self.isClassic():
             return False
         else:
-            return self.backendModule.isFirstStart()
+            return self._backend.isFirstStart()
 
     def setFirstStart(self):
         """Set firstStart key to false
         This means the profile is being stored and it isn't a first start
         anymore."""
         if not self.isClassic():
-            self.backendModule.setFirstStart()
-
-    def loadBackend(self):
-        """Load specific backend for manage user settings"""
-
-        try:
-            self.backendModule = __import__('backends.%s' % self.backend, \
-                                         globals(), locals(), self.backend)
-            print '\nimporting %s backend OK!' % self.backend
-            return True
-        except:
-            print '\nUnable to load %s backend' % self.backend
-            return False
-
-    def loadSettings(self):
-        """Load settings from active backend"""
-
-        return self.backendModule.loadSettings()
-
-    def writeSettings(self, prefsDict, keyBindingsTreeModel=None,
-                     pronunciationTreeModel=None):
-        """Write user settings using active backend"""
- 
-        return self.backendModule.writePreferences(prefsDict, \
-                                                keyBindingsTreeModel, \
-                                                pronunciationTreeModel)
-
-    def readPreferences(self):
-        """Read current user preferences"""
-
-        return self.backendModule.readPreferences()
-
+            self._backend.setFirstStart()
 
     def availableProfiles(self):
         """Get available profiles from active backend"""
 
-        return self.backendModule.availableProfiles() 
+        return self._backend.availableProfiles()
 
 



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