2023-08-25 07:22:47 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from pathlib import Path
|
|
|
|
from linkml_runtime.dumpers import yaml_dumper
|
2023-08-29 05:16:58 +00:00
|
|
|
from nwb_linkml.generators.pydantic import NWBPydanticGenerator
|
2023-09-04 20:49:07 +00:00
|
|
|
from linkml.generators.sqlalchemygen import SQLAlchemyGenerator, TemplateEnum
|
2023-08-25 07:22:47 +00:00
|
|
|
from nwb_linkml import io
|
|
|
|
|
|
|
|
def generate_core_yaml(output_path:Path):
|
|
|
|
core = io.load_nwb_core()
|
|
|
|
built_schemas = core.build().schemas
|
|
|
|
for schema in built_schemas:
|
|
|
|
output_file = output_path / (schema.name + '.yaml')
|
|
|
|
yaml_dumper.dump(schema, output_file)
|
|
|
|
|
|
|
|
def generate_core_pydantic(yaml_path:Path, output_path:Path):
|
|
|
|
for schema in yaml_path.glob('*.yaml'):
|
2023-08-29 05:16:58 +00:00
|
|
|
python_name = schema.stem.replace('.', '_').replace('-', '_')
|
|
|
|
pydantic_file = (output_path / python_name).with_suffix('.py')
|
2023-08-25 07:22:47 +00:00
|
|
|
|
2023-08-29 05:16:58 +00:00
|
|
|
generator = NWBPydanticGenerator(
|
2023-08-25 07:22:47 +00:00
|
|
|
str(schema),
|
2023-09-01 03:56:21 +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()
|
|
|
|
with open(pydantic_file, 'w') as pfile:
|
|
|
|
pfile.write(gen_pydantic)
|
|
|
|
|
2023-09-04 20:49:07 +00:00
|
|
|
def generate_core_sqlalchemy(yaml_path:Path, output_path:Path):
|
|
|
|
for schema in yaml_path.glob('*.yaml'):
|
|
|
|
python_name = schema.stem.replace('.', '_').replace('-', '_')
|
|
|
|
pydantic_file = (output_path / python_name).with_suffix('.py')
|
|
|
|
|
|
|
|
generator =SQLAlchemyGenerator(
|
|
|
|
str(schema)
|
|
|
|
)
|
|
|
|
gen_pydantic = generator.generate_sqla(template=TemplateEnum.DECLARATIVE)
|
|
|
|
with open(pydantic_file, 'w') as pfile:
|
|
|
|
pfile.write(gen_pydantic)
|
|
|
|
|
2023-08-25 07:22:47 +00:00
|
|
|
def parser() -> ArgumentParser:
|
|
|
|
parser = ArgumentParser('Generate NWB core schema')
|
|
|
|
parser.add_argument(
|
|
|
|
'--yaml',
|
|
|
|
help="directory to export linkML schema to",
|
|
|
|
type=Path,
|
|
|
|
default=Path(__file__).parent.parent / 'nwb_linkml' / 'schema'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--pydantic',
|
|
|
|
help="directory to export pydantic models",
|
|
|
|
type=Path,
|
2023-09-04 20:49:07 +00:00
|
|
|
default=Path(__file__).parent.parent / 'nwb_linkml' / 'models' / 'pydantic'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--sqlalchemy',
|
|
|
|
help="directory to export sqlalchemy models",
|
|
|
|
type=Path,
|
|
|
|
default=Path(__file__).parent.parent / 'nwb_linkml' / 'models' / 'sqlalchemy'
|
2023-08-25 07:22:47 +00:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parser().parse_args()
|
|
|
|
args.yaml.mkdir(exist_ok=True)
|
|
|
|
args.pydantic.mkdir(exist_ok=True)
|
2023-09-04 20:49:07 +00:00
|
|
|
args.sqlalchemy.mkdir(exist_ok=True)
|
2023-08-25 07:22:47 +00:00
|
|
|
generate_core_yaml(args.yaml)
|
2023-09-04 20:49:07 +00:00
|
|
|
#generate_core_pydantic(args.yaml, args.pydantic)
|
|
|
|
generate_core_sqlalchemy(args.yaml, args.sqlalchemy)
|
2023-08-25 07:22:47 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|