1 from __future__ import absolute_import
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module for the Check class based on the strategy design pattern."""
25
26
27 from warnings import warn
28
29
30 from lib.warnings import RelaxWarning
31
32
34 """Data checking class based on the U{strategy design pattern<https://en.wikipedia.org/wiki/Strategy_pattern>}."""
35
37 """Store the function argument for use in the __call__() method.
38
39 @param function: The function to store as self.checks which is called from the __call__() method.
40 @type function: function
41 """
42
43
44 self.checks = function
45
46
48 """Make the object callable, and perform the checks.
49
50 This will call the function used to initialise the class and then
51
52
53 @keyword escalate: The feedback to give if the check fails. This can be 0 for no printouts, 1 to throw a RelaxWarning, or 2 to raise a RelaxError.
54 @type escalate: int
55 @raises RelaxError: If escalate is set to 2 and the check fails.
56 @return: True if the check passes, False otherwise.
57 @rtype: bool
58 """
59
60
61 if 'escalate' not in kargs:
62 escalate = 2
63 else:
64 escalate = kargs['escalate']
65 del kargs['escalate']
66
67
68 error = self.checks(*args, **kargs)
69
70
71 if error == None:
72 return True
73
74
75 if escalate == 1:
76 warn(RelaxWarning(error.text))
77 return False
78
79
80 if escalate == 2:
81 raise error
82