API Reference¶
Changed in version 0.2.0: Interpolation of values in configuration files now works with default values from the specification.
Easy and simple configuration.
See also: Introduction
Overview¶
Special value for options that are not found and have no default. |
|
Special value for options without a value. |
|
Configuration class. |
|
Raised if there is a problem with the configuration. |
|
Raised if an option already exists in a section. |
|
Base Exception. |
|
Raised on an attempt to set a readonly option. |
|
Raised if there is a problem with the specification. |
|
For an explanation see the Introduction. |
|
Return a function that can be used as a converter. |
|
Return a converter function for logging levels. |
|
Return a converter function with a predicate. |
|
Return a function that can be used as a converter. |
-
easimpconf.
NOTFOUND
¶ Special value for options that are not found and have no default.
The truth value is
False
.
-
easimpconf.
NOVALUE
¶ Special value for options without a value (see Introduction).
The truth value is
True
-
class
easimpconf.
Config
(options, create_props, kwargs)[source]¶ Configuration class.
An instance of this class is returned by the function
configure()
.If
create_properties=True
it will have a property for each configuration option named as explained in the Introduction. For extra data this is just the name given in theadd()
method.Options can also be accessed like this:
config[secname, optname]
. To check if an option exists, thein
operator can be used:(secname, optname) in config
.For extra data either
None
must be used for the section name or only the name of the data, i.e.config[None, name]
andconfig[name]
are equivalent as are(None, name) in config
andname in config
.When a
Config
object is used as an iterator it yields 3-tuples for each option:(secname, optname, value)
.-
add
(key, value, readonly=True)[source]¶ Add extra data to configuration or an option to a section.
- Parameters
key – either a name of extra data or a tuple
('secname', 'optname')
value – the value of the data
readonly (bool) – if
True
the data cannot be changed
- Type
- Raises
DuplicateError – if an option already exists in a section
AttributeError – if an attribute with the same name already exists (only if
create_properties=True
)ConfigError – if
create_properties=True
andname
orsecname_optname
is not a valid Python identifier; the data can still be accessed withcfg['name']
orcfg['secname', 'optname']
-
extras
()[source]¶ Return names of extra data added with
add()
.- Returns
list with names of extra data
- Return type
-
write
(file, space_around_delimiters=True)[source]¶ Write a representation of the configuration to the specified file.
Extra data added with
add()
will be excluded.- Parameters
file (path-like object or text file opened for writing) – the file
space_around_delimiters (bool) – if
True
, delimiters between keys and values are surrounded by spaces
-
-
easimpconf.
configure
(conf, spec, *, create_properties=True, converters=None, **kwargs)[source]¶ For an explanation see the Introduction.
- Parameters
conf (path-like object or text file opened for reading or
ConfigParser
object) – the configurationspec (path-like object or text file opened for reading or
None
) – the specificationcreate_properties (bool) – if
True
properties will be created, else only item access with [sec,opt] can be usedconverters (dict) – same as the
converters
argument ofConfigParser
but used directly by this functionkwargs – arguments for the
ConfigParser
(ignored ifconf
is aConfigParser
object)
- Returns
configuration object
- Return type
- Raises
SpecError – if there is a problem with the specification
ConfigError – if there is a problem with the configuration
configparser.Error – from
ConfigParser
-
easimpconf.
convert_choice
(choices, *, converter=None, default=None)[source]¶ Return a function that can be used as a converter.
For an example see the source code of
convert_loglevel()
.- Parameters
choices – any container type that supports the
in
operator with acceptable valuesconverter – a callable that takes one string argument and returns an object of the desired type;
None
means no conversiondefault – a default value of the desired type or a subclass of
Exception
which will be raised
- Returns
converter function
- Return type
function(str)
-
easimpconf.
convert_loglevel
(default_level=None, *, numeric=False)[source]¶ Return a converter function for logging levels.
Valid values are the logging levels as defined in the
logging
module.- Parameters
- Raises
ValueError – if not a valid logging level and
default_level=None
- Returns
converter function
- Return type
function(str)
-
easimpconf.
convert_predicate
(predicate, *, converter=None, default=None)[source]¶ Return a converter function with a predicate.
>>> positive_float = convert_predicate(lambda x: x > 0.0, ... converter=float, default=0.0) >>> positive_float('1.2') 1.2 >>> positive_float('-1.2') 0.0
- Parameters
predicate – a callable that takes one argument of the desired type and returns
True
if it is acceptableconverter – a callable that takes one string argument and returns an object of the desired type;
None
means no conversiondefault – a default value of the desired type or a subclass of
Exception
which will be raised instead
- Returns
converter function
- Return type
function(str)
-
easimpconf.
convert_string
(*, start='|', newlines=True)[source]¶ Return a function that can be used as a converter.
The default converter
str
handles multiline values likeConfigParser
, i.e. preserving newlines but ignoring indentations (because nothing gets realy converted).A converter returned by this function can handle such values different.
>>> s = ''' ... |def add(a, b): ... | return a + b ''' >>> print(convert_string()(s)) def add(a, b): return a + b
- Parameters
- Raises
ValueError – if
start
is not a single character- Returns
converter function
- Return type
function(str)