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

Source Code for Module lib.warnings

  1  from __future__ import absolute_import 
  2  ############################################################################### 
  3  #                                                                             # 
  4  # Copyright (C) 2003-2008,2010,2013-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 containing all of the RelaxWarning objects.""" 
 25   
 26  # Python module imports. 
 27  import inspect 
 28  import sys 
 29  import warnings 
 30   
 31  # relax module imports. 
 32  from lib import ansi 
 33   
 34   
 35  # Module variables for changing the behaviour of the warning system. 
 36  ESCALATE = False    # If True, warnings will be converted into errors. 
 37  TRACEBACK = False    # If True, a traceback will be printed out with the warnings. 
 38   
 39   
 40  # The warning formatting function. 
41 -def format(message, category, filename, lineno, line=None):
42 """Replacement for warnings.formatwarning to customise output format.""" 43 44 # Add the text 'RelaxWarning: ' to the start of the warning message. 45 message = "RelaxWarning: %s\n" % message 46 47 # Text colouring 48 if ansi.enable_control_chars(stream=2): 49 # Strip the last newline, if it exists. 50 if message[-1] == '\n': 51 message = message[:-1] 52 53 # Reformat. 54 message = "%s%s%s\n" % (ansi.relax_warning, message, ansi.end) 55 56 # Return the warning message. 57 return message
58 59
60 -def showwarning_tb(message, category, filename, lineno, file=None, line=None):
61 """Replacement for warnings.showwarning to show tracebacks.""" 62 63 # Set up the output file if needed. 64 if file is None: 65 file = sys.stderr 66 67 # Print the stack traceback. 68 tb = "" 69 for frame in inspect.stack()[1:]: 70 file_name = frame[1] 71 lineNo = frame[2] 72 func = frame[3] 73 tb_frame = ' File "%s", line %i, in %s\n' % (file_name, lineNo, func) 74 try: 75 context = frame[4][frame[5]] 76 except TypeError: 77 pass 78 else: 79 tb_frame = '%s %s\n' % (tb_frame, context.strip()) 80 tb = tb_frame + tb 81 tb = "Traceback (most recent call last):\n%s" % tb 82 file.write(tb) 83 84 # Replicating the failsafe mode of the base Python warnings function here. 85 try: 86 file.write(format(message, category, filename, lineno, line)) 87 except IOError: 88 pass
89 90
91 -def setup():
92 """Set up the warning system.""" 93 94 # Format warning messages. 95 warnings.formatwarning = format 96 97 # Tracebacks. 98 if TRACEBACK: 99 warnings.showwarning = showwarning_tb 100 101 # Set warning filters. 102 if ESCALATE: 103 warnings.filterwarnings('error', category=BaseWarning) 104 else: 105 warnings.filterwarnings('always', category=BaseWarning)
106 107 108 109 # Base class for all warnings. 110 ############################## 111
112 -class BaseWarning(Warning):
113 - def __str__(self):
114 return str(self.text)
115 116 117 # Standard warnings. 118 #################### 119
120 -class RelaxWarning(BaseWarning):
121 - def __init__(self, text):
122 self.text = text
123 124 125 # Sequence errors. 126 ################## 127 128 # Cannot find the spin in the sequence.
129 -class RelaxNoSpinWarning(BaseWarning):
130 - def __init__(self, spin_id):
131 self.text = "Cannot find the spin %s within the sequence." % spin_id
132 133 134 # PDB warnings. 135 ############### 136 137 # Zero length interactomic vector.
138 -class RelaxZeroVectorWarning(BaseWarning):
139 - def __init__(self, spin_id1, spin_id2):
140 self.text = "The interatomic vector between the spins '%s' and '%s' is of zero length." % (spin_id1, spin_id2)
141 142 143 # The atom is missing from the PDB file.
144 -class RelaxNoAtomWarning(BaseWarning):
145 - def __init__(self, atom, res):
146 self.text = "The atom %s could not be found for residue %i" % (atom, res)
147 148 149 # The PDB file is missing.
150 -class RelaxNoPDBFileWarning(BaseWarning):
151 - def __init__(self, file):
152 self.text = "The PDB file %s cannot be found, no structures will be loaded." % file
153 154 155 # Nuclear warnings. 156 ################### 157 158 # Nucleus not set.
159 -class RelaxNucleusWarning(BaseWarning):
160 - def __init__(self, spin_id=None):
161 if spin_id != None: 162 self.text = "The type of nucleus for the spin '%s' has not yet been set." % spin_id 163 else: 164 self.text = "The type of nucleus has not yet been set."
165 166 # Spin type not set.
167 -class RelaxSpinTypeWarning(BaseWarning):
168 - def __init__(self, spin_id=None):
169 if spin_id != None: 170 self.text = "The nuclear isotope type for the spin '%s' has not yet been set. Please use the spin.isotope user function to set the type." % spin_id 171 else: 172 self.text = "The nuclear isotope type has not yet been set. Please use the spin.isotope user function to set the type."
173 174 # File warnings. 175 ################ 176 177 # No data in file.
178 -class RelaxFileEmptyWarning(BaseWarning):
179 - def __init__(self, file):
180 self.text = "The file '%s' contains no data." % file
181 182 183 # Misc. 184 ####### 185 186 # Deselection warnings
187 -class RelaxDeselectWarning(BaseWarning):
188 - def __init__(self, spin_id, reason):
189 self.text = "The spin '%s' has been deselected because of %s." % (spin_id, reason)
190 191 192 # Spectrometer information warnings. 193 #################################### 194 195 # No frequency information.
196 -class RelaxNoFrqWarning(BaseWarning):
197 - def __init__(self, pipe_name=None, id=None):
198 self.text = "No spectrometer frequency information" 199 if id != None: 200 self.text += " for the '%s' experiment ID" % id 201 self.text += " is present" 202 if pipe_name != None: 203 self.text += " in the '%s' data pipe" % pipe_name 204 self.text += "."
205