changeset 1234:9c17bd37e6e5

core: better management of default value in getConfig
author Goffi <goffi@goffi.org>
date Tue, 07 Oct 2014 17:12:41 +0200
parents 0b87d029f0a3
children 80d2ed788b40
files src/core/sat_main.py src/memory/memory.py src/plugins/plugin_misc_account.py src/plugins/plugin_tmp_demo_directory.py src/tools/config.py
diffstat 5 files changed, 21 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Mon Oct 06 17:25:41 2014 +0200
+++ b/src/core/sat_main.py	Tue Oct 07 17:12:41 2014 +0200
@@ -95,7 +95,7 @@
         self.bridge.register("getWaitingSub", self.memory.getWaitingSub)
         self.bridge.register("getWaitingConf", self.getWaitingConf)
         self.bridge.register("sendMessage", self._sendMessage)
-        self.bridge.register("getConfig", self.memory.getConfig)
+        self.bridge.register("getConfig", self._getConfig)
         self.bridge.register("setParam", self.setParam)
         self.bridge.register("getParamA", self.memory.getStringParamA)
         self.bridge.register("asyncGetParamA", self.memory.asyncGetStringParamA)
@@ -396,6 +396,15 @@
             raise exceptions.ProfileKeyUnknownError
         return [self.profiles[profile]]
 
+    def _getConfig(self, section, name):
+        """Get the main configuration option
+
+        @param section: section of the config file (None or '' for DEFAULT)
+        @param name: name of the option
+        @return: unicode representation of the option
+        """
+        return unicode(self.memory.getConfig(section, name, ''))
+
     ## Client management ##
 
     def setParam(self, name, value, category, security_limit, profile_key):
--- a/src/memory/memory.py	Mon Oct 06 17:25:41 2014 +0200
+++ b/src/memory/memory.py	Tue Oct 07 17:12:41 2014 +0200
@@ -226,12 +226,15 @@
             log.error(_("Can't read main config !"))
         return config
 
-    def getConfig(self, section, name):
+    def getConfig(self, section, name, default=None):
         """Get the main configuration option
+
         @param section: section of the config file (None or '' for DEFAULT)
         @param name: name of the option
+        @param default: value to use if not found
+        @return: str, list or dict
         """
-        return tools_config.getConfig(self.config, section, name, default='')
+        return tools_config.getConfig(self.config, section, name, default)
 
     def load_xml(self, filename):
         """Load parameters template from xml file
--- a/src/plugins/plugin_misc_account.py	Mon Oct 06 17:25:41 2014 +0200
+++ b/src/plugins/plugin_misc_account.py	Tue Oct 07 17:12:41 2014 +0200
@@ -151,7 +151,7 @@
         self.__delete_account_id = host.registerCallback(self.__deleteAccountCb, with_data=True)
 
     def getConfig(self, name):
-        return self.host.memory.getConfig(CONFIG_SECTION, name) or default_conf[name]
+        return self.host.memory.getConfig(CONFIG_SECTION, name, default_conf[name])
 
     def _registerAccount(self, email, password, profile):
 
--- a/src/plugins/plugin_tmp_demo_directory.py	Mon Oct 06 17:25:41 2014 +0200
+++ b/src/plugins/plugin_tmp_demo_directory.py	Tue Oct 07 17:12:41 2014 +0200
@@ -76,11 +76,11 @@
 
     def __init__(self, host):
         log.info(_(u"Plugin demo directory initialization"))
-        activate = host.memory.getConfig(CONFIG_SECTION, CONFIG_ACTIVATE) or 'false'
+        activate = host.memory.getConfig(CONFIG_SECTION, CONFIG_ACTIVATE, 'false')
         if not activate.lower() in ('true', 'yes', '1'):
             log.info("not activated")
             return
-        service_str = host.memory.getConfig(CONFIG_SECTION, CONFIG_SERVICE) or 'salut.libervia.org'
+        service_str = host.memory.getConfig(CONFIG_SECTION, CONFIG_SERVICE, 'salut.libervia.org')
         self.service = jid.JID(service_str)
         self.host = host
         host.memory.updateParams(self.params)
--- a/src/tools/config.py	Mon Oct 06 17:25:41 2014 +0200
+++ b/src/tools/config.py	Tue Oct 07 17:12:41 2014 +0200
@@ -73,7 +73,7 @@
     @param config (SafeConfigParser): the configuration instance
     @param section (str): section of the config file (None or '' for DEFAULT)
     @param name (str): name of the option
-    @param default (str): eventually default to this value, if not None
+    @param default: value to use if not found
     @return: str, list or dict
     """
     if not section:
@@ -81,10 +81,8 @@
 
     try:
         value = config.get(section, name)
-    except (NoOptionError, NoSectionError) as e:
-        if default is None:
-            raise e
-        value = default
+    except (NoOptionError, NoSectionError):
+        return default
 
     if name.endswith('_path') or name.endswith('_dir'):
         value = os.path.expanduser(value)