| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- """
- .. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
- """
- import warnings
- from datetime import datetime
- from typing import Callable
- from .error import ValidationError
- ValidationErrorHandler = Callable[[ValidationError], str]
- def return_null_string(e: ValidationError) -> str:
- """Null value handler that always returns an empty string.
- Args:
- e (ValidationError): A validation error.
- Returns:
- str: An empty string.
- """
- warnings.warn(
- "'return_null_string' is deprecated. Use 'NullValueHandler.return_null_string' instead.",
- DeprecationWarning,
- )
- return ""
- def return_timestamp(e: ValidationError) -> str:
- """Null value handler that returns a timestamp of when the function was called.
- Args:
- e (ValidationError): A validation error.
- Returns:
- str: A timestamp.
- """
- warnings.warn(
- "'return_timestamp' is deprecated. Use 'NullValueHandler.reserved_name_handler' instead.",
- DeprecationWarning,
- )
- return str(datetime.now().timestamp())
- def raise_error(e: ValidationError) -> str:
- """Null value handler that always raises an exception.
- Args:
- e (ValidationError): A validation error.
- Raises:
- ValidationError: Always raised.
- """
- raise e
- class NullValueHandler:
- @classmethod
- def return_null_string(cls, e: ValidationError) -> str:
- """Null value handler that always returns an empty string.
- Args:
- e (ValidationError): A validation error.
- Returns:
- str: An empty string.
- """
- return ""
- @classmethod
- def return_timestamp(cls, e: ValidationError) -> str:
- """Null value handler that returns a timestamp of when the function was called.
- Args:
- e (ValidationError): A validation error.
- Returns:
- str: A timestamp.
- """
- return str(datetime.now().timestamp())
- class ReservedNameHandler:
- @classmethod
- def add_leading_underscore(cls, e: ValidationError) -> str:
- """Reserved name handler that adds a leading underscore (``"_"``) to the name
- except for ``"."`` and ``".."``.
- Args:
- e (ValidationError): A reserved name error.
- Returns:
- str: The converted name.
- """
- if e.reserved_name in (".", "..") or e.reusable_name:
- return e.reserved_name
- return f"_{e.reserved_name}"
- @classmethod
- def add_trailing_underscore(cls, e: ValidationError) -> str:
- """Reserved name handler that adds a trailing underscore (``"_"``) to the name
- except for ``"."`` and ``".."``.
- Args:
- e (ValidationError): A reserved name error.
- Returns:
- str: The converted name.
- """
- if e.reserved_name in (".", "..") or e.reusable_name:
- return e.reserved_name
- return f"{e.reserved_name}_"
- @classmethod
- def as_is(cls, e: ValidationError) -> str:
- """Reserved name handler that returns the name as is.
- Args:
- e (ValidationError): A reserved name error.
- Returns:
- str: The name as is.
- """
- return e.reserved_name
|