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