python-poetry / tomlkit

Style-preserving TOML library for Python
MIT License
701 stars 99 forks source link

Comments from original TOML end up in the wrong spot #295

Open jcote-tc opened 1 year ago

jcote-tc commented 1 year ago

Hello,

I am using the tomlkit Python library to modify an existing TOML file.

The original file looks like this:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

I add two tables to that original array of tables "[[routes]]", with method = 'POST' and path = '/my/post/path'. When I do that, the resulting file becomes:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

The comment ends up in the wrong spot. What can I do to make sure it stays on top of my second "[[routes]]" such as:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

This is an extract of the code I use to add my 2 tables:

# result is <class 'tomlkit.items.Table'>
route_ratelimit = tomlkit.table()
route_ratelimit.add("enabled", True)
route_ratelimit_requests = tomlkit.table()
route_ratelimit_requests.add("per_second", 25)
result.append("rate_limit", route_ratelimit)
route_ratelimit.append("requests", route_ratelimit_requests)

Thank you for your assistance.

frostming commented 1 year ago

It's hard for such level of style-preserving. The parser couldn't know what table the comments should adhere to.

sallner commented 1 year ago

Would it be an option to add the comment inside the [[routes]] sections/entries?

jcote-tc commented 1 year ago

Would it be an option to add the comment inside the [[routes]] sections/entries?

I am not in control of the original file - but all comments are on top of the sections so it makes it easier to handle that use case. I wrote a python function to handle that scenario. Basically I find the index/location where I need to insert my new Table, check if the line above is a Comment and if so I do -1 to the index, which preserves the location of the commented line:

def search_for_insertion_point_index(parsed_toml):
    key_counter = 0
    stored_keys = []
    for k in parsed_toml.body:
        stored_keys.append(k)
        key_counter += 1
        if isinstance(k[1], tomlkit.items.Table):
            # insert new table before the first found tomlkit.items.Table
            # check if the last line is a comment
            if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
                #print("Comment found on the line before. Return index minus 1!")
                return parsed_toml.body.index(k) - 1
            #print(parsed_toml.body.index(k))
            return parsed_toml.body.index(k)
        if isinstance(k[1], tomlkit.items.AoT):
            # insert new table before the first found tomlkit.items.AoT
            # check if the line before is a comment
            if isinstance(stored_keys[parsed_toml.body.index(k) - 1][1], tomlkit.items.Comment):
                #print("Comment found on the line before. Return index minus 1!")
                return parsed_toml.body.index(k) - 1
            #print(parsed_toml.body.index(k))
            return parsed_toml.body.index(k)