[gnome-tweak-tool] Dont crash on missing schemas



commit 81ee17f1b0352347b1bf5f2bdad25e0fc2c7c3eb
Author: John Stowers <john stowers gmail com>
Date:   Wed Jan 18 21:10:45 2012 +1300

    Dont crash on missing schemas

 gtweak/gsettings.py  |   23 ++++++++++++++++++++++-
 gtweak/tweakmodel.py |    3 +++
 gtweak/widgets.py    |   32 ++++++++++++++++++++------------
 3 files changed, 45 insertions(+), 13 deletions(-)
---
diff --git a/gtweak/gsettings.py b/gtweak/gsettings.py
index 9ab8749..e334637 100644
--- a/gtweak/gsettings.py
+++ b/gtweak/gsettings.py
@@ -23,6 +23,12 @@ import gtweak
 
 from gi.repository import Gio, GLib
 
+_SCHEMA_CACHE = {}
+_GSETTINGS_SCHEMAS = set(Gio.Settings.list_schemas())
+
+class GSettingsMissingError(Exception):
+    pass
+
 class _GSettingsSchema:
     def __init__(self, schema_name, schema_dir=None, schema_filename=None, **options):
         if not schema_dir:
@@ -64,10 +70,25 @@ class _GSettingsSchema:
     def __repr__(self):
         return "<gtweak.gsettings._GSettingsSchema: %s>" % self._schema_name
 
-_SCHEMA_CACHE = {}
+class GSettingsFakeSetting:
+    def __init__(self):
+        pass
+
+    def get_range(self, *args, **kwargs):
+        return False, []
+
+    def get_string(self, *args, **kwargs):
+        return ""
+
+    def __getattr__(self, name):
+        def noop(*args, **kwargs):
+            pass
+        return noop
 
 class GSettingsSetting(Gio.Settings):
     def __init__(self, schema_name, **options):
+        if schema_name not in _GSETTINGS_SCHEMAS:
+            raise GSettingsMissingError(schema_name)
         Gio.Settings.__init__(self, schema_name)
         if schema_name not in _SCHEMA_CACHE:
             _SCHEMA_CACHE[schema_name] = _GSettingsSchema(schema_name, **options)
diff --git a/gtweak/tweakmodel.py b/gtweak/tweakmodel.py
index d67ce5a..558a329 100644
--- a/gtweak/tweakmodel.py
+++ b/gtweak/tweakmodel.py
@@ -35,6 +35,7 @@ class Tweak:
         self.name = name
         self.description = description
         self.group_name = options.get("group_name",_("Miscellaneous"))
+        self.loaded = True
 
         self._search_cache = None
 
@@ -148,6 +149,8 @@ class TweakModel(Gtk.ListStore):
         self._tweak_group_names[tweakgroup.name] = tweakgroup
 
     def add_tweak_auto_to_group(self, tweak):
+        if not tweak.loaded:
+            return
         name = tweak.group_name
         try:
             group = self._tweak_group_names[name]
diff --git a/gtweak/widgets.py b/gtweak/widgets.py
index 4a7646f..c915a17 100644
--- a/gtweak/widgets.py
+++ b/gtweak/widgets.py
@@ -15,10 +15,12 @@
 # You should have received a copy of the GNU General Public License
 # along with gnome-tweak-tool.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
+
 from gi.repository import Gtk, Gdk, Gio, Pango
 
 from gtweak.tweakmodel import Tweak
-from gtweak.gsettings import GSettingsSetting
+from gtweak.gsettings import GSettingsSetting, GSettingsFakeSetting, GSettingsMissingError
 from gtweak.gconf import GConfSetting
 
 def build_label_beside_widget(txt, *widget, **kwargs):
@@ -97,11 +99,17 @@ class _GSettingsTweak(Tweak):
     def __init__(self, schema_name, key_name, **options):
         self.schema_name = schema_name
         self.key_name = key_name
-        self.settings = GSettingsSetting(schema_name, **options)
-        Tweak.__init__(self,
-            options.get("summary",self.settings.schema_get_summary(key_name)),
-            options.get("description",self.settings.schema_get_description(key_name)),
-            **options)
+        try:
+            self.settings = GSettingsSetting(schema_name, **options)
+            Tweak.__init__(self,
+                options.get("summary",self.settings.schema_get_summary(key_name)),
+                options.get("description",self.settings.schema_get_description(key_name)),
+                **options)
+        except GSettingsMissingError, e:
+            self.settings = GSettingsFakeSetting()
+            Tweak.__init__(self,"","")
+            self.loaded = False
+            logging.info("Missing gsettings %s (key %s)" % (e.message, key_name))
 
 class GSettingsSwitchTweak(_GSettingsTweak):
     def __init__(self, schema_name, key_name, **options):
@@ -109,7 +117,7 @@ class GSettingsSwitchTweak(_GSettingsTweak):
 
         w = Gtk.Switch()
         self.settings.bind(key_name, w, "active", Gio.SettingsBindFlags.DEFAULT)
-        self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
+        self.widget = build_label_beside_widget(self.name, w)
         # never change the size of a switch
         self.widget_for_size_group = None
 
@@ -119,7 +127,7 @@ class GSettingsFontButtonTweak(_GSettingsTweak):
 
         w = Gtk.FontButton()
         self.settings.bind(key_name, w, "font-name", Gio.SettingsBindFlags.DEFAULT)
-        self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
+        self.widget = build_label_beside_widget(self.name, w)
         self.widget_for_size_group = w
 
 class GSettingsRangeTweak(_GSettingsTweak):
@@ -131,7 +139,7 @@ class GSettingsRangeTweak(_GSettingsTweak):
 
         w = Gtk.HScale.new_with_range(_min, _max, options.get('adjustment_step', 1))
         self.settings.bind(key_name, w.get_adjustment(), "value", Gio.SettingsBindFlags.DEFAULT)
-        self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
+        self.widget = build_label_beside_widget(self.name, w)
         self.widget_for_size_group = w
 
 class GSettingsSpinButtonTweak(_GSettingsTweak):
@@ -146,7 +154,7 @@ class GSettingsSpinButtonTweak(_GSettingsTweak):
         w.set_adjustment(adjustment)
         w.set_digits(options.get('digits', 0))
         self.settings.bind(key_name, adjustment, "value", Gio.SettingsBindFlags.DEFAULT)
-        self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
+        self.widget = build_label_beside_widget(self.name, w)
         self.widget_for_size_group = w
 
 class GSettingsComboEnumTweak(_GSettingsTweak):
@@ -161,7 +169,7 @@ class GSettingsComboEnumTweak(_GSettingsTweak):
         w.connect('changed', self._on_combo_changed)
         self.combo = w
 
-        self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
+        self.widget = build_label_beside_widget(self.name, w)
         self.widget_for_size_group = w
 
 
@@ -246,7 +254,7 @@ class GConfFontButtonTweak(_GConfTweak):
         w = Gtk.FontButton()
         w.props.font_name = self.gconf.get_value()
         w.connect("notify::font-name", self._on_fontbutton_changed)
-        self.widget = build_label_beside_widget(self.gconf.schema_get_summary(), w)
+        self.widget = build_label_beside_widget(self.name, w)
         self.widget_for_size_group = w
 
     def _on_fontbutton_changed(self, btn, param):



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