Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ env:
- TOXENV=pypy3
- TOXENV=flake8
install:
- travis_retry pip install tox
- travis_retry make install
script:
- make test
- make test
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rconradharris It doesn't look like this project is configured on travis yet


To run the tests install tox::
Expand Down
16 changes: 13 additions & 3 deletions envparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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()
Expand Down Expand Up @@ -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()
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[wheel]
universal = 1

[flake8]
max-line-length = 100
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'),
Expand All @@ -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',
Expand All @@ -70,6 +71,6 @@ def run_tests(self):
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
extras_require={
'testing': ['pytest'],
'testing': ['pytest', 'tox', 'flake8', 'coverage'],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rconradharris Not sure if this is strictly required given that tox seems to be able to install stuff. I personally find it clearer to have the testing dependencies listed here. I found that after running tox, I still didn't have flake8 installed.

}
)
16 changes: 16 additions & 0 deletions tests/test_read_envfile.py
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ commands =
deps =
flake8
commands =
flake8 envparse.py tests --max-line-length=100
flake8