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.errors import RelaxError
31 from lib.warnings import RelaxWarning
32
33
35 """Data checking class based on the U{strategy design pattern<https://en.wikipedia.org/wiki/Strategy_pattern>}."""
36
38 """Store the function argument for use in the __call__() method.
39
40 @param function: The function to store as self.checks which is called from the __call__() method.
41 @type function: function
42 """
43
44
45 self.checks = function
46
47
49 """Make the object callable, and perform the checks.
50
51 This will call the function used to initialise the class and then
52
53
54 @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.
55 @type escalate: int
56 @raises RelaxError: If escalate is set to 2 and the check fails.
57 @return: True if the check passes, False otherwise.
58 @rtype: bool
59 """
60
61
62 if 'escalate' not in kargs:
63 escalate = 2
64 else:
65 escalate = kargs['escalate']
66 del kargs['escalate']
67
68
69 error = self.checks(*args, **kargs)
70
71
72 if error == None:
73 return True
74
75
76 if escalate == 1:
77 warn(RelaxWarning(error.text))
78 return False
79
80
81 if escalate == 2:
82 raise error
83