diff --git a/src/core/sat_main.py b/src/core/sat_main.py index c7dfff25..fc9e7587 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -95,7 +95,7 @@ class SAT(service.Service): 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 @@ class SAT(service.Service): 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): diff --git a/src/memory/memory.py b/src/memory/memory.py index d14dca1e..18af8f7e 100644 --- a/src/memory/memory.py +++ b/src/memory/memory.py @@ -226,12 +226,15 @@ class Memory(object): 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 diff --git a/src/plugins/plugin_misc_account.py b/src/plugins/plugin_misc_account.py index 772cb8e6..8302ac71 100755 --- a/src/plugins/plugin_misc_account.py +++ b/src/plugins/plugin_misc_account.py @@ -151,7 +151,7 @@ class MiscAccount(object): 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): diff --git a/src/plugins/plugin_tmp_demo_directory.py b/src/plugins/plugin_tmp_demo_directory.py index dd20c5f3..5a57c10c 100755 --- a/src/plugins/plugin_tmp_demo_directory.py +++ b/src/plugins/plugin_tmp_demo_directory.py @@ -76,11 +76,11 @@ class DemoDirectory(object): 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) diff --git a/src/tools/config.py b/src/tools/config.py index c8b4c6e4..589667f6 100644 --- a/src/tools/config.py +++ b/src/tools/config.py @@ -73,7 +73,7 @@ def getConfig(config, section, name, default=None): @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 @@ def getConfig(config, section, name, default=None): 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)