Show More
@@ -1,35 +1,46 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ IPython extension: add %clear magic """ |
|
2 | """ IPython extension: add %clear magic """ | |
3 |
|
3 | |||
4 | import IPython.ipapi |
|
4 | import IPython.ipapi | |
5 | ip = IPython.ipapi.get() |
|
5 | import gc | |
6 |
|
6 | ip = IPython.ipapi.get() | ||
7 |
|
7 | |||
8 | def clear_f(self,arg): |
|
8 | ||
9 | """ Clear various data (e.g. stored history data) |
|
9 | def clear_f(self,arg): | |
10 |
|
10 | """ Clear various data (e.g. stored history data) | ||
11 | %clear out - clear output history |
|
11 | ||
12 |
%clear |
|
12 | %clear out - clear output history | |
13 | """ |
|
13 | %clear in - clear input history | |
14 |
|
|
14 | """ | |
15 | api = self.getapi() |
|
15 | ||
16 | for target in arg.split(): |
|
16 | api = self.getapi() | |
17 | if target == 'out': |
|
17 | for target in arg.split(): | |
18 | print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh']) |
|
18 | if target == 'out': | |
19 | self.outputcache.flush() |
|
19 | print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh']) | |
20 | elif target == 'in': |
|
20 | self.outputcache.flush() | |
21 | print "Flushing input history" |
|
21 | elif target == 'in': | |
22 | from IPython import iplib |
|
22 | print "Flushing input history" | |
23 | del self.input_hist[:] |
|
23 | from IPython import iplib | |
24 |
del self.input_hist |
|
24 | del self.input_hist[:] | |
25 | for n in range(1,self.outputcache.prompt_count + 1): |
|
25 | del self.input_hist_raw[:] | |
26 | key = '_i'+`n` |
|
26 | for n in range(1,self.outputcache.prompt_count + 1): | |
27 |
|
|
27 | key = '_i'+`n` | |
28 | del self.user_ns[key] |
|
28 | try: | |
29 | except: pass |
|
29 | del self.user_ns[key] | |
30 |
|
30 | except: pass | ||
31 | ip.expose_magic("clear",clear_f) |
|
31 | elif target == 'array': | |
32 |
|
32 | try: | ||
33 |
|
33 | pylab=ip.IP.pylab | ||
34 |
|
34 | for x in self.user_ns.keys(): | ||
35 |
|
35 | if isinstance(self.user_ns[x],pylab.arraytype): | ||
|
36 | del self.user_ns[x] | |||
|
37 | except AttributeError: | |||
|
38 | print "Clear array only available in -pylab mode" | |||
|
39 | gc.collect() | |||
|
40 | ||||
|
41 | ||||
|
42 | ip.expose_magic("clear",clear_f) | |||
|
43 | ||||
|
44 | ||||
|
45 | ||||
|
46 |
@@ -1,588 +1,586 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Classes for handling input/output prompts. |
|
3 | Classes for handling input/output prompts. | |
4 |
|
4 | |||
5 |
$Id: Prompts.py 126 |
|
5 | $Id: Prompts.py 1264 2006-04-11 19:24:29Z vivainio $""" | |
6 |
|
6 | |||
7 | #***************************************************************************** |
|
7 | #***************************************************************************** | |
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
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 | from IPython import Release |
|
14 | from IPython import Release | |
15 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | __author__ = '%s <%s>' % Release.authors['Fernando'] | |
16 | __license__ = Release.license |
|
16 | __license__ = Release.license | |
17 | __version__ = Release.version |
|
17 | __version__ = Release.version | |
18 |
|
18 | |||
19 | #**************************************************************************** |
|
19 | #**************************************************************************** | |
20 | # Required modules |
|
20 | # Required modules | |
21 | import __builtin__ |
|
21 | import __builtin__ | |
22 | import os |
|
22 | import os | |
23 | import socket |
|
23 | import socket | |
24 | import sys |
|
24 | import sys | |
25 | import time |
|
25 | import time | |
26 | from pprint import pprint,pformat |
|
26 | from pprint import pprint,pformat | |
27 |
|
27 | |||
28 | # IPython's own |
|
28 | # IPython's own | |
29 | from IPython import ColorANSI |
|
29 | from IPython import ColorANSI | |
30 | from IPython.Itpl import ItplNS |
|
30 | from IPython.Itpl import ItplNS | |
31 | from IPython.ipstruct import Struct |
|
31 | from IPython.ipstruct import Struct | |
32 | from IPython.macro import Macro |
|
32 | from IPython.macro import Macro | |
33 | from IPython.genutils import * |
|
33 | from IPython.genutils import * | |
34 |
|
34 | |||
35 | #**************************************************************************** |
|
35 | #**************************************************************************** | |
36 | #Color schemes for Prompts. |
|
36 | #Color schemes for Prompts. | |
37 |
|
37 | |||
38 | PromptColors = ColorANSI.ColorSchemeTable() |
|
38 | PromptColors = ColorANSI.ColorSchemeTable() | |
39 | InputColors = ColorANSI.InputTermColors # just a shorthand |
|
39 | InputColors = ColorANSI.InputTermColors # just a shorthand | |
40 | Colors = ColorANSI.TermColors # just a shorthand |
|
40 | Colors = ColorANSI.TermColors # just a shorthand | |
41 |
|
41 | |||
42 | PromptColors.add_scheme(ColorANSI.ColorScheme( |
|
42 | PromptColors.add_scheme(ColorANSI.ColorScheme( | |
43 | 'NoColor', |
|
43 | 'NoColor', | |
44 | in_prompt = InputColors.NoColor, # Input prompt |
|
44 | in_prompt = InputColors.NoColor, # Input prompt | |
45 | in_number = InputColors.NoColor, # Input prompt number |
|
45 | in_number = InputColors.NoColor, # Input prompt number | |
46 | in_prompt2 = InputColors.NoColor, # Continuation prompt |
|
46 | in_prompt2 = InputColors.NoColor, # Continuation prompt | |
47 | in_normal = InputColors.NoColor, # color off (usu. Colors.Normal) |
|
47 | in_normal = InputColors.NoColor, # color off (usu. Colors.Normal) | |
48 |
|
48 | |||
49 | out_prompt = Colors.NoColor, # Output prompt |
|
49 | out_prompt = Colors.NoColor, # Output prompt | |
50 | out_number = Colors.NoColor, # Output prompt number |
|
50 | out_number = Colors.NoColor, # Output prompt number | |
51 |
|
51 | |||
52 | normal = Colors.NoColor # color off (usu. Colors.Normal) |
|
52 | normal = Colors.NoColor # color off (usu. Colors.Normal) | |
53 | )) |
|
53 | )) | |
54 |
|
54 | |||
55 | # make some schemes as instances so we can copy them for modification easily: |
|
55 | # make some schemes as instances so we can copy them for modification easily: | |
56 | __PColLinux = ColorANSI.ColorScheme( |
|
56 | __PColLinux = ColorANSI.ColorScheme( | |
57 | 'Linux', |
|
57 | 'Linux', | |
58 | in_prompt = InputColors.Green, |
|
58 | in_prompt = InputColors.Green, | |
59 | in_number = InputColors.LightGreen, |
|
59 | in_number = InputColors.LightGreen, | |
60 | in_prompt2 = InputColors.Green, |
|
60 | in_prompt2 = InputColors.Green, | |
61 | in_normal = InputColors.Normal, # color off (usu. Colors.Normal) |
|
61 | in_normal = InputColors.Normal, # color off (usu. Colors.Normal) | |
62 |
|
62 | |||
63 | out_prompt = Colors.Red, |
|
63 | out_prompt = Colors.Red, | |
64 | out_number = Colors.LightRed, |
|
64 | out_number = Colors.LightRed, | |
65 |
|
65 | |||
66 | normal = Colors.Normal |
|
66 | normal = Colors.Normal | |
67 | ) |
|
67 | ) | |
68 | # Don't forget to enter it into the table! |
|
68 | # Don't forget to enter it into the table! | |
69 | PromptColors.add_scheme(__PColLinux) |
|
69 | PromptColors.add_scheme(__PColLinux) | |
70 |
|
70 | |||
71 | # Slightly modified Linux for light backgrounds |
|
71 | # Slightly modified Linux for light backgrounds | |
72 | __PColLightBG = __PColLinux.copy('LightBG') |
|
72 | __PColLightBG = __PColLinux.copy('LightBG') | |
73 |
|
73 | |||
74 | __PColLightBG.colors.update( |
|
74 | __PColLightBG.colors.update( | |
75 | in_prompt = InputColors.Blue, |
|
75 | in_prompt = InputColors.Blue, | |
76 | in_number = InputColors.LightBlue, |
|
76 | in_number = InputColors.LightBlue, | |
77 | in_prompt2 = InputColors.Blue |
|
77 | in_prompt2 = InputColors.Blue | |
78 | ) |
|
78 | ) | |
79 | PromptColors.add_scheme(__PColLightBG) |
|
79 | PromptColors.add_scheme(__PColLightBG) | |
80 |
|
80 | |||
81 | del Colors,InputColors |
|
81 | del Colors,InputColors | |
82 |
|
82 | |||
83 | #----------------------------------------------------------------------------- |
|
83 | #----------------------------------------------------------------------------- | |
84 | def multiple_replace(dict, text): |
|
84 | def multiple_replace(dict, text): | |
85 | """ Replace in 'text' all occurences of any key in the given |
|
85 | """ Replace in 'text' all occurences of any key in the given | |
86 | dictionary by its corresponding value. Returns the new string.""" |
|
86 | dictionary by its corresponding value. Returns the new string.""" | |
87 |
|
87 | |||
88 | # Function by Xavier Defrang, originally found at: |
|
88 | # Function by Xavier Defrang, originally found at: | |
89 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 |
|
89 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 | |
90 |
|
90 | |||
91 | # Create a regular expression from the dictionary keys |
|
91 | # Create a regular expression from the dictionary keys | |
92 | regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) |
|
92 | regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) | |
93 | # For each match, look-up corresponding value in dictionary |
|
93 | # For each match, look-up corresponding value in dictionary | |
94 | return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text) |
|
94 | return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text) | |
95 |
|
95 | |||
96 | #----------------------------------------------------------------------------- |
|
96 | #----------------------------------------------------------------------------- | |
97 | # Special characters that can be used in prompt templates, mainly bash-like |
|
97 | # Special characters that can be used in prompt templates, mainly bash-like | |
98 |
|
98 | |||
99 | # If $HOME isn't defined (Windows), make it an absurd string so that it can |
|
99 | # If $HOME isn't defined (Windows), make it an absurd string so that it can | |
100 | # never be expanded out into '~'. Basically anything which can never be a |
|
100 | # never be expanded out into '~'. Basically anything which can never be a | |
101 | # reasonable directory name will do, we just want the $HOME -> '~' operation |
|
101 | # reasonable directory name will do, we just want the $HOME -> '~' operation | |
102 | # to become a no-op. We pre-compute $HOME here so it's not done on every |
|
102 | # to become a no-op. We pre-compute $HOME here so it's not done on every | |
103 | # prompt call. |
|
103 | # prompt call. | |
104 |
|
104 | |||
105 | # FIXME: |
|
105 | # FIXME: | |
106 |
|
106 | |||
107 | # - This should be turned into a class which does proper namespace management, |
|
107 | # - This should be turned into a class which does proper namespace management, | |
108 | # since the prompt specials need to be evaluated in a certain namespace. |
|
108 | # since the prompt specials need to be evaluated in a certain namespace. | |
109 | # Currently it's just globals, which need to be managed manually by code |
|
109 | # Currently it's just globals, which need to be managed manually by code | |
110 | # below. |
|
110 | # below. | |
111 |
|
111 | |||
112 | # - I also need to split up the color schemes from the prompt specials |
|
112 | # - I also need to split up the color schemes from the prompt specials | |
113 | # somehow. I don't have a clean design for that quite yet. |
|
113 | # somehow. I don't have a clean design for that quite yet. | |
114 |
|
114 | |||
115 | HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~") |
|
115 | HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~") | |
116 |
|
116 | |||
117 | # We precompute a few more strings here for the prompt_specials, which are |
|
117 | # We precompute a few more strings here for the prompt_specials, which are | |
118 | # fixed once ipython starts. This reduces the runtime overhead of computing |
|
118 | # fixed once ipython starts. This reduces the runtime overhead of computing | |
119 | # prompt strings. |
|
119 | # prompt strings. | |
120 | USER = os.environ.get("USER") |
|
120 | USER = os.environ.get("USER") | |
121 | HOSTNAME = socket.gethostname() |
|
121 | HOSTNAME = socket.gethostname() | |
122 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] |
|
122 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] | |
123 | ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0] |
|
123 | ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0] | |
124 |
|
124 | |||
125 | prompt_specials_color = { |
|
125 | prompt_specials_color = { | |
126 | # Prompt/history count |
|
126 | # Prompt/history count | |
127 | '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
127 | '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', | |
128 | '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
128 | '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', | |
129 | # Prompt/history count, with the actual digits replaced by dots. Used |
|
129 | # Prompt/history count, with the actual digits replaced by dots. Used | |
130 | # mainly in continuation prompts (prompt_in2) |
|
130 | # mainly in continuation prompts (prompt_in2) | |
131 | '\\D': '${"."*len(str(self.cache.prompt_count))}', |
|
131 | '\\D': '${"."*len(str(self.cache.prompt_count))}', | |
132 | # Current working directory |
|
132 | # Current working directory | |
133 | '\\w': '${os.getcwd()}', |
|
133 | '\\w': '${os.getcwd()}', | |
134 | # Current time |
|
134 | # Current time | |
135 | '\\t' : '${time.strftime("%H:%M:%S")}', |
|
135 | '\\t' : '${time.strftime("%H:%M:%S")}', | |
136 | # Basename of current working directory. |
|
136 | # Basename of current working directory. | |
137 | # (use os.sep to make this portable across OSes) |
|
137 | # (use os.sep to make this portable across OSes) | |
138 | '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep, |
|
138 | '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep, | |
139 | # These X<N> are an extension to the normal bash prompts. They return |
|
139 | # These X<N> are an extension to the normal bash prompts. They return | |
140 | # N terms of the path, after replacing $HOME with '~' |
|
140 | # N terms of the path, after replacing $HOME with '~' | |
141 | '\\X0': '${os.getcwd().replace("%s","~")}' % HOME, |
|
141 | '\\X0': '${os.getcwd().replace("%s","~")}' % HOME, | |
142 | '\\X1': '${self.cwd_filt(1)}', |
|
142 | '\\X1': '${self.cwd_filt(1)}', | |
143 | '\\X2': '${self.cwd_filt(2)}', |
|
143 | '\\X2': '${self.cwd_filt(2)}', | |
144 | '\\X3': '${self.cwd_filt(3)}', |
|
144 | '\\X3': '${self.cwd_filt(3)}', | |
145 | '\\X4': '${self.cwd_filt(4)}', |
|
145 | '\\X4': '${self.cwd_filt(4)}', | |
146 | '\\X5': '${self.cwd_filt(5)}', |
|
146 | '\\X5': '${self.cwd_filt(5)}', | |
147 | # Y<N> are similar to X<N>, but they show '~' if it's the directory |
|
147 | # Y<N> are similar to X<N>, but they show '~' if it's the directory | |
148 | # N+1 in the list. Somewhat like %cN in tcsh. |
|
148 | # N+1 in the list. Somewhat like %cN in tcsh. | |
149 | '\\Y0': '${self.cwd_filt2(0)}', |
|
149 | '\\Y0': '${self.cwd_filt2(0)}', | |
150 | '\\Y1': '${self.cwd_filt2(1)}', |
|
150 | '\\Y1': '${self.cwd_filt2(1)}', | |
151 | '\\Y2': '${self.cwd_filt2(2)}', |
|
151 | '\\Y2': '${self.cwd_filt2(2)}', | |
152 | '\\Y3': '${self.cwd_filt2(3)}', |
|
152 | '\\Y3': '${self.cwd_filt2(3)}', | |
153 | '\\Y4': '${self.cwd_filt2(4)}', |
|
153 | '\\Y4': '${self.cwd_filt2(4)}', | |
154 | '\\Y5': '${self.cwd_filt2(5)}', |
|
154 | '\\Y5': '${self.cwd_filt2(5)}', | |
155 | # Hostname up to first . |
|
155 | # Hostname up to first . | |
156 | '\\h': HOSTNAME_SHORT, |
|
156 | '\\h': HOSTNAME_SHORT, | |
157 | # Full hostname |
|
157 | # Full hostname | |
158 | '\\H': HOSTNAME, |
|
158 | '\\H': HOSTNAME, | |
159 | # Username of current user |
|
159 | # Username of current user | |
160 | '\\u': USER, |
|
160 | '\\u': USER, | |
161 | # Escaped '\' |
|
161 | # Escaped '\' | |
162 | '\\\\': '\\', |
|
162 | '\\\\': '\\', | |
163 | # Newline |
|
163 | # Newline | |
164 | '\\n': '\n', |
|
164 | '\\n': '\n', | |
165 | # Carriage return |
|
165 | # Carriage return | |
166 | '\\r': '\r', |
|
166 | '\\r': '\r', | |
167 | # Release version |
|
167 | # Release version | |
168 | '\\v': __version__, |
|
168 | '\\v': __version__, | |
169 | # Root symbol ($ or #) |
|
169 | # Root symbol ($ or #) | |
170 | '\\$': ROOT_SYMBOL, |
|
170 | '\\$': ROOT_SYMBOL, | |
171 | } |
|
171 | } | |
172 |
|
172 | |||
173 | # A copy of the prompt_specials dictionary but with all color escapes removed, |
|
173 | # A copy of the prompt_specials dictionary but with all color escapes removed, | |
174 | # so we can correctly compute the prompt length for the auto_rewrite method. |
|
174 | # so we can correctly compute the prompt length for the auto_rewrite method. | |
175 | prompt_specials_nocolor = prompt_specials_color.copy() |
|
175 | prompt_specials_nocolor = prompt_specials_color.copy() | |
176 | prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}' |
|
176 | prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}' | |
177 | prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}' |
|
177 | prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}' | |
178 |
|
178 | |||
179 | # Add in all the InputTermColors color escapes as valid prompt characters. |
|
179 | # Add in all the InputTermColors color escapes as valid prompt characters. | |
180 | # They all get added as \\C_COLORNAME, so that we don't have any conflicts |
|
180 | # They all get added as \\C_COLORNAME, so that we don't have any conflicts | |
181 | # with a color name which may begin with a letter used by any other of the |
|
181 | # with a color name which may begin with a letter used by any other of the | |
182 | # allowed specials. This of course means that \\C will never be allowed for |
|
182 | # allowed specials. This of course means that \\C will never be allowed for | |
183 | # anything else. |
|
183 | # anything else. | |
184 | input_colors = ColorANSI.InputTermColors |
|
184 | input_colors = ColorANSI.InputTermColors | |
185 | for _color in dir(input_colors): |
|
185 | for _color in dir(input_colors): | |
186 | if _color[0] != '_': |
|
186 | if _color[0] != '_': | |
187 | c_name = '\\C_'+_color |
|
187 | c_name = '\\C_'+_color | |
188 | prompt_specials_color[c_name] = getattr(input_colors,_color) |
|
188 | prompt_specials_color[c_name] = getattr(input_colors,_color) | |
189 | prompt_specials_nocolor[c_name] = '' |
|
189 | prompt_specials_nocolor[c_name] = '' | |
190 |
|
190 | |||
191 | # we default to no color for safety. Note that prompt_specials is a global |
|
191 | # we default to no color for safety. Note that prompt_specials is a global | |
192 | # variable used by all prompt objects. |
|
192 | # variable used by all prompt objects. | |
193 | prompt_specials = prompt_specials_nocolor |
|
193 | prompt_specials = prompt_specials_nocolor | |
194 |
|
194 | |||
195 | #----------------------------------------------------------------------------- |
|
195 | #----------------------------------------------------------------------------- | |
196 | def str_safe(arg): |
|
196 | def str_safe(arg): | |
197 | """Convert to a string, without ever raising an exception. |
|
197 | """Convert to a string, without ever raising an exception. | |
198 |
|
198 | |||
199 | If str(arg) fails, <ERROR: ... > is returned, where ... is the exception |
|
199 | If str(arg) fails, <ERROR: ... > is returned, where ... is the exception | |
200 | error message.""" |
|
200 | error message.""" | |
201 |
|
201 | |||
202 | try: |
|
202 | try: | |
203 | out = str(arg) |
|
203 | out = str(arg) | |
204 | except UnicodeError: |
|
204 | except UnicodeError: | |
205 | try: |
|
205 | try: | |
206 | out = arg.encode('utf_8','replace') |
|
206 | out = arg.encode('utf_8','replace') | |
207 | except Exception,msg: |
|
207 | except Exception,msg: | |
208 | # let's keep this little duplication here, so that the most common |
|
208 | # let's keep this little duplication here, so that the most common | |
209 | # case doesn't suffer from a double try wrapping. |
|
209 | # case doesn't suffer from a double try wrapping. | |
210 | out = '<ERROR: %s>' % msg |
|
210 | out = '<ERROR: %s>' % msg | |
211 | except Exception,msg: |
|
211 | except Exception,msg: | |
212 | out = '<ERROR: %s>' % msg |
|
212 | out = '<ERROR: %s>' % msg | |
213 | return out |
|
213 | return out | |
214 |
|
214 | |||
215 | class BasePrompt: |
|
215 | class BasePrompt: | |
216 | """Interactive prompt similar to Mathematica's.""" |
|
216 | """Interactive prompt similar to Mathematica's.""" | |
217 | def __init__(self,cache,sep,prompt,pad_left=False): |
|
217 | def __init__(self,cache,sep,prompt,pad_left=False): | |
218 |
|
218 | |||
219 | # Hack: we access information about the primary prompt through the |
|
219 | # Hack: we access information about the primary prompt through the | |
220 | # cache argument. We need this, because we want the secondary prompt |
|
220 | # cache argument. We need this, because we want the secondary prompt | |
221 | # to be aligned with the primary one. Color table info is also shared |
|
221 | # to be aligned with the primary one. Color table info is also shared | |
222 | # by all prompt classes through the cache. Nice OO spaghetti code! |
|
222 | # by all prompt classes through the cache. Nice OO spaghetti code! | |
223 | self.cache = cache |
|
223 | self.cache = cache | |
224 | self.sep = sep |
|
224 | self.sep = sep | |
225 |
|
225 | |||
226 | # regexp to count the number of spaces at the end of a prompt |
|
226 | # regexp to count the number of spaces at the end of a prompt | |
227 | # expression, useful for prompt auto-rewriting |
|
227 | # expression, useful for prompt auto-rewriting | |
228 | self.rspace = re.compile(r'(\s*)$') |
|
228 | self.rspace = re.compile(r'(\s*)$') | |
229 | # Flag to left-pad prompt strings to match the length of the primary |
|
229 | # Flag to left-pad prompt strings to match the length of the primary | |
230 | # prompt |
|
230 | # prompt | |
231 | self.pad_left = pad_left |
|
231 | self.pad_left = pad_left | |
232 | # Set template to create each actual prompt (where numbers change) |
|
232 | # Set template to create each actual prompt (where numbers change) | |
233 | self.p_template = prompt |
|
233 | self.p_template = prompt | |
234 | self.set_p_str() |
|
234 | self.set_p_str() | |
235 |
|
235 | |||
236 | def set_p_str(self): |
|
236 | def set_p_str(self): | |
237 | """ Set the interpolating prompt strings. |
|
237 | """ Set the interpolating prompt strings. | |
238 |
|
238 | |||
239 | This must be called every time the color settings change, because the |
|
239 | This must be called every time the color settings change, because the | |
240 | prompt_specials global may have changed.""" |
|
240 | prompt_specials global may have changed.""" | |
241 |
|
241 | |||
242 | import os,time # needed in locals for prompt string handling |
|
242 | import os,time # needed in locals for prompt string handling | |
243 | loc = locals() |
|
243 | loc = locals() | |
244 | self.p_str = ItplNS('%s%s%s' % |
|
244 | self.p_str = ItplNS('%s%s%s' % | |
245 | ('${self.sep}${self.col_p}', |
|
245 | ('${self.sep}${self.col_p}', | |
246 | multiple_replace(prompt_specials, self.p_template), |
|
246 | multiple_replace(prompt_specials, self.p_template), | |
247 | '${self.col_norm}'),self.cache.user_ns,loc) |
|
247 | '${self.col_norm}'),self.cache.user_ns,loc) | |
248 |
|
248 | |||
249 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
249 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, | |
250 | self.p_template), |
|
250 | self.p_template), | |
251 | self.cache.user_ns,loc) |
|
251 | self.cache.user_ns,loc) | |
252 |
|
252 | |||
253 | def write(self,msg): # dbg |
|
253 | def write(self,msg): # dbg | |
254 | sys.stdout.write(msg) |
|
254 | sys.stdout.write(msg) | |
255 | return '' |
|
255 | return '' | |
256 |
|
256 | |||
257 | def __str__(self): |
|
257 | def __str__(self): | |
258 | """Return a string form of the prompt. |
|
258 | """Return a string form of the prompt. | |
259 |
|
259 | |||
260 | This for is useful for continuation and output prompts, since it is |
|
260 | This for is useful for continuation and output prompts, since it is | |
261 | left-padded to match lengths with the primary one (if the |
|
261 | left-padded to match lengths with the primary one (if the | |
262 | self.pad_left attribute is set).""" |
|
262 | self.pad_left attribute is set).""" | |
263 |
|
263 | |||
264 | out_str = str_safe(self.p_str) |
|
264 | out_str = str_safe(self.p_str) | |
265 | if self.pad_left: |
|
265 | if self.pad_left: | |
266 | # We must find the amount of padding required to match lengths, |
|
266 | # We must find the amount of padding required to match lengths, | |
267 | # taking the color escapes (which are invisible on-screen) into |
|
267 | # taking the color escapes (which are invisible on-screen) into | |
268 | # account. |
|
268 | # account. | |
269 | esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor)) |
|
269 | esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor)) | |
270 | format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad) |
|
270 | format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad) | |
271 | return format % out_str |
|
271 | return format % out_str | |
272 | else: |
|
272 | else: | |
273 | return out_str |
|
273 | return out_str | |
274 |
|
274 | |||
275 | # these path filters are put in as methods so that we can control the |
|
275 | # these path filters are put in as methods so that we can control the | |
276 | # namespace where the prompt strings get evaluated |
|
276 | # namespace where the prompt strings get evaluated | |
277 | def cwd_filt(self,depth): |
|
277 | def cwd_filt(self,depth): | |
278 | """Return the last depth elements of the current working directory. |
|
278 | """Return the last depth elements of the current working directory. | |
279 |
|
279 | |||
280 | $HOME is always replaced with '~'. |
|
280 | $HOME is always replaced with '~'. | |
281 | If depth==0, the full path is returned.""" |
|
281 | If depth==0, the full path is returned.""" | |
282 |
|
282 | |||
283 | cwd = os.getcwd().replace(HOME,"~") |
|
283 | cwd = os.getcwd().replace(HOME,"~") | |
284 | out = os.sep.join(cwd.split(os.sep)[-depth:]) |
|
284 | out = os.sep.join(cwd.split(os.sep)[-depth:]) | |
285 | if out: |
|
285 | if out: | |
286 | return out |
|
286 | return out | |
287 | else: |
|
287 | else: | |
288 | return os.sep |
|
288 | return os.sep | |
289 |
|
289 | |||
290 | def cwd_filt2(self,depth): |
|
290 | def cwd_filt2(self,depth): | |
291 | """Return the last depth elements of the current working directory. |
|
291 | """Return the last depth elements of the current working directory. | |
292 |
|
292 | |||
293 | $HOME is always replaced with '~'. |
|
293 | $HOME is always replaced with '~'. | |
294 | If depth==0, the full path is returned.""" |
|
294 | If depth==0, the full path is returned.""" | |
295 |
|
295 | |||
296 | cwd = os.getcwd().replace(HOME,"~").split(os.sep) |
|
296 | cwd = os.getcwd().replace(HOME,"~").split(os.sep) | |
297 | if '~' in cwd and len(cwd) == depth+1: |
|
297 | if '~' in cwd and len(cwd) == depth+1: | |
298 | depth += 1 |
|
298 | depth += 1 | |
299 | out = os.sep.join(cwd[-depth:]) |
|
299 | out = os.sep.join(cwd[-depth:]) | |
300 | if out: |
|
300 | if out: | |
301 | return out |
|
301 | return out | |
302 | else: |
|
302 | else: | |
303 | return os.sep |
|
303 | return os.sep | |
304 |
|
304 | |||
305 | class Prompt1(BasePrompt): |
|
305 | class Prompt1(BasePrompt): | |
306 | """Input interactive prompt similar to Mathematica's.""" |
|
306 | """Input interactive prompt similar to Mathematica's.""" | |
307 |
|
307 | |||
308 | def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True): |
|
308 | def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True): | |
309 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
309 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) | |
310 |
|
310 | |||
311 | def set_colors(self): |
|
311 | def set_colors(self): | |
312 | self.set_p_str() |
|
312 | self.set_p_str() | |
313 | Colors = self.cache.color_table.active_colors # shorthand |
|
313 | Colors = self.cache.color_table.active_colors # shorthand | |
314 | self.col_p = Colors.in_prompt |
|
314 | self.col_p = Colors.in_prompt | |
315 | self.col_num = Colors.in_number |
|
315 | self.col_num = Colors.in_number | |
316 | self.col_norm = Colors.in_normal |
|
316 | self.col_norm = Colors.in_normal | |
317 | # We need a non-input version of these escapes for the '--->' |
|
317 | # We need a non-input version of these escapes for the '--->' | |
318 | # auto-call prompts used in the auto_rewrite() method. |
|
318 | # auto-call prompts used in the auto_rewrite() method. | |
319 | self.col_p_ni = self.col_p.replace('\001','').replace('\002','') |
|
319 | self.col_p_ni = self.col_p.replace('\001','').replace('\002','') | |
320 | self.col_norm_ni = Colors.normal |
|
320 | self.col_norm_ni = Colors.normal | |
321 |
|
321 | |||
322 | def __str__(self): |
|
322 | def __str__(self): | |
323 | self.cache.prompt_count += 1 |
|
323 | self.cache.prompt_count += 1 | |
324 | self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1] |
|
324 | self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1] | |
325 | return str_safe(self.p_str) |
|
325 | return str_safe(self.p_str) | |
326 |
|
326 | |||
327 | def auto_rewrite(self): |
|
327 | def auto_rewrite(self): | |
328 | """Print a string of the form '--->' which lines up with the previous |
|
328 | """Print a string of the form '--->' which lines up with the previous | |
329 | input string. Useful for systems which re-write the user input when |
|
329 | input string. Useful for systems which re-write the user input when | |
330 | handling automatically special syntaxes.""" |
|
330 | handling automatically special syntaxes.""" | |
331 |
|
331 | |||
332 | curr = str(self.cache.last_prompt) |
|
332 | curr = str(self.cache.last_prompt) | |
333 | nrspaces = len(self.rspace.search(curr).group()) |
|
333 | nrspaces = len(self.rspace.search(curr).group()) | |
334 | return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1), |
|
334 | return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1), | |
335 | ' '*nrspaces,self.col_norm_ni) |
|
335 | ' '*nrspaces,self.col_norm_ni) | |
336 |
|
336 | |||
337 | class PromptOut(BasePrompt): |
|
337 | class PromptOut(BasePrompt): | |
338 | """Output interactive prompt similar to Mathematica's.""" |
|
338 | """Output interactive prompt similar to Mathematica's.""" | |
339 |
|
339 | |||
340 | def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True): |
|
340 | def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True): | |
341 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
341 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) | |
342 | if not self.p_template: |
|
342 | if not self.p_template: | |
343 | self.__str__ = lambda: '' |
|
343 | self.__str__ = lambda: '' | |
344 |
|
344 | |||
345 | def set_colors(self): |
|
345 | def set_colors(self): | |
346 | self.set_p_str() |
|
346 | self.set_p_str() | |
347 | Colors = self.cache.color_table.active_colors # shorthand |
|
347 | Colors = self.cache.color_table.active_colors # shorthand | |
348 | self.col_p = Colors.out_prompt |
|
348 | self.col_p = Colors.out_prompt | |
349 | self.col_num = Colors.out_number |
|
349 | self.col_num = Colors.out_number | |
350 | self.col_norm = Colors.normal |
|
350 | self.col_norm = Colors.normal | |
351 |
|
351 | |||
352 | class Prompt2(BasePrompt): |
|
352 | class Prompt2(BasePrompt): | |
353 | """Interactive continuation prompt.""" |
|
353 | """Interactive continuation prompt.""" | |
354 |
|
354 | |||
355 | def __init__(self,cache,prompt=' .\\D.: ',pad_left=True): |
|
355 | def __init__(self,cache,prompt=' .\\D.: ',pad_left=True): | |
356 | self.cache = cache |
|
356 | self.cache = cache | |
357 | self.p_template = prompt |
|
357 | self.p_template = prompt | |
358 | self.pad_left = pad_left |
|
358 | self.pad_left = pad_left | |
359 | self.set_p_str() |
|
359 | self.set_p_str() | |
360 |
|
360 | |||
361 | def set_p_str(self): |
|
361 | def set_p_str(self): | |
362 | import os,time # needed in locals for prompt string handling |
|
362 | import os,time # needed in locals for prompt string handling | |
363 | loc = locals() |
|
363 | loc = locals() | |
364 | self.p_str = ItplNS('%s%s%s' % |
|
364 | self.p_str = ItplNS('%s%s%s' % | |
365 | ('${self.col_p2}', |
|
365 | ('${self.col_p2}', | |
366 | multiple_replace(prompt_specials, self.p_template), |
|
366 | multiple_replace(prompt_specials, self.p_template), | |
367 | '$self.col_norm'), |
|
367 | '$self.col_norm'), | |
368 | self.cache.user_ns,loc) |
|
368 | self.cache.user_ns,loc) | |
369 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
369 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, | |
370 | self.p_template), |
|
370 | self.p_template), | |
371 | self.cache.user_ns,loc) |
|
371 | self.cache.user_ns,loc) | |
372 |
|
372 | |||
373 | def set_colors(self): |
|
373 | def set_colors(self): | |
374 | self.set_p_str() |
|
374 | self.set_p_str() | |
375 | Colors = self.cache.color_table.active_colors |
|
375 | Colors = self.cache.color_table.active_colors | |
376 | self.col_p2 = Colors.in_prompt2 |
|
376 | self.col_p2 = Colors.in_prompt2 | |
377 | self.col_norm = Colors.in_normal |
|
377 | self.col_norm = Colors.in_normal | |
378 | # FIXME (2004-06-16) HACK: prevent crashes for users who haven't |
|
378 | # FIXME (2004-06-16) HACK: prevent crashes for users who haven't | |
379 | # updated their prompt_in2 definitions. Remove eventually. |
|
379 | # updated their prompt_in2 definitions. Remove eventually. | |
380 | self.col_p = Colors.out_prompt |
|
380 | self.col_p = Colors.out_prompt | |
381 | self.col_num = Colors.out_number |
|
381 | self.col_num = Colors.out_number | |
382 |
|
382 | |||
383 | #----------------------------------------------------------------------------- |
|
383 | #----------------------------------------------------------------------------- | |
384 | class CachedOutput: |
|
384 | class CachedOutput: | |
385 | """Class for printing output from calculations while keeping a cache of |
|
385 | """Class for printing output from calculations while keeping a cache of | |
386 | reults. It dynamically creates global variables prefixed with _ which |
|
386 | reults. It dynamically creates global variables prefixed with _ which | |
387 | contain these results. |
|
387 | contain these results. | |
388 |
|
388 | |||
389 | Meant to be used as a sys.displayhook replacement, providing numbered |
|
389 | Meant to be used as a sys.displayhook replacement, providing numbered | |
390 | prompts and cache services. |
|
390 | prompts and cache services. | |
391 |
|
391 | |||
392 | Initialize with initial and final values for cache counter (this defines |
|
392 | Initialize with initial and final values for cache counter (this defines | |
393 | the maximum size of the cache.""" |
|
393 | the maximum size of the cache.""" | |
394 |
|
394 | |||
395 | def __init__(self,shell,cache_size,Pprint, |
|
395 | def __init__(self,shell,cache_size,Pprint, | |
396 | colors='NoColor',input_sep='\n', |
|
396 | colors='NoColor',input_sep='\n', | |
397 | output_sep='\n',output_sep2='', |
|
397 | output_sep='\n',output_sep2='', | |
398 | ps1 = None, ps2 = None,ps_out = None,pad_left=True): |
|
398 | ps1 = None, ps2 = None,ps_out = None,pad_left=True): | |
399 |
|
399 | |||
400 | cache_size_min = 20 |
|
400 | cache_size_min = 20 | |
401 | if cache_size <= 0: |
|
401 | if cache_size <= 0: | |
402 | self.do_full_cache = 0 |
|
402 | self.do_full_cache = 0 | |
403 | cache_size = 0 |
|
403 | cache_size = 0 | |
404 | elif cache_size < cache_size_min: |
|
404 | elif cache_size < cache_size_min: | |
405 | self.do_full_cache = 0 |
|
405 | self.do_full_cache = 0 | |
406 | cache_size = 0 |
|
406 | cache_size = 0 | |
407 | warn('caching was disabled (min value for cache size is %s).' % |
|
407 | warn('caching was disabled (min value for cache size is %s).' % | |
408 | cache_size_min,level=3) |
|
408 | cache_size_min,level=3) | |
409 | else: |
|
409 | else: | |
410 | self.do_full_cache = 1 |
|
410 | self.do_full_cache = 1 | |
411 |
|
411 | |||
412 | self.cache_size = cache_size |
|
412 | self.cache_size = cache_size | |
413 | self.input_sep = input_sep |
|
413 | self.input_sep = input_sep | |
414 |
|
414 | |||
415 | # we need a reference to the user-level namespace |
|
415 | # we need a reference to the user-level namespace | |
416 | self.shell = shell |
|
416 | self.shell = shell | |
417 | self.user_ns = shell.user_ns |
|
417 | self.user_ns = shell.user_ns | |
418 | # and to the user's input |
|
418 | # and to the user's input | |
419 | self.input_hist = shell.input_hist |
|
419 | self.input_hist = shell.input_hist | |
420 | # and to the user's logger, for logging output |
|
420 | # and to the user's logger, for logging output | |
421 | self.logger = shell.logger |
|
421 | self.logger = shell.logger | |
422 |
|
422 | |||
423 | # Set input prompt strings and colors |
|
423 | # Set input prompt strings and colors | |
424 | if cache_size == 0: |
|
424 | if cache_size == 0: | |
425 | if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> ' |
|
425 | if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> ' | |
426 | if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... ' |
|
426 | if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... ' | |
427 | self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ') |
|
427 | self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ') | |
428 | self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ') |
|
428 | self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ') | |
429 | self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','') |
|
429 | self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','') | |
430 |
|
430 | |||
431 | self.color_table = PromptColors |
|
431 | self.color_table = PromptColors | |
432 | self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str, |
|
432 | self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str, | |
433 | pad_left=pad_left) |
|
433 | pad_left=pad_left) | |
434 | self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left) |
|
434 | self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left) | |
435 | self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str, |
|
435 | self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str, | |
436 | pad_left=pad_left) |
|
436 | pad_left=pad_left) | |
437 | self.set_colors(colors) |
|
437 | self.set_colors(colors) | |
438 |
|
438 | |||
439 | # other more normal stuff |
|
439 | # other more normal stuff | |
440 | # b/c each call to the In[] prompt raises it by 1, even the first. |
|
440 | # b/c each call to the In[] prompt raises it by 1, even the first. | |
441 | self.prompt_count = 0 |
|
441 | self.prompt_count = 0 | |
442 | # Store the last prompt string each time, we need it for aligning |
|
442 | # Store the last prompt string each time, we need it for aligning | |
443 | # continuation and auto-rewrite prompts |
|
443 | # continuation and auto-rewrite prompts | |
444 | self.last_prompt = '' |
|
444 | self.last_prompt = '' | |
445 | self.entries = [None] # output counter starts at 1 for the user |
|
|||
446 | self.Pprint = Pprint |
|
445 | self.Pprint = Pprint | |
447 | self.output_sep = output_sep |
|
446 | self.output_sep = output_sep | |
448 | self.output_sep2 = output_sep2 |
|
447 | self.output_sep2 = output_sep2 | |
449 | self._,self.__,self.___ = '','','' |
|
448 | self._,self.__,self.___ = '','','' | |
450 | self.pprint_types = map(type,[(),[],{}]) |
|
449 | self.pprint_types = map(type,[(),[],{}]) | |
451 |
|
450 | |||
452 | # these are deliberately global: |
|
451 | # these are deliberately global: | |
453 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
452 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} | |
454 | self.user_ns.update(to_user_ns) |
|
453 | self.user_ns.update(to_user_ns) | |
455 |
|
454 | |||
456 | def _set_prompt_str(self,p_str,cache_def,no_cache_def): |
|
455 | def _set_prompt_str(self,p_str,cache_def,no_cache_def): | |
457 | if p_str is None: |
|
456 | if p_str is None: | |
458 | if self.do_full_cache: |
|
457 | if self.do_full_cache: | |
459 | return cache_def |
|
458 | return cache_def | |
460 | else: |
|
459 | else: | |
461 | return no_cache_def |
|
460 | return no_cache_def | |
462 | else: |
|
461 | else: | |
463 | return p_str |
|
462 | return p_str | |
464 |
|
463 | |||
465 | def set_colors(self,colors): |
|
464 | def set_colors(self,colors): | |
466 | """Set the active color scheme and configure colors for the three |
|
465 | """Set the active color scheme and configure colors for the three | |
467 | prompt subsystems.""" |
|
466 | prompt subsystems.""" | |
468 |
|
467 | |||
469 | # FIXME: the prompt_specials global should be gobbled inside this |
|
468 | # FIXME: the prompt_specials global should be gobbled inside this | |
470 | # class instead. Do it when cleaning up the whole 3-prompt system. |
|
469 | # class instead. Do it when cleaning up the whole 3-prompt system. | |
471 | global prompt_specials |
|
470 | global prompt_specials | |
472 | if colors.lower()=='nocolor': |
|
471 | if colors.lower()=='nocolor': | |
473 | prompt_specials = prompt_specials_nocolor |
|
472 | prompt_specials = prompt_specials_nocolor | |
474 | else: |
|
473 | else: | |
475 | prompt_specials = prompt_specials_color |
|
474 | prompt_specials = prompt_specials_color | |
476 |
|
475 | |||
477 | self.color_table.set_active_scheme(colors) |
|
476 | self.color_table.set_active_scheme(colors) | |
478 | self.prompt1.set_colors() |
|
477 | self.prompt1.set_colors() | |
479 | self.prompt2.set_colors() |
|
478 | self.prompt2.set_colors() | |
480 | self.prompt_out.set_colors() |
|
479 | self.prompt_out.set_colors() | |
481 |
|
480 | |||
482 | def __call__(self,arg=None): |
|
481 | def __call__(self,arg=None): | |
483 | """Printing with history cache management. |
|
482 | """Printing with history cache management. | |
484 |
|
483 | |||
485 | This is invoked everytime the interpreter needs to print, and is |
|
484 | This is invoked everytime the interpreter needs to print, and is | |
486 | activated by setting the variable sys.displayhook to it.""" |
|
485 | activated by setting the variable sys.displayhook to it.""" | |
487 |
|
486 | |||
488 | # If something injected a '_' variable in __builtin__, delete |
|
487 | # If something injected a '_' variable in __builtin__, delete | |
489 | # ipython's automatic one so we don't clobber that. gettext() in |
|
488 | # ipython's automatic one so we don't clobber that. gettext() in | |
490 | # particular uses _, so we need to stay away from it. |
|
489 | # particular uses _, so we need to stay away from it. | |
491 | if '_' in __builtin__.__dict__: |
|
490 | if '_' in __builtin__.__dict__: | |
492 | try: |
|
491 | try: | |
493 | del self.user_ns['_'] |
|
492 | del self.user_ns['_'] | |
494 | except KeyError: |
|
493 | except KeyError: | |
495 | pass |
|
494 | pass | |
496 | if arg is not None: |
|
495 | if arg is not None: | |
497 | cout_write = Term.cout.write # fast lookup |
|
496 | cout_write = Term.cout.write # fast lookup | |
498 | # first handle the cache and counters |
|
497 | # first handle the cache and counters | |
499 |
|
498 | |||
500 | # do not print output if input ends in ';' |
|
499 | # do not print output if input ends in ';' | |
501 | if self.input_hist[self.prompt_count].endswith(';\n'): |
|
500 | if self.input_hist[self.prompt_count].endswith(';\n'): | |
502 | return |
|
501 | return | |
503 | # don't use print, puts an extra space |
|
502 | # don't use print, puts an extra space | |
504 | cout_write(self.output_sep) |
|
503 | cout_write(self.output_sep) | |
505 | if self.do_full_cache: |
|
504 | if self.do_full_cache: | |
506 | cout_write(str(self.prompt_out)) |
|
505 | cout_write(str(self.prompt_out)) | |
507 |
|
506 | |||
508 | if isinstance(arg,Macro): |
|
507 | if isinstance(arg,Macro): | |
509 | print 'Executing Macro...' |
|
508 | print 'Executing Macro...' | |
510 | # in case the macro takes a long time to execute |
|
509 | # in case the macro takes a long time to execute | |
511 | Term.cout.flush() |
|
510 | Term.cout.flush() | |
512 | self.shell.runlines(arg.value) |
|
511 | self.shell.runlines(arg.value) | |
513 | return None |
|
512 | return None | |
514 |
|
513 | |||
515 | # and now call a possibly user-defined print mechanism |
|
514 | # and now call a possibly user-defined print mechanism | |
516 | manipulated_val = self.display(arg) |
|
515 | manipulated_val = self.display(arg) | |
517 |
|
516 | |||
518 | # user display hooks can change the variable to be stored in |
|
517 | # user display hooks can change the variable to be stored in | |
519 | # output history |
|
518 | # output history | |
520 |
|
519 | |||
521 | if manipulated_val is not None: |
|
520 | if manipulated_val is not None: | |
522 | arg = manipulated_val |
|
521 | arg = manipulated_val | |
523 |
|
522 | |||
524 | # avoid recursive reference when displaying _oh/Out |
|
523 | # avoid recursive reference when displaying _oh/Out | |
525 | if arg is not self.user_ns['_oh']: |
|
524 | if arg is not self.user_ns['_oh']: | |
526 | self.update(arg) |
|
525 | self.update(arg) | |
527 |
|
526 | |||
528 | if self.logger.log_output: |
|
527 | if self.logger.log_output: | |
529 | self.logger.log_write(repr(arg),'output') |
|
528 | self.logger.log_write(repr(arg),'output') | |
530 | cout_write(self.output_sep2) |
|
529 | cout_write(self.output_sep2) | |
531 | Term.cout.flush() |
|
530 | Term.cout.flush() | |
532 |
|
531 | |||
533 | def _display(self,arg): |
|
532 | def _display(self,arg): | |
534 | """Default printer method, uses pprint. |
|
533 | """Default printer method, uses pprint. | |
535 |
|
534 | |||
536 | Do ip.set_hook("result_display", my_displayhook) for custom result |
|
535 | Do ip.set_hook("result_display", my_displayhook) for custom result | |
537 | display, e.g. when your own objects need special formatting. |
|
536 | display, e.g. when your own objects need special formatting. | |
538 | """ |
|
537 | """ | |
539 |
|
538 | |||
540 | return self.shell.hooks.result_display(arg) |
|
539 | return self.shell.hooks.result_display(arg) | |
541 |
|
540 | |||
542 | # Assign the default display method: |
|
541 | # Assign the default display method: | |
543 | display = _display |
|
542 | display = _display | |
544 |
|
543 | |||
545 | def update(self,arg): |
|
544 | def update(self,arg): | |
546 | #print '***cache_count', self.cache_count # dbg |
|
545 | #print '***cache_count', self.cache_count # dbg | |
547 | if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache: |
|
546 | if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache: | |
548 | warn('Output cache limit (currently '+\ |
|
547 | warn('Output cache limit (currently '+\ | |
549 | `self.cache_count`+' entries) hit.\n' |
|
548 | `self.cache_count`+' entries) hit.\n' | |
550 | 'Flushing cache and resetting history counter...\n' |
|
549 | 'Flushing cache and resetting history counter...\n' | |
551 | 'The only history variables available will be _,__,___ and _1\n' |
|
550 | 'The only history variables available will be _,__,___ and _1\n' | |
552 | 'with the current result.') |
|
551 | 'with the current result.') | |
553 |
|
552 | |||
554 | self.flush() |
|
553 | self.flush() | |
555 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
554 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise | |
556 | # we cause buggy behavior for things like gettext). |
|
555 | # we cause buggy behavior for things like gettext). | |
557 | if '_' not in __builtin__.__dict__: |
|
556 | if '_' not in __builtin__.__dict__: | |
558 | self.___ = self.__ |
|
557 | self.___ = self.__ | |
559 | self.__ = self._ |
|
558 | self.__ = self._ | |
560 | self._ = arg |
|
559 | self._ = arg | |
561 | self.user_ns.update({'_':self._,'__':self.__,'___':self.___}) |
|
560 | self.user_ns.update({'_':self._,'__':self.__,'___':self.___}) | |
562 |
|
561 | |||
563 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
562 | # hackish access to top-level namespace to create _1,_2... dynamically | |
564 | to_main = {} |
|
563 | to_main = {} | |
565 | if self.do_full_cache: |
|
564 | if self.do_full_cache: | |
566 | self.entries.append(arg) |
|
|||
567 | new_result = '_'+`self.prompt_count` |
|
565 | new_result = '_'+`self.prompt_count` | |
568 |
to_main[new_result] = |
|
566 | to_main[new_result] = arg | |
569 | self.user_ns.update(to_main) |
|
567 | self.user_ns.update(to_main) | |
570 | self.user_ns['_oh'][self.prompt_count] = arg |
|
568 | self.user_ns['_oh'][self.prompt_count] = arg | |
571 |
|
569 | |||
572 | def flush(self): |
|
570 | def flush(self): | |
573 | if not self.do_full_cache: |
|
571 | if not self.do_full_cache: | |
574 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
572 | raise ValueError,"You shouldn't have reached the cache flush "\ | |
575 | "if full caching is not enabled!" |
|
573 | "if full caching is not enabled!" | |
576 | # delete auto-generated vars from global namespace |
|
574 | # delete auto-generated vars from global namespace | |
577 |
|
575 | |||
578 | for n in range(1,self.prompt_count + 1): |
|
576 | for n in range(1,self.prompt_count + 1): | |
579 | key = '_'+`n` |
|
577 | key = '_'+`n` | |
580 | try: |
|
578 | try: | |
581 | del self.user_ns[key] |
|
579 | del self.user_ns[key] | |
582 | except: pass |
|
580 | except: pass | |
583 | self.user_ns['_oh'].clear() |
|
581 | self.user_ns['_oh'].clear() | |
584 |
|
582 | |||
585 | if '_' not in __builtin__.__dict__: |
|
583 | if '_' not in __builtin__.__dict__: | |
586 | self.user_ns.update({'_':None,'__':None, '___':None}) |
|
584 | self.user_ns.update({'_':None,'__':None, '___':None}) | |
587 | import gc |
|
585 | import gc | |
588 | gc.collect() # xxx needed? |
|
586 | gc.collect() # xxx needed? |
General Comments 0
You need to be logged in to leave comments.
Login now