-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from iiif-prezi/modify-skeleton
Skeleton utilities
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |