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

24 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-23 06:00 +0000

1import json 

2import pathlib 

3from importlib.resources import files 

4 

5from formkit_ninja import schemas as schema_path 

6from formkit_ninja.formkit_schema import FormKitNode 

7 

8 

9class 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]