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 again

erikg / Revelation

Revelation is a password manager for the GNOME desktop, released under the GNU GPL license. It stores all your accounts and passwords in a single, secure place, and gives you access to it through a user-friendly graphical interface.

Clone this repository (size: 2.1 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/revelation
hg clone ssh://hg@bitbucket.org/erikg/revelation

Revelation / src / lib / config.py.in

#
# Revelation 0.4.6 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# module for configuration handling
#
#
# Copyright (c) 2003-2006 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         = "How much for the ape?"
URL             = "http://oss.codepoet.no/revelation/"
AUTHOR          = "Erik Grinaker <erikg@codepoet.no>"
COPYRIGHT       = "Copyright \302\251 2003-2006 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  = {}

                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 == None:
                        return

                elif 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
                if self.callbacks.has_key(id) == False:

                        # workaround for 64-bit overflows in gnome-python (bug #170822)
                        id += (2 ** 32)

                        if self.callbacks.has_key(id) == False:
                                raise ConfigError

                callback, userdata = self.callbacks[id]
                callback(entry.get_key(), v, userdata)


        def __resolve_keypath(self, key):
                "Resolves a key path"

                return key[0] == "/" and key or self.basedir + "/" + key


        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)


        def set_force(self, key, value):
                "Sets a value directly, ignoring if it already exists"

                key = self.__resolve_keypath(key)

                if type(value) == str:
                        self.client.set_string(key, value)

                elif type(value) == bool:
                        self.client.set_bool(key, value)

                elif type(value) == int:
                        self.client.set_int(key, value)



def install_schema(file):
        "Installs a gconf schema"

        if os.access(file, os.F_OK) == False:
                return False

        output, status = util.execute(FILE_GCONFTOOL + " --install-schema-file=" + file)

        return status == 0