Package: envyaml
EnvYAML | Easy configuration file parser for structured dataMotivationModern configuration files become to be more and more complex, flexible, and readable.
YAML file format is perfect to store configuration but had no option to pass environment variables. They give flexibility, readability and provide an option to store complex data structure.
This project goal is to simplify usage of the YAML file and environment variables as program configuration files with easy config key access.InstallpipinstallenvyamlBasic usageLet's assume we had a project with this config fileenv.yaml# env.yamlproject:name:"${PROJECT_NAME}-${PROJECT_ID}"database:host:$DATABASE_HOSTport:3301username:usernamepassword:$DATABASE_PASSWORDdatabase:testtable:user:table_userblog:table_blogquery:|-SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3insert:|-INSERT INTO "{table}" (user, login) VALUES ($1, $2)redis:host:$REDIS_HOST|127.0.0.1port:5040db:$REDIS_DB|3# with default valueconfig:expire:300prefix:$REDIS_PREFIXescaped:$$.extraempty_env:$NOT_EXIST_ENV_VARIABLEEnvironment variables set toPROJECT_NAME=simple-hello
PROJECT_ID=42
DATABASE_HOST=xxx.xxx.xxx.xxx
DATABASE_PASSWORD=super-secret-password
REDIS_PREFIX=stateParse file withEnvYAMLfromenvyamlimportEnvYAML# read file env.yaml and parse configenv=EnvYAML('env.yaml')# access project nameprint(env['project.name'])# >> simple-hello-42# access whole database sectionprint(env['database'])# {# 'database': 'test',# 'host': 'xxx.xxx.xxx.xxx',# 'password': 'super-secret-password',# 'port': 3301,# 'table':#   {#       'blog': 'table_blog',#       'user': 'table_user'#   },# 'username': 'username'# }# access database host value as key itemprint(env['database.host'])# >> xxx.xxx.xxx.xxx# access database user table value as key itemprint(env['database.table.user'])# >> table_user# get sql query with $1,$2,$3 variablesprint(env['database.query'])# >> SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3# using default values if variable not defined# one example is redis host and redis port, when $REDIS_HOST not set then default value will be usedprint(env['redis.host'])# >> 127.0.0.1# one example is redis host and redis port, when $REDIS_DB not set then default value will be usedprint(env['redis.db'])# >> 3# access list items by numberprint(env['list_test'][0])# >> one# access list items by number as keyprint(env['list_test.1'])# >> two# test if you have keyprint('redis.port'inenv)# >> TrueAccess config withgetfunction and default valueprint(env.get('not.exist.value','default'))# >> defaultprint(env.get('empty_env','default'))# >> defaultprint(env['empty_env'])# >> NoneUseformatfunction to update placeholderprint(env.format('database.insert',table="users"))# >> INSERT INTO "users" (user, login) VALUES ($1, $2)Strict modeThis mode isenable by defaultand prevents from declaring variables that do not exist inenvironment variablesor.envfile. This leads to having runtimeValueErrorexception when variables do not define with messageStrict mode enabled, variable $VAR not defined!. To disablestrictmode specifystrict=Falseat EnvYAML object initialization. Another option to disablestrictmode is to defineENVYAML_STRICT_DISABLEenvironment variable before initializing EnvYAML object.Escaped variablesIn case of usage$in env.yaml file as value double$$should be used. Example:
Useescapedvariableprint(env['escaped'])# >> $.extraLicenseMIT licensed. See theLICENSEfile for more details.