config.py

Go to the documentation of this file.
00001 # Copyright 2005, 2006  Timo Savola
00002 #
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU Lesser General Public License as published
00005 # by the Free Software Foundation; either version 2.1 of the License, or
00006 # (at your option) any later version.
00007 
00008 '''Utility for loading and saving settings.  (A convenience layer on top of GConf.)'''
00009 
00010 import gobject
00011 import gconf
00012 
00013 class Directory (gobject.GObject):
00014         '''A handle for accessing settings under a particular GConf directory.  When registering a
00015            callback, it will be called immediately with the current value of the associated
00016            variable.  The correct set/get/add method needs to be used depending on the type of the
00017            target variable.'''
00018 
00019         def __init__(self, name):
00020                 '''Initialise a handle to the "/apps/NAME" directory.'''
00021 
00022                 gobject.GObject.__init__(self)
00023 
00024                 self.root = '/apps/%s' % name
00025 
00026                 self.client = gconf.client_get_default()
00027                 self.client.add_dir(self.root, gconf.CLIENT_PRELOAD_ONELEVEL)
00028 
00029                 self.notifications = []
00030 
00031         def _make_path(self, name):
00032                 return '%s/%s' % (self.root, name)
00033 
00034         def directory(self, name):
00035                 '''Create a handle to the NAME subdirectory.'''
00036 
00037                 root = self._make_path(name)
00038                 return Config(root)
00039 
00040         def remove_all(self):
00041                 '''Remove all callbacks registered via this handle.'''
00042 
00043                 for id in self.notifications:
00044                         self.client.notify_remove(id)
00045 
00046                 del self.notifications[:]
00047 
00048         def _add(self, name, callback, validate):
00049                 def call(client, id, entry, data):
00050                         value = validate(entry.value)
00051                         if value is not None:
00052                                 callback(value)
00053 
00054                 key = self._make_path(name)
00055 
00056                 id = self.client.notify_add(key, call)
00057                 self.notifications.append(id)
00058 
00059                 gvalue = self.client.get(key)
00060                 value = validate(gvalue)
00061                 if value is not None:
00062                         callback(value)
00063 
00064         def _get(self, name, validate, default):
00065                 key = self._make_path(name)
00066 
00067                 gvalue = self.client.get(key)
00068                 value = validate(gvalue)
00069 
00070                 if value is not None:
00071                         return value
00072                 else:
00073                         return default
00074 
00075         def add_string(self, name, callback):
00076                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00077 
00078                 self._add(name, callback, _validate_string)
00079 
00080         def add_strings(self, name, callback):
00081                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00082 
00083                 self._add(name, callback, _validate_string_list)
00084 
00085         def add_int(self, name, callback):
00086                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00087 
00088                 self._add(name, callback, _validate_int)
00089 
00090         def add_ints(self, name, callback):
00091                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00092 
00093                 self._add(name, callback, _validate_int_list)
00094 
00095         def add_bool(self, name, callback):
00096                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00097 
00098                 self._add(name, callback, _validate_bool)
00099 
00100         def add_bools(self, name, callback):
00101                 '''Register CALLBACK to be called when the value of the NAME variable changes.'''
00102 
00103                 self._add(name, callback, _validate_bool_list)
00104 
00105         def get_string(self, name, default = None):
00106                 '''Get the current value of the NAME variable.'''
00107 
00108                 return self._get(name, _validate_string, default)
00109 
00110         def get_strings(self, name, default = None):
00111                 '''Get the current value of the NAME variable.'''
00112 
00113                 return self._get(name, _validate_string_list, default)
00114 
00115         def get_int(self, name, default = None):
00116                 '''Get the current value of the NAME variable.'''
00117 
00118                 return self._get(name, _validate_int, default)
00119 
00120         def get_ints(self, name, default = None):
00121                 '''Get the current value of the NAME variable.'''
00122 
00123                 return self._get(name, _validate_int_list, default)
00124 
00125         def get_bool(self, name, default = None):
00126                 '''Get the current value of the NAME variable.'''
00127 
00128                 return self._get(name, _validate_bool, default)
00129 
00130         def get_bools(self, name, default = None):
00131                 '''Get the current value of the NAME variable.'''
00132 
00133                 return self._get(name, _validate_bool_list, default)
00134 
00135         def set_string(self, name, value):
00136                 '''Change the value of the NAME variable.'''
00137 
00138                 key = self._make_path(name)
00139                 self.client.set_string(key, value)
00140 
00141         def set_strings(self, name, values):
00142                 '''Change the value of the NAME variable.'''
00143 
00144                 key = self._make_path(name)
00145                 self.client.set_list(key, gconf.VALUE_STRING, values)
00146 
00147         def set_int(self, name, value):
00148                 '''Change the value of the NAME variable.'''
00149 
00150                 key = self._make_path(name)
00151                 self.client.set_int(key, value)
00152 
00153         def set_ints(self, name, values):
00154                 '''Change the value of the NAME variable.'''
00155 
00156                 key = self._make_path(name)
00157                 self.client.set_list(key, gconf.VALUE_INT, values)
00158 
00159         def set_bool(self, name, value):
00160                 '''Change the value of the NAME variable.'''
00161 
00162                 key = self._make_path(name)
00163                 self.client.set_bool(key, value)
00164 
00165         def set_bools(self, name, values):
00166                 '''Change the value of the NAME variable.'''
00167 
00168                 key = self._make_path(name)
00169                 self.client.set_list(key, gconf.VALUE_BOOL, values)
00170 
00171         def unset(self, name):
00172                 '''Change the value of the NAME variable to its default.'''
00173 
00174                 key = self._make_path(name)
00175                 self.client.unset(key)
00176 
00177 gobject.type_register(Directory)
00178 gobject.signal_new('changed', Directory, gobject.SIGNAL_DETAILED, gobject.TYPE_NONE, [gobject.GObject])
00179 
00180 def _validate_string(value):
00181         if value and value.type == gconf.VALUE_STRING:
00182                 return value.get_string()
00183 
00184 def _validate_string_list(value):
00185         if value and value.type == gconf.VALUE_LIST and value.get_list_type() == gconf.VALUE_STRING:
00186                 return [item.get_string() for item in value.get_list()]
00187 
00188 def _validate_int(value):
00189         if value and value.type == gconf.VALUE_INT:
00190                 return value.get_int()
00191 
00192 def _validate_int_list(value):
00193         if value and value.type == gconf.VALUE_LIST and value.get_list_type() == gconf.VALUE_INT:
00194                 return [item.get_int() for item in value.get_list()]
00195 
00196 def _validate_bool(value):
00197         if value is not None and value.type == gconf.VALUE_BOOL:
00198                 return value.get_bool()
00199 
00200 def _validate_bool_list(value):
00201         if value and value.type == gconf.VALUE_LIST and value.get_list_type() == gconf.VALUE_BOOL:
00202                 return [item.get_bool() for item in value.get_list()]

Generated on Thu Jan 18 09:47:40 2007 for Encode by  doxygen 1.4.7