Author: macraild Date: Thu Aug 31 12:36:29 2006 New Revision: 2543 URL: http://svn.gna.org/viewcvs/relax?rev=2543&view=rev Log: Further modifications to the Warnings system. Discussed in thread starting https://mail.gna.org/public/relax-devel/2006-08/msg00082.html Modified: branches/warning/errors.py branches/warning/generic_fns/pdb.py Modified: branches/warning/errors.py URL: http://svn.gna.org/viewcvs/relax/branches/warning/errors.py?rev=2543&r1=2542&r2=2543&view=diff ============================================================================== --- branches/warning/errors.py (original) +++ branches/warning/errors.py Thu Aug 31 12:36:29 2006 @@ -604,6 +604,30 @@ # Warning objects. ################## +# Replacement for warnings.formatwarning to customise output format. +def format(message, category, filename, lineno): + if issubclass(category, self.BaseWarning): + message = "RelaxWarning: %s\n\n" % message + + if Debug: + tb = "" + for frame in inspect.stack()[4:]: + file = frame[1] + lineNo = frame[2] + func = frame[3] + tb_frame = ' File "%s", line %i, in %s\n' % (file, lineNo, func) + try: + context = frame[4][frame[5]] + except TypeError: + pass + else: + tb_frame = '%s %s\n' % (tb_frame, context.strip()) + tb = tb_frame + tb + tb = "Traceback (most recent call last):\n%s" % tb + message = tb + message + + return message + class RelaxWarnings: def __init__(self): """Class for placing all the warnings below into __builtin__""" @@ -627,30 +651,7 @@ __builtin__.AllRelaxWarnings = object, # Format warning messages. - def format(message, category, filename, lineno): - if issubclass(category, self.BaseWarning): - message = "RelaxWarning: %s\n\n" % message - - if Debug: - tb = "" - for frame in inspect.stack()[4:]: - file = frame[1] - lineNo = frame[2] - func = frame[3] - tb_frame = ' File "%s", line %i, in %s\n' % (file, lineNo, func) - try: - context = frame[4][frame[5]] - except TypeError: - pass - else: - tb_frame = '%s %s\n' % (tb_frame, context.strip()) - tb = tb_frame + tb - tb = "Traceback (most recent call last):\n%s" % tb - message = tb + message - - return message - - warnings.formatwarning = format + warnings.formatwarning = self.format # Set warning filters. if Pedantic: @@ -658,6 +659,32 @@ else: warnings.filterwarnings('always', category=self.BaseWarning) + # Replacement for warnings.formatwarning to customise output format. + def format(self, message, category, filename, lineno): + if issubclass(category, self.BaseWarning): + message = "RelaxWarning: %s\n\n" % message + + # Print stack-trace in debug mode. + if Debug: + tb = "" + for frame in inspect.stack()[4:]: + file = frame[1] + lineNo = frame[2] + func = frame[3] + tb_frame = ' File "%s", line %i, in %s\n' % (file, lineNo, func) + try: + context = frame[4][frame[5]] + except TypeError: + pass + else: + tb_frame = '%s %s\n' % (tb_frame, context.strip()) + tb = tb_frame + tb + tb = "Traceback (most recent call last):\n%s" % tb + message = tb + message + + return message + + # Base class for all warnings. ############################ Modified: branches/warning/generic_fns/pdb.py URL: http://svn.gna.org/viewcvs/relax/branches/warning/generic_fns/pdb.py?rev=2543&r1=2542&r2=2543&view=diff ============================================================================== --- branches/warning/generic_fns/pdb.py (original) +++ branches/warning/generic_fns/pdb.py Thu Aug 31 12:36:29 2006 @@ -152,8 +152,7 @@ if fail: raise RelaxFileError, ('PDB', self.file_path) else: - if self.print_flag: - warn(RelaxNoPDBFileWarning(self.file_path)) + warn(RelaxNoPDBFileWarning(self.file_path)) return @@ -238,14 +237,12 @@ # Test if the proton atom exists for residue i. if not pdb_res.atoms.has_key(self.proton): - if self.print_flag: - warn(RelaxNoAtomWarning(self.proton, self.relax.data.res[self.run][j].num)) + warn(RelaxNoAtomWarning(self.proton, self.relax.data.res[self.run][j].num)) self.relax.data.res[self.run][j].xh_vect.append(None) # Test if the heteronucleus atom exists for residue i. elif not pdb_res.atoms.has_key(self.heteronuc): - if self.print_flag: - warn(RelaxNoAtomWarning(self.heteronuc, self.relax.data.res[self.run][j].num)) + warn(RelaxNoAtomWarning(self.heteronuc, self.relax.data.res[self.run][j].num)) self.relax.data.res[self.run][j].xh_vect.append(None) # Calculate the vector.