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.warnings import RelaxWarning 
31   
32   
33 -class Check:
34 """Data checking class based on the U{strategy design pattern<https://en.wikipedia.org/wiki/Strategy_pattern>}.""" 35
36 - def __init__(self, function):
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 # Store the function. 44 self.checks = function
45 46
47 - def __call__(self, *args, **kargs):
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 # Remove the escalate keyword argument. 61 if 'escalate' not in kargs: 62 escalate = 2 63 else: 64 escalate = kargs['escalate'] 65 del kargs['escalate'] 66 67 # Perform the check. 68 error = self.checks(*args, **kargs) 69 70 # No errors. 71 if error == None: 72 return True 73 74 # Send the text of the RelaxError object into the RelaxWarning system. 75 if escalate == 1: 76 warn(RelaxWarning(error.text)) 77 return False 78 79 # The error system. 80 if escalate == 2: 81 raise error
82