Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions envparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,16 @@ def cast(cls, value, cast=str, subcast=None):
url = shortcut(urlparse.urlparse)

@staticmethod
def read_envfile(path=None, **overrides):
def read_envfile(path=None, override=False, **overrides):
"""
Read a .env file (line delimited KEY=VALUE) into os.environ.

If not given a path to the file, recurses up the directory tree until
found.

If `override=True`, override the values in the environment. Defaults to
`False`, prioritising values in the environment over those in the file.

Uses code from Honcho (github.com/nickstenning/honcho) for parsing the
file.
"""
Expand Down Expand Up @@ -208,10 +211,16 @@ def read_envfile(path=None, **overrides):
if not re.match(r'[A-Za-z_][A-Za-z_0-9]*', name):
continue
value = value.replace(r'\n', '\n').replace(r'\t', '\t')
os.environ.setdefault(name, value)
if override:
os.environ[name] = value
else:
os.environ.setdefault(name, value)

for name, value in overrides.items():
os.environ.setdefault(name, value)
if override:
os.environ[name] = value
else:
os.environ.setdefault(name, value)

# Convenience object if no schema is required.
env = Env()