Module relax_warnings
[hide private]
[frames] | no frames]

Source Code for Module relax_warnings

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module containing all of the RelaxWarning objects.""" 
 24   
 25  # Python module imports. 
 26  import inspect 
 27  import sys 
 28  import warnings 
 29   
 30  # relax module imports. 
 31  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 XH bond vector.
116 -class RelaxZeroVectorWarning(BaseWarning):
117 - def __init__(self, res):
118 self.text = "The XH bond vector for residue " + repr(res) + " is of zero length."
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 # File warnings. 134 ################ 135 136 # No data in file.
137 -class RelaxFileEmptyWarning(BaseWarning):
138 - def __init__(self, file):
139 self.text = "The file '%s' contains no data." % file
140 141 142 # Misc. 143 ####### 144 145 # Deselection warnings
146 -class RelaxDeselectWarning(BaseWarning):
147 - def __init__(self, spin_id, reason):
148 self.text = "The spin '%s' has been deselected because of %s." % (spin_id, reason)
149