Imperfect

0.4.0 · active · verified Thu Apr 16

Imperfect is a Python library (version 0.4.0) designed for editing `configparser`-compatible INI files while preserving comments and whitespace. It operates by parsing configuration files into a Concrete Syntax Tree (CST) of nodes, allowing programmatic modifications that maintain the original file's structural integrity. The library is under active development with irregular releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an existing INI-style configuration string using `imperfect.parse_string`, modify a value using `set_value`, and then retrieve the modified content, showcasing the preservation of comments and whitespace.

import imperfect
import io

# Simulate an existing config file content
initial_config_content = '''
[metadata]
# the package name
name = imperfect
# slurp the readme
long_description = file: README.md

[options]
packages = imperfect
'''

# Parse the string content into an imperfect.ConfigFile object
conf: imperfect.ConfigFile = imperfect.parse_string(initial_config_content)

# Set a new value, it will be added at the end of the section by default
conf.set_value("metadata", "long_description_content_type", "text/markdown")

# Print the modified content, preserving comments and whitespace
print(conf.text)

view raw JSON →