def __init__(self, show_script=True, quit=True, raise_relax_error=False): """The interpreter class. @param show_script: If true, the relax will print the script contents prior to executing the script. @type show_script: bool @param quit: If true, the default, then relax will exit after running the run() method. @type quit: bool @param raise_relax_error: If false, the default, then relax will print a nice error message to STDERR, without a traceback, when a RelaxError occurs. This is to make things nicer for the user. @type raise_relax_error: bool """ # Place the arguments in the class namespace. self.__show_script = show_script self.__quit_flag = quit self.__raise_relax_error = raise_relax_error # Build the intro string. info = Info_box() self.__intro_string = info.intro_text() # The prompts (change the Python prompt, as well as the function printouts). if ansi.enable_control_chars(stream=1): self.prompt_colour_on() else: self.prompt_colour_off() # Set up the interpreter objects. self._locals = self._setup()
def format(message, category, filename, lineno, line=None): """ Replacement for warnings.formatwarning to customise output format.""" # Add the text 'RelaxWarning: ' to the start of the warning message. message = "RelaxWarning: %s\n" % message # Print stack-trace in pedantic mode. if status.pedantic: 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 # Text colouring if ansi.enable_control_chars(stream=2): # Strip the last newline, if it exists. if message[-1] == '\n': message = message[:-1] # Reformat. message = "%s%s%s\n" % (ansi.relax_warning, message, ansi.end) # Return the warning message. return message
def __init__(self, show_script=True, raise_relax_error=False): """The interpreter class. @param show_script: If true, the relax will print the script contents prior to executing the script. @type show_script: bool @param raise_relax_error: If false, the default, then relax will print a nice error message to STDERR, without a traceback, when a RelaxError occurs. This is to make things nicer for the user. @type raise_relax_error: bool """ # Place the arguments in the class namespace. self.__show_script = show_script self.__raise_relax_error = raise_relax_error # Build the intro string. info = Info_box() self.__intro_string = info.intro_text() # The prompts (change the Python prompt, as well as the function printouts). if ansi.enable_control_chars(stream=1) and not status.show_gui: self.prompt_colour_on() else: self.prompt_colour_off() # Set up the interpreter objects. self._locals = self._setup()
def __str__(self): """Modify the behaviour of the error system.""" # Save the state if the escalate flag is turned on. if SAVE_ERROR_STATE: save_state() # Modify the error message to include 'RelaxError' at the start (using coloured text if a TTY). if ansi.enable_control_chars(stream=2): return ("%sRelaxError: %s%s\n" % (ansi.relax_error, self.text, ansi.end)) else: return ("RelaxError: %s\n" % self.text)
def __str__(self): """Modify the behaviour of the error system.""" # Save the state if the pedantic flag is turned on. from status import Status; status = Status() if status.pedantic: save_state() # Modify the error message to include 'RelaxError' at the start (using coloured text if a TTY). if ansi.enable_control_chars(stream=2): return ("%sRelaxError: %s%s\n" % (ansi.relax_error, self.text, ansi.end)) else: return ("RelaxError: %s\n" % self.text)
def format(message, category, filename, lineno, line=None): """Replacement for warnings.formatwarning to customise output format.""" # Add the text 'RelaxWarning: ' to the start of the warning message. message = "RelaxWarning: %s\n" % message # Text colouring if ansi.enable_control_chars(stream=2): # Strip the last newline, if it exists. if message[-1] == '\n': message = message[:-1] # Reformat. message = "%s%s%s\n" % (ansi.relax_warning, message, ansi.end) # Return the warning message. return message
def interact_script(self, intro=None, local={}, script_file=None, quit=True, show_script=True, raise_relax_error=False): """Replacement function for 'code.InteractiveConsole.interact'. This will execute the script file. @param intro: The string to print prior to jumping to the prompt mode. @type intro: str @param local: A namespace which will become that of the prompt (i.e. the namespace visible to the user when in the prompt mode). This should be the output of a function such as locals(). @type local: dict @param script_file: The script file to be executed. @type script_file: None or str @param quit: If true, the default, then relax will exit after running the script. @type quit: bool @param show_script: If true, the relax will print the script contents prior to executing the script. @type show_script: bool @param raise_relax_error: If false, the default, then a nice error message will be sent to STDERR, without a traceback, when a RelaxError occurs. This is to make things nicer for the user. @type raise_relax_error: bool """ # Print the program introduction. if intro: sys.stdout.write("%s\n" % intro) # Print the script. if show_script: try: file = open(script_file, 'r') except IOError: try: raise RelaxError("The script file '" + script_file + "' does not exist.") except AllRelaxErrors: instance = sys.exc_info()[1] sys.stdout.write(instance.__str__()) sys.stdout.write("\n") return # Coloured text. if ansi.enable_control_chars(stream=1): sys.stdout.write(ansi.script) # Print the script. sys.stdout.write("script = " + repr(script_file) + "\n") sys.stdout.write("----------------------------------------------------------------------------------------------------\n") sys.stdout.write(file.read()) sys.stdout.write("----------------------------------------------------------------------------------------------------") # End coloured text. if ansi.enable_control_chars(stream=1): sys.stdout.write(ansi.end) # Terminating newline. sys.stdout.write("\n") # Close the script file handle. file.close() # The execution flag. exec_pass = True # Execute the script. try: exec_script(script_file, local) # Catch ctrl-C. except KeyboardInterrupt: # Throw the error. if status.debug: raise # Be nicer to the user. else: sys.stderr.write("\nScript execution cancelled.\n") # The script failed. exec_pass = False # Catch the RelaxErrors. except AllRelaxErrors: instance = sys.exc_info()[1] # Throw the error. if raise_relax_error: raise # Nice output for the user. else: # Print the scary traceback normally hidden from the user. if status.debug: self.showtraceback() # Print the RelaxError message line. else: sys.stderr.write(instance.__str__()) # The script failed. exec_pass = False # Throw all other errors. except: # Raise the error. raise # Add an empty line to make exiting relax look better. if show_script: sys.stdout.write("\n") # Quit relax. # FIXME: need to drop off end of interpreter loop to exit cleanly #if quit: # sys.exit() # Return the execution flag. return exec_pass
def interact_script(self, intro=None, local={}, script_file=None, show_script=True, raise_relax_error=False): """Replacement function for 'code.InteractiveConsole.interact'. This will execute the script file. @param intro: The string to print prior to jumping to the prompt mode. @type intro: str @param local: A namespace which will become that of the prompt (i.e. the namespace visible to the user when in the prompt mode). This should be the output of a function such as locals(). @type local: dict @param script_file: The script file to be executed. @type script_file: None or str @param show_script: If true, the relax will print the script contents prior to executing the script. @type show_script: bool @param raise_relax_error: If false, the default, then a nice error message will be sent to STDERR, without a traceback, when a RelaxError occurs. This is to make things nicer for the user. @type raise_relax_error: bool """ # Print the program introduction. if intro: sys.stdout.write("%s\n" % intro) # Print the script. if show_script: try: file = open(script_file, 'r') except IOError: try: raise RelaxError("The script file '" + script_file + "' does not exist.") except AllRelaxErrors: instance = sys.exc_info()[1] sys.stdout.write(instance.__str__()) sys.stdout.write("\n") return # Coloured text. if ansi.enable_control_chars(stream=1) and not status.show_gui: sys.stdout.write(ansi.script) # Print the script. sys.stdout.write("script = " + repr(script_file) + "\n") sys.stdout.write( "----------------------------------------------------------------------------------------------------\n" ) sys.stdout.write(file.read()) sys.stdout.write( "\n----------------------------------------------------------------------------------------------------" ) # End coloured text. if ansi.enable_control_chars(stream=1) and not status.show_gui: sys.stdout.write(ansi.end) # Terminating newline. sys.stdout.write("\n") # Close the script file handle. file.close() # The execution flag. exec_pass = True # Execute the script. try: exec_script(script_file, local) # Catch ctrl-C. except KeyboardInterrupt: # Throw the error. if status.debug: raise # Be nicer to the user. else: sys.stderr.write("\nScript execution cancelled.\n") # The script failed. exec_pass = False # Catch the RelaxErrors. except AllRelaxErrors: instance = sys.exc_info()[1] # Throw the error. if raise_relax_error: raise # Nice output for the user. else: # Print the scary traceback normally hidden from the user. if status.debug or status.traceback: self.showtraceback() # Print the RelaxError message line. else: sys.stderr.write(instance.__str__()) # The script failed. exec_pass = False # Throw all other errors. except: # Raise the error. raise # Add an empty line to make exiting relax look better. if show_script: sys.stdout.write("\n") # Return the execution flag. return exec_pass