Show More
@@ -1,2167 +1,2171 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | General purpose utilities. |
|
4 | 4 | |
|
5 | 5 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
6 | 6 | these things are also convenient when working at the command line. |
|
7 | 7 | |
|
8 | 8 | $Id: genutils.py 2998 2008-01-31 10:06:04Z vivainio $""" |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | 17 | from IPython import Release |
|
18 | 18 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
19 | 19 | __license__ = Release.license |
|
20 | 20 | |
|
21 | 21 | #**************************************************************************** |
|
22 | 22 | # required modules from the Python standard library |
|
23 | 23 | import __main__ |
|
24 | 24 | import commands |
|
25 | 25 | try: |
|
26 | 26 | import doctest |
|
27 | 27 | except ImportError: |
|
28 | 28 | pass |
|
29 | 29 | import os |
|
30 | 30 | import platform |
|
31 | 31 | import re |
|
32 | 32 | import shlex |
|
33 | 33 | import shutil |
|
34 | 34 | import subprocess |
|
35 | 35 | import sys |
|
36 | 36 | import tempfile |
|
37 | 37 | import time |
|
38 | 38 | import types |
|
39 | 39 | import warnings |
|
40 | 40 | |
|
41 | 41 | # Curses and termios are Unix-only modules |
|
42 | 42 | try: |
|
43 | 43 | import curses |
|
44 | 44 | # We need termios as well, so if its import happens to raise, we bail on |
|
45 | 45 | # using curses altogether. |
|
46 | 46 | import termios |
|
47 | 47 | except ImportError: |
|
48 | 48 | USE_CURSES = False |
|
49 | 49 | else: |
|
50 | 50 | # Curses on Solaris may not be complete, so we can't use it there |
|
51 | 51 | USE_CURSES = hasattr(curses,'initscr') |
|
52 | 52 | |
|
53 | 53 | # Other IPython utilities |
|
54 | 54 | import IPython |
|
55 | 55 | from IPython.Itpl import Itpl,itpl,printpl |
|
56 | 56 | from IPython import DPyGetOpt, platutils |
|
57 | 57 | from IPython.generics import result_display |
|
58 | 58 | import IPython.ipapi |
|
59 | 59 | from IPython.external.path import path |
|
60 | 60 | if os.name == "nt": |
|
61 | 61 | from IPython.winconsole import get_console_size |
|
62 | 62 | |
|
63 | 63 | try: |
|
64 | 64 | set |
|
65 | 65 | except: |
|
66 | 66 | from sets import Set as set |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | #**************************************************************************** |
|
70 | 70 | # Exceptions |
|
71 | 71 | class Error(Exception): |
|
72 | 72 | """Base class for exceptions in this module.""" |
|
73 | 73 | pass |
|
74 | 74 | |
|
75 | 75 | #---------------------------------------------------------------------------- |
|
76 | 76 | class IOStream: |
|
77 | 77 | def __init__(self,stream,fallback): |
|
78 | 78 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
79 | 79 | stream = fallback |
|
80 | 80 | self.stream = stream |
|
81 | 81 | self._swrite = stream.write |
|
82 | 82 | self.flush = stream.flush |
|
83 | 83 | |
|
84 | 84 | def write(self,data): |
|
85 | 85 | try: |
|
86 | 86 | self._swrite(data) |
|
87 | 87 | except: |
|
88 | 88 | try: |
|
89 | 89 | # print handles some unicode issues which may trip a plain |
|
90 | 90 | # write() call. Attempt to emulate write() by using a |
|
91 | 91 | # trailing comma |
|
92 | 92 | print >> self.stream, data, |
|
93 | 93 | except: |
|
94 | 94 | # if we get here, something is seriously broken. |
|
95 | 95 | print >> sys.stderr, \ |
|
96 | 96 | 'ERROR - failed to write data to stream:', self.stream |
|
97 | 97 | |
|
98 | 98 | def close(self): |
|
99 | 99 | pass |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | class IOTerm: |
|
103 | 103 | """ Term holds the file or file-like objects for handling I/O operations. |
|
104 | 104 | |
|
105 | 105 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
106 | 106 | Windows they can can replaced to allow editing the strings before they are |
|
107 | 107 | displayed.""" |
|
108 | 108 | |
|
109 | 109 | # In the future, having IPython channel all its I/O operations through |
|
110 | 110 | # this class will make it easier to embed it into other environments which |
|
111 | 111 | # are not a normal terminal (such as a GUI-based shell) |
|
112 | 112 | def __init__(self,cin=None,cout=None,cerr=None): |
|
113 | 113 | self.cin = IOStream(cin,sys.stdin) |
|
114 | 114 | self.cout = IOStream(cout,sys.stdout) |
|
115 | 115 | self.cerr = IOStream(cerr,sys.stderr) |
|
116 | 116 | |
|
117 | 117 | # Global variable to be used for all I/O |
|
118 | 118 | Term = IOTerm() |
|
119 | 119 | |
|
120 | 120 | import IPython.rlineimpl as readline |
|
121 | 121 | # Remake Term to use the readline i/o facilities |
|
122 | 122 | if sys.platform == 'win32' and readline.have_readline: |
|
123 | 123 | |
|
124 | 124 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | #**************************************************************************** |
|
128 | 128 | # Generic warning/error printer, used by everything else |
|
129 | 129 | def warn(msg,level=2,exit_val=1): |
|
130 | 130 | """Standard warning printer. Gives formatting consistency. |
|
131 | 131 | |
|
132 | 132 | Output is sent to Term.cerr (sys.stderr by default). |
|
133 | 133 | |
|
134 | 134 | Options: |
|
135 | 135 | |
|
136 | 136 | -level(2): allows finer control: |
|
137 | 137 | 0 -> Do nothing, dummy function. |
|
138 | 138 | 1 -> Print message. |
|
139 | 139 | 2 -> Print 'WARNING:' + message. (Default level). |
|
140 | 140 | 3 -> Print 'ERROR:' + message. |
|
141 | 141 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
142 | 142 | |
|
143 | 143 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
144 | 144 | warning. Ignored for all other levels.""" |
|
145 | 145 | |
|
146 | 146 | if level>0: |
|
147 | 147 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
148 | 148 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
149 | 149 | if level == 4: |
|
150 | 150 | print >> Term.cerr,'Exiting.\n' |
|
151 | 151 | sys.exit(exit_val) |
|
152 | 152 | |
|
153 | 153 | def info(msg): |
|
154 | 154 | """Equivalent to warn(msg,level=1).""" |
|
155 | 155 | |
|
156 | 156 | warn(msg,level=1) |
|
157 | 157 | |
|
158 | 158 | def error(msg): |
|
159 | 159 | """Equivalent to warn(msg,level=3).""" |
|
160 | 160 | |
|
161 | 161 | warn(msg,level=3) |
|
162 | 162 | |
|
163 | 163 | def fatal(msg,exit_val=1): |
|
164 | 164 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
165 | 165 | |
|
166 | 166 | warn(msg,exit_val=exit_val,level=4) |
|
167 | 167 | |
|
168 | 168 | #--------------------------------------------------------------------------- |
|
169 | 169 | # Debugging routines |
|
170 | 170 | # |
|
171 | 171 | def debugx(expr,pre_msg=''): |
|
172 | 172 | """Print the value of an expression from the caller's frame. |
|
173 | 173 | |
|
174 | 174 | Takes an expression, evaluates it in the caller's frame and prints both |
|
175 | 175 | the given expression and the resulting value (as well as a debug mark |
|
176 | 176 | indicating the name of the calling function. The input must be of a form |
|
177 | 177 | suitable for eval(). |
|
178 | 178 | |
|
179 | 179 | An optional message can be passed, which will be prepended to the printed |
|
180 | 180 | expr->value pair.""" |
|
181 | 181 | |
|
182 | 182 | cf = sys._getframe(1) |
|
183 | 183 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
184 | 184 | eval(expr,cf.f_globals,cf.f_locals)) |
|
185 | 185 | |
|
186 | 186 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
187 | 187 | #def debugx(expr,pre_msg=''): pass |
|
188 | 188 | |
|
189 | 189 | #---------------------------------------------------------------------------- |
|
190 | 190 | StringTypes = types.StringTypes |
|
191 | 191 | |
|
192 | 192 | # Basic timing functionality |
|
193 | 193 | |
|
194 | 194 | # If possible (Unix), use the resource module instead of time.clock() |
|
195 | 195 | try: |
|
196 | 196 | import resource |
|
197 | 197 | def clocku(): |
|
198 | 198 | """clocku() -> floating point number |
|
199 | 199 | |
|
200 | 200 | Return the *USER* CPU time in seconds since the start of the process. |
|
201 | 201 | This is done via a call to resource.getrusage, so it avoids the |
|
202 | 202 | wraparound problems in time.clock().""" |
|
203 | 203 | |
|
204 | 204 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
205 | 205 | |
|
206 | 206 | def clocks(): |
|
207 | 207 | """clocks() -> floating point number |
|
208 | 208 | |
|
209 | 209 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
210 | 210 | This is done via a call to resource.getrusage, so it avoids the |
|
211 | 211 | wraparound problems in time.clock().""" |
|
212 | 212 | |
|
213 | 213 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
214 | 214 | |
|
215 | 215 | def clock(): |
|
216 | 216 | """clock() -> floating point number |
|
217 | 217 | |
|
218 | 218 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
219 | 219 | the process. This is done via a call to resource.getrusage, so it |
|
220 | 220 | avoids the wraparound problems in time.clock().""" |
|
221 | 221 | |
|
222 | 222 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
223 | 223 | return u+s |
|
224 | 224 | |
|
225 | 225 | def clock2(): |
|
226 | 226 | """clock2() -> (t_user,t_system) |
|
227 | 227 | |
|
228 | 228 | Similar to clock(), but return a tuple of user/system times.""" |
|
229 | 229 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
230 | 230 | |
|
231 | 231 | except ImportError: |
|
232 | 232 | # There is no distinction of user/system time under windows, so we just use |
|
233 | 233 | # time.clock() for everything... |
|
234 | 234 | clocku = clocks = clock = time.clock |
|
235 | 235 | def clock2(): |
|
236 | 236 | """Under windows, system CPU time can't be measured. |
|
237 | 237 | |
|
238 | 238 | This just returns clock() and zero.""" |
|
239 | 239 | return time.clock(),0.0 |
|
240 | 240 | |
|
241 | 241 | def timings_out(reps,func,*args,**kw): |
|
242 | 242 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
243 | 243 | |
|
244 | 244 | Execute a function reps times, return a tuple with the elapsed total |
|
245 | 245 | CPU time in seconds, the time per call and the function's output. |
|
246 | 246 | |
|
247 | 247 | Under Unix, the return value is the sum of user+system time consumed by |
|
248 | 248 | the process, computed via the resource module. This prevents problems |
|
249 | 249 | related to the wraparound effect which the time.clock() function has. |
|
250 | 250 | |
|
251 | 251 | Under Windows the return value is in wall clock seconds. See the |
|
252 | 252 | documentation for the time module for more details.""" |
|
253 | 253 | |
|
254 | 254 | reps = int(reps) |
|
255 | 255 | assert reps >=1, 'reps must be >= 1' |
|
256 | 256 | if reps==1: |
|
257 | 257 | start = clock() |
|
258 | 258 | out = func(*args,**kw) |
|
259 | 259 | tot_time = clock()-start |
|
260 | 260 | else: |
|
261 | 261 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
262 | 262 | start = clock() |
|
263 | 263 | for dummy in rng: func(*args,**kw) |
|
264 | 264 | out = func(*args,**kw) # one last time |
|
265 | 265 | tot_time = clock()-start |
|
266 | 266 | av_time = tot_time / reps |
|
267 | 267 | return tot_time,av_time,out |
|
268 | 268 | |
|
269 | 269 | def timings(reps,func,*args,**kw): |
|
270 | 270 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
271 | 271 | |
|
272 | 272 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
273 | 273 | time in seconds and the time per call. These are just the first two values |
|
274 | 274 | in timings_out().""" |
|
275 | 275 | |
|
276 | 276 | return timings_out(reps,func,*args,**kw)[0:2] |
|
277 | 277 | |
|
278 | 278 | def timing(func,*args,**kw): |
|
279 | 279 | """timing(func,*args,**kw) -> t_total |
|
280 | 280 | |
|
281 | 281 | Execute a function once, return the elapsed total CPU time in |
|
282 | 282 | seconds. This is just the first value in timings_out().""" |
|
283 | 283 | |
|
284 | 284 | return timings_out(1,func,*args,**kw)[0] |
|
285 | 285 | |
|
286 | 286 | #**************************************************************************** |
|
287 | 287 | # file and system |
|
288 | 288 | |
|
289 | 289 | def arg_split(s,posix=False): |
|
290 | 290 | """Split a command line's arguments in a shell-like manner. |
|
291 | 291 | |
|
292 | 292 | This is a modified version of the standard library's shlex.split() |
|
293 | 293 | function, but with a default of posix=False for splitting, so that quotes |
|
294 | 294 | in inputs are respected.""" |
|
295 | 295 | |
|
296 | 296 | # XXX - there may be unicode-related problems here!!! I'm not sure that |
|
297 | 297 | # shlex is truly unicode-safe, so it might be necessary to do |
|
298 | 298 | # |
|
299 | 299 | # s = s.encode(sys.stdin.encoding) |
|
300 | 300 | # |
|
301 | 301 | # first, to ensure that shlex gets a normal string. Input from anyone who |
|
302 | 302 | # knows more about unicode and shlex than I would be good to have here... |
|
303 | 303 | lex = shlex.shlex(s, posix=posix) |
|
304 | 304 | lex.whitespace_split = True |
|
305 | 305 | return list(lex) |
|
306 | 306 | |
|
307 | 307 | def system(cmd,verbose=0,debug=0,header=''): |
|
308 | 308 | """Execute a system command, return its exit status. |
|
309 | 309 | |
|
310 | 310 | Options: |
|
311 | 311 | |
|
312 | 312 | - verbose (0): print the command to be executed. |
|
313 | 313 | |
|
314 | 314 | - debug (0): only print, do not actually execute. |
|
315 | 315 | |
|
316 | 316 | - header (''): Header to print on screen prior to the executed command (it |
|
317 | 317 | is only prepended to the command, no newlines are added). |
|
318 | 318 | |
|
319 | 319 | Note: a stateful version of this function is available through the |
|
320 | 320 | SystemExec class.""" |
|
321 | 321 | |
|
322 | 322 | stat = 0 |
|
323 | 323 | if verbose or debug: print header+cmd |
|
324 | 324 | sys.stdout.flush() |
|
325 | 325 | if not debug: stat = os.system(cmd) |
|
326 | 326 | return stat |
|
327 | 327 | |
|
328 | 328 | def abbrev_cwd(): |
|
329 | 329 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
330 | 330 | cwd = os.getcwd().replace('\\','/') |
|
331 | 331 | drivepart = '' |
|
332 | 332 | tail = cwd |
|
333 | 333 | if sys.platform == 'win32': |
|
334 | 334 | if len(cwd) < 4: |
|
335 | 335 | return cwd |
|
336 | 336 | drivepart,tail = os.path.splitdrive(cwd) |
|
337 | 337 | |
|
338 | 338 | |
|
339 | 339 | parts = tail.split('/') |
|
340 | 340 | if len(parts) > 2: |
|
341 | 341 | tail = '/'.join(parts[-2:]) |
|
342 | 342 | |
|
343 | 343 | return (drivepart + ( |
|
344 | 344 | cwd == '/' and '/' or tail)) |
|
345 | 345 | |
|
346 | 346 | |
|
347 | 347 | # This function is used by ipython in a lot of places to make system calls. |
|
348 | 348 | # We need it to be slightly different under win32, due to the vagaries of |
|
349 | 349 | # 'network shares'. A win32 override is below. |
|
350 | 350 | |
|
351 | 351 | def shell(cmd,verbose=0,debug=0,header=''): |
|
352 | 352 | """Execute a command in the system shell, always return None. |
|
353 | 353 | |
|
354 | 354 | Options: |
|
355 | 355 | |
|
356 | 356 | - verbose (0): print the command to be executed. |
|
357 | 357 | |
|
358 | 358 | - debug (0): only print, do not actually execute. |
|
359 | 359 | |
|
360 | 360 | - header (''): Header to print on screen prior to the executed command (it |
|
361 | 361 | is only prepended to the command, no newlines are added). |
|
362 | 362 | |
|
363 | 363 | Note: this is similar to genutils.system(), but it returns None so it can |
|
364 | 364 | be conveniently used in interactive loops without getting the return value |
|
365 | 365 | (typically 0) printed many times.""" |
|
366 | 366 | |
|
367 | 367 | stat = 0 |
|
368 | 368 | if verbose or debug: print header+cmd |
|
369 | 369 | # flush stdout so we don't mangle python's buffering |
|
370 | 370 | sys.stdout.flush() |
|
371 | 371 | |
|
372 | 372 | if not debug: |
|
373 | 373 | platutils.set_term_title("IPy " + cmd) |
|
374 | 374 | os.system(cmd) |
|
375 | 375 | platutils.set_term_title("IPy " + abbrev_cwd()) |
|
376 | 376 | |
|
377 | 377 | # override shell() for win32 to deal with network shares |
|
378 | 378 | if os.name in ('nt','dos'): |
|
379 | 379 | |
|
380 | 380 | shell_ori = shell |
|
381 | 381 | |
|
382 | 382 | def shell(cmd,verbose=0,debug=0,header=''): |
|
383 | 383 | if os.getcwd().startswith(r"\\"): |
|
384 | 384 | path = os.getcwd() |
|
385 | 385 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
386 | 386 | # as cmd.exe cannot handle UNC addresses) |
|
387 | 387 | os.chdir("c:") |
|
388 | 388 | # issue pushd to the UNC-share and then run the command |
|
389 | 389 | try: |
|
390 | 390 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
391 | 391 | finally: |
|
392 | 392 | os.chdir(path) |
|
393 | 393 | else: |
|
394 | 394 | shell_ori(cmd,verbose,debug,header) |
|
395 | 395 | |
|
396 | 396 | shell.__doc__ = shell_ori.__doc__ |
|
397 | 397 | |
|
398 | 398 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
399 | 399 | """Dummy substitute for perl's backquotes. |
|
400 | 400 | |
|
401 | 401 | Executes a command and returns the output. |
|
402 | 402 | |
|
403 | 403 | Accepts the same arguments as system(), plus: |
|
404 | 404 | |
|
405 | 405 | - split(0): if true, the output is returned as a list split on newlines. |
|
406 | 406 | |
|
407 | 407 | Note: a stateful version of this function is available through the |
|
408 | 408 | SystemExec class. |
|
409 | 409 | |
|
410 | 410 | This is pretty much deprecated and rarely used, |
|
411 | 411 | genutils.getoutputerror may be what you need. |
|
412 | 412 | |
|
413 | 413 | """ |
|
414 | 414 | |
|
415 | 415 | if verbose or debug: print header+cmd |
|
416 | 416 | if not debug: |
|
417 | 417 | output = os.popen(cmd).read() |
|
418 | 418 | # stipping last \n is here for backwards compat. |
|
419 | 419 | if output.endswith('\n'): |
|
420 | 420 | output = output[:-1] |
|
421 | 421 | if split: |
|
422 | 422 | return output.split('\n') |
|
423 | 423 | else: |
|
424 | 424 | return output |
|
425 | 425 | |
|
426 | 426 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
427 | 427 | """Return (standard output,standard error) of executing cmd in a shell. |
|
428 | 428 | |
|
429 | 429 | Accepts the same arguments as system(), plus: |
|
430 | 430 | |
|
431 | 431 | - split(0): if true, each of stdout/err is returned as a list split on |
|
432 | 432 | newlines. |
|
433 | 433 | |
|
434 | 434 | Note: a stateful version of this function is available through the |
|
435 | 435 | SystemExec class.""" |
|
436 | 436 | |
|
437 | 437 | if verbose or debug: print header+cmd |
|
438 | 438 | if not cmd: |
|
439 | 439 | if split: |
|
440 | 440 | return [],[] |
|
441 | 441 | else: |
|
442 | 442 | return '','' |
|
443 | 443 | if not debug: |
|
444 | 444 | pin,pout,perr = os.popen3(cmd) |
|
445 | 445 | tout = pout.read().rstrip() |
|
446 | 446 | terr = perr.read().rstrip() |
|
447 | 447 | pin.close() |
|
448 | 448 | pout.close() |
|
449 | 449 | perr.close() |
|
450 | 450 | if split: |
|
451 | 451 | return tout.split('\n'),terr.split('\n') |
|
452 | 452 | else: |
|
453 | 453 | return tout,terr |
|
454 | 454 | |
|
455 | 455 | # for compatibility with older naming conventions |
|
456 | 456 | xsys = system |
|
457 | 457 | bq = getoutput |
|
458 | 458 | |
|
459 | 459 | class SystemExec: |
|
460 | 460 | """Access the system and getoutput functions through a stateful interface. |
|
461 | 461 | |
|
462 | 462 | Note: here we refer to the system and getoutput functions from this |
|
463 | 463 | library, not the ones from the standard python library. |
|
464 | 464 | |
|
465 | 465 | This class offers the system and getoutput functions as methods, but the |
|
466 | 466 | verbose, debug and header parameters can be set for the instance (at |
|
467 | 467 | creation time or later) so that they don't need to be specified on each |
|
468 | 468 | call. |
|
469 | 469 | |
|
470 | 470 | For efficiency reasons, there's no way to override the parameters on a |
|
471 | 471 | per-call basis other than by setting instance attributes. If you need |
|
472 | 472 | local overrides, it's best to directly call system() or getoutput(). |
|
473 | 473 | |
|
474 | 474 | The following names are provided as alternate options: |
|
475 | 475 | - xsys: alias to system |
|
476 | 476 | - bq: alias to getoutput |
|
477 | 477 | |
|
478 | 478 | An instance can then be created as: |
|
479 | 479 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
480 | 480 | """ |
|
481 | 481 | |
|
482 | 482 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
483 | 483 | """Specify the instance's values for verbose, debug and header.""" |
|
484 | 484 | setattr_list(self,'verbose debug header split') |
|
485 | 485 | |
|
486 | 486 | def system(self,cmd): |
|
487 | 487 | """Stateful interface to system(), with the same keyword parameters.""" |
|
488 | 488 | |
|
489 | 489 | system(cmd,self.verbose,self.debug,self.header) |
|
490 | 490 | |
|
491 | 491 | def shell(self,cmd): |
|
492 | 492 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
493 | 493 | |
|
494 | 494 | shell(cmd,self.verbose,self.debug,self.header) |
|
495 | 495 | |
|
496 | 496 | xsys = system # alias |
|
497 | 497 | |
|
498 | 498 | def getoutput(self,cmd): |
|
499 | 499 | """Stateful interface to getoutput().""" |
|
500 | 500 | |
|
501 | 501 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
502 | 502 | |
|
503 | 503 | def getoutputerror(self,cmd): |
|
504 | 504 | """Stateful interface to getoutputerror().""" |
|
505 | 505 | |
|
506 | 506 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
507 | 507 | |
|
508 | 508 | bq = getoutput # alias |
|
509 | 509 | |
|
510 | 510 | #----------------------------------------------------------------------------- |
|
511 | 511 | def mutex_opts(dict,ex_op): |
|
512 | 512 | """Check for presence of mutually exclusive keys in a dict. |
|
513 | 513 | |
|
514 | 514 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
515 | 515 | for op1,op2 in ex_op: |
|
516 | 516 | if op1 in dict and op2 in dict: |
|
517 | 517 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
518 | 518 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
519 | 519 | |
|
520 | 520 | #----------------------------------------------------------------------------- |
|
521 | 521 | def get_py_filename(name): |
|
522 | 522 | """Return a valid python filename in the current directory. |
|
523 | 523 | |
|
524 | 524 | If the given name is not a file, it adds '.py' and searches again. |
|
525 | 525 | Raises IOError with an informative message if the file isn't found.""" |
|
526 | 526 | |
|
527 | 527 | name = os.path.expanduser(name) |
|
528 | 528 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
529 | 529 | name += '.py' |
|
530 | 530 | if os.path.isfile(name): |
|
531 | 531 | return name |
|
532 | 532 | else: |
|
533 | 533 | raise IOError,'File `%s` not found.' % name |
|
534 | 534 | |
|
535 | 535 | #----------------------------------------------------------------------------- |
|
536 | 536 | def filefind(fname,alt_dirs = None): |
|
537 | 537 | """Return the given filename either in the current directory, if it |
|
538 | 538 | exists, or in a specified list of directories. |
|
539 | 539 | |
|
540 | 540 | ~ expansion is done on all file and directory names. |
|
541 | 541 | |
|
542 | 542 | Upon an unsuccessful search, raise an IOError exception.""" |
|
543 | 543 | |
|
544 | 544 | if alt_dirs is None: |
|
545 | 545 | try: |
|
546 | 546 | alt_dirs = get_home_dir() |
|
547 | 547 | except HomeDirError: |
|
548 | 548 | alt_dirs = os.getcwd() |
|
549 | 549 | search = [fname] + list_strings(alt_dirs) |
|
550 | 550 | search = map(os.path.expanduser,search) |
|
551 | 551 | #print 'search list for',fname,'list:',search # dbg |
|
552 | 552 | fname = search[0] |
|
553 | 553 | if os.path.isfile(fname): |
|
554 | 554 | return fname |
|
555 | 555 | for direc in search[1:]: |
|
556 | 556 | testname = os.path.join(direc,fname) |
|
557 | 557 | #print 'testname',testname # dbg |
|
558 | 558 | if os.path.isfile(testname): |
|
559 | 559 | return testname |
|
560 | 560 | raise IOError,'File' + `fname` + \ |
|
561 | 561 | ' not found in current or supplied directories:' + `alt_dirs` |
|
562 | 562 | |
|
563 | 563 | #---------------------------------------------------------------------------- |
|
564 | 564 | def file_read(filename): |
|
565 | 565 | """Read a file and close it. Returns the file source.""" |
|
566 | 566 | fobj = open(filename,'r'); |
|
567 | 567 | source = fobj.read(); |
|
568 | 568 | fobj.close() |
|
569 | 569 | return source |
|
570 | 570 | |
|
571 | 571 | def file_readlines(filename): |
|
572 | 572 | """Read a file and close it. Returns the file source using readlines().""" |
|
573 | 573 | fobj = open(filename,'r'); |
|
574 | 574 | lines = fobj.readlines(); |
|
575 | 575 | fobj.close() |
|
576 | 576 | return lines |
|
577 | 577 | |
|
578 | 578 | #---------------------------------------------------------------------------- |
|
579 | 579 | def target_outdated(target,deps): |
|
580 | 580 | """Determine whether a target is out of date. |
|
581 | 581 | |
|
582 | 582 | target_outdated(target,deps) -> 1/0 |
|
583 | 583 | |
|
584 | 584 | deps: list of filenames which MUST exist. |
|
585 | 585 | target: single filename which may or may not exist. |
|
586 | 586 | |
|
587 | 587 | If target doesn't exist or is older than any file listed in deps, return |
|
588 | 588 | true, otherwise return false. |
|
589 | 589 | """ |
|
590 | 590 | try: |
|
591 | 591 | target_time = os.path.getmtime(target) |
|
592 | 592 | except os.error: |
|
593 | 593 | return 1 |
|
594 | 594 | for dep in deps: |
|
595 | 595 | dep_time = os.path.getmtime(dep) |
|
596 | 596 | if dep_time > target_time: |
|
597 | 597 | #print "For target",target,"Dep failed:",dep # dbg |
|
598 | 598 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
599 | 599 | return 1 |
|
600 | 600 | return 0 |
|
601 | 601 | |
|
602 | 602 | #----------------------------------------------------------------------------- |
|
603 | 603 | def target_update(target,deps,cmd): |
|
604 | 604 | """Update a target with a given command given a list of dependencies. |
|
605 | 605 | |
|
606 | 606 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
607 | 607 | |
|
608 | 608 | This is just a wrapper around target_outdated() which calls the given |
|
609 | 609 | command if target is outdated.""" |
|
610 | 610 | |
|
611 | 611 | if target_outdated(target,deps): |
|
612 | 612 | xsys(cmd) |
|
613 | 613 | |
|
614 | 614 | #---------------------------------------------------------------------------- |
|
615 | 615 | def unquote_ends(istr): |
|
616 | 616 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
617 | 617 | |
|
618 | 618 | if not istr: |
|
619 | 619 | return istr |
|
620 | 620 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
621 | 621 | (istr[0]=='"' and istr[-1]=='"'): |
|
622 | 622 | return istr[1:-1] |
|
623 | 623 | else: |
|
624 | 624 | return istr |
|
625 | 625 | |
|
626 | 626 | #---------------------------------------------------------------------------- |
|
627 | 627 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
628 | 628 | """ Process command-line options and arguments. |
|
629 | 629 | |
|
630 | 630 | Arguments: |
|
631 | 631 | |
|
632 | 632 | - argv: list of arguments, typically sys.argv. |
|
633 | 633 | |
|
634 | 634 | - names: list of option names. See DPyGetOpt docs for details on options |
|
635 | 635 | syntax. |
|
636 | 636 | |
|
637 | 637 | - defaults: dict of default values. |
|
638 | 638 | |
|
639 | 639 | - usage: optional usage notice to print if a wrong argument is passed. |
|
640 | 640 | |
|
641 | 641 | Return a dict of options and a list of free arguments.""" |
|
642 | 642 | |
|
643 | 643 | getopt = DPyGetOpt.DPyGetOpt() |
|
644 | 644 | getopt.setIgnoreCase(0) |
|
645 | 645 | getopt.parseConfiguration(names) |
|
646 | 646 | |
|
647 | 647 | try: |
|
648 | 648 | getopt.processArguments(argv) |
|
649 | 649 | except DPyGetOpt.ArgumentError, exc: |
|
650 | 650 | print usage |
|
651 | 651 | warn('"%s"' % exc,level=4) |
|
652 | 652 | |
|
653 | 653 | defaults.update(getopt.optionValues) |
|
654 | 654 | args = getopt.freeValues |
|
655 | 655 | |
|
656 | 656 | return defaults,args |
|
657 | 657 | |
|
658 | 658 | #---------------------------------------------------------------------------- |
|
659 | 659 | def optstr2types(ostr): |
|
660 | 660 | """Convert a string of option names to a dict of type mappings. |
|
661 | 661 | |
|
662 | 662 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
663 | 663 | |
|
664 | 664 | This is used to get the types of all the options in a string formatted |
|
665 | 665 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
666 | 666 | which are strings (they need no further conversion). This function's main |
|
667 | 667 | use is to get a typemap for use with read_dict(). |
|
668 | 668 | """ |
|
669 | 669 | |
|
670 | 670 | typeconv = {None:'',int:'',float:''} |
|
671 | 671 | typemap = {'s':None,'i':int,'f':float} |
|
672 | 672 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
673 | 673 | |
|
674 | 674 | for w in ostr.split(): |
|
675 | 675 | oname,alias,otype = opt_re.match(w).groups() |
|
676 | 676 | if otype == '' or alias == '!': # simple switches are integers too |
|
677 | 677 | otype = 'i' |
|
678 | 678 | typeconv[typemap[otype]] += oname + ' ' |
|
679 | 679 | return typeconv |
|
680 | 680 | |
|
681 | 681 | #---------------------------------------------------------------------------- |
|
682 | 682 | def read_dict(filename,type_conv=None,**opt): |
|
683 | 683 | r"""Read a dictionary of key=value pairs from an input file, optionally |
|
684 | 684 | performing conversions on the resulting values. |
|
685 | 685 | |
|
686 | 686 | read_dict(filename,type_conv,**opt) -> dict |
|
687 | 687 | |
|
688 | 688 | Only one value per line is accepted, the format should be |
|
689 | 689 | # optional comments are ignored |
|
690 | 690 | key value\n |
|
691 | 691 | |
|
692 | 692 | Args: |
|
693 | 693 | |
|
694 | 694 | - type_conv: A dictionary specifying which keys need to be converted to |
|
695 | 695 | which types. By default all keys are read as strings. This dictionary |
|
696 | 696 | should have as its keys valid conversion functions for strings |
|
697 | 697 | (int,long,float,complex, or your own). The value for each key |
|
698 | 698 | (converter) should be a whitespace separated string containing the names |
|
699 | 699 | of all the entries in the file to be converted using that function. For |
|
700 | 700 | keys to be left alone, use None as the conversion function (only needed |
|
701 | 701 | with purge=1, see below). |
|
702 | 702 | |
|
703 | 703 | - opt: dictionary with extra options as below (default in parens) |
|
704 | 704 | |
|
705 | 705 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
706 | 706 | of the dictionary to be returned. If purge is going to be used, the |
|
707 | 707 | set of keys to be left as strings also has to be explicitly specified |
|
708 | 708 | using the (non-existent) conversion function None. |
|
709 | 709 | |
|
710 | 710 | fs(None): field separator. This is the key/value separator to be used |
|
711 | 711 | when parsing the file. The None default means any whitespace [behavior |
|
712 | 712 | of string.split()]. |
|
713 | 713 | |
|
714 | 714 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
715 | 715 | |
|
716 | 716 | warn(1): warning level if requested keys are not found in file. |
|
717 | 717 | - 0: silently ignore. |
|
718 | 718 | - 1: inform but proceed. |
|
719 | 719 | - 2: raise KeyError exception. |
|
720 | 720 | |
|
721 | 721 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
722 | 722 | |
|
723 | 723 | unique([]): list of keys (or space separated string) which can't be |
|
724 | 724 | repeated. If one such key is found in the file, each new instance |
|
725 | 725 | overwrites the previous one. For keys not listed here, the behavior is |
|
726 | 726 | to make a list of all appearances. |
|
727 | 727 | |
|
728 | 728 | Example: |
|
729 | 729 | |
|
730 | 730 | If the input file test.ini contains (we put it in a string to keep the test |
|
731 | 731 | self-contained): |
|
732 | 732 | |
|
733 | 733 | >>> test_ini = '''\ |
|
734 | 734 | ... i 3 |
|
735 | 735 | ... x 4.5 |
|
736 | 736 | ... y 5.5 |
|
737 | 737 | ... s hi ho''' |
|
738 | 738 | |
|
739 | 739 | Then we can use it as follows: |
|
740 | 740 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
741 | 741 | |
|
742 | 742 | >>> d = read_dict(test_ini) |
|
743 | 743 | |
|
744 | 744 | >>> sorted(d.items()) |
|
745 | 745 | [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')] |
|
746 | 746 | |
|
747 | 747 | >>> d = read_dict(test_ini,type_conv) |
|
748 | 748 | |
|
749 | 749 | >>> sorted(d.items()) |
|
750 | 750 | [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')] |
|
751 | 751 | |
|
752 | 752 | >>> d = read_dict(test_ini,type_conv,purge=True) |
|
753 | 753 | |
|
754 | 754 | >>> sorted(d.items()) |
|
755 | 755 | [('i', 3), ('s', 'hi ho'), ('x', 4.5)] |
|
756 | 756 | """ |
|
757 | 757 | |
|
758 | 758 | # starting config |
|
759 | 759 | opt.setdefault('purge',0) |
|
760 | 760 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
761 | 761 | opt.setdefault('strip',0) |
|
762 | 762 | opt.setdefault('warn',1) |
|
763 | 763 | opt.setdefault('no_empty',0) |
|
764 | 764 | opt.setdefault('unique','') |
|
765 | 765 | if type(opt['unique']) in StringTypes: |
|
766 | 766 | unique_keys = qw(opt['unique']) |
|
767 | 767 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
768 | 768 | unique_keys = opt['unique'] |
|
769 | 769 | else: |
|
770 | 770 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
771 | 771 | |
|
772 | 772 | dict = {} |
|
773 | 773 | |
|
774 | 774 | # first read in table of values as strings |
|
775 | 775 | if '\n' in filename: |
|
776 | 776 | lines = filename.splitlines() |
|
777 | 777 | file = None |
|
778 | 778 | else: |
|
779 | 779 | file = open(filename,'r') |
|
780 | 780 | lines = file.readlines() |
|
781 | 781 | for line in lines: |
|
782 | 782 | line = line.strip() |
|
783 | 783 | if len(line) and line[0]=='#': continue |
|
784 | 784 | if len(line)>0: |
|
785 | 785 | lsplit = line.split(opt['fs'],1) |
|
786 | 786 | try: |
|
787 | 787 | key,val = lsplit |
|
788 | 788 | except ValueError: |
|
789 | 789 | key,val = lsplit[0],'' |
|
790 | 790 | key = key.strip() |
|
791 | 791 | if opt['strip']: val = val.strip() |
|
792 | 792 | if val == "''" or val == '""': val = '' |
|
793 | 793 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
794 | 794 | continue |
|
795 | 795 | # if a key is found more than once in the file, build a list |
|
796 | 796 | # unless it's in the 'unique' list. In that case, last found in file |
|
797 | 797 | # takes precedence. User beware. |
|
798 | 798 | try: |
|
799 | 799 | if dict[key] and key in unique_keys: |
|
800 | 800 | dict[key] = val |
|
801 | 801 | elif type(dict[key]) is types.ListType: |
|
802 | 802 | dict[key].append(val) |
|
803 | 803 | else: |
|
804 | 804 | dict[key] = [dict[key],val] |
|
805 | 805 | except KeyError: |
|
806 | 806 | dict[key] = val |
|
807 | 807 | # purge if requested |
|
808 | 808 | if opt['purge']: |
|
809 | 809 | accepted_keys = qwflat(type_conv.values()) |
|
810 | 810 | for key in dict.keys(): |
|
811 | 811 | if key in accepted_keys: continue |
|
812 | 812 | del(dict[key]) |
|
813 | 813 | # now convert if requested |
|
814 | 814 | if type_conv==None: return dict |
|
815 | 815 | conversions = type_conv.keys() |
|
816 | 816 | try: conversions.remove(None) |
|
817 | 817 | except: pass |
|
818 | 818 | for convert in conversions: |
|
819 | 819 | for val in qw(type_conv[convert]): |
|
820 | 820 | try: |
|
821 | 821 | dict[val] = convert(dict[val]) |
|
822 | 822 | except KeyError,e: |
|
823 | 823 | if opt['warn'] == 0: |
|
824 | 824 | pass |
|
825 | 825 | elif opt['warn'] == 1: |
|
826 | 826 | print >>sys.stderr, 'Warning: key',val,\ |
|
827 | 827 | 'not found in file',filename |
|
828 | 828 | elif opt['warn'] == 2: |
|
829 | 829 | raise KeyError,e |
|
830 | 830 | else: |
|
831 | 831 | raise ValueError,'Warning level must be 0,1 or 2' |
|
832 | 832 | |
|
833 | 833 | return dict |
|
834 | 834 | |
|
835 | 835 | #---------------------------------------------------------------------------- |
|
836 | 836 | def flag_calls(func): |
|
837 | 837 | """Wrap a function to detect and flag when it gets called. |
|
838 | 838 | |
|
839 | 839 | This is a decorator which takes a function and wraps it in a function with |
|
840 | 840 | a 'called' attribute. wrapper.called is initialized to False. |
|
841 | 841 | |
|
842 | 842 | The wrapper.called attribute is set to False right before each call to the |
|
843 | 843 | wrapped function, so if the call fails it remains False. After the call |
|
844 | 844 | completes, wrapper.called is set to True and the output is returned. |
|
845 | 845 | |
|
846 | 846 | Testing for truth in wrapper.called allows you to determine if a call to |
|
847 | 847 | func() was attempted and succeeded.""" |
|
848 | 848 | |
|
849 | 849 | def wrapper(*args,**kw): |
|
850 | 850 | wrapper.called = False |
|
851 | 851 | out = func(*args,**kw) |
|
852 | 852 | wrapper.called = True |
|
853 | 853 | return out |
|
854 | 854 | |
|
855 | 855 | wrapper.called = False |
|
856 | 856 | wrapper.__doc__ = func.__doc__ |
|
857 | 857 | return wrapper |
|
858 | 858 | |
|
859 | 859 | #---------------------------------------------------------------------------- |
|
860 | 860 | def dhook_wrap(func,*a,**k): |
|
861 | 861 | """Wrap a function call in a sys.displayhook controller. |
|
862 | 862 | |
|
863 | 863 | Returns a wrapper around func which calls func, with all its arguments and |
|
864 | 864 | keywords unmodified, using the default sys.displayhook. Since IPython |
|
865 | 865 | modifies sys.displayhook, it breaks the behavior of certain systems that |
|
866 | 866 | rely on the default behavior, notably doctest. |
|
867 | 867 | """ |
|
868 | 868 | |
|
869 | 869 | def f(*a,**k): |
|
870 | 870 | |
|
871 | 871 | dhook_s = sys.displayhook |
|
872 | 872 | sys.displayhook = sys.__displayhook__ |
|
873 | 873 | try: |
|
874 | 874 | out = func(*a,**k) |
|
875 | 875 | finally: |
|
876 | 876 | sys.displayhook = dhook_s |
|
877 | 877 | |
|
878 | 878 | return out |
|
879 | 879 | |
|
880 | 880 | f.__doc__ = func.__doc__ |
|
881 | 881 | return f |
|
882 | 882 | |
|
883 | 883 | #---------------------------------------------------------------------------- |
|
884 | 884 | def doctest_reload(): |
|
885 | 885 | """Properly reload doctest to reuse it interactively. |
|
886 | 886 | |
|
887 | 887 | This routine: |
|
888 | 888 | |
|
889 | 889 | - reloads doctest |
|
890 | 890 | |
|
891 | 891 | - resets its global 'master' attribute to None, so that multiple uses of |
|
892 | 892 | the module interactively don't produce cumulative reports. |
|
893 | 893 | |
|
894 | 894 | - Monkeypatches its core test runner method to protect it from IPython's |
|
895 | 895 | modified displayhook. Doctest expects the default displayhook behavior |
|
896 | 896 | deep down, so our modification breaks it completely. For this reason, a |
|
897 | 897 | hard monkeypatch seems like a reasonable solution rather than asking |
|
898 | 898 | users to manually use a different doctest runner when under IPython.""" |
|
899 | 899 | |
|
900 | 900 | import doctest |
|
901 | 901 | reload(doctest) |
|
902 | 902 | doctest.master=None |
|
903 | 903 | |
|
904 | 904 | try: |
|
905 | 905 | doctest.DocTestRunner |
|
906 | 906 | except AttributeError: |
|
907 | 907 | # This is only for python 2.3 compatibility, remove once we move to |
|
908 | 908 | # 2.4 only. |
|
909 | 909 | pass |
|
910 | 910 | else: |
|
911 | 911 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) |
|
912 | 912 | |
|
913 | 913 | #---------------------------------------------------------------------------- |
|
914 | 914 | class HomeDirError(Error): |
|
915 | 915 | pass |
|
916 | 916 | |
|
917 | 917 | def get_home_dir(): |
|
918 | 918 | """Return the closest possible equivalent to a 'home' directory. |
|
919 | 919 | |
|
920 | 920 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
921 | 921 | |
|
922 | 922 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
923 | 923 | raised for all other OSes. """ |
|
924 | 924 | |
|
925 | 925 | isdir = os.path.isdir |
|
926 | 926 | env = os.environ |
|
927 | 927 | |
|
928 | 928 | # first, check py2exe distribution root directory for _ipython. |
|
929 | 929 | # This overrides all. Normally does not exist. |
|
930 | 930 | |
|
931 | 931 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
932 | 932 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
933 | 933 | root, rest = IPython.__file__.lower().split('library.zip') |
|
934 | 934 | else: |
|
935 | 935 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
936 | 936 | root=os.path.abspath(root).rstrip('\\') |
|
937 | 937 | if isdir(os.path.join(root, '_ipython')): |
|
938 | 938 | os.environ["IPYKITROOT"] = root |
|
939 | 939 | return root |
|
940 | 940 | try: |
|
941 | 941 | homedir = env['HOME'] |
|
942 | 942 | if not isdir(homedir): |
|
943 | 943 | # in case a user stuck some string which does NOT resolve to a |
|
944 | 944 | # valid path, it's as good as if we hadn't foud it |
|
945 | raise KeyError | |
|
945 | ||
|
946 | #raise KeyError # dbg | |
|
947 | # dbg - figuring out what's going on here | |
|
948 | pp = os.listdir(homedir+'/..') | |
|
949 | raise ValueError('Wrong dir: %s\n%s' % (homedir,pp)) # dbg | |
|
946 | 950 | return homedir |
|
947 | 951 | except KeyError: |
|
948 | 952 | if os.name == 'posix': |
|
949 | 953 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
950 | 954 | elif os.name == 'nt': |
|
951 | 955 | # For some strange reason, win9x returns 'nt' for os.name. |
|
952 | 956 | try: |
|
953 | 957 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
954 | 958 | if not isdir(homedir): |
|
955 | 959 | homedir = os.path.join(env['USERPROFILE']) |
|
956 | 960 | if not isdir(homedir): |
|
957 | 961 | raise HomeDirError |
|
958 | 962 | return homedir |
|
959 | 963 | except KeyError: |
|
960 | 964 | try: |
|
961 | 965 | # Use the registry to get the 'My Documents' folder. |
|
962 | 966 | import _winreg as wreg |
|
963 | 967 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
964 | 968 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
965 | 969 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
966 | 970 | key.Close() |
|
967 | 971 | if not isdir(homedir): |
|
968 | 972 | e = ('Invalid "Personal" folder registry key ' |
|
969 | 973 | 'typically "My Documents".\n' |
|
970 | 974 | 'Value: %s\n' |
|
971 | 975 | 'This is not a valid directory on your system.' % |
|
972 | 976 | homedir) |
|
973 | 977 | raise HomeDirError(e) |
|
974 | 978 | return homedir |
|
975 | 979 | except HomeDirError: |
|
976 | 980 | raise |
|
977 | 981 | except: |
|
978 | 982 | return 'C:\\' |
|
979 | 983 | elif os.name == 'dos': |
|
980 | 984 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
981 | 985 | return 'C:\\' |
|
982 | 986 | else: |
|
983 | 987 | raise HomeDirError,'support for your operating system not implemented.' |
|
984 | 988 | |
|
985 | 989 | |
|
986 | 990 | def get_ipython_dir(): |
|
987 | 991 | """Get the IPython directory for this platform and user. |
|
988 | 992 | |
|
989 | 993 | This uses the logic in `get_home_dir` to find the home directory |
|
990 | 994 | and the adds either .ipython or _ipython to the end of the path. |
|
991 | 995 | """ |
|
992 | 996 | if os.name == 'posix': |
|
993 | 997 | ipdir_def = '.ipython' |
|
994 | 998 | else: |
|
995 | 999 | ipdir_def = '_ipython' |
|
996 | 1000 | home_dir = get_home_dir() |
|
997 | 1001 | ipdir = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
998 | 1002 | os.path.join(home_dir, ipdir_def))) |
|
999 | 1003 | return ipdir.decode(sys.getfilesystemencoding()) |
|
1000 | 1004 | |
|
1001 | 1005 | def get_security_dir(): |
|
1002 | 1006 | """Get the IPython security directory. |
|
1003 | 1007 | |
|
1004 | 1008 | This directory is the default location for all security related files, |
|
1005 | 1009 | including SSL/TLS certificates and FURL files. |
|
1006 | 1010 | |
|
1007 | 1011 | If the directory does not exist, it is created with 0700 permissions. |
|
1008 | 1012 | If it exists, permissions are set to 0700. |
|
1009 | 1013 | """ |
|
1010 | 1014 | security_dir = os.path.join(get_ipython_dir(), 'security') |
|
1011 | 1015 | if not os.path.isdir(security_dir): |
|
1012 | 1016 | os.mkdir(security_dir, 0700) |
|
1013 | 1017 | else: |
|
1014 | 1018 | os.chmod(security_dir, 0700) |
|
1015 | 1019 | return security_dir |
|
1016 | 1020 | |
|
1017 | 1021 | #**************************************************************************** |
|
1018 | 1022 | # strings and text |
|
1019 | 1023 | |
|
1020 | 1024 | class LSString(str): |
|
1021 | 1025 | """String derivative with a special access attributes. |
|
1022 | 1026 | |
|
1023 | 1027 | These are normal strings, but with the special attributes: |
|
1024 | 1028 | |
|
1025 | 1029 | .l (or .list) : value as list (split on newlines). |
|
1026 | 1030 | .n (or .nlstr): original value (the string itself). |
|
1027 | 1031 | .s (or .spstr): value as whitespace-separated string. |
|
1028 | 1032 | .p (or .paths): list of path objects |
|
1029 | 1033 | |
|
1030 | 1034 | Any values which require transformations are computed only once and |
|
1031 | 1035 | cached. |
|
1032 | 1036 | |
|
1033 | 1037 | Such strings are very useful to efficiently interact with the shell, which |
|
1034 | 1038 | typically only understands whitespace-separated options for commands.""" |
|
1035 | 1039 | |
|
1036 | 1040 | def get_list(self): |
|
1037 | 1041 | try: |
|
1038 | 1042 | return self.__list |
|
1039 | 1043 | except AttributeError: |
|
1040 | 1044 | self.__list = self.split('\n') |
|
1041 | 1045 | return self.__list |
|
1042 | 1046 | |
|
1043 | 1047 | l = list = property(get_list) |
|
1044 | 1048 | |
|
1045 | 1049 | def get_spstr(self): |
|
1046 | 1050 | try: |
|
1047 | 1051 | return self.__spstr |
|
1048 | 1052 | except AttributeError: |
|
1049 | 1053 | self.__spstr = self.replace('\n',' ') |
|
1050 | 1054 | return self.__spstr |
|
1051 | 1055 | |
|
1052 | 1056 | s = spstr = property(get_spstr) |
|
1053 | 1057 | |
|
1054 | 1058 | def get_nlstr(self): |
|
1055 | 1059 | return self |
|
1056 | 1060 | |
|
1057 | 1061 | n = nlstr = property(get_nlstr) |
|
1058 | 1062 | |
|
1059 | 1063 | def get_paths(self): |
|
1060 | 1064 | try: |
|
1061 | 1065 | return self.__paths |
|
1062 | 1066 | except AttributeError: |
|
1063 | 1067 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
1064 | 1068 | return self.__paths |
|
1065 | 1069 | |
|
1066 | 1070 | p = paths = property(get_paths) |
|
1067 | 1071 | |
|
1068 | 1072 | def print_lsstring(arg): |
|
1069 | 1073 | """ Prettier (non-repr-like) and more informative printer for LSString """ |
|
1070 | 1074 | print "LSString (.p, .n, .l, .s available). Value:" |
|
1071 | 1075 | print arg |
|
1072 | 1076 | |
|
1073 | 1077 | print_lsstring = result_display.when_type(LSString)(print_lsstring) |
|
1074 | 1078 | |
|
1075 | 1079 | #---------------------------------------------------------------------------- |
|
1076 | 1080 | class SList(list): |
|
1077 | 1081 | """List derivative with a special access attributes. |
|
1078 | 1082 | |
|
1079 | 1083 | These are normal lists, but with the special attributes: |
|
1080 | 1084 | |
|
1081 | 1085 | .l (or .list) : value as list (the list itself). |
|
1082 | 1086 | .n (or .nlstr): value as a string, joined on newlines. |
|
1083 | 1087 | .s (or .spstr): value as a string, joined on spaces. |
|
1084 | 1088 | .p (or .paths): list of path objects |
|
1085 | 1089 | |
|
1086 | 1090 | Any values which require transformations are computed only once and |
|
1087 | 1091 | cached.""" |
|
1088 | 1092 | |
|
1089 | 1093 | def get_list(self): |
|
1090 | 1094 | return self |
|
1091 | 1095 | |
|
1092 | 1096 | l = list = property(get_list) |
|
1093 | 1097 | |
|
1094 | 1098 | def get_spstr(self): |
|
1095 | 1099 | try: |
|
1096 | 1100 | return self.__spstr |
|
1097 | 1101 | except AttributeError: |
|
1098 | 1102 | self.__spstr = ' '.join(self) |
|
1099 | 1103 | return self.__spstr |
|
1100 | 1104 | |
|
1101 | 1105 | s = spstr = property(get_spstr) |
|
1102 | 1106 | |
|
1103 | 1107 | def get_nlstr(self): |
|
1104 | 1108 | try: |
|
1105 | 1109 | return self.__nlstr |
|
1106 | 1110 | except AttributeError: |
|
1107 | 1111 | self.__nlstr = '\n'.join(self) |
|
1108 | 1112 | return self.__nlstr |
|
1109 | 1113 | |
|
1110 | 1114 | n = nlstr = property(get_nlstr) |
|
1111 | 1115 | |
|
1112 | 1116 | def get_paths(self): |
|
1113 | 1117 | try: |
|
1114 | 1118 | return self.__paths |
|
1115 | 1119 | except AttributeError: |
|
1116 | 1120 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
1117 | 1121 | return self.__paths |
|
1118 | 1122 | |
|
1119 | 1123 | p = paths = property(get_paths) |
|
1120 | 1124 | |
|
1121 | 1125 | def grep(self, pattern, prune = False, field = None): |
|
1122 | 1126 | """ Return all strings matching 'pattern' (a regex or callable) |
|
1123 | 1127 | |
|
1124 | 1128 | This is case-insensitive. If prune is true, return all items |
|
1125 | 1129 | NOT matching the pattern. |
|
1126 | 1130 | |
|
1127 | 1131 | If field is specified, the match must occur in the specified |
|
1128 | 1132 | whitespace-separated field. |
|
1129 | 1133 | |
|
1130 | 1134 | Examples:: |
|
1131 | 1135 | |
|
1132 | 1136 | a.grep( lambda x: x.startswith('C') ) |
|
1133 | 1137 | a.grep('Cha.*log', prune=1) |
|
1134 | 1138 | a.grep('chm', field=-1) |
|
1135 | 1139 | """ |
|
1136 | 1140 | |
|
1137 | 1141 | def match_target(s): |
|
1138 | 1142 | if field is None: |
|
1139 | 1143 | return s |
|
1140 | 1144 | parts = s.split() |
|
1141 | 1145 | try: |
|
1142 | 1146 | tgt = parts[field] |
|
1143 | 1147 | return tgt |
|
1144 | 1148 | except IndexError: |
|
1145 | 1149 | return "" |
|
1146 | 1150 | |
|
1147 | 1151 | if isinstance(pattern, basestring): |
|
1148 | 1152 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
|
1149 | 1153 | else: |
|
1150 | 1154 | pred = pattern |
|
1151 | 1155 | if not prune: |
|
1152 | 1156 | return SList([el for el in self if pred(match_target(el))]) |
|
1153 | 1157 | else: |
|
1154 | 1158 | return SList([el for el in self if not pred(match_target(el))]) |
|
1155 | 1159 | def fields(self, *fields): |
|
1156 | 1160 | """ Collect whitespace-separated fields from string list |
|
1157 | 1161 | |
|
1158 | 1162 | Allows quick awk-like usage of string lists. |
|
1159 | 1163 | |
|
1160 | 1164 | Example data (in var a, created by 'a = !ls -l'):: |
|
1161 | 1165 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
|
1162 | 1166 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
|
1163 | 1167 | |
|
1164 | 1168 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] |
|
1165 | 1169 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] |
|
1166 | 1170 | (note the joining by space). |
|
1167 | 1171 | a.fields(-1) is ['ChangeLog', 'IPython'] |
|
1168 | 1172 | |
|
1169 | 1173 | IndexErrors are ignored. |
|
1170 | 1174 | |
|
1171 | 1175 | Without args, fields() just split()'s the strings. |
|
1172 | 1176 | """ |
|
1173 | 1177 | if len(fields) == 0: |
|
1174 | 1178 | return [el.split() for el in self] |
|
1175 | 1179 | |
|
1176 | 1180 | res = SList() |
|
1177 | 1181 | for el in [f.split() for f in self]: |
|
1178 | 1182 | lineparts = [] |
|
1179 | 1183 | |
|
1180 | 1184 | for fd in fields: |
|
1181 | 1185 | try: |
|
1182 | 1186 | lineparts.append(el[fd]) |
|
1183 | 1187 | except IndexError: |
|
1184 | 1188 | pass |
|
1185 | 1189 | if lineparts: |
|
1186 | 1190 | res.append(" ".join(lineparts)) |
|
1187 | 1191 | |
|
1188 | 1192 | return res |
|
1189 | 1193 | def sort(self,field= None, nums = False): |
|
1190 | 1194 | """ sort by specified fields (see fields()) |
|
1191 | 1195 | |
|
1192 | 1196 | Example:: |
|
1193 | 1197 | a.sort(1, nums = True) |
|
1194 | 1198 | |
|
1195 | 1199 | Sorts a by second field, in numerical order (so that 21 > 3) |
|
1196 | 1200 | |
|
1197 | 1201 | """ |
|
1198 | 1202 | |
|
1199 | 1203 | #decorate, sort, undecorate |
|
1200 | 1204 | if field is not None: |
|
1201 | 1205 | dsu = [[SList([line]).fields(field), line] for line in self] |
|
1202 | 1206 | else: |
|
1203 | 1207 | dsu = [[line, line] for line in self] |
|
1204 | 1208 | if nums: |
|
1205 | 1209 | for i in range(len(dsu)): |
|
1206 | 1210 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
|
1207 | 1211 | try: |
|
1208 | 1212 | n = int(numstr) |
|
1209 | 1213 | except ValueError: |
|
1210 | 1214 | n = 0; |
|
1211 | 1215 | dsu[i][0] = n |
|
1212 | 1216 | |
|
1213 | 1217 | |
|
1214 | 1218 | dsu.sort() |
|
1215 | 1219 | return SList([t[1] for t in dsu]) |
|
1216 | 1220 | |
|
1217 | 1221 | def print_slist(arg): |
|
1218 | 1222 | """ Prettier (non-repr-like) and more informative printer for SList """ |
|
1219 | 1223 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" |
|
1220 | 1224 | if hasattr(arg, 'hideonce') and arg.hideonce: |
|
1221 | 1225 | arg.hideonce = False |
|
1222 | 1226 | return |
|
1223 | 1227 | |
|
1224 | 1228 | nlprint(arg) |
|
1225 | 1229 | |
|
1226 | 1230 | print_slist = result_display.when_type(SList)(print_slist) |
|
1227 | 1231 | |
|
1228 | 1232 | |
|
1229 | 1233 | |
|
1230 | 1234 | #---------------------------------------------------------------------------- |
|
1231 | 1235 | def esc_quotes(strng): |
|
1232 | 1236 | """Return the input string with single and double quotes escaped out""" |
|
1233 | 1237 | |
|
1234 | 1238 | return strng.replace('"','\\"').replace("'","\\'") |
|
1235 | 1239 | |
|
1236 | 1240 | #---------------------------------------------------------------------------- |
|
1237 | 1241 | def make_quoted_expr(s): |
|
1238 | 1242 | """Return string s in appropriate quotes, using raw string if possible. |
|
1239 | 1243 | |
|
1240 | 1244 | Effectively this turns string: cd \ao\ao\ |
|
1241 | 1245 | to: r"cd \ao\ao\_"[:-1] |
|
1242 | 1246 | |
|
1243 | 1247 | Note the use of raw string and padding at the end to allow trailing backslash. |
|
1244 | 1248 | |
|
1245 | 1249 | """ |
|
1246 | 1250 | |
|
1247 | 1251 | tail = '' |
|
1248 | 1252 | tailpadding = '' |
|
1249 | 1253 | raw = '' |
|
1250 | 1254 | if "\\" in s: |
|
1251 | 1255 | raw = 'r' |
|
1252 | 1256 | if s.endswith('\\'): |
|
1253 | 1257 | tail = '[:-1]' |
|
1254 | 1258 | tailpadding = '_' |
|
1255 | 1259 | if '"' not in s: |
|
1256 | 1260 | quote = '"' |
|
1257 | 1261 | elif "'" not in s: |
|
1258 | 1262 | quote = "'" |
|
1259 | 1263 | elif '"""' not in s and not s.endswith('"'): |
|
1260 | 1264 | quote = '"""' |
|
1261 | 1265 | elif "'''" not in s and not s.endswith("'"): |
|
1262 | 1266 | quote = "'''" |
|
1263 | 1267 | else: |
|
1264 | 1268 | # give up, backslash-escaped string will do |
|
1265 | 1269 | return '"%s"' % esc_quotes(s) |
|
1266 | 1270 | res = raw + quote + s + tailpadding + quote + tail |
|
1267 | 1271 | return res |
|
1268 | 1272 | |
|
1269 | 1273 | |
|
1270 | 1274 | #---------------------------------------------------------------------------- |
|
1271 | 1275 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
1272 | 1276 | """Take multiple lines of input. |
|
1273 | 1277 | |
|
1274 | 1278 | A list with each line of input as a separate element is returned when a |
|
1275 | 1279 | termination string is entered (defaults to a single '.'). Input can also |
|
1276 | 1280 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
1277 | 1281 | |
|
1278 | 1282 | Lines of input which end in \\ are joined into single entries (and a |
|
1279 | 1283 | secondary continuation prompt is issued as long as the user terminates |
|
1280 | 1284 | lines with \\). This allows entering very long strings which are still |
|
1281 | 1285 | meant to be treated as single entities. |
|
1282 | 1286 | """ |
|
1283 | 1287 | |
|
1284 | 1288 | try: |
|
1285 | 1289 | if header: |
|
1286 | 1290 | header += '\n' |
|
1287 | 1291 | lines = [raw_input(header + ps1)] |
|
1288 | 1292 | except EOFError: |
|
1289 | 1293 | return [] |
|
1290 | 1294 | terminate = [terminate_str] |
|
1291 | 1295 | try: |
|
1292 | 1296 | while lines[-1:] != terminate: |
|
1293 | 1297 | new_line = raw_input(ps1) |
|
1294 | 1298 | while new_line.endswith('\\'): |
|
1295 | 1299 | new_line = new_line[:-1] + raw_input(ps2) |
|
1296 | 1300 | lines.append(new_line) |
|
1297 | 1301 | |
|
1298 | 1302 | return lines[:-1] # don't return the termination command |
|
1299 | 1303 | except EOFError: |
|
1300 | 1304 | |
|
1301 | 1305 | return lines |
|
1302 | 1306 | |
|
1303 | 1307 | #---------------------------------------------------------------------------- |
|
1304 | 1308 | def raw_input_ext(prompt='', ps2='... '): |
|
1305 | 1309 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
1306 | 1310 | |
|
1307 | 1311 | line = raw_input(prompt) |
|
1308 | 1312 | while line.endswith('\\'): |
|
1309 | 1313 | line = line[:-1] + raw_input(ps2) |
|
1310 | 1314 | return line |
|
1311 | 1315 | |
|
1312 | 1316 | #---------------------------------------------------------------------------- |
|
1313 | 1317 | def ask_yes_no(prompt,default=None): |
|
1314 | 1318 | """Asks a question and returns a boolean (y/n) answer. |
|
1315 | 1319 | |
|
1316 | 1320 | If default is given (one of 'y','n'), it is used if the user input is |
|
1317 | 1321 | empty. Otherwise the question is repeated until an answer is given. |
|
1318 | 1322 | |
|
1319 | 1323 | An EOF is treated as the default answer. If there is no default, an |
|
1320 | 1324 | exception is raised to prevent infinite loops. |
|
1321 | 1325 | |
|
1322 | 1326 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1323 | 1327 | |
|
1324 | 1328 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1325 | 1329 | ans = None |
|
1326 | 1330 | while ans not in answers.keys(): |
|
1327 | 1331 | try: |
|
1328 | 1332 | ans = raw_input(prompt+' ').lower() |
|
1329 | 1333 | if not ans: # response was an empty string |
|
1330 | 1334 | ans = default |
|
1331 | 1335 | except KeyboardInterrupt: |
|
1332 | 1336 | pass |
|
1333 | 1337 | except EOFError: |
|
1334 | 1338 | if default in answers.keys(): |
|
1335 | 1339 | ans = default |
|
1336 | 1340 | |
|
1337 | 1341 | else: |
|
1338 | 1342 | raise |
|
1339 | 1343 | |
|
1340 | 1344 | return answers[ans] |
|
1341 | 1345 | |
|
1342 | 1346 | #---------------------------------------------------------------------------- |
|
1343 | 1347 | def marquee(txt='',width=78,mark='*'): |
|
1344 | 1348 | """Return the input string centered in a 'marquee'.""" |
|
1345 | 1349 | if not txt: |
|
1346 | 1350 | return (mark*width)[:width] |
|
1347 | 1351 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1348 | 1352 | if nmark < 0: nmark =0 |
|
1349 | 1353 | marks = mark*nmark |
|
1350 | 1354 | return '%s %s %s' % (marks,txt,marks) |
|
1351 | 1355 | |
|
1352 | 1356 | #---------------------------------------------------------------------------- |
|
1353 | 1357 | class EvalDict: |
|
1354 | 1358 | """ |
|
1355 | 1359 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1356 | 1360 | |
|
1357 | 1361 | Usage: |
|
1358 | 1362 | >>> number = 19 |
|
1359 | 1363 | |
|
1360 | 1364 | >>> text = "python" |
|
1361 | 1365 | |
|
1362 | 1366 | >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1363 | 1367 | Python 2.1 rules! |
|
1364 | 1368 | """ |
|
1365 | 1369 | |
|
1366 | 1370 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1367 | 1371 | # modified (shorter) version of: |
|
1368 | 1372 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1369 | 1373 | # Skip Montanaro (skip@pobox.com). |
|
1370 | 1374 | |
|
1371 | 1375 | def __getitem__(self, name): |
|
1372 | 1376 | frame = sys._getframe(1) |
|
1373 | 1377 | return eval(name, frame.f_globals, frame.f_locals) |
|
1374 | 1378 | |
|
1375 | 1379 | EvalString = EvalDict # for backwards compatibility |
|
1376 | 1380 | #---------------------------------------------------------------------------- |
|
1377 | 1381 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1378 | 1382 | """Similar to Perl's qw() operator, but with some more options. |
|
1379 | 1383 | |
|
1380 | 1384 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1381 | 1385 | |
|
1382 | 1386 | words can also be a list itself, and with flat=1, the output will be |
|
1383 | 1387 | recursively flattened. |
|
1384 | 1388 | |
|
1385 | 1389 | Examples: |
|
1386 | 1390 | |
|
1387 | 1391 | >>> qw('1 2') |
|
1388 | 1392 | ['1', '2'] |
|
1389 | 1393 | |
|
1390 | 1394 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1391 | 1395 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1392 | 1396 | |
|
1393 | 1397 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1394 | 1398 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] |
|
1395 | 1399 | """ |
|
1396 | 1400 | |
|
1397 | 1401 | if type(words) in StringTypes: |
|
1398 | 1402 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1399 | 1403 | if word and not word.isspace() ] |
|
1400 | 1404 | if flat: |
|
1401 | 1405 | return flatten(map(qw,words,[1]*len(words))) |
|
1402 | 1406 | return map(qw,words) |
|
1403 | 1407 | |
|
1404 | 1408 | #---------------------------------------------------------------------------- |
|
1405 | 1409 | def qwflat(words,sep=None,maxsplit=-1): |
|
1406 | 1410 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1407 | 1411 | return qw(words,1,sep,maxsplit) |
|
1408 | 1412 | |
|
1409 | 1413 | #---------------------------------------------------------------------------- |
|
1410 | 1414 | def qw_lol(indata): |
|
1411 | 1415 | """qw_lol('a b') -> [['a','b']], |
|
1412 | 1416 | otherwise it's just a call to qw(). |
|
1413 | 1417 | |
|
1414 | 1418 | We need this to make sure the modules_some keys *always* end up as a |
|
1415 | 1419 | list of lists.""" |
|
1416 | 1420 | |
|
1417 | 1421 | if type(indata) in StringTypes: |
|
1418 | 1422 | return [qw(indata)] |
|
1419 | 1423 | else: |
|
1420 | 1424 | return qw(indata) |
|
1421 | 1425 | |
|
1422 | 1426 | #----------------------------------------------------------------------------- |
|
1423 | 1427 | def list_strings(arg): |
|
1424 | 1428 | """Always return a list of strings, given a string or list of strings |
|
1425 | 1429 | as input.""" |
|
1426 | 1430 | |
|
1427 | 1431 | if type(arg) in StringTypes: return [arg] |
|
1428 | 1432 | else: return arg |
|
1429 | 1433 | |
|
1430 | 1434 | #---------------------------------------------------------------------------- |
|
1431 | 1435 | def grep(pat,list,case=1): |
|
1432 | 1436 | """Simple minded grep-like function. |
|
1433 | 1437 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1434 | 1438 | |
|
1435 | 1439 | It only does simple string matching, with no support for regexps. Use the |
|
1436 | 1440 | option case=0 for case-insensitive matching.""" |
|
1437 | 1441 | |
|
1438 | 1442 | # This is pretty crude. At least it should implement copying only references |
|
1439 | 1443 | # to the original data in case it's big. Now it copies the data for output. |
|
1440 | 1444 | out=[] |
|
1441 | 1445 | if case: |
|
1442 | 1446 | for term in list: |
|
1443 | 1447 | if term.find(pat)>-1: out.append(term) |
|
1444 | 1448 | else: |
|
1445 | 1449 | lpat=pat.lower() |
|
1446 | 1450 | for term in list: |
|
1447 | 1451 | if term.lower().find(lpat)>-1: out.append(term) |
|
1448 | 1452 | |
|
1449 | 1453 | if len(out): return out |
|
1450 | 1454 | else: return None |
|
1451 | 1455 | |
|
1452 | 1456 | #---------------------------------------------------------------------------- |
|
1453 | 1457 | def dgrep(pat,*opts): |
|
1454 | 1458 | """Return grep() on dir()+dir(__builtins__). |
|
1455 | 1459 | |
|
1456 | 1460 | A very common use of grep() when working interactively.""" |
|
1457 | 1461 | |
|
1458 | 1462 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1459 | 1463 | |
|
1460 | 1464 | #---------------------------------------------------------------------------- |
|
1461 | 1465 | def idgrep(pat): |
|
1462 | 1466 | """Case-insensitive dgrep()""" |
|
1463 | 1467 | |
|
1464 | 1468 | return dgrep(pat,0) |
|
1465 | 1469 | |
|
1466 | 1470 | #---------------------------------------------------------------------------- |
|
1467 | 1471 | def igrep(pat,list): |
|
1468 | 1472 | """Synonym for case-insensitive grep.""" |
|
1469 | 1473 | |
|
1470 | 1474 | return grep(pat,list,case=0) |
|
1471 | 1475 | |
|
1472 | 1476 | #---------------------------------------------------------------------------- |
|
1473 | 1477 | def indent(str,nspaces=4,ntabs=0): |
|
1474 | 1478 | """Indent a string a given number of spaces or tabstops. |
|
1475 | 1479 | |
|
1476 | 1480 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1477 | 1481 | """ |
|
1478 | 1482 | if str is None: |
|
1479 | 1483 | return |
|
1480 | 1484 | ind = '\t'*ntabs+' '*nspaces |
|
1481 | 1485 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1482 | 1486 | if outstr.endswith(os.linesep+ind): |
|
1483 | 1487 | return outstr[:-len(ind)] |
|
1484 | 1488 | else: |
|
1485 | 1489 | return outstr |
|
1486 | 1490 | |
|
1487 | 1491 | #----------------------------------------------------------------------------- |
|
1488 | 1492 | def native_line_ends(filename,backup=1): |
|
1489 | 1493 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1490 | 1494 | |
|
1491 | 1495 | If the optional backup argument is given as false, no backup of the |
|
1492 | 1496 | original file is left. """ |
|
1493 | 1497 | |
|
1494 | 1498 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1495 | 1499 | |
|
1496 | 1500 | bak_filename = filename + backup_suffixes[os.name] |
|
1497 | 1501 | |
|
1498 | 1502 | original = open(filename).read() |
|
1499 | 1503 | shutil.copy2(filename,bak_filename) |
|
1500 | 1504 | try: |
|
1501 | 1505 | new = open(filename,'wb') |
|
1502 | 1506 | new.write(os.linesep.join(original.splitlines())) |
|
1503 | 1507 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1504 | 1508 | new.close() |
|
1505 | 1509 | except: |
|
1506 | 1510 | os.rename(bak_filename,filename) |
|
1507 | 1511 | if not backup: |
|
1508 | 1512 | try: |
|
1509 | 1513 | os.remove(bak_filename) |
|
1510 | 1514 | except: |
|
1511 | 1515 | pass |
|
1512 | 1516 | |
|
1513 | 1517 | #---------------------------------------------------------------------------- |
|
1514 | 1518 | def get_pager_cmd(pager_cmd = None): |
|
1515 | 1519 | """Return a pager command. |
|
1516 | 1520 | |
|
1517 | 1521 | Makes some attempts at finding an OS-correct one.""" |
|
1518 | 1522 | |
|
1519 | 1523 | if os.name == 'posix': |
|
1520 | 1524 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1521 | 1525 | elif os.name in ['nt','dos']: |
|
1522 | 1526 | default_pager_cmd = 'type' |
|
1523 | 1527 | |
|
1524 | 1528 | if pager_cmd is None: |
|
1525 | 1529 | try: |
|
1526 | 1530 | pager_cmd = os.environ['PAGER'] |
|
1527 | 1531 | except: |
|
1528 | 1532 | pager_cmd = default_pager_cmd |
|
1529 | 1533 | return pager_cmd |
|
1530 | 1534 | |
|
1531 | 1535 | #----------------------------------------------------------------------------- |
|
1532 | 1536 | def get_pager_start(pager,start): |
|
1533 | 1537 | """Return the string for paging files with an offset. |
|
1534 | 1538 | |
|
1535 | 1539 | This is the '+N' argument which less and more (under Unix) accept. |
|
1536 | 1540 | """ |
|
1537 | 1541 | |
|
1538 | 1542 | if pager in ['less','more']: |
|
1539 | 1543 | if start: |
|
1540 | 1544 | start_string = '+' + str(start) |
|
1541 | 1545 | else: |
|
1542 | 1546 | start_string = '' |
|
1543 | 1547 | else: |
|
1544 | 1548 | start_string = '' |
|
1545 | 1549 | return start_string |
|
1546 | 1550 | |
|
1547 | 1551 | #---------------------------------------------------------------------------- |
|
1548 | 1552 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() |
|
1549 | 1553 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
1550 | 1554 | import msvcrt |
|
1551 | 1555 | def page_more(): |
|
1552 | 1556 | """ Smart pausing between pages |
|
1553 | 1557 | |
|
1554 | 1558 | @return: True if need print more lines, False if quit |
|
1555 | 1559 | """ |
|
1556 | 1560 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1557 | 1561 | ans = msvcrt.getch() |
|
1558 | 1562 | if ans in ("q", "Q"): |
|
1559 | 1563 | result = False |
|
1560 | 1564 | else: |
|
1561 | 1565 | result = True |
|
1562 | 1566 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1563 | 1567 | return result |
|
1564 | 1568 | else: |
|
1565 | 1569 | def page_more(): |
|
1566 | 1570 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1567 | 1571 | if ans.lower().startswith('q'): |
|
1568 | 1572 | return False |
|
1569 | 1573 | else: |
|
1570 | 1574 | return True |
|
1571 | 1575 | |
|
1572 | 1576 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1573 | 1577 | |
|
1574 | 1578 | def page_dumb(strng,start=0,screen_lines=25): |
|
1575 | 1579 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1576 | 1580 | |
|
1577 | 1581 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1578 | 1582 | mode.""" |
|
1579 | 1583 | |
|
1580 | 1584 | out_ln = strng.splitlines()[start:] |
|
1581 | 1585 | screens = chop(out_ln,screen_lines-1) |
|
1582 | 1586 | if len(screens) == 1: |
|
1583 | 1587 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1584 | 1588 | else: |
|
1585 | 1589 | last_escape = "" |
|
1586 | 1590 | for scr in screens[0:-1]: |
|
1587 | 1591 | hunk = os.linesep.join(scr) |
|
1588 | 1592 | print >>Term.cout, last_escape + hunk |
|
1589 | 1593 | if not page_more(): |
|
1590 | 1594 | return |
|
1591 | 1595 | esc_list = esc_re.findall(hunk) |
|
1592 | 1596 | if len(esc_list) > 0: |
|
1593 | 1597 | last_escape = esc_list[-1] |
|
1594 | 1598 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1595 | 1599 | |
|
1596 | 1600 | #---------------------------------------------------------------------------- |
|
1597 | 1601 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1598 | 1602 | """Print a string, piping through a pager after a certain length. |
|
1599 | 1603 | |
|
1600 | 1604 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1601 | 1605 | terminal screen (total lines minus lines you need to reserve to show other |
|
1602 | 1606 | information). |
|
1603 | 1607 | |
|
1604 | 1608 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1605 | 1609 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1606 | 1610 | printing, paging after that. That is, if you want auto-detection but need |
|
1607 | 1611 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1608 | 1612 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1609 | 1613 | |
|
1610 | 1614 | If a string won't fit in the allowed lines, it is sent through the |
|
1611 | 1615 | specified pager command. If none given, look for PAGER in the environment, |
|
1612 | 1616 | and ultimately default to less. |
|
1613 | 1617 | |
|
1614 | 1618 | If no system pager works, the string is sent through a 'dumb pager' |
|
1615 | 1619 | written in python, very simplistic. |
|
1616 | 1620 | """ |
|
1617 | 1621 | |
|
1618 | 1622 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
1619 | 1623 | # negative value. Offset to 0 for robustness. |
|
1620 | 1624 | start = max(0,start) |
|
1621 | 1625 | |
|
1622 | 1626 | # first, try the hook |
|
1623 | 1627 | ip = IPython.ipapi.get() |
|
1624 | 1628 | if ip: |
|
1625 | 1629 | try: |
|
1626 | 1630 | ip.IP.hooks.show_in_pager(strng) |
|
1627 | 1631 | return |
|
1628 | 1632 | except IPython.ipapi.TryNext: |
|
1629 | 1633 | pass |
|
1630 | 1634 | |
|
1631 | 1635 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1632 | 1636 | TERM = os.environ.get('TERM','dumb') |
|
1633 | 1637 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1634 | 1638 | print strng |
|
1635 | 1639 | return |
|
1636 | 1640 | # chop off the topmost part of the string we don't want to see |
|
1637 | 1641 | str_lines = strng.split(os.linesep)[start:] |
|
1638 | 1642 | str_toprint = os.linesep.join(str_lines) |
|
1639 | 1643 | num_newlines = len(str_lines) |
|
1640 | 1644 | len_str = len(str_toprint) |
|
1641 | 1645 | |
|
1642 | 1646 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1643 | 1647 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1644 | 1648 | # terminals. If someone later feels like refining it, it's not hard. |
|
1645 | 1649 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1646 | 1650 | |
|
1647 | 1651 | if os.name == "nt": |
|
1648 | 1652 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1649 | 1653 | else: |
|
1650 | 1654 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1651 | 1655 | |
|
1652 | 1656 | # auto-determine screen size |
|
1653 | 1657 | if screen_lines <= 0: |
|
1654 | 1658 | if TERM=='xterm': |
|
1655 | 1659 | use_curses = USE_CURSES |
|
1656 | 1660 | else: |
|
1657 | 1661 | # curses causes problems on many terminals other than xterm. |
|
1658 | 1662 | use_curses = False |
|
1659 | 1663 | if use_curses: |
|
1660 | 1664 | # There is a bug in curses, where *sometimes* it fails to properly |
|
1661 | 1665 | # initialize, and then after the endwin() call is made, the |
|
1662 | 1666 | # terminal is left in an unusable state. Rather than trying to |
|
1663 | 1667 | # check everytime for this (by requesting and comparing termios |
|
1664 | 1668 | # flags each time), we just save the initial terminal state and |
|
1665 | 1669 | # unconditionally reset it every time. It's cheaper than making |
|
1666 | 1670 | # the checks. |
|
1667 | 1671 | term_flags = termios.tcgetattr(sys.stdout) |
|
1668 | 1672 | scr = curses.initscr() |
|
1669 | 1673 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1670 | 1674 | curses.endwin() |
|
1671 | 1675 | # Restore terminal state in case endwin() didn't. |
|
1672 | 1676 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
|
1673 | 1677 | # Now we have what we needed: the screen size in rows/columns |
|
1674 | 1678 | screen_lines += screen_lines_real |
|
1675 | 1679 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1676 | 1680 | #screen_cols,'columns.' # dbg |
|
1677 | 1681 | else: |
|
1678 | 1682 | screen_lines += screen_lines_def |
|
1679 | 1683 | |
|
1680 | 1684 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1681 | 1685 | if numlines <= screen_lines : |
|
1682 | 1686 | #print '*** normal print' # dbg |
|
1683 | 1687 | print >>Term.cout, str_toprint |
|
1684 | 1688 | else: |
|
1685 | 1689 | # Try to open pager and default to internal one if that fails. |
|
1686 | 1690 | # All failure modes are tagged as 'retval=1', to match the return |
|
1687 | 1691 | # value of a failed system command. If any intermediate attempt |
|
1688 | 1692 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1689 | 1693 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1690 | 1694 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1691 | 1695 | if os.name == 'nt': |
|
1692 | 1696 | if pager_cmd.startswith('type'): |
|
1693 | 1697 | # The default WinXP 'type' command is failing on complex strings. |
|
1694 | 1698 | retval = 1 |
|
1695 | 1699 | else: |
|
1696 | 1700 | tmpname = tempfile.mktemp('.txt') |
|
1697 | 1701 | tmpfile = file(tmpname,'wt') |
|
1698 | 1702 | tmpfile.write(strng) |
|
1699 | 1703 | tmpfile.close() |
|
1700 | 1704 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1701 | 1705 | if os.system(cmd): |
|
1702 | 1706 | retval = 1 |
|
1703 | 1707 | else: |
|
1704 | 1708 | retval = None |
|
1705 | 1709 | os.remove(tmpname) |
|
1706 | 1710 | else: |
|
1707 | 1711 | try: |
|
1708 | 1712 | retval = None |
|
1709 | 1713 | # if I use popen4, things hang. No idea why. |
|
1710 | 1714 | #pager,shell_out = os.popen4(pager_cmd) |
|
1711 | 1715 | pager = os.popen(pager_cmd,'w') |
|
1712 | 1716 | pager.write(strng) |
|
1713 | 1717 | pager.close() |
|
1714 | 1718 | retval = pager.close() # success returns None |
|
1715 | 1719 | except IOError,msg: # broken pipe when user quits |
|
1716 | 1720 | if msg.args == (32,'Broken pipe'): |
|
1717 | 1721 | retval = None |
|
1718 | 1722 | else: |
|
1719 | 1723 | retval = 1 |
|
1720 | 1724 | except OSError: |
|
1721 | 1725 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1722 | 1726 | retval = 1 |
|
1723 | 1727 | if retval is not None: |
|
1724 | 1728 | page_dumb(strng,screen_lines=screen_lines) |
|
1725 | 1729 | |
|
1726 | 1730 | #---------------------------------------------------------------------------- |
|
1727 | 1731 | def page_file(fname,start = 0, pager_cmd = None): |
|
1728 | 1732 | """Page a file, using an optional pager command and starting line. |
|
1729 | 1733 | """ |
|
1730 | 1734 | |
|
1731 | 1735 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1732 | 1736 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1733 | 1737 | |
|
1734 | 1738 | try: |
|
1735 | 1739 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1736 | 1740 | raise EnvironmentError |
|
1737 | 1741 | xsys(pager_cmd + ' ' + fname) |
|
1738 | 1742 | except: |
|
1739 | 1743 | try: |
|
1740 | 1744 | if start > 0: |
|
1741 | 1745 | start -= 1 |
|
1742 | 1746 | page(open(fname).read(),start) |
|
1743 | 1747 | except: |
|
1744 | 1748 | print 'Unable to show file',`fname` |
|
1745 | 1749 | |
|
1746 | 1750 | |
|
1747 | 1751 | #---------------------------------------------------------------------------- |
|
1748 | 1752 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1749 | 1753 | """Print a string snipping the midsection to fit in width. |
|
1750 | 1754 | |
|
1751 | 1755 | print_full: mode control: |
|
1752 | 1756 | - 0: only snip long strings |
|
1753 | 1757 | - 1: send to page() directly. |
|
1754 | 1758 | - 2: snip long strings and ask for full length viewing with page() |
|
1755 | 1759 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1756 | 1760 | |
|
1757 | 1761 | if print_full == 1: |
|
1758 | 1762 | page(header+str) |
|
1759 | 1763 | return 0 |
|
1760 | 1764 | |
|
1761 | 1765 | print header, |
|
1762 | 1766 | if len(str) < width: |
|
1763 | 1767 | print str |
|
1764 | 1768 | snip = 0 |
|
1765 | 1769 | else: |
|
1766 | 1770 | whalf = int((width -5)/2) |
|
1767 | 1771 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1768 | 1772 | snip = 1 |
|
1769 | 1773 | if snip and print_full == 2: |
|
1770 | 1774 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1771 | 1775 | page(str) |
|
1772 | 1776 | return snip |
|
1773 | 1777 | |
|
1774 | 1778 | #**************************************************************************** |
|
1775 | 1779 | # lists, dicts and structures |
|
1776 | 1780 | |
|
1777 | 1781 | def belong(candidates,checklist): |
|
1778 | 1782 | """Check whether a list of items appear in a given list of options. |
|
1779 | 1783 | |
|
1780 | 1784 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1781 | 1785 | |
|
1782 | 1786 | return [x in checklist for x in candidates] |
|
1783 | 1787 | |
|
1784 | 1788 | #---------------------------------------------------------------------------- |
|
1785 | 1789 | def uniq_stable(elems): |
|
1786 | 1790 | """uniq_stable(elems) -> list |
|
1787 | 1791 | |
|
1788 | 1792 | Return from an iterable, a list of all the unique elements in the input, |
|
1789 | 1793 | but maintaining the order in which they first appear. |
|
1790 | 1794 | |
|
1791 | 1795 | A naive solution to this problem which just makes a dictionary with the |
|
1792 | 1796 | elements as keys fails to respect the stability condition, since |
|
1793 | 1797 | dictionaries are unsorted by nature. |
|
1794 | 1798 | |
|
1795 | 1799 | Note: All elements in the input must be valid dictionary keys for this |
|
1796 | 1800 | routine to work, as it internally uses a dictionary for efficiency |
|
1797 | 1801 | reasons.""" |
|
1798 | 1802 | |
|
1799 | 1803 | unique = [] |
|
1800 | 1804 | unique_dict = {} |
|
1801 | 1805 | for nn in elems: |
|
1802 | 1806 | if nn not in unique_dict: |
|
1803 | 1807 | unique.append(nn) |
|
1804 | 1808 | unique_dict[nn] = None |
|
1805 | 1809 | return unique |
|
1806 | 1810 | |
|
1807 | 1811 | #---------------------------------------------------------------------------- |
|
1808 | 1812 | class NLprinter: |
|
1809 | 1813 | """Print an arbitrarily nested list, indicating index numbers. |
|
1810 | 1814 | |
|
1811 | 1815 | An instance of this class called nlprint is available and callable as a |
|
1812 | 1816 | function. |
|
1813 | 1817 | |
|
1814 | 1818 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1815 | 1819 | and using 'sep' to separate the index from the value. """ |
|
1816 | 1820 | |
|
1817 | 1821 | def __init__(self): |
|
1818 | 1822 | self.depth = 0 |
|
1819 | 1823 | |
|
1820 | 1824 | def __call__(self,lst,pos='',**kw): |
|
1821 | 1825 | """Prints the nested list numbering levels.""" |
|
1822 | 1826 | kw.setdefault('indent',' ') |
|
1823 | 1827 | kw.setdefault('sep',': ') |
|
1824 | 1828 | kw.setdefault('start',0) |
|
1825 | 1829 | kw.setdefault('stop',len(lst)) |
|
1826 | 1830 | # we need to remove start and stop from kw so they don't propagate |
|
1827 | 1831 | # into a recursive call for a nested list. |
|
1828 | 1832 | start = kw['start']; del kw['start'] |
|
1829 | 1833 | stop = kw['stop']; del kw['stop'] |
|
1830 | 1834 | if self.depth == 0 and 'header' in kw.keys(): |
|
1831 | 1835 | print kw['header'] |
|
1832 | 1836 | |
|
1833 | 1837 | for idx in range(start,stop): |
|
1834 | 1838 | elem = lst[idx] |
|
1835 | 1839 | if type(elem)==type([]): |
|
1836 | 1840 | self.depth += 1 |
|
1837 | 1841 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1838 | 1842 | self.depth -= 1 |
|
1839 | 1843 | else: |
|
1840 | 1844 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1841 | 1845 | |
|
1842 | 1846 | nlprint = NLprinter() |
|
1843 | 1847 | #---------------------------------------------------------------------------- |
|
1844 | 1848 | def all_belong(candidates,checklist): |
|
1845 | 1849 | """Check whether a list of items ALL appear in a given list of options. |
|
1846 | 1850 | |
|
1847 | 1851 | Returns a single 1 or 0 value.""" |
|
1848 | 1852 | |
|
1849 | 1853 | return 1-(0 in [x in checklist for x in candidates]) |
|
1850 | 1854 | |
|
1851 | 1855 | #---------------------------------------------------------------------------- |
|
1852 | 1856 | def sort_compare(lst1,lst2,inplace = 1): |
|
1853 | 1857 | """Sort and compare two lists. |
|
1854 | 1858 | |
|
1855 | 1859 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1856 | 1860 | to avoid that (at the cost of temporary copy creation).""" |
|
1857 | 1861 | if not inplace: |
|
1858 | 1862 | lst1 = lst1[:] |
|
1859 | 1863 | lst2 = lst2[:] |
|
1860 | 1864 | lst1.sort(); lst2.sort() |
|
1861 | 1865 | return lst1 == lst2 |
|
1862 | 1866 | |
|
1863 | 1867 | #---------------------------------------------------------------------------- |
|
1864 | 1868 | def list2dict(lst): |
|
1865 | 1869 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1866 | 1870 | |
|
1867 | 1871 | dic = {} |
|
1868 | 1872 | for k,v in lst: dic[k] = v |
|
1869 | 1873 | return dic |
|
1870 | 1874 | |
|
1871 | 1875 | #---------------------------------------------------------------------------- |
|
1872 | 1876 | def list2dict2(lst,default=''): |
|
1873 | 1877 | """Takes a list and turns it into a dict. |
|
1874 | 1878 | Much slower than list2dict, but more versatile. This version can take |
|
1875 | 1879 | lists with sublists of arbitrary length (including sclars).""" |
|
1876 | 1880 | |
|
1877 | 1881 | dic = {} |
|
1878 | 1882 | for elem in lst: |
|
1879 | 1883 | if type(elem) in (types.ListType,types.TupleType): |
|
1880 | 1884 | size = len(elem) |
|
1881 | 1885 | if size == 0: |
|
1882 | 1886 | pass |
|
1883 | 1887 | elif size == 1: |
|
1884 | 1888 | dic[elem] = default |
|
1885 | 1889 | else: |
|
1886 | 1890 | k,v = elem[0], elem[1:] |
|
1887 | 1891 | if len(v) == 1: v = v[0] |
|
1888 | 1892 | dic[k] = v |
|
1889 | 1893 | else: |
|
1890 | 1894 | dic[elem] = default |
|
1891 | 1895 | return dic |
|
1892 | 1896 | |
|
1893 | 1897 | #---------------------------------------------------------------------------- |
|
1894 | 1898 | def flatten(seq): |
|
1895 | 1899 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1896 | 1900 | |
|
1897 | 1901 | return [x for subseq in seq for x in subseq] |
|
1898 | 1902 | |
|
1899 | 1903 | #---------------------------------------------------------------------------- |
|
1900 | 1904 | def get_slice(seq,start=0,stop=None,step=1): |
|
1901 | 1905 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1902 | 1906 | if stop == None: |
|
1903 | 1907 | stop = len(seq) |
|
1904 | 1908 | item = lambda i: seq[i] |
|
1905 | 1909 | return map(item,xrange(start,stop,step)) |
|
1906 | 1910 | |
|
1907 | 1911 | #---------------------------------------------------------------------------- |
|
1908 | 1912 | def chop(seq,size): |
|
1909 | 1913 | """Chop a sequence into chunks of the given size.""" |
|
1910 | 1914 | chunk = lambda i: seq[i:i+size] |
|
1911 | 1915 | return map(chunk,xrange(0,len(seq),size)) |
|
1912 | 1916 | |
|
1913 | 1917 | #---------------------------------------------------------------------------- |
|
1914 | 1918 | # with is a keyword as of python 2.5, so this function is renamed to withobj |
|
1915 | 1919 | # from its old 'with' name. |
|
1916 | 1920 | def with_obj(object, **args): |
|
1917 | 1921 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1918 | 1922 | |
|
1919 | 1923 | Example: |
|
1920 | 1924 | with_obj(jim, |
|
1921 | 1925 | born = 1960, |
|
1922 | 1926 | haircolour = 'Brown', |
|
1923 | 1927 | eyecolour = 'Green') |
|
1924 | 1928 | |
|
1925 | 1929 | Credit: Greg Ewing, in |
|
1926 | 1930 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. |
|
1927 | 1931 | |
|
1928 | 1932 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' |
|
1929 | 1933 | has become a keyword for Python 2.5, so we had to rename it.""" |
|
1930 | 1934 | |
|
1931 | 1935 | object.__dict__.update(args) |
|
1932 | 1936 | |
|
1933 | 1937 | #---------------------------------------------------------------------------- |
|
1934 | 1938 | def setattr_list(obj,alist,nspace = None): |
|
1935 | 1939 | """Set a list of attributes for an object taken from a namespace. |
|
1936 | 1940 | |
|
1937 | 1941 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1938 | 1942 | alist with their values taken from nspace, which must be a dict (something |
|
1939 | 1943 | like locals() will often do) If nspace isn't given, locals() of the |
|
1940 | 1944 | *caller* is used, so in most cases you can omit it. |
|
1941 | 1945 | |
|
1942 | 1946 | Note that alist can be given as a string, which will be automatically |
|
1943 | 1947 | split into a list on whitespace. If given as a list, it must be a list of |
|
1944 | 1948 | *strings* (the variable names themselves), not of variables.""" |
|
1945 | 1949 | |
|
1946 | 1950 | # this grabs the local variables from the *previous* call frame -- that is |
|
1947 | 1951 | # the locals from the function that called setattr_list(). |
|
1948 | 1952 | # - snipped from weave.inline() |
|
1949 | 1953 | if nspace is None: |
|
1950 | 1954 | call_frame = sys._getframe().f_back |
|
1951 | 1955 | nspace = call_frame.f_locals |
|
1952 | 1956 | |
|
1953 | 1957 | if type(alist) in StringTypes: |
|
1954 | 1958 | alist = alist.split() |
|
1955 | 1959 | for attr in alist: |
|
1956 | 1960 | val = eval(attr,nspace) |
|
1957 | 1961 | setattr(obj,attr,val) |
|
1958 | 1962 | |
|
1959 | 1963 | #---------------------------------------------------------------------------- |
|
1960 | 1964 | def getattr_list(obj,alist,*args): |
|
1961 | 1965 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1962 | 1966 | |
|
1963 | 1967 | Get a list of named attributes for an object. When a default argument is |
|
1964 | 1968 | given, it is returned when the attribute doesn't exist; without it, an |
|
1965 | 1969 | exception is raised in that case. |
|
1966 | 1970 | |
|
1967 | 1971 | Note that alist can be given as a string, which will be automatically |
|
1968 | 1972 | split into a list on whitespace. If given as a list, it must be a list of |
|
1969 | 1973 | *strings* (the variable names themselves), not of variables.""" |
|
1970 | 1974 | |
|
1971 | 1975 | if type(alist) in StringTypes: |
|
1972 | 1976 | alist = alist.split() |
|
1973 | 1977 | if args: |
|
1974 | 1978 | if len(args)==1: |
|
1975 | 1979 | default = args[0] |
|
1976 | 1980 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1977 | 1981 | else: |
|
1978 | 1982 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1979 | 1983 | else: |
|
1980 | 1984 | return map(lambda attr: getattr(obj,attr),alist) |
|
1981 | 1985 | |
|
1982 | 1986 | #---------------------------------------------------------------------------- |
|
1983 | 1987 | def map_method(method,object_list,*argseq,**kw): |
|
1984 | 1988 | """map_method(method,object_list,*args,**kw) -> list |
|
1985 | 1989 | |
|
1986 | 1990 | Return a list of the results of applying the methods to the items of the |
|
1987 | 1991 | argument sequence(s). If more than one sequence is given, the method is |
|
1988 | 1992 | called with an argument list consisting of the corresponding item of each |
|
1989 | 1993 | sequence. All sequences must be of the same length. |
|
1990 | 1994 | |
|
1991 | 1995 | Keyword arguments are passed verbatim to all objects called. |
|
1992 | 1996 | |
|
1993 | 1997 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1994 | 1998 | |
|
1995 | 1999 | out_list = [] |
|
1996 | 2000 | idx = 0 |
|
1997 | 2001 | for object in object_list: |
|
1998 | 2002 | try: |
|
1999 | 2003 | handler = getattr(object, method) |
|
2000 | 2004 | except AttributeError: |
|
2001 | 2005 | out_list.append(None) |
|
2002 | 2006 | else: |
|
2003 | 2007 | if argseq: |
|
2004 | 2008 | args = map(lambda lst:lst[idx],argseq) |
|
2005 | 2009 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
2006 | 2010 | out_list.append(handler(args,**kw)) |
|
2007 | 2011 | else: |
|
2008 | 2012 | out_list.append(handler(**kw)) |
|
2009 | 2013 | idx += 1 |
|
2010 | 2014 | return out_list |
|
2011 | 2015 | |
|
2012 | 2016 | #---------------------------------------------------------------------------- |
|
2013 | 2017 | def get_class_members(cls): |
|
2014 | 2018 | ret = dir(cls) |
|
2015 | 2019 | if hasattr(cls,'__bases__'): |
|
2016 | 2020 | for base in cls.__bases__: |
|
2017 | 2021 | ret.extend(get_class_members(base)) |
|
2018 | 2022 | return ret |
|
2019 | 2023 | |
|
2020 | 2024 | #---------------------------------------------------------------------------- |
|
2021 | 2025 | def dir2(obj): |
|
2022 | 2026 | """dir2(obj) -> list of strings |
|
2023 | 2027 | |
|
2024 | 2028 | Extended version of the Python builtin dir(), which does a few extra |
|
2025 | 2029 | checks, and supports common objects with unusual internals that confuse |
|
2026 | 2030 | dir(), such as Traits and PyCrust. |
|
2027 | 2031 | |
|
2028 | 2032 | This version is guaranteed to return only a list of true strings, whereas |
|
2029 | 2033 | dir() returns anything that objects inject into themselves, even if they |
|
2030 | 2034 | are later not really valid for attribute access (many extension libraries |
|
2031 | 2035 | have such bugs). |
|
2032 | 2036 | """ |
|
2033 | 2037 | |
|
2034 | 2038 | # Start building the attribute list via dir(), and then complete it |
|
2035 | 2039 | # with a few extra special-purpose calls. |
|
2036 | 2040 | words = dir(obj) |
|
2037 | 2041 | |
|
2038 | 2042 | if hasattr(obj,'__class__'): |
|
2039 | 2043 | words.append('__class__') |
|
2040 | 2044 | words.extend(get_class_members(obj.__class__)) |
|
2041 | 2045 | #if '__base__' in words: 1/0 |
|
2042 | 2046 | |
|
2043 | 2047 | # Some libraries (such as traits) may introduce duplicates, we want to |
|
2044 | 2048 | # track and clean this up if it happens |
|
2045 | 2049 | may_have_dupes = False |
|
2046 | 2050 | |
|
2047 | 2051 | # this is the 'dir' function for objects with Enthought's traits |
|
2048 | 2052 | if hasattr(obj, 'trait_names'): |
|
2049 | 2053 | try: |
|
2050 | 2054 | words.extend(obj.trait_names()) |
|
2051 | 2055 | may_have_dupes = True |
|
2052 | 2056 | except TypeError: |
|
2053 | 2057 | # This will happen if `obj` is a class and not an instance. |
|
2054 | 2058 | pass |
|
2055 | 2059 | |
|
2056 | 2060 | # Support for PyCrust-style _getAttributeNames magic method. |
|
2057 | 2061 | if hasattr(obj, '_getAttributeNames'): |
|
2058 | 2062 | try: |
|
2059 | 2063 | words.extend(obj._getAttributeNames()) |
|
2060 | 2064 | may_have_dupes = True |
|
2061 | 2065 | except TypeError: |
|
2062 | 2066 | # `obj` is a class and not an instance. Ignore |
|
2063 | 2067 | # this error. |
|
2064 | 2068 | pass |
|
2065 | 2069 | |
|
2066 | 2070 | if may_have_dupes: |
|
2067 | 2071 | # eliminate possible duplicates, as some traits may also |
|
2068 | 2072 | # appear as normal attributes in the dir() call. |
|
2069 | 2073 | words = list(set(words)) |
|
2070 | 2074 | words.sort() |
|
2071 | 2075 | |
|
2072 | 2076 | # filter out non-string attributes which may be stuffed by dir() calls |
|
2073 | 2077 | # and poor coding in third-party modules |
|
2074 | 2078 | return [w for w in words if isinstance(w, basestring)] |
|
2075 | 2079 | |
|
2076 | 2080 | #---------------------------------------------------------------------------- |
|
2077 | 2081 | def import_fail_info(mod_name,fns=None): |
|
2078 | 2082 | """Inform load failure for a module.""" |
|
2079 | 2083 | |
|
2080 | 2084 | if fns == None: |
|
2081 | 2085 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
2082 | 2086 | else: |
|
2083 | 2087 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
2084 | 2088 | |
|
2085 | 2089 | #---------------------------------------------------------------------------- |
|
2086 | 2090 | # Proposed popitem() extension, written as a method |
|
2087 | 2091 | |
|
2088 | 2092 | |
|
2089 | 2093 | class NotGiven: pass |
|
2090 | 2094 | |
|
2091 | 2095 | def popkey(dct,key,default=NotGiven): |
|
2092 | 2096 | """Return dct[key] and delete dct[key]. |
|
2093 | 2097 | |
|
2094 | 2098 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
2095 | 2099 | KeyError. """ |
|
2096 | 2100 | |
|
2097 | 2101 | try: |
|
2098 | 2102 | val = dct[key] |
|
2099 | 2103 | except KeyError: |
|
2100 | 2104 | if default is NotGiven: |
|
2101 | 2105 | raise |
|
2102 | 2106 | else: |
|
2103 | 2107 | return default |
|
2104 | 2108 | else: |
|
2105 | 2109 | del dct[key] |
|
2106 | 2110 | return val |
|
2107 | 2111 | |
|
2108 | 2112 | def wrap_deprecated(func, suggest = '<nothing>'): |
|
2109 | 2113 | def newFunc(*args, **kwargs): |
|
2110 | 2114 | warnings.warn("Call to deprecated function %s, use %s instead" % |
|
2111 | 2115 | ( func.__name__, suggest), |
|
2112 | 2116 | category=DeprecationWarning, |
|
2113 | 2117 | stacklevel = 2) |
|
2114 | 2118 | return func(*args, **kwargs) |
|
2115 | 2119 | return newFunc |
|
2116 | 2120 | |
|
2117 | 2121 | |
|
2118 | 2122 | def _num_cpus_unix(): |
|
2119 | 2123 | """Return the number of active CPUs on a Unix system.""" |
|
2120 | 2124 | return os.sysconf("SC_NPROCESSORS_ONLN") |
|
2121 | 2125 | |
|
2122 | 2126 | |
|
2123 | 2127 | def _num_cpus_darwin(): |
|
2124 | 2128 | """Return the number of active CPUs on a Darwin system.""" |
|
2125 | 2129 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) |
|
2126 | 2130 | return p.stdout.read() |
|
2127 | 2131 | |
|
2128 | 2132 | |
|
2129 | 2133 | def _num_cpus_windows(): |
|
2130 | 2134 | """Return the number of active CPUs on a Windows system.""" |
|
2131 | 2135 | return os.environ.get("NUMBER_OF_PROCESSORS") |
|
2132 | 2136 | |
|
2133 | 2137 | |
|
2134 | 2138 | def num_cpus(): |
|
2135 | 2139 | """Return the effective number of CPUs in the system as an integer. |
|
2136 | 2140 | |
|
2137 | 2141 | This cross-platform function makes an attempt at finding the total number of |
|
2138 | 2142 | available CPUs in the system, as returned by various underlying system and |
|
2139 | 2143 | python calls. |
|
2140 | 2144 | |
|
2141 | 2145 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
|
2142 | 2146 | it return a large positive number that's actually incorrect). |
|
2143 | 2147 | """ |
|
2144 | 2148 | |
|
2145 | 2149 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
|
2146 | 2150 | # for the names of the keys we needed to look up for this function. This |
|
2147 | 2151 | # code was inspired by their equivalent function. |
|
2148 | 2152 | |
|
2149 | 2153 | ncpufuncs = {'Linux':_num_cpus_unix, |
|
2150 | 2154 | 'Darwin':_num_cpus_darwin, |
|
2151 | 2155 | 'Windows':_num_cpus_windows, |
|
2152 | 2156 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' |
|
2153 | 2157 | # See http://bugs.python.org/issue1082 for details. |
|
2154 | 2158 | 'Microsoft':_num_cpus_windows, |
|
2155 | 2159 | } |
|
2156 | 2160 | |
|
2157 | 2161 | ncpufunc = ncpufuncs.get(platform.system(), |
|
2158 | 2162 | # default to unix version (Solaris, AIX, etc) |
|
2159 | 2163 | _num_cpus_unix) |
|
2160 | 2164 | |
|
2161 | 2165 | try: |
|
2162 | 2166 | ncpus = max(1,int(ncpufunc())) |
|
2163 | 2167 | except: |
|
2164 | 2168 | ncpus = 1 |
|
2165 | 2169 | return ncpus |
|
2166 | 2170 | |
|
2167 | 2171 | #*************************** end of file <genutils.py> ********************** |
@@ -1,2695 +1,2696 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.3 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
13 | 13 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
14 | 14 | # |
|
15 | 15 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | 16 | # the file COPYING, distributed as part of this software. |
|
17 | 17 | # |
|
18 | 18 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
19 | 19 | # Python standard library. Over time, all of that class has been copied |
|
20 | 20 | # verbatim here for modifications which could not be accomplished by |
|
21 | 21 | # subclassing. At this point, there are no dependencies at all on the code |
|
22 | 22 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
23 | 23 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
24 | 24 | # due. |
|
25 | 25 | #***************************************************************************** |
|
26 | 26 | |
|
27 | 27 | #**************************************************************************** |
|
28 | 28 | # Modules and globals |
|
29 | 29 | |
|
30 | 30 | from IPython import Release |
|
31 | 31 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
32 | 32 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
33 | 33 | __license__ = Release.license |
|
34 | 34 | __version__ = Release.version |
|
35 | 35 | |
|
36 | 36 | # Python standard modules |
|
37 | 37 | import __main__ |
|
38 | 38 | import __builtin__ |
|
39 | 39 | import StringIO |
|
40 | 40 | import bdb |
|
41 | 41 | import cPickle as pickle |
|
42 | 42 | import codeop |
|
43 | 43 | import exceptions |
|
44 | 44 | import glob |
|
45 | 45 | import inspect |
|
46 | 46 | import keyword |
|
47 | 47 | import new |
|
48 | 48 | import os |
|
49 | 49 | import pydoc |
|
50 | 50 | import re |
|
51 | 51 | import shutil |
|
52 | 52 | import string |
|
53 | 53 | import sys |
|
54 | 54 | import tempfile |
|
55 | 55 | import traceback |
|
56 | 56 | import types |
|
57 | 57 | import warnings |
|
58 | 58 | warnings.filterwarnings('ignore', r'.*sets module*') |
|
59 | 59 | from sets import Set |
|
60 | 60 | from pprint import pprint, pformat |
|
61 | 61 | |
|
62 | 62 | # IPython's own modules |
|
63 | 63 | #import IPython |
|
64 | 64 | from IPython import Debugger,OInspect,PyColorize,ultraTB |
|
65 | 65 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
66 | 66 | from IPython.Extensions import pickleshare |
|
67 | 67 | from IPython.FakeModule import FakeModule |
|
68 | 68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
69 | 69 | from IPython.Logger import Logger |
|
70 | 70 | from IPython.Magic import Magic |
|
71 | 71 | from IPython.Prompts import CachedOutput |
|
72 | 72 | from IPython.ipstruct import Struct |
|
73 | 73 | from IPython.background_jobs import BackgroundJobManager |
|
74 | 74 | from IPython.usage import cmd_line_usage,interactive_usage |
|
75 | 75 | from IPython.genutils import * |
|
76 | 76 | from IPython.strdispatch import StrDispatch |
|
77 | 77 | import IPython.ipapi |
|
78 | 78 | import IPython.history |
|
79 | 79 | import IPython.prefilter as prefilter |
|
80 | 80 | import IPython.shadowns |
|
81 | 81 | # Globals |
|
82 | 82 | |
|
83 | 83 | # store the builtin raw_input globally, and use this always, in case user code |
|
84 | 84 | # overwrites it (like wx.py.PyShell does) |
|
85 | 85 | raw_input_original = raw_input |
|
86 | 86 | |
|
87 | 87 | # compiled regexps for autoindent management |
|
88 | 88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | #**************************************************************************** |
|
92 | 92 | # Some utility function definitions |
|
93 | 93 | |
|
94 | 94 | ini_spaces_re = re.compile(r'^(\s+)') |
|
95 | 95 | |
|
96 | 96 | def num_ini_spaces(strng): |
|
97 | 97 | """Return the number of initial spaces in a string""" |
|
98 | 98 | |
|
99 | 99 | ini_spaces = ini_spaces_re.match(strng) |
|
100 | 100 | if ini_spaces: |
|
101 | 101 | return ini_spaces.end() |
|
102 | 102 | else: |
|
103 | 103 | return 0 |
|
104 | 104 | |
|
105 | 105 | def softspace(file, newvalue): |
|
106 | 106 | """Copied from code.py, to remove the dependency""" |
|
107 | 107 | |
|
108 | 108 | oldvalue = 0 |
|
109 | 109 | try: |
|
110 | 110 | oldvalue = file.softspace |
|
111 | 111 | except AttributeError: |
|
112 | 112 | pass |
|
113 | 113 | try: |
|
114 | 114 | file.softspace = newvalue |
|
115 | 115 | except (AttributeError, TypeError): |
|
116 | 116 | # "attribute-less object" or "read-only attributes" |
|
117 | 117 | pass |
|
118 | 118 | return oldvalue |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | #**************************************************************************** |
|
122 | 122 | # Local use exceptions |
|
123 | 123 | class SpaceInInput(exceptions.Exception): pass |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | #**************************************************************************** |
|
127 | 127 | # Local use classes |
|
128 | 128 | class Bunch: pass |
|
129 | 129 | |
|
130 | 130 | class Undefined: pass |
|
131 | 131 | |
|
132 | 132 | class Quitter(object): |
|
133 | 133 | """Simple class to handle exit, similar to Python 2.5's. |
|
134 | 134 | |
|
135 | 135 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
136 | 136 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
137 | 137 | |
|
138 | 138 | def __init__(self,shell,name): |
|
139 | 139 | self.shell = shell |
|
140 | 140 | self.name = name |
|
141 | 141 | |
|
142 | 142 | def __repr__(self): |
|
143 | 143 | return 'Type %s() to exit.' % self.name |
|
144 | 144 | __str__ = __repr__ |
|
145 | 145 | |
|
146 | 146 | def __call__(self): |
|
147 | 147 | self.shell.exit() |
|
148 | 148 | |
|
149 | 149 | class InputList(list): |
|
150 | 150 | """Class to store user input. |
|
151 | 151 | |
|
152 | 152 | It's basically a list, but slices return a string instead of a list, thus |
|
153 | 153 | allowing things like (assuming 'In' is an instance): |
|
154 | 154 | |
|
155 | 155 | exec In[4:7] |
|
156 | 156 | |
|
157 | 157 | or |
|
158 | 158 | |
|
159 | 159 | exec In[5:9] + In[14] + In[21:25]""" |
|
160 | 160 | |
|
161 | 161 | def __getslice__(self,i,j): |
|
162 | 162 | return ''.join(list.__getslice__(self,i,j)) |
|
163 | 163 | |
|
164 | 164 | class SyntaxTB(ultraTB.ListTB): |
|
165 | 165 | """Extension which holds some state: the last exception value""" |
|
166 | 166 | |
|
167 | 167 | def __init__(self,color_scheme = 'NoColor'): |
|
168 | 168 | ultraTB.ListTB.__init__(self,color_scheme) |
|
169 | 169 | self.last_syntax_error = None |
|
170 | 170 | |
|
171 | 171 | def __call__(self, etype, value, elist): |
|
172 | 172 | self.last_syntax_error = value |
|
173 | 173 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
174 | 174 | |
|
175 | 175 | def clear_err_state(self): |
|
176 | 176 | """Return the current error state and clear it""" |
|
177 | 177 | e = self.last_syntax_error |
|
178 | 178 | self.last_syntax_error = None |
|
179 | 179 | return e |
|
180 | 180 | |
|
181 | 181 | #**************************************************************************** |
|
182 | 182 | # Main IPython class |
|
183 | 183 | |
|
184 | 184 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
185 | 185 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
186 | 186 | # attributes and methods, but too much user code out there relies on the |
|
187 | 187 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
188 | 188 | # |
|
189 | 189 | # But at least now, all the pieces have been separated and we could, in |
|
190 | 190 | # principle, stop using the mixin. This will ease the transition to the |
|
191 | 191 | # chainsaw branch. |
|
192 | 192 | |
|
193 | 193 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
194 | 194 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
195 | 195 | # class, to prevent clashes. |
|
196 | 196 | |
|
197 | 197 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
198 | 198 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
199 | 199 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
200 | 200 | # 'self.value'] |
|
201 | 201 | |
|
202 | 202 | class InteractiveShell(object,Magic): |
|
203 | 203 | """An enhanced console for Python.""" |
|
204 | 204 | |
|
205 | 205 | # class attribute to indicate whether the class supports threads or not. |
|
206 | 206 | # Subclasses with thread support should override this as needed. |
|
207 | 207 | isthreaded = False |
|
208 | 208 | |
|
209 | 209 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
210 | 210 | user_ns=None,user_global_ns=None,banner2='', |
|
211 | 211 | custom_exceptions=((),None),embedded=False): |
|
212 | 212 | |
|
213 | 213 | # log system |
|
214 | 214 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
215 | 215 | |
|
216 | 216 | # Job manager (for jobs run as background threads) |
|
217 | 217 | self.jobs = BackgroundJobManager() |
|
218 | 218 | |
|
219 | 219 | # Store the actual shell's name |
|
220 | 220 | self.name = name |
|
221 | 221 | self.more = False |
|
222 | 222 | |
|
223 | 223 | # We need to know whether the instance is meant for embedding, since |
|
224 | 224 | # global/local namespaces need to be handled differently in that case |
|
225 | 225 | self.embedded = embedded |
|
226 | 226 | if embedded: |
|
227 | 227 | # Control variable so users can, from within the embedded instance, |
|
228 | 228 | # permanently deactivate it. |
|
229 | 229 | self.embedded_active = True |
|
230 | 230 | |
|
231 | 231 | # command compiler |
|
232 | 232 | self.compile = codeop.CommandCompiler() |
|
233 | 233 | |
|
234 | 234 | # User input buffer |
|
235 | 235 | self.buffer = [] |
|
236 | 236 | |
|
237 | 237 | # Default name given in compilation of code |
|
238 | 238 | self.filename = '<ipython console>' |
|
239 | 239 | |
|
240 | 240 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
241 | 241 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
242 | 242 | __builtin__.exit = Quitter(self,'exit') |
|
243 | 243 | __builtin__.quit = Quitter(self,'quit') |
|
244 | 244 | |
|
245 | 245 | # Make an empty namespace, which extension writers can rely on both |
|
246 | 246 | # existing and NEVER being used by ipython itself. This gives them a |
|
247 | 247 | # convenient location for storing additional information and state |
|
248 | 248 | # their extensions may require, without fear of collisions with other |
|
249 | 249 | # ipython names that may develop later. |
|
250 | 250 | self.meta = Struct() |
|
251 | 251 | |
|
252 | 252 | # Create the namespace where the user will operate. user_ns is |
|
253 | 253 | # normally the only one used, and it is passed to the exec calls as |
|
254 | 254 | # the locals argument. But we do carry a user_global_ns namespace |
|
255 | 255 | # given as the exec 'globals' argument, This is useful in embedding |
|
256 | 256 | # situations where the ipython shell opens in a context where the |
|
257 | 257 | # distinction between locals and globals is meaningful. For |
|
258 | 258 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
259 | 259 | |
|
260 | 260 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
261 | 261 | # level as a dict instead of a module. This is a manual fix, but I |
|
262 | 262 | # should really track down where the problem is coming from. Alex |
|
263 | 263 | # Schmolck reported this problem first. |
|
264 | 264 | |
|
265 | 265 | # A useful post by Alex Martelli on this topic: |
|
266 | 266 | # Re: inconsistent value from __builtins__ |
|
267 | 267 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
268 | 268 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
269 | 269 | # Gruppen: comp.lang.python |
|
270 | 270 | |
|
271 | 271 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
272 | 272 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
273 | 273 | # > <type 'dict'> |
|
274 | 274 | # > >>> print type(__builtins__) |
|
275 | 275 | # > <type 'module'> |
|
276 | 276 | # > Is this difference in return value intentional? |
|
277 | 277 | |
|
278 | 278 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
279 | 279 | # or a module, and it's been that way for a long time. Whether it's |
|
280 | 280 | # intentional (or sensible), I don't know. In any case, the idea is |
|
281 | 281 | # that if you need to access the built-in namespace directly, you |
|
282 | 282 | # should start with "import __builtin__" (note, no 's') which will |
|
283 | 283 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
284 | 284 | |
|
285 | 285 | # These routines return properly built dicts as needed by the rest of |
|
286 | 286 | # the code, and can also be used by extension writers to generate |
|
287 | 287 | # properly initialized namespaces. |
|
288 | 288 | user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, |
|
289 | 289 | user_global_ns) |
|
290 | 290 | |
|
291 | 291 | # Assign namespaces |
|
292 | 292 | # This is the namespace where all normal user variables live |
|
293 | 293 | self.user_ns = user_ns |
|
294 | 294 | self.user_global_ns = user_global_ns |
|
295 | 295 | # A namespace to keep track of internal data structures to prevent |
|
296 | 296 | # them from cluttering user-visible stuff. Will be updated later |
|
297 | 297 | self.internal_ns = {} |
|
298 | 298 | |
|
299 | 299 | # Namespace of system aliases. Each entry in the alias |
|
300 | 300 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
301 | 301 | # of positional arguments of the alias. |
|
302 | 302 | self.alias_table = {} |
|
303 | 303 | |
|
304 | 304 | # A table holding all the namespaces IPython deals with, so that |
|
305 | 305 | # introspection facilities can search easily. |
|
306 | 306 | self.ns_table = {'user':user_ns, |
|
307 | 307 | 'user_global':user_global_ns, |
|
308 | 308 | 'alias':self.alias_table, |
|
309 | 309 | 'internal':self.internal_ns, |
|
310 | 310 | 'builtin':__builtin__.__dict__ |
|
311 | 311 | } |
|
312 | 312 | # The user namespace MUST have a pointer to the shell itself. |
|
313 | 313 | self.user_ns[name] = self |
|
314 | 314 | |
|
315 | 315 | # We need to insert into sys.modules something that looks like a |
|
316 | 316 | # module but which accesses the IPython namespace, for shelve and |
|
317 | 317 | # pickle to work interactively. Normally they rely on getting |
|
318 | 318 | # everything out of __main__, but for embedding purposes each IPython |
|
319 | 319 | # instance has its own private namespace, so we can't go shoving |
|
320 | 320 | # everything into __main__. |
|
321 | 321 | |
|
322 | 322 | # note, however, that we should only do this for non-embedded |
|
323 | 323 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
324 | 324 | # namespace. Embedded instances, on the other hand, should not do |
|
325 | 325 | # this because they need to manage the user local/global namespaces |
|
326 | 326 | # only, but they live within a 'normal' __main__ (meaning, they |
|
327 | 327 | # shouldn't overtake the execution environment of the script they're |
|
328 | 328 | # embedded in). |
|
329 | 329 | |
|
330 | 330 | if not embedded: |
|
331 | 331 | try: |
|
332 | 332 | main_name = self.user_ns['__name__'] |
|
333 | 333 | except KeyError: |
|
334 | 334 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
335 | 335 | else: |
|
336 | 336 | #print "pickle hack in place" # dbg |
|
337 | 337 | #print 'main_name:',main_name # dbg |
|
338 | 338 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
339 | 339 | |
|
340 | 340 | # Now that FakeModule produces a real module, we've run into a nasty |
|
341 | 341 | # problem: after script execution (via %run), the module where the user |
|
342 | 342 | # code ran is deleted. Now that this object is a true module (needed |
|
343 | 343 | # so docetst and other tools work correctly), the Python module |
|
344 | 344 | # teardown mechanism runs over it, and sets to None every variable |
|
345 | 345 | # present in that module. This means that later calls to functions |
|
346 | 346 | # defined in the script (which have become interactively visible after |
|
347 | 347 | # script exit) fail, because they hold references to objects that have |
|
348 | 348 | # become overwritten into None. The only solution I see right now is |
|
349 | 349 | # to protect every FakeModule used by %run by holding an internal |
|
350 | 350 | # reference to it. This private list will be used for that. The |
|
351 | 351 | # %reset command will flush it as well. |
|
352 | 352 | self._user_main_modules = [] |
|
353 | 353 | |
|
354 | 354 | # List of input with multi-line handling. |
|
355 | 355 | # Fill its zero entry, user counter starts at 1 |
|
356 | 356 | self.input_hist = InputList(['\n']) |
|
357 | 357 | # This one will hold the 'raw' input history, without any |
|
358 | 358 | # pre-processing. This will allow users to retrieve the input just as |
|
359 | 359 | # it was exactly typed in by the user, with %hist -r. |
|
360 | 360 | self.input_hist_raw = InputList(['\n']) |
|
361 | 361 | |
|
362 | 362 | # list of visited directories |
|
363 | 363 | try: |
|
364 | 364 | self.dir_hist = [os.getcwd()] |
|
365 | 365 | except OSError: |
|
366 | 366 | self.dir_hist = [] |
|
367 | 367 | |
|
368 | 368 | # dict of output history |
|
369 | 369 | self.output_hist = {} |
|
370 | 370 | |
|
371 | 371 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
372 | 372 | # under Win32 have it set to None, and we need to have a known valid |
|
373 | 373 | # encoding to use in the raw_input() method |
|
374 | 374 | try: |
|
375 | 375 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
376 | 376 | except AttributeError: |
|
377 | 377 | self.stdin_encoding = 'ascii' |
|
378 | 378 | |
|
379 | 379 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
380 | 380 | no_alias = {} |
|
381 | 381 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
382 | 382 | for key in keyword.kwlist + no_alias_magics: |
|
383 | 383 | no_alias[key] = 1 |
|
384 | 384 | no_alias.update(__builtin__.__dict__) |
|
385 | 385 | self.no_alias = no_alias |
|
386 | 386 | |
|
387 | 387 | # make global variables for user access to these |
|
388 | 388 | self.user_ns['_ih'] = self.input_hist |
|
389 | 389 | self.user_ns['_oh'] = self.output_hist |
|
390 | 390 | self.user_ns['_dh'] = self.dir_hist |
|
391 | 391 | |
|
392 | 392 | # user aliases to input and output histories |
|
393 | 393 | self.user_ns['In'] = self.input_hist |
|
394 | 394 | self.user_ns['Out'] = self.output_hist |
|
395 | 395 | |
|
396 | 396 | self.user_ns['_sh'] = IPython.shadowns |
|
397 | 397 | # Object variable to store code object waiting execution. This is |
|
398 | 398 | # used mainly by the multithreaded shells, but it can come in handy in |
|
399 | 399 | # other situations. No need to use a Queue here, since it's a single |
|
400 | 400 | # item which gets cleared once run. |
|
401 | 401 | self.code_to_run = None |
|
402 | 402 | |
|
403 | 403 | # escapes for automatic behavior on the command line |
|
404 | 404 | self.ESC_SHELL = '!' |
|
405 | 405 | self.ESC_SH_CAP = '!!' |
|
406 | 406 | self.ESC_HELP = '?' |
|
407 | 407 | self.ESC_MAGIC = '%' |
|
408 | 408 | self.ESC_QUOTE = ',' |
|
409 | 409 | self.ESC_QUOTE2 = ';' |
|
410 | 410 | self.ESC_PAREN = '/' |
|
411 | 411 | |
|
412 | 412 | # And their associated handlers |
|
413 | 413 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
414 | 414 | self.ESC_QUOTE : self.handle_auto, |
|
415 | 415 | self.ESC_QUOTE2 : self.handle_auto, |
|
416 | 416 | self.ESC_MAGIC : self.handle_magic, |
|
417 | 417 | self.ESC_HELP : self.handle_help, |
|
418 | 418 | self.ESC_SHELL : self.handle_shell_escape, |
|
419 | 419 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
420 | 420 | } |
|
421 | 421 | |
|
422 | 422 | # class initializations |
|
423 | 423 | Magic.__init__(self,self) |
|
424 | 424 | |
|
425 | 425 | # Python source parser/formatter for syntax highlighting |
|
426 | 426 | pyformat = PyColorize.Parser().format |
|
427 | 427 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
428 | 428 | |
|
429 | 429 | # hooks holds pointers used for user-side customizations |
|
430 | 430 | self.hooks = Struct() |
|
431 | 431 | |
|
432 | 432 | self.strdispatchers = {} |
|
433 | 433 | |
|
434 | 434 | # Set all default hooks, defined in the IPython.hooks module. |
|
435 | 435 | hooks = IPython.hooks |
|
436 | 436 | for hook_name in hooks.__all__: |
|
437 | 437 | # default hooks have priority 100, i.e. low; user hooks should have |
|
438 | 438 | # 0-100 priority |
|
439 | 439 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
440 | 440 | #print "bound hook",hook_name |
|
441 | 441 | |
|
442 | 442 | # Flag to mark unconditional exit |
|
443 | 443 | self.exit_now = False |
|
444 | 444 | |
|
445 | 445 | self.usage_min = """\ |
|
446 | 446 | An enhanced console for Python. |
|
447 | 447 | Some of its features are: |
|
448 | 448 | - Readline support if the readline library is present. |
|
449 | 449 | - Tab completion in the local namespace. |
|
450 | 450 | - Logging of input, see command-line options. |
|
451 | 451 | - System shell escape via ! , eg !ls. |
|
452 | 452 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
453 | 453 | - Keeps track of locally defined variables via %who, %whos. |
|
454 | 454 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
455 | 455 | """ |
|
456 | 456 | if usage: self.usage = usage |
|
457 | 457 | else: self.usage = self.usage_min |
|
458 | 458 | |
|
459 | 459 | # Storage |
|
460 | 460 | self.rc = rc # This will hold all configuration information |
|
461 | 461 | self.pager = 'less' |
|
462 | 462 | # temporary files used for various purposes. Deleted at exit. |
|
463 | 463 | self.tempfiles = [] |
|
464 | 464 | |
|
465 | 465 | # Keep track of readline usage (later set by init_readline) |
|
466 | 466 | self.has_readline = False |
|
467 | 467 | |
|
468 | 468 | # template for logfile headers. It gets resolved at runtime by the |
|
469 | 469 | # logstart method. |
|
470 | 470 | self.loghead_tpl = \ |
|
471 | 471 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
472 | 472 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
473 | 473 | #log# opts = %s |
|
474 | 474 | #log# args = %s |
|
475 | 475 | #log# It is safe to make manual edits below here. |
|
476 | 476 | #log#----------------------------------------------------------------------- |
|
477 | 477 | """ |
|
478 | 478 | # for pushd/popd management |
|
479 | 479 | try: |
|
480 | 480 | self.home_dir = get_home_dir() |
|
481 | 481 | except HomeDirError,msg: |
|
482 | 482 | fatal(msg) |
|
483 | 483 | |
|
484 | 484 | self.dir_stack = [] |
|
485 | 485 | |
|
486 | 486 | # Functions to call the underlying shell. |
|
487 | 487 | |
|
488 | 488 | # The first is similar to os.system, but it doesn't return a value, |
|
489 | 489 | # and it allows interpolation of variables in the user's namespace. |
|
490 | 490 | self.system = lambda cmd: \ |
|
491 | 491 | self.hooks.shell_hook(self.var_expand(cmd,depth=2)) |
|
492 | 492 | |
|
493 | 493 | # These are for getoutput and getoutputerror: |
|
494 | 494 | self.getoutput = lambda cmd: \ |
|
495 | 495 | getoutput(self.var_expand(cmd,depth=2), |
|
496 | 496 | header=self.rc.system_header, |
|
497 | 497 | verbose=self.rc.system_verbose) |
|
498 | 498 | |
|
499 | 499 | self.getoutputerror = lambda cmd: \ |
|
500 | 500 | getoutputerror(self.var_expand(cmd,depth=2), |
|
501 | 501 | header=self.rc.system_header, |
|
502 | 502 | verbose=self.rc.system_verbose) |
|
503 | 503 | |
|
504 | 504 | |
|
505 | 505 | # keep track of where we started running (mainly for crash post-mortem) |
|
506 | 506 | self.starting_dir = os.getcwd() |
|
507 | 507 | |
|
508 | 508 | # Various switches which can be set |
|
509 | 509 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
510 | 510 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
511 | 511 | self.banner2 = banner2 |
|
512 | 512 | |
|
513 | 513 | # TraceBack handlers: |
|
514 | 514 | |
|
515 | 515 | # Syntax error handler. |
|
516 | 516 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
517 | 517 | |
|
518 | 518 | # The interactive one is initialized with an offset, meaning we always |
|
519 | 519 | # want to remove the topmost item in the traceback, which is our own |
|
520 | 520 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
521 | 521 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
522 | 522 | color_scheme='NoColor', |
|
523 | 523 | tb_offset = 1) |
|
524 | 524 | |
|
525 | 525 | # IPython itself shouldn't crash. This will produce a detailed |
|
526 | 526 | # post-mortem if it does. But we only install the crash handler for |
|
527 | 527 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
528 | 528 | # and lose the crash handler. This is because exceptions in the main |
|
529 | 529 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
530 | 530 | # and there's no point in printing crash dumps for every user exception. |
|
531 | 531 | if self.isthreaded: |
|
532 | 532 | ipCrashHandler = ultraTB.FormattedTB() |
|
533 | 533 | else: |
|
534 | 534 | from IPython import CrashHandler |
|
535 | 535 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
536 | 536 | self.set_crash_handler(ipCrashHandler) |
|
537 | 537 | |
|
538 | 538 | # and add any custom exception handlers the user may have specified |
|
539 | 539 | self.set_custom_exc(*custom_exceptions) |
|
540 | 540 | |
|
541 | 541 | # indentation management |
|
542 | 542 | self.autoindent = False |
|
543 | 543 | self.indent_current_nsp = 0 |
|
544 | 544 | |
|
545 | 545 | # Make some aliases automatically |
|
546 | 546 | # Prepare list of shell aliases to auto-define |
|
547 | 547 | if os.name == 'posix': |
|
548 | 548 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
549 | 549 | 'mv mv -i','rm rm -i','cp cp -i', |
|
550 | 550 | 'cat cat','less less','clear clear', |
|
551 | 551 | # a better ls |
|
552 | 552 | 'ls ls -F', |
|
553 | 553 | # long ls |
|
554 | 554 | 'll ls -lF') |
|
555 | 555 | # Extra ls aliases with color, which need special treatment on BSD |
|
556 | 556 | # variants |
|
557 | 557 | ls_extra = ( # color ls |
|
558 | 558 | 'lc ls -F -o --color', |
|
559 | 559 | # ls normal files only |
|
560 | 560 | 'lf ls -F -o --color %l | grep ^-', |
|
561 | 561 | # ls symbolic links |
|
562 | 562 | 'lk ls -F -o --color %l | grep ^l', |
|
563 | 563 | # directories or links to directories, |
|
564 | 564 | 'ldir ls -F -o --color %l | grep /$', |
|
565 | 565 | # things which are executable |
|
566 | 566 | 'lx ls -F -o --color %l | grep ^-..x', |
|
567 | 567 | ) |
|
568 | 568 | # The BSDs don't ship GNU ls, so they don't understand the |
|
569 | 569 | # --color switch out of the box |
|
570 | 570 | if 'bsd' in sys.platform: |
|
571 | 571 | ls_extra = ( # ls normal files only |
|
572 | 572 | 'lf ls -lF | grep ^-', |
|
573 | 573 | # ls symbolic links |
|
574 | 574 | 'lk ls -lF | grep ^l', |
|
575 | 575 | # directories or links to directories, |
|
576 | 576 | 'ldir ls -lF | grep /$', |
|
577 | 577 | # things which are executable |
|
578 | 578 | 'lx ls -lF | grep ^-..x', |
|
579 | 579 | ) |
|
580 | 580 | auto_alias = auto_alias + ls_extra |
|
581 | 581 | elif os.name in ['nt','dos']: |
|
582 | 582 | auto_alias = ('ls dir /on', |
|
583 | 583 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
584 | 584 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
585 | 585 | 'ren ren','cls cls','copy copy') |
|
586 | 586 | else: |
|
587 | 587 | auto_alias = () |
|
588 | 588 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
589 | 589 | |
|
590 | 590 | |
|
591 | 591 | # Produce a public API instance |
|
592 | 592 | self.api = IPython.ipapi.IPApi(self) |
|
593 | 593 | |
|
594 | 594 | # Call the actual (public) initializer |
|
595 | 595 | self.init_auto_alias() |
|
596 | 596 | |
|
597 | 597 | # track which builtins we add, so we can clean up later |
|
598 | 598 | self.builtins_added = {} |
|
599 | 599 | # This method will add the necessary builtins for operation, but |
|
600 | 600 | # tracking what it did via the builtins_added dict. |
|
601 | 601 | |
|
602 | 602 | #TODO: remove this, redundant |
|
603 | 603 | self.add_builtins() |
|
604 | 604 | |
|
605 | 605 | |
|
606 | 606 | |
|
607 | 607 | |
|
608 | 608 | # end __init__ |
|
609 | 609 | |
|
610 | 610 | def var_expand(self,cmd,depth=0): |
|
611 | 611 | """Expand python variables in a string. |
|
612 | 612 | |
|
613 | 613 | The depth argument indicates how many frames above the caller should |
|
614 | 614 | be walked to look for the local namespace where to expand variables. |
|
615 | 615 | |
|
616 | 616 | The global namespace for expansion is always the user's interactive |
|
617 | 617 | namespace. |
|
618 | 618 | """ |
|
619 | 619 | |
|
620 | 620 | return str(ItplNS(cmd, |
|
621 | 621 | self.user_ns, # globals |
|
622 | 622 | # Skip our own frame in searching for locals: |
|
623 | 623 | sys._getframe(depth+1).f_locals # locals |
|
624 | 624 | )) |
|
625 | 625 | |
|
626 | 626 | def pre_config_initialization(self): |
|
627 | 627 | """Pre-configuration init method |
|
628 | 628 | |
|
629 | 629 | This is called before the configuration files are processed to |
|
630 | 630 | prepare the services the config files might need. |
|
631 | 631 | |
|
632 | 632 | self.rc already has reasonable default values at this point. |
|
633 | 633 | """ |
|
634 | 634 | rc = self.rc |
|
635 | 635 | try: |
|
636 | 636 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
637 | 637 | except exceptions.UnicodeDecodeError: |
|
638 | 638 | print "Your ipythondir can't be decoded to unicode!" |
|
639 | 639 | print "Please set HOME environment variable to something that" |
|
640 | 640 | print r"only has ASCII characters, e.g. c:\home" |
|
641 | 641 | print "Now it is",rc.ipythondir |
|
642 | 642 | sys.exit() |
|
643 | 643 | self.shadowhist = IPython.history.ShadowHist(self.db) |
|
644 | 644 | |
|
645 | 645 | |
|
646 | 646 | def post_config_initialization(self): |
|
647 | 647 | """Post configuration init method |
|
648 | 648 | |
|
649 | 649 | This is called after the configuration files have been processed to |
|
650 | 650 | 'finalize' the initialization.""" |
|
651 | 651 | |
|
652 | 652 | rc = self.rc |
|
653 | 653 | |
|
654 | 654 | # Object inspector |
|
655 | 655 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
656 | 656 | PyColorize.ANSICodeColors, |
|
657 | 657 | 'NoColor', |
|
658 | 658 | rc.object_info_string_level) |
|
659 | 659 | |
|
660 | 660 | self.rl_next_input = None |
|
661 | 661 | self.rl_do_indent = False |
|
662 | 662 | # Load readline proper |
|
663 | 663 | if rc.readline: |
|
664 | 664 | self.init_readline() |
|
665 | 665 | |
|
666 | 666 | |
|
667 | 667 | # local shortcut, this is used a LOT |
|
668 | 668 | self.log = self.logger.log |
|
669 | 669 | |
|
670 | 670 | # Initialize cache, set in/out prompts and printing system |
|
671 | 671 | self.outputcache = CachedOutput(self, |
|
672 | 672 | rc.cache_size, |
|
673 | 673 | rc.pprint, |
|
674 | 674 | input_sep = rc.separate_in, |
|
675 | 675 | output_sep = rc.separate_out, |
|
676 | 676 | output_sep2 = rc.separate_out2, |
|
677 | 677 | ps1 = rc.prompt_in1, |
|
678 | 678 | ps2 = rc.prompt_in2, |
|
679 | 679 | ps_out = rc.prompt_out, |
|
680 | 680 | pad_left = rc.prompts_pad_left) |
|
681 | 681 | |
|
682 | 682 | # user may have over-ridden the default print hook: |
|
683 | 683 | try: |
|
684 | 684 | self.outputcache.__class__.display = self.hooks.display |
|
685 | 685 | except AttributeError: |
|
686 | 686 | pass |
|
687 | 687 | |
|
688 | 688 | # I don't like assigning globally to sys, because it means when |
|
689 | 689 | # embedding instances, each embedded instance overrides the previous |
|
690 | 690 | # choice. But sys.displayhook seems to be called internally by exec, |
|
691 | 691 | # so I don't see a way around it. We first save the original and then |
|
692 | 692 | # overwrite it. |
|
693 | 693 | self.sys_displayhook = sys.displayhook |
|
694 | 694 | sys.displayhook = self.outputcache |
|
695 | 695 | |
|
696 | 696 | # Do a proper resetting of doctest, including the necessary displayhook |
|
697 | 697 | # monkeypatching |
|
698 | 698 | try: |
|
699 | 699 | doctest_reload() |
|
700 | 700 | except ImportError: |
|
701 | 701 | warn("doctest module does not exist.") |
|
702 | 702 | |
|
703 | 703 | # Set user colors (don't do it in the constructor above so that it |
|
704 | 704 | # doesn't crash if colors option is invalid) |
|
705 | 705 | self.magic_colors(rc.colors) |
|
706 | 706 | |
|
707 | 707 | # Set calling of pdb on exceptions |
|
708 | 708 | self.call_pdb = rc.pdb |
|
709 | 709 | |
|
710 | 710 | # Load user aliases |
|
711 | 711 | for alias in rc.alias: |
|
712 | 712 | self.magic_alias(alias) |
|
713 | 713 | |
|
714 | 714 | self.hooks.late_startup_hook() |
|
715 | 715 | |
|
716 | 716 | for cmd in self.rc.autoexec: |
|
717 | 717 | #print "autoexec>",cmd #dbg |
|
718 | 718 | self.api.runlines(cmd) |
|
719 | 719 | |
|
720 | 720 | batchrun = False |
|
721 | 721 | for batchfile in [path(arg) for arg in self.rc.args |
|
722 | 722 | if arg.lower().endswith('.ipy')]: |
|
723 | 723 | if not batchfile.isfile(): |
|
724 | 724 | print "No such batch file:", batchfile |
|
725 | 725 | continue |
|
726 | 726 | self.api.runlines(batchfile.text()) |
|
727 | 727 | batchrun = True |
|
728 | 728 | # without -i option, exit after running the batch file |
|
729 | 729 | if batchrun and not self.rc.interact: |
|
730 | 730 | self.ask_exit() |
|
731 | 731 | |
|
732 | 732 | def add_builtins(self): |
|
733 | 733 | """Store ipython references into the builtin namespace. |
|
734 | 734 | |
|
735 | 735 | Some parts of ipython operate via builtins injected here, which hold a |
|
736 | 736 | reference to IPython itself.""" |
|
737 | 737 | |
|
738 | 738 | # TODO: deprecate all of these, they are unsafe |
|
739 | 739 | builtins_new = dict(__IPYTHON__ = self, |
|
740 | 740 | ip_set_hook = self.set_hook, |
|
741 | 741 | jobs = self.jobs, |
|
742 | 742 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), |
|
743 | 743 | ipalias = wrap_deprecated(self.ipalias), |
|
744 | 744 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), |
|
745 | 745 | #_ip = self.api |
|
746 | 746 | ) |
|
747 | 747 | for biname,bival in builtins_new.items(): |
|
748 | 748 | try: |
|
749 | 749 | # store the orignal value so we can restore it |
|
750 | 750 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
751 | 751 | except KeyError: |
|
752 | 752 | # or mark that it wasn't defined, and we'll just delete it at |
|
753 | 753 | # cleanup |
|
754 | 754 | self.builtins_added[biname] = Undefined |
|
755 | 755 | __builtin__.__dict__[biname] = bival |
|
756 | 756 | |
|
757 | 757 | # Keep in the builtins a flag for when IPython is active. We set it |
|
758 | 758 | # with setdefault so that multiple nested IPythons don't clobber one |
|
759 | 759 | # another. Each will increase its value by one upon being activated, |
|
760 | 760 | # which also gives us a way to determine the nesting level. |
|
761 | 761 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
762 | 762 | |
|
763 | 763 | def clean_builtins(self): |
|
764 | 764 | """Remove any builtins which might have been added by add_builtins, or |
|
765 | 765 | restore overwritten ones to their previous values.""" |
|
766 | 766 | for biname,bival in self.builtins_added.items(): |
|
767 | 767 | if bival is Undefined: |
|
768 | 768 | del __builtin__.__dict__[biname] |
|
769 | 769 | else: |
|
770 | 770 | __builtin__.__dict__[biname] = bival |
|
771 | 771 | self.builtins_added.clear() |
|
772 | 772 | |
|
773 | 773 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
774 | 774 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
775 | 775 | |
|
776 | 776 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
777 | 777 | adding your function to one of these hooks, you can modify IPython's |
|
778 | 778 | behavior to call at runtime your own routines.""" |
|
779 | 779 | |
|
780 | 780 | # At some point in the future, this should validate the hook before it |
|
781 | 781 | # accepts it. Probably at least check that the hook takes the number |
|
782 | 782 | # of args it's supposed to. |
|
783 | 783 | |
|
784 | 784 | f = new.instancemethod(hook,self,self.__class__) |
|
785 | 785 | |
|
786 | 786 | # check if the hook is for strdispatcher first |
|
787 | 787 | if str_key is not None: |
|
788 | 788 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
789 | 789 | sdp.add_s(str_key, f, priority ) |
|
790 | 790 | self.strdispatchers[name] = sdp |
|
791 | 791 | return |
|
792 | 792 | if re_key is not None: |
|
793 | 793 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
794 | 794 | sdp.add_re(re.compile(re_key), f, priority ) |
|
795 | 795 | self.strdispatchers[name] = sdp |
|
796 | 796 | return |
|
797 | 797 | |
|
798 | 798 | dp = getattr(self.hooks, name, None) |
|
799 | 799 | if name not in IPython.hooks.__all__: |
|
800 | 800 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
801 | 801 | if not dp: |
|
802 | 802 | dp = IPython.hooks.CommandChainDispatcher() |
|
803 | 803 | |
|
804 | 804 | try: |
|
805 | 805 | dp.add(f,priority) |
|
806 | 806 | except AttributeError: |
|
807 | 807 | # it was not commandchain, plain old func - replace |
|
808 | 808 | dp = f |
|
809 | 809 | |
|
810 | 810 | setattr(self.hooks,name, dp) |
|
811 | 811 | |
|
812 | 812 | |
|
813 | 813 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
814 | 814 | |
|
815 | 815 | def set_crash_handler(self,crashHandler): |
|
816 | 816 | """Set the IPython crash handler. |
|
817 | 817 | |
|
818 | 818 | This must be a callable with a signature suitable for use as |
|
819 | 819 | sys.excepthook.""" |
|
820 | 820 | |
|
821 | 821 | # Install the given crash handler as the Python exception hook |
|
822 | 822 | sys.excepthook = crashHandler |
|
823 | 823 | |
|
824 | 824 | # The instance will store a pointer to this, so that runtime code |
|
825 | 825 | # (such as magics) can access it. This is because during the |
|
826 | 826 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
827 | 827 | # frameworks). |
|
828 | 828 | self.sys_excepthook = sys.excepthook |
|
829 | 829 | |
|
830 | 830 | |
|
831 | 831 | def set_custom_exc(self,exc_tuple,handler): |
|
832 | 832 | """set_custom_exc(exc_tuple,handler) |
|
833 | 833 | |
|
834 | 834 | Set a custom exception handler, which will be called if any of the |
|
835 | 835 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
836 | 836 | runcode() method. |
|
837 | 837 | |
|
838 | 838 | Inputs: |
|
839 | 839 | |
|
840 | 840 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
841 | 841 | handler for. It is very important that you use a tuple, and NOT A |
|
842 | 842 | LIST here, because of the way Python's except statement works. If |
|
843 | 843 | you only want to trap a single exception, use a singleton tuple: |
|
844 | 844 | |
|
845 | 845 | exc_tuple == (MyCustomException,) |
|
846 | 846 | |
|
847 | 847 | - handler: this must be defined as a function with the following |
|
848 | 848 | basic interface: def my_handler(self,etype,value,tb). |
|
849 | 849 | |
|
850 | 850 | This will be made into an instance method (via new.instancemethod) |
|
851 | 851 | of IPython itself, and it will be called if any of the exceptions |
|
852 | 852 | listed in the exc_tuple are caught. If the handler is None, an |
|
853 | 853 | internal basic one is used, which just prints basic info. |
|
854 | 854 | |
|
855 | 855 | WARNING: by putting in your own exception handler into IPython's main |
|
856 | 856 | execution loop, you run a very good chance of nasty crashes. This |
|
857 | 857 | facility should only be used if you really know what you are doing.""" |
|
858 | 858 | |
|
859 | 859 | assert type(exc_tuple)==type(()) , \ |
|
860 | 860 | "The custom exceptions must be given AS A TUPLE." |
|
861 | 861 | |
|
862 | 862 | def dummy_handler(self,etype,value,tb): |
|
863 | 863 | print '*** Simple custom exception handler ***' |
|
864 | 864 | print 'Exception type :',etype |
|
865 | 865 | print 'Exception value:',value |
|
866 | 866 | print 'Traceback :',tb |
|
867 | 867 | print 'Source code :','\n'.join(self.buffer) |
|
868 | 868 | |
|
869 | 869 | if handler is None: handler = dummy_handler |
|
870 | 870 | |
|
871 | 871 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
872 | 872 | self.custom_exceptions = exc_tuple |
|
873 | 873 | |
|
874 | 874 | def set_custom_completer(self,completer,pos=0): |
|
875 | 875 | """set_custom_completer(completer,pos=0) |
|
876 | 876 | |
|
877 | 877 | Adds a new custom completer function. |
|
878 | 878 | |
|
879 | 879 | The position argument (defaults to 0) is the index in the completers |
|
880 | 880 | list where you want the completer to be inserted.""" |
|
881 | 881 | |
|
882 | 882 | newcomp = new.instancemethod(completer,self.Completer, |
|
883 | 883 | self.Completer.__class__) |
|
884 | 884 | self.Completer.matchers.insert(pos,newcomp) |
|
885 | 885 | |
|
886 | 886 | def set_completer(self): |
|
887 | 887 | """reset readline's completer to be our own.""" |
|
888 | 888 | self.readline.set_completer(self.Completer.complete) |
|
889 | 889 | |
|
890 | 890 | def _get_call_pdb(self): |
|
891 | 891 | return self._call_pdb |
|
892 | 892 | |
|
893 | 893 | def _set_call_pdb(self,val): |
|
894 | 894 | |
|
895 | 895 | if val not in (0,1,False,True): |
|
896 | 896 | raise ValueError,'new call_pdb value must be boolean' |
|
897 | 897 | |
|
898 | 898 | # store value in instance |
|
899 | 899 | self._call_pdb = val |
|
900 | 900 | |
|
901 | 901 | # notify the actual exception handlers |
|
902 | 902 | self.InteractiveTB.call_pdb = val |
|
903 | 903 | if self.isthreaded: |
|
904 | 904 | try: |
|
905 | 905 | self.sys_excepthook.call_pdb = val |
|
906 | 906 | except: |
|
907 | 907 | warn('Failed to activate pdb for threaded exception handler') |
|
908 | 908 | |
|
909 | 909 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
910 | 910 | 'Control auto-activation of pdb at exceptions') |
|
911 | 911 | |
|
912 | 912 | |
|
913 | 913 | # These special functions get installed in the builtin namespace, to |
|
914 | 914 | # provide programmatic (pure python) access to magics, aliases and system |
|
915 | 915 | # calls. This is important for logging, user scripting, and more. |
|
916 | 916 | |
|
917 | 917 | # We are basically exposing, via normal python functions, the three |
|
918 | 918 | # mechanisms in which ipython offers special call modes (magics for |
|
919 | 919 | # internal control, aliases for direct system access via pre-selected |
|
920 | 920 | # names, and !cmd for calling arbitrary system commands). |
|
921 | 921 | |
|
922 | 922 | def ipmagic(self,arg_s): |
|
923 | 923 | """Call a magic function by name. |
|
924 | 924 | |
|
925 | 925 | Input: a string containing the name of the magic function to call and any |
|
926 | 926 | additional arguments to be passed to the magic. |
|
927 | 927 | |
|
928 | 928 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
929 | 929 | prompt: |
|
930 | 930 | |
|
931 | 931 | In[1]: %name -opt foo bar |
|
932 | 932 | |
|
933 | 933 | To call a magic without arguments, simply use ipmagic('name'). |
|
934 | 934 | |
|
935 | 935 | This provides a proper Python function to call IPython's magics in any |
|
936 | 936 | valid Python code you can type at the interpreter, including loops and |
|
937 | 937 | compound statements. It is added by IPython to the Python builtin |
|
938 | 938 | namespace upon initialization.""" |
|
939 | 939 | |
|
940 | 940 | args = arg_s.split(' ',1) |
|
941 | 941 | magic_name = args[0] |
|
942 | 942 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
943 | 943 | |
|
944 | 944 | try: |
|
945 | 945 | magic_args = args[1] |
|
946 | 946 | except IndexError: |
|
947 | 947 | magic_args = '' |
|
948 | 948 | fn = getattr(self,'magic_'+magic_name,None) |
|
949 | 949 | if fn is None: |
|
950 | 950 | error("Magic function `%s` not found." % magic_name) |
|
951 | 951 | else: |
|
952 | 952 | magic_args = self.var_expand(magic_args,1) |
|
953 | 953 | return fn(magic_args) |
|
954 | 954 | |
|
955 | 955 | def ipalias(self,arg_s): |
|
956 | 956 | """Call an alias by name. |
|
957 | 957 | |
|
958 | 958 | Input: a string containing the name of the alias to call and any |
|
959 | 959 | additional arguments to be passed to the magic. |
|
960 | 960 | |
|
961 | 961 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
962 | 962 | prompt: |
|
963 | 963 | |
|
964 | 964 | In[1]: name -opt foo bar |
|
965 | 965 | |
|
966 | 966 | To call an alias without arguments, simply use ipalias('name'). |
|
967 | 967 | |
|
968 | 968 | This provides a proper Python function to call IPython's aliases in any |
|
969 | 969 | valid Python code you can type at the interpreter, including loops and |
|
970 | 970 | compound statements. It is added by IPython to the Python builtin |
|
971 | 971 | namespace upon initialization.""" |
|
972 | 972 | |
|
973 | 973 | args = arg_s.split(' ',1) |
|
974 | 974 | alias_name = args[0] |
|
975 | 975 | try: |
|
976 | 976 | alias_args = args[1] |
|
977 | 977 | except IndexError: |
|
978 | 978 | alias_args = '' |
|
979 | 979 | if alias_name in self.alias_table: |
|
980 | 980 | self.call_alias(alias_name,alias_args) |
|
981 | 981 | else: |
|
982 | 982 | error("Alias `%s` not found." % alias_name) |
|
983 | 983 | |
|
984 | 984 | def ipsystem(self,arg_s): |
|
985 | 985 | """Make a system call, using IPython.""" |
|
986 | 986 | |
|
987 | 987 | self.system(arg_s) |
|
988 | 988 | |
|
989 | 989 | def complete(self,text): |
|
990 | 990 | """Return a sorted list of all possible completions on text. |
|
991 | 991 | |
|
992 | 992 | Inputs: |
|
993 | 993 | |
|
994 | 994 | - text: a string of text to be completed on. |
|
995 | 995 | |
|
996 | 996 | This is a wrapper around the completion mechanism, similar to what |
|
997 | 997 | readline does at the command line when the TAB key is hit. By |
|
998 | 998 | exposing it as a method, it can be used by other non-readline |
|
999 | 999 | environments (such as GUIs) for text completion. |
|
1000 | 1000 | |
|
1001 | 1001 | Simple usage example: |
|
1002 | 1002 | |
|
1003 | 1003 | In [7]: x = 'hello' |
|
1004 | 1004 | |
|
1005 | 1005 | In [8]: x |
|
1006 | 1006 | Out[8]: 'hello' |
|
1007 | 1007 | |
|
1008 | 1008 | In [9]: print x |
|
1009 | 1009 | hello |
|
1010 | 1010 | |
|
1011 | 1011 | In [10]: _ip.IP.complete('x.l') |
|
1012 | 1012 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1013 | 1013 | """ |
|
1014 | 1014 | |
|
1015 | 1015 | complete = self.Completer.complete |
|
1016 | 1016 | state = 0 |
|
1017 | 1017 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1018 | 1018 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1019 | 1019 | # start using sets instead, which are faster. |
|
1020 | 1020 | comps = {} |
|
1021 | 1021 | while True: |
|
1022 | 1022 | newcomp = complete(text,state,line_buffer=text) |
|
1023 | 1023 | if newcomp is None: |
|
1024 | 1024 | break |
|
1025 | 1025 | comps[newcomp] = 1 |
|
1026 | 1026 | state += 1 |
|
1027 | 1027 | outcomps = comps.keys() |
|
1028 | 1028 | outcomps.sort() |
|
1029 | 1029 | #print "T:",text,"OC:",outcomps # dbg |
|
1030 | 1030 | #print "vars:",self.user_ns.keys() |
|
1031 | 1031 | return outcomps |
|
1032 | 1032 | |
|
1033 | 1033 | def set_completer_frame(self, frame=None): |
|
1034 | 1034 | if frame: |
|
1035 | 1035 | self.Completer.namespace = frame.f_locals |
|
1036 | 1036 | self.Completer.global_namespace = frame.f_globals |
|
1037 | 1037 | else: |
|
1038 | 1038 | self.Completer.namespace = self.user_ns |
|
1039 | 1039 | self.Completer.global_namespace = self.user_global_ns |
|
1040 | 1040 | |
|
1041 | 1041 | def init_auto_alias(self): |
|
1042 | 1042 | """Define some aliases automatically. |
|
1043 | 1043 | |
|
1044 | 1044 | These are ALL parameter-less aliases""" |
|
1045 | 1045 | |
|
1046 | 1046 | for alias,cmd in self.auto_alias: |
|
1047 | 1047 | self.getapi().defalias(alias,cmd) |
|
1048 | 1048 | |
|
1049 | 1049 | |
|
1050 | 1050 | def alias_table_validate(self,verbose=0): |
|
1051 | 1051 | """Update information about the alias table. |
|
1052 | 1052 | |
|
1053 | 1053 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1054 | 1054 | |
|
1055 | 1055 | no_alias = self.no_alias |
|
1056 | 1056 | for k in self.alias_table.keys(): |
|
1057 | 1057 | if k in no_alias: |
|
1058 | 1058 | del self.alias_table[k] |
|
1059 | 1059 | if verbose: |
|
1060 | 1060 | print ("Deleting alias <%s>, it's a Python " |
|
1061 | 1061 | "keyword or builtin." % k) |
|
1062 | 1062 | |
|
1063 | 1063 | def set_autoindent(self,value=None): |
|
1064 | 1064 | """Set the autoindent flag, checking for readline support. |
|
1065 | 1065 | |
|
1066 | 1066 | If called with no arguments, it acts as a toggle.""" |
|
1067 | 1067 | |
|
1068 | 1068 | if not self.has_readline: |
|
1069 | 1069 | if os.name == 'posix': |
|
1070 | 1070 | warn("The auto-indent feature requires the readline library") |
|
1071 | 1071 | self.autoindent = 0 |
|
1072 | 1072 | return |
|
1073 | 1073 | if value is None: |
|
1074 | 1074 | self.autoindent = not self.autoindent |
|
1075 | 1075 | else: |
|
1076 | 1076 | self.autoindent = value |
|
1077 | 1077 | |
|
1078 | 1078 | def rc_set_toggle(self,rc_field,value=None): |
|
1079 | 1079 | """Set or toggle a field in IPython's rc config. structure. |
|
1080 | 1080 | |
|
1081 | 1081 | If called with no arguments, it acts as a toggle. |
|
1082 | 1082 | |
|
1083 | 1083 | If called with a non-existent field, the resulting AttributeError |
|
1084 | 1084 | exception will propagate out.""" |
|
1085 | 1085 | |
|
1086 | 1086 | rc_val = getattr(self.rc,rc_field) |
|
1087 | 1087 | if value is None: |
|
1088 | 1088 | value = not rc_val |
|
1089 | 1089 | setattr(self.rc,rc_field,value) |
|
1090 | 1090 | |
|
1091 | 1091 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1092 | 1092 | """Install the user configuration directory. |
|
1093 | 1093 | |
|
1094 | 1094 | Can be called when running for the first time or to upgrade the user's |
|
1095 | 1095 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1096 | 1096 | and 'upgrade'.""" |
|
1097 | 1097 | |
|
1098 | 1098 | def wait(): |
|
1099 | 1099 | try: |
|
1100 | 1100 | raw_input("Please press <RETURN> to start IPython.") |
|
1101 | 1101 | except EOFError: |
|
1102 | 1102 | print >> Term.cout |
|
1103 | 1103 | print '*'*70 |
|
1104 | 1104 | |
|
1105 | 1105 | cwd = os.getcwd() # remember where we started |
|
1106 | 1106 | glb = glob.glob |
|
1107 | 1107 | print '*'*70 |
|
1108 | 1108 | if mode == 'install': |
|
1109 | 1109 | print \ |
|
1110 | 1110 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1111 | 1111 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1112 | 1112 | else: |
|
1113 | 1113 | print 'I am going to upgrade your configuration in:' |
|
1114 | 1114 | |
|
1115 | 1115 | print ipythondir |
|
1116 | 1116 | |
|
1117 | 1117 | rcdirend = os.path.join('IPython','UserConfig') |
|
1118 | 1118 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1119 | 1119 | try: |
|
1120 | 1120 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1121 | 1121 | print "Initializing from configuration",rcdir |
|
1122 | 1122 | except IndexError: |
|
1123 | 1123 | warning = """ |
|
1124 | 1124 | Installation error. IPython's directory was not found. |
|
1125 | 1125 | |
|
1126 | 1126 | Check the following: |
|
1127 | 1127 | |
|
1128 | 1128 | The ipython/IPython directory should be in a directory belonging to your |
|
1129 | 1129 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1130 | 1130 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1131 | 1131 | |
|
1132 | 1132 | IPython will create a minimal default configuration for you. |
|
1133 | 1133 | |
|
1134 | 1134 | """ |
|
1135 | 1135 | warn(warning) |
|
1136 | 1136 | wait() |
|
1137 | 1137 | |
|
1138 | 1138 | if sys.platform =='win32': |
|
1139 | 1139 | inif = 'ipythonrc.ini' |
|
1140 | 1140 | else: |
|
1141 | 1141 | inif = 'ipythonrc' |
|
1142 | 1142 | minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' } |
|
1143 | 1143 | os.makedirs(ipythondir, mode = 0777) |
|
1144 | 1144 | for f, cont in minimal_setup.items(): |
|
1145 | 1145 | open(ipythondir + '/' + f,'w').write(cont) |
|
1146 | 1146 | |
|
1147 | 1147 | return |
|
1148 | 1148 | |
|
1149 | 1149 | if mode == 'install': |
|
1150 | 1150 | try: |
|
1151 | 1151 | shutil.copytree(rcdir,ipythondir) |
|
1152 | 1152 | os.chdir(ipythondir) |
|
1153 | 1153 | rc_files = glb("ipythonrc*") |
|
1154 | 1154 | for rc_file in rc_files: |
|
1155 | 1155 | os.rename(rc_file,rc_file+rc_suffix) |
|
1156 | 1156 | except: |
|
1157 | 1157 | warning = """ |
|
1158 | 1158 | |
|
1159 | 1159 | There was a problem with the installation: |
|
1160 | 1160 | %s |
|
1161 | 1161 | Try to correct it or contact the developers if you think it's a bug. |
|
1162 | 1162 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1163 | 1163 | warn(warning) |
|
1164 | 1164 | wait() |
|
1165 | 1165 | return |
|
1166 | 1166 | |
|
1167 | 1167 | elif mode == 'upgrade': |
|
1168 | 1168 | try: |
|
1169 | 1169 | os.chdir(ipythondir) |
|
1170 | 1170 | except: |
|
1171 | 1171 | print """ |
|
1172 | 1172 | Can not upgrade: changing to directory %s failed. Details: |
|
1173 | 1173 | %s |
|
1174 | 1174 | """ % (ipythondir,sys.exc_info()[1]) |
|
1175 | 1175 | wait() |
|
1176 | 1176 | return |
|
1177 | 1177 | else: |
|
1178 | 1178 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1179 | 1179 | for new_full_path in sources: |
|
1180 | 1180 | new_filename = os.path.basename(new_full_path) |
|
1181 | 1181 | if new_filename.startswith('ipythonrc'): |
|
1182 | 1182 | new_filename = new_filename + rc_suffix |
|
1183 | 1183 | # The config directory should only contain files, skip any |
|
1184 | 1184 | # directories which may be there (like CVS) |
|
1185 | 1185 | if os.path.isdir(new_full_path): |
|
1186 | 1186 | continue |
|
1187 | 1187 | if os.path.exists(new_filename): |
|
1188 | 1188 | old_file = new_filename+'.old' |
|
1189 | 1189 | if os.path.exists(old_file): |
|
1190 | 1190 | os.remove(old_file) |
|
1191 | 1191 | os.rename(new_filename,old_file) |
|
1192 | 1192 | shutil.copy(new_full_path,new_filename) |
|
1193 | 1193 | else: |
|
1194 | 1194 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1195 | 1195 | |
|
1196 | 1196 | # Fix line-endings to those native to each platform in the config |
|
1197 | 1197 | # directory. |
|
1198 | 1198 | try: |
|
1199 | 1199 | os.chdir(ipythondir) |
|
1200 | 1200 | except: |
|
1201 | 1201 | print """ |
|
1202 | 1202 | Problem: changing to directory %s failed. |
|
1203 | 1203 | Details: |
|
1204 | 1204 | %s |
|
1205 | 1205 | |
|
1206 | 1206 | Some configuration files may have incorrect line endings. This should not |
|
1207 | 1207 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1208 | 1208 | wait() |
|
1209 | 1209 | else: |
|
1210 | 1210 | for fname in glb('ipythonrc*'): |
|
1211 | 1211 | try: |
|
1212 | 1212 | native_line_ends(fname,backup=0) |
|
1213 | 1213 | except IOError: |
|
1214 | 1214 | pass |
|
1215 | 1215 | |
|
1216 | 1216 | if mode == 'install': |
|
1217 | 1217 | print """ |
|
1218 | 1218 | Successful installation! |
|
1219 | 1219 | |
|
1220 | 1220 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1221 | 1221 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1222 | 1222 | distribution) to make sure that your system environment is properly configured |
|
1223 | 1223 | to take advantage of IPython's features. |
|
1224 | 1224 | |
|
1225 | 1225 | Important note: the configuration system has changed! The old system is |
|
1226 | 1226 | still in place, but its setting may be partly overridden by the settings in |
|
1227 | 1227 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1228 | 1228 | if some of the new settings bother you. |
|
1229 | 1229 | |
|
1230 | 1230 | """ |
|
1231 | 1231 | else: |
|
1232 | 1232 | print """ |
|
1233 | 1233 | Successful upgrade! |
|
1234 | 1234 | |
|
1235 | 1235 | All files in your directory: |
|
1236 | 1236 | %(ipythondir)s |
|
1237 | 1237 | which would have been overwritten by the upgrade were backed up with a .old |
|
1238 | 1238 | extension. If you had made particular customizations in those files you may |
|
1239 | 1239 | want to merge them back into the new files.""" % locals() |
|
1240 | 1240 | wait() |
|
1241 | 1241 | os.chdir(cwd) |
|
1242 | 1242 | # end user_setup() |
|
1243 | 1243 | |
|
1244 | 1244 | def atexit_operations(self): |
|
1245 | 1245 | """This will be executed at the time of exit. |
|
1246 | 1246 | |
|
1247 | 1247 | Saving of persistent data should be performed here. """ |
|
1248 | 1248 | |
|
1249 | 1249 | #print '*** IPython exit cleanup ***' # dbg |
|
1250 | 1250 | # input history |
|
1251 | 1251 | self.savehist() |
|
1252 | 1252 | |
|
1253 | 1253 | # Cleanup all tempfiles left around |
|
1254 | 1254 | for tfile in self.tempfiles: |
|
1255 | 1255 | try: |
|
1256 | 1256 | os.unlink(tfile) |
|
1257 | 1257 | except OSError: |
|
1258 | 1258 | pass |
|
1259 | 1259 | |
|
1260 | 1260 | self.hooks.shutdown_hook() |
|
1261 | 1261 | |
|
1262 | 1262 | def savehist(self): |
|
1263 | 1263 | """Save input history to a file (via readline library).""" |
|
1264 | 1264 | |
|
1265 | 1265 | if not self.has_readline: |
|
1266 | 1266 | return |
|
1267 | 1267 | |
|
1268 | 1268 | try: |
|
1269 | 1269 | self.readline.write_history_file(self.histfile) |
|
1270 | 1270 | except: |
|
1271 | 1271 | print 'Unable to save IPython command history to file: ' + \ |
|
1272 | 1272 | `self.histfile` |
|
1273 | 1273 | |
|
1274 | 1274 | def reloadhist(self): |
|
1275 | 1275 | """Reload the input history from disk file.""" |
|
1276 | 1276 | |
|
1277 | 1277 | if self.has_readline: |
|
1278 | 1278 | try: |
|
1279 | 1279 | self.readline.clear_history() |
|
1280 | 1280 | self.readline.read_history_file(self.shell.histfile) |
|
1281 | 1281 | except AttributeError: |
|
1282 | 1282 | pass |
|
1283 | 1283 | |
|
1284 | 1284 | |
|
1285 | 1285 | def history_saving_wrapper(self, func): |
|
1286 | 1286 | """ Wrap func for readline history saving |
|
1287 | 1287 | |
|
1288 | 1288 | Convert func into callable that saves & restores |
|
1289 | 1289 | history around the call """ |
|
1290 | 1290 | |
|
1291 | 1291 | if not self.has_readline: |
|
1292 | 1292 | return func |
|
1293 | 1293 | |
|
1294 | 1294 | def wrapper(): |
|
1295 | 1295 | self.savehist() |
|
1296 | 1296 | try: |
|
1297 | 1297 | func() |
|
1298 | 1298 | finally: |
|
1299 | 1299 | readline.read_history_file(self.histfile) |
|
1300 | 1300 | return wrapper |
|
1301 | 1301 | |
|
1302 | 1302 | |
|
1303 | 1303 | def pre_readline(self): |
|
1304 | 1304 | """readline hook to be used at the start of each line. |
|
1305 | 1305 | |
|
1306 | 1306 | Currently it handles auto-indent only.""" |
|
1307 | 1307 | |
|
1308 | 1308 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1309 | 1309 | |
|
1310 | 1310 | if self.rl_do_indent: |
|
1311 | 1311 | self.readline.insert_text(self.indent_current_str()) |
|
1312 | 1312 | if self.rl_next_input is not None: |
|
1313 | 1313 | self.readline.insert_text(self.rl_next_input) |
|
1314 | 1314 | self.rl_next_input = None |
|
1315 | 1315 | |
|
1316 | 1316 | def init_readline(self): |
|
1317 | 1317 | """Command history completion/saving/reloading.""" |
|
1318 | 1318 | |
|
1319 | 1319 | |
|
1320 | 1320 | import IPython.rlineimpl as readline |
|
1321 | 1321 | |
|
1322 | 1322 | if not readline.have_readline: |
|
1323 | 1323 | self.has_readline = 0 |
|
1324 | 1324 | self.readline = None |
|
1325 | 1325 | # no point in bugging windows users with this every time: |
|
1326 | 1326 | warn('Readline services not available on this platform.') |
|
1327 | 1327 | else: |
|
1328 | 1328 | sys.modules['readline'] = readline |
|
1329 | 1329 | import atexit |
|
1330 | 1330 | from IPython.completer import IPCompleter |
|
1331 | 1331 | self.Completer = IPCompleter(self, |
|
1332 | 1332 | self.user_ns, |
|
1333 | 1333 | self.user_global_ns, |
|
1334 | 1334 | self.rc.readline_omit__names, |
|
1335 | 1335 | self.alias_table) |
|
1336 | 1336 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1337 | 1337 | self.strdispatchers['complete_command'] = sdisp |
|
1338 | 1338 | self.Completer.custom_completers = sdisp |
|
1339 | 1339 | # Platform-specific configuration |
|
1340 | 1340 | if os.name == 'nt': |
|
1341 | 1341 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1342 | 1342 | else: |
|
1343 | 1343 | self.readline_startup_hook = readline.set_startup_hook |
|
1344 | 1344 | |
|
1345 | 1345 | # Load user's initrc file (readline config) |
|
1346 | 1346 | # Or if libedit is used, load editrc. |
|
1347 | 1347 | inputrc_name = os.environ.get('INPUTRC') |
|
1348 | 1348 | if inputrc_name is None: |
|
1349 | 1349 | home_dir = get_home_dir() |
|
1350 | 1350 | if home_dir is not None: |
|
1351 | 1351 | inputrc_name = '.inputrc' |
|
1352 | 1352 | if readline.uses_libedit: |
|
1353 | 1353 | inputrc_name = '.editrc' |
|
1354 | 1354 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1355 | 1355 | if os.path.isfile(inputrc_name): |
|
1356 | 1356 | try: |
|
1357 | 1357 | readline.read_init_file(inputrc_name) |
|
1358 | 1358 | except: |
|
1359 | 1359 | warn('Problems reading readline initialization file <%s>' |
|
1360 | 1360 | % inputrc_name) |
|
1361 | 1361 | |
|
1362 | 1362 | self.has_readline = 1 |
|
1363 | 1363 | self.readline = readline |
|
1364 | 1364 | # save this in sys so embedded copies can restore it properly |
|
1365 | 1365 | sys.ipcompleter = self.Completer.complete |
|
1366 | 1366 | self.set_completer() |
|
1367 | 1367 | |
|
1368 | 1368 | # Configure readline according to user's prefs |
|
1369 | 1369 | # This is only done if GNU readline is being used. If libedit |
|
1370 | 1370 | # is being used (as on Leopard) the readline config is |
|
1371 | 1371 | # not run as the syntax for libedit is different. |
|
1372 | 1372 | if not readline.uses_libedit: |
|
1373 | 1373 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1374 | #print "loading rl:",rlcommand # dbg | |
|
1374 | 1375 | readline.parse_and_bind(rlcommand) |
|
1375 | 1376 | |
|
1376 | 1377 | # remove some chars from the delimiters list |
|
1377 | 1378 | delims = readline.get_completer_delims() |
|
1378 | 1379 | delims = delims.translate(string._idmap, |
|
1379 | 1380 | self.rc.readline_remove_delims) |
|
1380 | 1381 | readline.set_completer_delims(delims) |
|
1381 | 1382 | # otherwise we end up with a monster history after a while: |
|
1382 | 1383 | readline.set_history_length(1000) |
|
1383 | 1384 | try: |
|
1384 | 1385 | #print '*** Reading readline history' # dbg |
|
1385 | 1386 | readline.read_history_file(self.histfile) |
|
1386 | 1387 | except IOError: |
|
1387 | 1388 | pass # It doesn't exist yet. |
|
1388 | 1389 | |
|
1389 | 1390 | atexit.register(self.atexit_operations) |
|
1390 | 1391 | del atexit |
|
1391 | 1392 | |
|
1392 | 1393 | # Configure auto-indent for all platforms |
|
1393 | 1394 | self.set_autoindent(self.rc.autoindent) |
|
1394 | 1395 | |
|
1395 | 1396 | def ask_yes_no(self,prompt,default=True): |
|
1396 | 1397 | if self.rc.quiet: |
|
1397 | 1398 | return True |
|
1398 | 1399 | return ask_yes_no(prompt,default) |
|
1399 | 1400 | |
|
1400 | 1401 | def _should_recompile(self,e): |
|
1401 | 1402 | """Utility routine for edit_syntax_error""" |
|
1402 | 1403 | |
|
1403 | 1404 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1404 | 1405 | '<console>','<BackgroundJob compilation>', |
|
1405 | 1406 | None): |
|
1406 | 1407 | |
|
1407 | 1408 | return False |
|
1408 | 1409 | try: |
|
1409 | 1410 | if (self.rc.autoedit_syntax and |
|
1410 | 1411 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1411 | 1412 | '[Y/n] ','y')): |
|
1412 | 1413 | return False |
|
1413 | 1414 | except EOFError: |
|
1414 | 1415 | return False |
|
1415 | 1416 | |
|
1416 | 1417 | def int0(x): |
|
1417 | 1418 | try: |
|
1418 | 1419 | return int(x) |
|
1419 | 1420 | except TypeError: |
|
1420 | 1421 | return 0 |
|
1421 | 1422 | # always pass integer line and offset values to editor hook |
|
1422 | 1423 | try: |
|
1423 | 1424 | self.hooks.fix_error_editor(e.filename, |
|
1424 | 1425 | int0(e.lineno),int0(e.offset),e.msg) |
|
1425 | 1426 | except IPython.ipapi.TryNext: |
|
1426 | 1427 | warn('Could not open editor') |
|
1427 | 1428 | return False |
|
1428 | 1429 | return True |
|
1429 | 1430 | |
|
1430 | 1431 | def edit_syntax_error(self): |
|
1431 | 1432 | """The bottom half of the syntax error handler called in the main loop. |
|
1432 | 1433 | |
|
1433 | 1434 | Loop until syntax error is fixed or user cancels. |
|
1434 | 1435 | """ |
|
1435 | 1436 | |
|
1436 | 1437 | while self.SyntaxTB.last_syntax_error: |
|
1437 | 1438 | # copy and clear last_syntax_error |
|
1438 | 1439 | err = self.SyntaxTB.clear_err_state() |
|
1439 | 1440 | if not self._should_recompile(err): |
|
1440 | 1441 | return |
|
1441 | 1442 | try: |
|
1442 | 1443 | # may set last_syntax_error again if a SyntaxError is raised |
|
1443 | 1444 | self.safe_execfile(err.filename,self.user_ns) |
|
1444 | 1445 | except: |
|
1445 | 1446 | self.showtraceback() |
|
1446 | 1447 | else: |
|
1447 | 1448 | try: |
|
1448 | 1449 | f = file(err.filename) |
|
1449 | 1450 | try: |
|
1450 | 1451 | sys.displayhook(f.read()) |
|
1451 | 1452 | finally: |
|
1452 | 1453 | f.close() |
|
1453 | 1454 | except: |
|
1454 | 1455 | self.showtraceback() |
|
1455 | 1456 | |
|
1456 | 1457 | def showsyntaxerror(self, filename=None): |
|
1457 | 1458 | """Display the syntax error that just occurred. |
|
1458 | 1459 | |
|
1459 | 1460 | This doesn't display a stack trace because there isn't one. |
|
1460 | 1461 | |
|
1461 | 1462 | If a filename is given, it is stuffed in the exception instead |
|
1462 | 1463 | of what was there before (because Python's parser always uses |
|
1463 | 1464 | "<string>" when reading from a string). |
|
1464 | 1465 | """ |
|
1465 | 1466 | etype, value, last_traceback = sys.exc_info() |
|
1466 | 1467 | |
|
1467 | 1468 | # See note about these variables in showtraceback() below |
|
1468 | 1469 | sys.last_type = etype |
|
1469 | 1470 | sys.last_value = value |
|
1470 | 1471 | sys.last_traceback = last_traceback |
|
1471 | 1472 | |
|
1472 | 1473 | if filename and etype is SyntaxError: |
|
1473 | 1474 | # Work hard to stuff the correct filename in the exception |
|
1474 | 1475 | try: |
|
1475 | 1476 | msg, (dummy_filename, lineno, offset, line) = value |
|
1476 | 1477 | except: |
|
1477 | 1478 | # Not the format we expect; leave it alone |
|
1478 | 1479 | pass |
|
1479 | 1480 | else: |
|
1480 | 1481 | # Stuff in the right filename |
|
1481 | 1482 | try: |
|
1482 | 1483 | # Assume SyntaxError is a class exception |
|
1483 | 1484 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1484 | 1485 | except: |
|
1485 | 1486 | # If that failed, assume SyntaxError is a string |
|
1486 | 1487 | value = msg, (filename, lineno, offset, line) |
|
1487 | 1488 | self.SyntaxTB(etype,value,[]) |
|
1488 | 1489 | |
|
1489 | 1490 | def debugger(self,force=False): |
|
1490 | 1491 | """Call the pydb/pdb debugger. |
|
1491 | 1492 | |
|
1492 | 1493 | Keywords: |
|
1493 | 1494 | |
|
1494 | 1495 | - force(False): by default, this routine checks the instance call_pdb |
|
1495 | 1496 | flag and does not actually invoke the debugger if the flag is false. |
|
1496 | 1497 | The 'force' option forces the debugger to activate even if the flag |
|
1497 | 1498 | is false. |
|
1498 | 1499 | """ |
|
1499 | 1500 | |
|
1500 | 1501 | if not (force or self.call_pdb): |
|
1501 | 1502 | return |
|
1502 | 1503 | |
|
1503 | 1504 | if not hasattr(sys,'last_traceback'): |
|
1504 | 1505 | error('No traceback has been produced, nothing to debug.') |
|
1505 | 1506 | return |
|
1506 | 1507 | |
|
1507 | 1508 | # use pydb if available |
|
1508 | 1509 | if Debugger.has_pydb: |
|
1509 | 1510 | from pydb import pm |
|
1510 | 1511 | else: |
|
1511 | 1512 | # fallback to our internal debugger |
|
1512 | 1513 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1513 | 1514 | self.history_saving_wrapper(pm)() |
|
1514 | 1515 | |
|
1515 | 1516 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1516 | 1517 | """Display the exception that just occurred. |
|
1517 | 1518 | |
|
1518 | 1519 | If nothing is known about the exception, this is the method which |
|
1519 | 1520 | should be used throughout the code for presenting user tracebacks, |
|
1520 | 1521 | rather than directly invoking the InteractiveTB object. |
|
1521 | 1522 | |
|
1522 | 1523 | A specific showsyntaxerror() also exists, but this method can take |
|
1523 | 1524 | care of calling it if needed, so unless you are explicitly catching a |
|
1524 | 1525 | SyntaxError exception, don't try to analyze the stack manually and |
|
1525 | 1526 | simply call this method.""" |
|
1526 | 1527 | |
|
1527 | 1528 | |
|
1528 | 1529 | # Though this won't be called by syntax errors in the input line, |
|
1529 | 1530 | # there may be SyntaxError cases whith imported code. |
|
1530 | 1531 | |
|
1531 | 1532 | try: |
|
1532 | 1533 | if exc_tuple is None: |
|
1533 | 1534 | etype, value, tb = sys.exc_info() |
|
1534 | 1535 | else: |
|
1535 | 1536 | etype, value, tb = exc_tuple |
|
1536 | 1537 | |
|
1537 | 1538 | if etype is SyntaxError: |
|
1538 | 1539 | self.showsyntaxerror(filename) |
|
1539 | 1540 | elif etype is IPython.ipapi.UsageError: |
|
1540 | 1541 | print "UsageError:", value |
|
1541 | 1542 | else: |
|
1542 | 1543 | # WARNING: these variables are somewhat deprecated and not |
|
1543 | 1544 | # necessarily safe to use in a threaded environment, but tools |
|
1544 | 1545 | # like pdb depend on their existence, so let's set them. If we |
|
1545 | 1546 | # find problems in the field, we'll need to revisit their use. |
|
1546 | 1547 | sys.last_type = etype |
|
1547 | 1548 | sys.last_value = value |
|
1548 | 1549 | sys.last_traceback = tb |
|
1549 | 1550 | |
|
1550 | 1551 | if etype in self.custom_exceptions: |
|
1551 | 1552 | self.CustomTB(etype,value,tb) |
|
1552 | 1553 | else: |
|
1553 | 1554 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1554 | 1555 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1555 | 1556 | # pdb mucks up readline, fix it back |
|
1556 | 1557 | self.set_completer() |
|
1557 | 1558 | except KeyboardInterrupt: |
|
1558 | 1559 | self.write("\nKeyboardInterrupt\n") |
|
1559 | 1560 | |
|
1560 | 1561 | |
|
1561 | 1562 | |
|
1562 | 1563 | def mainloop(self,banner=None): |
|
1563 | 1564 | """Creates the local namespace and starts the mainloop. |
|
1564 | 1565 | |
|
1565 | 1566 | If an optional banner argument is given, it will override the |
|
1566 | 1567 | internally created default banner.""" |
|
1567 | 1568 | |
|
1568 | 1569 | if self.rc.c: # Emulate Python's -c option |
|
1569 | 1570 | self.exec_init_cmd() |
|
1570 | 1571 | if banner is None: |
|
1571 | 1572 | if not self.rc.banner: |
|
1572 | 1573 | banner = '' |
|
1573 | 1574 | # banner is string? Use it directly! |
|
1574 | 1575 | elif isinstance(self.rc.banner,basestring): |
|
1575 | 1576 | banner = self.rc.banner |
|
1576 | 1577 | else: |
|
1577 | 1578 | banner = self.BANNER+self.banner2 |
|
1578 | 1579 | |
|
1579 | 1580 | # if you run stuff with -c <cmd>, raw hist is not updated |
|
1580 | 1581 | # ensure that it's in sync |
|
1581 | 1582 | if len(self.input_hist) != len (self.input_hist_raw): |
|
1582 | 1583 | self.input_hist_raw = InputList(self.input_hist) |
|
1583 | 1584 | |
|
1584 | 1585 | while 1: |
|
1585 | 1586 | try: |
|
1586 | 1587 | self.interact(banner) |
|
1587 | 1588 | #self.interact_with_readline() |
|
1588 | 1589 | # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above |
|
1589 | 1590 | |
|
1590 | 1591 | break |
|
1591 | 1592 | except KeyboardInterrupt: |
|
1592 | 1593 | # this should not be necessary, but KeyboardInterrupt |
|
1593 | 1594 | # handling seems rather unpredictable... |
|
1594 | 1595 | self.write("\nKeyboardInterrupt in interact()\n") |
|
1595 | 1596 | |
|
1596 | 1597 | def exec_init_cmd(self): |
|
1597 | 1598 | """Execute a command given at the command line. |
|
1598 | 1599 | |
|
1599 | 1600 | This emulates Python's -c option.""" |
|
1600 | 1601 | |
|
1601 | 1602 | #sys.argv = ['-c'] |
|
1602 | 1603 | self.push(self.prefilter(self.rc.c, False)) |
|
1603 | 1604 | if not self.rc.interact: |
|
1604 | 1605 | self.ask_exit() |
|
1605 | 1606 | |
|
1606 | 1607 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1607 | 1608 | """Embeds IPython into a running python program. |
|
1608 | 1609 | |
|
1609 | 1610 | Input: |
|
1610 | 1611 | |
|
1611 | 1612 | - header: An optional header message can be specified. |
|
1612 | 1613 | |
|
1613 | 1614 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1614 | 1615 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1615 | 1616 | program variables become visible but user-specific configuration |
|
1616 | 1617 | remains possible. |
|
1617 | 1618 | |
|
1618 | 1619 | - stack_depth: specifies how many levels in the stack to go to |
|
1619 | 1620 | looking for namespaces (when local_ns and global_ns are None). This |
|
1620 | 1621 | allows an intermediate caller to make sure that this function gets |
|
1621 | 1622 | the namespace from the intended level in the stack. By default (0) |
|
1622 | 1623 | it will get its locals and globals from the immediate caller. |
|
1623 | 1624 | |
|
1624 | 1625 | Warning: it's possible to use this in a program which is being run by |
|
1625 | 1626 | IPython itself (via %run), but some funny things will happen (a few |
|
1626 | 1627 | globals get overwritten). In the future this will be cleaned up, as |
|
1627 | 1628 | there is no fundamental reason why it can't work perfectly.""" |
|
1628 | 1629 | |
|
1629 | 1630 | # Get locals and globals from caller |
|
1630 | 1631 | if local_ns is None or global_ns is None: |
|
1631 | 1632 | call_frame = sys._getframe(stack_depth).f_back |
|
1632 | 1633 | |
|
1633 | 1634 | if local_ns is None: |
|
1634 | 1635 | local_ns = call_frame.f_locals |
|
1635 | 1636 | if global_ns is None: |
|
1636 | 1637 | global_ns = call_frame.f_globals |
|
1637 | 1638 | |
|
1638 | 1639 | # Update namespaces and fire up interpreter |
|
1639 | 1640 | |
|
1640 | 1641 | # The global one is easy, we can just throw it in |
|
1641 | 1642 | self.user_global_ns = global_ns |
|
1642 | 1643 | |
|
1643 | 1644 | # but the user/local one is tricky: ipython needs it to store internal |
|
1644 | 1645 | # data, but we also need the locals. We'll copy locals in the user |
|
1645 | 1646 | # one, but will track what got copied so we can delete them at exit. |
|
1646 | 1647 | # This is so that a later embedded call doesn't see locals from a |
|
1647 | 1648 | # previous call (which most likely existed in a separate scope). |
|
1648 | 1649 | local_varnames = local_ns.keys() |
|
1649 | 1650 | self.user_ns.update(local_ns) |
|
1650 | 1651 | #self.user_ns['local_ns'] = local_ns # dbg |
|
1651 | 1652 | |
|
1652 | 1653 | # Patch for global embedding to make sure that things don't overwrite |
|
1653 | 1654 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1654 | 1655 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1655 | 1656 | if local_ns is None and global_ns is None: |
|
1656 | 1657 | self.user_global_ns.update(__main__.__dict__) |
|
1657 | 1658 | |
|
1658 | 1659 | # make sure the tab-completer has the correct frame information, so it |
|
1659 | 1660 | # actually completes using the frame's locals/globals |
|
1660 | 1661 | self.set_completer_frame() |
|
1661 | 1662 | |
|
1662 | 1663 | # before activating the interactive mode, we need to make sure that |
|
1663 | 1664 | # all names in the builtin namespace needed by ipython point to |
|
1664 | 1665 | # ourselves, and not to other instances. |
|
1665 | 1666 | self.add_builtins() |
|
1666 | 1667 | |
|
1667 | 1668 | self.interact(header) |
|
1668 | 1669 | |
|
1669 | 1670 | # now, purge out the user namespace from anything we might have added |
|
1670 | 1671 | # from the caller's local namespace |
|
1671 | 1672 | delvar = self.user_ns.pop |
|
1672 | 1673 | for var in local_varnames: |
|
1673 | 1674 | delvar(var,None) |
|
1674 | 1675 | # and clean builtins we may have overridden |
|
1675 | 1676 | self.clean_builtins() |
|
1676 | 1677 | |
|
1677 | 1678 | def interact_prompt(self): |
|
1678 | 1679 | """ Print the prompt (in read-eval-print loop) |
|
1679 | 1680 | |
|
1680 | 1681 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1681 | 1682 | used in standard IPython flow. |
|
1682 | 1683 | """ |
|
1683 | 1684 | if self.more: |
|
1684 | 1685 | try: |
|
1685 | 1686 | prompt = self.hooks.generate_prompt(True) |
|
1686 | 1687 | except: |
|
1687 | 1688 | self.showtraceback() |
|
1688 | 1689 | if self.autoindent: |
|
1689 | 1690 | self.rl_do_indent = True |
|
1690 | 1691 | |
|
1691 | 1692 | else: |
|
1692 | 1693 | try: |
|
1693 | 1694 | prompt = self.hooks.generate_prompt(False) |
|
1694 | 1695 | except: |
|
1695 | 1696 | self.showtraceback() |
|
1696 | 1697 | self.write(prompt) |
|
1697 | 1698 | |
|
1698 | 1699 | def interact_handle_input(self,line): |
|
1699 | 1700 | """ Handle the input line (in read-eval-print loop) |
|
1700 | 1701 | |
|
1701 | 1702 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1702 | 1703 | used in standard IPython flow. |
|
1703 | 1704 | """ |
|
1704 | 1705 | if line.lstrip() == line: |
|
1705 | 1706 | self.shadowhist.add(line.strip()) |
|
1706 | 1707 | lineout = self.prefilter(line,self.more) |
|
1707 | 1708 | |
|
1708 | 1709 | if line.strip(): |
|
1709 | 1710 | if self.more: |
|
1710 | 1711 | self.input_hist_raw[-1] += '%s\n' % line |
|
1711 | 1712 | else: |
|
1712 | 1713 | self.input_hist_raw.append('%s\n' % line) |
|
1713 | 1714 | |
|
1714 | 1715 | |
|
1715 | 1716 | self.more = self.push(lineout) |
|
1716 | 1717 | if (self.SyntaxTB.last_syntax_error and |
|
1717 | 1718 | self.rc.autoedit_syntax): |
|
1718 | 1719 | self.edit_syntax_error() |
|
1719 | 1720 | |
|
1720 | 1721 | def interact_with_readline(self): |
|
1721 | 1722 | """ Demo of using interact_handle_input, interact_prompt |
|
1722 | 1723 | |
|
1723 | 1724 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), |
|
1724 | 1725 | it should work like this. |
|
1725 | 1726 | """ |
|
1726 | 1727 | self.readline_startup_hook(self.pre_readline) |
|
1727 | 1728 | while not self.exit_now: |
|
1728 | 1729 | self.interact_prompt() |
|
1729 | 1730 | if self.more: |
|
1730 | 1731 | self.rl_do_indent = True |
|
1731 | 1732 | else: |
|
1732 | 1733 | self.rl_do_indent = False |
|
1733 | 1734 | line = raw_input_original().decode(self.stdin_encoding) |
|
1734 | 1735 | self.interact_handle_input(line) |
|
1735 | 1736 | |
|
1736 | 1737 | |
|
1737 | 1738 | def interact(self, banner=None): |
|
1738 | 1739 | """Closely emulate the interactive Python console. |
|
1739 | 1740 | |
|
1740 | 1741 | The optional banner argument specify the banner to print |
|
1741 | 1742 | before the first interaction; by default it prints a banner |
|
1742 | 1743 | similar to the one printed by the real Python interpreter, |
|
1743 | 1744 | followed by the current class name in parentheses (so as not |
|
1744 | 1745 | to confuse this with the real interpreter -- since it's so |
|
1745 | 1746 | close!). |
|
1746 | 1747 | |
|
1747 | 1748 | """ |
|
1748 | 1749 | |
|
1749 | 1750 | if self.exit_now: |
|
1750 | 1751 | # batch run -> do not interact |
|
1751 | 1752 | return |
|
1752 | 1753 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1753 | 1754 | if banner is None: |
|
1754 | 1755 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1755 | 1756 | (sys.version, sys.platform, cprt, |
|
1756 | 1757 | self.__class__.__name__)) |
|
1757 | 1758 | else: |
|
1758 | 1759 | self.write(banner) |
|
1759 | 1760 | |
|
1760 | 1761 | more = 0 |
|
1761 | 1762 | |
|
1762 | 1763 | # Mark activity in the builtins |
|
1763 | 1764 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1764 | 1765 | |
|
1765 | 1766 | if self.has_readline: |
|
1766 | 1767 | self.readline_startup_hook(self.pre_readline) |
|
1767 | 1768 | # exit_now is set by a call to %Exit or %Quit, through the |
|
1768 | 1769 | # ask_exit callback. |
|
1769 | 1770 | |
|
1770 | 1771 | while not self.exit_now: |
|
1771 | 1772 | self.hooks.pre_prompt_hook() |
|
1772 | 1773 | if more: |
|
1773 | 1774 | try: |
|
1774 | 1775 | prompt = self.hooks.generate_prompt(True) |
|
1775 | 1776 | except: |
|
1776 | 1777 | self.showtraceback() |
|
1777 | 1778 | if self.autoindent: |
|
1778 | 1779 | self.rl_do_indent = True |
|
1779 | 1780 | |
|
1780 | 1781 | else: |
|
1781 | 1782 | try: |
|
1782 | 1783 | prompt = self.hooks.generate_prompt(False) |
|
1783 | 1784 | except: |
|
1784 | 1785 | self.showtraceback() |
|
1785 | 1786 | try: |
|
1786 | 1787 | line = self.raw_input(prompt,more) |
|
1787 | 1788 | if self.exit_now: |
|
1788 | 1789 | # quick exit on sys.std[in|out] close |
|
1789 | 1790 | break |
|
1790 | 1791 | if self.autoindent: |
|
1791 | 1792 | self.rl_do_indent = False |
|
1792 | 1793 | |
|
1793 | 1794 | except KeyboardInterrupt: |
|
1794 | 1795 | #double-guard against keyboardinterrupts during kbdint handling |
|
1795 | 1796 | try: |
|
1796 | 1797 | self.write('\nKeyboardInterrupt\n') |
|
1797 | 1798 | self.resetbuffer() |
|
1798 | 1799 | # keep cache in sync with the prompt counter: |
|
1799 | 1800 | self.outputcache.prompt_count -= 1 |
|
1800 | 1801 | |
|
1801 | 1802 | if self.autoindent: |
|
1802 | 1803 | self.indent_current_nsp = 0 |
|
1803 | 1804 | more = 0 |
|
1804 | 1805 | except KeyboardInterrupt: |
|
1805 | 1806 | pass |
|
1806 | 1807 | except EOFError: |
|
1807 | 1808 | if self.autoindent: |
|
1808 | 1809 | self.rl_do_indent = False |
|
1809 | 1810 | self.readline_startup_hook(None) |
|
1810 | 1811 | self.write('\n') |
|
1811 | 1812 | self.exit() |
|
1812 | 1813 | except bdb.BdbQuit: |
|
1813 | 1814 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1814 | 1815 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1815 | 1816 | 'for IPython to properly format this particular exception.\n' |
|
1816 | 1817 | 'IPython will resume normal operation.') |
|
1817 | 1818 | except: |
|
1818 | 1819 | # exceptions here are VERY RARE, but they can be triggered |
|
1819 | 1820 | # asynchronously by signal handlers, for example. |
|
1820 | 1821 | self.showtraceback() |
|
1821 | 1822 | else: |
|
1822 | 1823 | more = self.push(line) |
|
1823 | 1824 | if (self.SyntaxTB.last_syntax_error and |
|
1824 | 1825 | self.rc.autoedit_syntax): |
|
1825 | 1826 | self.edit_syntax_error() |
|
1826 | 1827 | |
|
1827 | 1828 | # We are off again... |
|
1828 | 1829 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1829 | 1830 | |
|
1830 | 1831 | def excepthook(self, etype, value, tb): |
|
1831 | 1832 | """One more defense for GUI apps that call sys.excepthook. |
|
1832 | 1833 | |
|
1833 | 1834 | GUI frameworks like wxPython trap exceptions and call |
|
1834 | 1835 | sys.excepthook themselves. I guess this is a feature that |
|
1835 | 1836 | enables them to keep running after exceptions that would |
|
1836 | 1837 | otherwise kill their mainloop. This is a bother for IPython |
|
1837 | 1838 | which excepts to catch all of the program exceptions with a try: |
|
1838 | 1839 | except: statement. |
|
1839 | 1840 | |
|
1840 | 1841 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1841 | 1842 | any app directly invokes sys.excepthook, it will look to the user like |
|
1842 | 1843 | IPython crashed. In order to work around this, we can disable the |
|
1843 | 1844 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1844 | 1845 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1845 | 1846 | call sys.excepthook will generate a regular-looking exception from |
|
1846 | 1847 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1847 | 1848 | crashes. |
|
1848 | 1849 | |
|
1849 | 1850 | This hook should be used sparingly, only in places which are not likely |
|
1850 | 1851 | to be true IPython errors. |
|
1851 | 1852 | """ |
|
1852 | 1853 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1853 | 1854 | |
|
1854 | 1855 | def expand_aliases(self,fn,rest): |
|
1855 | 1856 | """ Expand multiple levels of aliases: |
|
1856 | 1857 | |
|
1857 | 1858 | if: |
|
1858 | 1859 | |
|
1859 | 1860 | alias foo bar /tmp |
|
1860 | 1861 | alias baz foo |
|
1861 | 1862 | |
|
1862 | 1863 | then: |
|
1863 | 1864 | |
|
1864 | 1865 | baz huhhahhei -> bar /tmp huhhahhei |
|
1865 | 1866 | |
|
1866 | 1867 | """ |
|
1867 | 1868 | line = fn + " " + rest |
|
1868 | 1869 | |
|
1869 | 1870 | done = Set() |
|
1870 | 1871 | while 1: |
|
1871 | 1872 | pre,fn,rest = prefilter.splitUserInput(line, |
|
1872 | 1873 | prefilter.shell_line_split) |
|
1873 | 1874 | if fn in self.alias_table: |
|
1874 | 1875 | if fn in done: |
|
1875 | 1876 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1876 | 1877 | return "" |
|
1877 | 1878 | done.add(fn) |
|
1878 | 1879 | |
|
1879 | 1880 | l2 = self.transform_alias(fn,rest) |
|
1880 | 1881 | # dir -> dir |
|
1881 | 1882 | # print "alias",line, "->",l2 #dbg |
|
1882 | 1883 | if l2 == line: |
|
1883 | 1884 | break |
|
1884 | 1885 | # ls -> ls -F should not recurse forever |
|
1885 | 1886 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1886 | 1887 | line = l2 |
|
1887 | 1888 | break |
|
1888 | 1889 | |
|
1889 | 1890 | line=l2 |
|
1890 | 1891 | |
|
1891 | 1892 | |
|
1892 | 1893 | # print "al expand to",line #dbg |
|
1893 | 1894 | else: |
|
1894 | 1895 | break |
|
1895 | 1896 | |
|
1896 | 1897 | return line |
|
1897 | 1898 | |
|
1898 | 1899 | def transform_alias(self, alias,rest=''): |
|
1899 | 1900 | """ Transform alias to system command string. |
|
1900 | 1901 | """ |
|
1901 | 1902 | trg = self.alias_table[alias] |
|
1902 | 1903 | |
|
1903 | 1904 | nargs,cmd = trg |
|
1904 | 1905 | # print trg #dbg |
|
1905 | 1906 | if ' ' in cmd and os.path.isfile(cmd): |
|
1906 | 1907 | cmd = '"%s"' % cmd |
|
1907 | 1908 | |
|
1908 | 1909 | # Expand the %l special to be the user's input line |
|
1909 | 1910 | if cmd.find('%l') >= 0: |
|
1910 | 1911 | cmd = cmd.replace('%l',rest) |
|
1911 | 1912 | rest = '' |
|
1912 | 1913 | if nargs==0: |
|
1913 | 1914 | # Simple, argument-less aliases |
|
1914 | 1915 | cmd = '%s %s' % (cmd,rest) |
|
1915 | 1916 | else: |
|
1916 | 1917 | # Handle aliases with positional arguments |
|
1917 | 1918 | args = rest.split(None,nargs) |
|
1918 | 1919 | if len(args)< nargs: |
|
1919 | 1920 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1920 | 1921 | (alias,nargs,len(args))) |
|
1921 | 1922 | return None |
|
1922 | 1923 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1923 | 1924 | # Now call the macro, evaluating in the user's namespace |
|
1924 | 1925 | #print 'new command: <%r>' % cmd # dbg |
|
1925 | 1926 | return cmd |
|
1926 | 1927 | |
|
1927 | 1928 | def call_alias(self,alias,rest=''): |
|
1928 | 1929 | """Call an alias given its name and the rest of the line. |
|
1929 | 1930 | |
|
1930 | 1931 | This is only used to provide backwards compatibility for users of |
|
1931 | 1932 | ipalias(), use of which is not recommended for anymore.""" |
|
1932 | 1933 | |
|
1933 | 1934 | # Now call the macro, evaluating in the user's namespace |
|
1934 | 1935 | cmd = self.transform_alias(alias, rest) |
|
1935 | 1936 | try: |
|
1936 | 1937 | self.system(cmd) |
|
1937 | 1938 | except: |
|
1938 | 1939 | self.showtraceback() |
|
1939 | 1940 | |
|
1940 | 1941 | def indent_current_str(self): |
|
1941 | 1942 | """return the current level of indentation as a string""" |
|
1942 | 1943 | return self.indent_current_nsp * ' ' |
|
1943 | 1944 | |
|
1944 | 1945 | def autoindent_update(self,line): |
|
1945 | 1946 | """Keep track of the indent level.""" |
|
1946 | 1947 | |
|
1947 | 1948 | #debugx('line') |
|
1948 | 1949 | #debugx('self.indent_current_nsp') |
|
1949 | 1950 | if self.autoindent: |
|
1950 | 1951 | if line: |
|
1951 | 1952 | inisp = num_ini_spaces(line) |
|
1952 | 1953 | if inisp < self.indent_current_nsp: |
|
1953 | 1954 | self.indent_current_nsp = inisp |
|
1954 | 1955 | |
|
1955 | 1956 | if line[-1] == ':': |
|
1956 | 1957 | self.indent_current_nsp += 4 |
|
1957 | 1958 | elif dedent_re.match(line): |
|
1958 | 1959 | self.indent_current_nsp -= 4 |
|
1959 | 1960 | else: |
|
1960 | 1961 | self.indent_current_nsp = 0 |
|
1961 | 1962 | |
|
1962 | 1963 | def runlines(self,lines): |
|
1963 | 1964 | """Run a string of one or more lines of source. |
|
1964 | 1965 | |
|
1965 | 1966 | This method is capable of running a string containing multiple source |
|
1966 | 1967 | lines, as if they had been entered at the IPython prompt. Since it |
|
1967 | 1968 | exposes IPython's processing machinery, the given strings can contain |
|
1968 | 1969 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1969 | 1970 | |
|
1970 | 1971 | # We must start with a clean buffer, in case this is run from an |
|
1971 | 1972 | # interactive IPython session (via a magic, for example). |
|
1972 | 1973 | self.resetbuffer() |
|
1973 | 1974 | lines = lines.split('\n') |
|
1974 | 1975 | more = 0 |
|
1975 | 1976 | |
|
1976 | 1977 | for line in lines: |
|
1977 | 1978 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1978 | 1979 | # NOT skip even a blank line if we are in a code block (more is |
|
1979 | 1980 | # true) |
|
1980 | 1981 | |
|
1981 | 1982 | |
|
1982 | 1983 | if line or more: |
|
1983 | 1984 | # push to raw history, so hist line numbers stay in sync |
|
1984 | 1985 | self.input_hist_raw.append("# " + line + "\n") |
|
1985 | 1986 | more = self.push(self.prefilter(line,more)) |
|
1986 | 1987 | # IPython's runsource returns None if there was an error |
|
1987 | 1988 | # compiling the code. This allows us to stop processing right |
|
1988 | 1989 | # away, so the user gets the error message at the right place. |
|
1989 | 1990 | if more is None: |
|
1990 | 1991 | break |
|
1991 | 1992 | else: |
|
1992 | 1993 | self.input_hist_raw.append("\n") |
|
1993 | 1994 | # final newline in case the input didn't have it, so that the code |
|
1994 | 1995 | # actually does get executed |
|
1995 | 1996 | if more: |
|
1996 | 1997 | self.push('\n') |
|
1997 | 1998 | |
|
1998 | 1999 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1999 | 2000 | """Compile and run some source in the interpreter. |
|
2000 | 2001 | |
|
2001 | 2002 | Arguments are as for compile_command(). |
|
2002 | 2003 | |
|
2003 | 2004 | One several things can happen: |
|
2004 | 2005 | |
|
2005 | 2006 | 1) The input is incorrect; compile_command() raised an |
|
2006 | 2007 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2007 | 2008 | will be printed by calling the showsyntaxerror() method. |
|
2008 | 2009 | |
|
2009 | 2010 | 2) The input is incomplete, and more input is required; |
|
2010 | 2011 | compile_command() returned None. Nothing happens. |
|
2011 | 2012 | |
|
2012 | 2013 | 3) The input is complete; compile_command() returned a code |
|
2013 | 2014 | object. The code is executed by calling self.runcode() (which |
|
2014 | 2015 | also handles run-time exceptions, except for SystemExit). |
|
2015 | 2016 | |
|
2016 | 2017 | The return value is: |
|
2017 | 2018 | |
|
2018 | 2019 | - True in case 2 |
|
2019 | 2020 | |
|
2020 | 2021 | - False in the other cases, unless an exception is raised, where |
|
2021 | 2022 | None is returned instead. This can be used by external callers to |
|
2022 | 2023 | know whether to continue feeding input or not. |
|
2023 | 2024 | |
|
2024 | 2025 | The return value can be used to decide whether to use sys.ps1 or |
|
2025 | 2026 | sys.ps2 to prompt the next line.""" |
|
2026 | 2027 | |
|
2027 | 2028 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
2028 | 2029 | # this allows execution of indented pasted code. It is tempting |
|
2029 | 2030 | # to add '\n' at the end of source to run commands like ' a=1' |
|
2030 | 2031 | # directly, but this fails for more complicated scenarios |
|
2031 | 2032 | source=source.encode(self.stdin_encoding) |
|
2032 | 2033 | if source[:1] in [' ', '\t']: |
|
2033 | 2034 | source = 'if 1:\n%s' % source |
|
2034 | 2035 | |
|
2035 | 2036 | try: |
|
2036 | 2037 | code = self.compile(source,filename,symbol) |
|
2037 | 2038 | except (OverflowError, SyntaxError, ValueError, TypeError): |
|
2038 | 2039 | # Case 1 |
|
2039 | 2040 | self.showsyntaxerror(filename) |
|
2040 | 2041 | return None |
|
2041 | 2042 | |
|
2042 | 2043 | if code is None: |
|
2043 | 2044 | # Case 2 |
|
2044 | 2045 | return True |
|
2045 | 2046 | |
|
2046 | 2047 | # Case 3 |
|
2047 | 2048 | # We store the code object so that threaded shells and |
|
2048 | 2049 | # custom exception handlers can access all this info if needed. |
|
2049 | 2050 | # The source corresponding to this can be obtained from the |
|
2050 | 2051 | # buffer attribute as '\n'.join(self.buffer). |
|
2051 | 2052 | self.code_to_run = code |
|
2052 | 2053 | # now actually execute the code object |
|
2053 | 2054 | if self.runcode(code) == 0: |
|
2054 | 2055 | return False |
|
2055 | 2056 | else: |
|
2056 | 2057 | return None |
|
2057 | 2058 | |
|
2058 | 2059 | def runcode(self,code_obj): |
|
2059 | 2060 | """Execute a code object. |
|
2060 | 2061 | |
|
2061 | 2062 | When an exception occurs, self.showtraceback() is called to display a |
|
2062 | 2063 | traceback. |
|
2063 | 2064 | |
|
2064 | 2065 | Return value: a flag indicating whether the code to be run completed |
|
2065 | 2066 | successfully: |
|
2066 | 2067 | |
|
2067 | 2068 | - 0: successful execution. |
|
2068 | 2069 | - 1: an error occurred. |
|
2069 | 2070 | """ |
|
2070 | 2071 | |
|
2071 | 2072 | # Set our own excepthook in case the user code tries to call it |
|
2072 | 2073 | # directly, so that the IPython crash handler doesn't get triggered |
|
2073 | 2074 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2074 | 2075 | |
|
2075 | 2076 | # we save the original sys.excepthook in the instance, in case config |
|
2076 | 2077 | # code (such as magics) needs access to it. |
|
2077 | 2078 | self.sys_excepthook = old_excepthook |
|
2078 | 2079 | outflag = 1 # happens in more places, so it's easier as default |
|
2079 | 2080 | try: |
|
2080 | 2081 | try: |
|
2081 | 2082 | self.hooks.pre_runcode_hook() |
|
2082 | 2083 | exec code_obj in self.user_global_ns, self.user_ns |
|
2083 | 2084 | finally: |
|
2084 | 2085 | # Reset our crash handler in place |
|
2085 | 2086 | sys.excepthook = old_excepthook |
|
2086 | 2087 | except SystemExit: |
|
2087 | 2088 | self.resetbuffer() |
|
2088 | 2089 | self.showtraceback() |
|
2089 | 2090 | warn("Type %exit or %quit to exit IPython " |
|
2090 | 2091 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
2091 | 2092 | except self.custom_exceptions: |
|
2092 | 2093 | etype,value,tb = sys.exc_info() |
|
2093 | 2094 | self.CustomTB(etype,value,tb) |
|
2094 | 2095 | except: |
|
2095 | 2096 | self.showtraceback() |
|
2096 | 2097 | else: |
|
2097 | 2098 | outflag = 0 |
|
2098 | 2099 | if softspace(sys.stdout, 0): |
|
2099 | 2100 | |
|
2100 | 2101 | # Flush out code object which has been run (and source) |
|
2101 | 2102 | self.code_to_run = None |
|
2102 | 2103 | return outflag |
|
2103 | 2104 | |
|
2104 | 2105 | def push(self, line): |
|
2105 | 2106 | """Push a line to the interpreter. |
|
2106 | 2107 | |
|
2107 | 2108 | The line should not have a trailing newline; it may have |
|
2108 | 2109 | internal newlines. The line is appended to a buffer and the |
|
2109 | 2110 | interpreter's runsource() method is called with the |
|
2110 | 2111 | concatenated contents of the buffer as source. If this |
|
2111 | 2112 | indicates that the command was executed or invalid, the buffer |
|
2112 | 2113 | is reset; otherwise, the command is incomplete, and the buffer |
|
2113 | 2114 | is left as it was after the line was appended. The return |
|
2114 | 2115 | value is 1 if more input is required, 0 if the line was dealt |
|
2115 | 2116 | with in some way (this is the same as runsource()). |
|
2116 | 2117 | """ |
|
2117 | 2118 | |
|
2118 | 2119 | # autoindent management should be done here, and not in the |
|
2119 | 2120 | # interactive loop, since that one is only seen by keyboard input. We |
|
2120 | 2121 | # need this done correctly even for code run via runlines (which uses |
|
2121 | 2122 | # push). |
|
2122 | 2123 | |
|
2123 | 2124 | #print 'push line: <%s>' % line # dbg |
|
2124 | 2125 | for subline in line.splitlines(): |
|
2125 | 2126 | self.autoindent_update(subline) |
|
2126 | 2127 | self.buffer.append(line) |
|
2127 | 2128 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2128 | 2129 | if not more: |
|
2129 | 2130 | self.resetbuffer() |
|
2130 | 2131 | return more |
|
2131 | 2132 | |
|
2132 | 2133 | def split_user_input(self, line): |
|
2133 | 2134 | # This is really a hold-over to support ipapi and some extensions |
|
2134 | 2135 | return prefilter.splitUserInput(line) |
|
2135 | 2136 | |
|
2136 | 2137 | def resetbuffer(self): |
|
2137 | 2138 | """Reset the input buffer.""" |
|
2138 | 2139 | self.buffer[:] = [] |
|
2139 | 2140 | |
|
2140 | 2141 | def raw_input(self,prompt='',continue_prompt=False): |
|
2141 | 2142 | """Write a prompt and read a line. |
|
2142 | 2143 | |
|
2143 | 2144 | The returned line does not include the trailing newline. |
|
2144 | 2145 | When the user enters the EOF key sequence, EOFError is raised. |
|
2145 | 2146 | |
|
2146 | 2147 | Optional inputs: |
|
2147 | 2148 | |
|
2148 | 2149 | - prompt(''): a string to be printed to prompt the user. |
|
2149 | 2150 | |
|
2150 | 2151 | - continue_prompt(False): whether this line is the first one or a |
|
2151 | 2152 | continuation in a sequence of inputs. |
|
2152 | 2153 | """ |
|
2153 | 2154 | |
|
2154 | 2155 | # Code run by the user may have modified the readline completer state. |
|
2155 | 2156 | # We must ensure that our completer is back in place. |
|
2156 | 2157 | if self.has_readline: |
|
2157 | 2158 | self.set_completer() |
|
2158 | 2159 | |
|
2159 | 2160 | try: |
|
2160 | 2161 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
2161 | 2162 | except ValueError: |
|
2162 | 2163 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
2163 | 2164 | " or sys.stdout.close()!\nExiting IPython!") |
|
2164 | 2165 | self.ask_exit() |
|
2165 | 2166 | return "" |
|
2166 | 2167 | |
|
2167 | 2168 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2168 | 2169 | # than necessary. We do this by trimming out the auto-indent initial |
|
2169 | 2170 | # spaces, if the user's actual input started itself with whitespace. |
|
2170 | 2171 | #debugx('self.buffer[-1]') |
|
2171 | 2172 | |
|
2172 | 2173 | if self.autoindent: |
|
2173 | 2174 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2174 | 2175 | line = line[self.indent_current_nsp:] |
|
2175 | 2176 | self.indent_current_nsp = 0 |
|
2176 | 2177 | |
|
2177 | 2178 | # store the unfiltered input before the user has any chance to modify |
|
2178 | 2179 | # it. |
|
2179 | 2180 | if line.strip(): |
|
2180 | 2181 | if continue_prompt: |
|
2181 | 2182 | self.input_hist_raw[-1] += '%s\n' % line |
|
2182 | 2183 | if self.has_readline: # and some config option is set? |
|
2183 | 2184 | try: |
|
2184 | 2185 | histlen = self.readline.get_current_history_length() |
|
2185 | 2186 | if histlen > 1: |
|
2186 | 2187 | newhist = self.input_hist_raw[-1].rstrip() |
|
2187 | 2188 | self.readline.remove_history_item(histlen-1) |
|
2188 | 2189 | self.readline.replace_history_item(histlen-2, |
|
2189 | 2190 | newhist.encode(self.stdin_encoding)) |
|
2190 | 2191 | except AttributeError: |
|
2191 | 2192 | pass # re{move,place}_history_item are new in 2.4. |
|
2192 | 2193 | else: |
|
2193 | 2194 | self.input_hist_raw.append('%s\n' % line) |
|
2194 | 2195 | # only entries starting at first column go to shadow history |
|
2195 | 2196 | if line.lstrip() == line: |
|
2196 | 2197 | self.shadowhist.add(line.strip()) |
|
2197 | 2198 | elif not continue_prompt: |
|
2198 | 2199 | self.input_hist_raw.append('\n') |
|
2199 | 2200 | try: |
|
2200 | 2201 | lineout = self.prefilter(line,continue_prompt) |
|
2201 | 2202 | except: |
|
2202 | 2203 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2203 | 2204 | # can't take all of ipython with it. |
|
2204 | 2205 | self.showtraceback() |
|
2205 | 2206 | return '' |
|
2206 | 2207 | else: |
|
2207 | 2208 | return lineout |
|
2208 | 2209 | |
|
2209 | 2210 | def _prefilter(self, line, continue_prompt): |
|
2210 | 2211 | """Calls different preprocessors, depending on the form of line.""" |
|
2211 | 2212 | |
|
2212 | 2213 | # All handlers *must* return a value, even if it's blank (''). |
|
2213 | 2214 | |
|
2214 | 2215 | # Lines are NOT logged here. Handlers should process the line as |
|
2215 | 2216 | # needed, update the cache AND log it (so that the input cache array |
|
2216 | 2217 | # stays synced). |
|
2217 | 2218 | |
|
2218 | 2219 | #..................................................................... |
|
2219 | 2220 | # Code begins |
|
2220 | 2221 | |
|
2221 | 2222 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2222 | 2223 | |
|
2223 | 2224 | # save the line away in case we crash, so the post-mortem handler can |
|
2224 | 2225 | # record it |
|
2225 | 2226 | self._last_input_line = line |
|
2226 | 2227 | |
|
2227 | 2228 | #print '***line: <%s>' % line # dbg |
|
2228 | 2229 | |
|
2229 | 2230 | if not line: |
|
2230 | 2231 | # Return immediately on purely empty lines, so that if the user |
|
2231 | 2232 | # previously typed some whitespace that started a continuation |
|
2232 | 2233 | # prompt, he can break out of that loop with just an empty line. |
|
2233 | 2234 | # This is how the default python prompt works. |
|
2234 | 2235 | |
|
2235 | 2236 | # Only return if the accumulated input buffer was just whitespace! |
|
2236 | 2237 | if ''.join(self.buffer).isspace(): |
|
2237 | 2238 | self.buffer[:] = [] |
|
2238 | 2239 | return '' |
|
2239 | 2240 | |
|
2240 | 2241 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
2241 | 2242 | |
|
2242 | 2243 | # the input history needs to track even empty lines |
|
2243 | 2244 | stripped = line.strip() |
|
2244 | 2245 | |
|
2245 | 2246 | if not stripped: |
|
2246 | 2247 | if not continue_prompt: |
|
2247 | 2248 | self.outputcache.prompt_count -= 1 |
|
2248 | 2249 | return self.handle_normal(line_info) |
|
2249 | 2250 | |
|
2250 | 2251 | # print '***cont',continue_prompt # dbg |
|
2251 | 2252 | # special handlers are only allowed for single line statements |
|
2252 | 2253 | if continue_prompt and not self.rc.multi_line_specials: |
|
2253 | 2254 | return self.handle_normal(line_info) |
|
2254 | 2255 | |
|
2255 | 2256 | |
|
2256 | 2257 | # See whether any pre-existing handler can take care of it |
|
2257 | 2258 | rewritten = self.hooks.input_prefilter(stripped) |
|
2258 | 2259 | if rewritten != stripped: # ok, some prefilter did something |
|
2259 | 2260 | rewritten = line_info.pre + rewritten # add indentation |
|
2260 | 2261 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
2261 | 2262 | continue_prompt)) |
|
2262 | 2263 | |
|
2263 | 2264 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2264 | 2265 | |
|
2265 | 2266 | return prefilter.prefilter(line_info, self) |
|
2266 | 2267 | |
|
2267 | 2268 | |
|
2268 | 2269 | def _prefilter_dumb(self, line, continue_prompt): |
|
2269 | 2270 | """simple prefilter function, for debugging""" |
|
2270 | 2271 | return self.handle_normal(line,continue_prompt) |
|
2271 | 2272 | |
|
2272 | 2273 | |
|
2273 | 2274 | def multiline_prefilter(self, line, continue_prompt): |
|
2274 | 2275 | """ Run _prefilter for each line of input |
|
2275 | 2276 | |
|
2276 | 2277 | Covers cases where there are multiple lines in the user entry, |
|
2277 | 2278 | which is the case when the user goes back to a multiline history |
|
2278 | 2279 | entry and presses enter. |
|
2279 | 2280 | |
|
2280 | 2281 | """ |
|
2281 | 2282 | out = [] |
|
2282 | 2283 | for l in line.rstrip('\n').split('\n'): |
|
2283 | 2284 | out.append(self._prefilter(l, continue_prompt)) |
|
2284 | 2285 | return '\n'.join(out) |
|
2285 | 2286 | |
|
2286 | 2287 | # Set the default prefilter() function (this can be user-overridden) |
|
2287 | 2288 | prefilter = multiline_prefilter |
|
2288 | 2289 | |
|
2289 | 2290 | def handle_normal(self,line_info): |
|
2290 | 2291 | """Handle normal input lines. Use as a template for handlers.""" |
|
2291 | 2292 | |
|
2292 | 2293 | # With autoindent on, we need some way to exit the input loop, and I |
|
2293 | 2294 | # don't want to force the user to have to backspace all the way to |
|
2294 | 2295 | # clear the line. The rule will be in this case, that either two |
|
2295 | 2296 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2296 | 2297 | # of a size different to the indent level, will exit the input loop. |
|
2297 | 2298 | line = line_info.line |
|
2298 | 2299 | continue_prompt = line_info.continue_prompt |
|
2299 | 2300 | |
|
2300 | 2301 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2301 | 2302 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2302 | 2303 | (self.buffer[-1]).isspace() )): |
|
2303 | 2304 | line = '' |
|
2304 | 2305 | |
|
2305 | 2306 | self.log(line,line,continue_prompt) |
|
2306 | 2307 | return line |
|
2307 | 2308 | |
|
2308 | 2309 | def handle_alias(self,line_info): |
|
2309 | 2310 | """Handle alias input lines. """ |
|
2310 | 2311 | tgt = self.alias_table[line_info.iFun] |
|
2311 | 2312 | # print "=>",tgt #dbg |
|
2312 | 2313 | if callable(tgt): |
|
2313 | 2314 | if '$' in line_info.line: |
|
2314 | 2315 | call_meth = '(_ip, _ip.itpl(%s))' |
|
2315 | 2316 | else: |
|
2316 | 2317 | call_meth = '(_ip,%s)' |
|
2317 | 2318 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, |
|
2318 | 2319 | line_info.iFun, |
|
2319 | 2320 | make_quoted_expr(line_info.line)) |
|
2320 | 2321 | else: |
|
2321 | 2322 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2322 | 2323 | |
|
2323 | 2324 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2324 | 2325 | # aliases won't work in indented sections. |
|
2325 | 2326 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2326 | 2327 | make_quoted_expr( transformed )) |
|
2327 | 2328 | |
|
2328 | 2329 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
2329 | 2330 | #print 'line out:',line_out # dbg |
|
2330 | 2331 | return line_out |
|
2331 | 2332 | |
|
2332 | 2333 | def handle_shell_escape(self, line_info): |
|
2333 | 2334 | """Execute the line in a shell, empty return value""" |
|
2334 | 2335 | #print 'line in :', `line` # dbg |
|
2335 | 2336 | line = line_info.line |
|
2336 | 2337 | if line.lstrip().startswith('!!'): |
|
2337 | 2338 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
2338 | 2339 | # call to %sx and the actual command to be executed, so |
|
2339 | 2340 | # handle_magic can work correctly. Note that this works even if |
|
2340 | 2341 | # the line is indented, so it handles multi_line_specials |
|
2341 | 2342 | # properly. |
|
2342 | 2343 | new_rest = line.lstrip()[2:] |
|
2343 | 2344 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
2344 | 2345 | line_info.iFun = 'sx' |
|
2345 | 2346 | line_info.theRest = new_rest |
|
2346 | 2347 | return self.handle_magic(line_info) |
|
2347 | 2348 | else: |
|
2348 | 2349 | cmd = line.lstrip().lstrip('!') |
|
2349 | 2350 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2350 | 2351 | make_quoted_expr(cmd)) |
|
2351 | 2352 | # update cache/log and return |
|
2352 | 2353 | self.log(line,line_out,line_info.continue_prompt) |
|
2353 | 2354 | return line_out |
|
2354 | 2355 | |
|
2355 | 2356 | def handle_magic(self, line_info): |
|
2356 | 2357 | """Execute magic functions.""" |
|
2357 | 2358 | iFun = line_info.iFun |
|
2358 | 2359 | theRest = line_info.theRest |
|
2359 | 2360 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
2360 | 2361 | make_quoted_expr(iFun + " " + theRest)) |
|
2361 | 2362 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
2362 | 2363 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2363 | 2364 | return cmd |
|
2364 | 2365 | |
|
2365 | 2366 | def handle_auto(self, line_info): |
|
2366 | 2367 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2367 | 2368 | |
|
2368 | 2369 | line = line_info.line |
|
2369 | 2370 | iFun = line_info.iFun |
|
2370 | 2371 | theRest = line_info.theRest |
|
2371 | 2372 | pre = line_info.pre |
|
2372 | 2373 | continue_prompt = line_info.continue_prompt |
|
2373 | 2374 | obj = line_info.ofind(self)['obj'] |
|
2374 | 2375 | |
|
2375 | 2376 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2376 | 2377 | |
|
2377 | 2378 | # This should only be active for single-line input! |
|
2378 | 2379 | if continue_prompt: |
|
2379 | 2380 | self.log(line,line,continue_prompt) |
|
2380 | 2381 | return line |
|
2381 | 2382 | |
|
2382 | 2383 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) |
|
2383 | 2384 | auto_rewrite = True |
|
2384 | 2385 | |
|
2385 | 2386 | if pre == self.ESC_QUOTE: |
|
2386 | 2387 | # Auto-quote splitting on whitespace |
|
2387 | 2388 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2388 | 2389 | elif pre == self.ESC_QUOTE2: |
|
2389 | 2390 | # Auto-quote whole string |
|
2390 | 2391 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2391 | 2392 | elif pre == self.ESC_PAREN: |
|
2392 | 2393 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2393 | 2394 | else: |
|
2394 | 2395 | # Auto-paren. |
|
2395 | 2396 | # We only apply it to argument-less calls if the autocall |
|
2396 | 2397 | # parameter is set to 2. We only need to check that autocall is < |
|
2397 | 2398 | # 2, since this function isn't called unless it's at least 1. |
|
2398 | 2399 | if not theRest and (self.rc.autocall < 2) and not force_auto: |
|
2399 | 2400 | newcmd = '%s %s' % (iFun,theRest) |
|
2400 | 2401 | auto_rewrite = False |
|
2401 | 2402 | else: |
|
2402 | 2403 | if not force_auto and theRest.startswith('['): |
|
2403 | 2404 | if hasattr(obj,'__getitem__'): |
|
2404 | 2405 | # Don't autocall in this case: item access for an object |
|
2405 | 2406 | # which is BOTH callable and implements __getitem__. |
|
2406 | 2407 | newcmd = '%s %s' % (iFun,theRest) |
|
2407 | 2408 | auto_rewrite = False |
|
2408 | 2409 | else: |
|
2409 | 2410 | # if the object doesn't support [] access, go ahead and |
|
2410 | 2411 | # autocall |
|
2411 | 2412 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2412 | 2413 | elif theRest.endswith(';'): |
|
2413 | 2414 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2414 | 2415 | else: |
|
2415 | 2416 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2416 | 2417 | |
|
2417 | 2418 | if auto_rewrite: |
|
2418 | 2419 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2419 | 2420 | |
|
2420 | 2421 | try: |
|
2421 | 2422 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2422 | 2423 | # we use it and only print uncolored rewrite if we have unicode |
|
2423 | 2424 | rw = str(rw) |
|
2424 | 2425 | print >>Term.cout, rw |
|
2425 | 2426 | except UnicodeEncodeError: |
|
2426 | 2427 | print "-------------->" + newcmd |
|
2427 | 2428 | |
|
2428 | 2429 | # log what is now valid Python, not the actual user input (without the |
|
2429 | 2430 | # final newline) |
|
2430 | 2431 | self.log(line,newcmd,continue_prompt) |
|
2431 | 2432 | return newcmd |
|
2432 | 2433 | |
|
2433 | 2434 | def handle_help(self, line_info): |
|
2434 | 2435 | """Try to get some help for the object. |
|
2435 | 2436 | |
|
2436 | 2437 | obj? or ?obj -> basic information. |
|
2437 | 2438 | obj?? or ??obj -> more details. |
|
2438 | 2439 | """ |
|
2439 | 2440 | |
|
2440 | 2441 | line = line_info.line |
|
2441 | 2442 | # We need to make sure that we don't process lines which would be |
|
2442 | 2443 | # otherwise valid python, such as "x=1 # what?" |
|
2443 | 2444 | try: |
|
2444 | 2445 | codeop.compile_command(line) |
|
2445 | 2446 | except SyntaxError: |
|
2446 | 2447 | # We should only handle as help stuff which is NOT valid syntax |
|
2447 | 2448 | if line[0]==self.ESC_HELP: |
|
2448 | 2449 | line = line[1:] |
|
2449 | 2450 | elif line[-1]==self.ESC_HELP: |
|
2450 | 2451 | line = line[:-1] |
|
2451 | 2452 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
2452 | 2453 | if line: |
|
2453 | 2454 | #print 'line:<%r>' % line # dbg |
|
2454 | 2455 | self.magic_pinfo(line) |
|
2455 | 2456 | else: |
|
2456 | 2457 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2457 | 2458 | return '' # Empty string is needed here! |
|
2458 | 2459 | except: |
|
2459 | 2460 | # Pass any other exceptions through to the normal handler |
|
2460 | 2461 | return self.handle_normal(line_info) |
|
2461 | 2462 | else: |
|
2462 | 2463 | # If the code compiles ok, we should handle it normally |
|
2463 | 2464 | return self.handle_normal(line_info) |
|
2464 | 2465 | |
|
2465 | 2466 | def getapi(self): |
|
2466 | 2467 | """ Get an IPApi object for this shell instance |
|
2467 | 2468 | |
|
2468 | 2469 | Getting an IPApi object is always preferable to accessing the shell |
|
2469 | 2470 | directly, but this holds true especially for extensions. |
|
2470 | 2471 | |
|
2471 | 2472 | It should always be possible to implement an extension with IPApi |
|
2472 | 2473 | alone. If not, contact maintainer to request an addition. |
|
2473 | 2474 | |
|
2474 | 2475 | """ |
|
2475 | 2476 | return self.api |
|
2476 | 2477 | |
|
2477 | 2478 | def handle_emacs(self, line_info): |
|
2478 | 2479 | """Handle input lines marked by python-mode.""" |
|
2479 | 2480 | |
|
2480 | 2481 | # Currently, nothing is done. Later more functionality can be added |
|
2481 | 2482 | # here if needed. |
|
2482 | 2483 | |
|
2483 | 2484 | # The input cache shouldn't be updated |
|
2484 | 2485 | return line_info.line |
|
2485 | 2486 | |
|
2486 | 2487 | |
|
2487 | 2488 | def mktempfile(self,data=None): |
|
2488 | 2489 | """Make a new tempfile and return its filename. |
|
2489 | 2490 | |
|
2490 | 2491 | This makes a call to tempfile.mktemp, but it registers the created |
|
2491 | 2492 | filename internally so ipython cleans it up at exit time. |
|
2492 | 2493 | |
|
2493 | 2494 | Optional inputs: |
|
2494 | 2495 | |
|
2495 | 2496 | - data(None): if data is given, it gets written out to the temp file |
|
2496 | 2497 | immediately, and the file is closed again.""" |
|
2497 | 2498 | |
|
2498 | 2499 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2499 | 2500 | self.tempfiles.append(filename) |
|
2500 | 2501 | |
|
2501 | 2502 | if data: |
|
2502 | 2503 | tmp_file = open(filename,'w') |
|
2503 | 2504 | tmp_file.write(data) |
|
2504 | 2505 | tmp_file.close() |
|
2505 | 2506 | return filename |
|
2506 | 2507 | |
|
2507 | 2508 | def write(self,data): |
|
2508 | 2509 | """Write a string to the default output""" |
|
2509 | 2510 | Term.cout.write(data) |
|
2510 | 2511 | |
|
2511 | 2512 | def write_err(self,data): |
|
2512 | 2513 | """Write a string to the default error output""" |
|
2513 | 2514 | Term.cerr.write(data) |
|
2514 | 2515 | |
|
2515 | 2516 | def ask_exit(self): |
|
2516 | 2517 | """ Call for exiting. Can be overiden and used as a callback. """ |
|
2517 | 2518 | self.exit_now = True |
|
2518 | 2519 | |
|
2519 | 2520 | def exit(self): |
|
2520 | 2521 | """Handle interactive exit. |
|
2521 | 2522 | |
|
2522 | 2523 | This method calls the ask_exit callback.""" |
|
2523 | 2524 | |
|
2524 | 2525 | if self.rc.confirm_exit: |
|
2525 | 2526 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2526 | 2527 | self.ask_exit() |
|
2527 | 2528 | else: |
|
2528 | 2529 | self.ask_exit() |
|
2529 | 2530 | |
|
2530 | 2531 | def safe_execfile(self,fname,*where,**kw): |
|
2531 | 2532 | """A safe version of the builtin execfile(). |
|
2532 | 2533 | |
|
2533 | 2534 | This version will never throw an exception, and knows how to handle |
|
2534 | 2535 | ipython logs as well. |
|
2535 | 2536 | |
|
2536 | 2537 | :Parameters: |
|
2537 | 2538 | fname : string |
|
2538 | 2539 | Name of the file to be executed. |
|
2539 | 2540 | |
|
2540 | 2541 | where : tuple |
|
2541 | 2542 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2542 | 2543 | If only one is given, it is passed as both. |
|
2543 | 2544 | |
|
2544 | 2545 | :Keywords: |
|
2545 | 2546 | islog : boolean (False) |
|
2546 | 2547 | |
|
2547 | 2548 | quiet : boolean (True) |
|
2548 | 2549 | |
|
2549 | 2550 | exit_ignore : boolean (False) |
|
2550 | 2551 | """ |
|
2551 | 2552 | |
|
2552 | 2553 | def syspath_cleanup(): |
|
2553 | 2554 | """Internal cleanup routine for sys.path.""" |
|
2554 | 2555 | if add_dname: |
|
2555 | 2556 | try: |
|
2556 | 2557 | sys.path.remove(dname) |
|
2557 | 2558 | except ValueError: |
|
2558 | 2559 | # For some reason the user has already removed it, ignore. |
|
2559 | 2560 | pass |
|
2560 | 2561 | |
|
2561 | 2562 | fname = os.path.expanduser(fname) |
|
2562 | 2563 | |
|
2563 | 2564 | # Find things also in current directory. This is needed to mimic the |
|
2564 | 2565 | # behavior of running a script from the system command line, where |
|
2565 | 2566 | # Python inserts the script's directory into sys.path |
|
2566 | 2567 | dname = os.path.dirname(os.path.abspath(fname)) |
|
2567 | 2568 | add_dname = False |
|
2568 | 2569 | if dname not in sys.path: |
|
2569 | 2570 | sys.path.insert(0,dname) |
|
2570 | 2571 | add_dname = True |
|
2571 | 2572 | |
|
2572 | 2573 | try: |
|
2573 | 2574 | xfile = open(fname) |
|
2574 | 2575 | except: |
|
2575 | 2576 | print >> Term.cerr, \ |
|
2576 | 2577 | 'Could not open file <%s> for safe execution.' % fname |
|
2577 | 2578 | syspath_cleanup() |
|
2578 | 2579 | return None |
|
2579 | 2580 | |
|
2580 | 2581 | kw.setdefault('islog',0) |
|
2581 | 2582 | kw.setdefault('quiet',1) |
|
2582 | 2583 | kw.setdefault('exit_ignore',0) |
|
2583 | 2584 | |
|
2584 | 2585 | first = xfile.readline() |
|
2585 | 2586 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2586 | 2587 | xfile.close() |
|
2587 | 2588 | # line by line execution |
|
2588 | 2589 | if first.startswith(loghead) or kw['islog']: |
|
2589 | 2590 | print 'Loading log file <%s> one line at a time...' % fname |
|
2590 | 2591 | if kw['quiet']: |
|
2591 | 2592 | stdout_save = sys.stdout |
|
2592 | 2593 | sys.stdout = StringIO.StringIO() |
|
2593 | 2594 | try: |
|
2594 | 2595 | globs,locs = where[0:2] |
|
2595 | 2596 | except: |
|
2596 | 2597 | try: |
|
2597 | 2598 | globs = locs = where[0] |
|
2598 | 2599 | except: |
|
2599 | 2600 | globs = locs = globals() |
|
2600 | 2601 | badblocks = [] |
|
2601 | 2602 | |
|
2602 | 2603 | # we also need to identify indented blocks of code when replaying |
|
2603 | 2604 | # logs and put them together before passing them to an exec |
|
2604 | 2605 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2605 | 2606 | # file. It's easiest if we swallow the whole thing in memory |
|
2606 | 2607 | # first, and manually walk through the lines list moving the |
|
2607 | 2608 | # counter ourselves. |
|
2608 | 2609 | indent_re = re.compile('\s+\S') |
|
2609 | 2610 | xfile = open(fname) |
|
2610 | 2611 | filelines = xfile.readlines() |
|
2611 | 2612 | xfile.close() |
|
2612 | 2613 | nlines = len(filelines) |
|
2613 | 2614 | lnum = 0 |
|
2614 | 2615 | while lnum < nlines: |
|
2615 | 2616 | line = filelines[lnum] |
|
2616 | 2617 | lnum += 1 |
|
2617 | 2618 | # don't re-insert logger status info into cache |
|
2618 | 2619 | if line.startswith('#log#'): |
|
2619 | 2620 | continue |
|
2620 | 2621 | else: |
|
2621 | 2622 | # build a block of code (maybe a single line) for execution |
|
2622 | 2623 | block = line |
|
2623 | 2624 | try: |
|
2624 | 2625 | next = filelines[lnum] # lnum has already incremented |
|
2625 | 2626 | except: |
|
2626 | 2627 | next = None |
|
2627 | 2628 | while next and indent_re.match(next): |
|
2628 | 2629 | block += next |
|
2629 | 2630 | lnum += 1 |
|
2630 | 2631 | try: |
|
2631 | 2632 | next = filelines[lnum] |
|
2632 | 2633 | except: |
|
2633 | 2634 | next = None |
|
2634 | 2635 | # now execute the block of one or more lines |
|
2635 | 2636 | try: |
|
2636 | 2637 | exec block in globs,locs |
|
2637 | 2638 | except SystemExit: |
|
2638 | 2639 | pass |
|
2639 | 2640 | except: |
|
2640 | 2641 | badblocks.append(block.rstrip()) |
|
2641 | 2642 | if kw['quiet']: # restore stdout |
|
2642 | 2643 | sys.stdout.close() |
|
2643 | 2644 | sys.stdout = stdout_save |
|
2644 | 2645 | print 'Finished replaying log file <%s>' % fname |
|
2645 | 2646 | if badblocks: |
|
2646 | 2647 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2647 | 2648 | '<%s> reported errors:' % fname) |
|
2648 | 2649 | |
|
2649 | 2650 | for badline in badblocks: |
|
2650 | 2651 | print >> sys.stderr, badline |
|
2651 | 2652 | else: # regular file execution |
|
2652 | 2653 | try: |
|
2653 | 2654 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
2654 | 2655 | # Work around a bug in Python for Windows. The bug was |
|
2655 | 2656 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
2656 | 2657 | # SVN Python as of March/07. For details, see: |
|
2657 | 2658 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
2658 | 2659 | try: |
|
2659 | 2660 | globs,locs = where[0:2] |
|
2660 | 2661 | except: |
|
2661 | 2662 | try: |
|
2662 | 2663 | globs = locs = where[0] |
|
2663 | 2664 | except: |
|
2664 | 2665 | globs = locs = globals() |
|
2665 | 2666 | exec file(fname) in globs,locs |
|
2666 | 2667 | else: |
|
2667 | 2668 | execfile(fname,*where) |
|
2668 | 2669 | except SyntaxError: |
|
2669 | 2670 | self.showsyntaxerror() |
|
2670 | 2671 | warn('Failure executing file: <%s>' % fname) |
|
2671 | 2672 | except SystemExit,status: |
|
2672 | 2673 | # Code that correctly sets the exit status flag to success (0) |
|
2673 | 2674 | # shouldn't be bothered with a traceback. Note that a plain |
|
2674 | 2675 | # sys.exit() does NOT set the message to 0 (it's empty) so that |
|
2675 | 2676 | # will still get a traceback. Note that the structure of the |
|
2676 | 2677 | # SystemExit exception changed between Python 2.4 and 2.5, so |
|
2677 | 2678 | # the checks must be done in a version-dependent way. |
|
2678 | 2679 | show = False |
|
2679 | 2680 | |
|
2680 | 2681 | if sys.version_info[:2] > (2,5): |
|
2681 | 2682 | if status.message!=0 and not kw['exit_ignore']: |
|
2682 | 2683 | show = True |
|
2683 | 2684 | else: |
|
2684 | 2685 | if status.code and not kw['exit_ignore']: |
|
2685 | 2686 | show = True |
|
2686 | 2687 | if show: |
|
2687 | 2688 | self.showtraceback() |
|
2688 | 2689 | warn('Failure executing file: <%s>' % fname) |
|
2689 | 2690 | except: |
|
2690 | 2691 | self.showtraceback() |
|
2691 | 2692 | warn('Failure executing file: <%s>' % fname) |
|
2692 | 2693 | |
|
2693 | 2694 | syspath_cleanup() |
|
2694 | 2695 | |
|
2695 | 2696 | #************************* end of file <iplib.py> ***************************** |
@@ -1,278 +1,291 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """Tests for genutils.py""" |
|
4 | 4 | |
|
5 | 5 | __docformat__ = "restructuredtext en" |
|
6 | 6 | |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
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 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | from IPython import genutils | |
|
19 | from IPython.testing.decorators import skipif, skip_if_not_win32 | |
|
20 | from nose import with_setup | |
|
21 | from nose.tools import raises | |
|
18 | # stdlib | |
|
19 | import os | |
|
20 | import shutil | |
|
21 | import sys | |
|
22 | import tempfile | |
|
22 | 23 | |
|
23 | 24 | from os.path import join, abspath, split |
|
24 | import os, sys, IPython | |
|
25 | ||
|
26 | # third-party | |
|
25 | 27 | import nose.tools as nt |
|
26 | 28 | |
|
27 | env = os.environ | |
|
29 | from nose import with_setup | |
|
30 | from nose.tools import raises | |
|
28 | 31 | |
|
32 | # Our own | |
|
33 | import IPython | |
|
34 | from IPython import genutils | |
|
35 | from IPython.testing.decorators import skipif, skip_if_not_win32 | |
|
36 | ||
|
37 | # Platform-dependent imports | |
|
29 | 38 | try: |
|
30 | 39 | import _winreg as wreg |
|
31 | 40 | except ImportError: |
|
32 | 41 | #Fake _winreg module on none windows platforms |
|
33 | 42 | import new |
|
34 | 43 | sys.modules["_winreg"] = new.module("_winreg") |
|
35 | 44 | import _winreg as wreg |
|
36 | 45 | #Add entries that needs to be stubbed by the testing code |
|
37 | 46 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
38 | 47 | |
|
39 | test_file_path = split(abspath(__file__))[0] | |
|
40 | ||
|
48 | #----------------------------------------------------------------------------- | |
|
49 | # Globals | |
|
50 | #----------------------------------------------------------------------------- | |
|
51 | env = os.environ | |
|
52 | TEST_FILE_PATH = split(abspath(__file__))[0] | |
|
53 | TMP_TEST_DIR = tempfile.mkdtemp() | |
|
54 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | |
|
55 | IP_TEST_DIR = join(HOME_TEST_DIR,'_ipython') | |
|
41 | 56 | # |
|
42 | 57 | # Setup/teardown functions/decorators |
|
43 | 58 | # |
|
44 | 59 | |
|
45 | ||
|
46 | 60 | def setup(): |
|
47 | 61 | """Setup testenvironment for the module: |
|
48 | 62 | |
|
49 | 63 | - Adds dummy home dir tree |
|
50 | 64 | """ |
|
51 | try: | |
|
52 | os.makedirs("home_test_dir/_ipython") | |
|
53 | except WindowsError: | |
|
54 | pass #Or should we complain that the test directory already exists?? | |
|
65 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
|
66 | # problem because that exception is only defined on Windows... | |
|
67 | os.makedirs(IP_TEST_DIR) | |
|
55 | 68 | |
|
56 | 69 | def teardown(): |
|
57 | 70 | """Teardown testenvironment for the module: |
|
58 | 71 | |
|
59 | 72 | - Remove dummy home dir tree |
|
60 | 73 | """ |
|
61 | try: | |
|
62 | os.removedirs("home_test_dir/_ipython") | |
|
63 | except WindowsError: | |
|
64 | pass #Or should we complain that the test directory already exists?? | |
|
74 | # Note: we remove the parent test dir, which is the root of all test | |
|
75 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
|
76 | # that non-empty directories are all recursively removed. | |
|
77 | shutil.rmtree(TMP_TEST_DIR) | |
|
65 | 78 | |
|
66 | 79 | |
|
67 | 80 | def setup_environment(): |
|
68 | 81 | """Setup testenvironment for some functions that are tested |
|
69 | 82 | in this module. In particular this functions stores attributes |
|
70 | 83 | and other things that we need to stub in some test functions. |
|
71 | 84 | This needs to be done on a function level and not module level because |
|
72 | 85 | each testfunction needs a pristine environment. |
|
73 | 86 | """ |
|
74 | 87 | global oldstuff, platformstuff |
|
75 | 88 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) |
|
76 | 89 | |
|
77 | 90 | if os.name == 'nt': |
|
78 | 91 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
79 | 92 | |
|
80 | 93 | if 'IPYTHONDIR' in env: |
|
81 | 94 | del env['IPYTHONDIR'] |
|
82 | 95 | |
|
83 | 96 | def teardown_environment(): |
|
84 | 97 | """Restore things that were remebered by the setup_environment function |
|
85 | 98 | """ |
|
86 | 99 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff |
|
87 | 100 | for key in env.keys(): |
|
88 | 101 | if key not in oldenv: |
|
89 | 102 | del env[key] |
|
90 | 103 | env.update(oldenv) |
|
91 | 104 | if hasattr(sys, 'frozen'): |
|
92 | 105 | del sys.frozen |
|
93 | 106 | if os.name == 'nt': |
|
94 | 107 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
95 | 108 | |
|
96 | 109 | # Build decorator that uses the setup_environment/setup_environment |
|
97 | 110 | with_enivronment = with_setup(setup_environment, teardown_environment) |
|
98 | 111 | |
|
99 | 112 | |
|
100 | 113 | # |
|
101 | 114 | # Tests for get_home_dir |
|
102 | 115 | # |
|
103 | 116 | |
|
104 | 117 | @skip_if_not_win32 |
|
105 | 118 | @with_enivronment |
|
106 | 119 | def test_get_home_dir_1(): |
|
107 | 120 | """Testcase for py2exe logic, un-compressed lib |
|
108 | 121 | """ |
|
109 | 122 | sys.frozen = True |
|
110 | 123 | |
|
111 | 124 | #fake filename for IPython.__init__ |
|
112 |
IPython.__file__ = abspath(join( |
|
|
125 | IPython.__file__ = abspath(join(TEST_FILE_PATH, "home_test_dir/Lib/IPython/__init__.py")) | |
|
113 | 126 | |
|
114 | 127 | home_dir = genutils.get_home_dir() |
|
115 |
nt.assert_equal(home_dir, abspath( |
|
|
128 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
|
116 | 129 | |
|
117 | 130 | @skip_if_not_win32 |
|
118 | 131 | @with_enivronment |
|
119 | 132 | def test_get_home_dir_2(): |
|
120 | 133 | """Testcase for py2exe logic, compressed lib |
|
121 | 134 | """ |
|
122 | 135 | sys.frozen = True |
|
123 | 136 | #fake filename for IPython.__init__ |
|
124 |
IPython.__file__ = abspath(join( |
|
|
137 | IPython.__file__ = abspath(join(TEST_FILE_PATH, "home_test_dir/Library.zip/IPython/__init__.py")).lower() | |
|
125 | 138 | |
|
126 | 139 | home_dir = genutils.get_home_dir() |
|
127 |
nt.assert_equal(home_dir, abspath( |
|
|
140 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) | |
|
128 | 141 | |
|
129 | 142 | @with_enivronment |
|
130 | 143 | def test_get_home_dir_3(): |
|
131 | 144 | """Testcase $HOME is set, then use its value as home directory.""" |
|
132 | env["HOME"] = join(test_file_path, "home_test_dir") | |
|
145 | env["HOME"] = HOME_TEST_DIR | |
|
133 | 146 | home_dir = genutils.get_home_dir() |
|
134 | 147 | nt.assert_equal(home_dir, env["HOME"]) |
|
135 | 148 | |
|
136 | 149 | @with_enivronment |
|
137 | 150 | def test_get_home_dir_4(): |
|
138 | 151 | """Testcase $HOME is not set, os=='poix'. |
|
139 | 152 | This should fail with HomeDirError""" |
|
140 | 153 | |
|
141 | 154 | os.name = 'posix' |
|
142 | 155 | if 'HOME' in env: del env['HOME'] |
|
143 | 156 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) |
|
144 | 157 | |
|
145 | 158 | @skip_if_not_win32 |
|
146 | 159 | @with_enivronment |
|
147 | 160 | def test_get_home_dir_5(): |
|
148 | 161 | """Testcase $HOME is not set, os=='nt' |
|
149 | 162 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
150 | 163 | |
|
151 | 164 | os.name = 'nt' |
|
152 | 165 | if 'HOME' in env: del env['HOME'] |
|
153 |
env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath( |
|
|
166 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "home_test_dir" | |
|
154 | 167 | |
|
155 | 168 | home_dir = genutils.get_home_dir() |
|
156 |
nt.assert_equal(home_dir, abspath( |
|
|
169 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
|
157 | 170 | |
|
158 | 171 | @skip_if_not_win32 |
|
159 | 172 | @with_enivronment |
|
160 | 173 | def test_get_home_dir_6(): |
|
161 | 174 | """Testcase $HOME is not set, os=='nt' |
|
162 | 175 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
163 | 176 | env['USERPROFILE'] points to path |
|
164 | 177 | """ |
|
165 | 178 | |
|
166 | 179 | os.name = 'nt' |
|
167 | 180 | if 'HOME' in env: del env['HOME'] |
|
168 |
env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath( |
|
|
169 |
env["USERPROFILE"] = abspath( |
|
|
181 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST" | |
|
182 | env["USERPROFILE"] = abspath(HOME_TEST_DIR) | |
|
170 | 183 | |
|
171 | 184 | home_dir = genutils.get_home_dir() |
|
172 |
nt.assert_equal(home_dir, abspath( |
|
|
185 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
|
173 | 186 | |
|
174 | 187 | # Should we stub wreg fully so we can run the test on all platforms? |
|
175 | 188 | @skip_if_not_win32 |
|
176 | 189 | @with_enivronment |
|
177 | 190 | def test_get_home_dir_7(): |
|
178 | 191 | """Testcase $HOME is not set, os=='nt' |
|
179 | 192 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
180 | 193 | """ |
|
181 | 194 | os.name = 'nt' |
|
182 | 195 | if 'HOME' in env: del env['HOME'] |
|
183 | 196 | if 'HOMEDRIVE' in env: del env['HOMEDRIVE'] |
|
184 | 197 | |
|
185 | 198 | #Stub windows registry functions |
|
186 | 199 | def OpenKey(x, y): |
|
187 | 200 | class key: |
|
188 | 201 | def Close(self): |
|
189 | 202 | pass |
|
190 | 203 | return key() |
|
191 | 204 | def QueryValueEx(x, y): |
|
192 | return [abspath(join(test_file_path, "home_test_dir"))] | |
|
205 | return [abspath(HOME_TEST_DIR)] | |
|
193 | 206 | |
|
194 | 207 | wreg.OpenKey = OpenKey |
|
195 | 208 | wreg.QueryValueEx = QueryValueEx |
|
196 | 209 | |
|
197 | 210 | home_dir = genutils.get_home_dir() |
|
198 |
nt.assert_equal(home_dir, abspath( |
|
|
211 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
|
199 | 212 | |
|
200 | 213 | |
|
201 | 214 | # |
|
202 | 215 | # Tests for get_ipython_dir |
|
203 | 216 | # |
|
204 | 217 | |
|
205 | 218 | @with_enivronment |
|
206 | 219 | def test_get_ipython_dir_1(): |
|
207 | 220 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
208 | 221 | env['IPYTHONDIR'] = "someplace/.ipython" |
|
209 | 222 | ipdir = genutils.get_ipython_dir() |
|
210 | 223 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) |
|
211 | 224 | |
|
212 | 225 | |
|
213 | 226 | @with_enivronment |
|
214 | 227 | def test_get_ipython_dir_2(): |
|
215 | 228 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
216 | 229 | genutils.get_home_dir = lambda : "someplace" |
|
217 | 230 | os.name = "posix" |
|
218 | 231 | ipdir = genutils.get_ipython_dir() |
|
219 | 232 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) |
|
220 | 233 | |
|
221 | 234 | @with_enivronment |
|
222 | 235 | def test_get_ipython_dir_3(): |
|
223 | 236 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
224 | 237 | genutils.get_home_dir = lambda : "someplace" |
|
225 | 238 | os.name = "nt" |
|
226 | 239 | ipdir = genutils.get_ipython_dir() |
|
227 | 240 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) |
|
228 | 241 | |
|
229 | 242 | |
|
230 | 243 | # |
|
231 | 244 | # Tests for get_security_dir |
|
232 | 245 | # |
|
233 | 246 | |
|
234 | 247 | @with_enivronment |
|
235 | 248 | def test_get_security_dir(): |
|
236 | 249 | """Testcase to see if we can call get_security_dir without Exceptions.""" |
|
237 | 250 | sdir = genutils.get_security_dir() |
|
238 | 251 | |
|
239 | 252 | |
|
240 | 253 | # |
|
241 | 254 | # Tests for popkey |
|
242 | 255 | # |
|
243 | 256 | |
|
244 | 257 | def test_popkey_1(): |
|
245 | 258 | """test_popkey_1, Basic usage test of popkey |
|
246 | 259 | """ |
|
247 | 260 | dct = dict(a=1, b=2, c=3) |
|
248 | 261 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
249 | 262 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
250 | 263 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
251 | 264 | nt.assert_equal(dct, dict(c=3)) |
|
252 | 265 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
253 | 266 | nt.assert_equal(dct, dict()) |
|
254 | 267 | |
|
255 | 268 | def test_popkey_2(): |
|
256 | 269 | """test_popkey_2, Test to see that popkey of non occuring keys |
|
257 | 270 | generates a KeyError exception |
|
258 | 271 | """ |
|
259 | 272 | dct = dict(a=1, b=2, c=3) |
|
260 | 273 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") |
|
261 | 274 | |
|
262 | 275 | def test_popkey_3(): |
|
263 | 276 | """test_popkey_3, Tests to see that popkey calls returns the correct value |
|
264 | 277 | and that the key/value was removed from the dict. |
|
265 | 278 | """ |
|
266 | 279 | dct = dict(a=1, b=2, c=3) |
|
267 | 280 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) |
|
268 | 281 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
269 | 282 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) |
|
270 | 283 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
271 | 284 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) |
|
272 | 285 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
273 | 286 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
274 | 287 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
275 | 288 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
276 | 289 | nt.assert_equal(dct, dict(c=3)) |
|
277 | 290 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
278 | 291 | nt.assert_equal(dct, dict()) |
General Comments 0
You need to be logged in to leave comments.
Login now