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

TOML Parse and Rewriting not handling comments and indentation #392

Open
lupaulus opened this issue Jan 9, 2025 · 0 comments
Open

TOML Parse and Rewriting not handling comments and indentation #392

lupaulus opened this issue Jan 9, 2025 · 0 comments

Comments

@lupaulus
Copy link

lupaulus commented Jan 9, 2025

Hello,

Using tomlkit 0.13.2, having a problem when read and rewriting the content.
The comments and the indentation disappear.

        if not bool(self._config):
            return None

        if not os.path.exists(self.path):
            open(self.path, 'a').close()

        # Read content of file
        with open(self.path, 'r', encoding="utf-8") as file:
            # Read the entire content of the file
            content = file.read()

        doc = tomlkit.parse(content)
        doc.update(self._config) # config Dict from

        with open(self.path, 'w', encoding="utf-8") as file:
            file.write(tomlkit.dumps(doc))

Config load

        with open(self.path, "r") as file:
            self._config = tomlkit.load(file).unwrap()
            return self._config

Here the toml as input

[Publisher]
   [Publisher.Specifications]
       Protocol = "MQTTv5"
       Port = 1166
       SendTimePeriod = 20 # seconds
       TimeGroupPeriod = 200 # milliseconds
#       ProxyHost = "111.111.111.111"
#       ProxyPort = 1616

Here the toml as output

[Publisher.Specifications]
Protocol = "MQTTv5"
Port = 1166
SendTimePeriod = 20
TimeGroupPeriod = 200
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

No branches or pull requests

1 participant