Custom ConfigParser class that preserves comments in file when writing
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file commented_configparser-2.0.0.tar.gz
.
File metadata
- Download URL: commented_configparser-2.0.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9168eb964242835996482ce9f4a6f522c27ed77107907eda9f13a099975ee7d8 |
|
MD5 | c88de81aa03198c5f2d9b986ffea69e0 |
|
BLAKE2b-256 | 0469c7a1cd2598c67b7ff15c32bf1b6e54ea2c43c04e466083ee0d59407ac940 |
File details
Details for the file commented_configparser-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: commented_configparser-2.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b090af1e4281611dc23c231acadb5e29232a4c9f8960efa84421359ef2e72a5 |
|
MD5 | 7c04de560a0dda64791674d5f756c801 |
|
BLAKE2b-256 | 2bc3ccc24f82c94689a76f4fda76124a9fa047478f0db605cb4685b114eb86df |