Add 'default' argument to conf.get* functions
Change-Id: I048f1a1b3a70b7f44e4310e7c88d4522c475db55 Closes-Bug: #1408352
This commit is contained in:
parent
d014d9f060
commit
f698bb2f09
|
@ -11,6 +11,8 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import functools
|
||||||
|
|
||||||
from six.moves import configparser
|
from six.moves import configparser
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,18 +35,27 @@ DEFAULTS = {
|
||||||
'listen_port': '5050',
|
'listen_port': '5050',
|
||||||
'authenticate': 'true',
|
'authenticate': 'true',
|
||||||
# General service settings
|
# General service settings
|
||||||
'database': '',
|
|
||||||
'processing_hooks': 'scheduler,validate_interfaces',
|
'processing_hooks': 'scheduler,validate_interfaces',
|
||||||
'debug': 'false',
|
'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():
|
def init_conf():
|
||||||
global CONF, get, getint, getboolean, read
|
global CONF, get, getint, getboolean, read
|
||||||
CONF = configparser.ConfigParser(defaults=DEFAULTS)
|
CONF = configparser.ConfigParser(defaults=DEFAULTS)
|
||||||
get = CONF.get
|
get = with_default(CONF.get)
|
||||||
getint = CONF.getint
|
getint = with_default(CONF.getint)
|
||||||
getboolean = CONF.getboolean
|
getboolean = with_default(CONF.getboolean)
|
||||||
read = CONF.read
|
read = CONF.read
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ def init():
|
||||||
"""Initialize the database."""
|
"""Initialize the database."""
|
||||||
global _DB_NAME
|
global _DB_NAME
|
||||||
|
|
||||||
_DB_NAME = conf.get('discoverd', 'database').strip()
|
_DB_NAME = conf.get('discoverd', 'database', default='').strip()
|
||||||
if not _DB_NAME:
|
if not _DB_NAME:
|
||||||
LOG.critical('Configuration option discoverd.database should be set')
|
LOG.critical('Configuration option discoverd.database should be set')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
Loading…
Reference in New Issue