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 comment and other formatting roundtripping #192

Open
kislyuk opened this issue May 26, 2024 · 0 comments
Open

TOML comment and other formatting roundtripping #192

kislyuk opened this issue May 26, 2024 · 0 comments

Comments

@kislyuk
Copy link
Owner

kislyuk commented May 26, 2024

tomlkit provides the ability to emit comments as metadata keys, similar to what we do in yq -Y:

import tomlkit


def repr(i, indent=0):
    if isinstance(i, tomlkit.items.Item):
        print(" " * indent, "ITEM", type(i), type(i.value), "TRIVIA", i.trivia)
        if isinstance(i.value, tomlkit.container.Container):
            repr(i.value, indent=indent + 2)
        else:
            print(" " * indent, "SCALAR", type(i), i)
    if isinstance(i, tomlkit.container.Container):
        print(" " * indent, "CONTAINER", type(i))
        for k, v in i.body:
            print(" " * indent, k, "\t", type(v), "\t>>>", str(v)[:20] + "..." if len(str(v)) > 20 else str(v))
            if isinstance(v, (dict, list)):
                repr(v, indent=indent + 2)


with open("pyproject.toml") as fh:
    doc = tomlkit.parse(fh.read())
    repr(doc)
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