Skip to main content

Read variables from environment or files into dataclasses

Project description

envee

Build status

Read 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. The envee library 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.

Usage

Variables to read from the environment are declared using classes annotated with the @envee.environment decorator. Using envee.read() the fields of the classes are filled using the environment.

Example:

from typing import Optional
import envee

@envee.environment
class Environment:
    username: str
    debug: Optional[str]
    workers: int = 5

env = envee.read(Environment)

Environment variables names and file paths

For each field, per default envee looks for environment variables with the upper case name of the field. The corresponding file is looked in the directory /run/secrets and 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, the read() method looks for the environment variables USERNAME, DEBUG, and WORKERS, respectively the files /run/secrets/username, /run/secrets/debug, and /run/secrets/workers.

Types

Variables are typed using the dataclasses. Primitive types such as int, float, or str are automatically converted while reading. For more complex types, a conversion function needs to be provided. If fields are typed as Optional, their value will be set to None if 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 as Optional, a RuntimeError is raised.

dotenv (.env) support

envee provides rudimentary support for .env files. Currently, the path to the .env file must be specified in the read() 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 .env file take precedence over environment variables but not files.

The following .env file can be read using the following Python code:

DEBUG="True" # a comment
WORKERS=5
MULTILINE="first
second
3"
@envee.environment
class Environment:
    debug: str
    workers: int
    multiline: str

env = envee.read(Environment, dotenv_path="/path/to/.env/file")

Advanced usage

Override environment variable names

In the following example the field debug is filled using the environment variable PROJECT_DEBUG.

import envee

@envee.environment
class Environment:
    debug: str = envee.field(env_name="PROJECT_DEBUG")

env = envee.read(Environment)

Override file locations

The default location can be changed by passing a different location to the read() method.

import envee

@envee.environment
class Environment:
    debug: str

env = envee.read(Environment, default_files_location="/path/to/a/directory")

Alternatively, the fields metadata can specify the file_location or file_name. The parameter file_location overrides default_files_location. file_name overrides the default file name. The direct path to a file can be specified using file_path. file_path will take precedence over file_location or file_name.

import envee

@envee.environment
class Environment:
    debug: str = envee.field(file_location="/other/dir", file_name="DEBUG.txt")

env = envee.read(Environment)

Complex datatypes

For complex datatypes, a conversion function needs to be passed to the field.

import envee

@envee.environment
class Environment:
    timestamp: datetime.datetime = envee.field(
        conversion_func=lambda x: datetime.datetime.fromisoformat(x)
    )


env = envee.read(Environment)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envee-0.1.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envee-0.1.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file envee-0.1.2.tar.gz.

File metadata

  • Download URL: envee-0.1.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for envee-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4ef60bd765951c33d31c098ce791ff274433fec55ad99dcf0b6189a2213c1e9c
MD5 ba65a6b4d7c34824b8e283fadd1a9599
BLAKE2b-256 f4dde221228b7aabe91f3f6e613be5853f51bb5028a6c1b25ab2f1756e94d081

See more details on using hashes here.

File details

Details for the file envee-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: envee-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for envee-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6183bbc1f1b6b2917e8ece1a8eba928fdba9a16a82d8cf03c57b494a737b04f7
MD5 8219d446e888571963961c652cff1759
BLAKE2b-256 bcd54d73c6f3c989beac822544282f16a248a7921dc997cc684f439466284caa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page