~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/gconfwrapper.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2010-04-14 14:05:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100414140534-grv0bwv9wv97khir
Tags: 0.2-0ubuntu1
* Mago tests updated for Lucid
  + Fixes arguments handling (LP: #562965)
  + Fixes seahorse tests (LP: #552618)
  + Fixes ubuntu-menu tests (LP: #551492)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GConf wrapper 
 
2
# Modified from Johan Dahlin's code:
 
3
# http://www.daa.com.au/pipermail/pygtk/2002-August/003220.html
 
4
import gconf
 
5
from gconf import VALUE_BOOL, VALUE_INT, VALUE_STRING, VALUE_FLOAT
 
6
from types import StringType, IntType, FloatType, BooleanType
 
7
 
 
8
class GConf:
 
9
    """
 
10
    This class provides a handy interface for manipulating a gconf key.
 
11
 
 
12
    >>> from gconfwrapper import GConf
 
13
    >>> interface=GConf("/desktop/gnome/interface/")
 
14
    >>> interface["accessibility"]
 
15
    False
 
16
    >>> interface["gtk_theme"]="Human"
 
17
 
 
18
    This class can also be used without being instantiated.
 
19
 
 
20
    >>> GConf.get_item('/desktop/gnome/interface/accessibility')
 
21
    False
 
22
    >>> GConf.set_item('/desktop/gnome/interface/accessibility')
 
23
 
 
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.
 
27
 
 
28
    The static methods are for convenience, avoiding instance instantiation when
 
29
    getting or setting only a single gconf key.
 
30
 
 
31
    @group Automatically invoked accessors: *value, *string, *bool, *int,
 
32
        *float, __getitem__, __setitem__, _get_type
 
33
    """
 
34
 
 
35
 
 
36
    class GConfError(Exception):
 
37
        """
 
38
        Exception class for GConf exceptions
 
39
        """
 
40
        pass
 
41
 
 
42
    def __init__ (self, domain):
 
43
        """
 
44
        Constructor for the GConf class
 
45
 
 
46
        @param domain: The GConf domain, for instance: /desktop/gnome/interface/
 
47
        @type domain: string
 
48
        """
 
49
        self._domain = domain
 
50
        self._gconf_client = gconf.client_get_default ()
 
51
 
 
52
    def __getitem__ (self, attr):
 
53
        return self.get_value (attr)
 
54
 
 
55
    def __setitem__ (self, key, val):
 
56
        self.set_value (key, val)
 
57
 
 
58
    def _get_type (self, key):
 
59
        KeyType = type (key)
 
60
        if KeyType == StringType:
 
61
            return 'string'
 
62
        elif KeyType == IntType:
 
63
            return 'int'
 
64
        elif KeyType == FloatType:
 
65
            return 'float'
 
66
        elif KeyType == BooleanType:
 
67
            return 'bool'
 
68
        else:
 
69
            raise self.GConfError, 'unsupported type: %s' % str (KeyType)
 
70
 
 
71
    # Public functions
 
72
 
 
73
    def set_domain (self, domain):
 
74
        """
 
75
        Change the domain of the current GConf instance
 
76
 
 
77
        @param domain: New domain to use
 
78
        @type domain: string
 
79
        """
 
80
        self._domain = domain
 
81
 
 
82
    def get_domain (self):
 
83
        """
 
84
        Get the domain of the current GConf instance
 
85
 
 
86
        @return: Domain of the current GConf instance
 
87
        @rtype: string
 
88
        """
 
89
        return self._domain
 
90
 
 
91
    def get_gconf_client (self):
 
92
        """
 
93
        Access the pygtk GConf client
 
94
 
 
95
        @return: The pygtk GConf client
 
96
        @rtype: pygtk gconf object
 
97
        """
 
98
        return self._gconf_client
 
99
 
 
100
    def get_value (self, key):
 
101
        '''
 
102
        Returns the value of key 'key'
 
103
 
 
104
        This is automatically invoked by the instance __getitem__ and
 
105
        __setitem__ so should not need to be called directly.
 
106
 
 
107
        @param key: Target key to get
 
108
        @type key: string
 
109
        @return: Current value of target key
 
110
        @rtype: bool, int, string, float
 
111
        '''
 
112
        if '/' in key:
 
113
            raise self.GConfError, 'key must not contain /'
 
114
 
 
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 ()
 
125
 
 
126
    def set_value (self, key, value):
 
127
        '''
 
128
        Sets the value of key 'key' to 'value'
 
129
 
 
130
        This is automatically invoked by the instance __getitem__ and
 
131
        __setitem__ so should not need to be called directly.
 
132
 
 
133
        @param key: Target key to set
 
134
        @type key: string
 
135
        @param value: Value to set
 
136
        @type value: bool, int, string, float
 
137
        '''
 
138
        value_type = self._get_type (value)
 
139
 
 
140
        if '/' in key:
 
141
            raise self.GConfError, 'key must not contain /'
 
142
 
 
143
        func = getattr (self._gconf_client, 'set_' + value_type)
 
144
        apply (func, (self._domain + key, value))
 
145
 
 
146
    def get_string (self, key):
 
147
        if '/' in key:
 
148
            raise self.GConfError, 'key must not contain /'
 
149
 
 
150
        return self._gconf_client.get_string (self._domain + key)
 
151
 
 
152
    def set_string (self, key, value):
 
153
        if type (value) != StringType:
 
154
            raise self.GConfError, 'value must be a string'
 
155
        if '/' in key:
 
156
            raise self.GConfError, 'key must not contain /'
 
157
 
 
158
        self._gconf_client.set_string (self._domain + key, value)
 
159
 
 
160
    def get_bool (self, key):
 
161
        if '/' in key:
 
162
            raise self.GConfError, 'key must not contain /'
 
163
 
 
164
        return self._gconf_client.get_bool (self._domain + key)
 
165
 
 
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'
 
170
        if '/' in key:
 
171
            raise self.GConfError, 'key must not contain /'
 
172
 
 
173
        self._gconf_client.set_bool (self._domain + key, value)
 
174
 
 
175
    def get_int (self, key):
 
176
        if '/' in key:
 
177
            raise self.GConfError, 'key must not contain /'
 
178
 
 
179
        return self._gconf_client.get_int (self._domain + key)
 
180
 
 
181
    def set_int (self, key, value):
 
182
        if type (value) != IntType:
 
183
            raise self.GConfError, 'value must be an int'
 
184
        if '/' in key:
 
185
            raise self.GConfError, 'key must not contain /'
 
186
 
 
187
        self._gconf_client.set_int (self._domain + key, value)
 
188
 
 
189
    def get_float (self, key):
 
190
        if '/' in key:
 
191
            raise self.GConfError, 'key must not contain /'
 
192
 
 
193
        return self._gconf_client.get_float (self._domain + key)
 
194
 
 
195
    def set_float (self, key, value):
 
196
        if type (value) != FloatType:
 
197
            raise self.GConfError, 'value must be an float'
 
198
 
 
199
        if '/' in key:
 
200
            raise self.GConfError, 'key must not contain /'
 
201
 
 
202
        self._gconf_client.set_float (self._domain + key, value)
 
203
 
 
204
    # Some even simpler methods for the truly lazy
 
205
    @staticmethod
 
206
    def get_item(key):
 
207
        """
 
208
        Pass this a key and it will return the value
 
209
 
 
210
        >>> GConf.get_item("/desktop/gnome/interface/accessibility")
 
211
        True
 
212
 
 
213
        @type key:  string
 
214
        @param key: The gconf path to the target key
 
215
        @rtype:     string, int, bool, float
 
216
        @return:    The contents of the specified key
 
217
        """
 
218
        dirname="%s/" % key.rpartition('/')[0]
 
219
        keyname=key.rpartition('/')[2]
 
220
        g=GConf(dirname)
 
221
        return g[keyname]
 
222
        
 
223
    @staticmethod
 
224
    def set_item(key, value):
 
225
        """
 
226
        Set key to value provided
 
227
 
 
228
        >>> GConf.set_item("/desktop/gnome/interface/accessibility", True)
 
229
 
 
230
        @type key:      string
 
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
 
234
        """
 
235
        dirname="%s/" % key.rpartition('/')[0]
 
236
        keyname=key.rpartition('/')[2]
 
237
        g=GConf(dirname)
 
238
        g[keyname]=value   
 
239
 
 
240
def test():
 
241
    c=GConf ('/apps/test-gconf/')
 
242
    c['foo']='1'
 
243
    c['bar']=2
 
244
    c['baz']=3.1
 
245
 
 
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)
 
250
 
 
251
if __name__ == '__main__':
 
252
    test()