from pathlib import Path

from ruamel import yaml

p = Path("openapi.yaml")
with p.open("r", encoding="utf8") as f:
    data = yaml.round_trip_load(f, preserve_quotes=True)

# Before, we ignored removal of holeStart and holeEnd since we were going to
# deprecate those two in favor of topMeasuredDepth and baseMeasuredDepth.
# However, now that that is done, there are no more ignore properties.
ignored_properties = set()

deleted_schemas = []
for name, schema in data["components"]["schemas"].items():
    if schema.get("deprecated") is True:
        print(f"Deleting deprecated schema {name}.")
        deleted_schemas.append(name)
        continue
    deleted_properties = []
    for property_name, property in schema.get("properties", {}).items():
        if property.get("deprecated") is True and property_name not in ignored_properties:
            deleted_properties.append(property_name)
            print(f"Deleting deprecated property {name}.{property_name}.")
    for prop_name in deleted_properties:
        del schema["properties"][prop_name]

for schema_name in deleted_schemas:
    del data["components"]["schemas"][schema_name]

with p.open("w", encoding="utf8") as f:
    yaml.round_trip_dump(data, f)
