|
5 | 5 | import logging |
6 | 6 | import os |
7 | 7 | import pathlib |
8 | | -import platform |
9 | 8 | import re |
10 | | -import sys |
11 | 9 | import warnings |
12 | 10 | from copy import deepcopy |
13 | 11 | from enum import Enum |
@@ -415,8 +413,6 @@ class FetchSchemaParam(BaseModel): |
415 | 413 | "replace" |
416 | 414 | # type 'FetchSchemaMode' requires: 'from __future__ import annotations' |
417 | 415 | ) |
418 | | - legacy_generator: Optional[bool] = False |
419 | | - """uses legacy command line for code generation if true""" |
420 | 416 | generate_annotations: Optional[bool] = True |
421 | 417 | """generate custom schema keywords in Fields and Classes. |
422 | 418 | Required to update the schema in OSW without information loss""" |
@@ -466,7 +462,6 @@ def fetch_schema( |
466 | 462 | schema_title=schema_title, |
467 | 463 | mode=mode, |
468 | 464 | final=last, |
469 | | - legacy_generator=fetchSchemaParam.legacy_generator, |
470 | 465 | generate_annotations=fetchSchemaParam.generate_annotations, |
471 | 466 | generator_options=fetchSchemaParam.generator_options, |
472 | 467 | offline_pages=fetchSchemaParam.offline_pages, |
@@ -521,8 +516,6 @@ class _FetchSchemaParam(BaseModel): |
521 | 516 | "replace" |
522 | 517 | # type 'FetchSchemaMode' requires: 'from __future__ import annotations' |
523 | 518 | ) |
524 | | - legacy_generator: Optional[bool] = False |
525 | | - """uses legacy command line for code generation if true""" |
526 | 519 | generate_annotations: Optional[bool] = False |
527 | 520 | """generate custom schema keywords in Fields and Classes. |
528 | 521 | Required to update the schema in OSW without information loss""" |
@@ -658,121 +651,84 @@ def _fetch_schema( |
658 | 651 | "output_model_type", "pydantic.BaseModel" |
659 | 652 | ) |
660 | 653 | if root: |
661 | | - if fetchSchemaParam.legacy_generator: |
662 | | - exec_name = "datamodel-codegen" |
663 | | - # default: assume datamodel-codegen is in PATH |
664 | | - exec_path = exec_name |
665 | | - if platform.system() == "Windows": |
666 | | - exec_name += ".exe" |
667 | | - exec_path = os.path.join( |
668 | | - os.path.dirname(os.path.abspath(sys.executable)), exec_name |
669 | | - ) |
670 | | - if not os.path.isfile(exec_path): |
671 | | - exec_path = os.path.join( |
672 | | - os.path.dirname(os.path.abspath(sys.executable)), |
673 | | - "Scripts", |
674 | | - exec_name, |
675 | | - ) |
676 | | - if not os.path.isfile(exec_path): |
677 | | - print("Error: datamodel-codegen not found") |
678 | | - return |
679 | | - os.system( |
680 | | - f"{exec_path} \ |
681 | | - --input {schema_path} \ |
682 | | - --input-file-type jsonschema \ |
683 | | - --output {temp_model_path} \ |
684 | | - --base-class opensemantic.v1.OswBaseModel \ |
685 | | - --use-default \ |
686 | | - --use-unique-items-as-set \ |
687 | | - --enum-field-as-literal all \ |
688 | | - --use-title-as-name \ |
689 | | - --use-schema-description \ |
690 | | - --use-field-description \ |
691 | | - --encoding utf-8 \ |
692 | | - --use-double-quotes \ |
693 | | - --collapse-root-models \ |
694 | | - --reuse-model \ |
695 | | - " |
696 | | - ) |
697 | | - else: |
698 | | - # suppress deprecation warnings from pydantic |
699 | | - # see https://github.com/koxudaxi/datamodel-code-generator/issues/2213 |
700 | | - warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20) |
701 | | - |
702 | | - if fetchSchemaParam.generate_annotations: |
703 | | - # monkey patch class |
704 | | - datamodel_code_generator.parser.jsonschema.JsonSchemaParser = ( |
705 | | - OOLDJsonSchemaParser |
706 | | - ) |
707 | | - datamodel_code_generator.generate( |
708 | | - input_=pathlib.Path(schema_path), |
709 | | - input_file_type="jsonschema", |
710 | | - output=pathlib.Path(temp_model_path), |
711 | | - base_class=( |
712 | | - "opensemantic.v1.OswBaseModel" |
713 | | - if data_model_type == "pydantic.BaseModel" |
714 | | - else "opensemantic.OswBaseModel" |
715 | | - ), |
716 | | - # use_default=True, |
717 | | - apply_default_values_for_required_fields=True, |
718 | | - use_unique_items_as_set=True, |
719 | | - enum_field_as_literal=datamodel_code_generator.LiteralType.Off, |
720 | | - # will create MyEnum(str, Enum) instead of MyEnum(Enum) |
721 | | - use_subclass_enum=True, |
722 | | - set_default_enum_member=True, |
723 | | - use_title_as_name=True, |
724 | | - use_schema_description=True, |
725 | | - use_field_description=True, |
726 | | - encoding="utf-8", |
727 | | - use_double_quotes=True, |
728 | | - collapse_root_models=True, |
729 | | - reuse_model=True, |
730 | | - field_include_all_keys=True, |
731 | | - allof_class_hierarchy=datamodel_code_generator.AllOfClassHierarchy.Always, |
732 | | - additional_imports=( |
733 | | - ["uuid.uuid4", "pydantic.ConfigDict"] |
734 | | - if data_model_type != "pydantic.BaseModel" |
735 | | - else ["uuid.uuid4"] |
736 | | - ), |
737 | | - **(fetchSchemaParam.generator_options or {}), |
| 654 | + # suppress deprecation warnings from pydantic |
| 655 | + # see https://github.com/koxudaxi/datamodel-code-generator/issues/2213 |
| 656 | + warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20) |
| 657 | + |
| 658 | + if fetchSchemaParam.generate_annotations: |
| 659 | + # monkey patch class |
| 660 | + datamodel_code_generator.parser.jsonschema.JsonSchemaParser = ( |
| 661 | + OOLDJsonSchemaParser |
738 | 662 | ) |
| 663 | + datamodel_code_generator.generate( |
| 664 | + input_=pathlib.Path(schema_path), |
| 665 | + input_file_type="jsonschema", |
| 666 | + output=pathlib.Path(temp_model_path), |
| 667 | + base_class=( |
| 668 | + "opensemantic.v1.OswBaseModel" |
| 669 | + if data_model_type == "pydantic.BaseModel" |
| 670 | + else "opensemantic.OswBaseModel" |
| 671 | + ), |
| 672 | + # use_default=True, |
| 673 | + apply_default_values_for_required_fields=True, |
| 674 | + use_unique_items_as_set=True, |
| 675 | + enum_field_as_literal=datamodel_code_generator.LiteralType.Off, |
| 676 | + # will create MyEnum(str, Enum) instead of MyEnum(Enum) |
| 677 | + use_subclass_enum=True, |
| 678 | + set_default_enum_member=True, |
| 679 | + use_title_as_name=True, |
| 680 | + use_schema_description=True, |
| 681 | + use_field_description=True, |
| 682 | + encoding="utf-8", |
| 683 | + use_double_quotes=True, |
| 684 | + collapse_root_models=True, |
| 685 | + reuse_model=True, |
| 686 | + field_include_all_keys=True, |
| 687 | + allof_class_hierarchy=datamodel_code_generator.AllOfClassHierarchy.Always, |
| 688 | + additional_imports=( |
| 689 | + ["uuid.uuid4", "pydantic.ConfigDict"] |
| 690 | + if data_model_type != "pydantic.BaseModel" |
| 691 | + else ["uuid.uuid4"] |
| 692 | + ), |
| 693 | + **(fetchSchemaParam.generator_options or {}), |
| 694 | + ) |
739 | 695 |
|
740 | | - # note: we could use OOLDJsonSchemaParser directly (see below), |
741 | | - # but datamodel_code_generator.generate |
742 | | - # does some pre- and postprocessing we do not want to duplicate |
743 | | - |
744 | | - # data_model_type = datamodel_code_generator.DataModelType.PydanticBaseModel |
745 | | - # #data_model_type = DataModelType.PydanticV2BaseModel |
746 | | - # target_python_version = datamodel_code_generator.PythonVersion.PY_38 |
747 | | - # data_model_types = datamodel_code_generator.model.get_data_model_types( |
748 | | - # data_model_type, target_python_version |
749 | | - # ) |
750 | | - # parser = OOLDJsonSchemaParserFixedRefs( |
751 | | - # source=pathlib.Path(schema_path), |
752 | | - |
753 | | - # base_class="opensemantic.OswBaseModel", |
754 | | - # data_model_type=data_model_types.data_model, |
755 | | - # data_model_root_type=data_model_types.root_model, |
756 | | - # data_model_field_type=data_model_types.field_model, |
757 | | - # data_type_manager_type=data_model_types.data_type_manager, |
758 | | - # target_python_version=target_python_version, |
759 | | - |
760 | | - # #use_default=True, |
761 | | - # apply_default_values_for_required_fields=True, |
762 | | - # use_unique_items_as_set=True, |
763 | | - # enum_field_as_literal=datamodel_code_generator.LiteralType.All, |
764 | | - # use_title_as_name=True, |
765 | | - # use_schema_description=True, |
766 | | - # use_field_description=True, |
767 | | - # encoding="utf-8", |
768 | | - # use_double_quotes=True, |
769 | | - # collapse_root_models=True, |
770 | | - # reuse_model=True, |
771 | | - # #field_include_all_keys=True |
772 | | - # ) |
773 | | - # result = parser.parse() |
774 | | - # with open(temp_model_path, "w", encoding="utf-8") as f: |
775 | | - # f.write(result) |
| 696 | + # note: we could use OOLDJsonSchemaParser directly (see below), |
| 697 | + # but datamodel_code_generator.generate |
| 698 | + # does some pre- and postprocessing we do not want to duplicate |
| 699 | + |
| 700 | + # data_model_type = datamodel_code_generator.DataModelType.PydanticBaseModel |
| 701 | + # #data_model_type = DataModelType.PydanticV2BaseModel |
| 702 | + # target_python_version = datamodel_code_generator.PythonVersion.PY_38 |
| 703 | + # data_model_types = datamodel_code_generator.model.get_data_model_types( |
| 704 | + # data_model_type, target_python_version |
| 705 | + # ) |
| 706 | + # parser = OOLDJsonSchemaParserFixedRefs( |
| 707 | + # source=pathlib.Path(schema_path), |
| 708 | + |
| 709 | + # base_class="opensemantic.OswBaseModel", |
| 710 | + # data_model_type=data_model_types.data_model, |
| 711 | + # data_model_root_type=data_model_types.root_model, |
| 712 | + # data_model_field_type=data_model_types.field_model, |
| 713 | + # data_type_manager_type=data_model_types.data_type_manager, |
| 714 | + # target_python_version=target_python_version, |
| 715 | + |
| 716 | + # #use_default=True, |
| 717 | + # apply_default_values_for_required_fields=True, |
| 718 | + # use_unique_items_as_set=True, |
| 719 | + # enum_field_as_literal=datamodel_code_generator.LiteralType.All, |
| 720 | + # use_title_as_name=True, |
| 721 | + # use_schema_description=True, |
| 722 | + # use_field_description=True, |
| 723 | + # encoding="utf-8", |
| 724 | + # use_double_quotes=True, |
| 725 | + # collapse_root_models=True, |
| 726 | + # reuse_model=True, |
| 727 | + # #field_include_all_keys=True |
| 728 | + # ) |
| 729 | + # result = parser.parse() |
| 730 | + # with open(temp_model_path, "w", encoding="utf-8") as f: |
| 731 | + # f.write(result) |
776 | 732 |
|
777 | 733 | # see https://koxudaxi.github.io/datamodel-code-generator/ |
778 | 734 | # --base-class OswBaseModel: use a custom base class |
|
0 commit comments