Skip to content

Commit

Permalink
Merge branch 'autoconfig-search-path'
Browse files Browse the repository at this point in the history
Closes #28 #12 #13 #19 #22
  • Loading branch information
henriquebastos committed Aug 5, 2017
2 parents b8f1598 + f018399 commit a23e9e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -224,6 +224,9 @@ How it works?
It recursively searches up your configuration module path looking for a
``settings.ini`` or a ``.env`` file.

Optionally, it accepts ``search_path`` argument to explicitly define
where the search starts.

The **config** object is an instance of ``AutoConfig`` to improve
*decouple*'s usage.

Expand Down
12 changes: 10 additions & 2 deletions decouple.py
Expand Up @@ -147,13 +147,21 @@ def get(self, key):
class AutoConfig(object):
"""
Autodetects the config file and type.
Parameters
----------
search_path : str, optional
Initial search path. If empty, the default search path is the
caller's path.
"""
SUPPORTED = {
'settings.ini': RepositoryIni,
'.env': RepositoryEnv,
}

def __init__(self):
def __init__(self, search_path=None):
self.search_path = search_path
self.config = None

def _find_file(self, path):
Expand Down Expand Up @@ -192,7 +200,7 @@ def _caller_path(self):

def __call__(self, *args, **kwargs):
if not self.config:
self._load(self._caller_path())
self._load(self.search_path or self._caller_path())

return self.config(*args, **kwargs)

Expand Down
1 change: 1 addition & 0 deletions tests/autoconfig/env/custom-path/.env
@@ -0,0 +1 @@
KEY=CUSTOMPATH
6 changes: 6 additions & 0 deletions tests/test_autoconfig.py
Expand Up @@ -60,3 +60,9 @@ def test_autoconfig_is_not_a_file():
with patch('os.path.isfile', return_value=False):
assert True == config('KeyFallback', cast=bool)
del os.environ['KeyFallback']


def test_autoconfig_search_path():
path = os.path.join(os.path.dirname(__file__), 'autoconfig', 'env', 'custom-path')
config = AutoConfig(path)
assert 'CUSTOMPATH' == config('KEY')

0 comments on commit a23e9e6

Please sign in to comment.