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.1.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for commented_configparser-2.1.0.tar.gz
Algorithm Hash digest
SHA256 6bc1343a7d427f6d6da44784d68ece409a5b730f73cfde764a2db0e47f13540e
MD5 b81b6328e3fb3259ab6401703d81ec46
BLAKE2b-256 8f55fb166f75bdca65ae674343d7e485e8489a2dd6c69fa462a5c84a3312f265

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for commented_configparser-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2b6cb63417f495a517a65dc7c4b41c1ecdfe52c4ff1cfcf048eb6af2f0c7dfe
MD5 bf289c6c27d18ce3e9ebbb27a9d62b4d
BLAKE2b-256 92f60dbb3adf4c424a4e4a6e81780b8d70622f35938a4f555a16a33ef83c0cc7

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