core: better management of default value in getConfig
This commit is contained in:
parent
97630ccab9
commit
6b20989ecc
5 changed files with 21 additions and 11 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue