Skip to content

Commit

Permalink
Merge pull request #27 from alexdlaird/feature-26-adl-overwrite-flag
Browse files Browse the repository at this point in the history
#26: Adding an "overwrite" flag to read_dotenv, False by default.
  • Loading branch information
jpadilla committed Dec 11, 2017
2 parents fb0fb72 + fa0a6fe commit 4a3ea82
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
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

0 comments on commit 4a3ea82

Please sign in to comment.