diff --git a/README.rst b/README.rst index c4c730a..d04a26c 100644 --- a/README.rst +++ b/README.rst @@ -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. diff --git a/decouple.py b/decouple.py index 28dc3dc..0e1b07e 100644 --- a/decouple.py +++ b/decouple.py @@ -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): @@ -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) diff --git a/tests/autoconfig/env/custom-path/.env b/tests/autoconfig/env/custom-path/.env new file mode 100644 index 0000000..1124b51 --- /dev/null +++ b/tests/autoconfig/env/custom-path/.env @@ -0,0 +1 @@ +KEY=CUSTOMPATH diff --git a/tests/test_autoconfig.py b/tests/test_autoconfig.py index 667c266..63136bc 100644 --- a/tests/test_autoconfig.py +++ b/tests/test_autoconfig.py @@ -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')