Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/form hydration #36

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,521 changes: 1,521 additions & 0 deletions docs/GWDM/1.0.form.json

Large diffs are not rendered by default.

2,161 changes: 2,161 additions & 0 deletions docs/GWDM/1.1.form.json

Large diffs are not rendered by default.

2,207 changes: 2,207 additions & 0 deletions docs/GWDM/1.2.form.json

Large diffs are not rendered by default.

1,484 changes: 1,484 additions & 0 deletions docs/HDRUK/2.1.2.form.json

Large diffs are not rendered by default.

1,484 changes: 1,484 additions & 0 deletions docs/HDRUK/2.1.3.form.json

Large diffs are not rendered by default.

1,786 changes: 1,786 additions & 0 deletions docs/HDRUK/2.2.0.form.json

Large diffs are not rendered by default.

1,833 changes: 1,833 additions & 0 deletions docs/HDRUK/2.2.1.form.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions hdr_schemata/models/HDRUK/v2_2_1/Origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Config:

purpose: Optional[List[Purpose]] = Field(None, **an.purpose.__dict__)
source: Optional[List[Source]] = Field(None, **an.source.__dict__)
collectionSituation: Optional[
List[Setting]
] = Field(None, **an.collectionSituation.__dict__)
collectionSituation: Optional[List[Setting]] = Field(
None, **an.collectionSituation.__dict__
)
2 changes: 1 addition & 1 deletion hdr_schemata/models/HDRUK/v2_2_1/Provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class Provenance(BaseProvenance):

origin: Optional[Origin] = Field(
None, description=an.origin.description, title=an.origin.title
)
)
37 changes: 28 additions & 9 deletions hdr_schemata/utils/create_markdown.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydantic import BaseModel, RootModel
import pandas as pd
import copy
import json
import typing
import enum
Expand Down Expand Up @@ -80,7 +81,7 @@ def get_fields(structure, model: type[BaseModel]):
"title": field.title,
"examples": field.examples,
"type": type_names,
# "types": _types,
"types": _types,
"is_list": is_list,
"is_optional": is_optional,
}
Expand Down Expand Up @@ -133,6 +134,7 @@ def json_to_markdown(structure, level=2):


def traverse_structure(data, form, parent=None):
data = copy.deepcopy(data)
for item in data:
k = item.pop("name")
if parent:
Expand All @@ -142,30 +144,47 @@ def traverse_structure(data, form, parent=None):
traverse_structure(subItems, form, parent=k)

types = item.pop("types")
info = None
infos = []
for t in types:
info = None
try:
if t and issubclass(t, RootModel):
info = t.model_json_schema()
if t:
if issubclass(t, RootModel):
info = t.model_json_schema()
else:
info = t.__name__
except:
...
if type(t) == enum.EnumMeta:
info = {"type": "string", "options": [m.value for m in t]}

if info:
infos.append(info)

_ = item.pop("type")
item["types"] = info

item["types"] = infos
form[k] = item


def create_markdown(Model, path, name):

def remove_types(data):
for d in data:
d.pop("types")
if d.get("subItems", None):
remove_types(d["subItems"])

structure = []
get_fields(structure, Model)

# form = {}
# traverse_structure(structure, form)
# print(json.dumps(form, indent=6))
form = {}
traverse_structure(structure, form)
with open(f"{path}/{name}.form.json", "w") as f:
json.dump(form, f, indent=6)

with open(f"{path}/{name}.structure.json", "w") as f:
print(json.dumps(structure, indent=6))
remove_types(structure)
json.dump(structure, f, indent=6)

md = json_to_markdown(structure)
Expand Down
10 changes: 10 additions & 0 deletions pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
cd hdr_schemata/models/HDRUK
python create_json_schema.py
cd ../GWDM
python create_json_schema.py

cd ../../../


python hdr_schemata/utils/create_markdown.py
3 changes: 0 additions & 3 deletions tester.py

This file was deleted.

Loading