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