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.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 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  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 pedantic mode. 44 if status.pedantic: 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.pedantic: 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 XH bond vector.
117 -class RelaxZeroVectorWarning(BaseWarning):
118 - def __init__(self, res):
119 self.text = "The XH bond vector for residue " + repr(res) + " is of zero length."
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 # File warnings. 135 ################ 136 137 # No data in file.
138 -class RelaxFileEmptyWarning(BaseWarning):
139 - def __init__(self, file):
140 self.text = "The file '%s' contains no data." % file
141 142 143 # Misc. 144 ####### 145 146 # Deselection warnings
147 -class RelaxDeselectWarning(BaseWarning):
148 - def __init__(self, spin_id, reason):
149 self.text = "The spin '%s' has been deselected because of %s." % (spin_id, reason)
150