Skip to content

Commit

Permalink
update form types
Browse files Browse the repository at this point in the history
  • Loading branch information
calmacx committed Apr 8, 2024
1 parent 439a5e4 commit 3f79f71
Show file tree
Hide file tree
Showing 8 changed files with 12,504 additions and 9 deletions.
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.

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

0 comments on commit 3f79f71

Please sign in to comment.