-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhandler.py
More file actions
138 lines (93 loc) · 3.19 KB
/
handler.py
File metadata and controls
138 lines (93 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
.. 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