config_reader¶
A configuration reader which reads values stored in two levels of keys. The first level is named section and the second level key.
argparse arguments (argparse): (You have to specify a mapping)
mapping = {
'section.key': 'args_attribute'
}
A python dictionary (dictonary):
{
'section': {
'key': 'value'
}
}
Environment variables (environ):
export prefix__section__key=value
INI file (ini):
[section]
key = value
-
class
jflib.config_reader.
ArgparseReader
(args, mapping={})[source]¶ This class tries to read configuration values from a argparse namespace object. This works fine if your section is one word long (–section-key = args.section_key = section + key) and not more than one word long (–my-section-key = args.my_section_key = my + section_key). By multi word section you have to specify a mapping ({‘my_section.key’: ‘my_section_key’}). Without a mapping all sections and keys are convert into lowercase (Section = section).
- Parameters
-
get
(section, key)[source]¶ Get a configuration value stored under a section and a key.
- Parameters
- Raises
ConfigValueError – Configuration value couldn’t be found.
- Return type
- Returns
The configuration value stored under a section and a key.
-
class
jflib.config_reader.
ConfigReader
(spec={}, **kwargs)[source]¶ Available readers: argparse, dictionary, environ, ini.
The arguments of this class have to be specified as keyword arguments. Each keyword stands for a configuration reader class. The order of the keywords is important. The first keyword, more specifically the first reader class, overwrites the next ones.
- Parameters
spec (
dict
) –A dictionary like this example:
spec = { 'section_1': { 'key_1': { 'description': 'Lorem ipsum', 'default': 123, 'not_empty': True, } } }
argparse (tuple) – A tuple (args, mapping). args: The parsed argparse object (Namespace). mapping: A dictionary like this one: {‘section.key’: ‘dest’}. dest are the propertiy name of the args object. or only the argparse object (Namespace).
dictonary (dict) – A two dimensional nested dictionary {‘section’: {‘key’: ‘value’}}
environ (str) – The prefix of the environment variables.
ini (str) – The path of the INI file.
-
check_section
(section, not_empty=False)[source]¶ Check all keys of a section.
- Raises
ValueError – If the value is not configured and can not be read by the readers.
ValueError – If not_empty is true and value is empty.
KeyError – By an unspecify section
- Return type
True
-
reader
¶
-
spec
¶ The specification dictionary. For more informations look at the class arguments of this class.
-
class
jflib.config_reader.
DictionaryReader
(dictionary)[source]¶ Useful for default values.
- Parameters
dictionary (
dict
) – A nested dictionary.
-
get
(section, key)[source]¶ Get a configuration value stored under a section and a key.
- Parameters
- Raises
ConfigValueError – Configuration value couldn’t be found.
- Return type
- Returns
The configuration value stored under a section and a key.
-
class
jflib.config_reader.
EnvironReader
(prefix=None)[source]¶ Read configuration values from environment variables. The name of the environment variables have to be in the form prefix__section__key. Note the two following underscores.
-
get
(section, key)[source]¶ Get a configuration value stored under a section and a key.
- Parameters
- Raises
ConfigValueError – Configuration value couldn’t be found.
- Return type
- Returns
The configuration value stored under a section and a key.
-
-
class
jflib.config_reader.
IniReader
(path)[source]¶ Read configuration files from text files in the INI format.
- Parameters
path (
str
) – The path of the INI file.
-
get
(section, key)[source]¶ Get a configuration value stored under a section and a key.
- Parameters
- Raises
ConfigValueError – Configuration value couldn’t be found.
- Return type
- Returns
The configuration value stored under a section and a key.
-
class
jflib.config_reader.
ReaderSelector
(*readers)[source]¶ Select for each get request which reader to use.
-
readers
¶ A list of readers.
-
-
class
jflib.config_reader.
SpecReader
(spec)[source]¶ Read the default values from the spec (specification) dictionary.
- Parameters
spec (
dict
) – The spec (specification) dictionary.
-
get
(section, key)[source]¶ Get a configuration value stored under a section and a key.
- Parameters
- Raises
ConfigValueError – Configuration value couldn’t be found.
- Return type
- Returns
The configuration value stored under a section and a key.
-
jflib.config_reader.
load_readers_by_keyword
(**kwargs)[source]¶ Available readers: argparse, dictionary, environ, ini.
The arguments of this class have to be specified as keyword arguments. Each keyword stands for a configuration reader class. The order of the keywords is important. The first keyword, more specifically the first reader class, overwrites the next ones.
- Parameters
argparse (tuple) – A tuple (args, mapping). args: The parsed argparse object (Namespace). mapping: A dictionary like this one: {‘section.key’: ‘dest’}. dest are the propertiy name of the args object. or only the argparse object (Namespace).
dictonary (dict) – A two dimensional nested dictionary {‘section’: {‘key’: ‘value’}}
environ (str) – The prefix of the environment variables.
ini (str) – The path of the INI file.
- Return type