github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/kontrol/scripts/json/reverse_key_values.py (about) 1 """ 2 Description: 3 Reverses the key-value pairs of a given JSON 4 The use case for this script within the project is to reverse the key-value 5 pairs of the auto generated file contracts-bedrock/deployments/hardhat/.deployment 6 so that it can be fed as the `--contract-names` argument to `kontrol summary` 7 This script is used in ../make-summary-deployment.sh 8 9 Usage: 10 To reverse the order of an $input_file and save it to an $output_file run 11 `python3 reverse_key_values.py $input_file $output_file` 12 """ 13 14 import sys 15 import json 16 17 def reverse_json(input_file, output_file): 18 try: 19 with open(input_file, 'r') as file: 20 json_data = json.load(file) 21 22 reversed_json = {str(value): key[0].lower() + key[1:] for key, value in json_data.items()} 23 24 with open(output_file, 'w') as file: 25 json.dump(reversed_json, file, indent=2) 26 27 print(f"Reversed JSON saved to {output_file}") 28 29 except FileNotFoundError: 30 print(f"Error: File not found: {input_file}") 31 sys.exit(1) 32 except json.JSONDecodeError: 33 print(f"Error: Invalid JSON format in file: {input_file}") 34 sys.exit(1) 35 36 if __name__ == "__main__": 37 # Check if both input and output file paths are provided 38 if len(sys.argv) != 3: 39 print("Usage: reverse_key_values.py <input_file_path> <output_file_path>") 40 sys.exit(1) 41 42 input_file_path = sys.argv[1] 43 output_file_path = sys.argv[2] 44 45 # Execute the function to reverse JSON and save to the output file 46 reverse_json(input_file_path, output_file_path)