Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the caller to control whether read_env will recurse. #9

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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