Skip to content

Instantly share code, notes, and snippets.

@tdavis
Created December 17, 2014 17:22
Show Gist options
  • Save tdavis/896ed03fc603ab4c6920 to your computer and use it in GitHub Desktop.
Save tdavis/896ed03fc603ab4c6920 to your computer and use it in GitHub Desktop.
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