Source

Revelation / src / lib / config.py.in

Full commit
#
# Revelation - 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import util

import gconf, gettext, os

_ = gettext.gettext

DIR_GCONFSCHEMAS= "@pkgschemadir@"
DIR_ICONS	= "@datadir@/icons"
DIR_LOCALE	= "@datadir@/locale"
DIR_UI		= "@pkgdatadir@/ui"

FILE_GCONFTOOL	= "@GCONFTOOL@"

APPNAME		= "Revelation"
PACKAGE		= "@PACKAGE@"
VERSION		= "@VERSION@"
DATAVERSION	= 2
RELNAME		= "One Toke Over the Line"
URL		= "http://revelation.olasagasti.info/"
AUTHORS		= [ "Erik Grinaker <erikg@codepoet.no>","Mikel Olasagasti Uranga <mikel@olasagasti.info>" ]
ARTISTS		= [ "Erik Grinaker <erikg@codepoet.no>" ]
COPYRIGHT	= _('Copyright') + " \302\251 2003-2007 Erik Grinaker\n2010-2012 Mikel Olasagasti Uranga"
LICENSE		= _("""Revelation 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.

Revelation 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 Revelation; if not, write to the Free Software Foundation, Inc., 
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA""")



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:
			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