Package lib :: Module checks
[hide private]
[frames] | no frames]

Source Code for Module lib.checks

 1  from __future__ import absolute_import 
 2  ############################################################################### 
 3  #                                                                             # 
 4  # Copyright (C) 2014 Edward d'Auvergne                                        # 
 5  #                                                                             # 
 6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 7  #                                                                             # 
 8  # This program is free software: you can redistribute it and/or modify        # 
 9  # it under the terms of the GNU General Public License as published by        # 
10  # the Free Software Foundation, either version 3 of the License, or           # 
11  # (at your option) any later version.                                         # 
12  #                                                                             # 
13  # This program is distributed in the hope that it will be useful,             # 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
16  # GNU General Public License for more details.                                # 
17  #                                                                             # 
18  # You should have received a copy of the GNU General Public License           # 
19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Module for the Check class based on the strategy design pattern.""" 
25   
26  # Python module imports. 
27  from warnings import warn 
28   
29  # relax module imports. 
30  from lib.errors import RelaxError 
31  from lib.warnings import RelaxWarning 
32   
33   
34 -class Check:
35 """Data checking class based on the U{strategy design pattern<https://en.wikipedia.org/wiki/Strategy_pattern>}.""" 36
37 - def __init__(self, function):
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 # Store the function. 45 self.checks = function
46 47
48 - def __call__(self, *args, **kargs):
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 # Remove the escalate keyword argument. 62 if 'escalate' not in kargs: 63 escalate = 2 64 else: 65 escalate = kargs['escalate'] 66 del kargs['escalate'] 67 68 # Perform the check. 69 error = self.checks(*args, **kargs) 70 71 # No errors. 72 if error == None: 73 return True 74 75 # Send the text of the RelaxError object into the RelaxWarning system. 76 if escalate == 1: 77 warn(RelaxWarning(error.text)) 78 return False 79 80 # The error system. 81 if escalate == 2: 82 raise error
83