|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# JSON config override script |
| 3 | +# This script merges two JSON files, where the second file overrides the first. |
| 4 | +# Usage: |
| 5 | +# ./merge_system_settings.py <base_file> <override_file> <output_file> |
| 6 | + |
| 7 | + |
| 8 | +import json |
| 9 | + |
| 10 | + |
| 11 | +def merge_dict(base, override): |
| 12 | + """ |
| 13 | + Merges two dictionaries, where the second dictionary overrides the first. |
| 14 | + If a key is missing from the second dictionary, the value from the first is used. |
| 15 | + If a key exists in both dictionaries and both values are dictionaries, we merge them recursively. |
| 16 | + If a key exists in both dictionaries and the values are not dictionaries, the value from the second dictionary is used. |
| 17 | +
|
| 18 | + :param base: The base dictionary. |
| 19 | + :param override: The dictionary with overrides. |
| 20 | + :return: A new dictionary with merged values. |
| 21 | + """ |
| 22 | + merged = base.copy() # Start with a copy of the base dictionary |
| 23 | + for key, value in override.items(): |
| 24 | + if key not in merged: |
| 25 | + raise KeyError(f"Key '{key}' in overrides not found in base.") |
| 26 | + # Assert types are the same if both exist |
| 27 | + base_value = merged[key] |
| 28 | + bvt, ovt = type(base_value), type(value) |
| 29 | + if bvt != ovt: |
| 30 | + raise TypeError(f"Type mismatch for key '{key}': base type {bvt} does not match override type {ovt}.") |
| 31 | + if bvt is dict: |
| 32 | + # If both are dictionaries, merge them recursively |
| 33 | + merged[key] = merge_dict(base_value, value) |
| 34 | + else: |
| 35 | + # Otherwise, override the value |
| 36 | + merged[key] = value |
| 37 | + return merged |
| 38 | + |
| 39 | + |
| 40 | +def merge_system_settings(base_file, override_file, output_file): |
| 41 | + """ |
| 42 | + Merges two YAML files containing system settings, where the second file overrides the first. |
| 43 | +
|
| 44 | + :param base_file: Path to the base system settings YAML file. |
| 45 | + :param override_file: Path to the override system settings YAML file. |
| 46 | + :param output_file: Path to save the merged output YAML file. |
| 47 | + """ |
| 48 | + with open(base_file, 'r') as f: |
| 49 | + base_settings = json.load(f) |
| 50 | + |
| 51 | + with open(override_file, 'r') as f: |
| 52 | + override_settings = json.load(f) |
| 53 | + |
| 54 | + merged_settings = merge_dict(base_settings, override_settings) |
| 55 | + |
| 56 | + with open(output_file, 'w') as f: |
| 57 | + json.dump(merged_settings, f, indent=4) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + import argparse |
| 62 | + |
| 63 | + parser = argparse.ArgumentParser(description="Merge system settings JSON files.") |
| 64 | + parser.add_argument("base_file", help="Path to the base system settings file.") |
| 65 | + parser.add_argument("override_file", help="Path to the override system settings file.") |
| 66 | + parser.add_argument("output_file", help="Path to save the merged output file.") |
| 67 | + |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + merge_system_settings(args.base_file, args.override_file, args.output_file) |
0 commit comments