From: Alfredo Deza Date: Tue, 24 Sep 2013 16:24:22 +0000 (-0400) Subject: create a helper for a safe get on ConfigParser X-Git-Tag: v1.2.7~10^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=00d6071fa618b3033d4c671a173fe3b101e88cc2;p=ceph-deploy.git create a helper for a safe get on ConfigParser Signed-off-by: Alfredo Deza --- diff --git a/ceph_deploy/config.py b/ceph_deploy/config.py index 74024ed..7a9e8de 100644 --- a/ceph_deploy/config.py +++ b/ceph_deploy/config.py @@ -1,4 +1,5 @@ import logging +import ConfigParser from cStringIO import StringIO @@ -10,6 +11,20 @@ from .sudo_pushy import get_transport LOG = logging.getLogger(__name__) + +def safe_get(cfg, section, key): + """ + Attempt to get a configuration value from a certain section + in a ``cfg`` object but returning None if not found. Avoids the need + to be doing try/except {ConfigParser Exceptions} every time. + """ + try: + return cfg.get(section, key) + except (ConfigParser.NoSectionError, + ConfigParser.NoOptionError): + return None + + def config_push(args): cfg = conf.load(args) conf_data = StringIO()