api.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. Provides the default implementation of :class:`ArrowFactory <arrow.factory.ArrowFactory>`
  3. methods for use as a module API.
  4. """
  5. from datetime import date, datetime
  6. from datetime import tzinfo as dt_tzinfo
  7. from time import struct_time
  8. from typing import Any, List, Optional, Tuple, Type, Union, overload
  9. from arrow.arrow import TZ_EXPR, Arrow
  10. from arrow.constants import DEFAULT_LOCALE
  11. from arrow.factory import ArrowFactory
  12. # internal default factory.
  13. _factory = ArrowFactory()
  14. # TODO: Use Positional Only Argument (https://www.python.org/dev/peps/pep-0570/)
  15. # after Python 3.7 deprecation
  16. @overload
  17. def get(
  18. *,
  19. locale: str = DEFAULT_LOCALE,
  20. tzinfo: Optional[TZ_EXPR] = None,
  21. normalize_whitespace: bool = False,
  22. ) -> Arrow: ... # pragma: no cover
  23. @overload
  24. def get(
  25. *args: int,
  26. locale: str = DEFAULT_LOCALE,
  27. tzinfo: Optional[TZ_EXPR] = None,
  28. normalize_whitespace: bool = False,
  29. ) -> Arrow: ... # pragma: no cover
  30. @overload
  31. def get(
  32. __obj: Union[
  33. Arrow,
  34. datetime,
  35. date,
  36. struct_time,
  37. dt_tzinfo,
  38. int,
  39. float,
  40. str,
  41. Tuple[int, int, int],
  42. ],
  43. *,
  44. locale: str = DEFAULT_LOCALE,
  45. tzinfo: Optional[TZ_EXPR] = None,
  46. normalize_whitespace: bool = False,
  47. ) -> Arrow: ... # pragma: no cover
  48. @overload
  49. def get(
  50. __arg1: Union[datetime, date],
  51. __arg2: TZ_EXPR,
  52. *,
  53. locale: str = DEFAULT_LOCALE,
  54. tzinfo: Optional[TZ_EXPR] = None,
  55. normalize_whitespace: bool = False,
  56. ) -> Arrow: ... # pragma: no cover
  57. @overload
  58. def get(
  59. __arg1: str,
  60. __arg2: Union[str, List[str]],
  61. *,
  62. locale: str = DEFAULT_LOCALE,
  63. tzinfo: Optional[TZ_EXPR] = None,
  64. normalize_whitespace: bool = False,
  65. ) -> Arrow: ... # pragma: no cover
  66. def get(*args: Any, **kwargs: Any) -> Arrow:
  67. """Calls the default :class:`ArrowFactory <arrow.factory.ArrowFactory>` ``get`` method."""
  68. return _factory.get(*args, **kwargs)
  69. get.__doc__ = _factory.get.__doc__
  70. def utcnow() -> Arrow:
  71. """Calls the default :class:`ArrowFactory <arrow.factory.ArrowFactory>` ``utcnow`` method."""
  72. return _factory.utcnow()
  73. utcnow.__doc__ = _factory.utcnow.__doc__
  74. def now(tz: Optional[TZ_EXPR] = None) -> Arrow:
  75. """Calls the default :class:`ArrowFactory <arrow.factory.ArrowFactory>` ``now`` method."""
  76. return _factory.now(tz)
  77. now.__doc__ = _factory.now.__doc__
  78. def factory(type: Type[Arrow]) -> ArrowFactory:
  79. """Returns an :class:`.ArrowFactory` for the specified :class:`Arrow <arrow.arrow.Arrow>`
  80. or derived type.
  81. :param type: the type, :class:`Arrow <arrow.arrow.Arrow>` or derived.
  82. """
  83. return ArrowFactory(type)
  84. __all__ = ["get", "utcnow", "now", "factory"]