Loader¶
- class django_docker_helpers.config.ConfigLoader(parsers, silent=False, suppress_logs=False, keep_read_records_max=1024)[source]¶
provides a single interface to read from specified config parsers in order they present;
tracks accessed from parsers options;
prints config options access log in pretty-print way.
Example
env = { 'PROJECT__DEBUG': 'false' } parsers = [ EnvironmentParser(scope='project', env=env), RedisParser('my/conf/service/config.yml', host=REDIS_HOST, port=REDIS_PORT), YamlParser(config='./tests/data/config.yml', scope='project'), ] configure = ConfigLoader(parsers=parsers) DEBUG = configure('debug') # 'false' DEBUG = configure('debug', coerce_type=bool) # False
- Initialization:
takes a list of initialized parsers;
it’s supposed to use ONLY unique parsers for
parsers
argument (or you are going to get the same initial arguments for all parsers of the same type infrom_env()
);the parsers’s order does matter.
- Parameters
parsers (
List
[BaseParser
]) – a list of initialized parserssilent (
bool
) – don’t raise exceptions if any read attempt failedsuppress_logs (
bool
) – don’t display any exception warnings on screenkeep_read_records_max (
int
) – max capacity queue length
- format_config_read_queue(use_color=False, max_col_width=50)[source]¶
Prepares a string with pretty printed config read queue.
- Parameters
use_color (
bool
) – use terminal colorsmax_col_width (
int
) – limit column width,50
by default
- Return type
str
- Returns
- static from_env(parser_modules=('django_docker_helpers.config.backends.EnvironmentParser', 'django_docker_helpers.config.backends.MPTRedisParser', 'django_docker_helpers.config.backends.MPTConsulParser', 'django_docker_helpers.config.backends.RedisParser', 'django_docker_helpers.config.backends.ConsulParser', 'django_docker_helpers.config.backends.YamlParser'), env=None, silent=False, suppress_logs=False, extra=None)[source]¶
Creates an instance of
ConfigLoader
with parsers initialized from environment variables.By default it tries to initialize all bundled parsers. Parsers may be customized with
parser_modules
argument orCONFIG__PARSERS
environment variable. Environment variable has a priority over the method argument.- Parameters
parser_modules (
Union
[List
[str
],Tuple
[str
],None
]) – a list of dot-separated module pathsenv (
Optional
[Dict
[str
,str
]]) – a dict with environment variables, default isos.environ
silent (
bool
) – passed toConfigLoader
suppress_logs (
bool
) – passed toConfigLoader
extra (
Optional
[dict
]) – pass extra arguments to every parser
- Return type
- Returns
an instance of
ConfigLoader
Example:
env = { 'CONFIG__PARSERS': 'EnvironmentParser,RedisParser,YamlParser', 'ENVIRONMENTPARSER__SCOPE': 'nested', 'YAMLPARSER__CONFIG': './tests/data/config.yml', 'REDISPARSER__HOST': 'wtf.test', 'NESTED__VARIABLE': 'i_am_here', } loader = ConfigLoader.from_env(env=env) assert [type(p) for p in loader.parsers] == [EnvironmentParser, RedisParser, YamlParser] assert loader.get('variable') == 'i_am_here', 'Ensure env copied from ConfigLoader' loader = ConfigLoader.from_env(parser_modules=['EnvironmentParser'], env={})
- get(variable_path, default=None, coerce_type=None, coercer=None, required=False, **kwargs)[source]¶
Tries to read a
variable_path
from each of the passed parsers. It stops if read was successful and returns a retrieved value. If none of the parsers contain a value for the specified path it returnsdefault
.- Parameters
variable_path (
str
) – a path to variable in configdefault (
Optional
[Any
]) – a default value ifvariable_path
is not present anywherecoerce_type (
Optional
[Type
]) – cast a result to a specified typecoercer (
Optional
[Callable
]) – perform the type casting with specified callbackrequired (
bool
) – raiseRequiredValueIsEmpty
if nodefault
and no resultkwargs – additional options to all parsers
- Returns
the first successfully read value from the list of parser instances or
default
- Raises
config.exceptions.RequiredValueIsEmpty – if nothing is read,``required`` flag is set, and there’s no
default
specified
- static import_parsers(parser_modules)[source]¶
Resolves and imports all modules specified in
parser_modules
. Short names from the local scope are supported (the scope isdjango_docker_helpers.config.backends
).- Parameters
parser_modules (
Iterable
[str
]) – a list of dot-separated module paths- Return type
Generator
[Type
[BaseParser
],None
,None
]- Returns
a generator of [probably]
BaseParser
Example:
parsers = list(ConfigLoader.import_parsers([ 'EnvironmentParser', 'django_docker_helpers.config.backends.YamlParser' ])) assert parsers == [EnvironmentParser, YamlParser]
- static load_parser_options_from_env(parser_class, env=None)[source]¶
Extracts arguments from
parser_class.__init__
and populates them from environment variables.Uses
__init__
argument type annotations for correct type casting.Note
Environment variables should be prefixed with
<UPPERCASEPARSERCLASSNAME>__
.- Parameters
parser_class (
Type
[BaseParser
]) – a subclass ofBaseParser
env (
Optional
[Dict
[str
,str
]]) – a dict with environment variables, default isos.environ
- Return type
Dict
[str
,Any
]- Returns
parser’s
__init__
arguments dict mapping
Example:
env = { 'REDISPARSER__ENDPOINT': 'go.deep', 'REDISPARSER__HOST': 'my-host', 'REDISPARSER__PORT': '66', } res = ConfigLoader.load_parser_options_from_env(RedisParser, env) assert res == {'endpoint': 'go.deep', 'host': 'my-host', 'port': 66}
- class django_docker_helpers.config.ConfigReadItem(variable_path, value, type, is_default, parser_name)¶
Create new instance of ConfigReadItem(variable_path, value, type, is_default, parser_name)
- property is_default¶
Alias for field number 3
- property parser_name¶
Alias for field number 4
- property type¶
Alias for field number 2
- property value¶
Alias for field number 1
- property variable_path¶
Alias for field number 0