Skip to main content

confget - parse configuration files

The confget library parses configuration files (currently INI-style files only) and allows a program to use the values defined in them. It provides various options for selecting the variable names and values to return and the configuration file sections to fetch them from.

The confget library may also be used as a command-line tool with the same interface as the C implementation.

The confget library is fully typed.

Specifying configuration values for the backends

The confget.defs module defines the Config class that is used to control the behavior of the various confget backends. Its main purpose is to specify the filename and, optionally, the section name for INI-style files, but other backends may use its fields in different ways.

A Config object is created using the following parameters:

  • a list of variable names to query (may be empty)
  • filename (str, optional): the name of the file to open
  • section (str, default ""): the name of the section within the file
  • section_specified (bool, default false): if section is an empty string, only fetch variables from the unnamed section at the start of the file instead of defaulting to the first section in the file

Parsing INI-style configuration files

The confget library's "ini" backend parses an INI-style configuration file. Its read_file() method parses the file and returns a dictionary of sections and the variables and their values within them:

import confget

cfg = confget.Config([], filename='config.ini')
ini = confget.BACKENDS['ini'](cfg)
data = ini.read_file()
print('Section names: {names}'.format(names=sorted(data.keys())))
print(data['server']['address'])

Letting variables in a section override the default ones

In some cases it is useful to have default values before the first named section in a file and then override some values in various sections. This may be useful for e.g. host-specific configuration kept in a section with the same name as the host.

The format module in the confget library allows, among other filtering modes, to get the list of variables with a section overriding the default ones:

from confget import backend, format

cfg = format.FormatConfig(['foo'], filename='config.ini', section='first',
                          section_override=True)
ini = backend.BACKENDS['ini'](cfg)
data = ini.read_file()
res = format.filter_vars(cfg, data)
assert len(res) == 1, repr(res)
print(res[0].output_full)

cfg = format.FormatConfig(['foo'], filename='config.ini', section='second',
                          section_override=True)
ini = backend.BACKENDS['ini'](cfg)
data = ini.read_file()
res = format.filter_vars(cfg, data)
assert len(res) == 1, repr(res)
print(res[0].output_full)

See the documentation of the FormatConfig class and the filter_vars() function in the confget.format module for more information and for a list of the various other filtering modes, all supported when the library is used as a command-line tool.

Comments: Peter Pentchev roam@ringlet.net

Metadata

Release files for confget 5.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for confget 5.1.2
File Size Uploaded
confget-5.1.2.tar.gz 18.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for confget 5.1.2
File Interpreter ABI Platform
confget-5.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.4 kB

Release files / confget-5.1.2.tar.gz

Download URL confget-5.1.2.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1e907efa55e89d6d0e69cbcd40d480dddeb09b482face4ad7c157debbcc13445
BLAKE2b-256 checksum
How to use checksums
6a1bc2843915d5f8c8b1c4fe3d2be2273e43a990848045b1849ccd282982843c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / confget-5.1.2-py3-none-any.whl

Download URL confget-5.1.2-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7eebf4abc8a3343417a8afff19407320a9d97fbfba482f72283f61a609149d7f
BLAKE2b-256 checksum
How to use checksums
ad96f1d390532f22b896276f779846061c85a41406758adeb1c71b819ec92df5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

5.1.2 This release

2 release files

5.1.1

2 release files

5.1.0

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.1

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page