Registry / serialization / config-formatter

config-formatter

JSON →
library1.2.0pypypi✓ verified 86d ago

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.

pip install config-formatter
INSTALL
IMPORT
SIG · CONFIG-FORMATTER
C
config-formatter
serializationpythonv1.2.0
Install
1.6s avg
Import
131ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.137s · 18MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.125s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ConfigFormatter
from config_formatter import ConfigFormatter

Initialize `ConfigFormatter` and use its `prettify` method to format a string containing `.ini` or `.cfg` content. The `prettify` method returns the formatted string, requiring manual file I/O for saving changes.

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")
config-formatter --version
Debug
Known issues
gotchaThe `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.
fix
Implement robust file I/O, such as writing the formatted content to a temporary file first and then atomically replacing the original file only after a successful write operation.
affects: All versions
gotchaWhile 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.
fix
Always review the formatted output (e.g., via a `diff`) and thoroughly test configurations with the consuming application after formatting, especially for critical files or those with unusual syntax.
affects: All versions
gotchaThe 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.
fix
Visually inspect generated diffs and test the formatted files with the consuming application if precise whitespace or comment placement is essential for your use case.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'config_formatter'
The 'config-formatter' library is not installed in your Python environment or there's a typo in the import statement.
fix
Install the library using pip: `pip install config-formatter` or correct the import statement to `import config_formatter`.
FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_file.ini'
The specified configuration file does not exist at the given path, or the path is incorrect.
fix
Ensure the file path is correct and the file exists. Check for typos in the filename or directory path.
ValueError: Invalid configuration section header: 'section with spaces'
The input '.ini' or '.cfg' file contains a malformed section header that the formatter cannot parse, such as one with unquoted spaces.
fix
Correct the section header in your configuration file to adhere to the INI/CFG format specification, e.g., `[section_with_no_spaces]` or `'[section with spaces]'` if the parser supports quoted headers.
TypeError: format_file() missing 1 required positional argument: 'file_path'
The `format_file` function was called without providing the necessary `file_path` argument.
fix
Provide the path to the configuration file when calling the function, e.g., `config_formatter.format_file('my_config.ini')`.
Upgrade
Version history
1.2.0latest on PyPI · released Nov 18, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
config-formatter — pip install config-formatter · libregistry