[Syncropated-commits] r156 - trunk/src

zimmerle at garage.maemo.org zimmerle at garage.maemo.org
Mon Jan 29 23:10:59 EET 2007


Author: zimmerle
Date: 2007-01-29 23:10:58 +0200 (Mon, 29 Jan 2007)
New Revision: 156

Added:
   trunk/src/Configuration.py
Log:
first Configuration.py import

Added: trunk/src/Configuration.py
===================================================================
--- trunk/src/Configuration.py	2007-01-29 21:03:12 UTC (rev 155)
+++ trunk/src/Configuration.py	2007-01-29 21:10:58 UTC (rev 156)
@@ -0,0 +1,193 @@
+# Configuration.py
+#
+#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
+#
+#  Author: Felipe Zimmerle (felipe at zimmerle.org)
+#
+#  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 sys
+import gobject
+from Logger import Logger
+
+log = Logger()
+
+class Configuration(gobject.GObject):
+
+	__instance = None
+	__engine = None
+	__default_values = None
+
+	__gsignals__ = { 
+		'configuration_changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING)),
+	}
+
+	def __new__(self, default_values=None):
+		"""
+		Handle the configurator class as Singleton
+		
+		"""
+
+		if self.__instance == None:
+			log.debug('Configuration :: Using default values: ' +str(default_values))
+			self.__default_values = default_values
+			self.__instance = super(Configuration, self).__new__(self)
+
+		return self.__instance
+
+	def __init__(self, default_values=None):
+		"""
+		Class constructor
+		
+		Define the engine (now supports Windows e Linux)
+		Should support Mac too, but not for now.
+
+		"""
+
+		gobject.GObject.__init__(self)
+
+		if self.__engine == None:
+			if sys.platform == "win32":
+				self.__engine = ConfigurationEngineWindows()
+			else:
+				self.__engine = ConfigurationEngineLinux()
+
+	def __repr__(self):
+		"""
+		Debug proupose only
+		
+		"""
+
+		return "<Configuration Singleton at: %s>" % (hex(id(self)))
+
+	def getValue(self, key):
+		"""
+		Return the value of the key 'key'. If the key doesn't exist in
+		configuration file, return the 'key' default value. If its
+		doesn't exist too, return None.
+
+		"""
+
+		try:
+			value = self.__engine.getValue(key)
+			if value != None and value != '':
+				return self.__engine.getValue(key)
+		except AttributeError, err:
+			log.debug("Configuration :: get AttributeError: " + key)
+			log.debug("Configuration :: get AttributeError: " + str(err))
+			pass
+
+		return self.getDefaultValue(key)
+
+	def setValue(self, key, value):
+		"""
+		Set the value of the key 'key' with the value 'value'.
+
+		If its not possible to do that for any reason, return False
+		else return True
+
+		"""
+
+		try:
+			self.__engine.setValue(key, value)
+			self.emit('configuration_changed', key, value)
+			return True
+		except AttributeError:
+			log.debug("Configuration :: set AttributeError: " + key)
+			return False
+
+	def getDefaultValue(self, key):
+		"""
+		Return the default value of the key 'key'. If its doesn't
+		exist, return None.
+
+		"""
+
+		try:
+			return self.__default_values[key]
+		except KeyError:
+			log.debug("Configuration :: KeyError: " + key)
+
+
+
+
+class ConfigurationEngineWindows(object):
+	"""
+	MS-Windows Configuration engine
+
+	"""
+
+	CONFIG_KEY = "Software\\INdT\\syncropated"
+	winreg = None
+
+	def __init__(self):
+		log.info("Configuration :: Windows Configuration Engine was started!")
+
+		import _winreg
+
+		self.winreg = _winreg
+
+		try:
+			self.key = self.winreg.OpenKey(self.winreg.HKEY_CURRENT_USER, ConfigurationEngineWindows.CONFIG_KEY)
+		except:
+			self.key = self.winreg.CreateKey(self.winreg.HKEY_CURRENT_USER, ConfigurationEngineWindows.CONFIG_KEY)
+
+	def getValue(self, key):
+		value = ''
+		try:
+			value = self.winreg.QueryValue(self.key, key)
+		except:
+			pass
+
+		return value
+
+	def setValue(self, key, value):
+		self.winreg.SetValue(self.key, key, self.winreg.REG_SZ,str(value))
+
+
+
+
+class ConfigurationEngineLinux(object):
+	"""
+	Linux configuration engine
+
+	"""
+
+
+	CONFIG_PATH = "/apps/syncropated"
+
+	def __init__(self):
+		log.info("Configuration :: Linux Configuration Engine was started!")
+
+		import gconf
+
+		self.config_client = gconf.client_get_default ()
+		self.config_client.add_dir ("/apps/syncropated", gconf.CLIENT_PRELOAD_NONE)
+
+	def getValue(self, key):
+		value = ''
+		try:
+			value = self.config_client.get_string(ConfigurationEngineLinux.CONFIG_PATH + "/" + key)
+		except:
+			pass
+
+		return value
+
+	def setValue(self, key, value):
+		self.config_client.set_string(ConfigurationEngineLinux.CONFIG_PATH + "/" + key, value)
+
+
+



More information about the Syncropated-commits mailing list