[gnome-tweak-tool] Improve robustness of gsettings helper



commit 9486f36a73dd8e3f40624757a2474f90b6466824
Author: John Stowers <john stowers gmail com>
Date:   Wed May 11 08:33:57 2011 +1200

    Improve robustness of gsettings helper
    
    	* Description is optional in the schema, continue parsing
    	  if it does not exist
    	* Add utilities for manipulating gsettings lists
    	* Remove get/set_value in the gsettings wrapper and use
    	  the Gio.Settings functions directly

 gtweak/gsettings.py          |   54 ++++++++++++++++++++++++++++++++++++-----
 gtweak/tweaks/tweak_shell.py |    4 +-
 gtweak/widgets.py            |    6 ++--
 3 files changed, 52 insertions(+), 12 deletions(-)
---
diff --git a/gtweak/gsettings.py b/gtweak/gsettings.py
index 6f20ec4..b570162 100644
--- a/gtweak/gsettings.py
+++ b/gtweak/gsettings.py
@@ -41,11 +41,18 @@ class _GSettingsSchema:
             for schema in dom.getElementsByTagName("schema"):
                 if schema_name == schema.getAttribute("id"):
                     for key in schema.getElementsByTagName("key"):
+                        #summary is compulsory, description is optional
+                        summary = key.getElementsByTagName("summary")[0].childNodes[0].data
+                        try:
+                            description = key.getElementsByTagName("description")[0].childNodes[0].data
+                        except:
+                            description = ""
                         self._schema[key.getAttribute("name")] = {
-                                "summary"       :   key.getElementsByTagName("summary")[0].childNodes[0].data,
-                                "description"   :   key.getElementsByTagName("description")[0].childNodes[0].data}
+                                "summary"       :   summary,
+                                "description"   :   description
+                        }
         except:
-            logging.critical("Error parsing schema", exc_info=True)
+            logging.critical("Error parsing schema %s (%s)" % (schema_name, schema_path), exc_info=True)
 
     def __repr__(self):
         return "<gtweak.gsettings._GSettingsSchema: %s>" % self._schema_name
@@ -61,6 +68,10 @@ class GSettingsSetting(Gio.Settings):
 
         self._schema = _SCHEMA_CACHE[schema_name]
 
+    def _setting_check_is_list(self, key):
+        variant = Gio.Settings.get_value(self, key)
+        return variant.get_type_string() == "as"
+
     def schema_get_summary(self, key):
         return self._schema._schema[key]["summary"]
         
@@ -70,13 +81,42 @@ class GSettingsSetting(Gio.Settings):
     def schema_get_all(self, key):
         return self._schema._schema[key]
 
-    def get_value(self, key):
-        return Gio.Settings.get_value(self,key).unpack()
+    def setting_add_to_list(self, key, value):
+        """ helper function, ensures value is present in the GSettingsList at key """
+        assert self._setting_check_is_list(key)
+
+        vals = self[key]
+        if value not in vals:
+            vals.append(value)
+            self[key] = vals
+            return True
 
-    def set_value(self, key, value):
-        Gio.Settings.set_value(self, key, GLib.Variant('s', value))
+    def setting_remove_from_list(self, key, value):
+        """ helper function, removes value in the GSettingsList at key (if present)"""
+        assert self._setting_check_is_list(key)
+
+        vals = self[key]
+        try:
+            vals.remove(value)
+            self[key] = vals
+            return True
+        except ValueError:
+            #not present
+            pass
+
+    def setting_is_in_list(self, key, value):
+        assert self._setting_check_is_list(key)
+        return value in self[key]
 
 if __name__ == "__main__":
+    gtweak.GSETTINGS_SCHEMA_DIR = "/usr/share/glib-2.0/schemas/"
+
     key = "draw-background"
     s = GSettingsSetting("org.gnome.desktop.background")
     print s.schema_get_summary(key), s.schema_get_description(key)
+
+    key = "disabled-extensions"
+    s = GSettingsSetting("org.gnome.shell")
+    assert s.setting_add_to_list(key, "foo")
+    assert s.setting_remove_from_list(key, "foo")
+    assert not s.setting_remove_from_list(key, "foo")
diff --git a/gtweak/tweaks/tweak_shell.py b/gtweak/tweaks/tweak_shell.py
index 1cea4d8..dd211db 100644
--- a/gtweak/tweaks/tweak_shell.py
+++ b/gtweak/tweaks/tweak_shell.py
@@ -75,7 +75,7 @@ class ShellThemeTweak(Tweak):
                 #check the correct gsettings key is present
                 try:
                     self._settings = GSettingsSetting(ShellThemeTweak.THEME_GSETTINGS_SCHEMA)
-                    name = self._settings.get_value(ShellThemeTweak.THEME_GSETTINGS_NAME)
+                    name = self._settings.get_string(ShellThemeTweak.THEME_GSETTINGS_NAME)
 
                     #assume the usertheme version is that version of the shell which
                     #it most supports (this is a poor assumption)
@@ -207,7 +207,7 @@ class ShellThemeTweak(Tweak):
 
     def _on_combo_changed(self, combo):
         val = combo.get_model().get_value(combo.get_active_iter(), 0)
-        self._settings.set_value(ShellThemeTweak.THEME_GSETTINGS_NAME, val)
+        self._settings.set_string(ShellThemeTweak.THEME_GSETTINGS_NAME, val)
 
         #reloading the theme is not really necessary, the user-theme should pick
         #pick up the change.
diff --git a/gtweak/widgets.py b/gtweak/widgets.py
index 4de13f0..ae6b442 100644
--- a/gtweak/widgets.py
+++ b/gtweak/widgets.py
@@ -131,7 +131,7 @@ class GSettingsComboEnumTweak(_GSettingsTweak):
     def _on_combo_changed(self, combo):
         val = self.combo.get_model().get_value(self.combo.get_active_iter(), 0)
         if self._values_are_different():
-            self.settings.set_value(self.key_name, val)
+            self.settings.set_string(self.key_name, val)
 
 class GSettingsComboTweak(_GSettingsTweak):
     def __init__(self, schema_name, key_name, key_options, **options):
@@ -144,7 +144,7 @@ class GSettingsComboTweak(_GSettingsTweak):
             assert len(key_options[0]) == 2
 
         combo = build_combo_box_text(
-                    self.settings.get_value(self.key_name),
+                    self.settings.get_string(self.key_name),
                     *key_options)
         combo.connect('changed', self._on_combo_changed)
         self.widget = build_label_beside_widget(self.name, combo)
@@ -154,7 +154,7 @@ class GSettingsComboTweak(_GSettingsTweak):
         _iter = combo.get_active_iter()
         if _iter:
             value = combo.get_model().get_value(_iter, 0)
-            self.settings.set_value(self.key_name, value)
+            self.settings.set_string(self.key_name, value)
 
 class _GConfTweak(Tweak):
     def __init__(self, key_name, key_type, **options):



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