1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import gconf
22
23
25 """
26 A dict-like interface to GConf
27 """
28
30 """
31 Arguments:
32
33 - `dir`: The base dir for this GConfDict.
34 Any key not starting with '/' is realtiv to this.
35 """
36 if dir is None:
37 dir = ''
38
39 self.dir = dir.rstrip('/')
40 self.gclient = gconf.client_get_default()
41 if self.dir != '':
42 self.gclient.add_dir(self.dir.rstrip('/'),
43 gconf.CLIENT_PRELOAD_NONE)
44
45
47 """
48 Expands relativ keys if needed
49 """
50 if len(key) > 0 and key[0] != '/':
51 key='%s/%s'%(self.dir, key)
52 return key
53
55 """
56 Expands relativ dirs if needed
57 """
58 if dir == '':
59 return '/'
60 else:
61 return dir.rstrip('/')
62
63
69
70 - def set(self, key, value):
75
76
83
85 """
86 Suggests to gconfd that you've just finished a block of
87 changes, and it would be an optimal time to sync to permanent
88 storage. This is only a suggestion; and gconfd will eventually
89 sync even if you don't call sync(). This
90 function is just a "hint" provided to gconfd to maximize
91 efficiency and minimize data loss.
92 """
93 self.gclient.suggest_sync()
94
95
97 """
98 Request notification of changes to key
99
100 - `func`: function to call when changes occur.
101 It's called as func(key, value, gconfdict, id, args)
102 - `args`: user data to pass to func
103
104 The function returns a connection ID you can use to call
105 remove_listener()
106 """
107 key = self.normalize_key(key)
108
109 def foo(client, id, entry, *args):
110 func(entry.key, self.to_python(entry.value), self, id, *args)
111
112 return self.gclient.notify_add(key, foo, args)
113
115 """
116 Remove a notification using the ID returned from add_listener()
117
118 - `id`: connection ID returned from add_listener()
119 """
120 self.gclient.notify_remove(id)
121
123 """
124 True if `key` is defined and not a dir
125 """
126 return self.normalize_key(key) in self.keys()
127
128
130 """
131 Convert a gconf value to a python value
132 Arguments:
133 - `gcvalue`: a gconf value
134 """
135 if gcvalue is None:
136 return None
137 else:
138 _type=gcvalue.type
139 if _type is gconf.VALUE_LIST:
140 return tuple(self.to_python(v) for v in gcvalue.get_list())
141 elif _type is gconf.VALUE_PAIR:
142 return self.to_python(gcvalue.get_car()), self.to_python(gcvalue.get_cdr())
143 else:
144 return getattr(gcvalue, 'get_%s'%_type.value_nick)()
145
146
148 """
149 Convert a Python value to a gconf value
150 """
151 if isinstance(value, basestring):
152 val=gconf.Value(gconf.VALUE_STRING)
153 val.set_string(value)
154 elif isinstance(value, int) or isinstance(value, long):
155 val=gconf.Value(gconf.VALUE_INT)
156 val.set_int(value)
157 elif isinstance(value, float):
158 val=gconf.Value(gconf.VALUE_FLOAT)
159 val.set_float(value)
160 elif isinstance(value, bool):
161 val=gconf.Value(gconf.VALUE_BOOL)
162 val.set_bool(value)
163 elif isinstance(value, tuple) and len(value)==2:
164 val=gconf.Value(gconf.VALUE_PAIR)
165 val.set_car(self.from_python(value[0]))
166 val.set_cdr(self.from_python(value[1]))
167 elif isinstance(value, list) or isinstance(value, tuple):
168 val=gconf.Value(gconf.VALUE_LIST)
169 l=[self.from_python(v) for v in value]
170 if len(l) > 0:
171 val.set_list_type(l[0].type)
172 val.set_list(l)
173 else: raise ValueError()
174 return val
175
177 """
178 return an iterator over the keys
179 """
180 for v in self.gclient.all_entries(self.normalize_dir(self.dir)):
181 yield v.key
182
184 """
185 return a list of all keys
186 """
187 return tuple(self.iterkeys())
188
189
191 """
192 return an iterator over the values
193 """
194 for v in self.gclient.all_entries(self.dir):
195 yield self.to_python(v.value())
196
198 """
199 return a list of all values
200 """
201 return tuple(self.itervalues())
202
204 """
205 return an iterator over (key, value) pairs
206 """
207 for v in self.gclient.all_entries(self.dir):
208 yield v.key, self.to_python(v.value)
209
210
212 """
213 return an iterator over the subdirs
214 """
215 for d in self.gclient.all_dirs(self.dir):
216 yield d
217
219 """
220 return a list of all subdirs
221 """
222 return tuple(self.iterdirs())
223
224
226 """
227 return a list of all sudbirs and keys
228 """
229 return self.dirs()+self.keys()
230
231
233 """
234 x.__getitem__(y) <==> x[y]
235 """
236 return self.get(key)
237
239 """
240 x.__setitem__(i, y) <==> x[i]=y
241 """
242 self.set(key, value)
243
245 """
246 x.__iter__() <==> iter(x)
247 """
248 return self.iterkeys()
249
251 """
252 x.__delitem__(y) <==> del x[y]
253 """
254 self.delete(key)
255
257 """
258 x.__len__() <==> len(x)
259 """
260 return len(self.keys())
261
263 """
264 x.__contains__(y) <==> y in x
265 """
266 return self.has_key(item)
267