Environment Parser¶
- class django_docker_helpers.config.backends.environment_parser.EnvironmentParser(scope=None, config=None, nested_delimiter='__', path_separator='.', env=None)[source]¶
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 prefixconfig (
Optional
[str
]) – not usednested_delimiter (
str
) – replacepath_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 isos.environ
- get(variable_path, default=None, coerce_type=None, coercer=None, **kwargs)[source]¶
Reads a value of
variable_path
from environment.If
coerce_type
isbool
and nocoercer
specified,coerces
forced to becoerce_str_to_bool()
- Parameters
variable_path (
str
) – a delimiter-separated path to a nested valuedefault (
Optional
[Any
]) – default value if there’s no object by specified pathcoerce_type (
Optional
[Type
]) – cast a type of a value to a specified onecoercer (
Optional
[Callable
]) – perform a type casting with specified callbackkwargs – additional arguments inherited parser may need
- Returns
value or default