From 4c33220680b65b38b862d451f02c7aa2877309c1 Mon Sep 17 00:00:00 2001 From: Hank Gay Date: Wed, 24 Jan 2018 10:17:23 -0500 Subject: [PATCH] Allow the caller to control whether read_env will recurse. The underlying library already exposes a flag to control this behavior, so I'm merely adding a passthrough for that flag. --- environs.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/environs.py b/environs.py index 5538e92..5cb9540 100644 --- a/environs.py +++ b/environs.py @@ -139,16 +139,20 @@ def __repr__(self): __str__ = __repr__ @staticmethod - def read_env(path=None): - """Read a .env file into os.environ. If .env is not found in the directory from - which this method is called, recurse up the directory tree until a .env file is found. + def read_env(path=None, recurse=True): + """Read a .env file into os.environ. + + If .env is not found in the directory from which this method is called, + the default behavior is to recurse up the directory tree until a .env + file is found. If you do not wish to recurse up the tree, you may pass + False as a second positional argument. """ # By default, start search from the same file this function is called if path is None: frame = inspect.currentframe().f_back caller_dir = os.path.dirname(frame.f_code.co_filename) path = os.path.join(os.path.abspath(caller_dir), '.env') - return _read_env(path=path) + return _read_env(path=path, recurse=recurse) @contextlib.contextmanager def prefixed(self, prefix):