A tool that extends configparser to enable enhanced processing of .ini files.
Project description
ConfigParserEnhanced
ConfigParserEnhanced
(CPE) provides extended functionality for the ConfigParser
module. This class attempts to satisfy the following goals:
- Provide a framework to embed extended ‘parsing’ into
Config.ini
style files with customizable handlers that allow 'commands' to be embedded into the key-value structure of typical.ini
file options. - Enable chaining of
[SECTIONS]
within a single.ini
file, which uses the parsing capability noted in (1). - Provide an extensible capability. We intend CPE to be used as a base class for other tools so that subclasses can add additional handlers for new ‘operations’ which can be used by the parser.
Configuration .ini
File Enhancements
CPE allows .ini
files to be augmented to embed commands into the key: value
structure of options within a section.
Normal .ini
files might have a structure that looks like this:
[SECTION NAME]
key1: value1
key2: value2
key4: value3
CPE augments this by allowing the keys to be used to embed operations.
To enable this, CPE attempts to split a key into three pieces, an operation, a parameter, and
an optional uniqueifier string. An option can have an operation extracted as
<operation> <parameter> [uniquestr]: value
for example:
[SECTION NAME]
operation1 parameter1: value1
operation2 parameter2 A: value2
operation2 parameter2 B: value2
key1: value1
key2: value2
key3: value3
Note: The
[uniquestr]
is optional and we can use it to prevent certain duplicate key errors from the underlyingConfigParser
which requires that each section must have no duplicate 'key' fields.
When the CPE parser successfully identifies a potential operation, it will attempt to find a handler
method named as _handler_<operation>()
or handler_<operation>()
, and if one exists then it will execute
that handler with the detected parameter (if any) and value (if any) from that entry.
New handlers can be added by creating a subclass of CPE and then adding the new handlers to the subclass.
Operations
The CPE base class provides the following handlers by default.
use
The use
handler is used in the following manner:
[SECTION-A]
key-A1: value-A1
key-A2: value-A2
key-A3: value-A3
[SECTION-B]
use SECTION-A
key-B1: value-B1
In this example, the entry use SECTION-A
that is inside [SECTION-B]
instructs the core
parser to recurse into [SECTION-A]
and process it before moving on with the rest of the
entries in [SECTION-B]
. In this example the following code could be used to parse
SECTION-B
.
ConfigParserEnhanced.configparserenhanceddata['SECTION-B']
would return the following
result:
>>> cpe = ConfigParserEnhanced(filename='config.ini')
>>> cpe.configparserenhanceddata['SECTION-B']
{
'key-A1': 'value-A1',
'key-A2': 'value-A2',
'key-A3': 'value-A3',
'key-B1': 'value-B1',
}
Updates
See the CHANGELOG for information on changes.
Links
Additional documentation can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for configparserenhanced-0.8.0a0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a77c7354c6bc09e769f8657b5a92fdb45bb2308d102150173cfa1cb2b5957cdf |
|
MD5 | 690c9f11d1eb969416682698951b9fc1 |
|
BLAKE2b-256 | 707f24451ca02e55f12cae694d5b47fbaeb724cef429e1eed6413f9c8bb1585c |
Hashes for configparserenhanced-0.8.0a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d854fdd54033a38ee4d34944e2b9c0d2fa3fc4e17f8dccf28a23c28bc38c733c |
|
MD5 | d47d42cd33b4c2d6a05fe09b440120f5 |
|
BLAKE2b-256 | 0492dd87642bad5e21f0c686caab1f56ba3ff310fddab54b681e32aa02a69bab |