preocts / commented-configparser Goto Github PK
View Code? Open in Web Editor NEWCustom ConfigParser class that preserves comments in file when writing
License: MIT License
Custom ConfigParser class that preserves comments in file when writing
License: MIT License
Importing multiple configs should preserve comments by key just fine. However, mirrored comments will duplicate. Handle that.
For those that do not want questionably useful interpolation.
Hi @Preocts!
Nice to see you're stepping up to address this shortcoming of the ConfigParser module. I did notice there are some unhandled edge cases which may deserve fixing or documenting.
Consider this config file:
; comment below @@header section is lost
value=below comment below @@header
; comment above header key
@@header = is placed below DEFAULT section
# 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
# Lost comment
multi-line-with-comment=
value01
# comment inside value
value02
value03
multi-line-with-section=
value01
[section in multi line comment]
value02
value03
closing=0
# Trailing comment
I parse it like this:
from commentedconfigparser import CommentedConfigParser
# Load the config like normal
config = CommentedConfigParser()
config.read("./config")
# This doesn't print the comment as part of the value
print(config["NEW SECTION"]["multi-line-with-comment"])
# Section is printed as part of the value...
print(config["NEW SECTION"]["multi-line-with-section"])
# ...but when changing the value...
config["NEW SECTION"]["multi-line-with-section"] = "reset\nmulti\nlines"
# ...the final result has a comment inserted:
# multi-line-with-section = reset
# # Trailing comment
# multi
# lines
print()
print("Final config file output:")
# Print the modified config file to stdout
with StringIO() as stream:
config.write(stream)
# Start from the beginning of the stream
stream.seek(0)
print(stream.read(), end="")
The output looks like this:
value02
value03
value01
[section in multi line comment]
value02
value03
Final config file output:
[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
[@@header]
# Welcome to our config
value = below comment below @@header
; comment above header key
@@header = is placed below DEFAULT section
# Welcome to our config
[NEW SECTION]
# Another comment
multi-line =
value01
value02
value03
multi-line-with-comment =
value01
value02
value03
multi-line-with-section = reset
# Trailing comment
multi
lines
closing = 0
Some comments are lost. Some are inserted at the wrong position. Also @@header
seems to have special meaning which one needs to look out for.
For my use case I need to preserve comments in the file, without interfering with lines inside a multiline value (e.g. they should be able to start with a # or have look like a section header).
I've subclassed ConfigParser myself to work around these issues at the expense of disallowing indentation inside the config file (with the exception of indentation as part of multiline values). Perhaps it could serve as inspiration to you.
P.S. I'm looking forward to receive feedback on my approach!
A deleted key should merge all of its comments to the key above it in the map.
Hey uh, about that, I tried to add this to my work project, but I'm stuck on 2.7, and I'm getting some errors
I'm going to need you to fix that
Hi, I tried to create this wheel multiple times for many projects, you definitely saved my life. I just want to say big thanks to you!
Great work!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.