thesimj / envyaml

Simple YAML configuration file parser
MIT License
78 stars 21 forks source link

added EnvYAML.__init__ kwargs support to allow override of envvars #18

Closed shaybensasson closed 3 years ago

shaybensasson commented 3 years ago

Notice the following .env snippet from the popular python-dotenv package:

DOMAIN=example.org
ADMIN_EMAIL=admin@${DOMAIN}
ADMIN_PASSWORD=super_secret_password
ROOT_URL=${DOMAIN}/app

If we load this .env using the current envyaml we'll get:

dict(
  DOMAIN="example.org",
  ADMIN_EMAIL="admin@${DOMAIN}",
  ADMIN_PASSWORD="super_secret_password",
  ROOT_URL="${DOMAIN}/app"
)

I suggest fixing this by adding a kwargs as I did on the EnvYAML.__init__ ctor.

This will allow the following code:

def envyaml_with_dotenv_demo():
    from envyaml import EnvYAML

    dotenv_path = Path('.') / '.env'

    # read file env.yaml and parse config
    env_yaml_path = Path('.') / 'env.yaml'
    env = EnvYAML(env_yaml_path,
                  env_file=dotenv_path,
                  # don't fail if some env var is missing
                  strict=False,
                  **dotenv.dotenv_values(dotenv_path)
                  )

I also added a dedicated test: test_it_should_override_cfg_with_kwargs.

I just noticed I forgot to update the setup.py::install_requires with python-dotenv.

thesimj commented 3 years ago

thanks, for your PR