|
| 1 | +# This script changes the speedBooster and bypassesDoorShell enums to have pure |
| 2 | +# string values, instead of a mix of boolean and string. |
| 3 | +# |
| 4 | +# To use, run "PYTHONPATH=. python migrations/stringify_enum_types.py" |
| 5 | +# from a working directory of "sm-json-data/scripts". |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | +import argparse |
| 9 | +import format_json |
| 10 | + |
| 11 | +argparser = argparse.ArgumentParser() |
| 12 | +argparser.add_argument( |
| 13 | + "--path", type=Path, default="..", help="Path to the repo base directory" |
| 14 | +) |
| 15 | +args = argparser.parse_args() |
| 16 | + |
| 17 | + |
| 18 | +def remap_values(value, mapping): |
| 19 | + """Update object value by looking it up in mapping, leaving it alone if |
| 20 | + it's already updated""" |
| 21 | + if isinstance(value, list): |
| 22 | + for v in value: |
| 23 | + remap_values(v, mapping) |
| 24 | + elif isinstance(value, dict): |
| 25 | + for k, v in value.items(): |
| 26 | + if k in mapping: |
| 27 | + assert ( |
| 28 | + v in mapping[k] or v in mapping[k].values() |
| 29 | + ), f"Unexpected value of '{k}': '{v}'" |
| 30 | + if v in mapping[k]: |
| 31 | + value[k] = mapping[k][v] |
| 32 | + else: |
| 33 | + remap_values(v, mapping) |
| 34 | + else: |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +def update_definitions(value, mapping): |
| 39 | + """Update type definition by looking it up in mapping, replacing the |
| 40 | + values of the fields specified in the mapping""" |
| 41 | + |
| 42 | + def is_type_definition(v): |
| 43 | + return isinstance(v, dict) |
| 44 | + |
| 45 | + if isinstance(value, list): |
| 46 | + for v in value: |
| 47 | + update_definitions(v, mapping) |
| 48 | + elif isinstance(value, dict): |
| 49 | + for k, v in value.items(): |
| 50 | + if k in mapping and is_type_definition(v): |
| 51 | + value[k].update(mapping[k]) |
| 52 | + else: |
| 53 | + update_definitions(v, mapping) |
| 54 | + else: |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +region_path = args.path / "region" |
| 59 | +schema_path = args.path / "schema" |
| 60 | + |
| 61 | +if not region_path.exists(): |
| 62 | + raise FileNotFoundError(f"Path {region_path} does not exist") |
| 63 | +if not schema_path.exists(): |
| 64 | + raise FileNotFoundError(f"Path {schema_path} does not exist") |
| 65 | + |
| 66 | +for path in sorted(region_path.glob("**/*.json")): |
| 67 | + room_json = json.load(path.open("r")) |
| 68 | + schema = room_json.get("$schema") |
| 69 | + if schema is None or schema.split("/")[-1] != "m3-room.schema.json": |
| 70 | + continue |
| 71 | + print("Processing", path) |
| 72 | + remap_values( |
| 73 | + room_json, |
| 74 | + { |
| 75 | + "speedBooster": {True: "yes", False: "no", "any": "any"}, |
| 76 | + "bypassesDoorShell": {True: "yes", False: "no", "free": "free"}, |
| 77 | + }, |
| 78 | + ) |
| 79 | + path.write_text(format_json.format(room_json, indent=2)) |
| 80 | + |
| 81 | +for path in sorted(schema_path.glob("**/*.json")): |
| 82 | + schema_json = json.load(path.open("r")) |
| 83 | + schema = schema_json.get("$schema") |
| 84 | + if schema is None: |
| 85 | + continue |
| 86 | + print("Processing", path) |
| 87 | + update_definitions( |
| 88 | + schema_json, |
| 89 | + { |
| 90 | + "speedBooster": { |
| 91 | + "type": "string", |
| 92 | + "enum": ["yes", "no", "any"], |
| 93 | + }, |
| 94 | + "bypassesDoorShell": { |
| 95 | + "type": "string", |
| 96 | + "enum": ["yes", "no", "free"], |
| 97 | + "default": "no", |
| 98 | + }, |
| 99 | + }, |
| 100 | + ) |
| 101 | + path.write_text(format_json.format(schema_json, indent=2)) |
0 commit comments