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-2013 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 warnings 
 29   
 30  # relax module imports. 
 31  from lib import ansi 
 32  from status import Status; status = Status() 
 33   
 34   
 35  # The warning formatting function. 
36 -def format(message, category, filename, lineno, line=None):
37 """ Replacement for warnings.formatwarning to customise output format.""" 38 39 # Add the text 'RelaxWarning: ' to the start of the warning message. 40 message = "RelaxWarning: %s\n" % message 41 42 # Print stack-trace in pedantic mode. 43 if status.pedantic: 44 tb = "" 45 for frame in inspect.stack()[4:]: 46 file = frame[1] 47 lineNo = frame[2] 48 func = frame[3] 49 tb_frame = ' File "%s", line %i, in %s\n' % (file, lineNo, func) 50 try: 51 context = frame[4][frame[5]] 52 except TypeError: 53 pass 54 else: 55 tb_frame = '%s %s\n' % (tb_frame, context.strip()) 56 tb = tb_frame + tb 57 tb = "Traceback (most recent call last):\n%s" % tb 58 message = tb + message 59 60 # Text colouring 61 if ansi.enable_control_chars(stream=2): 62 # Strip the last newline, if it exists. 63 if message[-1] == '\n': 64 message = message[:-1] 65 66 # Reformat. 67 message = "%s%s%s\n" % (ansi.relax_warning, message, ansi.end) 68 69 # Return the warning message. 70 return message
71 72
73 -def setup():
74 """Set up the warning system.""" 75 76 # Format warning messages. 77 warnings.formatwarning = format 78 79 # Set warning filters. 80 if status.pedantic: 81 warnings.filterwarnings('error', category=BaseWarning) 82 else: 83 warnings.filterwarnings('always', category=BaseWarning)
84 85 86 87 # Base class for all warnings. 88 ############################## 89
90 -class BaseWarning(Warning):
91 - def __str__(self):
92 return str(self.text)
93 94 95 # Standard warnings. 96 #################### 97
98 -class RelaxWarning(BaseWarning):
99 - def __init__(self, text):
100 self.text = text
101 102 103 # Sequence errors. 104 ################## 105 106 # Cannot find the spin in the sequence.
107 -class RelaxNoSpinWarning(BaseWarning):
108 - def __init__(self, spin_id):
109 self.text = "Cannot find the spin %s within the sequence." % spin_id
110 111 112 # PDB warnings. 113 ############### 114 115 # Zero length interactomic vector.
116 -class RelaxZeroVectorWarning(BaseWarning):
117 - def __init__(self, spin_id1, spin_id2):
118 self.text = "The interatomic vector between the spins '%s' and '%s' is of zero length." % (spin_id1, spin_id2)
119 120 121 # The atom is missing from the PDB file.
122 -class RelaxNoAtomWarning(BaseWarning):
123 - def __init__(self, atom, res):
124 self.text = "The atom %s could not be found for residue %i" % (atom, res)
125 126 127 # The PDB file is missing.
128 -class RelaxNoPDBFileWarning(BaseWarning):
129 - def __init__(self, file):
130 self.text = "The PDB file %s cannot be found, no structures will be loaded." % file
131 132 133 # Nuclear warnings. 134 ################### 135 136 # Nucleus not set.
137 -class RelaxNucleusWarning(BaseWarning):
138 - def __init__(self, spin_id=None):
139 if spin_id != None: 140 self.text = "The type of nucleus for the spin '%s' has not yet been set." % spin_id 141 else: 142 self.text = "The type of nucleus has not yet been set."
143 144 # Spin type not set.
145 -class RelaxSpinTypeWarning(BaseWarning):
146 - def __init__(self, spin_id=None):
147 if spin_id != None: 148 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 149 else: 150 self.text = "The nuclear isotope type has not yet been set. Please use the spin.isotope user function to set the type."
151 152 # File warnings. 153 ################ 154 155 # No data in file.
156 -class RelaxFileEmptyWarning(BaseWarning):
157 - def __init__(self, file):
158 self.text = "The file '%s' contains no data." % file
159 160 161 # Misc. 162 ####### 163 164 # Deselection warnings
165 -class RelaxDeselectWarning(BaseWarning):
166 - def __init__(self, spin_id, reason):
167 self.text = "The spin '%s' has been deselected because of %s." % (spin_id, reason)
168