Compare configuration files across environments
pip install philiprehberger-config-diff
Compare configuration files across environments.
pip install philiprehberger-config-diff
from philiprehberger_config_diff import diff_files, diff_dicts
# Compare files (JSON, TOML, INI, .env)
report = diff_files("config.dev.json", "config.prod.json")
for change in report.changes:
print(change) # "+ db.name = 'proddb'" / "~ port: 3000 -> 8080"
print(report.summary())
# "Added: 3, Removed: 1, Modified: 5"
# Filter by key patterns
report = diff_files("dev.env", "prod.env", include=["DB_*"])
# Compare dicts directly
report = diff_dicts(dev_config, prod_config)
include and exclude accept compiled re.Pattern objects alongside glob strings, which is handy for redacting secret keys by suffix or prefix.
import re
from philiprehberger_config_diff import diff_dicts
report = diff_dicts(
dev_config,
prod_config,
exclude=[re.compile(r".*_token$"), re.compile(r"^secret\.")],
)
DiffReport.to_dict() returns a JSON-serializable representation, and
DiffReport.is_empty() is a quick check for "no changes" in scripts or CI gates.
import json
from philiprehberger_config_diff import diff_dicts
report = diff_dicts({"a": 1}, {"a": 2, "b": 3})
if report.is_empty():
print("No drift detected")
else:
print(json.dumps(report.to_dict(), indent=2))
# {
# "changes": [
# {"path": "a", "type": "modified", "old": 1, "new": 2},
# {"path": "b", "type": "added", "old": null, "new": 3}
# ]
# }
from philiprehberger_config_diff import unified_diff
dev = {"db": {"host": "localhost", "port": 5432}}
prod = {"db": {"host": "prod-server", "port": 5432}}
print(unified_diff(dev, prod, left_label="dev", right_label="prod"))
# --- dev
# +++ prod
# @@ -1,2 +1,2 @@
# -db.host = 'localhost'
# +db.host = 'prod-server'
# db.port = 5432
| Function / Class | Description |
|---|---|
diff_files(left, right, include=None, exclude=None) | Compare config files |
diff_dicts(left, right, include=None, exclude=None) | Compare dicts |
unified_diff(left, right, *, context=3, left_label, right_label) | Render diff -u style output |
report.changes | List of Change objects |
report.added / report.removed / report.modified | Filtered changes |
report.summary() | Change count summary |
report.is_empty() | True when the report contains no changes |
report.to_dict() | JSON-serializable {"changes": [...]} representation |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: