Skip to content

Commit

Permalink
Merge branch 'env-override'
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquebastos committed Sep 15, 2015
2 parents d2235e8 + c26d7aa commit ba078cb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,15 @@ How it works?

- ``RepositoryIni``

Can read values from ini files.
Can read values from ``os.environ`` and ini files, in that order.

**Note:** Since version 3.0 *decouple* respects unix precedence of environment variables *over* config files.

- ``RepositoryEnv``

Can read ``.env`` files and when a parameter does not exist there,
it tries to find it on ``os.environ``.
Can read values from ``os.environ`` and ``.env`` files.

This process does **not** change nor add any environment variables.
**Note:** Since version 3.0 *decouple* respects unix precedence of environment variables *over* config files.

- ``RepositoryShell``

Expand Down
13 changes: 6 additions & 7 deletions decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ def __init__(self, source):
self.parser.readfp(open(source))

def __contains__(self, key):
return self.parser.has_option(self.SECTION, key)
return (key in os.environ or
self.parser.has_option(self.SECTION, key))

def get(self, key):
return self.parser.get(self.SECTION, key)
return (os.environ.get(key) or
self.parser.get(self.SECTION, key))


class RepositoryEnv(RepositoryBase):
Expand All @@ -122,13 +124,10 @@ def __init__(self, source):
self.data[k] = v

def __contains__(self, key):
return key in self.data or key in os.environ
return key in os.environ or key in self.data

def get(self, key):
try:
return self.data[key]
except KeyError:
return os.environ[key]
return os.environ.get(key) or self.data[key]


class RepositoryShell(RepositoryBase):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
README = os.path.join(os.path.dirname(__file__), 'README.rst')

setup(name='python-decouple',
version='2.4',
version='3.0',
description='Strict separation of settings from code.',
long_description=open(README).read(),
author="Henrique Bastos", author_email="[email protected]",
Expand Down
8 changes: 4 additions & 4 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
IgnoreSpace = text
RespectSingleQuoteSpace = ' text'
RespectDoubleQuoteSpace = " text"
KeyOverrideByEnv=NotThis
'''

@pytest.fixture(scope='module')
Expand Down Expand Up @@ -64,10 +65,9 @@ def test_env_bool_false(config):
assert False == config('KeyOff', cast=bool)

def test_env_os_environ(config):
os.environ['KeyFallback'] = 'On'
assert True == config('KeyTrue', cast=bool)
assert True == config('KeyFallback', cast=bool)
del os.environ['KeyFallback']
os.environ['KeyOverrideByEnv'] = 'This'
assert 'This' == config('KeyOverrideByEnv')
del os.environ['KeyOverrideByEnv']

def test_env_undefined(config):
with pytest.raises(UndefinedValueError):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_ini.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
import os
import sys
from mock import patch, mock_open
import pytest
Expand Down Expand Up @@ -31,6 +32,7 @@
PercentIsEscaped=%%
Interpolation=%(KeyOff)s
IgnoreSpace = text
KeyOverrideByEnv=NotThis
'''

@pytest.fixture(scope='module')
Expand Down Expand Up @@ -85,3 +87,8 @@ def test_ini_empty(config):

def test_ini_support_space(config):
assert 'text' == config('IgnoreSpace')

def test_ini_os_environ(config):
os.environ['KeyOverrideByEnv'] = 'This'
assert 'This' == config('KeyOverrideByEnv')
del os.environ['KeyOverrideByEnv']

0 comments on commit ba078cb

Please sign in to comment.