Commit 9486f36a authored by John Stowers's avatar John Stowers

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
parent 39c8d43f
......@@ -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
description = key.getElementsByTagName("description")[0].childNodes[0].data
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
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:
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]
self[key] = vals
return True
except ValueError:
#not present
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("")
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")
......@@ -75,7 +75,7 @@ class ShellThemeTweak(Tweak):
#check the correct gsettings key is present
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.
......@@ -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(
combo.connect('changed', self._on_combo_changed)
self.widget = build_label_beside_widget(, 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):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment