tools.configuration – Configuration parser

Configuration set up

The custom ConfigParser provides two new functionalities to parse lists:

Also it extends the python 2.x configuration parser with features coming from python 3:

Examples

>>> import pyhetdex.tools.configuration as pconf
>>> # This can be imported only after `pyhetdex.tools.configuration` has been
>>> # imported
>>> from six.moves import BasicInterpolation
>>> from six.moves import ExtendedInterpolation
>>> # standard config parser interpolation
>>> stdparser = pconf.ConfigParser()
>>> # equivalent
>>> stdparser = pconf.ConfigParser(interpolation=BasicInterpolation())
>>> # extended config parser interpolation
>>> extparser = pconf.ConfigParser(interpolation=ExtendedInterpolation())
# Configuration file: default interpolation
[general]
dir1 = /path/to
[section]
dir1 = /path/to
file1 = %(dir1)/file1

# Configuration file: extended interpolation
[general]
dir1 = /path/to
[section]
file1 = %{general:dir1}/file1

The configuration parser

class pyhetdex.tools.configuration.ConfigParser(*args, **kwargs)[source]

Bases: ConfigParser.ConfigParser

Customise configuration parser

For pyhton 3 all the args and kwargs are passed to the constructor of the parent class. For python 2 the extra keyword interpolation is added.

Parameters:

args : list

arguments passed to the parent class

kwargs : dict

keyword arguments passed to the parent class

interpolation : Interpolation instance

only for python 2: select which interpolation to use

get_list_of_list(section, option, use_default=False)[source]

A convenience method which coerces the option in the specified section to a list of lists. If the options is empty returns [[None, None]]

Parameters:

section : string

name of the section

option : string

name of the option

use_default : bool

whether default to [[None, None]]

Returns:

value : list of lists

parsed option

Raises:

NoOptionError

if the option doesn’t exist and no default required

Examples

>>> # cat settings.cfg:
>>> # [section]
>>> # wranges_bkg = 3500-4500,4500-5500
>>> conf = ConfigParser()
>>> conf.read_dict({"section": {"wranges_bkg": "3500-4500,4500-5500"}})
>>> conf.get_list_of_list("section", "wranges_bkg")
[[3500.0, 4500.0], [4500.0, 5500.0]]
>>> conf.get_list_of_list("section", "not_exist")
... 
Traceback (most recent call last):
...
NoOptionError: No option 'not_exist' in section: 'section'
>>> conf.get_list_of_list("section", "not_exist", use_default=True)
[[None, None]]
get_list(section, option, use_default=False)[source]

A convenience method which coerces the option in the specified section to a list. If the options is empty returns the empty list [].

Parameters:

section : string

name of the section

option : string

name of the option

use_default : bool

whether default to []

Returns:

value : list of lists

parsed option

Raises:

NoOptionError

if the option doesn’t exist and no default required

Examples

>>> # cat settings.cfg:
>>> # [section]
>>> # wranges_iq = 3500, 4500, 5500
>>> # literal_list = ['a', 'b', 'c']
>>> conf = ConfigParser()
>>> conf.read_dict({"section": {"wranges_iq": "3500, 4500, 5500",
...                             "literal_list": "['a', 'b', 'c']"}})
>>> conf.get_list("section", "wranges_iq")
[3500, 4500, 5500]
>>> conf.get_list("section", "literal_list")
['a', 'b', 'c']
>>> conf.get_list("section", "not_exist")
... 
Traceback (most recent call last):
...
NoOptionError: No option 'not_exist' in section: 'section'
>>> conf.get_list("section", "not_exist", use_default=True)
[]
read_dict(dictionary, source='<dict>')[source]

Read configuration from a dictionary.

Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order.

All types held in the dictionary are converted to strings during reading, including section names, option names and keys.

Optional second argument is the source specifying the name of the dictionary being read.

Notes

This method provides an implementation (taken from python3.4 configparser module) if the method is not already present in ConfigParser (thus for python <3.2)

_interpolate(section, option, rawval, vars)[source]

In python 2 replaces the standard interpolation with an instance derived from Interpolation. This method is never called by python 3

Reimplement the interpolation

The implementation of the interpolation objects has been copied from the python 3.5 development branch and adapted to work on python 2.7.

class pyhetdex.tools.configuration.Interpolation[source]

Dummy interpolation that passes the value through with no changes.

class pyhetdex.tools.configuration.BasicInterpolation[source]

Bases: pyhetdex.tools.configuration.Interpolation

Interpolation as implemented in the classic python 3 ConfigParser.

The option values can contain format strings which refer to other values in the same section, or values in the special default section.

For example:

something: %(dir)s/whatever

would resolve the %(dir)s to the value of dir. All reference expansions are done late, on demand. If a user needs to use a bare % in a configuration file, she can escape it by writing %%. Other % usage is considered a user error and raises InterpolationSyntaxError.

class pyhetdex.tools.configuration.ExtendedInterpolation[source]

Bases: pyhetdex.tools.configuration.Interpolation

Advanced variant of interpolation, supports the syntax used by zc.buildout. Enables interpolation between sections.

For example:

[general]
dir1 = /path/to
[section]
file1 = %{general:dir1}/file1

would resolve file1 = /path/to/file1.