Exemple #1
0
    def read_env(path=None,
                 recurse=True,
                 stream=None,
                 verbose=False,
                 override=False):
        """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)
            start = os.path.join(os.path.abspath(caller_dir))
        else:
            start = path
        if recurse:
            for dirname in _walk_to_root(start):
                check_path = os.path.join(dirname, ".env")
                if os.path.exists(check_path):
                    return load_dotenv(check_path,
                                       stream=stream,
                                       verbose=verbose,
                                       override=override)
        else:
            if path is None:
                start = os.path.join(start, ".env")
            return load_dotenv(start,
                               stream=stream,
                               verbose=verbose,
                               override=override)
Exemple #2
0
    def read_env(path=None, recurse=True, stream=None, verbose=False, override=False):
        """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)
            start = os.path.join(os.path.abspath(caller_dir))
        else:
            start = path
        if recurse:
            for dirname in _walk_to_root(start):
                check_path = os.path.join(dirname, ".env")
                if os.path.exists(check_path):
                    return load_dotenv(
                        check_path, stream=stream, verbose=verbose, override=override
                    )
        else:
            if path is None:
                start = os.path.join(start, ".env")
            return load_dotenv(start, stream=stream, verbose=verbose, override=override)
Exemple #3
0
    def read_env(
        path: _StrType = None,
        recurse: _BoolType = True,
        verbose: _BoolType = False,
        override: _BoolType = False,
        raise_error_if_not_found: _BoolType = False,
    ) -> None:
        """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.
        """
        if path is None:
            # By default, start search from the same directory this function is called
            current_frame = inspect.currentframe()
            if not current_frame:
                raise RuntimeError("Could not get current call frame.")
            frame = typing.cast(types.FrameType, current_frame.f_back)
            caller_dir = Path(frame.f_code.co_filename).parent.resolve()
            start = caller_dir / ".env"
        else:
            if Path(path).is_dir():
                raise ValueError("path must be a filename, not a directory.")
            start = Path(path)

        is_env_file_found = False
        # TODO: Remove str casts when we drop Python 3.5
        if recurse:
            start_dir, env_name = os.path.split(str(start))
            if not start_dir:  # Only a filename was given
                start_dir = os.getcwd()
            for dirname in _walk_to_root(start_dir):
                check_path = Path(dirname) / env_name
                if check_path.exists():
                    is_env_file_found = True
                    load_dotenv(str(check_path), verbose=verbose, override=override)
                    break
        else:
            if start.exists():
                is_env_file_found = True
                load_dotenv(str(start), verbose=verbose, override=override)

        if verbose:
            print("environ: is_env_file_found={}".format(is_env_file_found))
        if raise_error_if_not_found and not is_env_file_found:
            raise OSError("File not found: {}".format(path or ".env"))
Exemple #4
0
    def read_env(
        path: str = None,
        recurse: bool = True,
        stream: str = None,
        verbose: bool = False,
        override: bool = False,
    ) -> DotEnv:
        """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:
            current_frame = inspect.currentframe()
            if not current_frame:
                raise RuntimeError("Could not get current call frame.")
            frame = current_frame.f_back
            caller_dir = os.path.dirname(frame.f_code.co_filename)
            # Will be a directory
            start = os.path.join(os.path.abspath(caller_dir))
        else:
            # Could be directory or a file
            start = path
        if recurse:
            env_name = os.path.basename(start) if os.path.isfile(
                start) else ".env"
            for dirname in _walk_to_root(start):
                check_path = os.path.join(dirname, env_name)
                if os.path.exists(check_path):
                    return load_dotenv(check_path,
                                       stream=stream,
                                       verbose=verbose,
                                       override=override)
        else:
            if path is None:
                start = os.path.join(start, ".env")
            return load_dotenv(start,
                               stream=stream,
                               verbose=verbose,
                               override=override)
Exemple #5
0
    def read_env(
        path: typing.Optional[_StrType] = None,
        recurse: _BoolType = True,
        verbose: _BoolType = False,
        override: _BoolType = False,
    ) -> None:
        """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.
        """
        if path is None:
            # By default, start search from the same directory this function is called
            current_frame = inspect.currentframe()
            if current_frame is None:
                raise RuntimeError("Could not get current call frame.")
            frame = current_frame.f_back
            assert frame is not None
            caller_dir = Path(frame.f_code.co_filename).parent.resolve()
            start = caller_dir / ".env"
        else:
            if Path(path).is_dir():
                raise ValueError("path must be a filename, not a directory.")
            start = Path(path)
        if recurse:
            start_dir, env_name = os.path.split(start)
            if not start_dir:  # Only a filename was given
                start_dir = os.getcwd()
            for dirname in _walk_to_root(start_dir):
                check_path = Path(dirname) / env_name
                if check_path.exists():
                    load_dotenv(check_path, verbose=verbose, override=override)
                    return
        else:
            load_dotenv(str(start), verbose=verbose, override=override)