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.1.0.tar.gz
.
File metadata
- Download URL: commented_configparser-2.1.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 | 6bc1343a7d427f6d6da44784d68ece409a5b730f73cfde764a2db0e47f13540e |
|
MD5 | b81b6328e3fb3259ab6401703d81ec46 |
|
BLAKE2b-256 | 8f55fb166f75bdca65ae674343d7e485e8489a2dd6c69fa462a5c84a3312f265 |
File details
Details for the file commented_configparser-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: commented_configparser-2.1.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 | f2b6cb63417f495a517a65dc7c4b41c1ecdfe52c4ff1cfcf048eb6af2f0c7dfe |
|
MD5 | bf289c6c27d18ce3e9ebbb27a9d62b4d |
|
BLAKE2b-256 | 92f60dbb3adf4c424a4e4a6e81780b8d70622f35938a4f555a16a33ef83c0cc7 |