Skip to main content

Parser like ConfigParser but for updating configuration files

Project description

Built Status ReadTheDocs Coveralls PyPI-Server Conda-Forge Monthly Downloads Sponsor me

Config Updater

The sole purpose of ConfigUpdater is to easily update an INI config file with no changes to the original file except the intended ones. This means comments, the ordering of sections and key/value-pairs as wells as their cases are kept as in the original file. Thus ConfigUpdater provides complementary functionality to Python’s ConfigParser which is primarily meant for reading config files and writing new ones.

Features

The key differences to ConfigParser are:

  • minimal invasive changes in the update configuration file,

  • proper handling of comments,

  • only a single config file can be updated at a time,

  • the original case of sections and keys are kept,

  • control over the position of a new section/key

Following features are deliberately not implemented:

  • interpolation of values,

  • propagation of parameters from the default section,

  • conversions of values,

  • passing key/value-pairs with default argument,

  • non-strict mode allowing duplicate sections and keys.

Usage

First install the package with either:

pip install configupdater

or:

conda install -c conda-forge configupdater

Now we can simply do:

from configupdater import ConfigUpdater

updater = ConfigUpdater()
updater.read("setup.cfg")

which would read the file setup.cfg that is found in many projects.

To change the value of an existing key we can simply do:

updater["metadata"]["author"].value = "Alan Turing"

At any point we can print the current state of the configuration file with:

print(updater)

To update the read-in file just call updater.update_file() or updater.write(open('filename','w')) to write the changed configuration file to another destination. Before actually writing, ConfigUpdater will automatically check that the updated configuration file is still valid by parsing it with the help of ConfigParser.

Many of ConfigParser’s methods still exists and it’s best to look them up in the module reference. Let’s look at some examples.

Adding and removing options

Let’s say we have the following configuration in a string:

cfg = """
[metadata]
author = Ada Lovelace
summary = The Analytical Engine
"""

We can add an license option, i.e. a key/value pair, in the same way we would do with ConfigParser:

updater = ConfigUpdater()
updater.read_string(cfg)
updater["metadata"]["license"] = "MIT"

A simple print(updater) will give show you that the new option was appended to the end:

[metadata]
author = Ada Lovelace
summary = The Analytical Engine
license = MIT

Since the license is really important to us let’s say we want to add it before the summary and even add a short comment before it:

updater = ConfigUpdater()
updater.read_string(cfg)
(updater["metadata"]["summary"].add_before
                               .comment("Ada would have loved MIT")
                               .option("license", "MIT"))

which would result in:

[metadata]
author = Ada Lovelace
# Ada would have loved MIT
license = MIT
summary = Analytical Engine calculating the Bernoulli numbers

Using add_after would give the same result and looks like:

updater = ConfigUpdater()
updater.read_string(cfg)
(updater["metadata"]["author"].add_after
                              .comment("Ada would have loved MIT")
                              .option("license", "MIT"))

Let’s say we want to rename summary to the more common description:

updater = ConfigUpdater()
updater.read_string(cfg)
updater["metadata"]["summary"].key = "description"

If we wanted no summary at all, we could just do del updater["metadata"]["summary"].

Adding and removing sections

Adding and remove sections just works like adding and removing options but on a higher level. Sticking to our Ada Lovelace example, let’s say we want to add a section options just before metadata with a comment and two new lines to separate it from metadata:

updater = ConfigUpdater()
updater.read_string(cfg)
(updater["metadata"].add_before
                    .section("options")
                    .comment("Some specific project options")
                    .space(2))

As expected, this results in:

[options]
# Some specific project options


[metadata]
author = Ada Lovelace
summary = The Analytical Engine

We could now fill the new section with options like we learnt before. If we wanted to rename an existing section we could do this with the help of the name attribute:

updater["metadata"].name = "MetaData"

Sometimes it might be useful to inject a new section not in a programmatic way but more declarative. Let’s assume we have thus defined our new section in a multi-line string:

sphinx_sect_str = """
[build_sphinx]
source_dir = docs
build_dir = docs/_build
"""

With the help of two ConfigUpdater objects we can easily inject this section into our example:

sphinx = ConfigUpdater()
sphinx.read_string(sphinx_sect_str)
sphinx_sect = sphinx["build_sphinx"]

updater = ConfigUpdater()
updater.read_string(cfg)

(updater["metadata"].add_after
                    .space()
                    .section(sphinx_sect.detach()))

The detach method will remove the build_sphinx section from the first object and add it to the second object. This results in:

[metadata]
author = Ada Lovelace
summary = The Analytical Engine

[build_sphinx]
source_dir = docs
build_dir = docs/_build

Alternatively, if you want to preserve build_sphinx in both ConfigUpdater objects (i.e., prevent it from being removed from the first while still adding a copy to the second), you call also rely on stdlib’s copy.deepcopy function instead of detach:

from copy import deepcopy

(updater["metadata"].add_after
                    .space()
                    .section(deepcopy(sphinx_sect)))

This technique can be used for all objects inside ConfigUpdater: sections, options, comments and blank spaces.

Shallow copies are discouraged in the context of ConfigUpdater because each configuration block keeps a reference to its container to allow easy document editing. When doing editions (such as adding or changing options and comments) based on a shallow copy, the results can be unreliable and unexpected.

For more examples on how the API of ConfigUpdater works it’s best to take a look into the unit tests and read the references.

Notes

ConfigUpdater is mainly developed for PyScaffold.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ConfigUpdater-3.2.tar.gz (140.6 kB view details)

Uploaded Source

Built Distribution

ConfigUpdater-3.2-py2.py3-none-any.whl (34.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ConfigUpdater-3.2.tar.gz.

File metadata

  • Download URL: ConfigUpdater-3.2.tar.gz
  • Upload date:
  • Size: 140.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.17

File hashes

Hashes for ConfigUpdater-3.2.tar.gz
Algorithm Hash digest
SHA256 9fdac53831c1b062929bf398b649b87ca30e7f1a735f3fbf482072804106306b
MD5 3ceb028287e357e19aa98ce3d362eebb
BLAKE2b-256 2bf4603bd8a65e040b23d25b5843836297b0f4e430f509d8ed2ef8f072fb4127

See more details on using hashes here.

File details

Details for the file ConfigUpdater-3.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ConfigUpdater-3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0f65a041627d7693840b4dd743581db4c441c97195298a29d075f91b79539df2
MD5 1394bce9dbb46ca8a6190475aaf62347
BLAKE2b-256 e7f0b59cb7613d9d0f866b6ff247c5953ad78363c27ff5d684a2a98899ab8220

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page