Package easygconf :: Module gconfdict
[hide private]
[frames] | no frames]

Source Code for Module easygconf.gconfdict

  1  #-*- coding: utf-8-*- 
  2   
  3  ##  easygconf - a dict-like API for GConf 
  4  ##               <http://www.florian-diesch.de/software/easygconf/> 
  5  ##  Copyright (C) 2008, 2009 Florian Diesch <devel@florian-diesch.de> 
  6  ##   
  7  ##  This program is free software; you can redistribute it and/or modify 
  8  ##  it under the terms of the GNU General Public License as published by 
  9  ##  the Free Software Foundation; either version 2 of the License, or 
 10  ##  (at your option) any later version. 
 11  ##   
 12  ##  This program is distributed in the hope that it will be useful, 
 13  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  ##  GNU General Public License for more details. 
 16  ##   
 17  ##  You should have received a copy of the GNU General Public License along 
 18  ##  with this program; if not, write to the Free Software Foundation, Inc., 
 19  ##  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 20   
 21  import gconf 
 22   
 23   
24 -class GConfDict(object):
25 """ 26 A dict-like interface to GConf 27 """ 28
29 - def __init__(self, dir=None):
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
46 - def normalize_key(self, key):
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
54 - def normalize_dir(self, dir):
55 """ 56 Expands relativ dirs if needed 57 """ 58 if dir == '': 59 return '/' 60 else: 61 return dir.rstrip('/')
62 63
64 - def get(self, key):
65 """ 66 Returns the value for `key` 67 """ 68 return self.to_python(self.gclient.get(self.normalize_key(key)))
69
70 - def set(self, key, value):
71 """ 72 Set value for `key` to `value` 73 """ 74 self.gclient.set(self.normalize_key(key), self.from_python(value))
75 76
77 - def unset(self, key):
78 """ 79 Unset `key` 80 """ 81 key = self.normalize_key(key) 82 self.gclient.unset(key)
83
84 - def sync(self):
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
96 - def add_listener(self, key, func, *args):
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
114 - def remove_listener(self, id):
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
122 - def has_key(self, key):
123 """ 124 True if `key` is defined and not a dir 125 """ 126 return self.normalize_key(key) in self.keys()
127 128
129 - def to_python(self, gcvalue):
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
147 - def from_python(self, value):
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
176 - def iterkeys(self):
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
183 - def keys(self):
184 """ 185 return a list of all keys 186 """ 187 return tuple(self.iterkeys())
188 189
190 - def itervalues(self):
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
197 - def values(self):
198 """ 199 return a list of all values 200 """ 201 return tuple(self.itervalues())
202
203 - def iteritems(self):
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
211 - def iterdirs(self):
212 """ 213 return an iterator over the subdirs 214 """ 215 for d in self.gclient.all_dirs(self.dir): 216 yield d
217
218 - def dirs(self):
219 """ 220 return a list of all subdirs 221 """ 222 return tuple(self.iterdirs())
223 224
225 - def children(self):
226 """ 227 return a list of all sudbirs and keys 228 """ 229 return self.dirs()+self.keys()
230 231
232 - def __getitem__(self, key):
233 """ 234 x.__getitem__(y) <==> x[y] 235 """ 236 return self.get(key)
237
238 - def __setitem__(self, key, value):
239 """ 240 x.__setitem__(i, y) <==> x[i]=y 241 """ 242 self.set(key, value)
243
244 - def __iter__(self):
245 """ 246 x.__iter__() <==> iter(x) 247 """ 248 return self.iterkeys()
249
250 - def __delitem__(self, key):
251 """ 252 x.__delitem__(y) <==> del x[y] 253 """ 254 self.delete(key)
255
256 - def __len__(self):
257 """ 258 x.__len__() <==> len(x) 259 """ 260 return len(self.keys())
261
262 - def __contains__(self, item):
263 """ 264 x.__contains__(y) <==> y in x 265 """ 266 return self.has_key(item)
267