Coverage for formkit_ninja/schemas/__init__.py: 62%

1 import json

2 import pathlib

3 from importlib.resources import files

4

5 from formkit_ninja import schemas as schema_path

6 from formkit_ninja.formkit_schema import FormKitNode

7

8

9 class Schemas:

10 def __init__(self):

11 # All json files

12 schema_files: list[pathlib.Path] = list(files(schema_path).glob("*.json"))

13 # Name of the schema: path

14 schemas = {path_.name[:-5]: path_ for path_ in schema_files}

15 self.schemas = schemas

16

17 def list_schemas(self) -> list[str]:

18 return self.schemas.keys()

19

20 def as_text(self, schema: str):

21 return self.schemas[schema].read_text()

22

23 def as_json(self, schema: str) -> dict:

24 return json.loads(self.as_text(schema))

25

26 def as_dict(self, schema: str):

27 return json.dumps(self.as_json(schema))

28

29 def import_all(self):

30 from formkit_ninja.models import FormKitSchemaNode

31

32 for schema in self.schemas.keys():

33 node: FormKitNode = FormKitNode.parse_obj(self.as_json(schema))

34 parsed_node = node.__root__

35 list(FormKitSchemaNode.from_pydantic(parsed_node))[0]