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
  • args (Namespace) – The parsed argparse object.

  • mapping (Dict[str, str]) – A dictionary like this one: {‘section.key’: ‘dest’}. dest is the property name of the args object.

get(section, key)[source]

Get a configuration value stored under a section and a key.

Parameters
  • section (str) – Name of the section.

  • key (str) – Name of the key.

Raises

ConfigValueError – Configuration value couldn’t be found.

Return type

Any

Returns

The configuration value stored under a section and a key.

class jflib.config_reader.ClassInterface(reader)[source]
class jflib.config_reader.ClassInterfaceKey(reader, section)[source]
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
  • 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

bool

get_class_interface()[source]
Return type

ClassInterface

get_dictionary_interface()[source]
Return type

DictionaryInterface

reader: ReaderBase

ReaderSelector

spec: Dict[str, Dict[str, KeySpec]]

The specification dictionary. For more informations look at the class arguments of this class.

spec_to_argparse(parser)[source]
Return type

None

exception jflib.config_reader.ConfigValueError[source]

Configuration value can’t be found.

class jflib.config_reader.DictionaryInterface(reader)[source]
class jflib.config_reader.DictionaryInterfaceKey(reader, section)[source]
class jflib.config_reader.DictionaryReader(dictionary)[source]

Useful for default values.

Parameters

dictionary (Dict[str, Any]) – A nested dictionary.

get(section, key)[source]

Get a configuration value stored under a section and a key.

Parameters
  • section (str) – Name of the section.

  • key (str) – Name of the key.

Raises

ConfigValueError – Configuration value couldn’t be found.

Return type

Any

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.

Parameters

prefix (Optional[str]) – A enviroment prefix

get(section, key)[source]

Get a configuration value stored under a section and a key.

Parameters
  • section (str) – Name of the section.

  • key (str) – Name of the key.

Raises

ConfigValueError – Configuration value couldn’t be found.

Return type

Any

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
  • section (str) – Name of the section.

  • key (str) – Name of the key.

Raises

ConfigValueError – Configuration value couldn’t be found.

Return type

Any

Returns

The configuration value stored under a section and a key.

exception jflib.config_reader.IniReaderError[source]

Ini file not valid.

class jflib.config_reader.KeySpec(*args, **kwargs)[source]
default: Any
description: str
not_empty: bool
jflib.config_reader.Mapping

A dictionary like this one: {‘section.key’: ‘dest’}. dest is the property name of the args object.

alias of Dict[str, str]

class jflib.config_reader.ReaderBase[source]

Base class for all readers

abstract get(section, key)[source]
Return type

Any

class jflib.config_reader.ReaderSelector(*readers)[source]

Select for each get request which reader to use.

get(section, key)[source]

Get a configuration value stored under a section and a key.

Parameters
  • section (str) – Name of the section.

  • key (str) – Name of the key.

readers

A list of readers.

jflib.config_reader.Spec

A dictionary like this example:

spec = {
    'section_1': {
        'key_1': {
            'description': 'Lorem ipsum',
            'default': 123,
            'not_empty': True,
        }
    }
}

alias of Dict[str, Dict[str, KeySpec]]

class jflib.config_reader.SpecReader(spec)[source]

Read the default values from the spec (specification) dictionary.

Parameters

spec (Dict[str, Dict[str, KeySpec]]) – The spec (specification) dictionary.

get(section, key)[source]

Get a configuration value stored under a section and a key.

Parameters
  • section (str) – Name of the section.

  • key (str) – Name of the key.

Raises

ConfigValueError – Configuration value couldn’t be found.

Return type

Any

Returns

The configuration value stored under a section and a key.

jflib.config_reader.auto_type(value)[source]

https://stackoverflow.com/a/7019325

Return type

Any

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

List[ReaderBase]

jflib.config_reader.validate_key(key)[source]
Parameters

key (str) – Validate the name of a section or a key.

Return type

bool