Skip to main content

config-patching

Tools for customising configuration files without maintaining a complete copy of the upstream configuration.

Many Python libraries and Linux packages build their configuration from a base configuration and user-specific customisations. config-patching provides a similar mechanism for Python libraries whose configuration is stored in JSON, JSON5, or YAML.

Instead of copying and modifying the entire configuration file, you can provide a small patch containing only the settings you want to change. The patch is applied to the base configuration when it is loaded.

config-patching is based on the JSON Merge Patch mechanism defined by RFC 7396.

Early Development

config-patching was originally developed as part of the OpenFlexure Microscope Server project and has now been extracted into its own standalone tool, and relicensed under the MIT license.

The core patching mechanism is already established, but config-patching is still in early development as an independent project. The API and surrounding functionality may continue to evolve as the project matures.

In the future, we plan to add additional functionality, such as tools for creating patches from existing configuration files.

Installation

Install config-patching with pip:

pip install config-patching

or

pip install config-patching[all]

if you need JSON5/YAML support.

Loading a configuration

The main entry point is load_patched_config():

from config_patching import load_patched_config

config = load_patched_config("config.json")

If no special keys are present then load_patched_config() loads the specified configuration file and returns its contents unchanged.

If the configuration contains a base_config_file field, it is instead treated as a patch configuration. The specified base configuration is loaded first, and an optional patch is then applied to it.

There are therefore three possible cases:

1. A normal configuration

If base_config_file is not present, the file is treated as a normal configuration and returned as-is.

{
  "foo": "bar"
}

Calling:

config = load_patched_config("config.json")

returns the python dictionary:

{"foo": "bar"}

2. A base configuration with no patch

If base_config_file is present but patch is not, the base configuration is loaded and returned unchanged.

{
  "base_config_file": "base.json"
}

For example, if base.json contains:

{
  "foo": "bar",
  "enabled": true
}

then:

config = load_patched_config("config.json")

returns the python dictionary:

{
  "foo": "bar",
  "enabled": True
}

3. A base configuration with a patch

If both base_config_file and patch are present, the patch is applied to the base configuration.

{
  "base_config_file": "base.json",
  "patch": {
    "enabled": false
  }
}

Given this base configuration:

{
  "foo": "bar",
  "enabled": true
}

the result is the python dictionary:

{
  "foo": "bar",
  "enabled": False
}

Patch files

A file is only treated as a patch file when it contains the base_config_file field. This means existing configuration files can be passed directly to load_patched_config() without requiring any special wrapper.

For example, this remains a normal configuration file:

{
  "server": {
    "port": 8080
  }
}

while this is a patch configuration:

{
  "base_config_file": "base.json",
  "patch": {
    "server": {
      "port": 9090
    }
  }
}

The base_config_file path is resolved relative to the directory containing the patch configuration file.

Supported formats

config-patching supports:

  • JSON
  • JSON5 - JSON with comments!
  • YAML

Both YAML and JSON5 require separate libraries. To use these install with pip install config-patching[all]

TOML is not supported because the patching mechanism relies on null values, which are not available in TOML.

The patching mechanism in detail

Patches use JSON Merge Patch semantics.

Given this base configuration:

{
  "application": {
    "name": "example",
    "debug": false
  },
  "logging": {
    "level": "INFO",
    "format": "standard"
  }
}

A patch can contain only the keys you want to change, even if those keys are nested:

{
  "base_config_file": "base.json",
  "patch": {
    "application": {
      "debug": true
    },
    "logging": {
      "level": "DEBUG"
    }
  }
}

The resulting configuration is:

{
  "application": {
    "name": "example",
    "debug": True
  },
  "logging": {
    "level": "DEBUG",
    "format": "standard"
  }
}

Keys that are not included in the patch are left unchanged.

Removing a key

A value of null removes a key:

{
  "base_config_file": "config.json",
  "patch": {
    "logging": {
      "format": null
    }
  }
}

Replacing arrays

Arrays are replaced rather than merged:

{
  "base_config_file": "config.json",
  "patch": {
    "allowed_hosts": [
      "localhost",
      "example.org"
    ]
  }
}

The returned configuration contains the fully patched configuration.

YAML example

Patches can also be written in YAML:

base_config_file: /etc/my-library/config.yaml

patch:
  server:
    host: 0.0.0.0
    port: 8080
  logging:
    level: DEBUG

Contributing

As config-patching develops as a standalone project, there are plenty of opportunities to improve the implementation, documentation, testing, and tooling around it.

If you have an idea, find a bug, or would like to contribute a feature, please open an issue or submit a merge request.

Metadata

Release files for config-patching 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for config-patching 0.0.1
File Size Uploaded Provenance Actions
config_patching-0.0.1.tar.gz 10.7 kB Without provenance Details for config_patching-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for config-patching 0.0.1
File Interpreter ABI Platform Actions
config_patching-0.0.1-py3-none-any.whl Python 3 none any Details for config_patching-0.0.1-py3-none-any.whl

Total release size: 18.0 kB

Release files / config_patching-0.0.1.tar.gz

Download URL config_patching-0.0.1.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
12d1aa5bc73970412d28b3543f9ec2f62b2322107458eb72c072f5f980ef492e
BLAKE2b-256 checksum
How to use checksums
2f24bc1402c5055878b901c5d5af4c8e16c170c7980fa1e49a39b065b0661d0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / config_patching-0.0.1-py3-none-any.whl

Download URL config_patching-0.0.1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07ffc74208b73e320936d7d5a42b8a9855c44dda451c923c191ac02d6d650df8
BLAKE2b-256 checksum
How to use checksums
c96db7569ed6d2beecf93323e21e432f730cec363fb5e1937e9ece6865ab33c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.10.22

Security for config-patching 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page