From 2971b6fc77d10394d024575aaeee26ce37699896 2008-07-26 00:59:36 From: Gael Varoquaux Date: 2008-07-26 00:59:36 Subject: [PATCH] Modified OutputTrap to accept a file-like object and use it instead of SyncOutputTrap. --- diff --git a/IPython/frontend/prefilterfrontend.py b/IPython/frontend/prefilterfrontend.py index feda2c9..45532d4 100644 --- a/IPython/frontend/prefilterfrontend.py +++ b/IPython/frontend/prefilterfrontend.py @@ -21,7 +21,8 @@ from linefrontendbase import LineFrontEndBase, common_prefix from IPython.ipmaker import make_IPython from IPython.ipapi import IPApi -from IPython.kernel.core.sync_output_trap import SyncOutputTrap +from IPython.kernel.core.file_like import FileLike +from IPython.kernel.core.output_trap import OutputTrap from IPython.genutils import Term import pydoc @@ -33,13 +34,10 @@ import os def xterm_system(command): """ Run a command in a separate console window. """ - os.system(""" - xterm -title "%s" -e \'/bin/sh -c "%s ; - printf \\"\\\\n\\"; - printf \\"press a key to close\\" ; - printf \\"\x1b]0;%s (finished -- press a key to close)\x07\\" ; - read foo;"\' - """ % (command, command, command) ) + os.system(("""xterm -title "%s" -e \'/bin/sh -c "%s ; """ + """echo; echo press enter to close ; """ +# """echo \\"\x1b]0;%s (finished -- press enter to close)\x07\\" ; + """read foo;"\' """) % (command, command) ) #------------------------------------------------------------------------------- # Frontend class using ipython0 to do the prefiltering. @@ -70,8 +68,10 @@ class PrefilterFrontEnd(LineFrontEndBase): setattr(_ip.IP, 'magic_%s' % alias_name, magic) # FIXME: I should create a real file-like object dedicated to this # terminal - self.shell.output_trap = SyncOutputTrap(write_out=self.write, - write_err=self.write) + self.shell.output_trap = OutputTrap( + out=FileLike(write_callback=self.write), + err=FileLike(write_callback=self.write), + ) # Capture and release the outputs, to make sure all the # shadow variables are set self.capture_output() @@ -142,6 +142,7 @@ class PrefilterFrontEnd(LineFrontEndBase): def complete(self, line): word = line.split('\n')[-1].split(' ')[-1] completions = self.ipython0.complete(word) + # FIXME: The proper sort should be done in the complete method. key = lambda x: x.replace('_', '') completions.sort(key=key) if completions: diff --git a/IPython/kernel/core/sync_output_trap.py b/IPython/kernel/core/file_like.py similarity index 66% rename from IPython/kernel/core/sync_output_trap.py rename to IPython/kernel/core/file_like.py index 31b204a..a09f47a 100644 --- a/IPython/kernel/core/sync_output_trap.py +++ b/IPython/kernel/core/file_like.py @@ -1,6 +1,6 @@ # encoding: utf-8 -""" Redirects stdout/stderr to given write methods.""" +""" File like object that redirects its write calls to a given callback.""" __docformat__ = "restructuredtext en" @@ -11,12 +11,7 @@ __docformat__ = "restructuredtext en" # the file COPYING, distributed as part of this software. #------------------------------------------------------------------------------- -#------------------------------------------------------------------------------- -# Imports -#------------------------------------------------------------------------------- - import sys -from IPython.kernel.core.output_trap import OutputTrap class FileLike(object): """ FileLike object that redirects all write to a callback. @@ -26,8 +21,8 @@ class FileLike(object): """ closed = False - def __init__(self, write): - self.write = write + def __init__(self, write_callback): + self.write = write_callback def flush(self): pass @@ -45,23 +40,10 @@ class FileLike(object): def getvalue(self): return '' + def reset(self): + pass -class SyncOutputTrap(OutputTrap): - """ Object which redirect text sent to stdout and stderr to write - callbacks. - """ - - def __init__(self, write_out, write_err): - # Store callbacks - self.out = FileLike(write_out) - self.err = FileLike(write_err) - - # Boolean to check if the stdout/stderr hook is set. - self.out_set = False - self.err_set = False - - def clear(self): - """ Clear out the buffers. - """ + def truncate(self): pass + diff --git a/IPython/kernel/core/output_trap.py b/IPython/kernel/core/output_trap.py index 8e65662..c16571f 100644 --- a/IPython/kernel/core/output_trap.py +++ b/IPython/kernel/core/output_trap.py @@ -23,10 +23,16 @@ class OutputTrap(object): """ Object which can trap text sent to stdout and stderr. """ - def __init__(self): + def __init__(self, out=None, err=None): # Filelike objects to store stdout/stderr text. - self.out = StringIO() - self.err = StringIO() + if out is None: + self.out = StringIO() + else: + self.out = out + if err is None: + self.err = StringIO() + else: + self.err = err # Boolean to check if the stdout/stderr hook is set. self.out_set = False @@ -72,11 +78,11 @@ class OutputTrap(object): """ Clear out the buffers. """ - self.out.close() - self.out = StringIO() + self.out.reset() + self.out.truncate() - self.err.close() - self.err = StringIO() + self.err.reset() + self.err.truncate() def add_to_message(self, message): """ Add the text from stdout and stderr to the message from the