Loader¶
-
class
django_docker_helpers.config.ConfigLoader(parsers, silent=False, suppress_logs=False, keep_read_records_max=1024)[source]¶ Bases:
object- 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
parsersargument (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 parsers - silent (
bool) – don’t raise exceptions if any read attempt failed - suppress_logs (
bool) – don’t display any exception warnings on screen - keep_read_records_max (
int) – max capacity queue length
-
__call__(variable_path, default=None, coerce_type=None, coercer=None, required=False, **kwargs)[source]¶ A useful shortcut for method
get()
-
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 colors - max_col_width (
int) – limit column width,50by default
Return type: strReturns: - use_color (
-
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
ConfigLoaderwith parsers initialized from environment variables.By default it tries to initialize all bundled parsers. Parsers may be customized with
parser_modulesargument orCONFIG__PARSERSenvironment 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 paths - env (
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
ConfigLoaderExample:
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={})
- parser_modules (
-
get(variable_path, default=None, coerce_type=None, coercer=None, required=False, **kwargs)[source]¶ Tries to read a
variable_pathfrom 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 config - default (
Optional[Any]) – a default value ifvariable_pathis not present anywhere - coerce_type (
Optional[Type[+CT_co]]) – cast a result to a specified type - coercer (
Optional[Callable]) – perform the type casting with specified callback - required (
bool) – raiseRequiredValueIsEmptyif nodefaultand no result - kwargs – additional options to all parsers
Returns: the first successfully read value from the list of parser instances or
defaultRaises: config.exceptions.RequiredValueIsEmpty – if nothing is read,``required`` flag is set, and there’s no
defaultspecified- variable_path (
-
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 pathsReturn type: Generator[Type[BaseParser],None,None]Returns: a generator of [probably] BaseParserExample:
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 mappingExample:
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}
- parser_class (
-
class
django_docker_helpers.config.ConfigReadItem(variable_path, value, type, is_default, parser_name)¶ Bases:
tupleCreate new instance of ConfigReadItem(variable_path, value, type, is_default, parser_name)
-
__getnewargs__()¶ Return self as a plain tuple. Used by copy and pickle.
-
static
__new__(_cls, variable_path, value, type, is_default, parser_name)¶ Create new instance of ConfigReadItem(variable_path, value, type, is_default, parser_name)
-
__repr__()¶ Return a nicely formatted representation string
-
_asdict()¶ Return a new OrderedDict which maps field names to their values.
-
classmethod
_make(iterable, new=<built-in method __new__ of type object>, len=<built-in function len>)¶ Make a new ConfigReadItem object from a sequence or iterable
-
_replace(**kwds)¶ Return a new ConfigReadItem object replacing specified fields with new values
-
is_default¶ Alias for field number 3
-
parser_name¶ Alias for field number 4
-
type¶ Alias for field number 2
-
value¶ Alias for field number 1
-
variable_path¶ Alias for field number 0
-