evarify: Transform and Validate Environment Variables¶
evarify is a MIT Licensed Python library that provides some simple transformations and validators for environment variables.
For example, let’s say you are writing a daemon that has an environment
variable, SEED_SERVERS
that expects a comma-separated list of IP addresses.
evarify helps ensure that the value is being passed, that it is in the
expected format, and it will do the conversion to a list for you
from evarify import ConfigStore, EnvironmentVariable
from evarify.filters.python_basics import comma_separated_to_set, \
validate_is_boolean_true
settings = ConfigStore({
'SEED_SERVERS': EnvironmentVariable(
name='SEED_SERVERS',
filters=[comma_separated_to_set, validate_is_boolean_true],
),
})
settings.load_values()
# Assuming our environment variable was ``SEED_SERVERS=192.168.1.50,192.168.1.51``
>>> settings['SEED_SERVERS']
['192.168.1.50', '192.168.1.51']
All functions specified in the filters
keyword are ran in order against
the environment variable’s value. In the example above, we were able to
transform the input and validate it quickly and easily.
User Guide¶
Installation¶
This document covers the installation of evarify.
Distribute & Pip¶
Installing Requests is simple with pip, just run this in your terminal:
$ pip install evarify
GitHub¶
evarify is developed on GitHub, where you can always retrieve the latest version of the code.
You can either clone the public repository:
$ git clone git://github.com/gtaylor/evarify.git
Download the tarball:
$ curl -OL https://github.com/gtaylor/evarify/tarball/master
Or, download the zipball:
$ curl -OL https://github.com/gtaylor/evarify/zipball/master
Once you have a copy of the source, you can embed it in your Python package, or install it into your site-packages easily:
$ python setup.py install
Getting Started¶
This document will walk you through what you need to know in order to get started with using evarify.
ConfigStore and EnvironmentVariable¶
First, create a new module within your application that will hold your
configuration. We suggest something like config.py
.
Tip
You’ll want to be careful when importing any of your project’s other
modules from within config.py
so you won’t run into circular imports.
Here’s a good starting point:
from evarify import ConfigStore, EnvironmentVariable
from evarify.filters.python_basics import value_to_python_log_level, \
value_to_bool
settings = ConfigStore({
'LOGLEVEL': EnvironmentVariable(
name='LOGLEVEL',
help_txt='The desired logging level (DEBUG|INFO|WARN|ERROR).',
is_required=False,
default_val='INFO',
filters=[value_to_python_log_level],
)
})
You’ll notice that the top-level container is the
ConfigStore
class. This will hold all of your
EnvironmentVariable
definitions,
and eventually your loaded config values. See the API reference for each
of those classes for more details on what the arguments mean.
Loading Config Values from Environment Variables¶
The next step is to go to the entrypoint module for your project and import and load our config values:
from mymodule.config import settings
settings.load_values()
This causes the magic to happen. We iterate through your
EnvironmentVariable
definitions,
pull the values, run them through the filters, and set the corresponding
dict key in your ConfigStore
. You can
then reference it with the dict API:
>>> import logging
>>> from mymodule.config import settings
>>> settings.load_values()
>>> assert settings['LOGGING'] == logging.INFO
Filters¶
An important part of our
EnvironmentVariable
definitions the
filters
param. This is a list of filter functions to pass the environment
variable’s value through before storing the result in the
ConfigStore
. They fit the following
signature:
def your_filter(config_val, evar):
"""
:param str config_val: The env var value.
:param EnvironmentVariable evar: The EVar object we are validating
a value for.
:raises: ValueError if there are any issues with the value.
"""
# Your logic here. You can modify the config_val before returning.
return config_val
The environment variable’s values will be passed through these filters in order, so be sure to arrange things accordingly.
You can easily write your own filters, or use any of our built-ins. See the
evarify.filters.python_basics
API reference for a list.
Gotchas¶
- If an
EnvironmentVariable
has been set torequired=True
(the default), you must define the environment variable when running your application (even if it’s an empty value). - If
EnvironmentVariable
has been set torequired=False
, failing to define the environment variable will result in thedefault_val
being used. If nodefault_val
has been passed in, we default toNone
.
API Reference¶
evarify.ConfigStore¶
-
class
evarify.
ConfigStore
(evar_defs)[source]¶ This is the container for your
EnvironmentVariable
definitions, along with their eventual loaded config values. Onceload_values()
is ran on an instance of this class, the config values are addressable via the Python dict API.-
__init__
(evar_defs)[source]¶ Parameters: evar_defs (dict) – Pass in a dict whose keys are config names and the values are EnvironmentVariable
instances.
-
__weakref__
¶ list of weak references to the object (if defined)
-
evarify.EnvironmentVariable¶
-
class
evarify.
EnvironmentVariable
(name, is_required=True, default_val=None, filters=None, help_txt=None)[source]¶ Defines an Environment Variable to handle.
-
__init__
(name, is_required=True, default_val=None, filters=None, help_txt=None)[source]¶ Parameters: - name (str) – The name of the environment variable. This is case-sensitive!
- is_required (bool) – If
True
, this variable must be defined when your Python process starts. IfFalse
, the default loaded value will matchdefault_val
. - default_val – If
is_required
isFalse
and this environment variable is not defined, this value will be loaded. - filters (list) – A list of functions to pass the environment variable’s value (or default value) through. Order is significant!
- help_txt (str) – Optional help text describing the environment variable.
-
__weakref__
¶ list of weak references to the object (if defined)
-
evarify.filters.python_basics¶
-
evarify.filters.python_basics.
comma_separated_str_to_list
(config_val, evar)[source]¶ Splits a comma-separated environment variable into a list of strings.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Return type: Returns: The equivalent list for a comma-separated string.
-
evarify.filters.python_basics.
comma_separated_to_set
(config_val, evar)[source]¶ Splits a comma-separated environment variable into a set of strings.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Return type: Returns: The equivalent set for a comma-separated string.
-
evarify.filters.python_basics.
validate_is_boolean_true
(config_val, evar)[source]¶ Make sure the value evaluates to boolean True.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Raises: ValueError if the config value evaluates to boolean False.
-
evarify.filters.python_basics.
validate_is_not_none
(config_val, evar)[source]¶ If the value is
None
, fail validation.Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Raises: ValueError if the config value is None.
-
evarify.filters.python_basics.
value_to_bool
(config_val, evar)[source]¶ Massages the ‘true’ and ‘false’ strings to bool equivalents.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Return type: Returns: True or False, depending on the value.
-
evarify.filters.python_basics.
value_to_int
(config_val, evar)[source]¶ Convert the value to int.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Return type:
-
evarify.filters.python_basics.
value_to_none
(config_val, evar)[source]¶ Given a value that evaluates to a boolean False, return None.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Return type: str or None
Returns: Either the non-False value or None.
-
evarify.filters.python_basics.
value_to_python_log_level
(config_val, evar)[source]¶ Convert an evar value into a Python logging level constant.
Parameters: - config_val (str) – The env var value.
- evar (EnvironmentVariable) – The EVar object we are validating a value for.
Returns: A validated string.
Raises: ValueError if the log level is invalid.
Community Guide¶
Support¶
If you have questions or issues about evarify, here’s what is currently available:
File an Issue¶
If you run into unexpected behavior or have ideas for new features, file an issue on on GitHub.