Examples

This page contains the raw code + output of our examples. For detailed explanations of what is going on please see the Quick Start guide.

Example 1 - Basic Example

See Example: Basic Usage in the QuickStart guide for a detailed explanation of this example.

Input File Listing: Example 1

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

Code Listing: Example 1

 1#!/usr/bin/env python3
 2# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
 3"""
 4ActiveConfigParser example showing the basics.
 5"""
 6from activeconfigparser import ActiveConfigParser
 7
 8
 9filename = "ActiveConfigParser-example-01.ini"
10print(f"Using filename: `{filename}`\n")
11
12# Create the parser
13parser = ActiveConfigParser(filename=filename)
14
15# Additional ActiveConfigParser flags (optional)
16#parser.debug_level = 5
17#parser.exception_control_level = 4
18#parser.exception_control_compact_warnings = True
19#parser.exception_control_silent_warnings  = False
20
21# Parse SECTION-B
22section_name = "SECTION-B"
23parser.parse_section(section_name)
24
25# Extract the properties of the section as a dictionary
26print(f"Section data for `{section_name}`:")
27section = parser.activeconfigparserdata[section_name]
28print(f"{section}")
29print("")
30
31# List the section names
32print("Section List:")
33for section_name in parser.activeconfigparserdata.sections():
34    print(f"- {section_name}")
35print("")
36
37# Walk through the details of all sections in the file:
38print("Section Details:")
39for section_name, options in parser.activeconfigparserdata.items():
40    print(f"[{section_name}]")
41    for key, value in options.items():
42        print(f"{key} : {value}")
43    print("")
44
45# Write out a 'collapsed' version of the .ini file
46with open("_ActiveConfigParser-example-01-parsed.ini", "w") as ofp:
47    parser.write(ofp)

Output: Example 1

