Created
December 17, 2014 17:22
-
-
Save tdavis/896ed03fc603ab4c6920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import six | |
from six.moves import configparser | |
NO_FALLBACK = object() | |
def configparser_get(conf, section, name=None, fallback=NO_FALLBACK): | |
if name is None: | |
try: | |
if six.PY3: | |
return conf[section] | |
elif section not in conf.sections(): | |
raise configparser.NoSectionError(section) | |
return dict(conf.items(section)) | |
except (configparser.NoSectionError, KeyError): | |
if fallback is not NO_FALLBACK: | |
return fallback | |
raise | |
if six.PY3: | |
if fallback is not NO_FALLBACK: | |
return conf.get(section, name, fallback=fallback) | |
return conf.get(section, name) | |
else: | |
try: | |
return conf.get(section, name) | |
except configparser.NoOptionError: | |
if fallback is not NO_FALLBACK: | |
return fallback | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment