2023-08-24 02:56:09 +00:00
|
|
|
import pdb
|
|
|
|
|
2023-08-22 04:43:02 +00:00
|
|
|
import pytest
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
from .fixtures import nwb_core_fixture, tmp_output_dir
|
|
|
|
from linkml_runtime.dumpers import yaml_dumper
|
2023-08-24 02:56:09 +00:00
|
|
|
from linkml.generators import PydanticGenerator
|
2023-08-29 05:16:58 +00:00
|
|
|
from nwb_linkml.generators.pydantic import NWBPydanticGenerator
|
2023-08-22 04:43:02 +00:00
|
|
|
|
|
|
|
from nwb_linkml.lang_elements import NwbLangSchema
|
|
|
|
|
|
|
|
def test_generate_nwblang(tmp_output_dir):
|
|
|
|
output_file = (tmp_output_dir / NwbLangSchema.name).with_suffix('.yml')
|
|
|
|
yaml_dumper.dump(NwbLangSchema, output_file)
|
|
|
|
|
2023-08-24 02:56:09 +00:00
|
|
|
def test_generate_core(nwb_core_fixture, tmp_output_dir):
|
|
|
|
schemas = nwb_core_fixture.build().schemas
|
2023-08-29 05:16:58 +00:00
|
|
|
|
|
|
|
(tmp_output_dir / 'schema').mkdir(exist_ok=True)
|
|
|
|
|
2023-08-24 02:56:09 +00:00
|
|
|
for schema in schemas:
|
2023-08-29 05:16:58 +00:00
|
|
|
output_file = tmp_output_dir / 'schema' / (schema.name + '.yaml')
|
2023-08-24 02:56:09 +00:00
|
|
|
yaml_dumper.dump(schema, output_file)
|
|
|
|
|
|
|
|
@pytest.mark.depends(on=['test_generate_core'])
|
|
|
|
def test_generate_pydantic(tmp_output_dir):
|
2023-08-25 07:22:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# core_file = tmp_output_dir / 'core.yaml'
|
|
|
|
# pydantic_file = tmp_output_dir / 'core.py'
|
|
|
|
|
2023-08-29 05:16:58 +00:00
|
|
|
(tmp_output_dir / 'models').mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
for schema in (tmp_output_dir / 'schema').glob('*.yaml'):
|
|
|
|
# python friendly name
|
|
|
|
python_name = schema.stem.replace('.', '_').replace('-','_')
|
2023-08-25 07:22:47 +00:00
|
|
|
|
2023-08-29 05:16:58 +00:00
|
|
|
pydantic_file = (schema.parent.parent / 'models' / python_name).with_suffix('.py')
|
|
|
|
|
|
|
|
generator = NWBPydanticGenerator(
|
2023-08-25 07:22:47 +00:00
|
|
|
str(schema),
|
2023-08-31 22:36:58 +00:00
|
|
|
pydantic_version='2',
|
2023-08-25 07:22:47 +00:00
|
|
|
emit_metadata=True,
|
|
|
|
gen_classvars=True,
|
|
|
|
gen_slots=True
|
|
|
|
)
|
|
|
|
gen_pydantic = generator.serialize()
|
2023-08-29 05:16:58 +00:00
|
|
|
|
|
|
|
|
2023-08-25 07:22:47 +00:00
|
|
|
with open(pydantic_file, 'w') as pfile:
|
|
|
|
pfile.write(gen_pydantic)
|