Program execution output:

 1Using filename: `ActiveConfigParser-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

Parsed output file:

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

Example 2 - Cyclic Dependency

In this example we add a cyclic dependency into the .ini file. Generally this is a bad idea because a dependency graph of the sections should createa DAG but we try and handle it.

The default behaviour is to treat this as an accident that should be noted so we generate a warning and we don’t follow the back-edge in our DFS traversal. This warning can be changed to an exception by changing the exception_control_level parameter to 5.

Input File Listing: Example 2

We add the use SECCTION-B operation into SECTION-A on line 4.

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

Code Listing: Example 2

 1#!/usr/bin/env python3
 2# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
 3"""
 4ActiveConfigParser example demonstrating handling .ini files
 5with cyclic dependencies across ``use`` operations.
 6"""
 7from __future__ import print_function # python 2 -> 3 compatiblity
 8from activeconfigparser import ActiveConfigParser
 9
10
11filename = "ActiveConfigParser-example-02.ini"
12print(f"Using filename: `{filename}`\n")
13
14# Create the parser
15parser = ActiveConfigParser(filename=filename)
16
17# Additional ActiveConfigParser flags (optional)
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# Parse SECTION-B
24section_name = "SECTION-B"
25parser.parse_section(section_name)
26
27# Extract the properties of the section as a dictionary
28print(f"Section data for `{section_name}`:")
29section = parser.activeconfigparserdata[section_name]
30print(f"{section}")
31print("")
32
33## Write out a 'collapsed' version of the .ini file
34with open("_ActiveConfigParser-example-02-parsed.ini", "w") as ofp:
35    parser.write(ofp)

Output: Example 2

Here we see the output with the warning generated. The general format of the warnings is controled by ExceptionControl but the general format is to prefix the warning message lines with a !! to allow easy searching in large log files.

 1Using filename: `ActiveConfigParser-example-02.ini`
 2
 3!! ================================================================================
 4!! EXCEPTION SKIPPED
 5!! Event Type : WARNING
 6!! Exception  : ValueError
 7!! Message    : Detected a cycle in `use` dependencies in .ini file [PosixPath('ActiveConfigParser-example-02.ini')].
 8!!            : - cannot load [SECTION-B] from [SECTION-A].
 9!!
10!! Call Stack:
11!! File "/builds/semantik-software/code/python/ActiveConfigParser/examples/ActiveConfigParser-example-02.py", line 25, in <module>
12!!     parser.parse_section(section_name)
13!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 685, in parse_section
14!!     result = self._parse_section_r(section, initialize=initialize, finalize=finalize)
15!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 1008, in _parse_section_r
16!!     ophandler_f(section_name, handler_parameters)
17!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 722, in wrapper
18!!     output = func_handler(self, section_name, handler_parameters)
19!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 1339, in _handler_use
20!!     self._parse_section_r(op2, handler_parameters, finalize=False)
21!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 1008, in _parse_section_r
22!!     ophandler_f(section_name, handler_parameters)
23!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 722, in wrapper
24!!     output = func_handler(self, section_name, handler_parameters)
25!! File "/builds/semantik-software/code/python/ActiveConfigParser/venv-examples/lib/python3.14/site-packages/activeconfigparser/ActiveConfigParser.py", line 1346, in _handler_use
26!!     self.exception_control_event("WARNING", ValueError, message)
27!!
28!! Increase `exception_control_level` to 5 to raise this exception.
29!! ================================================================================
30Section data for `SECTION-B`:
31{'key A1': 'value A1', 'key A2': 'value A2', 'key A3': 'value A3', 'key B1': 'value B1'}
32

The same as in Example 01, we also save out the unrolled version of the parsed file. This differs from Example 01 in that we have key B1 inserted into SECTION-A on line 4.

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

Example 3 - Add Custom Operations

See Example: Add Custom Operations in the QuickStart guide for a detailed explanation of this example.

Input File Listing: Example 3

This .ini files includes the custom operation hello that we will create in a custom class that extends ActiveConfigParser.

1[SECTION-A]
2hello:
3hello world:
4hello world earth: moon

Code Listing: Example 3

The source code:

 1#!/usr/bin/env python3
 2# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
 3"""
 4This example demonstrates how one can extend ActiveConfigParser to
 5create new configuration file parser that has new operations.
 6"""
 7from __future__ import print_function # python 2 -> 3 compatiblity
 8from activeconfigparser import ActiveConfigParser
 9
10
11
12class MyActiveConfigParser(ActiveConfigParser):
13
14    def __init__(self, filename=None):
15        if filename is not None:
16            # self.inifilepath is an ActiveConfigParser property
17            self.inifilepath = filename
18        return
19
20    @ActiveConfigParser.operation_handler
21    def _handler_hello(self, section_name, handler_parameters) -> int:
22        op = handler_parameters.op
23        param = handler_parameters.params
24        value = handler_parameters.value
25        print(f"HELLO!")
26        print(f"- op   : {op}")
27        print(f"- param: {param}")
28        print(f"- value: {value}")
29        return 0
30
31
32
33filename = "ActiveConfigParser-example-03.ini"
34print(f"Using filename: `{filename}`\n")
35
36# Create the parser
37parser = MyActiveConfigParser(filename=filename)
38
39# Additional ActiveConfigParser flags (optional)
40#parser.debug_level = 5
41#parser.exception_control_level = 4
42#parser.exception_control_compact_warnings = True
43#parser.exception_control_silent_warnings  = False
44
45# Parse SECTION-A
46section_name = "SECTION-A"
47parser.parse_section(section_name)

The custom class MyActiveConfigParser is created to extend ActiveConfigParser and we add our custom handler for the hello operation in the _handler_hello method.

Output: Example 3

 1Using filename: `ActiveConfigParser-example-03.ini`
 2
 3HELLO!
 4- op   : hello
 5- param: []
 6- value: 
 7HELLO!
 8- op   : hello
 9- param: ['world']
10- value: 
11HELLO!
12- op   : hello
13- param: ['world', 'earth']
14- value: moon