Skip to main content

Custom ConfigParser class that preserves comments in file when writing

Project description

Python 3.8 | 3.9 | 3.10 | 3.11 | 3.12 | 3.13 Code style: black pre-commit

pre-commit.ci status Python tests

commented-configparser

A custom ConfigParser class that preserves comments and option casing when writing loaded config out.

This library gives you a custom class of the standard library's configparser.ConfigParger which will preserve the comments of a loaded config file when writing that file back out.


Install via pip

From pypi:

python -m pip install commented-configparser

From github:

python -m pip install commented-configparser@git+https://github.com/Preocts/commented-configparser@x.x.x

Note: Replace x.x.x with the desired tag or branch.


Example use

from commentedconfigparser import CommentedConfigParser

# Load the config like normal
config = CommentedConfigParser()
config.read("myconfig.ini")

# Use the config like normal
...

# Update the config like normal
...

# Save the config back to the file
with open("myconfig.ini", "w") as savefile:
    config.write(savefile)

Results

We favor the line spacing choices of the ConfigParser class so the input format may not be preserved completely. However, the comments will be preserved.

Before

# Welcome to our config
[DEFAULT]
# This value has some meaning to someone
foo=bar
# Make sure to add this when you need it
trace=false
logging=true
; This is a comment as well

    # so we need to track all of them

	; and many could be between things
[NEW SECTION]
# Another comment
multi-line=
	value01
	value02
	value03
closing=0
# Trailing comment

After

# Welcome to our config
[DEFAULT]
# This value has some meaning to someone
foo = bar
# Make sure to add this when you need it
trace = false
logging = true
; This is a comment as well
# so we need to track all of them
; and many could be between things

[NEW SECTION]
# Another comment
multi-line =
	value01
	value02
	value03
closing = 0
# Trailing comment

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

commented_configparser-2.0.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

commented_configparser-2.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file commented_configparser-2.0.0.tar.gz.

File metadata

File hashes

Hashes for commented_configparser-2.0.0.tar.gz
Algorithm Hash digest
SHA256 9168eb964242835996482ce9f4a6f522c27ed77107907eda9f13a099975ee7d8
MD5 c88de81aa03198c5f2d9b986ffea69e0
BLAKE2b-256 0469c7a1cd2598c67b7ff15c32bf1b6e54ea2c43c04e466083ee0d59407ac940

See more details on using hashes here.

File details

Details for the file commented_configparser-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for commented_configparser-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b090af1e4281611dc23c231acadb5e29232a4c9f8960efa84421359ef2e72a5
MD5 7c04de560a0dda64791674d5f756c801
BLAKE2b-256 2bc3ccc24f82c94689a76f4fda76124a9fa047478f0db605cb4685b114eb86df

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page