Coverage for formkit_ninja / schemas / __init__.py: 65.38%
24 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-06 04:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-06 04:12 +0000
1import json
2import pathlib
3from importlib.resources import files
5from formkit_ninja import formkit_schema
6from formkit_ninja import schemas as schema_path
7from formkit_ninja.services.schema_import import SchemaImportService
10class Schemas:
11 def __init__(self):
12 # All json files
13 schema_files: list[pathlib.Path] = list(files(schema_path).glob("*.json"))
14 # Name of the schema: path
15 schemas = {path_.name[:-5]: path_ for path_ in schema_files}
16 self.schemas = schemas
18 def list_schemas(self) -> list[str]:
19 return self.schemas.keys()
21 def as_text(self, schema: str):
22 return self.schemas[schema].read_text()
24 def as_json(self, schema: str) -> dict:
25 return json.loads(self.as_text(schema))
27 def as_dict(self, schema: str):
28 return json.dumps(self.as_json(schema))
30 def import_all(self):
31 for schema_name in self.schemas.keys():
32 schema = self.as_json(schema_name)
33 pydantic_schema = formkit_schema.FormKitSchema.parse_obj([schema])
34 SchemaImportService.import_schema(pydantic_schema, label=schema_name)