/
wrappers.py
245 lines (192 loc) · 7.59 KB
/
wrappers.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
from typing import *
from inspect import signature
from errors import ValidationError
from itertools import count
from functools import lru_cache, partial, update_wrapper
import inspect
from inspect import Parameter, Signature
import types
class Wrapper:
'''
Instances of this class act like "wrappers" for other objects
A wrapper has the next properties:
- Has the attributes __name__, __qualname__, __module__, __annotations__,
__doc__ equal than the wrapped object
- _str__ & __repr__ returns the same as if you call __str__ and __repr__ in the wrapped object
'''
def __init__(self, obj):
self.obj = obj
update_wrapper(self, obj)
@property
def wrapped(self):
'''
:return Returns the callable object wrapped by this instance
'''
return self.obj
def __str__(self):
return str(self.obj)
def __repr__(self):
return repr(self.obj)
class CallableWrapper(Wrapper):
'''
Its the same as Wrapper but instances of this class are also callables. When invoked, they call
the wrapped object with the same arguments.
'''
def __init__(self, obj):
'''
Initializes this instance.
:param obj: Is the object to be wrapped. Must be callable
'''
assert callable(obj)
super().__init__(obj)
def __call__(self, *args, **kwargs):
return self.obj(*args, **kwargs)
class ValidateFuncWrapper(CallableWrapper):
'''
An object of this class is a wrapper to a given function but it incorporates
validation features, such as checking the type of the input arguments or the return
value
'''
def __init__(self, func, options):
'''
Initializes this instance.
:param func: Is the function to be wrapped
:param options: Must be a dictionary with settings (check the module config)
'''
super().__init__(func)
self.options = options
@property
@lru_cache(maxsize=1)
def signature(self):
'''
Returns the signature of the wrapped function
'''
return inspect.signature(self)
@property
@lru_cache(maxsize=1)
def varargs_param(self):
'''
Return the parameter (instance of class inspect.Parameter)
in the signature of the wrapped function which is of type *args
or None if *args is not in the signature
'''
for param in self.signature.parameters.values():
if param.kind == Parameter.VAR_POSITIONAL:
return param
return None
@property
@lru_cache(maxsize=1)
def varkwargs_param(self):
'''
Return the parameter (instance of class inspect.Parameter)
in the signature of the wrapped function which is of type **kwargs
or None if **kwargs is not in the signature
'''
for param in self.signature.parameters.values():
if param.kind == Parameter.VAR_KEYWORD:
return param
return None
@property
@lru_cache(maxsize=1)
def param_validators(self):
'''
Returns a list of validators (on for each parameter in the wrapped function signature),
that will be used to validate the input arguments
'''
from parser import parse_annotation
signature = self.signature
validators = []
for param in signature.parameters.values():
validators.append(parse_annotation(
Any if param.annotation is Parameter.empty else param.annotation,
self.options
))
return dict(zip(signature.parameters.keys(), validators))
@property
@lru_cache(maxsize=1)
def return_validator(self):
'''
Returns the validator that will be used to validate the return value
'''
from parser import parse_annotation
signature = self.signature
return parse_annotation(
Any if signature.return_annotation is Parameter.empty else signature.return_annotation,
self.options
)
def bind(self, *args, **kwargs):
'''
Binds the given arguments to the signature of the wrapped function
(also applies default values)
'''
bounded = self.signature.bind(*args, **kwargs)
bounded.apply_defaults()
return bounded
def validate_input(self, *args, **kwargs):
'''
Validates the given input arguments.
Returns a tuple with the values args and kwargs which are the positional and
keyword arguments that should be passed when calling to the wrapped function
'''
if not self.options['match_args']:
# Dont validate input arguments
return args, kwargs
bounded = self.bind(*args, **kwargs)
args = []
varargs = []
varkwargs = {}
for key, value in bounded.arguments.items():
param = self.signature.parameters[key]
if param.kind == Parameter.VAR_KEYWORD:
# **kwargs
if key in self.param_validators and self.options['match_varkwargs']:
validate = partial(self.param_validators[key].validate, context={'func': self.__name__, 'param': 'values on **{}'.format(key)})
varkwargs.update(zip(value.keys(), map(validate, value.values())))
else:
varkwargs.update(value)
elif param.kind == Parameter.VAR_POSITIONAL:
# *args
if key in self.param_validators and self.options['match_varargs']:
validate = partial(self.param_validators[key].validate, context={'func': self.__name__, 'param': 'items on *{}'.format(key)})
varargs.extend(map(validate, value))
else:
# *args items will not be validated
varargs.extend(value)
else:
# Regular argument
validate = partial(self.param_validators[key].validate, context={'func': self.__name__, 'param': key})
args.append(validate(value))
args += varargs
return args, varkwargs
def validate_output(self, output):
'''
Validates the return value (the output of the wrapped function).
The value returned by this method will be used as the output of the call to
this wrapper function
'''
if not self.options['match_return']:
# Dont validate output
return output
return self.return_validator.validate(
output,
context={'func': self.__name__, 'param': 'return value'})
def __get__(self, obj, objtype):
options = self.options
class MethodWrapper(CallableWrapper):
def __call__(self, obj, *args, **kwargs):
# Validate 'self' object
if options['match_self'] and not isinstance(obj, objtype):
raise ValidationError(
func=self.__name__,
param='1st argument',
expected='instance of the class {}'.format(objtype.__name__),
got=type(obj).__name__ if obj is not None else str(None))
return super().__call__(obj, *args, **kwargs)
if obj is None:
return MethodWrapper(self)
return types.MethodType(MethodWrapper(self), obj)
def __call__(self, *args, **kwargs):
args, kwargs = self.validate_input(*args, **kwargs)
result = super().__call__(*args, **kwargs)
result = self.validate_output(result)
return result