[orca/new-settings] Added new settings backend based on YAML



commit 225c8b656541b216997641dacfa62ef0fb981b33
Author: Juanje Ojeda <jojeda emergya es>
Date:   Mon Oct 25 05:47:43 2010 +0200

    Added new settings backend based on YAML

 src/orca/backends/Makefile.am |    3 +-
 src/orca/backends/yaml.py     |   93 +++++++++++++++++++++++++++++++++++++++++
 src/orca/settings_manager.py  |    2 +-
 3 files changed, 96 insertions(+), 2 deletions(-)
---
diff --git a/src/orca/backends/Makefile.am b/src/orca/backends/Makefile.am
index dc84e24..37f3ac6 100644
--- a/src/orca/backends/Makefile.am
+++ b/src/orca/backends/Makefile.am
@@ -3,7 +3,8 @@ orca_pathdir=$(pyexecdir)
 orca_python_PYTHON = \
         __init__.py \
         classic.py \
-        gconf.py
+        gconf.py\
+        yaml.py
 
 orca_pythondir=$(pyexecdir)/orca/backends
 
diff --git a/src/orca/backends/yaml.py b/src/orca/backends/yaml.py
new file mode 100644
index 0000000..0348c6a
--- /dev/null
+++ b/src/orca/backends/yaml.py
@@ -0,0 +1,93 @@
+# Orca
+#
+# Copyright 2010 Consorcio Fernando de los Rios.
+# Author: Juanje Ojeda Croissier <jojeda emergya es>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Library General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library 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
+# Library General Public License for more details.
+#
+# You should have received a copy of the GNU Library General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
+# Boston MA  02110-1301 USA.
+
+"""YAML backend for Orca settings"""
+
+__id__        = "$Id$"
+__version__   = "$Revision$"
+__date__      = "$Date$"
+__copyright__ = "Copyright (c) 2010 Consorcio Fernando de los Rios."
+__license__   = "LGPL"
+
+from yaml import load, dump
+from xdg.BaseDirectory import xdg_data_home
+
+class Backend():
+
+    def __init__(self):
+        self.general = {}
+        self.pronunciations = {}
+        self.keybindings = {}
+        self.profiles = {}
+        self.settingsFile = os.path.join(xdg_data_home,
+                                         "orca/user-settings.conf")
+
+    def saveDefaultSettings(self, general, pronunciations, keybindings):
+        prefs = {'general': general,
+                 'profiles': { 'default': {} },
+                 'pronunciations': pronunciations,
+                 'keybindings': keybindings}
+        settingsFile = open(self.settingsFile, 'w')
+        dump(prefs, settingsFile)
+        settingsFile.close()
+
+    def saveProfileSettings(self, profile, general,
+                                  pronunciations, keybindings):
+        profilePrefs = {profile: general,
+                        'pronunciations': pronunciations,
+                        'keybindings': keybindings}
+        self._getSettings()
+        prefs = {'general': self.general,
+                 'profiles': profilePrefs,
+                 'pronunciations': pronunciations,
+                 'keybindings': keybindings}
+        settingsFile = open(self.settingsFile, 'w')
+        dump(prefs, settingsFile)
+        settingsFile.close()
+
+    def _getSettings(self):
+        settingsFile = open(self.settingsFile)
+        prefs = load(settingsFile)
+        self.general = prefs['general']
+        self.pronunciations = prefs['pronunciations']
+        self.keybindings = prefs['keybindings']
+
+    def getGeneral(self, profile):
+        self._getSettings()
+        return self.profiles[profile]
+
+    def getPronunciations(self, profile):
+        self._getSettings()
+        return self.profiles[profile]
+
+    def getKeybindings(self, profile):
+        self._getSettings()
+        return self.keybindings[profile]
+
+    def isFirstStart(self):
+        from orca.path import exists
+        if exists(self.settingsFile):
+            return False
+        else:
+            return True
+
+    def availableProfiles(self):
+        return self.profiles.keys()
+
diff --git a/src/orca/settings_manager.py b/src/orca/settings_manager.py
index 125004c..8ff4db6 100644
--- a/src/orca/settings_manager.py
+++ b/src/orca/settings_manager.py
@@ -38,7 +38,7 @@ class SettingsManager():
             cls.__instance = object.__new__(cls, *args, **kwargs)
         return cls.__instance
 
-    def __init__(self, backend='gconf', profile='default'):
+    def __init__(self, backend='yaml', profile='default'):
         """Initialize a SettingsManager Object.
         If backend isn't defined then uses default backend, in this
         case gconf-backend.



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