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.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)