def generate_models(output, input_text) -> None: """based on datamodel_code_generator.generate""" py37 = datamodel_code_generator.format.PythonVersion.PY_37 target_python_version = py37 validation = True base_class = "pydantic.BaseModel" custom_template_dir = BASE / pathlib.Path("template/pydantic") extra_template_data = None parser_class = K8SParser parser = parser_class( BaseModel, CustomRootType, DataModelField, base_class=base_class, custom_template_dir=custom_template_dir, extra_template_data=extra_template_data, target_python_version=target_python_version, text=input_text, dump_resolve_reference_action=dump_resolve_reference_action, validation=validation, ) with datamodel_code_generator.chdir(output): result = parser.parse() if isinstance(result, str): modules = {output: result} 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): body for name, body in sorted(result.items()) } header = "# automatically generated file. DO NOT CHANGE MANUALLY" file: Optional[IO[Any]] for path, body in modules.items(): if path is not None: if not path.parent.exists(): path.parent.mkdir(parents=True) file = path.open("wt") else: file = None print(header, file=file) if body: print("", file=file) print(body.rstrip(), file=file) if file is not None: file.close()
def validate_url(cls, value: Any) -> Optional[ParseResult]: if isinstance(value, str) and is_url(value): # pragma: no cover return urlparse(value) elif value is None: # pragma: no cover return None raise Error( f'This protocol doesn\'t support only http/https. --input={value}' ) # pragma: no cover
def validate_original_field_name_delimiter( cls, values: Dict[str, Any]) -> Dict[str, Any]: if values.get('original_field_name_delimiter') is not None: if not values.get('snake_case_field'): raise Error( "`--original-field-name-delimiter` can not be used without `--snake-case-field`." ) return values
def validate_literal_option(cls, values: Dict[str, Any]) -> Dict[str, Any]: if values.get('enum_field_as_literal'): target_python_version: PythonVersion = values['target_python_version'] if not target_python_version.has_literal_type: raise Error( f"`--enum-field-as-literal` isn't compatible with `--target-python-version {target_python_version.value}`.\n" # type: ignore f"You have to set `--target-python-version {target_python_version.PY_38.value}` or later version." ) return values
def validate_each_item(each_item: Any) -> Tuple[str, str]: if isinstance(each_item, str): # pragma: no cover try: field_name, field_value = each_item.split( ':', maxsplit=1) # type: str, str return field_name, field_value.lstrip() except ValueError: raise Error(f'Invalid http header: {each_item!r}') return each_item # pragma: no cover
def validate_use_generic_container_types( cls, values: Dict[str, Any]) -> Dict[str, Any]: if values.get('use_generic_container_types'): target_python_version: PythonVersion = values[ 'target_python_version'] if target_python_version == target_python_version.PY_36: raise Error( f"`--use-generic-container-types` can not be used with `--target-python_version` {target_python_version.PY_36.value}.\n" # type: ignore " The version will be not supported in a future version") return values