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:
>>> 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
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
kwargs : dict
interpolation : Interpolation instance
|
---|
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
option : string
use_default : bool
|
---|---|
Returns: | value : list of lists
|
Raises: | NoOptionError
|
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]]
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
option : string
use_default : bool
|
---|---|
Returns: | value : list of lists
|
Raises: | NoOptionError
|
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 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)
In python 2 replaces the standard interpolation with an instance derived from Interpolation. This method is never called by python 3
The implementation of the interpolation objects has been copied from the python 3.5 development branch and adapted to work on python 2.7.
Dummy interpolation that passes the value through with no changes.
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.
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.