2
# Modified from Johan Dahlin's code:
3
# http://www.daa.com.au/pipermail/pygtk/2002-August/003220.html
5
from gconf import VALUE_BOOL, VALUE_INT, VALUE_STRING, VALUE_FLOAT
6
from types import StringType, IntType, FloatType, BooleanType
10
This class provides a handy interface for manipulating a gconf key.
12
>>> from gconfwrapper import GConf
13
>>> interface=GConf("/desktop/gnome/interface/")
14
>>> interface["accessibility"]
16
>>> interface["gtk_theme"]="Human"
18
This class can also be used without being instantiated.
20
>>> GConf.get_item('/desktop/gnome/interface/accessibility')
22
>>> GConf.set_item('/desktop/gnome/interface/accessibility')
24
The instance methods support the __getitem__ and __setitem__ special methods
25
automatically invoke get_item and set_item to handle properties. Generally,
26
instance methods should not be called directly.
28
The static methods are for convenience, avoiding instance instantiation when
29
getting or setting only a single gconf key.
31
@group Automatically invoked accessors: *value, *string, *bool, *int,
32
*float, __getitem__, __setitem__, _get_type
36
class GConfError(Exception):
38
Exception class for GConf exceptions
42
def __init__ (self, domain):
44
Constructor for the GConf class
46
@param domain: The GConf domain, for instance: /desktop/gnome/interface/
50
self._gconf_client = gconf.client_get_default ()
52
def __getitem__ (self, attr):
53
return self.get_value (attr)
55
def __setitem__ (self, key, val):
56
self.set_value (key, val)
58
def _get_type (self, key):
60
if KeyType == StringType:
62
elif KeyType == IntType:
64
elif KeyType == FloatType:
66
elif KeyType == BooleanType:
69
raise self.GConfError, 'unsupported type: %s' % str (KeyType)
73
def set_domain (self, domain):
75
Change the domain of the current GConf instance
77
@param domain: New domain to use
82
def get_domain (self):
84
Get the domain of the current GConf instance
86
@return: Domain of the current GConf instance
91
def get_gconf_client (self):
93
Access the pygtk GConf client
95
@return: The pygtk GConf client
96
@rtype: pygtk gconf object
98
return self._gconf_client
100
def get_value (self, key):
102
Returns the value of key 'key'
104
This is automatically invoked by the instance __getitem__ and
105
__setitem__ so should not need to be called directly.
107
@param key: Target key to get
109
@return: Current value of target key
110
@rtype: bool, int, string, float
113
raise self.GConfError, 'key must not contain /'
115
value = self._gconf_client.get (self._domain + key)
116
ValueType = value.type
117
if ValueType == VALUE_BOOL:
118
return value.get_bool ()
119
elif ValueType == VALUE_INT:
120
return value.get_int ()
121
elif ValueType == VALUE_STRING:
122
return value.get_string ()
123
elif ValueType == VALUE_FLOAT:
124
return value.get_float ()
126
def set_value (self, key, value):
128
Sets the value of key 'key' to 'value'
130
This is automatically invoked by the instance __getitem__ and
131
__setitem__ so should not need to be called directly.
133
@param key: Target key to set
135
@param value: Value to set
136
@type value: bool, int, string, float
138
value_type = self._get_type (value)
141
raise self.GConfError, 'key must not contain /'
143
func = getattr (self._gconf_client, 'set_' + value_type)
144
apply (func, (self._domain + key, value))
146
def get_string (self, key):
148
raise self.GConfError, 'key must not contain /'
150
return self._gconf_client.get_string (self._domain + key)
152
def set_string (self, key, value):
153
if type (value) != StringType:
154
raise self.GConfError, 'value must be a string'
156
raise self.GConfError, 'key must not contain /'
158
self._gconf_client.set_string (self._domain + key, value)
160
def get_bool (self, key):
162
raise self.GConfError, 'key must not contain /'
164
return self._gconf_client.get_bool (self._domain + key)
166
def set_bool (self, key, value):
167
if type (value) != IntType and \
168
(key != 0 or key != 1):
169
raise self.GConfError, 'value must be a boolean'
171
raise self.GConfError, 'key must not contain /'
173
self._gconf_client.set_bool (self._domain + key, value)
175
def get_int (self, key):
177
raise self.GConfError, 'key must not contain /'
179
return self._gconf_client.get_int (self._domain + key)
181
def set_int (self, key, value):
182
if type (value) != IntType:
183
raise self.GConfError, 'value must be an int'
185
raise self.GConfError, 'key must not contain /'
187
self._gconf_client.set_int (self._domain + key, value)
189
def get_float (self, key):
191
raise self.GConfError, 'key must not contain /'
193
return self._gconf_client.get_float (self._domain + key)
195
def set_float (self, key, value):
196
if type (value) != FloatType:
197
raise self.GConfError, 'value must be an float'
200
raise self.GConfError, 'key must not contain /'
202
self._gconf_client.set_float (self._domain + key, value)
204
# Some even simpler methods for the truly lazy
208
Pass this a key and it will return the value
210
>>> GConf.get_item("/desktop/gnome/interface/accessibility")
214
@param key: The gconf path to the target key
215
@rtype: string, int, bool, float
216
@return: The contents of the specified key
218
dirname="%s/" % key.rpartition('/')[0]
219
keyname=key.rpartition('/')[2]
224
def set_item(key, value):
226
Set key to value provided
228
>>> GConf.set_item("/desktop/gnome/interface/accessibility", True)
231
@param key: The gconf path to the target key
232
@type value: string, int, bool, float
233
@param value: The desired new value of the specified key
235
dirname="%s/" % key.rpartition('/')[0]
236
keyname=key.rpartition('/')[2]
241
c=GConf ('/apps/test-gconf/')
246
print c['foo'], c['bar'], c['baz']
247
print "Accessibility: %s" % GConf.get_item('/desktop/gnome/interface/accessibility')
248
GConf.set_item('/apps/test-gconf/foobar', True)
249
GConf.set_item('/apps/test-gconf/barfoo', False)
251
if __name__ == '__main__':