Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

93 linhas
2.8 KiB

  1. import json
  2. from functools import lru_cache
  3. from pathlib import Path
  4. from typing import TYPE_CHECKING, Any, Callable, Optional, Type, TypeVar, Union
  5. from .parse import Protocol, load_file, load_str_bytes
  6. from .types import StrBytes
  7. from .typing import display_as_type
  8. __all__ = ('parse_file_as', 'parse_obj_as', 'parse_raw_as', 'schema_of', 'schema_json_of')
  9. NameFactory = Union[str, Callable[[Type[Any]], str]]
  10. if TYPE_CHECKING:
  11. from .typing import DictStrAny
  12. def _generate_parsing_type_name(type_: Any) -> str:
  13. return f'ParsingModel[{display_as_type(type_)}]'
  14. @lru_cache(maxsize=2048)
  15. def _get_parsing_type(type_: Any, *, type_name: Optional[NameFactory] = None) -> Any:
  16. from pydantic.main import create_model
  17. if type_name is None:
  18. type_name = _generate_parsing_type_name
  19. if not isinstance(type_name, str):
  20. type_name = type_name(type_)
  21. return create_model(type_name, __root__=(type_, ...))
  22. T = TypeVar('T')
  23. def parse_obj_as(type_: Type[T], obj: Any, *, type_name: Optional[NameFactory] = None) -> T:
  24. model_type = _get_parsing_type(type_, type_name=type_name) # type: ignore[arg-type]
  25. return model_type(__root__=obj).__root__
  26. def parse_file_as(
  27. type_: Type[T],
  28. path: Union[str, Path],
  29. *,
  30. content_type: str = None,
  31. encoding: str = 'utf8',
  32. proto: Protocol = None,
  33. allow_pickle: bool = False,
  34. json_loads: Callable[[str], Any] = json.loads,
  35. type_name: Optional[NameFactory] = None,
  36. ) -> T:
  37. obj = load_file(
  38. path,
  39. proto=proto,
  40. content_type=content_type,
  41. encoding=encoding,
  42. allow_pickle=allow_pickle,
  43. json_loads=json_loads,
  44. )
  45. return parse_obj_as(type_, obj, type_name=type_name)
  46. def parse_raw_as(
  47. type_: Type[T],
  48. b: StrBytes,
  49. *,
  50. content_type: str = None,
  51. encoding: str = 'utf8',
  52. proto: Protocol = None,
  53. allow_pickle: bool = False,
  54. json_loads: Callable[[str], Any] = json.loads,
  55. type_name: Optional[NameFactory] = None,
  56. ) -> T:
  57. obj = load_str_bytes(
  58. b,
  59. proto=proto,
  60. content_type=content_type,
  61. encoding=encoding,
  62. allow_pickle=allow_pickle,
  63. json_loads=json_loads,
  64. )
  65. return parse_obj_as(type_, obj, type_name=type_name)
  66. def schema_of(type_: Any, *, title: Optional[NameFactory] = None, **schema_kwargs: Any) -> 'DictStrAny':
  67. """Generate a JSON schema (as dict) for the passed model or dynamically generated one"""
  68. return _get_parsing_type(type_, type_name=title).schema(**schema_kwargs)
  69. def schema_json_of(type_: Any, *, title: Optional[NameFactory] = None, **schema_json_kwargs: Any) -> str:
  70. """Generate a JSON schema (as JSON) for the passed model or dynamically generated one"""
  71. return _get_parsing_type(type_, type_name=title).schema_json(**schema_json_kwargs)