Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
create_jsonschema.py 2.68 KiB
#!/usr/bin/env python3

# Copyright (C) 2023 IndiScale GmbH <www.indiscale.com>
# Copyright (C) 2023 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Create JSON-Schema according to configuration.

"""

import argparse
import json
from typing import List

import caosadvancedtools.json_schema_exporter as jsex
from caosadvancedtools.models import parser
# import tomli


def prepare_datamodel(modelfile, recordtypes: List[str], outfile: str,
                      do_not_create: List[str] = None):
    if do_not_create is None:
        do_not_create = []
    model = parser.parse_model_from_yaml(modelfile)

    exporter = jsex.JsonSchemaExporter(additional_properties=False,
                                       # additional_options_for_text_props=additional_text_options,
                                       # name_and_description_in_properties=True,
                                       name_property_for_new_records=True,
                                       do_not_create=do_not_create,
                                       # do_not_retrieve=do_not_retrieve,
                                       no_remote=True,
                                       use_rt_pool=model,
                                       )
    schemas = []
    for recordtype in recordtypes:
        schemas.append(exporter.recordtype_to_json_schema(model.get_deep(recordtype)))
    merged_schema = jsex.merge_schemas(schemas)

    with open(outfile, mode="w", encoding="utf8") as json_file:
        json.dump(merged_schema, json_file, ensure_ascii=False, indent=2)


def _parse_arguments():
    """Parse the arguments."""
    arg_parser = argparse.ArgumentParser(description='')

    return arg_parser.parse_args()


def main():
    """The main function of this script."""
    _ = _parse_arguments()
    prepare_datamodel("model_simple.yml", ["Training", "Person"], "model_schema.json",
                      do_not_create=["Organisation"])
    prepare_datamodel("model_multiple_refs.yml", ["Training", "Person"],
                      "schema_multiple_refs.json")


if __name__ == "__main__":
    main()