def __init__( self, source: Union[str, Path, List[Path], ParseResult], *, data_model_type: Type[DataModel] = pydantic_model.BaseModel, data_model_root_type: Type[DataModel] = pydantic_model.CustomRootType, data_type_manager_type: Type[DataTypeManager] = pydantic_model. DataTypeManager, data_model_field_type: Type[DataModelFieldBase] = pydantic_model. DataModelField, base_class: Optional[str] = None, custom_template_dir: Optional[Path] = None, extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]] = None, target_python_version: PythonVersion = PythonVersion.PY_37, dump_resolve_reference_action: Optional[Callable[[Iterable[str]], str]] = None, validation: bool = False, field_constraints: bool = False, snake_case_field: bool = False, strip_default_none: bool = False, aliases: Optional[Mapping[str, str]] = None, allow_population_by_field_name: bool = False, apply_default_values_for_required_fields: bool = False, force_optional_for_required_fields: bool = False, class_name: Optional[str] = None, use_standard_collections: bool = False, base_path: Optional[Path] = None, use_schema_description: bool = False, reuse_model: bool = False, encoding: str = 'utf-8', enum_field_as_literal: Optional[LiteralType] = None, set_default_enum_member: bool = False, strict_nullable: bool = False, use_generic_container_types: bool = False, enable_faux_immutability: bool = False, remote_text_cache: Optional[DefaultPutDict[str, str]] = None, disable_appending_item_suffix: bool = False, strict_types: Optional[Sequence[StrictTypes]] = None, empty_enum_field_name: Optional[str] = None, custom_class_name_generator: Optional[Callable[[str], str]] = None, ): super().__init__( source=source, data_model_type=data_model_type, data_model_root_type=data_model_root_type, data_type_manager_type=data_type_manager_type, data_model_field_type=data_model_field_type, base_class=base_class, custom_template_dir=custom_template_dir, extra_template_data=extra_template_data, target_python_version=target_python_version, dump_resolve_reference_action=dump_resolve_reference_action, validation=validation, field_constraints=field_constraints, snake_case_field=snake_case_field, strip_default_none=strip_default_none, aliases=aliases, allow_population_by_field_name=allow_population_by_field_name, apply_default_values_for_required_fields= apply_default_values_for_required_fields, force_optional_for_required_fields= force_optional_for_required_fields, class_name=class_name, use_standard_collections=use_standard_collections, base_path=base_path, use_schema_description=use_schema_description, reuse_model=reuse_model, encoding=encoding, enum_field_as_literal=enum_field_as_literal, set_default_enum_member=set_default_enum_member, strict_nullable=strict_nullable, use_generic_container_types=use_generic_container_types, enable_faux_immutability=enable_faux_immutability, remote_text_cache=remote_text_cache, disable_appending_item_suffix=disable_appending_item_suffix, strict_types=strict_types, empty_enum_field_name=empty_enum_field_name, custom_class_name_generator=custom_class_name_generator, ) self.remote_object_cache: DefaultPutDict[str, Dict[str, Any]] = DefaultPutDict() self.raw_obj: Dict[Any, Any] = {} self._root_id: Optional[str] = None self._root_id_base_path: Optional[str] = None self.reserved_refs: DefaultDict[Tuple[str], Set[str]] = defaultdict(set)
def __init__( self, source: Union[str, Path, List[Path], ParseResult], *, data_model_type: Type[DataModel] = pydantic_model.BaseModel, data_model_root_type: Type[DataModel] = pydantic_model.CustomRootType, data_type_manager_type: Type[DataTypeManager] = pydantic_model. DataTypeManager, data_model_field_type: Type[DataModelFieldBase] = pydantic_model. DataModelField, base_class: Optional[str] = None, custom_template_dir: Optional[Path] = None, extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]] = None, target_python_version: PythonVersion = PythonVersion.PY_37, dump_resolve_reference_action: Optional[Callable[[Iterable[str]], str]] = None, validation: bool = False, field_constraints: bool = False, snake_case_field: bool = False, strip_default_none: bool = False, aliases: Optional[Mapping[str, str]] = None, allow_population_by_field_name: bool = False, apply_default_values_for_required_fields: bool = False, force_optional_for_required_fields: bool = False, class_name: Optional[str] = None, use_standard_collections: bool = False, base_path: Optional[Path] = None, use_schema_description: bool = False, reuse_model: bool = False, encoding: str = 'utf-8', enum_field_as_literal: Optional[LiteralType] = None, set_default_enum_member: bool = False, strict_nullable: bool = False, use_generic_container_types: bool = False, enable_faux_immutability: bool = False, remote_text_cache: Optional[DefaultPutDict[str, str]] = None, disable_appending_item_suffix: bool = False, strict_types: Optional[Sequence[StrictTypes]] = None, empty_enum_field_name: Optional[str] = None, custom_class_name_generator: Optional[Callable[ [str], str]] = title_to_class_name, field_extra_keys: Optional[Set[str]] = None, field_include_all_keys: bool = False, ): self.data_type_manager: DataTypeManager = data_type_manager_type( target_python_version, use_standard_collections, use_generic_container_types, strict_types, ) self.data_model_type: Type[DataModel] = data_model_type self.data_model_root_type: Type[DataModel] = data_model_root_type self.data_model_field_type: Type[ DataModelFieldBase] = data_model_field_type self.imports: Imports = Imports() self.base_class: Optional[str] = base_class self.target_python_version: PythonVersion = target_python_version self.results: List[DataModel] = [] self.dump_resolve_reference_action: Optional[Callable[ [Iterable[str]], str]] = dump_resolve_reference_action self.validation: bool = validation self.field_constraints: bool = field_constraints self.snake_case_field: bool = snake_case_field self.strip_default_none: bool = strip_default_none self.apply_default_values_for_required_fields: bool = ( apply_default_values_for_required_fields) self.force_optional_for_required_fields: bool = ( force_optional_for_required_fields) self.use_schema_description: bool = use_schema_description self.reuse_model: bool = reuse_model self.encoding: str = encoding self.enum_field_as_literal: Optional[ LiteralType] = enum_field_as_literal self.set_default_enum_member: bool = set_default_enum_member self.strict_nullable: bool = strict_nullable self.use_generic_container_types: bool = use_generic_container_types self.enable_faux_immutability: bool = enable_faux_immutability self.custom_class_name_generator: Optional[Callable[ [str], str]] = custom_class_name_generator self.field_extra_keys: Set[str] = field_extra_keys or set() self.field_include_all_keys: bool = field_include_all_keys self.remote_text_cache: DefaultPutDict[str, str] = (remote_text_cache or DefaultPutDict()) self.current_source_path: Optional[Path] = None if base_path: self.base_path = base_path elif isinstance(source, Path): self.base_path = (source.absolute() if source.is_dir() else source.absolute().parent) else: self.base_path = Path.cwd() self.source: Union[str, Path, List[Path], ParseResult] = source self.custom_template_dir = custom_template_dir self.extra_template_data: DefaultDict[ str, Any] = extra_template_data or defaultdict(dict) if allow_population_by_field_name: self.extra_template_data[ALL_MODEL][ 'allow_population_by_field_name'] = True if enable_faux_immutability: self.extra_template_data[ALL_MODEL]['allow_mutation'] = False self.model_resolver = ModelResolver( base_url=source.geturl() if isinstance(source, ParseResult) else None, singular_name_suffix='' if disable_appending_item_suffix else None, aliases=aliases, empty_field_name=empty_enum_field_name, snake_case_field=snake_case_field, custom_class_name_generator=custom_class_name_generator, base_path=self.base_path, ) self.class_name: Optional[str] = class_name
def generate( input_: Union[Path, str, ParseResult], *, input_filename: Optional[str] = None, input_file_type: InputFileType = InputFileType.Auto, output: Optional[Path] = None, target_python_version: PythonVersion = PythonVersion.PY_37, base_class: str = DEFAULT_BASE_CLASS, custom_template_dir: Optional[Path] = None, extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]] = None, validation: bool = False, field_constraints: bool = False, snake_case_field: bool = False, strip_default_none: bool = False, aliases: Optional[Mapping[str, str]] = None, disable_timestamp: bool = False, allow_population_by_field_name: bool = False, apply_default_values_for_required_fields: bool = False, force_optional_for_required_fields: bool = False, class_name: Optional[str] = None, use_standard_collections: bool = False, use_schema_description: bool = False, reuse_model: bool = False, encoding: str = 'utf-8', enum_field_as_literal: Optional[LiteralType] = None, set_default_enum_member: bool = False, strict_nullable: bool = False, use_generic_container_types: bool = False, enable_faux_immutability: bool = False, disable_appending_item_suffix: bool = False, strict_types: Optional[Sequence[StrictTypes]] = None, empty_enum_field_name: Optional[str] = None, custom_class_name_generator: Optional[Callable[[str], str]] = None, ) -> None: remote_text_cache: DefaultPutDict[str, str] = DefaultPutDict() if isinstance(input_, str): input_text: Optional[str] = input_ elif isinstance(input_, ParseResult): from datamodel_code_generator.http import get_body input_text = remote_text_cache.get_or_put(input_.geturl(), default_factory=get_body) else: input_text = None if isinstance(input_, Path) and not input_.is_absolute(): input_ = input_.expanduser().resolve() if input_file_type == InputFileType.Auto: try: input_text_ = (get_first_file(input_).read_text( encoding=encoding) if isinstance(input_, Path) else input_text) input_file_type = ( InputFileType.OpenAPI if is_openapi(input_text_) # type: ignore else InputFileType.JsonSchema) except: raise Error('Invalid file format') if input_file_type == InputFileType.OpenAPI: from datamodel_code_generator.parser.openapi import OpenAPIParser parser_class: Type[Parser] = OpenAPIParser else: from datamodel_code_generator.parser.jsonschema import JsonSchemaParser parser_class = JsonSchemaParser if input_file_type in ( InputFileType.Json, InputFileType.Yaml, InputFileType.Dict, InputFileType.CSV, ): try: if isinstance(input_, Path) and input_.is_dir(): # pragma: no cover raise Error(f'Input must be a file for {input_file_type}') obj: Dict[Any, Any] if input_file_type == InputFileType.CSV: import csv def get_header_and_first_line( csv_file: IO[str]) -> Dict[str, Any]: csv_reader = csv.DictReader(csv_file) return dict( zip(csv_reader.fieldnames, next(csv_reader))) # type: ignore if isinstance(input_, Path): with input_.open(encoding=encoding) as f: obj = get_header_and_first_line(f) else: import io obj = get_header_and_first_line( io.StringIO(input_text)) else: obj = load_yaml( input_.read_text(encoding=encoding) # type: ignore if isinstance(input_, Path) else input_text) except: raise Error('Invalid file format') import json from genson import SchemaBuilder builder = SchemaBuilder() builder.add_object(obj) input_text = json.dumps(builder.to_schema()) parser = parser_class( source=input_ if isinstance(input_, ParseResult) else input_text or input_, base_class=base_class, custom_template_dir=custom_template_dir, extra_template_data=extra_template_data, target_python_version=target_python_version, dump_resolve_reference_action=dump_resolve_reference_action, validation=validation, field_constraints=field_constraints, snake_case_field=snake_case_field, strip_default_none=strip_default_none, aliases=aliases, allow_population_by_field_name=allow_population_by_field_name, apply_default_values_for_required_fields= apply_default_values_for_required_fields, force_optional_for_required_fields=force_optional_for_required_fields, class_name=class_name, use_standard_collections=use_standard_collections, base_path=input_.parent if isinstance(input_, Path) and input_.is_file() else None, use_schema_description=use_schema_description, reuse_model=reuse_model, enum_field_as_literal=enum_field_as_literal, set_default_enum_member=set_default_enum_member, strict_nullable=strict_nullable, use_generic_container_types=use_generic_container_types, enable_faux_immutability=enable_faux_immutability, remote_text_cache=remote_text_cache, disable_appending_item_suffix=disable_appending_item_suffix, strict_types=strict_types, empty_enum_field_name=empty_enum_field_name, custom_class_name_generator=custom_class_name_generator, ) with chdir(output): results = parser.parse() if not input_filename: # pragma: no cover if isinstance(input_, str): input_filename = '<stdin>' elif isinstance(input_, ParseResult): input_filename = input_.geturl() else: input_filename = input_.name if not results: raise Error('Models not found in the input data') elif isinstance(results, str): modules = {output: (results, input_filename)} else: if output is None: raise Error('Modular references require an output directory') if output.suffix: raise Error( 'Modular references require an output directory, not a file') modules = { output.joinpath(*name): ( result.body, str(result.source.as_posix() if result. source else input_filename), ) for name, result in sorted(results.items()) } timestamp = datetime.now(timezone.utc).replace(microsecond=0).isoformat() header = '''\ # generated by datamodel-codegen: # filename: {}''' if not disable_timestamp: header += f'\n# timestamp: {timestamp}' file: Optional[IO[Any]] for path, body_and_filename in modules.items(): body, filename = body_and_filename if path is None: file = None else: if not path.parent.exists(): path.parent.mkdir(parents=True) file = path.open('wt', encoding=encoding) print(header.format(filename), file=file) if body: print('', file=file) print(body.rstrip(), file=file) if file is not None: file.close()