diff --git a/utils/modify_skeleton.py b/utils/modify_skeleton.py new file mode 100644 index 0000000..60b6ceb --- /dev/null +++ b/utils/modify_skeleton.py @@ -0,0 +1,62 @@ +CHANGES = [ + { + "description": "Fix LngString's missing __root__", + "type": "replace", + "find": "class LngString(Base):\n pass", + "replace": "class LngString(Base):\n __root__: Dict[str, List[str]]" + }, + { + "description": "Rename v2 Service properties so they are not private", + "type": "replace", + "find": "class ServiceItem1(Base):\n _id: Id = Field(..., alias='@id')\n _type: str = Field(..., alias='@type')", + "replace": "class ServiceItem1(Base):\n id: Id = Field(..., alias='@id')\n type: str = Field(..., alias='@type')" + } +] + + +def process_change(skeleton, change): + print(f"Processing change: {change['description']} (Type: {change['type']})") + + if change["type"] == "replace": + needle = skeleton.find(change["find"]) + if needle == -1: + print("Needle not found - skipping change") + else: + skeleton = skeleton.replace(change["find"], change["replace"]) + + if change["type"] == "insert": + start = skeleton.find(change["before"]) + after = skeleton.find(change["after"]) + if start == -1: + print("Before string not found - skipping change") + else: + if after == -1: + print("After string not found - skipping change") + else: + skeleton = skeleton[:start] + change["data"] + skeleton[after:] + + return skeleton + + +def modify_skeleton(): + print("Opening Skeleton file...") + skeleton = open("../iiif_prezi3/skeleton.py").read() + + print(f"Processing {len(CHANGES)} changes") + for change in CHANGES: + skeleton = process_change(skeleton, change) + + print("Changes processed, writing out fixed Skeleton") + with open("../iiif_prezi3/skeleton.py", "w") as out: + out.write(skeleton) + + +if __name__ == "__main__": + print("== Prezi3 Skeleton Fixer ==") + safety = input("WARNING: This will overwrite the existing Skeleton in-place. Continue y/n? ") + + if safety.lower() != "y": + exit() + + modify_skeleton() + print("Done!") diff --git a/utils/regenerate_skeleton.py b/utils/regenerate_skeleton.py new file mode 100644 index 0000000..27d2040 --- /dev/null +++ b/utils/regenerate_skeleton.py @@ -0,0 +1,36 @@ +import os +import shlex +import subprocess + +import requests +from modify_skeleton import modify_skeleton + +SCHEMA_LOCATION = "https://raw.githubusercontent.com/IIIF/presentation-validator/main/schema/iiif_3_0.json" +DATAMODEL_COMMAND = "datamodel-codegen --input iiif_3_0.json --input-file-type jsonschema --use-default --base-class .base.Base --output ../iiif_prezi3/skeleton.py" + +if __name__ == "__main__": + print("== Prezi3 Skeleton Regenerator ==") + safety = input("WARNING: This will overwrite the existing Skeleton in-place. Continue y/n? ") + + if safety.lower() != "y": + exit() + + print("Downloading latest JSON Schema...") + js = requests.get(SCHEMA_LOCATION) + if js.status_code == 200: + with open("iiif_3_0.json", "wb") as out: + out.write(js.content) + else: + print(f"Error retrieving JSON Schema - Status {js.status_code}") + exit() + + print("Generating Skeleton file...") + subprocess.run(shlex.split(DATAMODEL_COMMAND), check=True) + + print("Running Skeleton modifications...") + modify_skeleton() + + print("Cleaning up Schema file...") + os.remove("iiif_3_0.json") + + print("Done!")