Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments not preserved on merge #171

Open
tkeffer opened this issue Oct 8, 2018 · 3 comments · May be fixed by #212
Open

Comments not preserved on merge #171

tkeffer opened this issue Oct 8, 2018 · 3 comments · May be fixed by #212

Comments

@tkeffer
Copy link

tkeffer commented Oct 8, 2018

Using ConfigObj v5.0.6, consider this:

import sys
from StringIO import StringIO
from configobj import ConfigObj

c = ConfigObj(StringIO("""[Section1]"""))

d = ConfigObj(StringIO("""[Section1]

  # This is a Section2 comment
  [[Section2]]
     option2 = foo
"""))

c.merge(d)

c.write(sys.stdout)

The output is:

[Section1]
[[Section2]]
option2 = foo

while I would have expected

[Section1]
# This is a Section2 comment
[[Section2]]
option2 = foo
@j-bennet
Copy link

+1, we have this issue in pgcli: dbcli/pgcli#1240.

@robdennis
Copy link
Member

makes sense and thanks for the MR; adding to 5.1.0 milestone

@tkeffer
Copy link
Author

tkeffer commented Jan 8, 2023

I should add that parentage is also not preserved on merge. This is what we do to get around these V5 limitations:

from configobj import Section

def merge_config(self_config, indict):
    """Merge and patch a config file"""

    # Do a ConfigObj merge:
    self_config.merge(indict)
    # Then fix comments and parentage:
    patch_config(self_config, indict)


def patch_config(self_config, indict):
    """The ConfigObj merge does not transfer over parentage, nor comments. This function
    fixes these limitations.
    """
    for key in self_config:
        if isinstance(self_config[key], Section) \
                and key in indict and isinstance(indict[key], Section):
            self_config[key].parent = self_config
            self_config[key].main = self_config.main
            self_config.comments[key] = indict.comments[key]
            self_config.inline_comments[key] = indict.inline_comments[key]
            patch_config(self_config[key], indict[key])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants