Skip to main content

A custom ConfigParser class that preserves comments and most formatting when writing loaded config out.

Project description

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

pre-commit.ci status Python tests

commented-configparser


A custom ConfigParser class that preserves comments and most formatting 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

There is the attempt to retain the original format. Known formats that will not be preserved:

  1. Indents will not be preserved outside of multi-line values
  2. Spacing around assignments will be normalized.

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
multiLine=
	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
multiLine =
	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-3.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

commented_configparser-3.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: commented_configparser-3.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for commented_configparser-3.0.0.tar.gz
Algorithm Hash digest
SHA256 dabdfc94f1166200ad3df3ddfc6d17a2902a554b773bc6f81101202dbf0ff457
MD5 acacc52744306c5beb17fe0a327fef94
BLAKE2b-256 403594a7f1ea8fe228c703460ed6726919f31dded615729670fb807fc843fa3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for commented_configparser-3.0.0.tar.gz:

Publisher: pypi-publish.yml on Preocts/commented-configparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for commented_configparser-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b8a9cac3e6fe0f9301ec8bc54f6d7ddb83da6e3f4d18dbd15459364f5166ab6
MD5 0950f310afb317f973f04bf1724b0d88
BLAKE2b-256 d981dfc2b8a565a7a3178e696e94ec444d98bf75ad2a2a717c7c00688e327359

See more details on using hashes here.

Provenance

The following attestation bundles were made for commented_configparser-3.0.0-py3-none-any.whl:

Publisher: pypi-publish.yml on Preocts/commented-configparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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