diff --git a/.travis.yml b/.travis.yml index 80c38c6..692b3c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,6 @@ env: - TOXENV=pypy3 - TOXENV=flake8 install: - - travis_retry pip install tox + - travis_retry make install script: - - make test + - make test diff --git a/Makefile b/Makefile index c0e4ef4..570d4bf 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ help: @echo ' make test run the test suite' @echo '' +install: + pip install -e .[testing] + release: python setup.py register sdist bdist_wheel upload diff --git a/README.rst b/README.rst index e154392..d0ec20d 100644 --- a/README.rst +++ b/README.rst @@ -182,9 +182,19 @@ Read from a .env file (line delimited KEY=VALUE): # Values can be read as normal env.int('FOO') +Contributing +------------ + +Clone the repo, create a virtualenv then run: + + $ make install + +which will install the package as well any dependencies required for running the +tests. Tests ------ +~~~~~ + .. image:: https://secure.travis-ci.org/rconradharris/envparse.png?branch=master To run the tests install tox:: diff --git a/envparse.py b/envparse.py index e13b584..048edba 100644 --- a/envparse.py +++ b/envparse.py @@ -164,10 +164,13 @@ def cast(cls, value, cast=str, subcast=None): url = shortcut(urlparse.urlparse) @staticmethod - def read_envfile(path=None, **overrides): + def read_envfile(path=None, _overwrite=False, **overrides): """ Read a .env file (line delimited KEY=VALUE) into os.environ. + If _overwrite is True, then existing environment variables will be overwritten by the + values within the file. + If not given a path to the file, recurses up the directory tree until found. @@ -179,6 +182,12 @@ def read_envfile(path=None, **overrides): caller_dir = os.path.dirname(frame.f_code.co_filename) path = os.path.join(os.path.abspath(caller_dir), '.env') + # Determine function to use for updating environment variables + if _overwrite: + set_env_var = os.environ.__setitem__ + else: + set_env_var = os.environ.setdefault + try: with open(path, 'r') as f: content = f.read() @@ -208,10 +217,11 @@ 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) + set_env_var(name, value) for name, value in overrides.items(): - os.environ.setdefault(name, value) + set_env_var(name, value) + # Convenience object if no schema is required. env = Env() diff --git a/setup.cfg b/setup.cfg index 5e40900..d6e10b1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,5 @@ [wheel] universal = 1 + +[flake8] +max-line-length = 100 diff --git a/setup.py b/setup.py index 898814c..22956fd 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import print_function -from setuptools import setup, find_packages +from setuptools import setup from setuptools.command.test import test as TestCommand import codecs import os @@ -36,11 +36,12 @@ def finalize_options(self): self.test_suite = True def run_tests(self): - #import here, cause outside the eggs aren't loaded + # import here, cause outside the eggs aren't loaded import pytest errno = pytest.main(self.pytest_args) sys.exit(errno) + setup( name='envparse', version=find_version('.', 'envparse.py'), @@ -56,7 +57,7 @@ def run_tests(self): py_modules=['envparse'], platforms='any', zip_safe=False, - classifiers = [ + classifiers=[ 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', @@ -70,6 +71,6 @@ def run_tests(self): 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', ], extras_require={ - 'testing': ['pytest'], + 'testing': ['pytest', 'tox', 'flake8', 'coverage'], } ) diff --git a/tests/test_read_envfile.py b/tests/test_read_envfile.py new file mode 100644 index 0000000..a910760 --- /dev/null +++ b/tests/test_read_envfile.py @@ -0,0 +1,16 @@ +import os + +from envparse import env + + +def test_existing_env_vars_can_be_overwritten(): + os.environ['STR'] = 'bar' + + # The value of STR is 'foo' in the sample file. Without the overwrite param, the env var should + # not be altered... + env.read_envfile("tests/envfile") + assert env.str("STR") == 'bar' + + # ...but with the overwrite param it should. + env.read_envfile("tests/envfile", _overwrite=True) + assert env.str("STR") == 'foo' diff --git a/tox.ini b/tox.ini index 2405dc3..dba3b64 100644 --- a/tox.ini +++ b/tox.ini @@ -19,4 +19,4 @@ commands = deps = flake8 commands = - flake8 envparse.py tests --max-line-length=100 + flake8