##// END OF EJS Templates
Add forgotten file.
Gael Varoquaux -
Show More
@@ -0,0 +1,143 b''
1 """
2 Frontend class that uses IPython0 to prefilter the inputs.
3
4 Using the IPython0 mechanism gives us access to the magics.
5 """
6 __docformat__ = "restructuredtext en"
7
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
14
15 #-------------------------------------------------------------------------------
16 # Imports
17 #-------------------------------------------------------------------------------
18 import sys
19
20 from linefrontendbase import LineFrontEndBase, common_prefix
21
22 from IPython.ipmaker import make_IPython
23 from IPython.ipapi import IPApi
24 from IPython.kernel.core.sync_output_trap import SyncOutputTrap
25
26 from IPython.genutils import Term
27
28 #-------------------------------------------------------------------------------
29 # Utility functions (temporary, should be moved out of here)
30 #-------------------------------------------------------------------------------
31 import os
32 def xterm_system(command):
33 """ Run a command in a separate console window.
34 """
35 os.system("""
36 xterm -title "%s" -e \'/bin/sh -c "%s ;
37 printf \\"\\\\n\\";
38 printf \\"press a key to close\\" ;
39 printf \\"\x1b]0;%s (finished -- press a key to close)\x07\\" ;
40 read foo;"\'
41 """ % (command, command, command) )
42
43 #-------------------------------------------------------------------------------
44 # Frontend class using ipython0 to do the prefiltering.
45 #-------------------------------------------------------------------------------
46 class PrefilterFrontEnd(LineFrontEndBase):
47
48 def __init__(self, *args, **kwargs):
49 LineFrontEndBase.__init__(self, *args, **kwargs)
50 # Instanciate an IPython0 interpreter to be able to use the
51 # prefiltering.
52 self.ipython0 = make_IPython()
53 # Set the pager:
54 self.ipython0.set_hook('show_in_pager',
55 lambda s, string: self.write("\n"+string))
56 self.ipython0.write = self.write
57 self._ip = _ip = IPApi(self.ipython0)
58 # XXX: Hack: mix the two namespaces
59 self.shell.user_ns = self.ipython0.user_ns
60 self.shell.user_global_ns = self.ipython0.user_global_ns
61 # Make sure the raw system call doesn't get called, as we don't
62 # have a stdin accessible.
63 self._ip.system = xterm_system
64 # Redefine a serie of magics to avoid os.system:
65 # FIXME: I am redefining way too much magics.
66 for alias_name, (_, alias_value) in \
67 _ip.IP.shell.alias_table.iteritems():
68 magic = lambda s : _ip.magic('sx %s %s' % (alias_value, s))
69 setattr(_ip.IP, 'magic_%s' % alias_name, magic)
70 # FIXME: I should create a real file-like object dedicated to this
71 # terminal
72 Term.cout.flush = lambda : None
73 Term.cout.getvalue = lambda : ''
74 Term.cerr.flush = lambda : None
75 Term.cerr.getvalue = lambda : ''
76 self.shell.output_trap = SyncOutputTrap(write_out=self.write,
77 write_err=self.write)
78
79
80
81 def prefilter_input(self, input_string):
82 """ Using IPython0 to prefilter the commands.
83 """
84 input_string = LineFrontEndBase.prefilter_input(self, input_string)
85 filtered_lines = []
86 # The IPython0 prefilters sometime produce output. We need to
87 # capture it.
88 self.capture_output()
89 self.last_result = dict(number=self.prompt_number)
90 try:
91 for line in input_string.split('\n'):
92 filtered_lines.append(self.ipython0.prefilter(line, False))
93 except:
94 # XXX: probably not the right thing to do.
95 self.ipython0.showsyntaxerror()
96 self.after_execute()
97 finally:
98 self.release_output()
99
100 filtered_string = '\n'.join(filtered_lines)
101 return filtered_string
102
103
104 def show_traceback(self):
105 self.capture_output()
106 self.ipython0.showtraceback()
107 self.release_output()
108
109
110 def capture_output(self):
111 """ Capture all the output mechanism we can think of.
112 """
113 self.__old_cout_write = Term.cout.write
114 self.__old_err_write = Term.cerr.write
115 Term.cout.write = self.write
116 Term.cerr.write = self.write
117 self.__old_stdout = sys.stdout
118 self.__old_stderr= sys.stderr
119 sys.stdout = Term.cout
120 sys.stderr = Term.cerr
121 # FIXME: I still need to provide the writelines method
122
123 def release_output(self):
124 """ Release all the different captures we have made,
125 and flush the buffers.
126 """
127 Term.cout.write = self.__old_cout_write
128 Term.cerr.write = self.__old_err_write
129 sys.stdout = self.__old_stdout
130 sys.stderr = self.__old_stderr
131
132
133 def complete(self, line):
134 word = line.split('\n')[-1].split(' ')[-1]
135 completions = self.ipython0.complete(word)
136 key = lambda x: x.replace('_', '')
137 completions.sort(key=key)
138 if completions:
139 prefix = common_prefix(completions)
140 line = line[:-len(word)] + prefix
141 return line, completions
142
143
General Comments 0
You need to be logged in to leave comments. Login now