Add 'default' argument to conf.get* functions

Change-Id: I048f1a1b3a70b7f44e4310e7c88d4522c475db55
Closes-Bug: #1408352
This commit is contained in:
Dmitry Tantsur 2015-01-07 17:34:40 +01:00
parent d014d9f060
commit f698bb2f09
2 changed files with 16 additions and 5 deletions

View File

@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
from six.moves import configparser
@ -33,18 +35,27 @@ DEFAULTS = {
'listen_port': '5050',
'authenticate': 'true',
# General service settings
'database': '',
'processing_hooks': 'scheduler,validate_interfaces',
'debug': 'false',
}
def with_default(func):
@functools.wraps(func)
def wrapper(section, option, default=None):
try:
return func(section, option)
except (configparser.NoSectionError, configparser.NoOptionError):
return default
return wrapper
def init_conf():
global CONF, get, getint, getboolean, read
CONF = configparser.ConfigParser(defaults=DEFAULTS)
get = CONF.get
getint = CONF.getint
getboolean = CONF.getboolean
get = with_default(CONF.get)
getint = with_default(CONF.getint)
getboolean = with_default(CONF.getboolean)
read = CONF.read

View File

@ -67,7 +67,7 @@ def init():
"""Initialize the database."""
global _DB_NAME
_DB_NAME = conf.get('discoverd', 'database').strip()
_DB_NAME = conf.get('discoverd', 'database', default='').strip()
if not _DB_NAME:
LOG.critical('Configuration option discoverd.database should be set')
sys.exit(1)