config#
Functions for handling configuration files and environment variables.
Functions#
|
Attempt to get a configuration value from |
|
Attempt to get a configuration value from |
|
Get a configuration variable from |
Module Contents#
- try_get_val_from_dict(key: str, config_dict: dict, value_name: str = None) tuple[str, str]#
Attempt to get a configuration value from a configuration dictionary. Return
Nonealong with an informative failure message if this is not possible.Parameters#
- key
Key under which to look up the value in the
config_dict.- config_dict
Dictionary of configuration keys and values in which to look up the value.
- value_name
Descriptive name for the configuration value, to allow more readable failure messages when the
keycannot be found inconfig_dict. IfNone, use the value ofkey. DefaultNone.
Returns#
- tuple[str, str]
A tuple. Its first entry is the value if it was successfully retrieved and otherwise
None. Its second entry isNoneon success and otherwise a description of the failure, as a string.
- try_get_val_from_env(env_variable_name: str, value_name: str = None) tuple[str, str]#
Attempt to get a configuration value from local environment variables. Return
Nonealong with an informative failure message if this is not possible.Parameters#
- env_variable_name
Name of the environment variable to attempt to retrieve.
- value_name
Descriptive name for the value, for more readable failure messages when the
env_variable_namecannot be located. IfNone, use the value ofenv_variable_name. DefaultNone.
Returns#
- tuple[str, str]
A tuple. Its first entry is the value of the variable if it was successfully retrieved and otherwise
None. Its second entry isNoneon success and otherwise a description of the failure, as a string.
- get_config_val(key: str, config_dict: dict = None, try_env: bool = True, env_variable_name: str = None, value_name: str = None) str#
Get a configuration variable from a configuration dictionary and/or from local environment variables.
First consult a configuration dictionary, if one has been provided. Then consult environment variables if directed to do so. If no valid value can be found, raise a
ValueError.Parameters#
- key
Key under which to look up the value in the
config_dict, if one is provided, and variable name to check in the environment variables, if a distinctenv_variable_nameis not provided.- config_dict
Dictionary of configuration keys and values in which to look up the value. If
None, only look in environment variables, providedtry_envisTrue.- try_env
Look for the value in the environment variables if it cannot be found in the
config_dict? DefaultTrue. IfTrue, with look for an environment variable corresponding tokey, but with that string converted to uppercase, (i.e. the output ofkey.upper()), unless a custom name is provided via theenv_variable_nameargument.- env_variable_name
Environmental variable name to check for the variable, if it cannot be found in the config. If
Noneandtry_envisTrue, use the value ofkey.upper().- value_name
Descriptive name for the configuration value, to produce more informative error messages when a valid value cannot be found. If
None, use the value ofkey. DefaultNone.
Returns#
- str
The configuration value.
Raises#
- ValueError
If the value cannot be found either in the configuration dictionary or in an environment variable, or if
config_dictisNoneandtry_envisFalse