Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#26: Adding an "overwrite" flag to read_dotenv, False by default. #27

Merged
merged 4 commits into from Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,6 @@ sudo: false
env:
- TOX_ENV=py27-flake8
- TOX_ENV=py27
- TOX_ENV=py33
- TOX_ENV=py34
- TOX_ENV=py35

Expand Down
14 changes: 11 additions & 3 deletions dotenv.py
Expand Up @@ -4,7 +4,7 @@
import warnings


__version__ = '1.4.1'
__version__ = '1.4.2'


line_re = re.compile(r"""
Expand Down Expand Up @@ -34,12 +34,17 @@
""", re.IGNORECASE | re.VERBOSE)


def read_dotenv(dotenv=None):
def read_dotenv(dotenv=None, override=False):
"""
Read a .env file into os.environ.

If not given a path to a dotenv path, does filthy magic stack backtracking
to find manage.py and then find the dotenv.

If tests rely on .env files, setting the overwrite flag to True is a safe
way to ensure tests run consistently across all environments.

:param override: True if values in .env should override system variables.
"""
if dotenv is None:
frame_filename = sys._getframe().f_back.f_code.co_filename
Expand All @@ -51,7 +56,10 @@ def read_dotenv(dotenv=None):
if os.path.exists(dotenv):
with open(dotenv) as f:
for k, v in parse_dotenv(f.read()).items():
os.environ.setdefault(k, v)
if override:
os.environ[k] = v
else:
os.environ.setdefault(k, v)
else:
warnings.warn("Not reading {0} - it doesn't exist.".format(dotenv),
stacklevel=2)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -49,7 +49,6 @@ def get_version(module):
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Internet :: WWW/HTTP',
]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,7 +1,7 @@
[tox]
envlist =
py27-flake8,
py27, py32, py33, py34, py35
py27, py32, py34, py35

[testenv]
commands = python setup.py test
Expand Down