Skip to content

Commit

Permalink
Merge pull request #141 from iiif-prezi/modify-skeleton
Browse files Browse the repository at this point in the history
Skeleton utilities
  • Loading branch information
digitaldogsbody committed Dec 24, 2022
2 parents de3e556 + e47a82f commit 585bd10
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
62 changes: 62 additions & 0 deletions utils/modify_skeleton.py
Original file line number Diff line number Diff line change
@@ -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!")
36 changes: 36 additions & 0 deletions utils/regenerate_skeleton.py
Original file line number Diff line number Diff line change
@@ -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!")

0 comments on commit 585bd10

Please sign in to comment.