Package: imperfect
ImperfectThis is a module for making automated edits to an existing
configparser-compatible ini file.  It operates like a CST, parsing into a tree
of nodes which can then be edited and written back, preserving whitespace and
comments.Quick Start(Tested astest_example_from_readmein the test suite)Let's say you have the following insetup.cfg:[metadata]# the package namename=imperfect# slurp the readmelong_description=file: README.md[options]packages=imperfectand you'd like to make an edit settinglong_description_content_typein[metadata]but don't care where it goes.  Default is at the end.importdifflibimportimperfectimportiowithopen("setup.cfg")asf:data=f.read()conf:imperfect.ConfigFile=imperfect.parse_string(data)conf.set_value("metadata","long_description_content_type","text/markdown")print(conf.text)What if you want to have control over the odering, and want it right afterlong_description?  Now with diffing and more internals...importdifflibimportimperfectimportiowithopen("setup.cfg")asf:data=f.read()conf:imperfect.ConfigFile=imperfect.parse_string(data)metadata_section=conf["metadata"]# Ignoring some whitespace for now, this looks like# long_description_content_type =  text/markdown\n# [                   entry                      ]# [            key            ][eq][    value    ]value=imperfect.ValueLine(whitespace_before_text='',text='text/markdown',whitespace_after_text='',newline='\n',)new_entry=imperfect.ConfigEntry(key="long_description_content_type",equals="=",value=[value],whitespace_before_equals=" ",whitespace_before_value="  ",)fori,entryinenumerate(metadata_section.entries):ifentry.key=="long_description":metadata_section.entries.insert(i+1,new_entry)breakprint(moreorless.unified_diff(data,conf.text,"config.cfg",end=""))withopen("setup.cfg","w")asf:f.write(conf.text)# orwithopen("setup.cfg","w")asf:conf.build(f)A note on whitespaceFollowing the convention used by configobj, whitespace generally is accumulated
and stored on the node that follows it.  This does reasonably well for adding
entries, but can have unexpected consequences when removing them.  For example,[section1]# this belongs to k1k1=foo# this belongs to k2k2=foo# k3 = foo (actually belongs to the following section)[section2]An insertion to the end ofsection1would go between k2 and the k3 comment.
Removingsection2would also remove the commented-outk3.I'm open to ideas that improve this.A note on formatsThe goal is to be as compatible as possible withRawConfigParser, which
includes keeping some odd behaviors that are bugs that have been around for a
decade and probably can't be changed now.Section names are very lenient.[[x]]yyis a legal section line, and the
resulting section name is[x.  Theyyhere is always allowed (we keep it
in the tree though), even withinline_commentsoff.\r(carriage return) is considered a whitespace, but not a line terminator.
This is a difference in behavior betweenstr.splitlines(True)andlist(io)-- configparser uses the latter.\tcounts as single whitespace.Supported parse optionsOption                 Default  Imperfect supports
allow_no_value         False    only False
delimiters             =,:      only =,:
comment_prefixes       #,;      only #,;
empty_lines_in_values  True     True (False is very close to working)TestingWe use hypothesis to generate plausible ini files and for all the ones that
RawConfigParser can accept, we test that we accept, have the same keys/values,
and can roundtrip it.This currently happens in text mode only.If you would like to test support on your file, trypython -m imperfect.verify <filename>Why not...configobjhas a completely different method for line continuationsI'm not aware of others with the goal of preserving whitespaceLicenseImperfect is copyrightTim Hatch, and licensed under
the MIT license.  I am providing code in this repository to you under an open
source license.  This is my personal repository; the license you receive to
my code is from me and not from my employer. See theLICENSEfile for details.