handler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. .. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
  3. """
  4. import warnings
  5. from datetime import datetime
  6. from typing import Callable
  7. from .error import ValidationError
  8. ValidationErrorHandler = Callable[[ValidationError], str]
  9. def return_null_string(e: ValidationError) -> str:
  10. """Null value handler that always returns an empty string.
  11. Args:
  12. e (ValidationError): A validation error.
  13. Returns:
  14. str: An empty string.
  15. """
  16. warnings.warn(
  17. "'return_null_string' is deprecated. Use 'NullValueHandler.return_null_string' instead.",
  18. DeprecationWarning,
  19. )
  20. return ""
  21. def return_timestamp(e: ValidationError) -> str:
  22. """Null value handler that returns a timestamp of when the function was called.
  23. Args:
  24. e (ValidationError): A validation error.
  25. Returns:
  26. str: A timestamp.
  27. """
  28. warnings.warn(
  29. "'return_timestamp' is deprecated. Use 'NullValueHandler.reserved_name_handler' instead.",
  30. DeprecationWarning,
  31. )
  32. return str(datetime.now().timestamp())
  33. def raise_error(e: ValidationError) -> str:
  34. """Null value handler that always raises an exception.
  35. Args:
  36. e (ValidationError): A validation error.
  37. Raises:
  38. ValidationError: Always raised.
  39. """
  40. raise e
  41. class NullValueHandler:
  42. @classmethod
  43. def return_null_string(cls, e: ValidationError) -> str:
  44. """Null value handler that always returns an empty string.
  45. Args:
  46. e (ValidationError): A validation error.
  47. Returns:
  48. str: An empty string.
  49. """
  50. return ""
  51. @classmethod
  52. def return_timestamp(cls, e: ValidationError) -> str:
  53. """Null value handler that returns a timestamp of when the function was called.
  54. Args:
  55. e (ValidationError): A validation error.
  56. Returns:
  57. str: A timestamp.
  58. """
  59. return str(datetime.now().timestamp())
  60. class ReservedNameHandler:
  61. @classmethod
  62. def add_leading_underscore(cls, e: ValidationError) -> str:
  63. """Reserved name handler that adds a leading underscore (``"_"``) to the name
  64. except for ``"."`` and ``".."``.
  65. Args:
  66. e (ValidationError): A reserved name error.
  67. Returns:
  68. str: The converted name.
  69. """
  70. if e.reserved_name in (".", "..") or e.reusable_name:
  71. return e.reserved_name
  72. return f"_{e.reserved_name}"
  73. @classmethod
  74. def add_trailing_underscore(cls, e: ValidationError) -> str:
  75. """Reserved name handler that adds a trailing underscore (``"_"``) to the name
  76. except for ``"."`` and ``".."``.
  77. Args:
  78. e (ValidationError): A reserved name error.
  79. Returns:
  80. str: The converted name.
  81. """
  82. if e.reserved_name in (".", "..") or e.reusable_name:
  83. return e.reserved_name
  84. return f"{e.reserved_name}_"
  85. @classmethod
  86. def as_is(cls, e: ValidationError) -> str:
  87. """Reserved name handler that returns the name as is.
  88. Args:
  89. e (ValidationError): A reserved name error.
  90. Returns:
  91. str: The name as is.
  92. """
  93. return e.reserved_name