Package: envee
enveeRead variables from the environment or files into dataclasses.While it is convenient to configure applications using environment variables during development, it is advised not to store sensitive information such as passwords in environment variables in production environments. Theenveelibrary allows reading variables either from the environment variables or files (which are typically used by e.g.docker secrets), thus keeping code used for development and production environments as close to each other as possible.UsageVariables to read from the environment are declared using classes annotated with the@envee.environmentdecorator. Usingenvee.read()the fields of the classes are filled using the environment.Example:fromtypingimportOptionalimportenvee@envee.environmentclassEnvironment:username:strdebug:Optional[str]workers:int=5env=envee.read(Environment)Environment variables names and file pathsFor each field, per defaultenveelooks for environment variables with the upper case name of the field. The corresponding file is looked in the directory/run/secretsand has the lower case field name as filename. If a corresponding file is found, the file will take precedence over an environment variable. For the example above, theread()method looks for the environment variablesUSERNAME,DEBUG, andWORKERS, respectively the files/run/secrets/username,/run/secrets/debug, and/run/secrets/workers.TypesVariables are typed using the dataclasses. Primitive types such asint,float, orstrare automatically converted while reading. For more complex types, a conversion function needs to be provided. If fields are typed asOptional, their value will be set toNoneif no variable is found. If a default value is defined, this value will be used when no corresponding environment variable or file is found. Otherwise, when no environment variable is found and the field is not typed asOptional, aRuntimeErroris raised.dotenv (.env) supportenveeprovides rudimentary support for.envfiles. Currently, the path to the.envfile must be specified in theread()method. The name of the variable name must be the upper case name of the field name. Comments and multiline variables are supported. Variables found in a.envfile take precedence over environment variables but not files.The following.envfile can be read using the following Python code:DEBUG="True" # a comment
WORKERS=5
MULTILINE="first
second
3"@envee.environmentclassEnvironment:debug:strworkers:intmultiline:strenv=envee.read(Environment,dotenv_path="/path/to/.env/file")Advanced usageOverride environment variable namesIn the following example the field debug is filled using the environment variablePROJECT_DEBUG.importenvee@envee.environmentclassEnvironment:debug:str=envee.field(env_name="PROJECT_DEBUG")env=envee.read(Environment)Override file locationsThe default location can be changed by passing a different location to theread()method.importenvee@envee.environmentclassEnvironment:debug:strenv=envee.read(Environment,default_files_location="/path/to/a/directory")Alternatively, the fields metadata can specify thefile_locationorfile_name. The parameterfile_locationoverridesdefault_files_location.file_nameoverrides the default file name. The direct path to a file can be specified usingfile_path.file_pathwill take precedence overfile_locationorfile_name.importenvee@envee.environmentclassEnvironment:debug:str=envee.field(file_location="/other/dir",file_name="DEBUG.txt")env=envee.read(Environment)Complex datatypesFor complex datatypes, a conversion function needs to be passed to the field.importenvee@envee.environmentclassEnvironment:timestamp:datetime.datetime=envee.field(conversion_func=lambdax:datetime.datetime.fromisoformat(x))env=envee.read(Environment)