##// END OF EJS Templates
Modified OutputTrap to accept a file-like object and use it instead of...
Gael Varoquaux -
Show More
@@ -21,7 +21,8 b' from linefrontendbase import LineFrontEndBase, common_prefix'
21
21
22 from IPython.ipmaker import make_IPython
22 from IPython.ipmaker import make_IPython
23 from IPython.ipapi import IPApi
23 from IPython.ipapi import IPApi
24 from IPython.kernel.core.sync_output_trap import SyncOutputTrap
24 from IPython.kernel.core.file_like import FileLike
25 from IPython.kernel.core.output_trap import OutputTrap
25
26
26 from IPython.genutils import Term
27 from IPython.genutils import Term
27 import pydoc
28 import pydoc
@@ -33,13 +34,10 b' import os'
33 def xterm_system(command):
34 def xterm_system(command):
34 """ Run a command in a separate console window.
35 """ Run a command in a separate console window.
35 """
36 """
36 os.system("""
37 os.system(("""xterm -title "%s" -e \'/bin/sh -c "%s ; """
37 xterm -title "%s" -e \'/bin/sh -c "%s ;
38 """echo; echo press enter to close ; """
38 printf \\"\\\\n\\";
39 # """echo \\"\x1b]0;%s (finished -- press enter to close)\x07\\" ;
39 printf \\"press a key to close\\" ;
40 """read foo;"\' """) % (command, command) )
40 printf \\"\x1b]0;%s (finished -- press a key to close)\x07\\" ;
41 read foo;"\'
42 """ % (command, command, command) )
43
41
44 #-------------------------------------------------------------------------------
42 #-------------------------------------------------------------------------------
45 # Frontend class using ipython0 to do the prefiltering.
43 # Frontend class using ipython0 to do the prefiltering.
@@ -70,8 +68,10 b' class PrefilterFrontEnd(LineFrontEndBase):'
70 setattr(_ip.IP, 'magic_%s' % alias_name, magic)
68 setattr(_ip.IP, 'magic_%s' % alias_name, magic)
71 # FIXME: I should create a real file-like object dedicated to this
69 # FIXME: I should create a real file-like object dedicated to this
72 # terminal
70 # terminal
73 self.shell.output_trap = SyncOutputTrap(write_out=self.write,
71 self.shell.output_trap = OutputTrap(
74 write_err=self.write)
72 out=FileLike(write_callback=self.write),
73 err=FileLike(write_callback=self.write),
74 )
75 # Capture and release the outputs, to make sure all the
75 # Capture and release the outputs, to make sure all the
76 # shadow variables are set
76 # shadow variables are set
77 self.capture_output()
77 self.capture_output()
@@ -142,6 +142,7 b' class PrefilterFrontEnd(LineFrontEndBase):'
142 def complete(self, line):
142 def complete(self, line):
143 word = line.split('\n')[-1].split(' ')[-1]
143 word = line.split('\n')[-1].split(' ')[-1]
144 completions = self.ipython0.complete(word)
144 completions = self.ipython0.complete(word)
145 # FIXME: The proper sort should be done in the complete method.
145 key = lambda x: x.replace('_', '')
146 key = lambda x: x.replace('_', '')
146 completions.sort(key=key)
147 completions.sort(key=key)
147 if completions:
148 if completions:
@@ -1,6 +1,6 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
2
3 """ Redirects stdout/stderr to given write methods."""
3 """ File like object that redirects its write calls to a given callback."""
4
4
5 __docformat__ = "restructuredtext en"
5 __docformat__ = "restructuredtext en"
6
6
@@ -11,12 +11,7 b' __docformat__ = "restructuredtext en"'
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-------------------------------------------------------------------------------
12 #-------------------------------------------------------------------------------
13
13
14 #-------------------------------------------------------------------------------
15 # Imports
16 #-------------------------------------------------------------------------------
17
18 import sys
14 import sys
19 from IPython.kernel.core.output_trap import OutputTrap
20
15
21 class FileLike(object):
16 class FileLike(object):
22 """ FileLike object that redirects all write to a callback.
17 """ FileLike object that redirects all write to a callback.
@@ -26,8 +21,8 b' class FileLike(object):'
26 """
21 """
27 closed = False
22 closed = False
28
23
29 def __init__(self, write):
24 def __init__(self, write_callback):
30 self.write = write
25 self.write = write_callback
31
26
32 def flush(self):
27 def flush(self):
33 pass
28 pass
@@ -45,23 +40,10 b' class FileLike(object):'
45 def getvalue(self):
40 def getvalue(self):
46 return ''
41 return ''
47
42
43 def reset(self):
44 pass
48
45
49 class SyncOutputTrap(OutputTrap):
46 def truncate(self):
50 """ Object which redirect text sent to stdout and stderr to write
51 callbacks.
52 """
53
54 def __init__(self, write_out, write_err):
55 # Store callbacks
56 self.out = FileLike(write_out)
57 self.err = FileLike(write_err)
58
59 # Boolean to check if the stdout/stderr hook is set.
60 self.out_set = False
61 self.err_set = False
62
63 def clear(self):
64 """ Clear out the buffers.
65 """
66 pass
47 pass
67
48
49
@@ -23,10 +23,16 b' class OutputTrap(object):'
23 """ Object which can trap text sent to stdout and stderr.
23 """ Object which can trap text sent to stdout and stderr.
24 """
24 """
25
25
26 def __init__(self):
26 def __init__(self, out=None, err=None):
27 # Filelike objects to store stdout/stderr text.
27 # Filelike objects to store stdout/stderr text.
28 self.out = StringIO()
28 if out is None:
29 self.err = StringIO()
29 self.out = StringIO()
30 else:
31 self.out = out
32 if err is None:
33 self.err = StringIO()
34 else:
35 self.err = err
30
36
31 # Boolean to check if the stdout/stderr hook is set.
37 # Boolean to check if the stdout/stderr hook is set.
32 self.out_set = False
38 self.out_set = False
@@ -72,11 +78,11 b' class OutputTrap(object):'
72 """ Clear out the buffers.
78 """ Clear out the buffers.
73 """
79 """
74
80
75 self.out.close()
81 self.out.reset()
76 self.out = StringIO()
82 self.out.truncate()
77
83
78 self.err.close()
84 self.err.reset()
79 self.err = StringIO()
85 self.err.truncate()
80
86
81 def add_to_message(self, message):
87 def add_to_message(self, message):
82 """ Add the text from stdout and stderr to the message from the
88 """ Add the text from stdout and stderr to the message from the
General Comments 0
You need to be logged in to leave comments. Login now