Skip to main content

Module for defining a config schema and importing it from environment variables.

Project description

anenvconf

A python3 module for defining a config schema and importing it from environment variables.

Install

python3 -m pip install anenvconf

Usage

Import the modules key classes, define the config schema and create a Config object with the schema.

from anenvconf import Config, ConfigValueType
config_schema = {
  'jsonvar1': {
    'type': ConfigValueType.JSON,
    'required_key': 'important_key'
  },
  'stringvar1': {},
  'stringvar2': {
    'default': "Default value in case setting this variable is not mandatory"
  },
  'boolvar1': {
    'type': ConfigValueType.BOOL,
    'default': True
  }
}

config = None

try:
  config = Config(config_schema)

except anenvconf.exceptions.InvalidValueException as e:
  print("Got an invalid value: ", e.text)

except anenvconf.exceptions.EnvironmentVarMissingException as e:
  print("A required environment variable is missing: ", e.text)

except anenvconf.exceptions.RequiredVarMissingException as e:
  print("A required value inside a variable is missing: ", e.text)

if config is not None:
  print(config.get_value("stringvar2"))

Schema

The schema is given as a dict, where the keys are the environment variables to read, and each of their values is an another dict that defines details about how to handle that variable. The dict that defines the handling can have the following keys:

type is the data type of the variable value, defined as ConfigValueType. Supported types are str (default), bool, intand json.

default is the default value of the variable. If defaultis not set, the environment variable is treated as mandatory. If you want to have optional variables, you can for example set default to None.

required_key defines a key that is required in the keys of a json type of value. If the key is not found, an exception is raised.

Example:

example_config_schema = {
  # Use "type" to define ConfigValueType, string is assumed by default.
  'env_variable_name1': {
    'type': ConfigValueType.JSON
  },

   # Default data type is string
  'env_variable_name2': {},

  # If default value exists, value from environment variable is not required.
  # Otherwise the key-value pair is expected to be found in the env variables.
  'env_variable_name3': {
    'type': ConfigValueType.JSON,
    'default': '{}'
  }
}

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

anenvconf-1.0.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

anenvconf-1.0.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file anenvconf-1.0.2.tar.gz.

File metadata

  • Download URL: anenvconf-1.0.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for anenvconf-1.0.2.tar.gz
Algorithm Hash digest
SHA256 09dfc6fa9c1b2ad41d4d1a46a1e1c48cad64cf1a62184cb7f5b0e49b822f24a6
MD5 455f704a08a7fa58ed9fea195e460c8e
BLAKE2b-256 ca27aca3945fb18f48d2a13cb7256b311e9ea8169e395de59b30360d9c600f88

See more details on using hashes here.

File details

Details for the file anenvconf-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: anenvconf-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for anenvconf-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da9dd79989b8b78416de74a813f3d2b05b628ff68f9f9cf08bc9904ff4c9456b
MD5 223983053022c1062ce35ffc133bc841
BLAKE2b-256 6da71ccf48fb21699b023da3d2154fe4bcea17ccc534e6bece417726c7fef644

See more details on using hashes here.

Supported by

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