Erik Grinaker is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againRevelation / src / lib / config.py.in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | #
# Revelation 0.4.0 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module for configuration handling
#
#
# Copyright (c) 2003-2005 Erik Grinaker
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import util
import gconf, os
APPNAME = "Revelation"
VERSION = "@VERSION@"
DATAVERSION = 1
RELNAME = "Better late than never"
URL = "http://oss.codepoet.no/revelation/"
AUTHOR = "Erik Grinaker <erikg@codepoet.no>"
COPYRIGHT = "Copyright \302\251 2003-2005 Erik Grinaker"
DIR_GCONFSCHEMAS= "@pkgschemadir@"
DIR_ICONS = "@datadir@/icons"
DIR_UI = "@pkgdatadir@/ui"
FILE_GCONFTOOL = "@GCONFTOOL@"
class ConfigError(Exception):
"Configuration error exception"
pass
class Config(object):
"Configuration class"
def __init__(self, basedir = "/apps/revelation"):
self.basedir = basedir
self.client = gconf.client_get_default()
self.callbacks = {}
# check config, install schema if needed
if self.check() == False:
if self.__install_schema(DIR_GCONFSCHEMAS + "/revelation.schemas") == False or self.check() == False:
raise ConfigError
self.client.add_dir(self.basedir, gconf.CLIENT_PRELOAD_NONE)
def __cb_notify(self, client, id, entry, data):
"Callback for handling gconf notifications"
value = entry.get_value()
if value.type == gconf.VALUE_STRING:
v = value.get_string()
elif value.type == gconf.VALUE_BOOL:
v = value.get_bool()
elif value.type == gconf.VALUE_INT:
v = value.get_int()
# look up and call the callback
callback, userdata = self.callbacks[id]
callback(entry.get_key(), v, userdata)
def __install_schema(self, file):
"Installs a gconf schema"
if os.access(file, os.F_OK) == False:
return False
util.execute(FILE_GCONFTOOL + " --install-schema-file=" + file)
def __resolve_keypath(self, key):
"Resolves a key path"
return key[0] == "/" and key or self.basedir + "/" + key
def check(self):
"Checks if the config is valid"
try:
self.get("file/autoload_file")
self.get("file/autosave")
self.get("view/window-position-y")
self.get("view/window-width")
self.get("launcher/website")
self.get("view/searchbar")
except ConfigError:
return False
else:
return True
def forget(self, id):
"Forgets a monitored key"
if not self.callbacks.has_key(id):
raise ConfigError
self.client.notify_remove(id)
del self.callbacks[id]
def get(self, key):
"Looks up a config value"
value = self.client.get(self.__resolve_keypath(key))
if value is None:
raise ConfigError
elif value.type == gconf.VALUE_STRING:
return str(value.get_string())
elif value.type == gconf.VALUE_INT:
return value.get_int()
elif value.type == gconf.VALUE_BOOL:
return value.get_bool()
def monitor(self, key, callback, userdata = None):
"Monitor a config key for changes"
key = self.__resolve_keypath(key)
id = self.client.notify_add(key, self.__cb_notify)
self.callbacks[id] = ( callback, userdata )
# call the callback to set an initial state
callback(key, self.get(key), userdata)
return id
def set(self, key, value):
"Sets a configuration value"
node = self.client.get(self.__resolve_keypath(key))
if node is None:
raise ConfigError
elif node.type == gconf.VALUE_STRING:
node.set_string(value)
elif node.type == gconf.VALUE_BOOL:
node.set_bool(value)
elif node.type == gconf.VALUE_INT:
node.set_int(int(value))
self.client.set(self.__resolve_keypath(key), node)
|