Skip to content

Commit

Permalink
Merge pull request #9 from gthank/master
Browse files Browse the repository at this point in the history
Allow the caller to control whether read_env will recurse.
  • Loading branch information
sloria authored Jan 25, 2018
2 parents ce22820 + 4c33220 commit 67b0ff9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 67b0ff9

Please sign in to comment.