-
Notifications
You must be signed in to change notification settings - Fork 25
/
core.py
349 lines (294 loc) · 10.2 KB
/
core.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import socket
import sys
from functools import partial, update_wrapper
from inspect import getcallargs
from io import StringIO
from types import MethodType
from typing import Callable, Type, Tuple
from . import exceptions
from .state import state
from .types import ExceptionType
class _Base:
exception: ExceptionType = exceptions.ContractError
def __init__(self, validator, *, message: str = None,
exception: Type[Exception] = None, debug: bool = False):
"""
Step 1. Set contract (validator).
"""
self.validator = validator
self.debug = debug
if exception:
self.exception = exception
if message:
self.exception = self.exception(message)
def validate(self, *args, **kwargs) -> None:
"""
Step 4. Process contract (validator)
"""
if hasattr(self.validator, 'is_valid'):
self._vaa_validation(*args, **kwargs)
else:
self._simple_validation(*args, **kwargs)
def _vaa_validation(self, *args, **kwargs) -> None:
params = kwargs.copy()
if hasattr(self, 'function'):
# detect original function
function = self.function
while hasattr(function, '__wrapped__'):
function = function.__wrapped__
# assign *args to real names
params.update(getcallargs(function, *args, **kwargs))
# drop args-kwargs, we already put them on the right places
for bad_name in ('args', 'kwargs'):
if bad_name in params and bad_name not in kwargs:
del params[bad_name]
validator = self.validator(data=params)
if validator.is_valid():
return
raise self.exception(validator.errors)
def _simple_validation(self, *args, **kwargs) -> None:
validation_result = self.validator(*args, **kwargs)
# is invalid (validator return error message)
if isinstance(validation_result, str):
raise self.exception(validation_result)
# is valid (truely result)
if validation_result:
return
# is invalid (falsy result)
raise self.exception
@property
def enabled(self) -> bool:
if self.debug:
return state.debug
else:
return state.main
def __call__(self, function: Callable) -> Callable:
"""
Step 2. Return wrapped function.
"""
self.function = function
def wrapped(*args, **kwargs):
if self.enabled:
return self.patched_function(*args, **kwargs)
else:
return function(*args, **kwargs)
return update_wrapper(wrapped, function)
class Pre(_Base):
"""
Check contract (validator) before function processing.
Validate input arguments.
"""
exception: ExceptionType = exceptions.PreContractError
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
self.validate(*args, **kwargs)
return self.function(*args, **kwargs)
class Post(_Base):
"""
Check contract (validator) after function processing.
Validate output result.
"""
exception: ExceptionType = exceptions.PostContractError
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
result = self.function(*args, **kwargs)
self.validate(result)
return result
class InvariantedClass:
_disable_patching: bool = False
def _validate(self) -> None:
"""
Step 5 (1st flow) or Step 4 (2nd flow). Process contract for object.
"""
# disable methods matching before validation
self._disable_patching = True
# validation by Invariant.validate
self._validate_base(self)
# enable methods matching after validation
self._disable_patching = False
def _patched_method(self, method: Callable, *args, **kwargs):
"""
Step 4 (1st flow). Call method
"""
self._validate()
result = method(*args, **kwargs)
self._validate()
return result
def __getattribute__(self, name: str):
"""
Step 3 (1st flow). Get method
"""
attr = super().__getattribute__(name)
# disable patching for InvariantedClass methods
if name in ('_patched_method', '_validate', '_validate_base', '_disable_patching'):
return attr
# disable patching by flag (if validation in progress)
if self._disable_patching:
return attr
# disable patching for attributes (not methods)
if not isinstance(attr, MethodType):
return attr
# patch
patched_method = partial(self._patched_method, attr)
return update_wrapper(patched_method, attr)
def __setattr__(self, name: str, value):
"""
Step 3 (2nd flow). Set some attribute
"""
# set
super().__setattr__(name, value)
if name == '_disable_patching':
return
# validation only after set
self._validate()
class Invariant(_Base):
exception: ExceptionType = exceptions.InvContractError
def validate(self, obj) -> None:
"""
Step 6. Process contract (validator)
"""
if hasattr(self.validator, 'is_valid') and hasattr(obj, '__dict__'):
kwargs = obj.__dict__.copy()
kwargs.pop('_disable_patching', '')
self._vaa_validation(**kwargs)
else:
self._simple_validation(obj)
def validate_chain(self, *args, **kwargs) -> None:
self.validate(*args, **kwargs)
self.child_validator(*args, **kwargs)
def __call__(self, _class: type):
"""
Step 2. Return wrapped class.
"""
# patch class parents and add method for validation
# if already invarianted
if hasattr(_class, '_validate_base'):
self.child_validator = _class._validate_base
patched_class = type(
_class.__name__,
(_class, ),
{'_validate_base': self.validate_chain},
)
# if it's first invariant
else:
patched_class = type(
_class.__name__ + 'Invarianted',
(InvariantedClass, _class),
{'_validate_base': self.validate},
)
# Magic: _validate_base method use Invariant as self, not _class
# return update_wrapper(patched_class, _class)
return patched_class
class Raises(_Base):
exception: ExceptionType = exceptions.RaisesContractError
def __init__(self, *exceptions, message=None, exception=None, debug=False):
"""
Step 1. Set allowed exceptions list.
"""
self.exceptions: Tuple[Exception, ...] = exceptions
super().__init__(
validator=None,
message=message,
exception=exception,
debug=debug,
)
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
try:
return self.function(*args, **kwargs)
except exceptions.ContractError:
raise
except Exception as exc:
if not isinstance(exc, self.exceptions):
raise self.exception from exc
raise
class Reason(_Base):
exception: ExceptionType = exceptions.ReasonContractError
def __init__(self, trigger: Exception, validator: Callable, *,
message: str = None, exception: ExceptionType = None, debug: bool = False):
"""
Step 1. Set allowed exceptions list.
"""
self.exceptions: Tuple[Exception, ...] = exceptions
self.trigger = trigger
super().__init__(
validator=validator,
message=message,
exception=exception,
debug=debug,
)
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
try:
return self.function(*args, **kwargs)
except self.trigger as origin:
try:
self.validate(*args, **kwargs)
except self.exception:
raise self.exception from origin
raise
class Offline(_Base):
exception: ExceptionType = exceptions.OfflineContractError
def __init__(self, *, message=None, exception=None, debug=False):
"""
Step 1. Init params.
"""
super().__init__(
validator=None,
message=message,
exception=exception,
debug=debug,
)
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
true_socket = socket.socket
socket.socket = self.fake_socket
try:
return self.function(*args, **kwargs)
finally:
socket.socket = true_socket
def fake_socket(self, *args, **kwargs):
raise self.exception
class PatchedStringIO(StringIO):
def __init__(self, exception):
self.exception = exception
def write(self, *args, **kwargs):
raise self.exception
class Silent(Offline):
exception: ExceptionType = exceptions.SilentContractError
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
true_stdout = sys.stdout
true_stderr = sys.stderr
sys.stdout = PatchedStringIO(exception=self.exception)
sys.stderr = PatchedStringIO(exception=self.exception)
try:
return self.function(*args, **kwargs)
finally:
sys.stdout = true_stdout
sys.stderr = true_stderr
class Ensure(_Base):
"""
Check both arguments and result (validator) after function processing.
Validate arguments and output result.
"""
exception: ExceptionType = exceptions.PostContractError
def patched_function(self, *args, **kwargs):
"""
Step 3. Wrapped function calling.
"""
result = self.function(*args, **kwargs)
self.validate(*args, result=result, **kwargs)
return result