Skip to content

Instantly share code, notes, and snippets.

@aubricus
Created February 28, 2017 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubricus/b6e77fc5257c874f487bd4509df39b3e to your computer and use it in GitHub Desktop.
Save aubricus/b6e77fc5257c874f487bd4509df39b3e to your computer and use it in GitHub Desktop.
# See: https://github.com/joke2k/django-environ/blob/v0.4.1/environ/environ.py#L48
class NoValue(object):
def __repr__(self):
return '<{0}>'.format(self.__class__.__name__)
# https://github.com/joke2k/django-environ/blob/v0.4.1/environ/environ.py#L67
NOTSET = NoValue()
def env(env_var, default=NOTSET):
"""
Tiny os.environ wrapper to DRY-up env / error handling.
The api here is a basic version of django.environ in preperation
for refactoring to that library permanently.
Raises djnago.core.execptions.ImproperlyConfigured
if no default is specified.
Kwargs:
default -- The default value to return if var is not set (default None)
"""
try:
return os.environ[env_var]
except KeyError as e:
if default is NOTSET:
error_msg = "{} is a required env setting but is not set!".format(env_var)
raise ImproperlyConfigured(error_msg)
else:
return default
except Exception as e:
print "Caught unhandled exception while accessing os.environ, {}".format(e.message)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment