Config Formatter
Config-formatter is a Python library designed for automatically formatting `.ini` and `.cfg` configuration files. It aims to standardize indentation, normalize value assignments, and preserve comments. The current version is 1.2.0, released in November 2023. Its release cadence appears to be infrequent but stable.
Warnings
- gotcha The `prettify` method returns a formatted string; it does not modify files in-place. Users must handle file I/O (reading and writing back to disk) explicitly. Improper handling (e.g., overwriting the original file before a successful write of the formatted content) can lead to data loss.
- gotcha While designed for standard `.ini` and `.cfg` files, highly custom or non-standard syntax (e.g., unusual comment styles, non-standard key-value assignments) might be reformatted in unexpected ways. This could potentially alter the intended structure or functionality if the formatter's interpretation differs from the application consuming the config.
- gotcha The formatter aims to preserve comments. However, minor whitespace adjustments around comments or within multi-line values can occur during the formatting process. If exact byte-for-byte preservation of whitespace or comment positioning is critical (e.g., for `git diff` consistency or specific parser requirements), these subtle changes might be problematic.
Install
-
pip install config-formatter
Imports
- ConfigFormatter
from config_formatter import ConfigFormatter
Quickstart
import os
from config_formatter import ConfigFormatter
# Create a dummy config file for demonstration
config_content_before = """
[main]
# A comment here
key1: value1
key2= value2
[section_one]
long_key_name = multiline value\
continues here
# Another comment
"""
with open("temp_config.ini", "w") as f:
f.write(config_content_before)
# Read, format, and print the content
with open("temp_config.ini", "r") as file:
formatter = ConfigFormatter()
formatted_content = formatter.prettify(file.read())
print("--- Original ---")
print(config_content_before)
print("\n--- Formatted ---")
print(formatted_content)
# Clean up the dummy file
os.remove("temp_config.ini")