DefStream 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- DefStream/Broker/Configuration.py +18 -0
- DefStream/Broker/Connection.py +97 -0
- DefStream/Broker/Consumer.py +59 -0
- DefStream/Broker/Errors.py +14 -0
- DefStream/Broker/Event.py +35 -0
- DefStream/Broker/Implements/__init__.py +1 -0
- DefStream/Broker/Policies.py +25 -0
- DefStream/Broker/Protocol.py +45 -0
- DefStream/Broker/__init__.py +47 -0
- DefStream/Descriptor/DataObject.py +384 -0
- DefStream/Descriptor/Types.py +229 -0
- DefStream/Descriptor/Value.py +132 -0
- DefStream/Descriptor/__init__.py +172 -0
- DefStream/Inspector/Extension.py +69 -0
- DefStream/Inspector/Extensions/Console.html +373 -0
- DefStream/Inspector/Extensions/Console.py +396 -0
- DefStream/Inspector/Extensions/Dashboard.html +206 -0
- DefStream/Inspector/Extensions/Dashboard.py +114 -0
- DefStream/Inspector/Extensions/HealthCheck.py +92 -0
- DefStream/Inspector/Extensions/Metrics.py +281 -0
- DefStream/Inspector/Extensions/Specifications.py +160 -0
- DefStream/Inspector/Extensions/__init__.py +13 -0
- DefStream/Inspector/Inspector.py +216 -0
- DefStream/Inspector/Response.py +14 -0
- DefStream/Inspector/__init__.py +9 -0
- DefStream/Validator/Pattern.py +36 -0
- DefStream/Validator/Patterns/Array.py +30 -0
- DefStream/Validator/Patterns/Common.py +93 -0
- DefStream/Validator/Patterns/Compare.py +180 -0
- DefStream/Validator/Patterns/Condition.py +60 -0
- DefStream/Validator/Patterns/DataObject.py +85 -0
- DefStream/Validator/Patterns/DateTime.py +111 -0
- DefStream/Validator/Patterns/Number.py +43 -0
- DefStream/Validator/Patterns/Object.py +150 -0
- DefStream/Validator/Patterns/Size.py +110 -0
- DefStream/Validator/Patterns/String.py +85 -0
- DefStream/Validator/Patterns/Type.py +350 -0
- DefStream/Validator/Patterns/__init__.py +137 -0
- DefStream/Validator/Validate.py +29 -0
- DefStream/Validator/Validator.py +30 -0
- DefStream/Validator/__init__.py +11 -0
- DefStream/Worker/Executor.py +298 -0
- DefStream/Worker/Message.py +103 -0
- DefStream/Worker/Pool.py +203 -0
- DefStream/Worker/Run.py +187 -0
- DefStream/Worker/Specification.py +91 -0
- DefStream/Worker/Worker.py +321 -0
- DefStream/Worker/__init__.py +49 -0
- DefStream/__init__.py +0 -0
- defstream-0.1.2.dist-info/METADATA +1197 -0
- defstream-0.1.2.dist-info/RECORD +54 -0
- defstream-0.1.2.dist-info/WHEEL +5 -0
- defstream-0.1.2.dist-info/licenses/LICENSE +674 -0
- defstream-0.1.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from ..Pattern import Pattern
|
|
4
|
+
from ast import literal_eval
|
|
5
|
+
from collections.abc import (
|
|
6
|
+
Iterable,
|
|
7
|
+
Mapping,
|
|
8
|
+
)
|
|
9
|
+
from typing import Type, Sequence, Union
|
|
10
|
+
|
|
11
|
+
__all__ = (
|
|
12
|
+
'IsTypeOf',
|
|
13
|
+
'IsBool',
|
|
14
|
+
'IsInteger',
|
|
15
|
+
'IsFloat',
|
|
16
|
+
'IsNumber',
|
|
17
|
+
'IsString',
|
|
18
|
+
'IsList',
|
|
19
|
+
'IsTuple',
|
|
20
|
+
'IsSet',
|
|
21
|
+
'IsArray',
|
|
22
|
+
'IsObject',
|
|
23
|
+
'IsByteString',
|
|
24
|
+
'IsByteArray',
|
|
25
|
+
'IsDecimal',
|
|
26
|
+
'IsDataObject',
|
|
27
|
+
'ToTypeOf',
|
|
28
|
+
'ToBool',
|
|
29
|
+
'ToInteger',
|
|
30
|
+
'ToFloat',
|
|
31
|
+
'ToString',
|
|
32
|
+
'ToList',
|
|
33
|
+
'ToTuple',
|
|
34
|
+
'ToSet',
|
|
35
|
+
'ToObject',
|
|
36
|
+
'ToByteString',
|
|
37
|
+
'ToByteArray',
|
|
38
|
+
'ToDecimal',
|
|
39
|
+
'ToDataObject',
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class IsTypeOf(Pattern):
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
type: Union[Type, Sequence[Type]],
|
|
47
|
+
patterns: Sequence[Pattern] = (),
|
|
48
|
+
error: BaseException = None
|
|
49
|
+
):
|
|
50
|
+
self.type = type
|
|
51
|
+
self.patterns = patterns if isinstance(patterns, Iterable) else (patterns,)
|
|
52
|
+
self.error = error
|
|
53
|
+
return
|
|
54
|
+
|
|
55
|
+
def __call__(self, parameter):
|
|
56
|
+
def op(parameter):
|
|
57
|
+
if isinstance(self.type, Iterable):
|
|
58
|
+
return isinstance(parameter, self.type)
|
|
59
|
+
return type(parameter) is self.type
|
|
60
|
+
if not op(parameter):
|
|
61
|
+
if self.error:
|
|
62
|
+
raise self.error
|
|
63
|
+
raise TypeError('{} must be {}'.format(
|
|
64
|
+
'\'{}\''.format(parameter) if isinstance(parameter, str) else parameter,
|
|
65
|
+
', '.join([t.__name__ for t in self.type]) if isinstance(self.type, Iterable) else self.type.__name__
|
|
66
|
+
))
|
|
67
|
+
for pattern in self.patterns:
|
|
68
|
+
parameter = pattern(parameter)
|
|
69
|
+
return parameter
|
|
70
|
+
|
|
71
|
+
def __repr__(self):
|
|
72
|
+
if issubclass(self.__class__, IsTypeOf):
|
|
73
|
+
return '{}({})'.format(
|
|
74
|
+
self.__class__.__name__,
|
|
75
|
+
', '.join([p.__repr__() for p in self.patterns]) if self.patterns and len(self.patterns) else ''
|
|
76
|
+
)
|
|
77
|
+
return '{}({}{})'.format(
|
|
78
|
+
self.__class__.__name__,
|
|
79
|
+
self.type.__name__,
|
|
80
|
+
', ({})'.format(
|
|
81
|
+
', '.join([p.__repr__() for p in self.patterns])
|
|
82
|
+
) if self.patterns and len(self.patterns) else ''
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class IsBool(IsTypeOf):
|
|
87
|
+
def __init__(self, *args, error: BaseException = None):
|
|
88
|
+
super().__init__(bool, patterns=args, error=error)
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class IsInteger(IsTypeOf):
|
|
93
|
+
def __init__(self, *args, error: BaseException = None):
|
|
94
|
+
super().__init__(int, patterns=args, error=error)
|
|
95
|
+
return
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class IsFloat(IsTypeOf):
|
|
99
|
+
def __init__(self, *args, error: BaseException = None):
|
|
100
|
+
super().__init__(float, patterns=args, error=error)
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class IsNumber(IsTypeOf):
|
|
105
|
+
def __init__(self, *args, error: BaseException = None):
|
|
106
|
+
super().__init__((int, float), patterns=args, error=error)
|
|
107
|
+
return
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class IsString(IsTypeOf):
|
|
111
|
+
def __init__(self, *args, error: BaseException = None):
|
|
112
|
+
super().__init__(str, patterns=args, error=error)
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class IsList(IsTypeOf):
|
|
117
|
+
def __init__(self, *args, error: BaseException = None):
|
|
118
|
+
super().__init__(list, patterns=args, error=error)
|
|
119
|
+
return
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class IsTuple(IsTypeOf):
|
|
123
|
+
def __init__(self, *args, error: BaseException = None):
|
|
124
|
+
super().__init__(tuple, patterns=args, error=error)
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class IsSet(IsTypeOf):
|
|
129
|
+
def __init__(self, *args, error: BaseException = None):
|
|
130
|
+
super().__init__(set, patterns=args, error=error)
|
|
131
|
+
return
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class IsArray(Pattern):
|
|
135
|
+
def __init__(self, *args, error=None):
|
|
136
|
+
self.patterns = args
|
|
137
|
+
self.error = error
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
def __call__(self, parameter: Iterable) -> Iterable:
|
|
141
|
+
if not isinstance(parameter, Iterable) or isinstance(parameter, (str, bytes)):
|
|
142
|
+
if self.error:
|
|
143
|
+
raise self.error
|
|
144
|
+
raise TypeError('{} must be a non-string, non-bytes iterable'.format(parameter))
|
|
145
|
+
for pattern in self.patterns:
|
|
146
|
+
parameter = pattern(parameter)
|
|
147
|
+
return parameter
|
|
148
|
+
|
|
149
|
+
def __repr__(self):
|
|
150
|
+
return '{}({})'.format(
|
|
151
|
+
self.__class__.__name__,
|
|
152
|
+
', '.join([p.__repr__() for p in self.patterns])
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class IsObject(IsTypeOf):
|
|
157
|
+
def __init__(self, *args, error: BaseException = None):
|
|
158
|
+
super().__init__(dict, patterns=args, error=error)
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class IsByteString(IsTypeOf):
|
|
163
|
+
def __init__(self, *args, error: BaseException = None):
|
|
164
|
+
super().__init__(bytes, patterns=args, error=error)
|
|
165
|
+
return
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class IsByteArray(IsTypeOf):
|
|
169
|
+
def __init__(self, *args, error: BaseException = None):
|
|
170
|
+
super().__init__(bytearray, patterns=args, error=error)
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class IsDecimal(IsTypeOf):
|
|
175
|
+
def __init__(self, *args, error: BaseException = None):
|
|
176
|
+
from decimal import Decimal
|
|
177
|
+
super().__init__(Decimal, patterns=args, error=error)
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class IsDataObject(Pattern):
|
|
182
|
+
def __init__(
|
|
183
|
+
self,
|
|
184
|
+
*patterns: Pattern,
|
|
185
|
+
error: BaseException = None
|
|
186
|
+
):
|
|
187
|
+
self.patterns = patterns if isinstance(patterns, Iterable) else (patterns,)
|
|
188
|
+
self.error = error
|
|
189
|
+
return
|
|
190
|
+
|
|
191
|
+
def __call__(self, parameter: object) -> object:
|
|
192
|
+
from dataclasses import is_dataclass
|
|
193
|
+
if not is_dataclass(parameter.__class__):
|
|
194
|
+
if self.error:
|
|
195
|
+
raise self.error
|
|
196
|
+
raise TypeError('{} must be dataclass'.format(
|
|
197
|
+
'\'{}\''.format(parameter) if isinstance(parameter, str) else parameter,
|
|
198
|
+
))
|
|
199
|
+
for pattern in self.patterns:
|
|
200
|
+
parameter = pattern(parameter)
|
|
201
|
+
return parameter
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class ToTypeOf(Pattern):
|
|
205
|
+
def __init__(
|
|
206
|
+
self,
|
|
207
|
+
type : type,
|
|
208
|
+
patterns: Sequence[Pattern] = (),
|
|
209
|
+
error: BaseException = None,
|
|
210
|
+
eval: bool = False,
|
|
211
|
+
):
|
|
212
|
+
self.type = type
|
|
213
|
+
self.patterns = patterns if isinstance(patterns, Iterable) else (patterns,)
|
|
214
|
+
self.error = error
|
|
215
|
+
self.eval = eval
|
|
216
|
+
return
|
|
217
|
+
|
|
218
|
+
def __call__(self, parameter):
|
|
219
|
+
try:
|
|
220
|
+
if self.eval:
|
|
221
|
+
parameter = self.type(
|
|
222
|
+
literal_eval(str(parameter))
|
|
223
|
+
)
|
|
224
|
+
else:
|
|
225
|
+
parameter = self.type(parameter)
|
|
226
|
+
except Exception as e:
|
|
227
|
+
if self.error:
|
|
228
|
+
raise self.error
|
|
229
|
+
raise ValueError('{} cannot be converted to {}'.format(
|
|
230
|
+
'\'{}\''.format(parameter) if isinstance(parameter, str) else parameter,
|
|
231
|
+
self.type.__name__,
|
|
232
|
+
))
|
|
233
|
+
for pattern in self.patterns:
|
|
234
|
+
parameter = pattern(parameter)
|
|
235
|
+
return parameter
|
|
236
|
+
|
|
237
|
+
def __repr__(self):
|
|
238
|
+
if issubclass(self.__class__, ToTypeOf):
|
|
239
|
+
return '{}({})'.format(
|
|
240
|
+
self.__class__.__name__,
|
|
241
|
+
', '.join([p.__repr__() for p in self.patterns]) if self.patterns and len(self.patterns) else ''
|
|
242
|
+
)
|
|
243
|
+
return '{}({}{})'.format(
|
|
244
|
+
self.__class__.__name__,
|
|
245
|
+
self.type.__name__,
|
|
246
|
+
', ({})'.format(
|
|
247
|
+
', '.join([p.__repr__() for p in self.patterns])
|
|
248
|
+
) if self.patterns and len(self.patterns) else ''
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
class ToBool(ToTypeOf):
|
|
253
|
+
def __init__(self, *args, error: BaseException = None):
|
|
254
|
+
super().__init__(bool, patterns=args, error=error, eval=True)
|
|
255
|
+
return
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class ToInteger(ToTypeOf):
|
|
259
|
+
def __init__(self, *args, error: BaseException = None):
|
|
260
|
+
super().__init__(int, patterns=args, error=error, eval=True)
|
|
261
|
+
return
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class ToFloat(ToTypeOf):
|
|
265
|
+
def __init__(self, *args, error: BaseException = None):
|
|
266
|
+
super().__init__(float, patterns=args, error=error, eval=True)
|
|
267
|
+
return
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class ToString(ToTypeOf):
|
|
271
|
+
def __init__(self, *args, error: BaseException = None):
|
|
272
|
+
super().__init__(str, patterns=args, error=error)
|
|
273
|
+
return
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
class ToList(ToTypeOf):
|
|
277
|
+
def __init__(self, *args, error: BaseException = None):
|
|
278
|
+
super().__init__(list, patterns=args, error=error)
|
|
279
|
+
return
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class ToTuple(ToTypeOf):
|
|
283
|
+
def __init__(self, *args, error: BaseException = None):
|
|
284
|
+
super().__init__(tuple, patterns=args, error=error)
|
|
285
|
+
return
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class ToSet(ToTypeOf):
|
|
289
|
+
def __init__(self, *args, error: BaseException = None):
|
|
290
|
+
super().__init__(set, patterns=args, error=error)
|
|
291
|
+
return
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
class ToObject(ToTypeOf):
|
|
295
|
+
def __init__(self, *args, error: BaseException = None):
|
|
296
|
+
super().__init__(dict, patterns=args, error=error)
|
|
297
|
+
return
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class ToByteString(ToTypeOf):
|
|
301
|
+
def __init__(self, *args, error: BaseException = None):
|
|
302
|
+
super().__init__(bytes, patterns=args, error=error)
|
|
303
|
+
return
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class ToByteArray(ToTypeOf):
|
|
307
|
+
def __init__(self, *args, error: BaseException = None):
|
|
308
|
+
super().__init__(bytearray, patterns=args, error=error)
|
|
309
|
+
return
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
class ToDecimal(ToTypeOf):
|
|
313
|
+
def __init__(self, *args, error: BaseException = None):
|
|
314
|
+
from decimal import Decimal
|
|
315
|
+
super().__init__(Decimal, patterns=args, error=error)
|
|
316
|
+
return
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class ToDataObject(Pattern):
|
|
320
|
+
def __init__(
|
|
321
|
+
self,
|
|
322
|
+
*patterns: Pattern,
|
|
323
|
+
error: BaseException = None
|
|
324
|
+
):
|
|
325
|
+
self.patterns = patterns if isinstance(patterns, Iterable) else (patterns,)
|
|
326
|
+
self.error = error
|
|
327
|
+
return
|
|
328
|
+
|
|
329
|
+
def __call__(self, parameter: Mapping) -> object:
|
|
330
|
+
from dataclasses import is_dataclass, make_dataclass
|
|
331
|
+
try:
|
|
332
|
+
parameter = make_dataclass(
|
|
333
|
+
'DynamicDataClass',
|
|
334
|
+
[(key, type(value)) for key, value in parameter.items()]
|
|
335
|
+
)(**parameter)
|
|
336
|
+
except Exception as e:
|
|
337
|
+
if self.error:
|
|
338
|
+
raise self.error
|
|
339
|
+
raise ValueError('{} cannot be converted to dataclass'.format(
|
|
340
|
+
'\'{}\''.format(parameter) if isinstance(parameter, str) else parameter
|
|
341
|
+
))
|
|
342
|
+
if not is_dataclass(parameter.__class__):
|
|
343
|
+
if self.error:
|
|
344
|
+
raise self.error
|
|
345
|
+
raise TypeError('{} must be dataclass'.format(
|
|
346
|
+
'\'{}\''.format(parameter) if isinstance(parameter, str) else parameter,
|
|
347
|
+
))
|
|
348
|
+
for pattern in self.patterns:
|
|
349
|
+
parameter = pattern(parameter)
|
|
350
|
+
return parameter
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
# COMMON
|
|
4
|
+
from .Common import (
|
|
5
|
+
SetDefault,
|
|
6
|
+
IsToNone,
|
|
7
|
+
IsNotToNone,
|
|
8
|
+
IsNotEmpty,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
# SIZE
|
|
12
|
+
from .Size import (
|
|
13
|
+
IsSizeOf,
|
|
14
|
+
IsSizeIn,
|
|
15
|
+
IsMinSizeOf,
|
|
16
|
+
IsMaxSizeOf,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# COMPARE
|
|
20
|
+
from .Compare import (
|
|
21
|
+
IsEqualTo,
|
|
22
|
+
IsNotEqualTo,
|
|
23
|
+
IsGreaterThan,
|
|
24
|
+
IsGreaterEqualTo,
|
|
25
|
+
IsLessEqualTo,
|
|
26
|
+
IsLessThan,
|
|
27
|
+
IsIn,
|
|
28
|
+
IsNotIn,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# CONDITION
|
|
32
|
+
from .Condition import (
|
|
33
|
+
All,
|
|
34
|
+
Any,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# TYPE
|
|
38
|
+
from .Type import (
|
|
39
|
+
IsTypeOf,
|
|
40
|
+
IsBool,
|
|
41
|
+
IsInteger,
|
|
42
|
+
IsFloat,
|
|
43
|
+
IsNumber,
|
|
44
|
+
IsString,
|
|
45
|
+
IsList,
|
|
46
|
+
IsTuple,
|
|
47
|
+
IsSet,
|
|
48
|
+
IsArray,
|
|
49
|
+
IsObject,
|
|
50
|
+
IsByteString,
|
|
51
|
+
IsByteArray,
|
|
52
|
+
IsDecimal,
|
|
53
|
+
IsDataObject,
|
|
54
|
+
ToTypeOf,
|
|
55
|
+
ToBool,
|
|
56
|
+
ToInteger,
|
|
57
|
+
ToFloat,
|
|
58
|
+
ToString,
|
|
59
|
+
ToList,
|
|
60
|
+
ToTuple,
|
|
61
|
+
ToSet,
|
|
62
|
+
ToObject,
|
|
63
|
+
ToByteString,
|
|
64
|
+
ToByteArray,
|
|
65
|
+
ToDecimal,
|
|
66
|
+
ToDataObject,
|
|
67
|
+
)
|
|
68
|
+
from .DateTime import (
|
|
69
|
+
IsDateTime,
|
|
70
|
+
IsDate,
|
|
71
|
+
IsTime,
|
|
72
|
+
ToDateTime,
|
|
73
|
+
ToDate,
|
|
74
|
+
ToTime,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
__all__ = (
|
|
79
|
+
# COMMON
|
|
80
|
+
'SetDefault',
|
|
81
|
+
'IsToNone',
|
|
82
|
+
'IsNotToNone',
|
|
83
|
+
'IsNotEmpty',
|
|
84
|
+
# SIZE
|
|
85
|
+
'IsSizeOf',
|
|
86
|
+
'IsSizeIn',
|
|
87
|
+
'IsMinSizeOf',
|
|
88
|
+
'IsMaxSizeOf',
|
|
89
|
+
# COMPARE
|
|
90
|
+
'IsEqualTo',
|
|
91
|
+
'IsNotEqualTo',
|
|
92
|
+
'IsGreaterThan',
|
|
93
|
+
'IsGreaterEqualTo',
|
|
94
|
+
'IsLessEqualTo',
|
|
95
|
+
'IsLessThan',
|
|
96
|
+
'IsIn',
|
|
97
|
+
'IsNotIn',
|
|
98
|
+
# CONDITION
|
|
99
|
+
'All',
|
|
100
|
+
'Any',
|
|
101
|
+
# TYPE
|
|
102
|
+
'IsTypeOf',
|
|
103
|
+
'IsBool',
|
|
104
|
+
'IsInteger',
|
|
105
|
+
'IsFloat',
|
|
106
|
+
'IsNumber',
|
|
107
|
+
'IsString',
|
|
108
|
+
'IsList',
|
|
109
|
+
'IsTuple',
|
|
110
|
+
'IsSet',
|
|
111
|
+
'IsArray',
|
|
112
|
+
'IsObject',
|
|
113
|
+
'IsByteString',
|
|
114
|
+
'IsByteArray',
|
|
115
|
+
'IsDecimal',
|
|
116
|
+
'IsDataObject',
|
|
117
|
+
'ToTypeOf',
|
|
118
|
+
'ToBool',
|
|
119
|
+
'ToInteger',
|
|
120
|
+
'ToFloat',
|
|
121
|
+
'ToString',
|
|
122
|
+
'ToList',
|
|
123
|
+
'ToTuple',
|
|
124
|
+
'ToSet',
|
|
125
|
+
'ToObject',
|
|
126
|
+
'ToByteString',
|
|
127
|
+
'ToByteArray',
|
|
128
|
+
'ToDecimal',
|
|
129
|
+
'ToDataObject',
|
|
130
|
+
# DateTime
|
|
131
|
+
'IsDateTime',
|
|
132
|
+
'IsDate',
|
|
133
|
+
'IsTime',
|
|
134
|
+
'ToDateTime',
|
|
135
|
+
'ToDate',
|
|
136
|
+
'ToTime',
|
|
137
|
+
)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .Validator import Validator
|
|
4
|
+
from .Patterns import IsObject
|
|
5
|
+
from .Patterns.Object import IsMappingOf
|
|
6
|
+
|
|
7
|
+
from functools import wraps
|
|
8
|
+
from inspect import signature
|
|
9
|
+
|
|
10
|
+
__all__ = (
|
|
11
|
+
'Validate',
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Validate(object):
|
|
16
|
+
"""Decorator for Validator of Function and Method"""
|
|
17
|
+
def __init__(self, mappings: dict):
|
|
18
|
+
self.mappings = mappings
|
|
19
|
+
return
|
|
20
|
+
|
|
21
|
+
def __call__(self, fn: callable):
|
|
22
|
+
@wraps(fn)
|
|
23
|
+
def wrapper(*args, **kwargs):
|
|
24
|
+
validator = Validator(IsObject(IsMappingOf(self.mappings)))
|
|
25
|
+
bound = signature(fn).bind(*args, **kwargs)
|
|
26
|
+
bound.apply_defaults()
|
|
27
|
+
bound.arguments = validator(dict(bound.arguments))
|
|
28
|
+
return fn(*bound.args, **bound.kwargs)
|
|
29
|
+
return wrapper
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .Pattern import Pattern
|
|
4
|
+
|
|
5
|
+
__all__ = (
|
|
6
|
+
'Validator',
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Validator(object):
|
|
11
|
+
"""
|
|
12
|
+
Validator Class
|
|
13
|
+
"""
|
|
14
|
+
def __init__(self, *args):
|
|
15
|
+
for pattern in args:
|
|
16
|
+
if not isinstance(pattern, Pattern):
|
|
17
|
+
raise RuntimeError('{} must be based {}'.format(pattern, Pattern))
|
|
18
|
+
self.patterns = args
|
|
19
|
+
return
|
|
20
|
+
|
|
21
|
+
def __call__(self, parameter):
|
|
22
|
+
for pattern in self.patterns:
|
|
23
|
+
parameter = pattern(parameter)
|
|
24
|
+
return parameter
|
|
25
|
+
|
|
26
|
+
def __repr__(self):
|
|
27
|
+
return 'Validator({})'.format(
|
|
28
|
+
', '.join([repr(p) for p in self.patterns])
|
|
29
|
+
)
|
|
30
|
+
|