Environment Parser

class django_docker_helpers.config.backends.environment_parser.EnvironmentParser(scope=None, config=None, nested_delimiter='__', path_separator='.', env=None)[source]

Bases: django_docker_helpers.config.backends.base.BaseParser

Provides a simple interface to read config options from environment variables.

Example:

from json import loads as json_load
from yaml import load as yaml_load

env = {
    'MY__VARIABLE': '33',
    'MY__NESTED__YAML__LIST__VARIABLE': '[33, 42]',
    'MY__NESTED__JSON__DICT__VARIABLE': '{"obj": true}',
}

parser = EnvironmentParser(env=env)
assert p.get('my.variable') == '33'

assert p.get('my.nested.yaml.list.variable',
             coerce_type=list, coercer=yaml_load) == [33, 42]
assert p.get('my.nested.json.dict.variable',
             coerce_type=dict, coercer=json_load) == {'obj': True}

parser = EnvironmentParser(env=env, scope='my.nested')
assert parser.get('yaml.list.variable',
                  coerce_type=list, coercer=yaml_load) == [33, 42]
Parameters:
  • scope (Optional[str]) – a global namespace-like variable prefix
  • config (Optional[str]) – not used
  • nested_delimiter (str) – replace path_separator with an appropriate environment variable delimiter, default is __
  • path_separator (str) – specifies which character separates nested variables, default is '.'
  • env (Optional[Dict[str, str]]) – a dict with environment variables, default is os.environ
__str__()[source]

Return str(self).

get(variable_path, default=None, coerce_type=None, coercer=None, **kwargs)[source]

Reads a value of variable_path from environment.

If coerce_type is bool and no coercer specified, coerces forced to be coerce_str_to_bool()

Parameters:
  • variable_path (str) – a delimiter-separated path to a nested value
  • default (Optional[Any]) – default value if there’s no object by specified path
  • coerce_type (Optional[Type[+CT_co]]) – cast a type of a value to a specified one
  • coercer (Optional[Callable]) – perform a type casting with specified callback
  • kwargs – additional arguments inherited parser may need
Returns:

value or default

get_client()[source]

If your backend needs a client, inherit this method and use client() shortcut.

Returns:an instance of backend-specific client