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-2014 Edward d'Auvergne                                   # 
  5  # Copyright (C) 2014 Troels E. Linnet                                         # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module containing all of the RelaxWarning objects.""" 
 26   
 27  # Python module imports. 
 28  import inspect 
 29  import sys 
 30  import warnings 
 31   
 32  # relax module imports. 
 33  from lib import ansi 
 34   
 35   
 36  # Module variables for changing the behaviour of the warning system. 
 37  ESCALATE = False    # If True, warnings will be converted into errors. 
 38  TRACEBACK = False    # If True, a traceback will be printed out with the warnings. 
 39   
 40   
 41  # The warning formatting function. 
42 -def format(message, category, filename, lineno, line=None):
43 """Replacement for warnings.formatwarning to customise output format.""" 44 45 # Add the text 'RelaxWarning: ' to the start of the warning message. 46 message = "RelaxWarning: %s\n" % message 47 48 # Text colouring 49 if ansi.enable_control_chars(stream=2): 50 # Strip the last newline, if it exists. 51 if message[-1] == '\n': 52 message = message[:-1] 53 54 # Reformat. 55 message = "%s%s%s\n" % (ansi.relax_warning, message, ansi.end) 56 57 # Return the warning message. 58 return message
59 60
61 -def showwarning_tb(message, category, filename, lineno, file=None, line=None):
62 """Replacement for warnings.showwarning to show tracebacks.""" 63 64 # Set up the output file if needed. 65 if file is None: 66 file = sys.stderr 67 68 # Print the stack traceback. 69 tb = "" 70 for frame in inspect.stack()[1:]: 71 file_name = frame[1] 72 lineNo = frame[2] 73 func = frame[3] 74 tb_frame = ' File "%s", line %i, in %s\n' % (file_name, lineNo, func) 75 try: 76 context = frame[4][frame[5]] 77 except TypeError: 78 pass 79 else: 80 tb_frame = '%s %s\n' % (tb_frame, context.strip()) 81 tb = tb_frame + tb 82 tb = "Traceback (most recent call last):\n%s" % tb 83 file.write(tb) 84 85 # Replicating the failsafe mode of the base Python warnings function here. 86 try: 87 file.write(format(message, category, filename, lineno, line)) 88 except IOError: 89 pass
90 91
92 -def setup():
93 """Set up the warning system.""" 94 95 # Format warning messages. 96 warnings.formatwarning = format 97 98 # Tracebacks. 99 if TRACEBACK: 100 warnings.showwarning = showwarning_tb 101 102 # Set warning filters. 103 if ESCALATE: 104 warnings.filterwarnings('error', category=BaseWarning) 105 else: 106 warnings.filterwarnings('always', category=BaseWarning)
107 108 109 110 # Base class for all warnings. 111 ############################## 112
113 -class BaseWarning(Warning):
114 - def __str__(self):
115 return str(self.text)
116 117 118 # Standard warnings. 119 #################### 120
121 -class RelaxWarning(BaseWarning):
122 - def __init__(self, text):
123 self.text = text
124 125 126 # Sequence errors. 127 ################## 128 129 # Cannot find the spin in the sequence.
130 -class RelaxNoSpinWarning(BaseWarning):
131 - def __init__(self, spin_id):
132 self.text = "Cannot find the spin %s within the sequence." % spin_id
133 134 135 # PDB warnings. 136 ############### 137 138 # Zero length interactomic vector.
139 -class RelaxZeroVectorWarning(BaseWarning):
140 - def __init__(self, spin_id1, spin_id2):
141 self.text = "The interatomic vector between the spins '%s' and '%s' is of zero length." % (spin_id1, spin_id2)
142 143 144 # The atom is missing from the PDB file.
145 -class RelaxNoAtomWarning(BaseWarning):
146 - def __init__(self, atom, res):
147 self.text = "The atom %s could not be found for residue %i" % (atom, res)
148 149 150 # The PDB file is missing.
151 -class RelaxNoPDBFileWarning(BaseWarning):
152 - def __init__(self, file):
153 self.text = "The PDB file %s cannot be found, no structures will be loaded." % file
154 155 156 # Nuclear warnings. 157 ################### 158 159 # Nucleus not set.
160 -class RelaxNucleusWarning(BaseWarning):
161 - def __init__(self, spin_id=None):
162 if spin_id != None: 163 self.text = "The type of nucleus for the spin '%s' has not yet been set." % spin_id 164 else: 165 self.text = "The type of nucleus has not yet been set."
166 167 # Spin type not set.
168 -class RelaxSpinTypeWarning(BaseWarning):
169 - def __init__(self, spin_id=None):
170 if spin_id != None: 171 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 172 else: 173 self.text = "The nuclear isotope type has not yet been set. Please use the spin.isotope user function to set the type."
174 175 # File warnings. 176 ################ 177 178 # No data in file.
179 -class RelaxFileEmptyWarning(BaseWarning):
180 - def __init__(self, file):
181 self.text = "The file '%s' contains no data." % file
182 183 184 # Misc. 185 ####### 186 187 # Deselection warnings
188 -class RelaxDeselectWarning(BaseWarning):
189 - def __init__(self, spin_id, reason):
190 self.text = "The spin '%s' has been deselected because of %s." % (spin_id, reason)
191