]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
create a helper for a safe get on ConfigParser
authorAlfredo Deza <alfredo.deza@inktank.com>
Tue, 24 Sep 2013 16:24:22 +0000 (12:24 -0400)
committerAlfredo Deza <alfredo.deza@inktank.com>
Tue, 24 Sep 2013 16:24:22 +0000 (12:24 -0400)
Signed-off-by: Alfredo Deza <alfredo.deza@inktank.com>
ceph_deploy/config.py

index 74024ed3ae31e9cc7e68af7daa2e1816acf654a9..7a9e8de34576dfc5a8bd5150b3a134331e6ae1b8 100644 (file)
@@ -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()