Author: bugman Date: Fri Aug 11 19:58:47 2006 New Revision: 2535 URL: http://svn.gna.org/viewcvs/relax?rev=2535&view=rev Log: Start of a new relax error/warning subsystem. The idea of this system was suggested in two threads which commence at: https://mail.gna.org/public/relax-devel/2006-08/msg00052.html https://mail.gna.org/public/relax-devel/2006-08/msg00053.html Essentially a rudimentary framework for replacing the entire Error and Warning system has been set up. Many essential elements are missing (for example the warning function, tracebacks, etc) however it shouldn't take long to add these. This new system significantly simplifies the RelaxError/RelaxWarning system, allows easy implementation of the --ignore-errors and --pedantic command line flags (https://mail.gna.org/public/relax-devel/2006-08/msg00052.html), and allows fine tuning of how RelaxErrors and RelaxWarnings are handled within the different relax user interfaces (UIs). Modified: 1.2/errors.py 1.2/relax Modified: 1.2/errors.py URL: http://svn.gna.org/viewcvs/relax/1.2/errors.py?rev=2535&r1=2534&r2=2535&view=diff ============================================================================== --- 1.2/errors.py (original) +++ 1.2/errors.py Fri Aug 11 19:58:47 2006 @@ -21,28 +21,64 @@ ############################################################################### import __builtin__ -from re import match +from re import match, search from types import ClassType import time - - -class RelaxErrors: - def __init__(self, relax): - """Class for placing all the errors below into __builtin__""" +import sys + + +class RelaxErrorSystem: + def __init__(self, relax, UI_mode=None): + """The relax error and warning system.""" + + # Arguments. + self.relax = relax + self.UI_mode = UI_mode + + # Instant exiting modes (i.e. no error system is necessary). + if UI_mode == 'version': + return + + # Loop over the errors, set their error handling functions, and pack them into __builtin__. + self.initialise_errors() + + + def error(self, message): + """Default error function - traceback, error message, then sys.exit().""" + + # Write the traceback to stderr. + + # Write the error message to stderr. + sys.stderr.write("RelaxError: " + message + "\n") + + # Hard exit! + sys.exit() + + + def initialise_errors(self): + """Set the error handling functions and place the errors into __builtin__.""" # Loop over all objects in 'self'. for name in dir(self): # Get the object. object = getattr(self, name) - # Skip over all non error class objects. - if type(object) != ClassType or not match('Relax', name): + # Skip over all non error/warning class objects. + if type(object) != ClassType or not search('^Relax', name): continue - # Add the top level relax class: - setattr(object, '_relax', relax) - - # Place the exceptions into __builtin__ + # Add the top level relax class. + setattr(object, 'relax', self.relax) + + # Set up the RelaxError handling functions. + if search('Error$', name): + object.exception = self.error + + # Set up the RelaxWarning handling functions. + if search('Warning$', name): + object.exception = self.warning + + # Place the errors into __builtin__ __builtin__.__setattr__(name, object) # Tuple of all the errors. @@ -52,12 +88,56 @@ __builtin__.AllRelaxErrors = object, + + class RelaxBadError: + def __init__(self, text): + """This error is really BAD!!!""" + + # Format the message. + message = text + " - This error is very, very bad." + + # Save the program state. + if Debug: + self.save_state() + + # Run the error handling code. + self.exception(message) + + + +class RelaxErrors: + def __init__(self, relax): + """Class for placing all the errors below into __builtin__""" + + # Loop over all objects in 'self'. + for name in dir(self): + # Get the object. + object = getattr(self, name) + + # Skip over all non error class objects. + if type(object) != ClassType or not match('Relax', name): + continue + + # Add the top level relax class: + setattr(object, '_relax', relax) + + # Place the exceptions into __builtin__ + __builtin__.__setattr__(name, object) + + # Tuple of all the errors. + if hasattr(__builtin__, 'AllRelaxErrors'): + __builtin__.AllRelaxErrors = __builtin__.AllRelaxErrors, object + else: + __builtin__.AllRelaxErrors = object, + + # Base class for all errors. ############################ class BaseError(Exception): def __str__(self): return ("RelaxError: " + self.text + "\n") + def save_state(self): now = time.localtime() file_name = "relax_state_%i%02i%02i_%02i%02i%02i" % (now[0], Modified: 1.2/relax URL: http://svn.gna.org/viewcvs/relax/1.2/relax?rev=2535&r1=2534&r2=2535&view=diff ============================================================================== --- 1.2/relax (original) +++ 1.2/relax Fri Aug 11 19:58:47 2006 @@ -66,7 +66,7 @@ # relax modules. from colour import Colour from data import Data -from errors import RelaxErrors +from errors import RelaxErrorSystem, RelaxErrors from io import IO from generic_fns.main import Generic from prompt.gpl import gpl @@ -128,6 +128,9 @@ # Process the command line arguments and determine the relax mode. mode, log_file, tee_file = self.arguments() + # Set up the relax error system. + RelaxErrorSystem(self, UI_mode=mode) + # Show the version number and exit. if mode == 'version': print 'relax ' + self.version