1#!/usr/bin/python 2# 3# Copyright (C) 2023 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18from collections import OrderedDict 19import json 20import re 21import sys 22 23# Add numbers to the names of fields to rearrange them better. 24# They will be removed from final file. 25field_substitute = { 26 "comment": "|000|comment", 27 "class": "|001|class", 28 "note": "|001|note", 29 "precise_nans": "|001|precise_nans", 30 "variants": "|001|variants", 31 "in": "|002|in", 32 "out": "|003|out", 33 "side_effects_comment": "|004|side_effects_comment", 34 "has_side_effects": "|005|has_side_effects" 35} 36 37def Version(str): 38 result = [] 39 isdigit = False 40 word = '' 41 for char in str + ('a' if str[-1:].isdigit() else '0'): 42 if char.isdigit() == isdigit: 43 word += char 44 else: 45 if isdigit: 46 result.append(('0' * 1000 + word)[-1000:]) 47 else: 48 result.append((word + ' ' * 1000)[:1000]) 49 isdigit = not isdigit 50 word = char 51 return '.'.join(result) 52 53 54def main(argv): 55 # Usage: prettify_intrinsics.py <file.json> 56 57 with open(argv[1]) as file: 58 json_intrinsics = json.load(file) 59 60 out_intrinsics = OrderedDict() 61 license = None 62 for intrinsic_name, intrinsic_body in json_intrinsics.items(): 63 new_intrinsic = {} 64 if intrinsic_name == 'License': 65 license = intrinsic_body 66 else: 67 for field, value in intrinsic_body.items(): 68 if field == 'variants': 69 new_intrinsic[field_substitute[field]] = sorted(value, key=Version) 70 else: 71 new_intrinsic[field_substitute[field]] = value 72 out_intrinsics[intrinsic_name] = new_intrinsic 73 74 text = json.dumps(out_intrinsics, indent=2, sort_keys=True) 75 76 # Add license back if present 77 if license: 78 license = json.dumps([license], indent=2) 79 text = '{\n "License":' + license[3:-2] + ',\n' + text[2:] 80 81 # Remove numbers from names of fields 82 text = re.sub('[|][0-9][0-9][0-9][|]', '', text) 83 84 def replace_if_short(match): 85 match = match.group() 86 replace = ' '.join(match.split()) 87 if len(replace) < 90: 88 return replace 89 else: 90 return match 91 92 # Make short lists one-liners 93 text = re.sub('[\[{][^][{}]*[]}]', replace_if_short, text) 94 95 # Remove trailing spaces 96 text = re.sub(' $', '', text, flags=re.MULTILINE) 97 98 # Fix the license 99 text = re.sub('\\\\u201c', '“', text, flags=re.MULTILINE) 100 text = re.sub('\\\\u201d', '”', text, flags=re.MULTILINE) 101 102 with open(argv[1], 'w') as file: 103 print(text, file=file) 104 105if __name__ == '__main__': 106 sys.exit(main(sys.argv)) 107