ConfigParserEnhanced

DEPRECATION NOTICE

This package was forked by the original author and subdivided into separate packages:

Users of ConfigParserEnhanced should switch to the new packages.

Indices and Tables

Overview

The ConfigParserEnhanced (CPE) package provides extended handling of .ini files beyond what ConfigParser provides by adding an active syntax to embed operations with options.

For example, a standard .ini file is generally formatted like this:

1[Section 1]
2Foo: Bar
3Baz: Bif
4
5[Section 2]
6Foo: Bar2
7Bif: Baz

These files are used to organize sets of key - value pairs called “options” within groups called “sections”. In the example above there are two sections, “Section 1” and “Section 2”. Each of them contains two options where Section 1 has the keys ‘Foo’ and ‘Baz’ which are assigned the values ‘Bar’ and ‘Bif’, respectively. For more details on .ini files please see the documentation for ConfigParser.

ConfigParserEnhanced extends the processing capabilities for .ini files by adding an active component of handling of each option. This allows us to merge the processing with the reading of each option. In this model, we treat individual options according to the following syntax:

operation param1 param2 ... 'param with spaces' ... paramN : value

The entries on an option in the key portion are space-separated generally and the first entry can be considered the operation. CPE will attempt to map the detected operation to a handler method and if a matching one is found then it will send the option to that handler for processing. Options that do not map to a handler will be treated as a standard “key:value” pair.

Internally, these handlers methods defined according to a naming convention like handler_<operation>().

CPE only provides one pre-defined operation: use which is formatted as use TARGET: where param1 is the TARGET (there is no value field for this one). The TARGET paramter takes the name of a target section that will be loaded in at this point. This works in the same way a #include would work in C++ and serves to insert the contents or processing of the target section into this location.

The use operation is useful for .ini files for complex systems by allowing developers to create a common section and then have specializations where they can customize options for a given project. For example:

1[COMMON]
2Key C1: Value C1
3Key C2: Value C2
4Key C3: Value C3
5
6[Data 1]
7Key D1: Value D1
8use COMMON
9Key D2: Value D2

In this example, processing section Data 1 via CPE will result in the following options: Key D1: Value D1, Key C1: Value C1, Key C2: Value C2, Key C2: Value C2, Key D2: Value D2.

An alternative way of looking at this is it’s like having a .ini file that is effectively the following where the use operations are replaced with the results of a Depth-First expansion of the linked sections:

 1[COMMON]
 2Key C1: Value C1
 3Key C2: Value C2
 4Key C3: Value C3
 5
 6[Data 1]
 7Key D1: Value D1
 8Key C1: Value C1
 9Key C2: Value C2
10Key C3: Value C3
11Key D2: Value D2

Examples

Here we show example usages of ConfigParserEnhanced. These examples can be found in the examples/ directory of the repository.

Example 1

example-01.ini

1[SECTION-A]
2key A1 : value A1
3key A2 : value A2
4key A3 : value A3
5
6[SECTION-B]
7use SECTION-A:
8key B1 : value B1

ConfigParserEnhanced-example-01.py

 1#!/usr/bin/env python3
 2# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
 3"""
 4Example application that uses ConfigParserEnhanced
 5"""
 6from __future__ import print_function # python 2 -> 3 compatiblity
 7
 8from configparserenhanced import ConfigParserEnhanced
 9
10
11
12def test_configparserEnhanced(filename="config.ini"):
13    print(f"Using filename: `{filename}`\n")
14
15    parser = ConfigParserEnhanced(filename=filename)
16
17    # Additional ConfigParserEnhanced flags:
18    #parser.debug_level = 5
19    #parser.exception_control_level = 4
20    #parser.exception_control_compact_warnings = True
21    #parser.exception_control_silent_warnings  = False
22
23    section_name = "SECTION-B"
24
25    parser.parse_section(section_name)
26
27    print(f"Section data for `{section_name}`:")
28    section = parser.configparserenhanceddata[section_name]
29    print(f"{section}")
30
31    print("")
32    print("Section List:")
33    for section_name in parser.configparserenhanceddata.sections():
34        print(f"- {section_name}")
35
36    print("")
37    print("Section Details:")
38    for section_name, options in parser.configparserenhanceddata.items():
39        print(f"[{section_name}]")
40        for key, value in options.items():
41            print(f"{key} : {value}")
42        print("")
43
44    # Write out a 'collapsed' version of the .ini file
45    with open("_example-01-parsed.ini", "w") as ofp:
46        parser.write(ofp)
47    return 0
48
49
50
51if __name__ == "__main__":
52    test_configparserEnhanced(filename="example-01.ini")
53    print("Done.")

Console Output

 1Using filename: `example-01.ini`
 2
 3Section data for `SECTION-B`:
 4{'key A1': 'value A1', 'key A2': 'value A2', 'key A3': 'value A3', 'key B1': 'value B1'}
 5
 6Section List:
 7- SECTION-A
 8- SECTION-B
 9
10Section Details:
11[SECTION-B]
12key A1 : value A1
13key A2 : value A2
14key A3 : value A3
15key B1 : value B1
16
17[SECTION-A]
18key A1 : value A1
19key A2 : value A2
20key A3 : value A3
21
22Done.