editing python configobj format config files

I’m trying to figure out how to reliably edit a configobj format (link) file that unfortunately has a bunch of blocks with the same “key = value” combinations initially after the piece of software is installed, so a simple in-line sed or equivalent is not a reasonable solution. Note this is ‘not’ an ini file format so that module isn’t an option for this case.

Looking for suggestions…

Example config file, lightly obfuscated for brevity:

[Section1]

[[Thing1]]

This section is for configuring posts to Thing1.

enable = false

username = replace_me

password = “replace_me”

[[Thing2]]

This section is for configuring posts to Thing2.

enable = false

username = replace_me

password = “replace_me”

[[Thing3]]

This section is for configuring posts to Thing3.

enable = false

username = replace_me

password = “replace_me”

The order of the [SectionN] blocks can be random. The order of the [[Thing1]] blocks ‘within’ a [SectionN] block can be random. The order of the entries within a [[ThingN] block can be random. The number of [[ThingN]] items within a [SectionN] block is not fixed nor predictable.

So for discussion, assume I want to configure [[Thing2]] enabled and set its values. I’m thinking something like…

  • name: configure [[Thing2]]
    blockinfile:
    path: /my/filename.txt

insertafter: “[Section1]”

content: |

[[Thing2]]

This section is for configuring posts to Thing2.

enable = true

username = myuserhere

password = “mypasshere”

This would ‘add’ a block, but how would I delete a pre-existing unconfigured [[Thing2]] block and do this in an idempotent way?

Or am I just overthinking and there’s a simpler way ?

Suggestions appreciated…