Skip to content

Commit

Permalink
Chore: Modularize getenvpass function
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 4, 2023
1 parent 927eff4 commit 037cf64
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pueblo/util/environ.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import getpass
import os
import typing as t

from dotenv import find_dotenv, load_dotenv


def getenvpass(env_var: str, prompt: str, skip_pytest_notebook: bool = True) -> None:
def init_dotenv():
"""
Find `.env` file and load environment variables.
"""
load_dotenv(find_dotenv())


def getenvpass(env_var: str, prompt: t.Union[str, None] = None, skip_pytest_notebook: bool = True) -> None:
"""
Read variable from environment or `.env` file.
If it is not defined, prompt interactively.
FIXME: Needs a patch to make it work with `pytest-notebook`,
see https://github.com/chrisjsewell/pytest-notebook/issues/43.
"""
load_dotenv(find_dotenv())

init_dotenv()
if env_var not in os.environ:
if "PYTEST_CURRENT_TEST" in os.environ and skip_pytest_notebook:
import pytest

pytest.exit(f"{env_var} not given [skip-notebook]")
else:
elif prompt:
os.environ[env_var] = getpass.getpass(prompt)

0 comments on commit 037cf64

Please sign in to comment.