Coder Social home page Coder Social logo

commented-configparser's People

Contributors

pre-commit-ci[bot] avatar preocts avatar surfernsk avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

commented-configparser's Issues

Handle layered configs

Importing multiple configs should preserve comments by key just fine. However, mirrored comments will duplicate. Handle that.

Handle edge cases or document limitations

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!

Need you to support 2.7

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

Just want to say thanks!

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.