Registry / serialization / configupdater

configupdater

JSON →
library3.2pypypi✓ verified 27d ago

ConfigUpdater is a Python library designed to update INI configuration files while preserving their original formatting, comments, and structure. It offers complementary functionality to Python's standard `ConfigParser`, which primarily focuses on reading and writing new files. The library aims for minimal, targeted changes, ensuring that the ordering of sections and key-value pairs, as well as their original casing, remain intact. The current version is 3.2, and it is actively maintained as part of the PyScaffold project.

pip install configupdater
INSTALL
IMPORT
SIG · CONFIGUPDATER
C
configupdater
serializationpythonv3.2
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 v3.2 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.136s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.126s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

ConfigUpdater
from configupdater import ConfigUpdater

This quickstart demonstrates how to read an INI file, modify an existing option, add new options (with and without comments), remove an option, and write the updated configuration back to the original file.

import os from configupdater import ConfigUpdater # Create a dummy config file for demonstration config_content = ''' [metadata] author = Ada Lovelace summary = The Analytical Engine version = 1.0 [options] verbose = yes path = /tmp/data ''' config_file_path = "example.ini" with open(config_file_path, "w") as f: f.write(config_content) updater = ConfigUpdater() updater.read(config_file_path) # Change an existing value updater["metadata"]["author"].value = "Grace Hopper" # Add a new option updater["metadata"]["license"] = "MIT" # Add a new option with a comment before it (updater["options"]["path"].add_before .comment(" # Path to store temporary files") .option("temp_dir", "/var/temp")) # Remove an option del updater["metadata"]["version"] # Print the current state (optional) # print(updater) # Write the changes back to the original file updater.update_file() print(f"Updated configuration written to {config_file_path}") with open(config_file_path, 'r') as f: print(f.read()) # Clean up the dummy file os.remove(config_file_path)
Debug
Known issues
gotchaShallow copies of configuration blocks (sections, options, comments) are highly discouraged. Each block maintains a reference to its container, and using shallow copies for modifications can lead to unreliable and unexpected results.
fix
Always use `copy.deepcopy()` from the standard library if you need to duplicate a configuration block and modify the copy independently.
affects: All versions
gotchaConfigUpdater does *not* implement all features found in Python's standard `ConfigParser`. Specifically, it does not support value interpolation, propagation of parameters from the default section, value conversions, passing key/value pairs with default arguments, or a non-strict mode allowing duplicate sections and keys. Its focus is solely on minimal invasive updates.
fix
Be aware of the distinct functionality. If you require these `ConfigParser` features, process your configuration with `ConfigParser` first, then use `ConfigUpdater` for targeted structural or value changes, or handle interpolation/conversion manually.
affects: All versions
gotchaDirect assignment to an option's `.value` property (`updater['section']['key'].value = 'multi\nline'`) will fail if the value contains multiple lines. Multi-line values are explicitly disallowed for direct assignment.
fix
For setting multi-line values, use the `set_values()` or `append()` methods provided by `ConfigUpdater` to handle them correctly.
affects: All versions
Errors
Common errors & fixes
configparser.ParsingError: Source contains parsing errors:
This error occurs when the INI file being read by ConfigUpdater has syntax errors, such as malformed lines or unexpected characters, often related to comments within options that ConfigParser (and by extension ConfigUpdater) might misinterpret.
fix
Review the configuration file for any syntax issues, especially inline comments on option lines. Ensure that the INI file adheres to the expected format for `ConfigParser`. If comments within options are causing issues, try moving them to their own lines or using `ConfigUpdater`'s comment handling methods if appropriate.
KeyError: 'section_name'
This error arises when attempting to access a configuration section or option using dictionary-like access (e.g., `updater['section']` or `updater['section']['option']`) that does not exist in the loaded INI file.
fix
Ensure that the section or option you are trying to access exists in your configuration file and that its name (including case) matches exactly. You can check for existence using `if 'section' in updater:` or `if 'option' in updater['section']:` before attempting to access it.
configupdater.exceptions.NoConfigFileReadError: No config file was read yet. Call 'read()' or 'read_string()' first.
This error indicates that you are trying to perform operations on a `ConfigUpdater` object (like accessing sections or options) before any configuration content has been loaded into it.
fix
Before interacting with the `ConfigUpdater` object to get or set values, you must first load a configuration file or string using `updater.read('path/to/config.ini')` or `updater.read_string(ini_string)`.
configupdater.exceptions.AlreadyAttachedError: {block} has been already attached to a container.
This ConfigUpdater-specific error occurs when you try to attach a block (like a section or an option) to a container when it's already part of another container or the same one, often due to improper copying or manipulation of internal objects.
fix
If you intend to use a block in multiple places or modify it independently, create a deep copy of the block using `import copy; new_block = copy.deepcopy(original_block)`. If moving a block, ensure it's detached from its current container first (though direct detachment methods are not typically exposed for end-users and `ConfigUpdater` aims to manage this internally with its `add_before`/`add_after` builders).
Upgrade
Version history
3.2latest on PyPI · released Nov 27, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
24
OpenAI (training)
1
Resources
configupdater — pip install configupdater · libregistry