summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorDylan Baker <baker.dylan.c@gmail.com>2015-09-22 12:58:11 -0700
committerDylan Baker <baker.dylan.c@gmail.com>2015-09-23 14:39:12 -0700
commit23e909cf605b28bda6a433c19a80dd2688ac54b3 (patch)
tree1dafb89f37259a4b2a0c525dcd12c524221c612f /framework
parent5c5d61cdc3ba416646afc7c0f6a01c06906bfe4a (diff)
framework/core: add fallback to PiglitConfig.safe_get
This is in keeping with the way that configparser.get() works in python3 (although there are some corner case differences since in python3 it uses keyword only arguments, which don't exist in python2). Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com> Reviewed-by: Thomas Wood <thomas.wood@intel.com>
Diffstat (limited to 'framework')
-rw-r--r--framework/core.py6
-rw-r--r--framework/tests/core_tests.py4
2 files changed, 7 insertions, 3 deletions
diff --git a/framework/core.py b/framework/core.py
index 341652adf..549130bb2 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -58,7 +58,7 @@ class PiglitConfig(ConfigParser.SafeConfigParser):
ConfigParser.SafeConfigParser.readfp(self, fp, filename)
self.filename = os.path.abspath(filename or fp.name)
- def safe_get(self, *args, **kwargs):
+ def safe_get(self, section, option, fallback=None, **kwargs):
"""A version of self.get that doesn't raise NoSectionError or
NoOptionError.
@@ -67,9 +67,9 @@ class PiglitConfig(ConfigParser.SafeConfigParser):
"""
try:
- return self.get(*args, **kwargs)
+ return self.get(section, option, **kwargs)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
- return None
+ return fallback
def required_get(self, section, option, **kwargs):
"""A version fo self.get that raises PiglitFatalError.
diff --git a/framework/tests/core_tests.py b/framework/tests/core_tests.py
index af0933900..9be9c57e3 100644
--- a/framework/tests/core_tests.py
+++ b/framework/tests/core_tests.py
@@ -267,3 +267,7 @@ class TestPiglitConfig(object):
"""core.PiglitConfig: required_get raises PiglitFatalError if the section is missing
"""
self.conf.required_get('invalid', 'invalid')
+
+ def test_safe_get_fallback(self):
+ """core.PiglitConfig: safe_get returns the value of fallback when the section or option is missing"""
+ nt.eq_(self.conf.safe_get('invalid', 'invalid', fallback='foo'), 'foo')