Show More
@@ -1,2167 +1,2171 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | General purpose utilities. |
|
3 | General purpose utilities. | |
4 |
|
4 | |||
5 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
5 | This is a grab-bag of stuff I find useful in most programs I write. Some of | |
6 | these things are also convenient when working at the command line. |
|
6 | these things are also convenient when working at the command line. | |
7 |
|
7 | |||
8 | $Id: genutils.py 2998 2008-01-31 10:06:04Z vivainio $""" |
|
8 | $Id: genutils.py 2998 2008-01-31 10:06:04Z vivainio $""" | |
9 |
|
9 | |||
10 | #***************************************************************************** |
|
10 | #***************************************************************************** | |
11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #***************************************************************************** |
|
15 | #***************************************************************************** | |
16 |
|
16 | |||
17 | from IPython import Release |
|
17 | from IPython import Release | |
18 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
18 | __author__ = '%s <%s>' % Release.authors['Fernando'] | |
19 | __license__ = Release.license |
|
19 | __license__ = Release.license | |
20 |
|
20 | |||
21 | #**************************************************************************** |
|
21 | #**************************************************************************** | |
22 | # required modules from the Python standard library |
|
22 | # required modules from the Python standard library | |
23 | import __main__ |
|
23 | import __main__ | |
24 | import commands |
|
24 | import commands | |
25 | try: |
|
25 | try: | |
26 | import doctest |
|
26 | import doctest | |
27 | except ImportError: |
|
27 | except ImportError: | |
28 | pass |
|
28 | pass | |
29 | import os |
|
29 | import os | |
30 | import platform |
|
30 | import platform | |
31 | import re |
|
31 | import re | |
32 | import shlex |
|
32 | import shlex | |
33 | import shutil |
|
33 | import shutil | |
34 | import subprocess |
|
34 | import subprocess | |
35 | import sys |
|
35 | import sys | |
36 | import tempfile |
|
36 | import tempfile | |
37 | import time |
|
37 | import time | |
38 | import types |
|
38 | import types | |
39 | import warnings |
|
39 | import warnings | |
40 |
|
40 | |||
41 | # Curses and termios are Unix-only modules |
|
41 | # Curses and termios are Unix-only modules | |
42 | try: |
|
42 | try: | |
43 | import curses |
|
43 | import curses | |
44 | # We need termios as well, so if its import happens to raise, we bail on |
|
44 | # We need termios as well, so if its import happens to raise, we bail on | |
45 | # using curses altogether. |
|
45 | # using curses altogether. | |
46 | import termios |
|
46 | import termios | |
47 | except ImportError: |
|
47 | except ImportError: | |
48 | USE_CURSES = False |
|
48 | USE_CURSES = False | |
49 | else: |
|
49 | else: | |
50 | # Curses on Solaris may not be complete, so we can't use it there |
|
50 | # Curses on Solaris may not be complete, so we can't use it there | |
51 | USE_CURSES = hasattr(curses,'initscr') |
|
51 | USE_CURSES = hasattr(curses,'initscr') | |
52 |
|
52 | |||
53 | # Other IPython utilities |
|
53 | # Other IPython utilities | |
54 | import IPython |
|
54 | import IPython | |
55 | from IPython.Itpl import Itpl,itpl,printpl |
|
55 | from IPython.Itpl import Itpl,itpl,printpl | |
56 | from IPython import DPyGetOpt, platutils |
|
56 | from IPython import DPyGetOpt, platutils | |
57 | from IPython.generics import result_display |
|
57 | from IPython.generics import result_display | |
58 | import IPython.ipapi |
|
58 | import IPython.ipapi | |
59 | from IPython.external.path import path |
|
59 | from IPython.external.path import path | |
60 | if os.name == "nt": |
|
60 | if os.name == "nt": | |
61 | from IPython.winconsole import get_console_size |
|
61 | from IPython.winconsole import get_console_size | |
62 |
|
62 | |||
63 | try: |
|
63 | try: | |
64 | set |
|
64 | set | |
65 | except: |
|
65 | except: | |
66 | from sets import Set as set |
|
66 | from sets import Set as set | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | #**************************************************************************** |
|
69 | #**************************************************************************** | |
70 | # Exceptions |
|
70 | # Exceptions | |
71 | class Error(Exception): |
|
71 | class Error(Exception): | |
72 | """Base class for exceptions in this module.""" |
|
72 | """Base class for exceptions in this module.""" | |
73 | pass |
|
73 | pass | |
74 |
|
74 | |||
75 | #---------------------------------------------------------------------------- |
|
75 | #---------------------------------------------------------------------------- | |
76 | class IOStream: |
|
76 | class IOStream: | |
77 | def __init__(self,stream,fallback): |
|
77 | def __init__(self,stream,fallback): | |
78 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
78 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): | |
79 | stream = fallback |
|
79 | stream = fallback | |
80 | self.stream = stream |
|
80 | self.stream = stream | |
81 | self._swrite = stream.write |
|
81 | self._swrite = stream.write | |
82 | self.flush = stream.flush |
|
82 | self.flush = stream.flush | |
83 |
|
83 | |||
84 | def write(self,data): |
|
84 | def write(self,data): | |
85 | try: |
|
85 | try: | |
86 | self._swrite(data) |
|
86 | self._swrite(data) | |
87 | except: |
|
87 | except: | |
88 | try: |
|
88 | try: | |
89 | # print handles some unicode issues which may trip a plain |
|
89 | # print handles some unicode issues which may trip a plain | |
90 | # write() call. Attempt to emulate write() by using a |
|
90 | # write() call. Attempt to emulate write() by using a | |
91 | # trailing comma |
|
91 | # trailing comma | |
92 | print >> self.stream, data, |
|
92 | print >> self.stream, data, | |
93 | except: |
|
93 | except: | |
94 | # if we get here, something is seriously broken. |
|
94 | # if we get here, something is seriously broken. | |
95 | print >> sys.stderr, \ |
|
95 | print >> sys.stderr, \ | |
96 | 'ERROR - failed to write data to stream:', self.stream |
|
96 | 'ERROR - failed to write data to stream:', self.stream | |
97 |
|
97 | |||
98 | def close(self): |
|
98 | def close(self): | |
99 | pass |
|
99 | pass | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | class IOTerm: |
|
102 | class IOTerm: | |
103 | """ Term holds the file or file-like objects for handling I/O operations. |
|
103 | """ Term holds the file or file-like objects for handling I/O operations. | |
104 |
|
104 | |||
105 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
105 | These are normally just sys.stdin, sys.stdout and sys.stderr but for | |
106 | Windows they can can replaced to allow editing the strings before they are |
|
106 | Windows they can can replaced to allow editing the strings before they are | |
107 | displayed.""" |
|
107 | displayed.""" | |
108 |
|
108 | |||
109 | # In the future, having IPython channel all its I/O operations through |
|
109 | # In the future, having IPython channel all its I/O operations through | |
110 | # this class will make it easier to embed it into other environments which |
|
110 | # this class will make it easier to embed it into other environments which | |
111 | # are not a normal terminal (such as a GUI-based shell) |
|
111 | # are not a normal terminal (such as a GUI-based shell) | |
112 | def __init__(self,cin=None,cout=None,cerr=None): |
|
112 | def __init__(self,cin=None,cout=None,cerr=None): | |
113 | self.cin = IOStream(cin,sys.stdin) |
|
113 | self.cin = IOStream(cin,sys.stdin) | |
114 | self.cout = IOStream(cout,sys.stdout) |
|
114 | self.cout = IOStream(cout,sys.stdout) | |
115 | self.cerr = IOStream(cerr,sys.stderr) |
|
115 | self.cerr = IOStream(cerr,sys.stderr) | |
116 |
|
116 | |||
117 | # Global variable to be used for all I/O |
|
117 | # Global variable to be used for all I/O | |
118 | Term = IOTerm() |
|
118 | Term = IOTerm() | |
119 |
|
119 | |||
120 | import IPython.rlineimpl as readline |
|
120 | import IPython.rlineimpl as readline | |
121 | # Remake Term to use the readline i/o facilities |
|
121 | # Remake Term to use the readline i/o facilities | |
122 | if sys.platform == 'win32' and readline.have_readline: |
|
122 | if sys.platform == 'win32' and readline.have_readline: | |
123 |
|
123 | |||
124 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
124 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | #**************************************************************************** |
|
127 | #**************************************************************************** | |
128 | # Generic warning/error printer, used by everything else |
|
128 | # Generic warning/error printer, used by everything else | |
129 | def warn(msg,level=2,exit_val=1): |
|
129 | def warn(msg,level=2,exit_val=1): | |
130 | """Standard warning printer. Gives formatting consistency. |
|
130 | """Standard warning printer. Gives formatting consistency. | |
131 |
|
131 | |||
132 | Output is sent to Term.cerr (sys.stderr by default). |
|
132 | Output is sent to Term.cerr (sys.stderr by default). | |
133 |
|
133 | |||
134 | Options: |
|
134 | Options: | |
135 |
|
135 | |||
136 | -level(2): allows finer control: |
|
136 | -level(2): allows finer control: | |
137 | 0 -> Do nothing, dummy function. |
|
137 | 0 -> Do nothing, dummy function. | |
138 | 1 -> Print message. |
|
138 | 1 -> Print message. | |
139 | 2 -> Print 'WARNING:' + message. (Default level). |
|
139 | 2 -> Print 'WARNING:' + message. (Default level). | |
140 | 3 -> Print 'ERROR:' + message. |
|
140 | 3 -> Print 'ERROR:' + message. | |
141 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
141 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). | |
142 |
|
142 | |||
143 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
143 | -exit_val (1): exit value returned by sys.exit() for a level 4 | |
144 | warning. Ignored for all other levels.""" |
|
144 | warning. Ignored for all other levels.""" | |
145 |
|
145 | |||
146 | if level>0: |
|
146 | if level>0: | |
147 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
147 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] | |
148 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
148 | print >> Term.cerr, '%s%s' % (header[level],msg) | |
149 | if level == 4: |
|
149 | if level == 4: | |
150 | print >> Term.cerr,'Exiting.\n' |
|
150 | print >> Term.cerr,'Exiting.\n' | |
151 | sys.exit(exit_val) |
|
151 | sys.exit(exit_val) | |
152 |
|
152 | |||
153 | def info(msg): |
|
153 | def info(msg): | |
154 | """Equivalent to warn(msg,level=1).""" |
|
154 | """Equivalent to warn(msg,level=1).""" | |
155 |
|
155 | |||
156 | warn(msg,level=1) |
|
156 | warn(msg,level=1) | |
157 |
|
157 | |||
158 | def error(msg): |
|
158 | def error(msg): | |
159 | """Equivalent to warn(msg,level=3).""" |
|
159 | """Equivalent to warn(msg,level=3).""" | |
160 |
|
160 | |||
161 | warn(msg,level=3) |
|
161 | warn(msg,level=3) | |
162 |
|
162 | |||
163 | def fatal(msg,exit_val=1): |
|
163 | def fatal(msg,exit_val=1): | |
164 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
164 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" | |
165 |
|
165 | |||
166 | warn(msg,exit_val=exit_val,level=4) |
|
166 | warn(msg,exit_val=exit_val,level=4) | |
167 |
|
167 | |||
168 | #--------------------------------------------------------------------------- |
|
168 | #--------------------------------------------------------------------------- | |
169 | # Debugging routines |
|
169 | # Debugging routines | |
170 | # |
|
170 | # | |
171 | def debugx(expr,pre_msg=''): |
|
171 | def debugx(expr,pre_msg=''): | |
172 | """Print the value of an expression from the caller's frame. |
|
172 | """Print the value of an expression from the caller's frame. | |
173 |
|
173 | |||
174 | Takes an expression, evaluates it in the caller's frame and prints both |
|
174 | Takes an expression, evaluates it in the caller's frame and prints both | |
175 | the given expression and the resulting value (as well as a debug mark |
|
175 | the given expression and the resulting value (as well as a debug mark | |
176 | indicating the name of the calling function. The input must be of a form |
|
176 | indicating the name of the calling function. The input must be of a form | |
177 | suitable for eval(). |
|
177 | suitable for eval(). | |
178 |
|
178 | |||
179 | An optional message can be passed, which will be prepended to the printed |
|
179 | An optional message can be passed, which will be prepended to the printed | |
180 | expr->value pair.""" |
|
180 | expr->value pair.""" | |
181 |
|
181 | |||
182 | cf = sys._getframe(1) |
|
182 | cf = sys._getframe(1) | |
183 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
183 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, | |
184 | eval(expr,cf.f_globals,cf.f_locals)) |
|
184 | eval(expr,cf.f_globals,cf.f_locals)) | |
185 |
|
185 | |||
186 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
186 | # deactivate it by uncommenting the following line, which makes it a no-op | |
187 | #def debugx(expr,pre_msg=''): pass |
|
187 | #def debugx(expr,pre_msg=''): pass | |
188 |
|
188 | |||
189 | #---------------------------------------------------------------------------- |
|
189 | #---------------------------------------------------------------------------- | |
190 | StringTypes = types.StringTypes |
|
190 | StringTypes = types.StringTypes | |
191 |
|
191 | |||
192 | # Basic timing functionality |
|
192 | # Basic timing functionality | |
193 |
|
193 | |||
194 | # If possible (Unix), use the resource module instead of time.clock() |
|
194 | # If possible (Unix), use the resource module instead of time.clock() | |
195 | try: |
|
195 | try: | |
196 | import resource |
|
196 | import resource | |
197 | def clocku(): |
|
197 | def clocku(): | |
198 | """clocku() -> floating point number |
|
198 | """clocku() -> floating point number | |
199 |
|
199 | |||
200 | Return the *USER* CPU time in seconds since the start of the process. |
|
200 | Return the *USER* CPU time in seconds since the start of the process. | |
201 | This is done via a call to resource.getrusage, so it avoids the |
|
201 | This is done via a call to resource.getrusage, so it avoids the | |
202 | wraparound problems in time.clock().""" |
|
202 | wraparound problems in time.clock().""" | |
203 |
|
203 | |||
204 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
204 | return resource.getrusage(resource.RUSAGE_SELF)[0] | |
205 |
|
205 | |||
206 | def clocks(): |
|
206 | def clocks(): | |
207 | """clocks() -> floating point number |
|
207 | """clocks() -> floating point number | |
208 |
|
208 | |||
209 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
209 | Return the *SYSTEM* CPU time in seconds since the start of the process. | |
210 | This is done via a call to resource.getrusage, so it avoids the |
|
210 | This is done via a call to resource.getrusage, so it avoids the | |
211 | wraparound problems in time.clock().""" |
|
211 | wraparound problems in time.clock().""" | |
212 |
|
212 | |||
213 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
213 | return resource.getrusage(resource.RUSAGE_SELF)[1] | |
214 |
|
214 | |||
215 | def clock(): |
|
215 | def clock(): | |
216 | """clock() -> floating point number |
|
216 | """clock() -> floating point number | |
217 |
|
217 | |||
218 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
218 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of | |
219 | the process. This is done via a call to resource.getrusage, so it |
|
219 | the process. This is done via a call to resource.getrusage, so it | |
220 | avoids the wraparound problems in time.clock().""" |
|
220 | avoids the wraparound problems in time.clock().""" | |
221 |
|
221 | |||
222 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
222 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] | |
223 | return u+s |
|
223 | return u+s | |
224 |
|
224 | |||
225 | def clock2(): |
|
225 | def clock2(): | |
226 | """clock2() -> (t_user,t_system) |
|
226 | """clock2() -> (t_user,t_system) | |
227 |
|
227 | |||
228 | Similar to clock(), but return a tuple of user/system times.""" |
|
228 | Similar to clock(), but return a tuple of user/system times.""" | |
229 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
229 | return resource.getrusage(resource.RUSAGE_SELF)[:2] | |
230 |
|
230 | |||
231 | except ImportError: |
|
231 | except ImportError: | |
232 | # There is no distinction of user/system time under windows, so we just use |
|
232 | # There is no distinction of user/system time under windows, so we just use | |
233 | # time.clock() for everything... |
|
233 | # time.clock() for everything... | |
234 | clocku = clocks = clock = time.clock |
|
234 | clocku = clocks = clock = time.clock | |
235 | def clock2(): |
|
235 | def clock2(): | |
236 | """Under windows, system CPU time can't be measured. |
|
236 | """Under windows, system CPU time can't be measured. | |
237 |
|
237 | |||
238 | This just returns clock() and zero.""" |
|
238 | This just returns clock() and zero.""" | |
239 | return time.clock(),0.0 |
|
239 | return time.clock(),0.0 | |
240 |
|
240 | |||
241 | def timings_out(reps,func,*args,**kw): |
|
241 | def timings_out(reps,func,*args,**kw): | |
242 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
242 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) | |
243 |
|
243 | |||
244 | Execute a function reps times, return a tuple with the elapsed total |
|
244 | Execute a function reps times, return a tuple with the elapsed total | |
245 | CPU time in seconds, the time per call and the function's output. |
|
245 | CPU time in seconds, the time per call and the function's output. | |
246 |
|
246 | |||
247 | Under Unix, the return value is the sum of user+system time consumed by |
|
247 | Under Unix, the return value is the sum of user+system time consumed by | |
248 | the process, computed via the resource module. This prevents problems |
|
248 | the process, computed via the resource module. This prevents problems | |
249 | related to the wraparound effect which the time.clock() function has. |
|
249 | related to the wraparound effect which the time.clock() function has. | |
250 |
|
250 | |||
251 | Under Windows the return value is in wall clock seconds. See the |
|
251 | Under Windows the return value is in wall clock seconds. See the | |
252 | documentation for the time module for more details.""" |
|
252 | documentation for the time module for more details.""" | |
253 |
|
253 | |||
254 | reps = int(reps) |
|
254 | reps = int(reps) | |
255 | assert reps >=1, 'reps must be >= 1' |
|
255 | assert reps >=1, 'reps must be >= 1' | |
256 | if reps==1: |
|
256 | if reps==1: | |
257 | start = clock() |
|
257 | start = clock() | |
258 | out = func(*args,**kw) |
|
258 | out = func(*args,**kw) | |
259 | tot_time = clock()-start |
|
259 | tot_time = clock()-start | |
260 | else: |
|
260 | else: | |
261 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
261 | rng = xrange(reps-1) # the last time is executed separately to store output | |
262 | start = clock() |
|
262 | start = clock() | |
263 | for dummy in rng: func(*args,**kw) |
|
263 | for dummy in rng: func(*args,**kw) | |
264 | out = func(*args,**kw) # one last time |
|
264 | out = func(*args,**kw) # one last time | |
265 | tot_time = clock()-start |
|
265 | tot_time = clock()-start | |
266 | av_time = tot_time / reps |
|
266 | av_time = tot_time / reps | |
267 | return tot_time,av_time,out |
|
267 | return tot_time,av_time,out | |
268 |
|
268 | |||
269 | def timings(reps,func,*args,**kw): |
|
269 | def timings(reps,func,*args,**kw): | |
270 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
270 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) | |
271 |
|
271 | |||
272 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
272 | Execute a function reps times, return a tuple with the elapsed total CPU | |
273 | time in seconds and the time per call. These are just the first two values |
|
273 | time in seconds and the time per call. These are just the first two values | |
274 | in timings_out().""" |
|
274 | in timings_out().""" | |
275 |
|
275 | |||
276 | return timings_out(reps,func,*args,**kw)[0:2] |
|
276 | return timings_out(reps,func,*args,**kw)[0:2] | |
277 |
|
277 | |||
278 | def timing(func,*args,**kw): |
|
278 | def timing(func,*args,**kw): | |
279 | """timing(func,*args,**kw) -> t_total |
|
279 | """timing(func,*args,**kw) -> t_total | |
280 |
|
280 | |||
281 | Execute a function once, return the elapsed total CPU time in |
|
281 | Execute a function once, return the elapsed total CPU time in | |
282 | seconds. This is just the first value in timings_out().""" |
|
282 | seconds. This is just the first value in timings_out().""" | |
283 |
|
283 | |||
284 | return timings_out(1,func,*args,**kw)[0] |
|
284 | return timings_out(1,func,*args,**kw)[0] | |
285 |
|
285 | |||
286 | #**************************************************************************** |
|
286 | #**************************************************************************** | |
287 | # file and system |
|
287 | # file and system | |
288 |
|
288 | |||
289 | def arg_split(s,posix=False): |
|
289 | def arg_split(s,posix=False): | |
290 | """Split a command line's arguments in a shell-like manner. |
|
290 | """Split a command line's arguments in a shell-like manner. | |
291 |
|
291 | |||
292 | This is a modified version of the standard library's shlex.split() |
|
292 | This is a modified version of the standard library's shlex.split() | |
293 | function, but with a default of posix=False for splitting, so that quotes |
|
293 | function, but with a default of posix=False for splitting, so that quotes | |
294 | in inputs are respected.""" |
|
294 | in inputs are respected.""" | |
295 |
|
295 | |||
296 | # XXX - there may be unicode-related problems here!!! I'm not sure that |
|
296 | # XXX - there may be unicode-related problems here!!! I'm not sure that | |
297 | # shlex is truly unicode-safe, so it might be necessary to do |
|
297 | # shlex is truly unicode-safe, so it might be necessary to do | |
298 | # |
|
298 | # | |
299 | # s = s.encode(sys.stdin.encoding) |
|
299 | # s = s.encode(sys.stdin.encoding) | |
300 | # |
|
300 | # | |
301 | # first, to ensure that shlex gets a normal string. Input from anyone who |
|
301 | # first, to ensure that shlex gets a normal string. Input from anyone who | |
302 | # knows more about unicode and shlex than I would be good to have here... |
|
302 | # knows more about unicode and shlex than I would be good to have here... | |
303 | lex = shlex.shlex(s, posix=posix) |
|
303 | lex = shlex.shlex(s, posix=posix) | |
304 | lex.whitespace_split = True |
|
304 | lex.whitespace_split = True | |
305 | return list(lex) |
|
305 | return list(lex) | |
306 |
|
306 | |||
307 | def system(cmd,verbose=0,debug=0,header=''): |
|
307 | def system(cmd,verbose=0,debug=0,header=''): | |
308 | """Execute a system command, return its exit status. |
|
308 | """Execute a system command, return its exit status. | |
309 |
|
309 | |||
310 | Options: |
|
310 | Options: | |
311 |
|
311 | |||
312 | - verbose (0): print the command to be executed. |
|
312 | - verbose (0): print the command to be executed. | |
313 |
|
313 | |||
314 | - debug (0): only print, do not actually execute. |
|
314 | - debug (0): only print, do not actually execute. | |
315 |
|
315 | |||
316 | - header (''): Header to print on screen prior to the executed command (it |
|
316 | - header (''): Header to print on screen prior to the executed command (it | |
317 | is only prepended to the command, no newlines are added). |
|
317 | is only prepended to the command, no newlines are added). | |
318 |
|
318 | |||
319 | Note: a stateful version of this function is available through the |
|
319 | Note: a stateful version of this function is available through the | |
320 | SystemExec class.""" |
|
320 | SystemExec class.""" | |
321 |
|
321 | |||
322 | stat = 0 |
|
322 | stat = 0 | |
323 | if verbose or debug: print header+cmd |
|
323 | if verbose or debug: print header+cmd | |
324 | sys.stdout.flush() |
|
324 | sys.stdout.flush() | |
325 | if not debug: stat = os.system(cmd) |
|
325 | if not debug: stat = os.system(cmd) | |
326 | return stat |
|
326 | return stat | |
327 |
|
327 | |||
328 | def abbrev_cwd(): |
|
328 | def abbrev_cwd(): | |
329 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
329 | """ Return abbreviated version of cwd, e.g. d:mydir """ | |
330 | cwd = os.getcwd().replace('\\','/') |
|
330 | cwd = os.getcwd().replace('\\','/') | |
331 | drivepart = '' |
|
331 | drivepart = '' | |
332 | tail = cwd |
|
332 | tail = cwd | |
333 | if sys.platform == 'win32': |
|
333 | if sys.platform == 'win32': | |
334 | if len(cwd) < 4: |
|
334 | if len(cwd) < 4: | |
335 | return cwd |
|
335 | return cwd | |
336 | drivepart,tail = os.path.splitdrive(cwd) |
|
336 | drivepart,tail = os.path.splitdrive(cwd) | |
337 |
|
337 | |||
338 |
|
338 | |||
339 | parts = tail.split('/') |
|
339 | parts = tail.split('/') | |
340 | if len(parts) > 2: |
|
340 | if len(parts) > 2: | |
341 | tail = '/'.join(parts[-2:]) |
|
341 | tail = '/'.join(parts[-2:]) | |
342 |
|
342 | |||
343 | return (drivepart + ( |
|
343 | return (drivepart + ( | |
344 | cwd == '/' and '/' or tail)) |
|
344 | cwd == '/' and '/' or tail)) | |
345 |
|
345 | |||
346 |
|
346 | |||
347 | # This function is used by ipython in a lot of places to make system calls. |
|
347 | # This function is used by ipython in a lot of places to make system calls. | |
348 | # We need it to be slightly different under win32, due to the vagaries of |
|
348 | # We need it to be slightly different under win32, due to the vagaries of | |
349 | # 'network shares'. A win32 override is below. |
|
349 | # 'network shares'. A win32 override is below. | |
350 |
|
350 | |||
351 | def shell(cmd,verbose=0,debug=0,header=''): |
|
351 | def shell(cmd,verbose=0,debug=0,header=''): | |
352 | """Execute a command in the system shell, always return None. |
|
352 | """Execute a command in the system shell, always return None. | |
353 |
|
353 | |||
354 | Options: |
|
354 | Options: | |
355 |
|
355 | |||
356 | - verbose (0): print the command to be executed. |
|
356 | - verbose (0): print the command to be executed. | |
357 |
|
357 | |||
358 | - debug (0): only print, do not actually execute. |
|
358 | - debug (0): only print, do not actually execute. | |
359 |
|
359 | |||
360 | - header (''): Header to print on screen prior to the executed command (it |
|
360 | - header (''): Header to print on screen prior to the executed command (it | |
361 | is only prepended to the command, no newlines are added). |
|
361 | is only prepended to the command, no newlines are added). | |
362 |
|
362 | |||
363 | Note: this is similar to genutils.system(), but it returns None so it can |
|
363 | Note: this is similar to genutils.system(), but it returns None so it can | |
364 | be conveniently used in interactive loops without getting the return value |
|
364 | be conveniently used in interactive loops without getting the return value | |
365 | (typically 0) printed many times.""" |
|
365 | (typically 0) printed many times.""" | |
366 |
|
366 | |||
367 | stat = 0 |
|
367 | stat = 0 | |
368 | if verbose or debug: print header+cmd |
|
368 | if verbose or debug: print header+cmd | |
369 | # flush stdout so we don't mangle python's buffering |
|
369 | # flush stdout so we don't mangle python's buffering | |
370 | sys.stdout.flush() |
|
370 | sys.stdout.flush() | |
371 |
|
371 | |||
372 | if not debug: |
|
372 | if not debug: | |
373 | platutils.set_term_title("IPy " + cmd) |
|
373 | platutils.set_term_title("IPy " + cmd) | |
374 | os.system(cmd) |
|
374 | os.system(cmd) | |
375 | platutils.set_term_title("IPy " + abbrev_cwd()) |
|
375 | platutils.set_term_title("IPy " + abbrev_cwd()) | |
376 |
|
376 | |||
377 | # override shell() for win32 to deal with network shares |
|
377 | # override shell() for win32 to deal with network shares | |
378 | if os.name in ('nt','dos'): |
|
378 | if os.name in ('nt','dos'): | |
379 |
|
379 | |||
380 | shell_ori = shell |
|
380 | shell_ori = shell | |
381 |
|
381 | |||
382 | def shell(cmd,verbose=0,debug=0,header=''): |
|
382 | def shell(cmd,verbose=0,debug=0,header=''): | |
383 | if os.getcwd().startswith(r"\\"): |
|
383 | if os.getcwd().startswith(r"\\"): | |
384 | path = os.getcwd() |
|
384 | path = os.getcwd() | |
385 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
385 | # change to c drive (cannot be on UNC-share when issuing os.system, | |
386 | # as cmd.exe cannot handle UNC addresses) |
|
386 | # as cmd.exe cannot handle UNC addresses) | |
387 | os.chdir("c:") |
|
387 | os.chdir("c:") | |
388 | # issue pushd to the UNC-share and then run the command |
|
388 | # issue pushd to the UNC-share and then run the command | |
389 | try: |
|
389 | try: | |
390 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
390 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) | |
391 | finally: |
|
391 | finally: | |
392 | os.chdir(path) |
|
392 | os.chdir(path) | |
393 | else: |
|
393 | else: | |
394 | shell_ori(cmd,verbose,debug,header) |
|
394 | shell_ori(cmd,verbose,debug,header) | |
395 |
|
395 | |||
396 | shell.__doc__ = shell_ori.__doc__ |
|
396 | shell.__doc__ = shell_ori.__doc__ | |
397 |
|
397 | |||
398 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
398 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): | |
399 | """Dummy substitute for perl's backquotes. |
|
399 | """Dummy substitute for perl's backquotes. | |
400 |
|
400 | |||
401 | Executes a command and returns the output. |
|
401 | Executes a command and returns the output. | |
402 |
|
402 | |||
403 | Accepts the same arguments as system(), plus: |
|
403 | Accepts the same arguments as system(), plus: | |
404 |
|
404 | |||
405 | - split(0): if true, the output is returned as a list split on newlines. |
|
405 | - split(0): if true, the output is returned as a list split on newlines. | |
406 |
|
406 | |||
407 | Note: a stateful version of this function is available through the |
|
407 | Note: a stateful version of this function is available through the | |
408 | SystemExec class. |
|
408 | SystemExec class. | |
409 |
|
409 | |||
410 | This is pretty much deprecated and rarely used, |
|
410 | This is pretty much deprecated and rarely used, | |
411 | genutils.getoutputerror may be what you need. |
|
411 | genutils.getoutputerror may be what you need. | |
412 |
|
412 | |||
413 | """ |
|
413 | """ | |
414 |
|
414 | |||
415 | if verbose or debug: print header+cmd |
|
415 | if verbose or debug: print header+cmd | |
416 | if not debug: |
|
416 | if not debug: | |
417 | output = os.popen(cmd).read() |
|
417 | output = os.popen(cmd).read() | |
418 | # stipping last \n is here for backwards compat. |
|
418 | # stipping last \n is here for backwards compat. | |
419 | if output.endswith('\n'): |
|
419 | if output.endswith('\n'): | |
420 | output = output[:-1] |
|
420 | output = output[:-1] | |
421 | if split: |
|
421 | if split: | |
422 | return output.split('\n') |
|
422 | return output.split('\n') | |
423 | else: |
|
423 | else: | |
424 | return output |
|
424 | return output | |
425 |
|
425 | |||
426 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
426 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): | |
427 | """Return (standard output,standard error) of executing cmd in a shell. |
|
427 | """Return (standard output,standard error) of executing cmd in a shell. | |
428 |
|
428 | |||
429 | Accepts the same arguments as system(), plus: |
|
429 | Accepts the same arguments as system(), plus: | |
430 |
|
430 | |||
431 | - split(0): if true, each of stdout/err is returned as a list split on |
|
431 | - split(0): if true, each of stdout/err is returned as a list split on | |
432 | newlines. |
|
432 | newlines. | |
433 |
|
433 | |||
434 | Note: a stateful version of this function is available through the |
|
434 | Note: a stateful version of this function is available through the | |
435 | SystemExec class.""" |
|
435 | SystemExec class.""" | |
436 |
|
436 | |||
437 | if verbose or debug: print header+cmd |
|
437 | if verbose or debug: print header+cmd | |
438 | if not cmd: |
|
438 | if not cmd: | |
439 | if split: |
|
439 | if split: | |
440 | return [],[] |
|
440 | return [],[] | |
441 | else: |
|
441 | else: | |
442 | return '','' |
|
442 | return '','' | |
443 | if not debug: |
|
443 | if not debug: | |
444 | pin,pout,perr = os.popen3(cmd) |
|
444 | pin,pout,perr = os.popen3(cmd) | |
445 | tout = pout.read().rstrip() |
|
445 | tout = pout.read().rstrip() | |
446 | terr = perr.read().rstrip() |
|
446 | terr = perr.read().rstrip() | |
447 | pin.close() |
|
447 | pin.close() | |
448 | pout.close() |
|
448 | pout.close() | |
449 | perr.close() |
|
449 | perr.close() | |
450 | if split: |
|
450 | if split: | |
451 | return tout.split('\n'),terr.split('\n') |
|
451 | return tout.split('\n'),terr.split('\n') | |
452 | else: |
|
452 | else: | |
453 | return tout,terr |
|
453 | return tout,terr | |
454 |
|
454 | |||
455 | # for compatibility with older naming conventions |
|
455 | # for compatibility with older naming conventions | |
456 | xsys = system |
|
456 | xsys = system | |
457 | bq = getoutput |
|
457 | bq = getoutput | |
458 |
|
458 | |||
459 | class SystemExec: |
|
459 | class SystemExec: | |
460 | """Access the system and getoutput functions through a stateful interface. |
|
460 | """Access the system and getoutput functions through a stateful interface. | |
461 |
|
461 | |||
462 | Note: here we refer to the system and getoutput functions from this |
|
462 | Note: here we refer to the system and getoutput functions from this | |
463 | library, not the ones from the standard python library. |
|
463 | library, not the ones from the standard python library. | |
464 |
|
464 | |||
465 | This class offers the system and getoutput functions as methods, but the |
|
465 | This class offers the system and getoutput functions as methods, but the | |
466 | verbose, debug and header parameters can be set for the instance (at |
|
466 | verbose, debug and header parameters can be set for the instance (at | |
467 | creation time or later) so that they don't need to be specified on each |
|
467 | creation time or later) so that they don't need to be specified on each | |
468 | call. |
|
468 | call. | |
469 |
|
469 | |||
470 | For efficiency reasons, there's no way to override the parameters on a |
|
470 | For efficiency reasons, there's no way to override the parameters on a | |
471 | per-call basis other than by setting instance attributes. If you need |
|
471 | per-call basis other than by setting instance attributes. If you need | |
472 | local overrides, it's best to directly call system() or getoutput(). |
|
472 | local overrides, it's best to directly call system() or getoutput(). | |
473 |
|
473 | |||
474 | The following names are provided as alternate options: |
|
474 | The following names are provided as alternate options: | |
475 | - xsys: alias to system |
|
475 | - xsys: alias to system | |
476 | - bq: alias to getoutput |
|
476 | - bq: alias to getoutput | |
477 |
|
477 | |||
478 | An instance can then be created as: |
|
478 | An instance can then be created as: | |
479 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
479 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') | |
480 | """ |
|
480 | """ | |
481 |
|
481 | |||
482 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
482 | def __init__(self,verbose=0,debug=0,header='',split=0): | |
483 | """Specify the instance's values for verbose, debug and header.""" |
|
483 | """Specify the instance's values for verbose, debug and header.""" | |
484 | setattr_list(self,'verbose debug header split') |
|
484 | setattr_list(self,'verbose debug header split') | |
485 |
|
485 | |||
486 | def system(self,cmd): |
|
486 | def system(self,cmd): | |
487 | """Stateful interface to system(), with the same keyword parameters.""" |
|
487 | """Stateful interface to system(), with the same keyword parameters.""" | |
488 |
|
488 | |||
489 | system(cmd,self.verbose,self.debug,self.header) |
|
489 | system(cmd,self.verbose,self.debug,self.header) | |
490 |
|
490 | |||
491 | def shell(self,cmd): |
|
491 | def shell(self,cmd): | |
492 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
492 | """Stateful interface to shell(), with the same keyword parameters.""" | |
493 |
|
493 | |||
494 | shell(cmd,self.verbose,self.debug,self.header) |
|
494 | shell(cmd,self.verbose,self.debug,self.header) | |
495 |
|
495 | |||
496 | xsys = system # alias |
|
496 | xsys = system # alias | |
497 |
|
497 | |||
498 | def getoutput(self,cmd): |
|
498 | def getoutput(self,cmd): | |
499 | """Stateful interface to getoutput().""" |
|
499 | """Stateful interface to getoutput().""" | |
500 |
|
500 | |||
501 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
501 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) | |
502 |
|
502 | |||
503 | def getoutputerror(self,cmd): |
|
503 | def getoutputerror(self,cmd): | |
504 | """Stateful interface to getoutputerror().""" |
|
504 | """Stateful interface to getoutputerror().""" | |
505 |
|
505 | |||
506 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
506 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) | |
507 |
|
507 | |||
508 | bq = getoutput # alias |
|
508 | bq = getoutput # alias | |
509 |
|
509 | |||
510 | #----------------------------------------------------------------------------- |
|
510 | #----------------------------------------------------------------------------- | |
511 | def mutex_opts(dict,ex_op): |
|
511 | def mutex_opts(dict,ex_op): | |
512 | """Check for presence of mutually exclusive keys in a dict. |
|
512 | """Check for presence of mutually exclusive keys in a dict. | |
513 |
|
513 | |||
514 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
514 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" | |
515 | for op1,op2 in ex_op: |
|
515 | for op1,op2 in ex_op: | |
516 | if op1 in dict and op2 in dict: |
|
516 | if op1 in dict and op2 in dict: | |
517 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
517 | raise ValueError,'\n*** ERROR in Arguments *** '\ | |
518 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
518 | 'Options '+op1+' and '+op2+' are mutually exclusive.' | |
519 |
|
519 | |||
520 | #----------------------------------------------------------------------------- |
|
520 | #----------------------------------------------------------------------------- | |
521 | def get_py_filename(name): |
|
521 | def get_py_filename(name): | |
522 | """Return a valid python filename in the current directory. |
|
522 | """Return a valid python filename in the current directory. | |
523 |
|
523 | |||
524 | If the given name is not a file, it adds '.py' and searches again. |
|
524 | If the given name is not a file, it adds '.py' and searches again. | |
525 | Raises IOError with an informative message if the file isn't found.""" |
|
525 | Raises IOError with an informative message if the file isn't found.""" | |
526 |
|
526 | |||
527 | name = os.path.expanduser(name) |
|
527 | name = os.path.expanduser(name) | |
528 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
528 | if not os.path.isfile(name) and not name.endswith('.py'): | |
529 | name += '.py' |
|
529 | name += '.py' | |
530 | if os.path.isfile(name): |
|
530 | if os.path.isfile(name): | |
531 | return name |
|
531 | return name | |
532 | else: |
|
532 | else: | |
533 | raise IOError,'File `%s` not found.' % name |
|
533 | raise IOError,'File `%s` not found.' % name | |
534 |
|
534 | |||
535 | #----------------------------------------------------------------------------- |
|
535 | #----------------------------------------------------------------------------- | |
536 | def filefind(fname,alt_dirs = None): |
|
536 | def filefind(fname,alt_dirs = None): | |
537 | """Return the given filename either in the current directory, if it |
|
537 | """Return the given filename either in the current directory, if it | |
538 | exists, or in a specified list of directories. |
|
538 | exists, or in a specified list of directories. | |
539 |
|
539 | |||
540 | ~ expansion is done on all file and directory names. |
|
540 | ~ expansion is done on all file and directory names. | |
541 |
|
541 | |||
542 | Upon an unsuccessful search, raise an IOError exception.""" |
|
542 | Upon an unsuccessful search, raise an IOError exception.""" | |
543 |
|
543 | |||
544 | if alt_dirs is None: |
|
544 | if alt_dirs is None: | |
545 | try: |
|
545 | try: | |
546 | alt_dirs = get_home_dir() |
|
546 | alt_dirs = get_home_dir() | |
547 | except HomeDirError: |
|
547 | except HomeDirError: | |
548 | alt_dirs = os.getcwd() |
|
548 | alt_dirs = os.getcwd() | |
549 | search = [fname] + list_strings(alt_dirs) |
|
549 | search = [fname] + list_strings(alt_dirs) | |
550 | search = map(os.path.expanduser,search) |
|
550 | search = map(os.path.expanduser,search) | |
551 | #print 'search list for',fname,'list:',search # dbg |
|
551 | #print 'search list for',fname,'list:',search # dbg | |
552 | fname = search[0] |
|
552 | fname = search[0] | |
553 | if os.path.isfile(fname): |
|
553 | if os.path.isfile(fname): | |
554 | return fname |
|
554 | return fname | |
555 | for direc in search[1:]: |
|
555 | for direc in search[1:]: | |
556 | testname = os.path.join(direc,fname) |
|
556 | testname = os.path.join(direc,fname) | |
557 | #print 'testname',testname # dbg |
|
557 | #print 'testname',testname # dbg | |
558 | if os.path.isfile(testname): |
|
558 | if os.path.isfile(testname): | |
559 | return testname |
|
559 | return testname | |
560 | raise IOError,'File' + `fname` + \ |
|
560 | raise IOError,'File' + `fname` + \ | |
561 | ' not found in current or supplied directories:' + `alt_dirs` |
|
561 | ' not found in current or supplied directories:' + `alt_dirs` | |
562 |
|
562 | |||
563 | #---------------------------------------------------------------------------- |
|
563 | #---------------------------------------------------------------------------- | |
564 | def file_read(filename): |
|
564 | def file_read(filename): | |
565 | """Read a file and close it. Returns the file source.""" |
|
565 | """Read a file and close it. Returns the file source.""" | |
566 | fobj = open(filename,'r'); |
|
566 | fobj = open(filename,'r'); | |
567 | source = fobj.read(); |
|
567 | source = fobj.read(); | |
568 | fobj.close() |
|
568 | fobj.close() | |
569 | return source |
|
569 | return source | |
570 |
|
570 | |||
571 | def file_readlines(filename): |
|
571 | def file_readlines(filename): | |
572 | """Read a file and close it. Returns the file source using readlines().""" |
|
572 | """Read a file and close it. Returns the file source using readlines().""" | |
573 | fobj = open(filename,'r'); |
|
573 | fobj = open(filename,'r'); | |
574 | lines = fobj.readlines(); |
|
574 | lines = fobj.readlines(); | |
575 | fobj.close() |
|
575 | fobj.close() | |
576 | return lines |
|
576 | return lines | |
577 |
|
577 | |||
578 | #---------------------------------------------------------------------------- |
|
578 | #---------------------------------------------------------------------------- | |
579 | def target_outdated(target,deps): |
|
579 | def target_outdated(target,deps): | |
580 | """Determine whether a target is out of date. |
|
580 | """Determine whether a target is out of date. | |
581 |
|
581 | |||
582 | target_outdated(target,deps) -> 1/0 |
|
582 | target_outdated(target,deps) -> 1/0 | |
583 |
|
583 | |||
584 | deps: list of filenames which MUST exist. |
|
584 | deps: list of filenames which MUST exist. | |
585 | target: single filename which may or may not exist. |
|
585 | target: single filename which may or may not exist. | |
586 |
|
586 | |||
587 | If target doesn't exist or is older than any file listed in deps, return |
|
587 | If target doesn't exist or is older than any file listed in deps, return | |
588 | true, otherwise return false. |
|
588 | true, otherwise return false. | |
589 | """ |
|
589 | """ | |
590 | try: |
|
590 | try: | |
591 | target_time = os.path.getmtime(target) |
|
591 | target_time = os.path.getmtime(target) | |
592 | except os.error: |
|
592 | except os.error: | |
593 | return 1 |
|
593 | return 1 | |
594 | for dep in deps: |
|
594 | for dep in deps: | |
595 | dep_time = os.path.getmtime(dep) |
|
595 | dep_time = os.path.getmtime(dep) | |
596 | if dep_time > target_time: |
|
596 | if dep_time > target_time: | |
597 | #print "For target",target,"Dep failed:",dep # dbg |
|
597 | #print "For target",target,"Dep failed:",dep # dbg | |
598 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
598 | #print "times (dep,tar):",dep_time,target_time # dbg | |
599 | return 1 |
|
599 | return 1 | |
600 | return 0 |
|
600 | return 0 | |
601 |
|
601 | |||
602 | #----------------------------------------------------------------------------- |
|
602 | #----------------------------------------------------------------------------- | |
603 | def target_update(target,deps,cmd): |
|
603 | def target_update(target,deps,cmd): | |
604 | """Update a target with a given command given a list of dependencies. |
|
604 | """Update a target with a given command given a list of dependencies. | |
605 |
|
605 | |||
606 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
606 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
607 |
|
607 | |||
608 | This is just a wrapper around target_outdated() which calls the given |
|
608 | This is just a wrapper around target_outdated() which calls the given | |
609 | command if target is outdated.""" |
|
609 | command if target is outdated.""" | |
610 |
|
610 | |||
611 | if target_outdated(target,deps): |
|
611 | if target_outdated(target,deps): | |
612 | xsys(cmd) |
|
612 | xsys(cmd) | |
613 |
|
613 | |||
614 | #---------------------------------------------------------------------------- |
|
614 | #---------------------------------------------------------------------------- | |
615 | def unquote_ends(istr): |
|
615 | def unquote_ends(istr): | |
616 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
616 | """Remove a single pair of quotes from the endpoints of a string.""" | |
617 |
|
617 | |||
618 | if not istr: |
|
618 | if not istr: | |
619 | return istr |
|
619 | return istr | |
620 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
620 | if (istr[0]=="'" and istr[-1]=="'") or \ | |
621 | (istr[0]=='"' and istr[-1]=='"'): |
|
621 | (istr[0]=='"' and istr[-1]=='"'): | |
622 | return istr[1:-1] |
|
622 | return istr[1:-1] | |
623 | else: |
|
623 | else: | |
624 | return istr |
|
624 | return istr | |
625 |
|
625 | |||
626 | #---------------------------------------------------------------------------- |
|
626 | #---------------------------------------------------------------------------- | |
627 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
627 | def process_cmdline(argv,names=[],defaults={},usage=''): | |
628 | """ Process command-line options and arguments. |
|
628 | """ Process command-line options and arguments. | |
629 |
|
629 | |||
630 | Arguments: |
|
630 | Arguments: | |
631 |
|
631 | |||
632 | - argv: list of arguments, typically sys.argv. |
|
632 | - argv: list of arguments, typically sys.argv. | |
633 |
|
633 | |||
634 | - names: list of option names. See DPyGetOpt docs for details on options |
|
634 | - names: list of option names. See DPyGetOpt docs for details on options | |
635 | syntax. |
|
635 | syntax. | |
636 |
|
636 | |||
637 | - defaults: dict of default values. |
|
637 | - defaults: dict of default values. | |
638 |
|
638 | |||
639 | - usage: optional usage notice to print if a wrong argument is passed. |
|
639 | - usage: optional usage notice to print if a wrong argument is passed. | |
640 |
|
640 | |||
641 | Return a dict of options and a list of free arguments.""" |
|
641 | Return a dict of options and a list of free arguments.""" | |
642 |
|
642 | |||
643 | getopt = DPyGetOpt.DPyGetOpt() |
|
643 | getopt = DPyGetOpt.DPyGetOpt() | |
644 | getopt.setIgnoreCase(0) |
|
644 | getopt.setIgnoreCase(0) | |
645 | getopt.parseConfiguration(names) |
|
645 | getopt.parseConfiguration(names) | |
646 |
|
646 | |||
647 | try: |
|
647 | try: | |
648 | getopt.processArguments(argv) |
|
648 | getopt.processArguments(argv) | |
649 | except DPyGetOpt.ArgumentError, exc: |
|
649 | except DPyGetOpt.ArgumentError, exc: | |
650 | print usage |
|
650 | print usage | |
651 | warn('"%s"' % exc,level=4) |
|
651 | warn('"%s"' % exc,level=4) | |
652 |
|
652 | |||
653 | defaults.update(getopt.optionValues) |
|
653 | defaults.update(getopt.optionValues) | |
654 | args = getopt.freeValues |
|
654 | args = getopt.freeValues | |
655 |
|
655 | |||
656 | return defaults,args |
|
656 | return defaults,args | |
657 |
|
657 | |||
658 | #---------------------------------------------------------------------------- |
|
658 | #---------------------------------------------------------------------------- | |
659 | def optstr2types(ostr): |
|
659 | def optstr2types(ostr): | |
660 | """Convert a string of option names to a dict of type mappings. |
|
660 | """Convert a string of option names to a dict of type mappings. | |
661 |
|
661 | |||
662 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
662 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} | |
663 |
|
663 | |||
664 | This is used to get the types of all the options in a string formatted |
|
664 | This is used to get the types of all the options in a string formatted | |
665 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
665 | with the conventions of DPyGetOpt. The 'type' None is used for options | |
666 | which are strings (they need no further conversion). This function's main |
|
666 | which are strings (they need no further conversion). This function's main | |
667 | use is to get a typemap for use with read_dict(). |
|
667 | use is to get a typemap for use with read_dict(). | |
668 | """ |
|
668 | """ | |
669 |
|
669 | |||
670 | typeconv = {None:'',int:'',float:''} |
|
670 | typeconv = {None:'',int:'',float:''} | |
671 | typemap = {'s':None,'i':int,'f':float} |
|
671 | typemap = {'s':None,'i':int,'f':float} | |
672 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
672 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') | |
673 |
|
673 | |||
674 | for w in ostr.split(): |
|
674 | for w in ostr.split(): | |
675 | oname,alias,otype = opt_re.match(w).groups() |
|
675 | oname,alias,otype = opt_re.match(w).groups() | |
676 | if otype == '' or alias == '!': # simple switches are integers too |
|
676 | if otype == '' or alias == '!': # simple switches are integers too | |
677 | otype = 'i' |
|
677 | otype = 'i' | |
678 | typeconv[typemap[otype]] += oname + ' ' |
|
678 | typeconv[typemap[otype]] += oname + ' ' | |
679 | return typeconv |
|
679 | return typeconv | |
680 |
|
680 | |||
681 | #---------------------------------------------------------------------------- |
|
681 | #---------------------------------------------------------------------------- | |
682 | def read_dict(filename,type_conv=None,**opt): |
|
682 | def read_dict(filename,type_conv=None,**opt): | |
683 | r"""Read a dictionary of key=value pairs from an input file, optionally |
|
683 | r"""Read a dictionary of key=value pairs from an input file, optionally | |
684 | performing conversions on the resulting values. |
|
684 | performing conversions on the resulting values. | |
685 |
|
685 | |||
686 | read_dict(filename,type_conv,**opt) -> dict |
|
686 | read_dict(filename,type_conv,**opt) -> dict | |
687 |
|
687 | |||
688 | Only one value per line is accepted, the format should be |
|
688 | Only one value per line is accepted, the format should be | |
689 | # optional comments are ignored |
|
689 | # optional comments are ignored | |
690 | key value\n |
|
690 | key value\n | |
691 |
|
691 | |||
692 | Args: |
|
692 | Args: | |
693 |
|
693 | |||
694 | - type_conv: A dictionary specifying which keys need to be converted to |
|
694 | - type_conv: A dictionary specifying which keys need to be converted to | |
695 | which types. By default all keys are read as strings. This dictionary |
|
695 | which types. By default all keys are read as strings. This dictionary | |
696 | should have as its keys valid conversion functions for strings |
|
696 | should have as its keys valid conversion functions for strings | |
697 | (int,long,float,complex, or your own). The value for each key |
|
697 | (int,long,float,complex, or your own). The value for each key | |
698 | (converter) should be a whitespace separated string containing the names |
|
698 | (converter) should be a whitespace separated string containing the names | |
699 | of all the entries in the file to be converted using that function. For |
|
699 | of all the entries in the file to be converted using that function. For | |
700 | keys to be left alone, use None as the conversion function (only needed |
|
700 | keys to be left alone, use None as the conversion function (only needed | |
701 | with purge=1, see below). |
|
701 | with purge=1, see below). | |
702 |
|
702 | |||
703 | - opt: dictionary with extra options as below (default in parens) |
|
703 | - opt: dictionary with extra options as below (default in parens) | |
704 |
|
704 | |||
705 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
705 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out | |
706 | of the dictionary to be returned. If purge is going to be used, the |
|
706 | of the dictionary to be returned. If purge is going to be used, the | |
707 | set of keys to be left as strings also has to be explicitly specified |
|
707 | set of keys to be left as strings also has to be explicitly specified | |
708 | using the (non-existent) conversion function None. |
|
708 | using the (non-existent) conversion function None. | |
709 |
|
709 | |||
710 | fs(None): field separator. This is the key/value separator to be used |
|
710 | fs(None): field separator. This is the key/value separator to be used | |
711 | when parsing the file. The None default means any whitespace [behavior |
|
711 | when parsing the file. The None default means any whitespace [behavior | |
712 | of string.split()]. |
|
712 | of string.split()]. | |
713 |
|
713 | |||
714 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
714 | strip(0): if 1, strip string values of leading/trailinig whitespace. | |
715 |
|
715 | |||
716 | warn(1): warning level if requested keys are not found in file. |
|
716 | warn(1): warning level if requested keys are not found in file. | |
717 | - 0: silently ignore. |
|
717 | - 0: silently ignore. | |
718 | - 1: inform but proceed. |
|
718 | - 1: inform but proceed. | |
719 | - 2: raise KeyError exception. |
|
719 | - 2: raise KeyError exception. | |
720 |
|
720 | |||
721 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
721 | no_empty(0): if 1, remove keys with whitespace strings as a value. | |
722 |
|
722 | |||
723 | unique([]): list of keys (or space separated string) which can't be |
|
723 | unique([]): list of keys (or space separated string) which can't be | |
724 | repeated. If one such key is found in the file, each new instance |
|
724 | repeated. If one such key is found in the file, each new instance | |
725 | overwrites the previous one. For keys not listed here, the behavior is |
|
725 | overwrites the previous one. For keys not listed here, the behavior is | |
726 | to make a list of all appearances. |
|
726 | to make a list of all appearances. | |
727 |
|
727 | |||
728 | Example: |
|
728 | Example: | |
729 |
|
729 | |||
730 | If the input file test.ini contains (we put it in a string to keep the test |
|
730 | If the input file test.ini contains (we put it in a string to keep the test | |
731 | self-contained): |
|
731 | self-contained): | |
732 |
|
732 | |||
733 | >>> test_ini = '''\ |
|
733 | >>> test_ini = '''\ | |
734 | ... i 3 |
|
734 | ... i 3 | |
735 | ... x 4.5 |
|
735 | ... x 4.5 | |
736 | ... y 5.5 |
|
736 | ... y 5.5 | |
737 | ... s hi ho''' |
|
737 | ... s hi ho''' | |
738 |
|
738 | |||
739 | Then we can use it as follows: |
|
739 | Then we can use it as follows: | |
740 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
740 | >>> type_conv={int:'i',float:'x',None:'s'} | |
741 |
|
741 | |||
742 | >>> d = read_dict(test_ini) |
|
742 | >>> d = read_dict(test_ini) | |
743 |
|
743 | |||
744 | >>> sorted(d.items()) |
|
744 | >>> sorted(d.items()) | |
745 | [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')] |
|
745 | [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')] | |
746 |
|
746 | |||
747 | >>> d = read_dict(test_ini,type_conv) |
|
747 | >>> d = read_dict(test_ini,type_conv) | |
748 |
|
748 | |||
749 | >>> sorted(d.items()) |
|
749 | >>> sorted(d.items()) | |
750 | [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')] |
|
750 | [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')] | |
751 |
|
751 | |||
752 | >>> d = read_dict(test_ini,type_conv,purge=True) |
|
752 | >>> d = read_dict(test_ini,type_conv,purge=True) | |
753 |
|
753 | |||
754 | >>> sorted(d.items()) |
|
754 | >>> sorted(d.items()) | |
755 | [('i', 3), ('s', 'hi ho'), ('x', 4.5)] |
|
755 | [('i', 3), ('s', 'hi ho'), ('x', 4.5)] | |
756 | """ |
|
756 | """ | |
757 |
|
757 | |||
758 | # starting config |
|
758 | # starting config | |
759 | opt.setdefault('purge',0) |
|
759 | opt.setdefault('purge',0) | |
760 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
760 | opt.setdefault('fs',None) # field sep defaults to any whitespace | |
761 | opt.setdefault('strip',0) |
|
761 | opt.setdefault('strip',0) | |
762 | opt.setdefault('warn',1) |
|
762 | opt.setdefault('warn',1) | |
763 | opt.setdefault('no_empty',0) |
|
763 | opt.setdefault('no_empty',0) | |
764 | opt.setdefault('unique','') |
|
764 | opt.setdefault('unique','') | |
765 | if type(opt['unique']) in StringTypes: |
|
765 | if type(opt['unique']) in StringTypes: | |
766 | unique_keys = qw(opt['unique']) |
|
766 | unique_keys = qw(opt['unique']) | |
767 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
767 | elif type(opt['unique']) in (types.TupleType,types.ListType): | |
768 | unique_keys = opt['unique'] |
|
768 | unique_keys = opt['unique'] | |
769 | else: |
|
769 | else: | |
770 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
770 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' | |
771 |
|
771 | |||
772 | dict = {} |
|
772 | dict = {} | |
773 |
|
773 | |||
774 | # first read in table of values as strings |
|
774 | # first read in table of values as strings | |
775 | if '\n' in filename: |
|
775 | if '\n' in filename: | |
776 | lines = filename.splitlines() |
|
776 | lines = filename.splitlines() | |
777 | file = None |
|
777 | file = None | |
778 | else: |
|
778 | else: | |
779 | file = open(filename,'r') |
|
779 | file = open(filename,'r') | |
780 | lines = file.readlines() |
|
780 | lines = file.readlines() | |
781 | for line in lines: |
|
781 | for line in lines: | |
782 | line = line.strip() |
|
782 | line = line.strip() | |
783 | if len(line) and line[0]=='#': continue |
|
783 | if len(line) and line[0]=='#': continue | |
784 | if len(line)>0: |
|
784 | if len(line)>0: | |
785 | lsplit = line.split(opt['fs'],1) |
|
785 | lsplit = line.split(opt['fs'],1) | |
786 | try: |
|
786 | try: | |
787 | key,val = lsplit |
|
787 | key,val = lsplit | |
788 | except ValueError: |
|
788 | except ValueError: | |
789 | key,val = lsplit[0],'' |
|
789 | key,val = lsplit[0],'' | |
790 | key = key.strip() |
|
790 | key = key.strip() | |
791 | if opt['strip']: val = val.strip() |
|
791 | if opt['strip']: val = val.strip() | |
792 | if val == "''" or val == '""': val = '' |
|
792 | if val == "''" or val == '""': val = '' | |
793 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
793 | if opt['no_empty'] and (val=='' or val.isspace()): | |
794 | continue |
|
794 | continue | |
795 | # if a key is found more than once in the file, build a list |
|
795 | # if a key is found more than once in the file, build a list | |
796 | # unless it's in the 'unique' list. In that case, last found in file |
|
796 | # unless it's in the 'unique' list. In that case, last found in file | |
797 | # takes precedence. User beware. |
|
797 | # takes precedence. User beware. | |
798 | try: |
|
798 | try: | |
799 | if dict[key] and key in unique_keys: |
|
799 | if dict[key] and key in unique_keys: | |
800 | dict[key] = val |
|
800 | dict[key] = val | |
801 | elif type(dict[key]) is types.ListType: |
|
801 | elif type(dict[key]) is types.ListType: | |
802 | dict[key].append(val) |
|
802 | dict[key].append(val) | |
803 | else: |
|
803 | else: | |
804 | dict[key] = [dict[key],val] |
|
804 | dict[key] = [dict[key],val] | |
805 | except KeyError: |
|
805 | except KeyError: | |
806 | dict[key] = val |
|
806 | dict[key] = val | |
807 | # purge if requested |
|
807 | # purge if requested | |
808 | if opt['purge']: |
|
808 | if opt['purge']: | |
809 | accepted_keys = qwflat(type_conv.values()) |
|
809 | accepted_keys = qwflat(type_conv.values()) | |
810 | for key in dict.keys(): |
|
810 | for key in dict.keys(): | |
811 | if key in accepted_keys: continue |
|
811 | if key in accepted_keys: continue | |
812 | del(dict[key]) |
|
812 | del(dict[key]) | |
813 | # now convert if requested |
|
813 | # now convert if requested | |
814 | if type_conv==None: return dict |
|
814 | if type_conv==None: return dict | |
815 | conversions = type_conv.keys() |
|
815 | conversions = type_conv.keys() | |
816 | try: conversions.remove(None) |
|
816 | try: conversions.remove(None) | |
817 | except: pass |
|
817 | except: pass | |
818 | for convert in conversions: |
|
818 | for convert in conversions: | |
819 | for val in qw(type_conv[convert]): |
|
819 | for val in qw(type_conv[convert]): | |
820 | try: |
|
820 | try: | |
821 | dict[val] = convert(dict[val]) |
|
821 | dict[val] = convert(dict[val]) | |
822 | except KeyError,e: |
|
822 | except KeyError,e: | |
823 | if opt['warn'] == 0: |
|
823 | if opt['warn'] == 0: | |
824 | pass |
|
824 | pass | |
825 | elif opt['warn'] == 1: |
|
825 | elif opt['warn'] == 1: | |
826 | print >>sys.stderr, 'Warning: key',val,\ |
|
826 | print >>sys.stderr, 'Warning: key',val,\ | |
827 | 'not found in file',filename |
|
827 | 'not found in file',filename | |
828 | elif opt['warn'] == 2: |
|
828 | elif opt['warn'] == 2: | |
829 | raise KeyError,e |
|
829 | raise KeyError,e | |
830 | else: |
|
830 | else: | |
831 | raise ValueError,'Warning level must be 0,1 or 2' |
|
831 | raise ValueError,'Warning level must be 0,1 or 2' | |
832 |
|
832 | |||
833 | return dict |
|
833 | return dict | |
834 |
|
834 | |||
835 | #---------------------------------------------------------------------------- |
|
835 | #---------------------------------------------------------------------------- | |
836 | def flag_calls(func): |
|
836 | def flag_calls(func): | |
837 | """Wrap a function to detect and flag when it gets called. |
|
837 | """Wrap a function to detect and flag when it gets called. | |
838 |
|
838 | |||
839 | This is a decorator which takes a function and wraps it in a function with |
|
839 | This is a decorator which takes a function and wraps it in a function with | |
840 | a 'called' attribute. wrapper.called is initialized to False. |
|
840 | a 'called' attribute. wrapper.called is initialized to False. | |
841 |
|
841 | |||
842 | The wrapper.called attribute is set to False right before each call to the |
|
842 | The wrapper.called attribute is set to False right before each call to the | |
843 | wrapped function, so if the call fails it remains False. After the call |
|
843 | wrapped function, so if the call fails it remains False. After the call | |
844 | completes, wrapper.called is set to True and the output is returned. |
|
844 | completes, wrapper.called is set to True and the output is returned. | |
845 |
|
845 | |||
846 | Testing for truth in wrapper.called allows you to determine if a call to |
|
846 | Testing for truth in wrapper.called allows you to determine if a call to | |
847 | func() was attempted and succeeded.""" |
|
847 | func() was attempted and succeeded.""" | |
848 |
|
848 | |||
849 | def wrapper(*args,**kw): |
|
849 | def wrapper(*args,**kw): | |
850 | wrapper.called = False |
|
850 | wrapper.called = False | |
851 | out = func(*args,**kw) |
|
851 | out = func(*args,**kw) | |
852 | wrapper.called = True |
|
852 | wrapper.called = True | |
853 | return out |
|
853 | return out | |
854 |
|
854 | |||
855 | wrapper.called = False |
|
855 | wrapper.called = False | |
856 | wrapper.__doc__ = func.__doc__ |
|
856 | wrapper.__doc__ = func.__doc__ | |
857 | return wrapper |
|
857 | return wrapper | |
858 |
|
858 | |||
859 | #---------------------------------------------------------------------------- |
|
859 | #---------------------------------------------------------------------------- | |
860 | def dhook_wrap(func,*a,**k): |
|
860 | def dhook_wrap(func,*a,**k): | |
861 | """Wrap a function call in a sys.displayhook controller. |
|
861 | """Wrap a function call in a sys.displayhook controller. | |
862 |
|
862 | |||
863 | Returns a wrapper around func which calls func, with all its arguments and |
|
863 | Returns a wrapper around func which calls func, with all its arguments and | |
864 | keywords unmodified, using the default sys.displayhook. Since IPython |
|
864 | keywords unmodified, using the default sys.displayhook. Since IPython | |
865 | modifies sys.displayhook, it breaks the behavior of certain systems that |
|
865 | modifies sys.displayhook, it breaks the behavior of certain systems that | |
866 | rely on the default behavior, notably doctest. |
|
866 | rely on the default behavior, notably doctest. | |
867 | """ |
|
867 | """ | |
868 |
|
868 | |||
869 | def f(*a,**k): |
|
869 | def f(*a,**k): | |
870 |
|
870 | |||
871 | dhook_s = sys.displayhook |
|
871 | dhook_s = sys.displayhook | |
872 | sys.displayhook = sys.__displayhook__ |
|
872 | sys.displayhook = sys.__displayhook__ | |
873 | try: |
|
873 | try: | |
874 | out = func(*a,**k) |
|
874 | out = func(*a,**k) | |
875 | finally: |
|
875 | finally: | |
876 | sys.displayhook = dhook_s |
|
876 | sys.displayhook = dhook_s | |
877 |
|
877 | |||
878 | return out |
|
878 | return out | |
879 |
|
879 | |||
880 | f.__doc__ = func.__doc__ |
|
880 | f.__doc__ = func.__doc__ | |
881 | return f |
|
881 | return f | |
882 |
|
882 | |||
883 | #---------------------------------------------------------------------------- |
|
883 | #---------------------------------------------------------------------------- | |
884 | def doctest_reload(): |
|
884 | def doctest_reload(): | |
885 | """Properly reload doctest to reuse it interactively. |
|
885 | """Properly reload doctest to reuse it interactively. | |
886 |
|
886 | |||
887 | This routine: |
|
887 | This routine: | |
888 |
|
888 | |||
889 | - reloads doctest |
|
889 | - reloads doctest | |
890 |
|
890 | |||
891 | - resets its global 'master' attribute to None, so that multiple uses of |
|
891 | - resets its global 'master' attribute to None, so that multiple uses of | |
892 | the module interactively don't produce cumulative reports. |
|
892 | the module interactively don't produce cumulative reports. | |
893 |
|
893 | |||
894 | - Monkeypatches its core test runner method to protect it from IPython's |
|
894 | - Monkeypatches its core test runner method to protect it from IPython's | |
895 | modified displayhook. Doctest expects the default displayhook behavior |
|
895 | modified displayhook. Doctest expects the default displayhook behavior | |
896 | deep down, so our modification breaks it completely. For this reason, a |
|
896 | deep down, so our modification breaks it completely. For this reason, a | |
897 | hard monkeypatch seems like a reasonable solution rather than asking |
|
897 | hard monkeypatch seems like a reasonable solution rather than asking | |
898 | users to manually use a different doctest runner when under IPython.""" |
|
898 | users to manually use a different doctest runner when under IPython.""" | |
899 |
|
899 | |||
900 | import doctest |
|
900 | import doctest | |
901 | reload(doctest) |
|
901 | reload(doctest) | |
902 | doctest.master=None |
|
902 | doctest.master=None | |
903 |
|
903 | |||
904 | try: |
|
904 | try: | |
905 | doctest.DocTestRunner |
|
905 | doctest.DocTestRunner | |
906 | except AttributeError: |
|
906 | except AttributeError: | |
907 | # This is only for python 2.3 compatibility, remove once we move to |
|
907 | # This is only for python 2.3 compatibility, remove once we move to | |
908 | # 2.4 only. |
|
908 | # 2.4 only. | |
909 | pass |
|
909 | pass | |
910 | else: |
|
910 | else: | |
911 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) |
|
911 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) | |
912 |
|
912 | |||
913 | #---------------------------------------------------------------------------- |
|
913 | #---------------------------------------------------------------------------- | |
914 | class HomeDirError(Error): |
|
914 | class HomeDirError(Error): | |
915 | pass |
|
915 | pass | |
916 |
|
916 | |||
917 | def get_home_dir(): |
|
917 | def get_home_dir(): | |
918 | """Return the closest possible equivalent to a 'home' directory. |
|
918 | """Return the closest possible equivalent to a 'home' directory. | |
919 |
|
919 | |||
920 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
920 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. | |
921 |
|
921 | |||
922 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
922 | Currently only Posix and NT are implemented, a HomeDirError exception is | |
923 | raised for all other OSes. """ |
|
923 | raised for all other OSes. """ | |
924 |
|
924 | |||
925 | isdir = os.path.isdir |
|
925 | isdir = os.path.isdir | |
926 | env = os.environ |
|
926 | env = os.environ | |
927 |
|
927 | |||
928 | # first, check py2exe distribution root directory for _ipython. |
|
928 | # first, check py2exe distribution root directory for _ipython. | |
929 | # This overrides all. Normally does not exist. |
|
929 | # This overrides all. Normally does not exist. | |
930 |
|
930 | |||
931 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
931 | if hasattr(sys, "frozen"): #Is frozen by py2exe | |
932 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
932 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file | |
933 | root, rest = IPython.__file__.lower().split('library.zip') |
|
933 | root, rest = IPython.__file__.lower().split('library.zip') | |
934 | else: |
|
934 | else: | |
935 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
935 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") | |
936 | root=os.path.abspath(root).rstrip('\\') |
|
936 | root=os.path.abspath(root).rstrip('\\') | |
937 | if isdir(os.path.join(root, '_ipython')): |
|
937 | if isdir(os.path.join(root, '_ipython')): | |
938 | os.environ["IPYKITROOT"] = root |
|
938 | os.environ["IPYKITROOT"] = root | |
939 |
|
|
939 | return root | |
940 | try: |
|
940 | try: | |
941 | homedir = env['HOME'] |
|
941 | homedir = env['HOME'] | |
942 | if not isdir(homedir): |
|
942 | if not isdir(homedir): | |
943 | # in case a user stuck some string which does NOT resolve to a |
|
943 | # in case a user stuck some string which does NOT resolve to a | |
944 | # valid path, it's as good as if we hadn't foud it |
|
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 | return homedir |
|
950 | return homedir | |
947 | except KeyError: |
|
951 | except KeyError: | |
948 | if os.name == 'posix': |
|
952 | if os.name == 'posix': | |
949 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
953 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' | |
950 | elif os.name == 'nt': |
|
954 | elif os.name == 'nt': | |
951 | # For some strange reason, win9x returns 'nt' for os.name. |
|
955 | # For some strange reason, win9x returns 'nt' for os.name. | |
952 | try: |
|
956 | try: | |
953 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
957 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) | |
954 | if not isdir(homedir): |
|
958 | if not isdir(homedir): | |
955 | homedir = os.path.join(env['USERPROFILE']) |
|
959 | homedir = os.path.join(env['USERPROFILE']) | |
956 | if not isdir(homedir): |
|
960 | if not isdir(homedir): | |
957 | raise HomeDirError |
|
961 | raise HomeDirError | |
958 | return homedir |
|
962 | return homedir | |
959 | except KeyError: |
|
963 | except KeyError: | |
960 | try: |
|
964 | try: | |
961 | # Use the registry to get the 'My Documents' folder. |
|
965 | # Use the registry to get the 'My Documents' folder. | |
962 | import _winreg as wreg |
|
966 | import _winreg as wreg | |
963 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
967 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, | |
964 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
968 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") | |
965 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
969 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
966 | key.Close() |
|
970 | key.Close() | |
967 | if not isdir(homedir): |
|
971 | if not isdir(homedir): | |
968 | e = ('Invalid "Personal" folder registry key ' |
|
972 | e = ('Invalid "Personal" folder registry key ' | |
969 | 'typically "My Documents".\n' |
|
973 | 'typically "My Documents".\n' | |
970 | 'Value: %s\n' |
|
974 | 'Value: %s\n' | |
971 | 'This is not a valid directory on your system.' % |
|
975 | 'This is not a valid directory on your system.' % | |
972 | homedir) |
|
976 | homedir) | |
973 | raise HomeDirError(e) |
|
977 | raise HomeDirError(e) | |
974 | return homedir |
|
978 | return homedir | |
975 | except HomeDirError: |
|
979 | except HomeDirError: | |
976 | raise |
|
980 | raise | |
977 | except: |
|
981 | except: | |
978 | return 'C:\\' |
|
982 | return 'C:\\' | |
979 | elif os.name == 'dos': |
|
983 | elif os.name == 'dos': | |
980 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
984 | # Desperate, may do absurd things in classic MacOS. May work under DOS. | |
981 | return 'C:\\' |
|
985 | return 'C:\\' | |
982 | else: |
|
986 | else: | |
983 | raise HomeDirError,'support for your operating system not implemented.' |
|
987 | raise HomeDirError,'support for your operating system not implemented.' | |
984 |
|
988 | |||
985 |
|
989 | |||
986 | def get_ipython_dir(): |
|
990 | def get_ipython_dir(): | |
987 | """Get the IPython directory for this platform and user. |
|
991 | """Get the IPython directory for this platform and user. | |
988 |
|
992 | |||
989 | This uses the logic in `get_home_dir` to find the home directory |
|
993 | This uses the logic in `get_home_dir` to find the home directory | |
990 | and the adds either .ipython or _ipython to the end of the path. |
|
994 | and the adds either .ipython or _ipython to the end of the path. | |
991 | """ |
|
995 | """ | |
992 | if os.name == 'posix': |
|
996 | if os.name == 'posix': | |
993 | ipdir_def = '.ipython' |
|
997 | ipdir_def = '.ipython' | |
994 | else: |
|
998 | else: | |
995 | ipdir_def = '_ipython' |
|
999 | ipdir_def = '_ipython' | |
996 | home_dir = get_home_dir() |
|
1000 | home_dir = get_home_dir() | |
997 | ipdir = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
1001 | ipdir = os.path.abspath(os.environ.get('IPYTHONDIR', | |
998 | os.path.join(home_dir, ipdir_def))) |
|
1002 | os.path.join(home_dir, ipdir_def))) | |
999 | return ipdir.decode(sys.getfilesystemencoding()) |
|
1003 | return ipdir.decode(sys.getfilesystemencoding()) | |
1000 |
|
1004 | |||
1001 | def get_security_dir(): |
|
1005 | def get_security_dir(): | |
1002 | """Get the IPython security directory. |
|
1006 | """Get the IPython security directory. | |
1003 |
|
1007 | |||
1004 | This directory is the default location for all security related files, |
|
1008 | This directory is the default location for all security related files, | |
1005 | including SSL/TLS certificates and FURL files. |
|
1009 | including SSL/TLS certificates and FURL files. | |
1006 |
|
1010 | |||
1007 | If the directory does not exist, it is created with 0700 permissions. |
|
1011 | If the directory does not exist, it is created with 0700 permissions. | |
1008 | If it exists, permissions are set to 0700. |
|
1012 | If it exists, permissions are set to 0700. | |
1009 | """ |
|
1013 | """ | |
1010 | security_dir = os.path.join(get_ipython_dir(), 'security') |
|
1014 | security_dir = os.path.join(get_ipython_dir(), 'security') | |
1011 | if not os.path.isdir(security_dir): |
|
1015 | if not os.path.isdir(security_dir): | |
1012 | os.mkdir(security_dir, 0700) |
|
1016 | os.mkdir(security_dir, 0700) | |
1013 | else: |
|
1017 | else: | |
1014 | os.chmod(security_dir, 0700) |
|
1018 | os.chmod(security_dir, 0700) | |
1015 | return security_dir |
|
1019 | return security_dir | |
1016 |
|
1020 | |||
1017 | #**************************************************************************** |
|
1021 | #**************************************************************************** | |
1018 | # strings and text |
|
1022 | # strings and text | |
1019 |
|
1023 | |||
1020 | class LSString(str): |
|
1024 | class LSString(str): | |
1021 | """String derivative with a special access attributes. |
|
1025 | """String derivative with a special access attributes. | |
1022 |
|
1026 | |||
1023 | These are normal strings, but with the special attributes: |
|
1027 | These are normal strings, but with the special attributes: | |
1024 |
|
1028 | |||
1025 | .l (or .list) : value as list (split on newlines). |
|
1029 | .l (or .list) : value as list (split on newlines). | |
1026 | .n (or .nlstr): original value (the string itself). |
|
1030 | .n (or .nlstr): original value (the string itself). | |
1027 | .s (or .spstr): value as whitespace-separated string. |
|
1031 | .s (or .spstr): value as whitespace-separated string. | |
1028 | .p (or .paths): list of path objects |
|
1032 | .p (or .paths): list of path objects | |
1029 |
|
1033 | |||
1030 | Any values which require transformations are computed only once and |
|
1034 | Any values which require transformations are computed only once and | |
1031 | cached. |
|
1035 | cached. | |
1032 |
|
1036 | |||
1033 | Such strings are very useful to efficiently interact with the shell, which |
|
1037 | Such strings are very useful to efficiently interact with the shell, which | |
1034 | typically only understands whitespace-separated options for commands.""" |
|
1038 | typically only understands whitespace-separated options for commands.""" | |
1035 |
|
1039 | |||
1036 | def get_list(self): |
|
1040 | def get_list(self): | |
1037 | try: |
|
1041 | try: | |
1038 | return self.__list |
|
1042 | return self.__list | |
1039 | except AttributeError: |
|
1043 | except AttributeError: | |
1040 | self.__list = self.split('\n') |
|
1044 | self.__list = self.split('\n') | |
1041 | return self.__list |
|
1045 | return self.__list | |
1042 |
|
1046 | |||
1043 | l = list = property(get_list) |
|
1047 | l = list = property(get_list) | |
1044 |
|
1048 | |||
1045 | def get_spstr(self): |
|
1049 | def get_spstr(self): | |
1046 | try: |
|
1050 | try: | |
1047 | return self.__spstr |
|
1051 | return self.__spstr | |
1048 | except AttributeError: |
|
1052 | except AttributeError: | |
1049 | self.__spstr = self.replace('\n',' ') |
|
1053 | self.__spstr = self.replace('\n',' ') | |
1050 | return self.__spstr |
|
1054 | return self.__spstr | |
1051 |
|
1055 | |||
1052 | s = spstr = property(get_spstr) |
|
1056 | s = spstr = property(get_spstr) | |
1053 |
|
1057 | |||
1054 | def get_nlstr(self): |
|
1058 | def get_nlstr(self): | |
1055 | return self |
|
1059 | return self | |
1056 |
|
1060 | |||
1057 | n = nlstr = property(get_nlstr) |
|
1061 | n = nlstr = property(get_nlstr) | |
1058 |
|
1062 | |||
1059 | def get_paths(self): |
|
1063 | def get_paths(self): | |
1060 | try: |
|
1064 | try: | |
1061 | return self.__paths |
|
1065 | return self.__paths | |
1062 | except AttributeError: |
|
1066 | except AttributeError: | |
1063 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
1067 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] | |
1064 | return self.__paths |
|
1068 | return self.__paths | |
1065 |
|
1069 | |||
1066 | p = paths = property(get_paths) |
|
1070 | p = paths = property(get_paths) | |
1067 |
|
1071 | |||
1068 | def print_lsstring(arg): |
|
1072 | def print_lsstring(arg): | |
1069 | """ Prettier (non-repr-like) and more informative printer for LSString """ |
|
1073 | """ Prettier (non-repr-like) and more informative printer for LSString """ | |
1070 | print "LSString (.p, .n, .l, .s available). Value:" |
|
1074 | print "LSString (.p, .n, .l, .s available). Value:" | |
1071 | print arg |
|
1075 | print arg | |
1072 |
|
1076 | |||
1073 | print_lsstring = result_display.when_type(LSString)(print_lsstring) |
|
1077 | print_lsstring = result_display.when_type(LSString)(print_lsstring) | |
1074 |
|
1078 | |||
1075 | #---------------------------------------------------------------------------- |
|
1079 | #---------------------------------------------------------------------------- | |
1076 | class SList(list): |
|
1080 | class SList(list): | |
1077 | """List derivative with a special access attributes. |
|
1081 | """List derivative with a special access attributes. | |
1078 |
|
1082 | |||
1079 | These are normal lists, but with the special attributes: |
|
1083 | These are normal lists, but with the special attributes: | |
1080 |
|
1084 | |||
1081 | .l (or .list) : value as list (the list itself). |
|
1085 | .l (or .list) : value as list (the list itself). | |
1082 | .n (or .nlstr): value as a string, joined on newlines. |
|
1086 | .n (or .nlstr): value as a string, joined on newlines. | |
1083 | .s (or .spstr): value as a string, joined on spaces. |
|
1087 | .s (or .spstr): value as a string, joined on spaces. | |
1084 | .p (or .paths): list of path objects |
|
1088 | .p (or .paths): list of path objects | |
1085 |
|
1089 | |||
1086 | Any values which require transformations are computed only once and |
|
1090 | Any values which require transformations are computed only once and | |
1087 | cached.""" |
|
1091 | cached.""" | |
1088 |
|
1092 | |||
1089 | def get_list(self): |
|
1093 | def get_list(self): | |
1090 | return self |
|
1094 | return self | |
1091 |
|
1095 | |||
1092 | l = list = property(get_list) |
|
1096 | l = list = property(get_list) | |
1093 |
|
1097 | |||
1094 | def get_spstr(self): |
|
1098 | def get_spstr(self): | |
1095 | try: |
|
1099 | try: | |
1096 | return self.__spstr |
|
1100 | return self.__spstr | |
1097 | except AttributeError: |
|
1101 | except AttributeError: | |
1098 | self.__spstr = ' '.join(self) |
|
1102 | self.__spstr = ' '.join(self) | |
1099 | return self.__spstr |
|
1103 | return self.__spstr | |
1100 |
|
1104 | |||
1101 | s = spstr = property(get_spstr) |
|
1105 | s = spstr = property(get_spstr) | |
1102 |
|
1106 | |||
1103 | def get_nlstr(self): |
|
1107 | def get_nlstr(self): | |
1104 | try: |
|
1108 | try: | |
1105 | return self.__nlstr |
|
1109 | return self.__nlstr | |
1106 | except AttributeError: |
|
1110 | except AttributeError: | |
1107 | self.__nlstr = '\n'.join(self) |
|
1111 | self.__nlstr = '\n'.join(self) | |
1108 | return self.__nlstr |
|
1112 | return self.__nlstr | |
1109 |
|
1113 | |||
1110 | n = nlstr = property(get_nlstr) |
|
1114 | n = nlstr = property(get_nlstr) | |
1111 |
|
1115 | |||
1112 | def get_paths(self): |
|
1116 | def get_paths(self): | |
1113 | try: |
|
1117 | try: | |
1114 | return self.__paths |
|
1118 | return self.__paths | |
1115 | except AttributeError: |
|
1119 | except AttributeError: | |
1116 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
1120 | self.__paths = [path(p) for p in self if os.path.exists(p)] | |
1117 | return self.__paths |
|
1121 | return self.__paths | |
1118 |
|
1122 | |||
1119 | p = paths = property(get_paths) |
|
1123 | p = paths = property(get_paths) | |
1120 |
|
1124 | |||
1121 | def grep(self, pattern, prune = False, field = None): |
|
1125 | def grep(self, pattern, prune = False, field = None): | |
1122 | """ Return all strings matching 'pattern' (a regex or callable) |
|
1126 | """ Return all strings matching 'pattern' (a regex or callable) | |
1123 |
|
1127 | |||
1124 | This is case-insensitive. If prune is true, return all items |
|
1128 | This is case-insensitive. If prune is true, return all items | |
1125 | NOT matching the pattern. |
|
1129 | NOT matching the pattern. | |
1126 |
|
1130 | |||
1127 | If field is specified, the match must occur in the specified |
|
1131 | If field is specified, the match must occur in the specified | |
1128 | whitespace-separated field. |
|
1132 | whitespace-separated field. | |
1129 |
|
1133 | |||
1130 | Examples:: |
|
1134 | Examples:: | |
1131 |
|
1135 | |||
1132 | a.grep( lambda x: x.startswith('C') ) |
|
1136 | a.grep( lambda x: x.startswith('C') ) | |
1133 | a.grep('Cha.*log', prune=1) |
|
1137 | a.grep('Cha.*log', prune=1) | |
1134 | a.grep('chm', field=-1) |
|
1138 | a.grep('chm', field=-1) | |
1135 | """ |
|
1139 | """ | |
1136 |
|
1140 | |||
1137 | def match_target(s): |
|
1141 | def match_target(s): | |
1138 | if field is None: |
|
1142 | if field is None: | |
1139 | return s |
|
1143 | return s | |
1140 | parts = s.split() |
|
1144 | parts = s.split() | |
1141 | try: |
|
1145 | try: | |
1142 | tgt = parts[field] |
|
1146 | tgt = parts[field] | |
1143 | return tgt |
|
1147 | return tgt | |
1144 | except IndexError: |
|
1148 | except IndexError: | |
1145 | return "" |
|
1149 | return "" | |
1146 |
|
1150 | |||
1147 | if isinstance(pattern, basestring): |
|
1151 | if isinstance(pattern, basestring): | |
1148 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
|
1152 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) | |
1149 | else: |
|
1153 | else: | |
1150 | pred = pattern |
|
1154 | pred = pattern | |
1151 | if not prune: |
|
1155 | if not prune: | |
1152 | return SList([el for el in self if pred(match_target(el))]) |
|
1156 | return SList([el for el in self if pred(match_target(el))]) | |
1153 | else: |
|
1157 | else: | |
1154 | return SList([el for el in self if not pred(match_target(el))]) |
|
1158 | return SList([el for el in self if not pred(match_target(el))]) | |
1155 | def fields(self, *fields): |
|
1159 | def fields(self, *fields): | |
1156 | """ Collect whitespace-separated fields from string list |
|
1160 | """ Collect whitespace-separated fields from string list | |
1157 |
|
1161 | |||
1158 | Allows quick awk-like usage of string lists. |
|
1162 | Allows quick awk-like usage of string lists. | |
1159 |
|
1163 | |||
1160 | Example data (in var a, created by 'a = !ls -l'):: |
|
1164 | Example data (in var a, created by 'a = !ls -l'):: | |
1161 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
|
1165 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog | |
1162 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
|
1166 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython | |
1163 |
|
1167 | |||
1164 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] |
|
1168 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] | |
1165 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] |
|
1169 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] | |
1166 | (note the joining by space). |
|
1170 | (note the joining by space). | |
1167 | a.fields(-1) is ['ChangeLog', 'IPython'] |
|
1171 | a.fields(-1) is ['ChangeLog', 'IPython'] | |
1168 |
|
1172 | |||
1169 | IndexErrors are ignored. |
|
1173 | IndexErrors are ignored. | |
1170 |
|
1174 | |||
1171 | Without args, fields() just split()'s the strings. |
|
1175 | Without args, fields() just split()'s the strings. | |
1172 | """ |
|
1176 | """ | |
1173 | if len(fields) == 0: |
|
1177 | if len(fields) == 0: | |
1174 | return [el.split() for el in self] |
|
1178 | return [el.split() for el in self] | |
1175 |
|
1179 | |||
1176 | res = SList() |
|
1180 | res = SList() | |
1177 | for el in [f.split() for f in self]: |
|
1181 | for el in [f.split() for f in self]: | |
1178 | lineparts = [] |
|
1182 | lineparts = [] | |
1179 |
|
1183 | |||
1180 | for fd in fields: |
|
1184 | for fd in fields: | |
1181 | try: |
|
1185 | try: | |
1182 | lineparts.append(el[fd]) |
|
1186 | lineparts.append(el[fd]) | |
1183 | except IndexError: |
|
1187 | except IndexError: | |
1184 | pass |
|
1188 | pass | |
1185 | if lineparts: |
|
1189 | if lineparts: | |
1186 | res.append(" ".join(lineparts)) |
|
1190 | res.append(" ".join(lineparts)) | |
1187 |
|
1191 | |||
1188 | return res |
|
1192 | return res | |
1189 | def sort(self,field= None, nums = False): |
|
1193 | def sort(self,field= None, nums = False): | |
1190 | """ sort by specified fields (see fields()) |
|
1194 | """ sort by specified fields (see fields()) | |
1191 |
|
1195 | |||
1192 | Example:: |
|
1196 | Example:: | |
1193 | a.sort(1, nums = True) |
|
1197 | a.sort(1, nums = True) | |
1194 |
|
1198 | |||
1195 | Sorts a by second field, in numerical order (so that 21 > 3) |
|
1199 | Sorts a by second field, in numerical order (so that 21 > 3) | |
1196 |
|
1200 | |||
1197 | """ |
|
1201 | """ | |
1198 |
|
1202 | |||
1199 | #decorate, sort, undecorate |
|
1203 | #decorate, sort, undecorate | |
1200 | if field is not None: |
|
1204 | if field is not None: | |
1201 | dsu = [[SList([line]).fields(field), line] for line in self] |
|
1205 | dsu = [[SList([line]).fields(field), line] for line in self] | |
1202 | else: |
|
1206 | else: | |
1203 | dsu = [[line, line] for line in self] |
|
1207 | dsu = [[line, line] for line in self] | |
1204 | if nums: |
|
1208 | if nums: | |
1205 | for i in range(len(dsu)): |
|
1209 | for i in range(len(dsu)): | |
1206 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
|
1210 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) | |
1207 | try: |
|
1211 | try: | |
1208 | n = int(numstr) |
|
1212 | n = int(numstr) | |
1209 | except ValueError: |
|
1213 | except ValueError: | |
1210 | n = 0; |
|
1214 | n = 0; | |
1211 | dsu[i][0] = n |
|
1215 | dsu[i][0] = n | |
1212 |
|
1216 | |||
1213 |
|
1217 | |||
1214 | dsu.sort() |
|
1218 | dsu.sort() | |
1215 | return SList([t[1] for t in dsu]) |
|
1219 | return SList([t[1] for t in dsu]) | |
1216 |
|
1220 | |||
1217 | def print_slist(arg): |
|
1221 | def print_slist(arg): | |
1218 | """ Prettier (non-repr-like) and more informative printer for SList """ |
|
1222 | """ Prettier (non-repr-like) and more informative printer for SList """ | |
1219 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" |
|
1223 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" | |
1220 | if hasattr(arg, 'hideonce') and arg.hideonce: |
|
1224 | if hasattr(arg, 'hideonce') and arg.hideonce: | |
1221 | arg.hideonce = False |
|
1225 | arg.hideonce = False | |
1222 | return |
|
1226 | return | |
1223 |
|
1227 | |||
1224 | nlprint(arg) |
|
1228 | nlprint(arg) | |
1225 |
|
1229 | |||
1226 | print_slist = result_display.when_type(SList)(print_slist) |
|
1230 | print_slist = result_display.when_type(SList)(print_slist) | |
1227 |
|
1231 | |||
1228 |
|
1232 | |||
1229 |
|
1233 | |||
1230 | #---------------------------------------------------------------------------- |
|
1234 | #---------------------------------------------------------------------------- | |
1231 | def esc_quotes(strng): |
|
1235 | def esc_quotes(strng): | |
1232 | """Return the input string with single and double quotes escaped out""" |
|
1236 | """Return the input string with single and double quotes escaped out""" | |
1233 |
|
1237 | |||
1234 | return strng.replace('"','\\"').replace("'","\\'") |
|
1238 | return strng.replace('"','\\"').replace("'","\\'") | |
1235 |
|
1239 | |||
1236 | #---------------------------------------------------------------------------- |
|
1240 | #---------------------------------------------------------------------------- | |
1237 | def make_quoted_expr(s): |
|
1241 | def make_quoted_expr(s): | |
1238 | """Return string s in appropriate quotes, using raw string if possible. |
|
1242 | """Return string s in appropriate quotes, using raw string if possible. | |
1239 |
|
1243 | |||
1240 | Effectively this turns string: cd \ao\ao\ |
|
1244 | Effectively this turns string: cd \ao\ao\ | |
1241 | to: r"cd \ao\ao\_"[:-1] |
|
1245 | to: r"cd \ao\ao\_"[:-1] | |
1242 |
|
1246 | |||
1243 | Note the use of raw string and padding at the end to allow trailing backslash. |
|
1247 | Note the use of raw string and padding at the end to allow trailing backslash. | |
1244 |
|
1248 | |||
1245 | """ |
|
1249 | """ | |
1246 |
|
1250 | |||
1247 | tail = '' |
|
1251 | tail = '' | |
1248 | tailpadding = '' |
|
1252 | tailpadding = '' | |
1249 | raw = '' |
|
1253 | raw = '' | |
1250 | if "\\" in s: |
|
1254 | if "\\" in s: | |
1251 | raw = 'r' |
|
1255 | raw = 'r' | |
1252 | if s.endswith('\\'): |
|
1256 | if s.endswith('\\'): | |
1253 | tail = '[:-1]' |
|
1257 | tail = '[:-1]' | |
1254 | tailpadding = '_' |
|
1258 | tailpadding = '_' | |
1255 | if '"' not in s: |
|
1259 | if '"' not in s: | |
1256 | quote = '"' |
|
1260 | quote = '"' | |
1257 | elif "'" not in s: |
|
1261 | elif "'" not in s: | |
1258 | quote = "'" |
|
1262 | quote = "'" | |
1259 | elif '"""' not in s and not s.endswith('"'): |
|
1263 | elif '"""' not in s and not s.endswith('"'): | |
1260 | quote = '"""' |
|
1264 | quote = '"""' | |
1261 | elif "'''" not in s and not s.endswith("'"): |
|
1265 | elif "'''" not in s and not s.endswith("'"): | |
1262 | quote = "'''" |
|
1266 | quote = "'''" | |
1263 | else: |
|
1267 | else: | |
1264 | # give up, backslash-escaped string will do |
|
1268 | # give up, backslash-escaped string will do | |
1265 | return '"%s"' % esc_quotes(s) |
|
1269 | return '"%s"' % esc_quotes(s) | |
1266 | res = raw + quote + s + tailpadding + quote + tail |
|
1270 | res = raw + quote + s + tailpadding + quote + tail | |
1267 | return res |
|
1271 | return res | |
1268 |
|
1272 | |||
1269 |
|
1273 | |||
1270 | #---------------------------------------------------------------------------- |
|
1274 | #---------------------------------------------------------------------------- | |
1271 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
1275 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): | |
1272 | """Take multiple lines of input. |
|
1276 | """Take multiple lines of input. | |
1273 |
|
1277 | |||
1274 | A list with each line of input as a separate element is returned when a |
|
1278 | A list with each line of input as a separate element is returned when a | |
1275 | termination string is entered (defaults to a single '.'). Input can also |
|
1279 | termination string is entered (defaults to a single '.'). Input can also | |
1276 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
1280 | terminate via EOF (^D in Unix, ^Z-RET in Windows). | |
1277 |
|
1281 | |||
1278 | Lines of input which end in \\ are joined into single entries (and a |
|
1282 | Lines of input which end in \\ are joined into single entries (and a | |
1279 | secondary continuation prompt is issued as long as the user terminates |
|
1283 | secondary continuation prompt is issued as long as the user terminates | |
1280 | lines with \\). This allows entering very long strings which are still |
|
1284 | lines with \\). This allows entering very long strings which are still | |
1281 | meant to be treated as single entities. |
|
1285 | meant to be treated as single entities. | |
1282 | """ |
|
1286 | """ | |
1283 |
|
1287 | |||
1284 | try: |
|
1288 | try: | |
1285 | if header: |
|
1289 | if header: | |
1286 | header += '\n' |
|
1290 | header += '\n' | |
1287 | lines = [raw_input(header + ps1)] |
|
1291 | lines = [raw_input(header + ps1)] | |
1288 | except EOFError: |
|
1292 | except EOFError: | |
1289 | return [] |
|
1293 | return [] | |
1290 | terminate = [terminate_str] |
|
1294 | terminate = [terminate_str] | |
1291 | try: |
|
1295 | try: | |
1292 | while lines[-1:] != terminate: |
|
1296 | while lines[-1:] != terminate: | |
1293 | new_line = raw_input(ps1) |
|
1297 | new_line = raw_input(ps1) | |
1294 | while new_line.endswith('\\'): |
|
1298 | while new_line.endswith('\\'): | |
1295 | new_line = new_line[:-1] + raw_input(ps2) |
|
1299 | new_line = new_line[:-1] + raw_input(ps2) | |
1296 | lines.append(new_line) |
|
1300 | lines.append(new_line) | |
1297 |
|
1301 | |||
1298 | return lines[:-1] # don't return the termination command |
|
1302 | return lines[:-1] # don't return the termination command | |
1299 | except EOFError: |
|
1303 | except EOFError: | |
1300 |
|
1304 | |||
1301 | return lines |
|
1305 | return lines | |
1302 |
|
1306 | |||
1303 | #---------------------------------------------------------------------------- |
|
1307 | #---------------------------------------------------------------------------- | |
1304 | def raw_input_ext(prompt='', ps2='... '): |
|
1308 | def raw_input_ext(prompt='', ps2='... '): | |
1305 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
1309 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" | |
1306 |
|
1310 | |||
1307 | line = raw_input(prompt) |
|
1311 | line = raw_input(prompt) | |
1308 | while line.endswith('\\'): |
|
1312 | while line.endswith('\\'): | |
1309 | line = line[:-1] + raw_input(ps2) |
|
1313 | line = line[:-1] + raw_input(ps2) | |
1310 | return line |
|
1314 | return line | |
1311 |
|
1315 | |||
1312 | #---------------------------------------------------------------------------- |
|
1316 | #---------------------------------------------------------------------------- | |
1313 | def ask_yes_no(prompt,default=None): |
|
1317 | def ask_yes_no(prompt,default=None): | |
1314 | """Asks a question and returns a boolean (y/n) answer. |
|
1318 | """Asks a question and returns a boolean (y/n) answer. | |
1315 |
|
1319 | |||
1316 | If default is given (one of 'y','n'), it is used if the user input is |
|
1320 | If default is given (one of 'y','n'), it is used if the user input is | |
1317 | empty. Otherwise the question is repeated until an answer is given. |
|
1321 | empty. Otherwise the question is repeated until an answer is given. | |
1318 |
|
1322 | |||
1319 | An EOF is treated as the default answer. If there is no default, an |
|
1323 | An EOF is treated as the default answer. If there is no default, an | |
1320 | exception is raised to prevent infinite loops. |
|
1324 | exception is raised to prevent infinite loops. | |
1321 |
|
1325 | |||
1322 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1326 | Valid answers are: y/yes/n/no (match is not case sensitive).""" | |
1323 |
|
1327 | |||
1324 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1328 | answers = {'y':True,'n':False,'yes':True,'no':False} | |
1325 | ans = None |
|
1329 | ans = None | |
1326 | while ans not in answers.keys(): |
|
1330 | while ans not in answers.keys(): | |
1327 | try: |
|
1331 | try: | |
1328 | ans = raw_input(prompt+' ').lower() |
|
1332 | ans = raw_input(prompt+' ').lower() | |
1329 | if not ans: # response was an empty string |
|
1333 | if not ans: # response was an empty string | |
1330 | ans = default |
|
1334 | ans = default | |
1331 | except KeyboardInterrupt: |
|
1335 | except KeyboardInterrupt: | |
1332 | pass |
|
1336 | pass | |
1333 | except EOFError: |
|
1337 | except EOFError: | |
1334 | if default in answers.keys(): |
|
1338 | if default in answers.keys(): | |
1335 | ans = default |
|
1339 | ans = default | |
1336 |
|
1340 | |||
1337 | else: |
|
1341 | else: | |
1338 | raise |
|
1342 | raise | |
1339 |
|
1343 | |||
1340 | return answers[ans] |
|
1344 | return answers[ans] | |
1341 |
|
1345 | |||
1342 | #---------------------------------------------------------------------------- |
|
1346 | #---------------------------------------------------------------------------- | |
1343 | def marquee(txt='',width=78,mark='*'): |
|
1347 | def marquee(txt='',width=78,mark='*'): | |
1344 | """Return the input string centered in a 'marquee'.""" |
|
1348 | """Return the input string centered in a 'marquee'.""" | |
1345 | if not txt: |
|
1349 | if not txt: | |
1346 | return (mark*width)[:width] |
|
1350 | return (mark*width)[:width] | |
1347 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1351 | nmark = (width-len(txt)-2)/len(mark)/2 | |
1348 | if nmark < 0: nmark =0 |
|
1352 | if nmark < 0: nmark =0 | |
1349 | marks = mark*nmark |
|
1353 | marks = mark*nmark | |
1350 | return '%s %s %s' % (marks,txt,marks) |
|
1354 | return '%s %s %s' % (marks,txt,marks) | |
1351 |
|
1355 | |||
1352 | #---------------------------------------------------------------------------- |
|
1356 | #---------------------------------------------------------------------------- | |
1353 | class EvalDict: |
|
1357 | class EvalDict: | |
1354 | """ |
|
1358 | """ | |
1355 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1359 | Emulate a dict which evaluates its contents in the caller's frame. | |
1356 |
|
1360 | |||
1357 | Usage: |
|
1361 | Usage: | |
1358 | >>> number = 19 |
|
1362 | >>> number = 19 | |
1359 |
|
1363 | |||
1360 | >>> text = "python" |
|
1364 | >>> text = "python" | |
1361 |
|
1365 | |||
1362 | >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1366 | >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() | |
1363 | Python 2.1 rules! |
|
1367 | Python 2.1 rules! | |
1364 | """ |
|
1368 | """ | |
1365 |
|
1369 | |||
1366 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1370 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a | |
1367 | # modified (shorter) version of: |
|
1371 | # modified (shorter) version of: | |
1368 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1372 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by | |
1369 | # Skip Montanaro (skip@pobox.com). |
|
1373 | # Skip Montanaro (skip@pobox.com). | |
1370 |
|
1374 | |||
1371 | def __getitem__(self, name): |
|
1375 | def __getitem__(self, name): | |
1372 | frame = sys._getframe(1) |
|
1376 | frame = sys._getframe(1) | |
1373 | return eval(name, frame.f_globals, frame.f_locals) |
|
1377 | return eval(name, frame.f_globals, frame.f_locals) | |
1374 |
|
1378 | |||
1375 | EvalString = EvalDict # for backwards compatibility |
|
1379 | EvalString = EvalDict # for backwards compatibility | |
1376 | #---------------------------------------------------------------------------- |
|
1380 | #---------------------------------------------------------------------------- | |
1377 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1381 | def qw(words,flat=0,sep=None,maxsplit=-1): | |
1378 | """Similar to Perl's qw() operator, but with some more options. |
|
1382 | """Similar to Perl's qw() operator, but with some more options. | |
1379 |
|
1383 | |||
1380 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1384 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) | |
1381 |
|
1385 | |||
1382 | words can also be a list itself, and with flat=1, the output will be |
|
1386 | words can also be a list itself, and with flat=1, the output will be | |
1383 | recursively flattened. |
|
1387 | recursively flattened. | |
1384 |
|
1388 | |||
1385 | Examples: |
|
1389 | Examples: | |
1386 |
|
1390 | |||
1387 | >>> qw('1 2') |
|
1391 | >>> qw('1 2') | |
1388 | ['1', '2'] |
|
1392 | ['1', '2'] | |
1389 |
|
1393 | |||
1390 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1394 | >>> qw(['a b','1 2',['m n','p q']]) | |
1391 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1395 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] | |
1392 |
|
1396 | |||
1393 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1397 | >>> qw(['a b','1 2',['m n','p q']],flat=1) | |
1394 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] |
|
1398 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] | |
1395 | """ |
|
1399 | """ | |
1396 |
|
1400 | |||
1397 | if type(words) in StringTypes: |
|
1401 | if type(words) in StringTypes: | |
1398 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1402 | return [word.strip() for word in words.split(sep,maxsplit) | |
1399 | if word and not word.isspace() ] |
|
1403 | if word and not word.isspace() ] | |
1400 | if flat: |
|
1404 | if flat: | |
1401 | return flatten(map(qw,words,[1]*len(words))) |
|
1405 | return flatten(map(qw,words,[1]*len(words))) | |
1402 | return map(qw,words) |
|
1406 | return map(qw,words) | |
1403 |
|
1407 | |||
1404 | #---------------------------------------------------------------------------- |
|
1408 | #---------------------------------------------------------------------------- | |
1405 | def qwflat(words,sep=None,maxsplit=-1): |
|
1409 | def qwflat(words,sep=None,maxsplit=-1): | |
1406 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1410 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" | |
1407 | return qw(words,1,sep,maxsplit) |
|
1411 | return qw(words,1,sep,maxsplit) | |
1408 |
|
1412 | |||
1409 | #---------------------------------------------------------------------------- |
|
1413 | #---------------------------------------------------------------------------- | |
1410 | def qw_lol(indata): |
|
1414 | def qw_lol(indata): | |
1411 | """qw_lol('a b') -> [['a','b']], |
|
1415 | """qw_lol('a b') -> [['a','b']], | |
1412 | otherwise it's just a call to qw(). |
|
1416 | otherwise it's just a call to qw(). | |
1413 |
|
1417 | |||
1414 | We need this to make sure the modules_some keys *always* end up as a |
|
1418 | We need this to make sure the modules_some keys *always* end up as a | |
1415 | list of lists.""" |
|
1419 | list of lists.""" | |
1416 |
|
1420 | |||
1417 | if type(indata) in StringTypes: |
|
1421 | if type(indata) in StringTypes: | |
1418 | return [qw(indata)] |
|
1422 | return [qw(indata)] | |
1419 | else: |
|
1423 | else: | |
1420 | return qw(indata) |
|
1424 | return qw(indata) | |
1421 |
|
1425 | |||
1422 | #----------------------------------------------------------------------------- |
|
1426 | #----------------------------------------------------------------------------- | |
1423 | def list_strings(arg): |
|
1427 | def list_strings(arg): | |
1424 | """Always return a list of strings, given a string or list of strings |
|
1428 | """Always return a list of strings, given a string or list of strings | |
1425 | as input.""" |
|
1429 | as input.""" | |
1426 |
|
1430 | |||
1427 | if type(arg) in StringTypes: return [arg] |
|
1431 | if type(arg) in StringTypes: return [arg] | |
1428 | else: return arg |
|
1432 | else: return arg | |
1429 |
|
1433 | |||
1430 | #---------------------------------------------------------------------------- |
|
1434 | #---------------------------------------------------------------------------- | |
1431 | def grep(pat,list,case=1): |
|
1435 | def grep(pat,list,case=1): | |
1432 | """Simple minded grep-like function. |
|
1436 | """Simple minded grep-like function. | |
1433 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1437 | grep(pat,list) returns occurrences of pat in list, None on failure. | |
1434 |
|
1438 | |||
1435 | It only does simple string matching, with no support for regexps. Use the |
|
1439 | It only does simple string matching, with no support for regexps. Use the | |
1436 | option case=0 for case-insensitive matching.""" |
|
1440 | option case=0 for case-insensitive matching.""" | |
1437 |
|
1441 | |||
1438 | # This is pretty crude. At least it should implement copying only references |
|
1442 | # This is pretty crude. At least it should implement copying only references | |
1439 | # to the original data in case it's big. Now it copies the data for output. |
|
1443 | # to the original data in case it's big. Now it copies the data for output. | |
1440 | out=[] |
|
1444 | out=[] | |
1441 | if case: |
|
1445 | if case: | |
1442 | for term in list: |
|
1446 | for term in list: | |
1443 | if term.find(pat)>-1: out.append(term) |
|
1447 | if term.find(pat)>-1: out.append(term) | |
1444 | else: |
|
1448 | else: | |
1445 | lpat=pat.lower() |
|
1449 | lpat=pat.lower() | |
1446 | for term in list: |
|
1450 | for term in list: | |
1447 | if term.lower().find(lpat)>-1: out.append(term) |
|
1451 | if term.lower().find(lpat)>-1: out.append(term) | |
1448 |
|
1452 | |||
1449 | if len(out): return out |
|
1453 | if len(out): return out | |
1450 | else: return None |
|
1454 | else: return None | |
1451 |
|
1455 | |||
1452 | #---------------------------------------------------------------------------- |
|
1456 | #---------------------------------------------------------------------------- | |
1453 | def dgrep(pat,*opts): |
|
1457 | def dgrep(pat,*opts): | |
1454 | """Return grep() on dir()+dir(__builtins__). |
|
1458 | """Return grep() on dir()+dir(__builtins__). | |
1455 |
|
1459 | |||
1456 | A very common use of grep() when working interactively.""" |
|
1460 | A very common use of grep() when working interactively.""" | |
1457 |
|
1461 | |||
1458 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1462 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) | |
1459 |
|
1463 | |||
1460 | #---------------------------------------------------------------------------- |
|
1464 | #---------------------------------------------------------------------------- | |
1461 | def idgrep(pat): |
|
1465 | def idgrep(pat): | |
1462 | """Case-insensitive dgrep()""" |
|
1466 | """Case-insensitive dgrep()""" | |
1463 |
|
1467 | |||
1464 | return dgrep(pat,0) |
|
1468 | return dgrep(pat,0) | |
1465 |
|
1469 | |||
1466 | #---------------------------------------------------------------------------- |
|
1470 | #---------------------------------------------------------------------------- | |
1467 | def igrep(pat,list): |
|
1471 | def igrep(pat,list): | |
1468 | """Synonym for case-insensitive grep.""" |
|
1472 | """Synonym for case-insensitive grep.""" | |
1469 |
|
1473 | |||
1470 | return grep(pat,list,case=0) |
|
1474 | return grep(pat,list,case=0) | |
1471 |
|
1475 | |||
1472 | #---------------------------------------------------------------------------- |
|
1476 | #---------------------------------------------------------------------------- | |
1473 | def indent(str,nspaces=4,ntabs=0): |
|
1477 | def indent(str,nspaces=4,ntabs=0): | |
1474 | """Indent a string a given number of spaces or tabstops. |
|
1478 | """Indent a string a given number of spaces or tabstops. | |
1475 |
|
1479 | |||
1476 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1480 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. | |
1477 | """ |
|
1481 | """ | |
1478 | if str is None: |
|
1482 | if str is None: | |
1479 | return |
|
1483 | return | |
1480 | ind = '\t'*ntabs+' '*nspaces |
|
1484 | ind = '\t'*ntabs+' '*nspaces | |
1481 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1485 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) | |
1482 | if outstr.endswith(os.linesep+ind): |
|
1486 | if outstr.endswith(os.linesep+ind): | |
1483 | return outstr[:-len(ind)] |
|
1487 | return outstr[:-len(ind)] | |
1484 | else: |
|
1488 | else: | |
1485 | return outstr |
|
1489 | return outstr | |
1486 |
|
1490 | |||
1487 | #----------------------------------------------------------------------------- |
|
1491 | #----------------------------------------------------------------------------- | |
1488 | def native_line_ends(filename,backup=1): |
|
1492 | def native_line_ends(filename,backup=1): | |
1489 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1493 | """Convert (in-place) a file to line-ends native to the current OS. | |
1490 |
|
1494 | |||
1491 | If the optional backup argument is given as false, no backup of the |
|
1495 | If the optional backup argument is given as false, no backup of the | |
1492 | original file is left. """ |
|
1496 | original file is left. """ | |
1493 |
|
1497 | |||
1494 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1498 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} | |
1495 |
|
1499 | |||
1496 | bak_filename = filename + backup_suffixes[os.name] |
|
1500 | bak_filename = filename + backup_suffixes[os.name] | |
1497 |
|
1501 | |||
1498 | original = open(filename).read() |
|
1502 | original = open(filename).read() | |
1499 | shutil.copy2(filename,bak_filename) |
|
1503 | shutil.copy2(filename,bak_filename) | |
1500 | try: |
|
1504 | try: | |
1501 | new = open(filename,'wb') |
|
1505 | new = open(filename,'wb') | |
1502 | new.write(os.linesep.join(original.splitlines())) |
|
1506 | new.write(os.linesep.join(original.splitlines())) | |
1503 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1507 | new.write(os.linesep) # ALWAYS put an eol at the end of the file | |
1504 | new.close() |
|
1508 | new.close() | |
1505 | except: |
|
1509 | except: | |
1506 | os.rename(bak_filename,filename) |
|
1510 | os.rename(bak_filename,filename) | |
1507 | if not backup: |
|
1511 | if not backup: | |
1508 | try: |
|
1512 | try: | |
1509 | os.remove(bak_filename) |
|
1513 | os.remove(bak_filename) | |
1510 | except: |
|
1514 | except: | |
1511 | pass |
|
1515 | pass | |
1512 |
|
1516 | |||
1513 | #---------------------------------------------------------------------------- |
|
1517 | #---------------------------------------------------------------------------- | |
1514 | def get_pager_cmd(pager_cmd = None): |
|
1518 | def get_pager_cmd(pager_cmd = None): | |
1515 | """Return a pager command. |
|
1519 | """Return a pager command. | |
1516 |
|
1520 | |||
1517 | Makes some attempts at finding an OS-correct one.""" |
|
1521 | Makes some attempts at finding an OS-correct one.""" | |
1518 |
|
1522 | |||
1519 | if os.name == 'posix': |
|
1523 | if os.name == 'posix': | |
1520 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1524 | default_pager_cmd = 'less -r' # -r for color control sequences | |
1521 | elif os.name in ['nt','dos']: |
|
1525 | elif os.name in ['nt','dos']: | |
1522 | default_pager_cmd = 'type' |
|
1526 | default_pager_cmd = 'type' | |
1523 |
|
1527 | |||
1524 | if pager_cmd is None: |
|
1528 | if pager_cmd is None: | |
1525 | try: |
|
1529 | try: | |
1526 | pager_cmd = os.environ['PAGER'] |
|
1530 | pager_cmd = os.environ['PAGER'] | |
1527 | except: |
|
1531 | except: | |
1528 | pager_cmd = default_pager_cmd |
|
1532 | pager_cmd = default_pager_cmd | |
1529 | return pager_cmd |
|
1533 | return pager_cmd | |
1530 |
|
1534 | |||
1531 | #----------------------------------------------------------------------------- |
|
1535 | #----------------------------------------------------------------------------- | |
1532 | def get_pager_start(pager,start): |
|
1536 | def get_pager_start(pager,start): | |
1533 | """Return the string for paging files with an offset. |
|
1537 | """Return the string for paging files with an offset. | |
1534 |
|
1538 | |||
1535 | This is the '+N' argument which less and more (under Unix) accept. |
|
1539 | This is the '+N' argument which less and more (under Unix) accept. | |
1536 | """ |
|
1540 | """ | |
1537 |
|
1541 | |||
1538 | if pager in ['less','more']: |
|
1542 | if pager in ['less','more']: | |
1539 | if start: |
|
1543 | if start: | |
1540 | start_string = '+' + str(start) |
|
1544 | start_string = '+' + str(start) | |
1541 | else: |
|
1545 | else: | |
1542 | start_string = '' |
|
1546 | start_string = '' | |
1543 | else: |
|
1547 | else: | |
1544 | start_string = '' |
|
1548 | start_string = '' | |
1545 | return start_string |
|
1549 | return start_string | |
1546 |
|
1550 | |||
1547 | #---------------------------------------------------------------------------- |
|
1551 | #---------------------------------------------------------------------------- | |
1548 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() |
|
1552 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() | |
1549 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
1553 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': | |
1550 | import msvcrt |
|
1554 | import msvcrt | |
1551 | def page_more(): |
|
1555 | def page_more(): | |
1552 | """ Smart pausing between pages |
|
1556 | """ Smart pausing between pages | |
1553 |
|
1557 | |||
1554 | @return: True if need print more lines, False if quit |
|
1558 | @return: True if need print more lines, False if quit | |
1555 | """ |
|
1559 | """ | |
1556 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1560 | Term.cout.write('---Return to continue, q to quit--- ') | |
1557 | ans = msvcrt.getch() |
|
1561 | ans = msvcrt.getch() | |
1558 | if ans in ("q", "Q"): |
|
1562 | if ans in ("q", "Q"): | |
1559 | result = False |
|
1563 | result = False | |
1560 | else: |
|
1564 | else: | |
1561 | result = True |
|
1565 | result = True | |
1562 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1566 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) | |
1563 | return result |
|
1567 | return result | |
1564 | else: |
|
1568 | else: | |
1565 | def page_more(): |
|
1569 | def page_more(): | |
1566 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1570 | ans = raw_input('---Return to continue, q to quit--- ') | |
1567 | if ans.lower().startswith('q'): |
|
1571 | if ans.lower().startswith('q'): | |
1568 | return False |
|
1572 | return False | |
1569 | else: |
|
1573 | else: | |
1570 | return True |
|
1574 | return True | |
1571 |
|
1575 | |||
1572 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1576 | esc_re = re.compile(r"(\x1b[^m]+m)") | |
1573 |
|
1577 | |||
1574 | def page_dumb(strng,start=0,screen_lines=25): |
|
1578 | def page_dumb(strng,start=0,screen_lines=25): | |
1575 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1579 | """Very dumb 'pager' in Python, for when nothing else works. | |
1576 |
|
1580 | |||
1577 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1581 | Only moves forward, same interface as page(), except for pager_cmd and | |
1578 | mode.""" |
|
1582 | mode.""" | |
1579 |
|
1583 | |||
1580 | out_ln = strng.splitlines()[start:] |
|
1584 | out_ln = strng.splitlines()[start:] | |
1581 | screens = chop(out_ln,screen_lines-1) |
|
1585 | screens = chop(out_ln,screen_lines-1) | |
1582 | if len(screens) == 1: |
|
1586 | if len(screens) == 1: | |
1583 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1587 | print >>Term.cout, os.linesep.join(screens[0]) | |
1584 | else: |
|
1588 | else: | |
1585 | last_escape = "" |
|
1589 | last_escape = "" | |
1586 | for scr in screens[0:-1]: |
|
1590 | for scr in screens[0:-1]: | |
1587 | hunk = os.linesep.join(scr) |
|
1591 | hunk = os.linesep.join(scr) | |
1588 | print >>Term.cout, last_escape + hunk |
|
1592 | print >>Term.cout, last_escape + hunk | |
1589 | if not page_more(): |
|
1593 | if not page_more(): | |
1590 | return |
|
1594 | return | |
1591 | esc_list = esc_re.findall(hunk) |
|
1595 | esc_list = esc_re.findall(hunk) | |
1592 | if len(esc_list) > 0: |
|
1596 | if len(esc_list) > 0: | |
1593 | last_escape = esc_list[-1] |
|
1597 | last_escape = esc_list[-1] | |
1594 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1598 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) | |
1595 |
|
1599 | |||
1596 | #---------------------------------------------------------------------------- |
|
1600 | #---------------------------------------------------------------------------- | |
1597 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1601 | def page(strng,start=0,screen_lines=0,pager_cmd = None): | |
1598 | """Print a string, piping through a pager after a certain length. |
|
1602 | """Print a string, piping through a pager after a certain length. | |
1599 |
|
1603 | |||
1600 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1604 | The screen_lines parameter specifies the number of *usable* lines of your | |
1601 | terminal screen (total lines minus lines you need to reserve to show other |
|
1605 | terminal screen (total lines minus lines you need to reserve to show other | |
1602 | information). |
|
1606 | information). | |
1603 |
|
1607 | |||
1604 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1608 | If you set screen_lines to a number <=0, page() will try to auto-determine | |
1605 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1609 | your screen size and will only use up to (screen_size+screen_lines) for | |
1606 | printing, paging after that. That is, if you want auto-detection but need |
|
1610 | printing, paging after that. That is, if you want auto-detection but need | |
1607 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1611 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for | |
1608 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1612 | auto-detection without any lines reserved simply use screen_lines = 0. | |
1609 |
|
1613 | |||
1610 | If a string won't fit in the allowed lines, it is sent through the |
|
1614 | If a string won't fit in the allowed lines, it is sent through the | |
1611 | specified pager command. If none given, look for PAGER in the environment, |
|
1615 | specified pager command. If none given, look for PAGER in the environment, | |
1612 | and ultimately default to less. |
|
1616 | and ultimately default to less. | |
1613 |
|
1617 | |||
1614 | If no system pager works, the string is sent through a 'dumb pager' |
|
1618 | If no system pager works, the string is sent through a 'dumb pager' | |
1615 | written in python, very simplistic. |
|
1619 | written in python, very simplistic. | |
1616 | """ |
|
1620 | """ | |
1617 |
|
1621 | |||
1618 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
1622 | # Some routines may auto-compute start offsets incorrectly and pass a | |
1619 | # negative value. Offset to 0 for robustness. |
|
1623 | # negative value. Offset to 0 for robustness. | |
1620 | start = max(0,start) |
|
1624 | start = max(0,start) | |
1621 |
|
1625 | |||
1622 | # first, try the hook |
|
1626 | # first, try the hook | |
1623 | ip = IPython.ipapi.get() |
|
1627 | ip = IPython.ipapi.get() | |
1624 | if ip: |
|
1628 | if ip: | |
1625 | try: |
|
1629 | try: | |
1626 | ip.IP.hooks.show_in_pager(strng) |
|
1630 | ip.IP.hooks.show_in_pager(strng) | |
1627 | return |
|
1631 | return | |
1628 | except IPython.ipapi.TryNext: |
|
1632 | except IPython.ipapi.TryNext: | |
1629 | pass |
|
1633 | pass | |
1630 |
|
1634 | |||
1631 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1635 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs | |
1632 | TERM = os.environ.get('TERM','dumb') |
|
1636 | TERM = os.environ.get('TERM','dumb') | |
1633 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1637 | if TERM in ['dumb','emacs'] and os.name != 'nt': | |
1634 | print strng |
|
1638 | print strng | |
1635 | return |
|
1639 | return | |
1636 | # chop off the topmost part of the string we don't want to see |
|
1640 | # chop off the topmost part of the string we don't want to see | |
1637 | str_lines = strng.split(os.linesep)[start:] |
|
1641 | str_lines = strng.split(os.linesep)[start:] | |
1638 | str_toprint = os.linesep.join(str_lines) |
|
1642 | str_toprint = os.linesep.join(str_lines) | |
1639 | num_newlines = len(str_lines) |
|
1643 | num_newlines = len(str_lines) | |
1640 | len_str = len(str_toprint) |
|
1644 | len_str = len(str_toprint) | |
1641 |
|
1645 | |||
1642 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1646 | # Dumb heuristics to guesstimate number of on-screen lines the string | |
1643 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1647 | # takes. Very basic, but good enough for docstrings in reasonable | |
1644 | # terminals. If someone later feels like refining it, it's not hard. |
|
1648 | # terminals. If someone later feels like refining it, it's not hard. | |
1645 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1649 | numlines = max(num_newlines,int(len_str/80)+1) | |
1646 |
|
1650 | |||
1647 | if os.name == "nt": |
|
1651 | if os.name == "nt": | |
1648 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1652 | screen_lines_def = get_console_size(defaulty=25)[1] | |
1649 | else: |
|
1653 | else: | |
1650 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1654 | screen_lines_def = 25 # default value if we can't auto-determine | |
1651 |
|
1655 | |||
1652 | # auto-determine screen size |
|
1656 | # auto-determine screen size | |
1653 | if screen_lines <= 0: |
|
1657 | if screen_lines <= 0: | |
1654 | if TERM=='xterm': |
|
1658 | if TERM=='xterm': | |
1655 | use_curses = USE_CURSES |
|
1659 | use_curses = USE_CURSES | |
1656 | else: |
|
1660 | else: | |
1657 | # curses causes problems on many terminals other than xterm. |
|
1661 | # curses causes problems on many terminals other than xterm. | |
1658 | use_curses = False |
|
1662 | use_curses = False | |
1659 | if use_curses: |
|
1663 | if use_curses: | |
1660 | # There is a bug in curses, where *sometimes* it fails to properly |
|
1664 | # There is a bug in curses, where *sometimes* it fails to properly | |
1661 | # initialize, and then after the endwin() call is made, the |
|
1665 | # initialize, and then after the endwin() call is made, the | |
1662 | # terminal is left in an unusable state. Rather than trying to |
|
1666 | # terminal is left in an unusable state. Rather than trying to | |
1663 | # check everytime for this (by requesting and comparing termios |
|
1667 | # check everytime for this (by requesting and comparing termios | |
1664 | # flags each time), we just save the initial terminal state and |
|
1668 | # flags each time), we just save the initial terminal state and | |
1665 | # unconditionally reset it every time. It's cheaper than making |
|
1669 | # unconditionally reset it every time. It's cheaper than making | |
1666 | # the checks. |
|
1670 | # the checks. | |
1667 | term_flags = termios.tcgetattr(sys.stdout) |
|
1671 | term_flags = termios.tcgetattr(sys.stdout) | |
1668 | scr = curses.initscr() |
|
1672 | scr = curses.initscr() | |
1669 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1673 | screen_lines_real,screen_cols = scr.getmaxyx() | |
1670 | curses.endwin() |
|
1674 | curses.endwin() | |
1671 | # Restore terminal state in case endwin() didn't. |
|
1675 | # Restore terminal state in case endwin() didn't. | |
1672 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
|
1676 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) | |
1673 | # Now we have what we needed: the screen size in rows/columns |
|
1677 | # Now we have what we needed: the screen size in rows/columns | |
1674 | screen_lines += screen_lines_real |
|
1678 | screen_lines += screen_lines_real | |
1675 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1679 | #print '***Screen size:',screen_lines_real,'lines x',\ | |
1676 | #screen_cols,'columns.' # dbg |
|
1680 | #screen_cols,'columns.' # dbg | |
1677 | else: |
|
1681 | else: | |
1678 | screen_lines += screen_lines_def |
|
1682 | screen_lines += screen_lines_def | |
1679 |
|
1683 | |||
1680 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1684 | #print 'numlines',numlines,'screenlines',screen_lines # dbg | |
1681 | if numlines <= screen_lines : |
|
1685 | if numlines <= screen_lines : | |
1682 | #print '*** normal print' # dbg |
|
1686 | #print '*** normal print' # dbg | |
1683 | print >>Term.cout, str_toprint |
|
1687 | print >>Term.cout, str_toprint | |
1684 | else: |
|
1688 | else: | |
1685 | # Try to open pager and default to internal one if that fails. |
|
1689 | # Try to open pager and default to internal one if that fails. | |
1686 | # All failure modes are tagged as 'retval=1', to match the return |
|
1690 | # All failure modes are tagged as 'retval=1', to match the return | |
1687 | # value of a failed system command. If any intermediate attempt |
|
1691 | # value of a failed system command. If any intermediate attempt | |
1688 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1692 | # sets retval to 1, at the end we resort to our own page_dumb() pager. | |
1689 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1693 | pager_cmd = get_pager_cmd(pager_cmd) | |
1690 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1694 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1691 | if os.name == 'nt': |
|
1695 | if os.name == 'nt': | |
1692 | if pager_cmd.startswith('type'): |
|
1696 | if pager_cmd.startswith('type'): | |
1693 | # The default WinXP 'type' command is failing on complex strings. |
|
1697 | # The default WinXP 'type' command is failing on complex strings. | |
1694 | retval = 1 |
|
1698 | retval = 1 | |
1695 | else: |
|
1699 | else: | |
1696 | tmpname = tempfile.mktemp('.txt') |
|
1700 | tmpname = tempfile.mktemp('.txt') | |
1697 | tmpfile = file(tmpname,'wt') |
|
1701 | tmpfile = file(tmpname,'wt') | |
1698 | tmpfile.write(strng) |
|
1702 | tmpfile.write(strng) | |
1699 | tmpfile.close() |
|
1703 | tmpfile.close() | |
1700 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1704 | cmd = "%s < %s" % (pager_cmd,tmpname) | |
1701 | if os.system(cmd): |
|
1705 | if os.system(cmd): | |
1702 | retval = 1 |
|
1706 | retval = 1 | |
1703 | else: |
|
1707 | else: | |
1704 | retval = None |
|
1708 | retval = None | |
1705 | os.remove(tmpname) |
|
1709 | os.remove(tmpname) | |
1706 | else: |
|
1710 | else: | |
1707 | try: |
|
1711 | try: | |
1708 | retval = None |
|
1712 | retval = None | |
1709 | # if I use popen4, things hang. No idea why. |
|
1713 | # if I use popen4, things hang. No idea why. | |
1710 | #pager,shell_out = os.popen4(pager_cmd) |
|
1714 | #pager,shell_out = os.popen4(pager_cmd) | |
1711 | pager = os.popen(pager_cmd,'w') |
|
1715 | pager = os.popen(pager_cmd,'w') | |
1712 | pager.write(strng) |
|
1716 | pager.write(strng) | |
1713 | pager.close() |
|
1717 | pager.close() | |
1714 | retval = pager.close() # success returns None |
|
1718 | retval = pager.close() # success returns None | |
1715 | except IOError,msg: # broken pipe when user quits |
|
1719 | except IOError,msg: # broken pipe when user quits | |
1716 | if msg.args == (32,'Broken pipe'): |
|
1720 | if msg.args == (32,'Broken pipe'): | |
1717 | retval = None |
|
1721 | retval = None | |
1718 | else: |
|
1722 | else: | |
1719 | retval = 1 |
|
1723 | retval = 1 | |
1720 | except OSError: |
|
1724 | except OSError: | |
1721 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1725 | # Other strange problems, sometimes seen in Win2k/cygwin | |
1722 | retval = 1 |
|
1726 | retval = 1 | |
1723 | if retval is not None: |
|
1727 | if retval is not None: | |
1724 | page_dumb(strng,screen_lines=screen_lines) |
|
1728 | page_dumb(strng,screen_lines=screen_lines) | |
1725 |
|
1729 | |||
1726 | #---------------------------------------------------------------------------- |
|
1730 | #---------------------------------------------------------------------------- | |
1727 | def page_file(fname,start = 0, pager_cmd = None): |
|
1731 | def page_file(fname,start = 0, pager_cmd = None): | |
1728 | """Page a file, using an optional pager command and starting line. |
|
1732 | """Page a file, using an optional pager command and starting line. | |
1729 | """ |
|
1733 | """ | |
1730 |
|
1734 | |||
1731 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1735 | pager_cmd = get_pager_cmd(pager_cmd) | |
1732 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1736 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1733 |
|
1737 | |||
1734 | try: |
|
1738 | try: | |
1735 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1739 | if os.environ['TERM'] in ['emacs','dumb']: | |
1736 | raise EnvironmentError |
|
1740 | raise EnvironmentError | |
1737 | xsys(pager_cmd + ' ' + fname) |
|
1741 | xsys(pager_cmd + ' ' + fname) | |
1738 | except: |
|
1742 | except: | |
1739 | try: |
|
1743 | try: | |
1740 | if start > 0: |
|
1744 | if start > 0: | |
1741 | start -= 1 |
|
1745 | start -= 1 | |
1742 | page(open(fname).read(),start) |
|
1746 | page(open(fname).read(),start) | |
1743 | except: |
|
1747 | except: | |
1744 | print 'Unable to show file',`fname` |
|
1748 | print 'Unable to show file',`fname` | |
1745 |
|
1749 | |||
1746 |
|
1750 | |||
1747 | #---------------------------------------------------------------------------- |
|
1751 | #---------------------------------------------------------------------------- | |
1748 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1752 | def snip_print(str,width = 75,print_full = 0,header = ''): | |
1749 | """Print a string snipping the midsection to fit in width. |
|
1753 | """Print a string snipping the midsection to fit in width. | |
1750 |
|
1754 | |||
1751 | print_full: mode control: |
|
1755 | print_full: mode control: | |
1752 | - 0: only snip long strings |
|
1756 | - 0: only snip long strings | |
1753 | - 1: send to page() directly. |
|
1757 | - 1: send to page() directly. | |
1754 | - 2: snip long strings and ask for full length viewing with page() |
|
1758 | - 2: snip long strings and ask for full length viewing with page() | |
1755 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1759 | Return 1 if snipping was necessary, 0 otherwise.""" | |
1756 |
|
1760 | |||
1757 | if print_full == 1: |
|
1761 | if print_full == 1: | |
1758 | page(header+str) |
|
1762 | page(header+str) | |
1759 | return 0 |
|
1763 | return 0 | |
1760 |
|
1764 | |||
1761 | print header, |
|
1765 | print header, | |
1762 | if len(str) < width: |
|
1766 | if len(str) < width: | |
1763 | print str |
|
1767 | print str | |
1764 | snip = 0 |
|
1768 | snip = 0 | |
1765 | else: |
|
1769 | else: | |
1766 | whalf = int((width -5)/2) |
|
1770 | whalf = int((width -5)/2) | |
1767 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1771 | print str[:whalf] + ' <...> ' + str[-whalf:] | |
1768 | snip = 1 |
|
1772 | snip = 1 | |
1769 | if snip and print_full == 2: |
|
1773 | if snip and print_full == 2: | |
1770 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1774 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': | |
1771 | page(str) |
|
1775 | page(str) | |
1772 | return snip |
|
1776 | return snip | |
1773 |
|
1777 | |||
1774 | #**************************************************************************** |
|
1778 | #**************************************************************************** | |
1775 | # lists, dicts and structures |
|
1779 | # lists, dicts and structures | |
1776 |
|
1780 | |||
1777 | def belong(candidates,checklist): |
|
1781 | def belong(candidates,checklist): | |
1778 | """Check whether a list of items appear in a given list of options. |
|
1782 | """Check whether a list of items appear in a given list of options. | |
1779 |
|
1783 | |||
1780 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1784 | Returns a list of 1 and 0, one for each candidate given.""" | |
1781 |
|
1785 | |||
1782 | return [x in checklist for x in candidates] |
|
1786 | return [x in checklist for x in candidates] | |
1783 |
|
1787 | |||
1784 | #---------------------------------------------------------------------------- |
|
1788 | #---------------------------------------------------------------------------- | |
1785 | def uniq_stable(elems): |
|
1789 | def uniq_stable(elems): | |
1786 | """uniq_stable(elems) -> list |
|
1790 | """uniq_stable(elems) -> list | |
1787 |
|
1791 | |||
1788 | Return from an iterable, a list of all the unique elements in the input, |
|
1792 | Return from an iterable, a list of all the unique elements in the input, | |
1789 | but maintaining the order in which they first appear. |
|
1793 | but maintaining the order in which they first appear. | |
1790 |
|
1794 | |||
1791 | A naive solution to this problem which just makes a dictionary with the |
|
1795 | A naive solution to this problem which just makes a dictionary with the | |
1792 | elements as keys fails to respect the stability condition, since |
|
1796 | elements as keys fails to respect the stability condition, since | |
1793 | dictionaries are unsorted by nature. |
|
1797 | dictionaries are unsorted by nature. | |
1794 |
|
1798 | |||
1795 | Note: All elements in the input must be valid dictionary keys for this |
|
1799 | Note: All elements in the input must be valid dictionary keys for this | |
1796 | routine to work, as it internally uses a dictionary for efficiency |
|
1800 | routine to work, as it internally uses a dictionary for efficiency | |
1797 | reasons.""" |
|
1801 | reasons.""" | |
1798 |
|
1802 | |||
1799 | unique = [] |
|
1803 | unique = [] | |
1800 | unique_dict = {} |
|
1804 | unique_dict = {} | |
1801 | for nn in elems: |
|
1805 | for nn in elems: | |
1802 | if nn not in unique_dict: |
|
1806 | if nn not in unique_dict: | |
1803 | unique.append(nn) |
|
1807 | unique.append(nn) | |
1804 | unique_dict[nn] = None |
|
1808 | unique_dict[nn] = None | |
1805 | return unique |
|
1809 | return unique | |
1806 |
|
1810 | |||
1807 | #---------------------------------------------------------------------------- |
|
1811 | #---------------------------------------------------------------------------- | |
1808 | class NLprinter: |
|
1812 | class NLprinter: | |
1809 | """Print an arbitrarily nested list, indicating index numbers. |
|
1813 | """Print an arbitrarily nested list, indicating index numbers. | |
1810 |
|
1814 | |||
1811 | An instance of this class called nlprint is available and callable as a |
|
1815 | An instance of this class called nlprint is available and callable as a | |
1812 | function. |
|
1816 | function. | |
1813 |
|
1817 | |||
1814 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1818 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' | |
1815 | and using 'sep' to separate the index from the value. """ |
|
1819 | and using 'sep' to separate the index from the value. """ | |
1816 |
|
1820 | |||
1817 | def __init__(self): |
|
1821 | def __init__(self): | |
1818 | self.depth = 0 |
|
1822 | self.depth = 0 | |
1819 |
|
1823 | |||
1820 | def __call__(self,lst,pos='',**kw): |
|
1824 | def __call__(self,lst,pos='',**kw): | |
1821 | """Prints the nested list numbering levels.""" |
|
1825 | """Prints the nested list numbering levels.""" | |
1822 | kw.setdefault('indent',' ') |
|
1826 | kw.setdefault('indent',' ') | |
1823 | kw.setdefault('sep',': ') |
|
1827 | kw.setdefault('sep',': ') | |
1824 | kw.setdefault('start',0) |
|
1828 | kw.setdefault('start',0) | |
1825 | kw.setdefault('stop',len(lst)) |
|
1829 | kw.setdefault('stop',len(lst)) | |
1826 | # we need to remove start and stop from kw so they don't propagate |
|
1830 | # we need to remove start and stop from kw so they don't propagate | |
1827 | # into a recursive call for a nested list. |
|
1831 | # into a recursive call for a nested list. | |
1828 | start = kw['start']; del kw['start'] |
|
1832 | start = kw['start']; del kw['start'] | |
1829 | stop = kw['stop']; del kw['stop'] |
|
1833 | stop = kw['stop']; del kw['stop'] | |
1830 | if self.depth == 0 and 'header' in kw.keys(): |
|
1834 | if self.depth == 0 and 'header' in kw.keys(): | |
1831 | print kw['header'] |
|
1835 | print kw['header'] | |
1832 |
|
1836 | |||
1833 | for idx in range(start,stop): |
|
1837 | for idx in range(start,stop): | |
1834 | elem = lst[idx] |
|
1838 | elem = lst[idx] | |
1835 | if type(elem)==type([]): |
|
1839 | if type(elem)==type([]): | |
1836 | self.depth += 1 |
|
1840 | self.depth += 1 | |
1837 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1841 | self.__call__(elem,itpl('$pos$idx,'),**kw) | |
1838 | self.depth -= 1 |
|
1842 | self.depth -= 1 | |
1839 | else: |
|
1843 | else: | |
1840 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1844 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') | |
1841 |
|
1845 | |||
1842 | nlprint = NLprinter() |
|
1846 | nlprint = NLprinter() | |
1843 | #---------------------------------------------------------------------------- |
|
1847 | #---------------------------------------------------------------------------- | |
1844 | def all_belong(candidates,checklist): |
|
1848 | def all_belong(candidates,checklist): | |
1845 | """Check whether a list of items ALL appear in a given list of options. |
|
1849 | """Check whether a list of items ALL appear in a given list of options. | |
1846 |
|
1850 | |||
1847 | Returns a single 1 or 0 value.""" |
|
1851 | Returns a single 1 or 0 value.""" | |
1848 |
|
1852 | |||
1849 | return 1-(0 in [x in checklist for x in candidates]) |
|
1853 | return 1-(0 in [x in checklist for x in candidates]) | |
1850 |
|
1854 | |||
1851 | #---------------------------------------------------------------------------- |
|
1855 | #---------------------------------------------------------------------------- | |
1852 | def sort_compare(lst1,lst2,inplace = 1): |
|
1856 | def sort_compare(lst1,lst2,inplace = 1): | |
1853 | """Sort and compare two lists. |
|
1857 | """Sort and compare two lists. | |
1854 |
|
1858 | |||
1855 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1859 | By default it does it in place, thus modifying the lists. Use inplace = 0 | |
1856 | to avoid that (at the cost of temporary copy creation).""" |
|
1860 | to avoid that (at the cost of temporary copy creation).""" | |
1857 | if not inplace: |
|
1861 | if not inplace: | |
1858 | lst1 = lst1[:] |
|
1862 | lst1 = lst1[:] | |
1859 | lst2 = lst2[:] |
|
1863 | lst2 = lst2[:] | |
1860 | lst1.sort(); lst2.sort() |
|
1864 | lst1.sort(); lst2.sort() | |
1861 | return lst1 == lst2 |
|
1865 | return lst1 == lst2 | |
1862 |
|
1866 | |||
1863 | #---------------------------------------------------------------------------- |
|
1867 | #---------------------------------------------------------------------------- | |
1864 | def list2dict(lst): |
|
1868 | def list2dict(lst): | |
1865 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1869 | """Takes a list of (key,value) pairs and turns it into a dict.""" | |
1866 |
|
1870 | |||
1867 | dic = {} |
|
1871 | dic = {} | |
1868 | for k,v in lst: dic[k] = v |
|
1872 | for k,v in lst: dic[k] = v | |
1869 | return dic |
|
1873 | return dic | |
1870 |
|
1874 | |||
1871 | #---------------------------------------------------------------------------- |
|
1875 | #---------------------------------------------------------------------------- | |
1872 | def list2dict2(lst,default=''): |
|
1876 | def list2dict2(lst,default=''): | |
1873 | """Takes a list and turns it into a dict. |
|
1877 | """Takes a list and turns it into a dict. | |
1874 | Much slower than list2dict, but more versatile. This version can take |
|
1878 | Much slower than list2dict, but more versatile. This version can take | |
1875 | lists with sublists of arbitrary length (including sclars).""" |
|
1879 | lists with sublists of arbitrary length (including sclars).""" | |
1876 |
|
1880 | |||
1877 | dic = {} |
|
1881 | dic = {} | |
1878 | for elem in lst: |
|
1882 | for elem in lst: | |
1879 | if type(elem) in (types.ListType,types.TupleType): |
|
1883 | if type(elem) in (types.ListType,types.TupleType): | |
1880 | size = len(elem) |
|
1884 | size = len(elem) | |
1881 | if size == 0: |
|
1885 | if size == 0: | |
1882 | pass |
|
1886 | pass | |
1883 | elif size == 1: |
|
1887 | elif size == 1: | |
1884 | dic[elem] = default |
|
1888 | dic[elem] = default | |
1885 | else: |
|
1889 | else: | |
1886 | k,v = elem[0], elem[1:] |
|
1890 | k,v = elem[0], elem[1:] | |
1887 | if len(v) == 1: v = v[0] |
|
1891 | if len(v) == 1: v = v[0] | |
1888 | dic[k] = v |
|
1892 | dic[k] = v | |
1889 | else: |
|
1893 | else: | |
1890 | dic[elem] = default |
|
1894 | dic[elem] = default | |
1891 | return dic |
|
1895 | return dic | |
1892 |
|
1896 | |||
1893 | #---------------------------------------------------------------------------- |
|
1897 | #---------------------------------------------------------------------------- | |
1894 | def flatten(seq): |
|
1898 | def flatten(seq): | |
1895 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1899 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" | |
1896 |
|
1900 | |||
1897 | return [x for subseq in seq for x in subseq] |
|
1901 | return [x for subseq in seq for x in subseq] | |
1898 |
|
1902 | |||
1899 | #---------------------------------------------------------------------------- |
|
1903 | #---------------------------------------------------------------------------- | |
1900 | def get_slice(seq,start=0,stop=None,step=1): |
|
1904 | def get_slice(seq,start=0,stop=None,step=1): | |
1901 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1905 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" | |
1902 | if stop == None: |
|
1906 | if stop == None: | |
1903 | stop = len(seq) |
|
1907 | stop = len(seq) | |
1904 | item = lambda i: seq[i] |
|
1908 | item = lambda i: seq[i] | |
1905 | return map(item,xrange(start,stop,step)) |
|
1909 | return map(item,xrange(start,stop,step)) | |
1906 |
|
1910 | |||
1907 | #---------------------------------------------------------------------------- |
|
1911 | #---------------------------------------------------------------------------- | |
1908 | def chop(seq,size): |
|
1912 | def chop(seq,size): | |
1909 | """Chop a sequence into chunks of the given size.""" |
|
1913 | """Chop a sequence into chunks of the given size.""" | |
1910 | chunk = lambda i: seq[i:i+size] |
|
1914 | chunk = lambda i: seq[i:i+size] | |
1911 | return map(chunk,xrange(0,len(seq),size)) |
|
1915 | return map(chunk,xrange(0,len(seq),size)) | |
1912 |
|
1916 | |||
1913 | #---------------------------------------------------------------------------- |
|
1917 | #---------------------------------------------------------------------------- | |
1914 | # with is a keyword as of python 2.5, so this function is renamed to withobj |
|
1918 | # with is a keyword as of python 2.5, so this function is renamed to withobj | |
1915 | # from its old 'with' name. |
|
1919 | # from its old 'with' name. | |
1916 | def with_obj(object, **args): |
|
1920 | def with_obj(object, **args): | |
1917 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1921 | """Set multiple attributes for an object, similar to Pascal's with. | |
1918 |
|
1922 | |||
1919 | Example: |
|
1923 | Example: | |
1920 | with_obj(jim, |
|
1924 | with_obj(jim, | |
1921 | born = 1960, |
|
1925 | born = 1960, | |
1922 | haircolour = 'Brown', |
|
1926 | haircolour = 'Brown', | |
1923 | eyecolour = 'Green') |
|
1927 | eyecolour = 'Green') | |
1924 |
|
1928 | |||
1925 | Credit: Greg Ewing, in |
|
1929 | Credit: Greg Ewing, in | |
1926 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. |
|
1930 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. | |
1927 |
|
1931 | |||
1928 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' |
|
1932 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' | |
1929 | has become a keyword for Python 2.5, so we had to rename it.""" |
|
1933 | has become a keyword for Python 2.5, so we had to rename it.""" | |
1930 |
|
1934 | |||
1931 | object.__dict__.update(args) |
|
1935 | object.__dict__.update(args) | |
1932 |
|
1936 | |||
1933 | #---------------------------------------------------------------------------- |
|
1937 | #---------------------------------------------------------------------------- | |
1934 | def setattr_list(obj,alist,nspace = None): |
|
1938 | def setattr_list(obj,alist,nspace = None): | |
1935 | """Set a list of attributes for an object taken from a namespace. |
|
1939 | """Set a list of attributes for an object taken from a namespace. | |
1936 |
|
1940 | |||
1937 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1941 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in | |
1938 | alist with their values taken from nspace, which must be a dict (something |
|
1942 | alist with their values taken from nspace, which must be a dict (something | |
1939 | like locals() will often do) If nspace isn't given, locals() of the |
|
1943 | like locals() will often do) If nspace isn't given, locals() of the | |
1940 | *caller* is used, so in most cases you can omit it. |
|
1944 | *caller* is used, so in most cases you can omit it. | |
1941 |
|
1945 | |||
1942 | Note that alist can be given as a string, which will be automatically |
|
1946 | Note that alist can be given as a string, which will be automatically | |
1943 | split into a list on whitespace. If given as a list, it must be a list of |
|
1947 | split into a list on whitespace. If given as a list, it must be a list of | |
1944 | *strings* (the variable names themselves), not of variables.""" |
|
1948 | *strings* (the variable names themselves), not of variables.""" | |
1945 |
|
1949 | |||
1946 | # this grabs the local variables from the *previous* call frame -- that is |
|
1950 | # this grabs the local variables from the *previous* call frame -- that is | |
1947 | # the locals from the function that called setattr_list(). |
|
1951 | # the locals from the function that called setattr_list(). | |
1948 | # - snipped from weave.inline() |
|
1952 | # - snipped from weave.inline() | |
1949 | if nspace is None: |
|
1953 | if nspace is None: | |
1950 | call_frame = sys._getframe().f_back |
|
1954 | call_frame = sys._getframe().f_back | |
1951 | nspace = call_frame.f_locals |
|
1955 | nspace = call_frame.f_locals | |
1952 |
|
1956 | |||
1953 | if type(alist) in StringTypes: |
|
1957 | if type(alist) in StringTypes: | |
1954 | alist = alist.split() |
|
1958 | alist = alist.split() | |
1955 | for attr in alist: |
|
1959 | for attr in alist: | |
1956 | val = eval(attr,nspace) |
|
1960 | val = eval(attr,nspace) | |
1957 | setattr(obj,attr,val) |
|
1961 | setattr(obj,attr,val) | |
1958 |
|
1962 | |||
1959 | #---------------------------------------------------------------------------- |
|
1963 | #---------------------------------------------------------------------------- | |
1960 | def getattr_list(obj,alist,*args): |
|
1964 | def getattr_list(obj,alist,*args): | |
1961 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1965 | """getattr_list(obj,alist[, default]) -> attribute list. | |
1962 |
|
1966 | |||
1963 | Get a list of named attributes for an object. When a default argument is |
|
1967 | Get a list of named attributes for an object. When a default argument is | |
1964 | given, it is returned when the attribute doesn't exist; without it, an |
|
1968 | given, it is returned when the attribute doesn't exist; without it, an | |
1965 | exception is raised in that case. |
|
1969 | exception is raised in that case. | |
1966 |
|
1970 | |||
1967 | Note that alist can be given as a string, which will be automatically |
|
1971 | Note that alist can be given as a string, which will be automatically | |
1968 | split into a list on whitespace. If given as a list, it must be a list of |
|
1972 | split into a list on whitespace. If given as a list, it must be a list of | |
1969 | *strings* (the variable names themselves), not of variables.""" |
|
1973 | *strings* (the variable names themselves), not of variables.""" | |
1970 |
|
1974 | |||
1971 | if type(alist) in StringTypes: |
|
1975 | if type(alist) in StringTypes: | |
1972 | alist = alist.split() |
|
1976 | alist = alist.split() | |
1973 | if args: |
|
1977 | if args: | |
1974 | if len(args)==1: |
|
1978 | if len(args)==1: | |
1975 | default = args[0] |
|
1979 | default = args[0] | |
1976 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1980 | return map(lambda attr: getattr(obj,attr,default),alist) | |
1977 | else: |
|
1981 | else: | |
1978 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1982 | raise ValueError,'getattr_list() takes only one optional argument' | |
1979 | else: |
|
1983 | else: | |
1980 | return map(lambda attr: getattr(obj,attr),alist) |
|
1984 | return map(lambda attr: getattr(obj,attr),alist) | |
1981 |
|
1985 | |||
1982 | #---------------------------------------------------------------------------- |
|
1986 | #---------------------------------------------------------------------------- | |
1983 | def map_method(method,object_list,*argseq,**kw): |
|
1987 | def map_method(method,object_list,*argseq,**kw): | |
1984 | """map_method(method,object_list,*args,**kw) -> list |
|
1988 | """map_method(method,object_list,*args,**kw) -> list | |
1985 |
|
1989 | |||
1986 | Return a list of the results of applying the methods to the items of the |
|
1990 | Return a list of the results of applying the methods to the items of the | |
1987 | argument sequence(s). If more than one sequence is given, the method is |
|
1991 | argument sequence(s). If more than one sequence is given, the method is | |
1988 | called with an argument list consisting of the corresponding item of each |
|
1992 | called with an argument list consisting of the corresponding item of each | |
1989 | sequence. All sequences must be of the same length. |
|
1993 | sequence. All sequences must be of the same length. | |
1990 |
|
1994 | |||
1991 | Keyword arguments are passed verbatim to all objects called. |
|
1995 | Keyword arguments are passed verbatim to all objects called. | |
1992 |
|
1996 | |||
1993 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1997 | This is Python code, so it's not nearly as fast as the builtin map().""" | |
1994 |
|
1998 | |||
1995 | out_list = [] |
|
1999 | out_list = [] | |
1996 | idx = 0 |
|
2000 | idx = 0 | |
1997 | for object in object_list: |
|
2001 | for object in object_list: | |
1998 | try: |
|
2002 | try: | |
1999 | handler = getattr(object, method) |
|
2003 | handler = getattr(object, method) | |
2000 | except AttributeError: |
|
2004 | except AttributeError: | |
2001 | out_list.append(None) |
|
2005 | out_list.append(None) | |
2002 | else: |
|
2006 | else: | |
2003 | if argseq: |
|
2007 | if argseq: | |
2004 | args = map(lambda lst:lst[idx],argseq) |
|
2008 | args = map(lambda lst:lst[idx],argseq) | |
2005 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
2009 | #print 'ob',object,'hand',handler,'ar',args # dbg | |
2006 | out_list.append(handler(args,**kw)) |
|
2010 | out_list.append(handler(args,**kw)) | |
2007 | else: |
|
2011 | else: | |
2008 | out_list.append(handler(**kw)) |
|
2012 | out_list.append(handler(**kw)) | |
2009 | idx += 1 |
|
2013 | idx += 1 | |
2010 | return out_list |
|
2014 | return out_list | |
2011 |
|
2015 | |||
2012 | #---------------------------------------------------------------------------- |
|
2016 | #---------------------------------------------------------------------------- | |
2013 | def get_class_members(cls): |
|
2017 | def get_class_members(cls): | |
2014 | ret = dir(cls) |
|
2018 | ret = dir(cls) | |
2015 | if hasattr(cls,'__bases__'): |
|
2019 | if hasattr(cls,'__bases__'): | |
2016 | for base in cls.__bases__: |
|
2020 | for base in cls.__bases__: | |
2017 | ret.extend(get_class_members(base)) |
|
2021 | ret.extend(get_class_members(base)) | |
2018 | return ret |
|
2022 | return ret | |
2019 |
|
2023 | |||
2020 | #---------------------------------------------------------------------------- |
|
2024 | #---------------------------------------------------------------------------- | |
2021 | def dir2(obj): |
|
2025 | def dir2(obj): | |
2022 | """dir2(obj) -> list of strings |
|
2026 | """dir2(obj) -> list of strings | |
2023 |
|
2027 | |||
2024 | Extended version of the Python builtin dir(), which does a few extra |
|
2028 | Extended version of the Python builtin dir(), which does a few extra | |
2025 | checks, and supports common objects with unusual internals that confuse |
|
2029 | checks, and supports common objects with unusual internals that confuse | |
2026 | dir(), such as Traits and PyCrust. |
|
2030 | dir(), such as Traits and PyCrust. | |
2027 |
|
2031 | |||
2028 | This version is guaranteed to return only a list of true strings, whereas |
|
2032 | This version is guaranteed to return only a list of true strings, whereas | |
2029 | dir() returns anything that objects inject into themselves, even if they |
|
2033 | dir() returns anything that objects inject into themselves, even if they | |
2030 | are later not really valid for attribute access (many extension libraries |
|
2034 | are later not really valid for attribute access (many extension libraries | |
2031 | have such bugs). |
|
2035 | have such bugs). | |
2032 | """ |
|
2036 | """ | |
2033 |
|
2037 | |||
2034 | # Start building the attribute list via dir(), and then complete it |
|
2038 | # Start building the attribute list via dir(), and then complete it | |
2035 | # with a few extra special-purpose calls. |
|
2039 | # with a few extra special-purpose calls. | |
2036 | words = dir(obj) |
|
2040 | words = dir(obj) | |
2037 |
|
2041 | |||
2038 | if hasattr(obj,'__class__'): |
|
2042 | if hasattr(obj,'__class__'): | |
2039 | words.append('__class__') |
|
2043 | words.append('__class__') | |
2040 | words.extend(get_class_members(obj.__class__)) |
|
2044 | words.extend(get_class_members(obj.__class__)) | |
2041 | #if '__base__' in words: 1/0 |
|
2045 | #if '__base__' in words: 1/0 | |
2042 |
|
2046 | |||
2043 | # Some libraries (such as traits) may introduce duplicates, we want to |
|
2047 | # Some libraries (such as traits) may introduce duplicates, we want to | |
2044 | # track and clean this up if it happens |
|
2048 | # track and clean this up if it happens | |
2045 | may_have_dupes = False |
|
2049 | may_have_dupes = False | |
2046 |
|
2050 | |||
2047 | # this is the 'dir' function for objects with Enthought's traits |
|
2051 | # this is the 'dir' function for objects with Enthought's traits | |
2048 | if hasattr(obj, 'trait_names'): |
|
2052 | if hasattr(obj, 'trait_names'): | |
2049 | try: |
|
2053 | try: | |
2050 | words.extend(obj.trait_names()) |
|
2054 | words.extend(obj.trait_names()) | |
2051 | may_have_dupes = True |
|
2055 | may_have_dupes = True | |
2052 | except TypeError: |
|
2056 | except TypeError: | |
2053 | # This will happen if `obj` is a class and not an instance. |
|
2057 | # This will happen if `obj` is a class and not an instance. | |
2054 | pass |
|
2058 | pass | |
2055 |
|
2059 | |||
2056 | # Support for PyCrust-style _getAttributeNames magic method. |
|
2060 | # Support for PyCrust-style _getAttributeNames magic method. | |
2057 | if hasattr(obj, '_getAttributeNames'): |
|
2061 | if hasattr(obj, '_getAttributeNames'): | |
2058 | try: |
|
2062 | try: | |
2059 | words.extend(obj._getAttributeNames()) |
|
2063 | words.extend(obj._getAttributeNames()) | |
2060 | may_have_dupes = True |
|
2064 | may_have_dupes = True | |
2061 | except TypeError: |
|
2065 | except TypeError: | |
2062 | # `obj` is a class and not an instance. Ignore |
|
2066 | # `obj` is a class and not an instance. Ignore | |
2063 | # this error. |
|
2067 | # this error. | |
2064 | pass |
|
2068 | pass | |
2065 |
|
2069 | |||
2066 | if may_have_dupes: |
|
2070 | if may_have_dupes: | |
2067 | # eliminate possible duplicates, as some traits may also |
|
2071 | # eliminate possible duplicates, as some traits may also | |
2068 | # appear as normal attributes in the dir() call. |
|
2072 | # appear as normal attributes in the dir() call. | |
2069 | words = list(set(words)) |
|
2073 | words = list(set(words)) | |
2070 | words.sort() |
|
2074 | words.sort() | |
2071 |
|
2075 | |||
2072 | # filter out non-string attributes which may be stuffed by dir() calls |
|
2076 | # filter out non-string attributes which may be stuffed by dir() calls | |
2073 | # and poor coding in third-party modules |
|
2077 | # and poor coding in third-party modules | |
2074 | return [w for w in words if isinstance(w, basestring)] |
|
2078 | return [w for w in words if isinstance(w, basestring)] | |
2075 |
|
2079 | |||
2076 | #---------------------------------------------------------------------------- |
|
2080 | #---------------------------------------------------------------------------- | |
2077 | def import_fail_info(mod_name,fns=None): |
|
2081 | def import_fail_info(mod_name,fns=None): | |
2078 | """Inform load failure for a module.""" |
|
2082 | """Inform load failure for a module.""" | |
2079 |
|
2083 | |||
2080 | if fns == None: |
|
2084 | if fns == None: | |
2081 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
2085 | warn("Loading of %s failed.\n" % (mod_name,)) | |
2082 | else: |
|
2086 | else: | |
2083 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
2087 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) | |
2084 |
|
2088 | |||
2085 | #---------------------------------------------------------------------------- |
|
2089 | #---------------------------------------------------------------------------- | |
2086 | # Proposed popitem() extension, written as a method |
|
2090 | # Proposed popitem() extension, written as a method | |
2087 |
|
2091 | |||
2088 |
|
2092 | |||
2089 | class NotGiven: pass |
|
2093 | class NotGiven: pass | |
2090 |
|
2094 | |||
2091 | def popkey(dct,key,default=NotGiven): |
|
2095 | def popkey(dct,key,default=NotGiven): | |
2092 | """Return dct[key] and delete dct[key]. |
|
2096 | """Return dct[key] and delete dct[key]. | |
2093 |
|
2097 | |||
2094 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
2098 | If default is given, return it if dct[key] doesn't exist, otherwise raise | |
2095 | KeyError. """ |
|
2099 | KeyError. """ | |
2096 |
|
2100 | |||
2097 | try: |
|
2101 | try: | |
2098 | val = dct[key] |
|
2102 | val = dct[key] | |
2099 | except KeyError: |
|
2103 | except KeyError: | |
2100 | if default is NotGiven: |
|
2104 | if default is NotGiven: | |
2101 | raise |
|
2105 | raise | |
2102 | else: |
|
2106 | else: | |
2103 | return default |
|
2107 | return default | |
2104 | else: |
|
2108 | else: | |
2105 | del dct[key] |
|
2109 | del dct[key] | |
2106 | return val |
|
2110 | return val | |
2107 |
|
2111 | |||
2108 | def wrap_deprecated(func, suggest = '<nothing>'): |
|
2112 | def wrap_deprecated(func, suggest = '<nothing>'): | |
2109 | def newFunc(*args, **kwargs): |
|
2113 | def newFunc(*args, **kwargs): | |
2110 | warnings.warn("Call to deprecated function %s, use %s instead" % |
|
2114 | warnings.warn("Call to deprecated function %s, use %s instead" % | |
2111 | ( func.__name__, suggest), |
|
2115 | ( func.__name__, suggest), | |
2112 | category=DeprecationWarning, |
|
2116 | category=DeprecationWarning, | |
2113 | stacklevel = 2) |
|
2117 | stacklevel = 2) | |
2114 | return func(*args, **kwargs) |
|
2118 | return func(*args, **kwargs) | |
2115 | return newFunc |
|
2119 | return newFunc | |
2116 |
|
2120 | |||
2117 |
|
2121 | |||
2118 | def _num_cpus_unix(): |
|
2122 | def _num_cpus_unix(): | |
2119 | """Return the number of active CPUs on a Unix system.""" |
|
2123 | """Return the number of active CPUs on a Unix system.""" | |
2120 | return os.sysconf("SC_NPROCESSORS_ONLN") |
|
2124 | return os.sysconf("SC_NPROCESSORS_ONLN") | |
2121 |
|
2125 | |||
2122 |
|
2126 | |||
2123 | def _num_cpus_darwin(): |
|
2127 | def _num_cpus_darwin(): | |
2124 | """Return the number of active CPUs on a Darwin system.""" |
|
2128 | """Return the number of active CPUs on a Darwin system.""" | |
2125 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) |
|
2129 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) | |
2126 | return p.stdout.read() |
|
2130 | return p.stdout.read() | |
2127 |
|
2131 | |||
2128 |
|
2132 | |||
2129 | def _num_cpus_windows(): |
|
2133 | def _num_cpus_windows(): | |
2130 | """Return the number of active CPUs on a Windows system.""" |
|
2134 | """Return the number of active CPUs on a Windows system.""" | |
2131 | return os.environ.get("NUMBER_OF_PROCESSORS") |
|
2135 | return os.environ.get("NUMBER_OF_PROCESSORS") | |
2132 |
|
2136 | |||
2133 |
|
2137 | |||
2134 | def num_cpus(): |
|
2138 | def num_cpus(): | |
2135 | """Return the effective number of CPUs in the system as an integer. |
|
2139 | """Return the effective number of CPUs in the system as an integer. | |
2136 |
|
2140 | |||
2137 | This cross-platform function makes an attempt at finding the total number of |
|
2141 | This cross-platform function makes an attempt at finding the total number of | |
2138 | available CPUs in the system, as returned by various underlying system and |
|
2142 | available CPUs in the system, as returned by various underlying system and | |
2139 | python calls. |
|
2143 | python calls. | |
2140 |
|
2144 | |||
2141 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
|
2145 | If it can't find a sensible answer, it returns 1 (though an error *may* make | |
2142 | it return a large positive number that's actually incorrect). |
|
2146 | it return a large positive number that's actually incorrect). | |
2143 | """ |
|
2147 | """ | |
2144 |
|
2148 | |||
2145 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
|
2149 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) | |
2146 | # for the names of the keys we needed to look up for this function. This |
|
2150 | # for the names of the keys we needed to look up for this function. This | |
2147 | # code was inspired by their equivalent function. |
|
2151 | # code was inspired by their equivalent function. | |
2148 |
|
2152 | |||
2149 | ncpufuncs = {'Linux':_num_cpus_unix, |
|
2153 | ncpufuncs = {'Linux':_num_cpus_unix, | |
2150 | 'Darwin':_num_cpus_darwin, |
|
2154 | 'Darwin':_num_cpus_darwin, | |
2151 | 'Windows':_num_cpus_windows, |
|
2155 | 'Windows':_num_cpus_windows, | |
2152 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' |
|
2156 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' | |
2153 | # See http://bugs.python.org/issue1082 for details. |
|
2157 | # See http://bugs.python.org/issue1082 for details. | |
2154 | 'Microsoft':_num_cpus_windows, |
|
2158 | 'Microsoft':_num_cpus_windows, | |
2155 | } |
|
2159 | } | |
2156 |
|
2160 | |||
2157 | ncpufunc = ncpufuncs.get(platform.system(), |
|
2161 | ncpufunc = ncpufuncs.get(platform.system(), | |
2158 | # default to unix version (Solaris, AIX, etc) |
|
2162 | # default to unix version (Solaris, AIX, etc) | |
2159 | _num_cpus_unix) |
|
2163 | _num_cpus_unix) | |
2160 |
|
2164 | |||
2161 | try: |
|
2165 | try: | |
2162 | ncpus = max(1,int(ncpufunc())) |
|
2166 | ncpus = max(1,int(ncpufunc())) | |
2163 | except: |
|
2167 | except: | |
2164 | ncpus = 1 |
|
2168 | ncpus = 1 | |
2165 | return ncpus |
|
2169 | return ncpus | |
2166 |
|
2170 | |||
2167 | #*************************** end of file <genutils.py> ********************** |
|
2171 | #*************************** end of file <genutils.py> ********************** |
@@ -1,2695 +1,2696 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | IPython -- An enhanced Interactive Python |
|
3 | IPython -- An enhanced Interactive Python | |
4 |
|
4 | |||
5 | Requires Python 2.3 or newer. |
|
5 | Requires Python 2.3 or newer. | |
6 |
|
6 | |||
7 | This file contains all the classes and helper functions specific to IPython. |
|
7 | This file contains all the classes and helper functions specific to IPython. | |
8 |
|
8 | |||
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | #***************************************************************************** |
|
11 | #***************************************************************************** | |
12 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
12 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
13 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
13 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
16 | # the file COPYING, distributed as part of this software. |
|
16 | # the file COPYING, distributed as part of this software. | |
17 | # |
|
17 | # | |
18 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
18 | # Note: this code originally subclassed code.InteractiveConsole from the | |
19 | # Python standard library. Over time, all of that class has been copied |
|
19 | # Python standard library. Over time, all of that class has been copied | |
20 | # verbatim here for modifications which could not be accomplished by |
|
20 | # verbatim here for modifications which could not be accomplished by | |
21 | # subclassing. At this point, there are no dependencies at all on the code |
|
21 | # subclassing. At this point, there are no dependencies at all on the code | |
22 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
22 | # module anymore (it is not even imported). The Python License (sec. 2) | |
23 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
23 | # allows for this, but it's always nice to acknowledge credit where credit is | |
24 | # due. |
|
24 | # due. | |
25 | #***************************************************************************** |
|
25 | #***************************************************************************** | |
26 |
|
26 | |||
27 | #**************************************************************************** |
|
27 | #**************************************************************************** | |
28 | # Modules and globals |
|
28 | # Modules and globals | |
29 |
|
29 | |||
30 | from IPython import Release |
|
30 | from IPython import Release | |
31 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
31 | __author__ = '%s <%s>\n%s <%s>' % \ | |
32 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
32 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) | |
33 | __license__ = Release.license |
|
33 | __license__ = Release.license | |
34 | __version__ = Release.version |
|
34 | __version__ = Release.version | |
35 |
|
35 | |||
36 | # Python standard modules |
|
36 | # Python standard modules | |
37 | import __main__ |
|
37 | import __main__ | |
38 | import __builtin__ |
|
38 | import __builtin__ | |
39 | import StringIO |
|
39 | import StringIO | |
40 | import bdb |
|
40 | import bdb | |
41 | import cPickle as pickle |
|
41 | import cPickle as pickle | |
42 | import codeop |
|
42 | import codeop | |
43 | import exceptions |
|
43 | import exceptions | |
44 | import glob |
|
44 | import glob | |
45 | import inspect |
|
45 | import inspect | |
46 | import keyword |
|
46 | import keyword | |
47 | import new |
|
47 | import new | |
48 | import os |
|
48 | import os | |
49 | import pydoc |
|
49 | import pydoc | |
50 | import re |
|
50 | import re | |
51 | import shutil |
|
51 | import shutil | |
52 | import string |
|
52 | import string | |
53 | import sys |
|
53 | import sys | |
54 | import tempfile |
|
54 | import tempfile | |
55 | import traceback |
|
55 | import traceback | |
56 | import types |
|
56 | import types | |
57 | import warnings |
|
57 | import warnings | |
58 | warnings.filterwarnings('ignore', r'.*sets module*') |
|
58 | warnings.filterwarnings('ignore', r'.*sets module*') | |
59 | from sets import Set |
|
59 | from sets import Set | |
60 | from pprint import pprint, pformat |
|
60 | from pprint import pprint, pformat | |
61 |
|
61 | |||
62 | # IPython's own modules |
|
62 | # IPython's own modules | |
63 | #import IPython |
|
63 | #import IPython | |
64 | from IPython import Debugger,OInspect,PyColorize,ultraTB |
|
64 | from IPython import Debugger,OInspect,PyColorize,ultraTB | |
65 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
65 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names | |
66 | from IPython.Extensions import pickleshare |
|
66 | from IPython.Extensions import pickleshare | |
67 | from IPython.FakeModule import FakeModule |
|
67 | from IPython.FakeModule import FakeModule | |
68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns | |
69 | from IPython.Logger import Logger |
|
69 | from IPython.Logger import Logger | |
70 | from IPython.Magic import Magic |
|
70 | from IPython.Magic import Magic | |
71 | from IPython.Prompts import CachedOutput |
|
71 | from IPython.Prompts import CachedOutput | |
72 | from IPython.ipstruct import Struct |
|
72 | from IPython.ipstruct import Struct | |
73 | from IPython.background_jobs import BackgroundJobManager |
|
73 | from IPython.background_jobs import BackgroundJobManager | |
74 | from IPython.usage import cmd_line_usage,interactive_usage |
|
74 | from IPython.usage import cmd_line_usage,interactive_usage | |
75 | from IPython.genutils import * |
|
75 | from IPython.genutils import * | |
76 | from IPython.strdispatch import StrDispatch |
|
76 | from IPython.strdispatch import StrDispatch | |
77 | import IPython.ipapi |
|
77 | import IPython.ipapi | |
78 | import IPython.history |
|
78 | import IPython.history | |
79 | import IPython.prefilter as prefilter |
|
79 | import IPython.prefilter as prefilter | |
80 | import IPython.shadowns |
|
80 | import IPython.shadowns | |
81 | # Globals |
|
81 | # Globals | |
82 |
|
82 | |||
83 | # store the builtin raw_input globally, and use this always, in case user code |
|
83 | # store the builtin raw_input globally, and use this always, in case user code | |
84 | # overwrites it (like wx.py.PyShell does) |
|
84 | # overwrites it (like wx.py.PyShell does) | |
85 | raw_input_original = raw_input |
|
85 | raw_input_original = raw_input | |
86 |
|
86 | |||
87 | # compiled regexps for autoindent management |
|
87 | # compiled regexps for autoindent management | |
88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | #**************************************************************************** |
|
91 | #**************************************************************************** | |
92 | # Some utility function definitions |
|
92 | # Some utility function definitions | |
93 |
|
93 | |||
94 | ini_spaces_re = re.compile(r'^(\s+)') |
|
94 | ini_spaces_re = re.compile(r'^(\s+)') | |
95 |
|
95 | |||
96 | def num_ini_spaces(strng): |
|
96 | def num_ini_spaces(strng): | |
97 | """Return the number of initial spaces in a string""" |
|
97 | """Return the number of initial spaces in a string""" | |
98 |
|
98 | |||
99 | ini_spaces = ini_spaces_re.match(strng) |
|
99 | ini_spaces = ini_spaces_re.match(strng) | |
100 | if ini_spaces: |
|
100 | if ini_spaces: | |
101 | return ini_spaces.end() |
|
101 | return ini_spaces.end() | |
102 | else: |
|
102 | else: | |
103 | return 0 |
|
103 | return 0 | |
104 |
|
104 | |||
105 | def softspace(file, newvalue): |
|
105 | def softspace(file, newvalue): | |
106 | """Copied from code.py, to remove the dependency""" |
|
106 | """Copied from code.py, to remove the dependency""" | |
107 |
|
107 | |||
108 | oldvalue = 0 |
|
108 | oldvalue = 0 | |
109 | try: |
|
109 | try: | |
110 | oldvalue = file.softspace |
|
110 | oldvalue = file.softspace | |
111 | except AttributeError: |
|
111 | except AttributeError: | |
112 | pass |
|
112 | pass | |
113 | try: |
|
113 | try: | |
114 | file.softspace = newvalue |
|
114 | file.softspace = newvalue | |
115 | except (AttributeError, TypeError): |
|
115 | except (AttributeError, TypeError): | |
116 | # "attribute-less object" or "read-only attributes" |
|
116 | # "attribute-less object" or "read-only attributes" | |
117 | pass |
|
117 | pass | |
118 | return oldvalue |
|
118 | return oldvalue | |
119 |
|
119 | |||
120 |
|
120 | |||
121 | #**************************************************************************** |
|
121 | #**************************************************************************** | |
122 | # Local use exceptions |
|
122 | # Local use exceptions | |
123 | class SpaceInInput(exceptions.Exception): pass |
|
123 | class SpaceInInput(exceptions.Exception): pass | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | #**************************************************************************** |
|
126 | #**************************************************************************** | |
127 | # Local use classes |
|
127 | # Local use classes | |
128 | class Bunch: pass |
|
128 | class Bunch: pass | |
129 |
|
129 | |||
130 | class Undefined: pass |
|
130 | class Undefined: pass | |
131 |
|
131 | |||
132 | class Quitter(object): |
|
132 | class Quitter(object): | |
133 | """Simple class to handle exit, similar to Python 2.5's. |
|
133 | """Simple class to handle exit, similar to Python 2.5's. | |
134 |
|
134 | |||
135 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
135 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 | |
136 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
136 | doesn't do (obviously, since it doesn't know about ipython).""" | |
137 |
|
137 | |||
138 | def __init__(self,shell,name): |
|
138 | def __init__(self,shell,name): | |
139 | self.shell = shell |
|
139 | self.shell = shell | |
140 | self.name = name |
|
140 | self.name = name | |
141 |
|
141 | |||
142 | def __repr__(self): |
|
142 | def __repr__(self): | |
143 | return 'Type %s() to exit.' % self.name |
|
143 | return 'Type %s() to exit.' % self.name | |
144 | __str__ = __repr__ |
|
144 | __str__ = __repr__ | |
145 |
|
145 | |||
146 | def __call__(self): |
|
146 | def __call__(self): | |
147 | self.shell.exit() |
|
147 | self.shell.exit() | |
148 |
|
148 | |||
149 | class InputList(list): |
|
149 | class InputList(list): | |
150 | """Class to store user input. |
|
150 | """Class to store user input. | |
151 |
|
151 | |||
152 | It's basically a list, but slices return a string instead of a list, thus |
|
152 | It's basically a list, but slices return a string instead of a list, thus | |
153 | allowing things like (assuming 'In' is an instance): |
|
153 | allowing things like (assuming 'In' is an instance): | |
154 |
|
154 | |||
155 | exec In[4:7] |
|
155 | exec In[4:7] | |
156 |
|
156 | |||
157 | or |
|
157 | or | |
158 |
|
158 | |||
159 | exec In[5:9] + In[14] + In[21:25]""" |
|
159 | exec In[5:9] + In[14] + In[21:25]""" | |
160 |
|
160 | |||
161 | def __getslice__(self,i,j): |
|
161 | def __getslice__(self,i,j): | |
162 | return ''.join(list.__getslice__(self,i,j)) |
|
162 | return ''.join(list.__getslice__(self,i,j)) | |
163 |
|
163 | |||
164 | class SyntaxTB(ultraTB.ListTB): |
|
164 | class SyntaxTB(ultraTB.ListTB): | |
165 | """Extension which holds some state: the last exception value""" |
|
165 | """Extension which holds some state: the last exception value""" | |
166 |
|
166 | |||
167 | def __init__(self,color_scheme = 'NoColor'): |
|
167 | def __init__(self,color_scheme = 'NoColor'): | |
168 | ultraTB.ListTB.__init__(self,color_scheme) |
|
168 | ultraTB.ListTB.__init__(self,color_scheme) | |
169 | self.last_syntax_error = None |
|
169 | self.last_syntax_error = None | |
170 |
|
170 | |||
171 | def __call__(self, etype, value, elist): |
|
171 | def __call__(self, etype, value, elist): | |
172 | self.last_syntax_error = value |
|
172 | self.last_syntax_error = value | |
173 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
173 | ultraTB.ListTB.__call__(self,etype,value,elist) | |
174 |
|
174 | |||
175 | def clear_err_state(self): |
|
175 | def clear_err_state(self): | |
176 | """Return the current error state and clear it""" |
|
176 | """Return the current error state and clear it""" | |
177 | e = self.last_syntax_error |
|
177 | e = self.last_syntax_error | |
178 | self.last_syntax_error = None |
|
178 | self.last_syntax_error = None | |
179 | return e |
|
179 | return e | |
180 |
|
180 | |||
181 | #**************************************************************************** |
|
181 | #**************************************************************************** | |
182 | # Main IPython class |
|
182 | # Main IPython class | |
183 |
|
183 | |||
184 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
184 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so | |
185 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
185 | # until a full rewrite is made. I've cleaned all cross-class uses of | |
186 | # attributes and methods, but too much user code out there relies on the |
|
186 | # attributes and methods, but too much user code out there relies on the | |
187 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
187 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. | |
188 | # |
|
188 | # | |
189 | # But at least now, all the pieces have been separated and we could, in |
|
189 | # But at least now, all the pieces have been separated and we could, in | |
190 | # principle, stop using the mixin. This will ease the transition to the |
|
190 | # principle, stop using the mixin. This will ease the transition to the | |
191 | # chainsaw branch. |
|
191 | # chainsaw branch. | |
192 |
|
192 | |||
193 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
193 | # For reference, the following is the list of 'self.foo' uses in the Magic | |
194 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
194 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython | |
195 | # class, to prevent clashes. |
|
195 | # class, to prevent clashes. | |
196 |
|
196 | |||
197 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
197 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', | |
198 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
198 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', | |
199 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
199 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', | |
200 | # 'self.value'] |
|
200 | # 'self.value'] | |
201 |
|
201 | |||
202 | class InteractiveShell(object,Magic): |
|
202 | class InteractiveShell(object,Magic): | |
203 | """An enhanced console for Python.""" |
|
203 | """An enhanced console for Python.""" | |
204 |
|
204 | |||
205 | # class attribute to indicate whether the class supports threads or not. |
|
205 | # class attribute to indicate whether the class supports threads or not. | |
206 | # Subclasses with thread support should override this as needed. |
|
206 | # Subclasses with thread support should override this as needed. | |
207 | isthreaded = False |
|
207 | isthreaded = False | |
208 |
|
208 | |||
209 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
209 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
210 | user_ns=None,user_global_ns=None,banner2='', |
|
210 | user_ns=None,user_global_ns=None,banner2='', | |
211 | custom_exceptions=((),None),embedded=False): |
|
211 | custom_exceptions=((),None),embedded=False): | |
212 |
|
212 | |||
213 | # log system |
|
213 | # log system | |
214 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
214 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') | |
215 |
|
215 | |||
216 | # Job manager (for jobs run as background threads) |
|
216 | # Job manager (for jobs run as background threads) | |
217 | self.jobs = BackgroundJobManager() |
|
217 | self.jobs = BackgroundJobManager() | |
218 |
|
218 | |||
219 | # Store the actual shell's name |
|
219 | # Store the actual shell's name | |
220 | self.name = name |
|
220 | self.name = name | |
221 | self.more = False |
|
221 | self.more = False | |
222 |
|
222 | |||
223 | # We need to know whether the instance is meant for embedding, since |
|
223 | # We need to know whether the instance is meant for embedding, since | |
224 | # global/local namespaces need to be handled differently in that case |
|
224 | # global/local namespaces need to be handled differently in that case | |
225 | self.embedded = embedded |
|
225 | self.embedded = embedded | |
226 | if embedded: |
|
226 | if embedded: | |
227 | # Control variable so users can, from within the embedded instance, |
|
227 | # Control variable so users can, from within the embedded instance, | |
228 | # permanently deactivate it. |
|
228 | # permanently deactivate it. | |
229 | self.embedded_active = True |
|
229 | self.embedded_active = True | |
230 |
|
230 | |||
231 | # command compiler |
|
231 | # command compiler | |
232 | self.compile = codeop.CommandCompiler() |
|
232 | self.compile = codeop.CommandCompiler() | |
233 |
|
233 | |||
234 | # User input buffer |
|
234 | # User input buffer | |
235 | self.buffer = [] |
|
235 | self.buffer = [] | |
236 |
|
236 | |||
237 | # Default name given in compilation of code |
|
237 | # Default name given in compilation of code | |
238 | self.filename = '<ipython console>' |
|
238 | self.filename = '<ipython console>' | |
239 |
|
239 | |||
240 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
240 | # Install our own quitter instead of the builtins. For python2.3-2.4, | |
241 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
241 | # this brings in behavior like 2.5, and for 2.5 it's identical. | |
242 | __builtin__.exit = Quitter(self,'exit') |
|
242 | __builtin__.exit = Quitter(self,'exit') | |
243 | __builtin__.quit = Quitter(self,'quit') |
|
243 | __builtin__.quit = Quitter(self,'quit') | |
244 |
|
244 | |||
245 | # Make an empty namespace, which extension writers can rely on both |
|
245 | # Make an empty namespace, which extension writers can rely on both | |
246 | # existing and NEVER being used by ipython itself. This gives them a |
|
246 | # existing and NEVER being used by ipython itself. This gives them a | |
247 | # convenient location for storing additional information and state |
|
247 | # convenient location for storing additional information and state | |
248 | # their extensions may require, without fear of collisions with other |
|
248 | # their extensions may require, without fear of collisions with other | |
249 | # ipython names that may develop later. |
|
249 | # ipython names that may develop later. | |
250 | self.meta = Struct() |
|
250 | self.meta = Struct() | |
251 |
|
251 | |||
252 | # Create the namespace where the user will operate. user_ns is |
|
252 | # Create the namespace where the user will operate. user_ns is | |
253 | # normally the only one used, and it is passed to the exec calls as |
|
253 | # normally the only one used, and it is passed to the exec calls as | |
254 | # the locals argument. But we do carry a user_global_ns namespace |
|
254 | # the locals argument. But we do carry a user_global_ns namespace | |
255 | # given as the exec 'globals' argument, This is useful in embedding |
|
255 | # given as the exec 'globals' argument, This is useful in embedding | |
256 | # situations where the ipython shell opens in a context where the |
|
256 | # situations where the ipython shell opens in a context where the | |
257 | # distinction between locals and globals is meaningful. For |
|
257 | # distinction between locals and globals is meaningful. For | |
258 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
258 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
259 |
|
259 | |||
260 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
260 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
261 | # level as a dict instead of a module. This is a manual fix, but I |
|
261 | # level as a dict instead of a module. This is a manual fix, but I | |
262 | # should really track down where the problem is coming from. Alex |
|
262 | # should really track down where the problem is coming from. Alex | |
263 | # Schmolck reported this problem first. |
|
263 | # Schmolck reported this problem first. | |
264 |
|
264 | |||
265 | # A useful post by Alex Martelli on this topic: |
|
265 | # A useful post by Alex Martelli on this topic: | |
266 | # Re: inconsistent value from __builtins__ |
|
266 | # Re: inconsistent value from __builtins__ | |
267 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
267 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
268 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
268 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
269 | # Gruppen: comp.lang.python |
|
269 | # Gruppen: comp.lang.python | |
270 |
|
270 | |||
271 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
271 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
272 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
272 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
273 | # > <type 'dict'> |
|
273 | # > <type 'dict'> | |
274 | # > >>> print type(__builtins__) |
|
274 | # > >>> print type(__builtins__) | |
275 | # > <type 'module'> |
|
275 | # > <type 'module'> | |
276 | # > Is this difference in return value intentional? |
|
276 | # > Is this difference in return value intentional? | |
277 |
|
277 | |||
278 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
278 | # Well, it's documented that '__builtins__' can be either a dictionary | |
279 | # or a module, and it's been that way for a long time. Whether it's |
|
279 | # or a module, and it's been that way for a long time. Whether it's | |
280 | # intentional (or sensible), I don't know. In any case, the idea is |
|
280 | # intentional (or sensible), I don't know. In any case, the idea is | |
281 | # that if you need to access the built-in namespace directly, you |
|
281 | # that if you need to access the built-in namespace directly, you | |
282 | # should start with "import __builtin__" (note, no 's') which will |
|
282 | # should start with "import __builtin__" (note, no 's') which will | |
283 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
283 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
284 |
|
284 | |||
285 | # These routines return properly built dicts as needed by the rest of |
|
285 | # These routines return properly built dicts as needed by the rest of | |
286 | # the code, and can also be used by extension writers to generate |
|
286 | # the code, and can also be used by extension writers to generate | |
287 | # properly initialized namespaces. |
|
287 | # properly initialized namespaces. | |
288 | user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, |
|
288 | user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, | |
289 | user_global_ns) |
|
289 | user_global_ns) | |
290 |
|
290 | |||
291 | # Assign namespaces |
|
291 | # Assign namespaces | |
292 | # This is the namespace where all normal user variables live |
|
292 | # This is the namespace where all normal user variables live | |
293 | self.user_ns = user_ns |
|
293 | self.user_ns = user_ns | |
294 | self.user_global_ns = user_global_ns |
|
294 | self.user_global_ns = user_global_ns | |
295 | # A namespace to keep track of internal data structures to prevent |
|
295 | # A namespace to keep track of internal data structures to prevent | |
296 | # them from cluttering user-visible stuff. Will be updated later |
|
296 | # them from cluttering user-visible stuff. Will be updated later | |
297 | self.internal_ns = {} |
|
297 | self.internal_ns = {} | |
298 |
|
298 | |||
299 | # Namespace of system aliases. Each entry in the alias |
|
299 | # Namespace of system aliases. Each entry in the alias | |
300 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
300 | # table must be a 2-tuple of the form (N,name), where N is the number | |
301 | # of positional arguments of the alias. |
|
301 | # of positional arguments of the alias. | |
302 | self.alias_table = {} |
|
302 | self.alias_table = {} | |
303 |
|
303 | |||
304 | # A table holding all the namespaces IPython deals with, so that |
|
304 | # A table holding all the namespaces IPython deals with, so that | |
305 | # introspection facilities can search easily. |
|
305 | # introspection facilities can search easily. | |
306 | self.ns_table = {'user':user_ns, |
|
306 | self.ns_table = {'user':user_ns, | |
307 | 'user_global':user_global_ns, |
|
307 | 'user_global':user_global_ns, | |
308 | 'alias':self.alias_table, |
|
308 | 'alias':self.alias_table, | |
309 | 'internal':self.internal_ns, |
|
309 | 'internal':self.internal_ns, | |
310 | 'builtin':__builtin__.__dict__ |
|
310 | 'builtin':__builtin__.__dict__ | |
311 | } |
|
311 | } | |
312 | # The user namespace MUST have a pointer to the shell itself. |
|
312 | # The user namespace MUST have a pointer to the shell itself. | |
313 | self.user_ns[name] = self |
|
313 | self.user_ns[name] = self | |
314 |
|
314 | |||
315 | # We need to insert into sys.modules something that looks like a |
|
315 | # We need to insert into sys.modules something that looks like a | |
316 | # module but which accesses the IPython namespace, for shelve and |
|
316 | # module but which accesses the IPython namespace, for shelve and | |
317 | # pickle to work interactively. Normally they rely on getting |
|
317 | # pickle to work interactively. Normally they rely on getting | |
318 | # everything out of __main__, but for embedding purposes each IPython |
|
318 | # everything out of __main__, but for embedding purposes each IPython | |
319 | # instance has its own private namespace, so we can't go shoving |
|
319 | # instance has its own private namespace, so we can't go shoving | |
320 | # everything into __main__. |
|
320 | # everything into __main__. | |
321 |
|
321 | |||
322 | # note, however, that we should only do this for non-embedded |
|
322 | # note, however, that we should only do this for non-embedded | |
323 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
323 | # ipythons, which really mimic the __main__.__dict__ with their own | |
324 | # namespace. Embedded instances, on the other hand, should not do |
|
324 | # namespace. Embedded instances, on the other hand, should not do | |
325 | # this because they need to manage the user local/global namespaces |
|
325 | # this because they need to manage the user local/global namespaces | |
326 | # only, but they live within a 'normal' __main__ (meaning, they |
|
326 | # only, but they live within a 'normal' __main__ (meaning, they | |
327 | # shouldn't overtake the execution environment of the script they're |
|
327 | # shouldn't overtake the execution environment of the script they're | |
328 | # embedded in). |
|
328 | # embedded in). | |
329 |
|
329 | |||
330 | if not embedded: |
|
330 | if not embedded: | |
331 | try: |
|
331 | try: | |
332 | main_name = self.user_ns['__name__'] |
|
332 | main_name = self.user_ns['__name__'] | |
333 | except KeyError: |
|
333 | except KeyError: | |
334 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
334 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' | |
335 | else: |
|
335 | else: | |
336 | #print "pickle hack in place" # dbg |
|
336 | #print "pickle hack in place" # dbg | |
337 | #print 'main_name:',main_name # dbg |
|
337 | #print 'main_name:',main_name # dbg | |
338 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
338 | sys.modules[main_name] = FakeModule(self.user_ns) | |
339 |
|
339 | |||
340 | # Now that FakeModule produces a real module, we've run into a nasty |
|
340 | # Now that FakeModule produces a real module, we've run into a nasty | |
341 | # problem: after script execution (via %run), the module where the user |
|
341 | # problem: after script execution (via %run), the module where the user | |
342 | # code ran is deleted. Now that this object is a true module (needed |
|
342 | # code ran is deleted. Now that this object is a true module (needed | |
343 | # so docetst and other tools work correctly), the Python module |
|
343 | # so docetst and other tools work correctly), the Python module | |
344 | # teardown mechanism runs over it, and sets to None every variable |
|
344 | # teardown mechanism runs over it, and sets to None every variable | |
345 | # present in that module. This means that later calls to functions |
|
345 | # present in that module. This means that later calls to functions | |
346 | # defined in the script (which have become interactively visible after |
|
346 | # defined in the script (which have become interactively visible after | |
347 | # script exit) fail, because they hold references to objects that have |
|
347 | # script exit) fail, because they hold references to objects that have | |
348 | # become overwritten into None. The only solution I see right now is |
|
348 | # become overwritten into None. The only solution I see right now is | |
349 | # to protect every FakeModule used by %run by holding an internal |
|
349 | # to protect every FakeModule used by %run by holding an internal | |
350 | # reference to it. This private list will be used for that. The |
|
350 | # reference to it. This private list will be used for that. The | |
351 | # %reset command will flush it as well. |
|
351 | # %reset command will flush it as well. | |
352 | self._user_main_modules = [] |
|
352 | self._user_main_modules = [] | |
353 |
|
353 | |||
354 | # List of input with multi-line handling. |
|
354 | # List of input with multi-line handling. | |
355 | # Fill its zero entry, user counter starts at 1 |
|
355 | # Fill its zero entry, user counter starts at 1 | |
356 | self.input_hist = InputList(['\n']) |
|
356 | self.input_hist = InputList(['\n']) | |
357 | # This one will hold the 'raw' input history, without any |
|
357 | # This one will hold the 'raw' input history, without any | |
358 | # pre-processing. This will allow users to retrieve the input just as |
|
358 | # pre-processing. This will allow users to retrieve the input just as | |
359 | # it was exactly typed in by the user, with %hist -r. |
|
359 | # it was exactly typed in by the user, with %hist -r. | |
360 | self.input_hist_raw = InputList(['\n']) |
|
360 | self.input_hist_raw = InputList(['\n']) | |
361 |
|
361 | |||
362 | # list of visited directories |
|
362 | # list of visited directories | |
363 | try: |
|
363 | try: | |
364 | self.dir_hist = [os.getcwd()] |
|
364 | self.dir_hist = [os.getcwd()] | |
365 | except OSError: |
|
365 | except OSError: | |
366 | self.dir_hist = [] |
|
366 | self.dir_hist = [] | |
367 |
|
367 | |||
368 | # dict of output history |
|
368 | # dict of output history | |
369 | self.output_hist = {} |
|
369 | self.output_hist = {} | |
370 |
|
370 | |||
371 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
371 | # Get system encoding at startup time. Certain terminals (like Emacs | |
372 | # under Win32 have it set to None, and we need to have a known valid |
|
372 | # under Win32 have it set to None, and we need to have a known valid | |
373 | # encoding to use in the raw_input() method |
|
373 | # encoding to use in the raw_input() method | |
374 | try: |
|
374 | try: | |
375 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
375 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
376 | except AttributeError: |
|
376 | except AttributeError: | |
377 | self.stdin_encoding = 'ascii' |
|
377 | self.stdin_encoding = 'ascii' | |
378 |
|
378 | |||
379 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
379 | # dict of things NOT to alias (keywords, builtins and some magics) | |
380 | no_alias = {} |
|
380 | no_alias = {} | |
381 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
381 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] | |
382 | for key in keyword.kwlist + no_alias_magics: |
|
382 | for key in keyword.kwlist + no_alias_magics: | |
383 | no_alias[key] = 1 |
|
383 | no_alias[key] = 1 | |
384 | no_alias.update(__builtin__.__dict__) |
|
384 | no_alias.update(__builtin__.__dict__) | |
385 | self.no_alias = no_alias |
|
385 | self.no_alias = no_alias | |
386 |
|
386 | |||
387 | # make global variables for user access to these |
|
387 | # make global variables for user access to these | |
388 | self.user_ns['_ih'] = self.input_hist |
|
388 | self.user_ns['_ih'] = self.input_hist | |
389 | self.user_ns['_oh'] = self.output_hist |
|
389 | self.user_ns['_oh'] = self.output_hist | |
390 | self.user_ns['_dh'] = self.dir_hist |
|
390 | self.user_ns['_dh'] = self.dir_hist | |
391 |
|
391 | |||
392 | # user aliases to input and output histories |
|
392 | # user aliases to input and output histories | |
393 | self.user_ns['In'] = self.input_hist |
|
393 | self.user_ns['In'] = self.input_hist | |
394 | self.user_ns['Out'] = self.output_hist |
|
394 | self.user_ns['Out'] = self.output_hist | |
395 |
|
395 | |||
396 | self.user_ns['_sh'] = IPython.shadowns |
|
396 | self.user_ns['_sh'] = IPython.shadowns | |
397 | # Object variable to store code object waiting execution. This is |
|
397 | # Object variable to store code object waiting execution. This is | |
398 | # used mainly by the multithreaded shells, but it can come in handy in |
|
398 | # used mainly by the multithreaded shells, but it can come in handy in | |
399 | # other situations. No need to use a Queue here, since it's a single |
|
399 | # other situations. No need to use a Queue here, since it's a single | |
400 | # item which gets cleared once run. |
|
400 | # item which gets cleared once run. | |
401 | self.code_to_run = None |
|
401 | self.code_to_run = None | |
402 |
|
402 | |||
403 | # escapes for automatic behavior on the command line |
|
403 | # escapes for automatic behavior on the command line | |
404 | self.ESC_SHELL = '!' |
|
404 | self.ESC_SHELL = '!' | |
405 | self.ESC_SH_CAP = '!!' |
|
405 | self.ESC_SH_CAP = '!!' | |
406 | self.ESC_HELP = '?' |
|
406 | self.ESC_HELP = '?' | |
407 | self.ESC_MAGIC = '%' |
|
407 | self.ESC_MAGIC = '%' | |
408 | self.ESC_QUOTE = ',' |
|
408 | self.ESC_QUOTE = ',' | |
409 | self.ESC_QUOTE2 = ';' |
|
409 | self.ESC_QUOTE2 = ';' | |
410 | self.ESC_PAREN = '/' |
|
410 | self.ESC_PAREN = '/' | |
411 |
|
411 | |||
412 | # And their associated handlers |
|
412 | # And their associated handlers | |
413 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
413 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, | |
414 | self.ESC_QUOTE : self.handle_auto, |
|
414 | self.ESC_QUOTE : self.handle_auto, | |
415 | self.ESC_QUOTE2 : self.handle_auto, |
|
415 | self.ESC_QUOTE2 : self.handle_auto, | |
416 | self.ESC_MAGIC : self.handle_magic, |
|
416 | self.ESC_MAGIC : self.handle_magic, | |
417 | self.ESC_HELP : self.handle_help, |
|
417 | self.ESC_HELP : self.handle_help, | |
418 | self.ESC_SHELL : self.handle_shell_escape, |
|
418 | self.ESC_SHELL : self.handle_shell_escape, | |
419 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
419 | self.ESC_SH_CAP : self.handle_shell_escape, | |
420 | } |
|
420 | } | |
421 |
|
421 | |||
422 | # class initializations |
|
422 | # class initializations | |
423 | Magic.__init__(self,self) |
|
423 | Magic.__init__(self,self) | |
424 |
|
424 | |||
425 | # Python source parser/formatter for syntax highlighting |
|
425 | # Python source parser/formatter for syntax highlighting | |
426 | pyformat = PyColorize.Parser().format |
|
426 | pyformat = PyColorize.Parser().format | |
427 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
427 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) | |
428 |
|
428 | |||
429 | # hooks holds pointers used for user-side customizations |
|
429 | # hooks holds pointers used for user-side customizations | |
430 | self.hooks = Struct() |
|
430 | self.hooks = Struct() | |
431 |
|
431 | |||
432 | self.strdispatchers = {} |
|
432 | self.strdispatchers = {} | |
433 |
|
433 | |||
434 | # Set all default hooks, defined in the IPython.hooks module. |
|
434 | # Set all default hooks, defined in the IPython.hooks module. | |
435 | hooks = IPython.hooks |
|
435 | hooks = IPython.hooks | |
436 | for hook_name in hooks.__all__: |
|
436 | for hook_name in hooks.__all__: | |
437 | # default hooks have priority 100, i.e. low; user hooks should have |
|
437 | # default hooks have priority 100, i.e. low; user hooks should have | |
438 | # 0-100 priority |
|
438 | # 0-100 priority | |
439 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
439 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
440 | #print "bound hook",hook_name |
|
440 | #print "bound hook",hook_name | |
441 |
|
441 | |||
442 | # Flag to mark unconditional exit |
|
442 | # Flag to mark unconditional exit | |
443 | self.exit_now = False |
|
443 | self.exit_now = False | |
444 |
|
444 | |||
445 | self.usage_min = """\ |
|
445 | self.usage_min = """\ | |
446 | An enhanced console for Python. |
|
446 | An enhanced console for Python. | |
447 | Some of its features are: |
|
447 | Some of its features are: | |
448 | - Readline support if the readline library is present. |
|
448 | - Readline support if the readline library is present. | |
449 | - Tab completion in the local namespace. |
|
449 | - Tab completion in the local namespace. | |
450 | - Logging of input, see command-line options. |
|
450 | - Logging of input, see command-line options. | |
451 | - System shell escape via ! , eg !ls. |
|
451 | - System shell escape via ! , eg !ls. | |
452 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
452 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) | |
453 | - Keeps track of locally defined variables via %who, %whos. |
|
453 | - Keeps track of locally defined variables via %who, %whos. | |
454 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
454 | - Show object information with a ? eg ?x or x? (use ?? for more info). | |
455 | """ |
|
455 | """ | |
456 | if usage: self.usage = usage |
|
456 | if usage: self.usage = usage | |
457 | else: self.usage = self.usage_min |
|
457 | else: self.usage = self.usage_min | |
458 |
|
458 | |||
459 | # Storage |
|
459 | # Storage | |
460 | self.rc = rc # This will hold all configuration information |
|
460 | self.rc = rc # This will hold all configuration information | |
461 | self.pager = 'less' |
|
461 | self.pager = 'less' | |
462 | # temporary files used for various purposes. Deleted at exit. |
|
462 | # temporary files used for various purposes. Deleted at exit. | |
463 | self.tempfiles = [] |
|
463 | self.tempfiles = [] | |
464 |
|
464 | |||
465 | # Keep track of readline usage (later set by init_readline) |
|
465 | # Keep track of readline usage (later set by init_readline) | |
466 | self.has_readline = False |
|
466 | self.has_readline = False | |
467 |
|
467 | |||
468 | # template for logfile headers. It gets resolved at runtime by the |
|
468 | # template for logfile headers. It gets resolved at runtime by the | |
469 | # logstart method. |
|
469 | # logstart method. | |
470 | self.loghead_tpl = \ |
|
470 | self.loghead_tpl = \ | |
471 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
471 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** | |
472 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
472 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW | |
473 | #log# opts = %s |
|
473 | #log# opts = %s | |
474 | #log# args = %s |
|
474 | #log# args = %s | |
475 | #log# It is safe to make manual edits below here. |
|
475 | #log# It is safe to make manual edits below here. | |
476 | #log#----------------------------------------------------------------------- |
|
476 | #log#----------------------------------------------------------------------- | |
477 | """ |
|
477 | """ | |
478 | # for pushd/popd management |
|
478 | # for pushd/popd management | |
479 | try: |
|
479 | try: | |
480 | self.home_dir = get_home_dir() |
|
480 | self.home_dir = get_home_dir() | |
481 | except HomeDirError,msg: |
|
481 | except HomeDirError,msg: | |
482 | fatal(msg) |
|
482 | fatal(msg) | |
483 |
|
483 | |||
484 | self.dir_stack = [] |
|
484 | self.dir_stack = [] | |
485 |
|
485 | |||
486 | # Functions to call the underlying shell. |
|
486 | # Functions to call the underlying shell. | |
487 |
|
487 | |||
488 | # The first is similar to os.system, but it doesn't return a value, |
|
488 | # The first is similar to os.system, but it doesn't return a value, | |
489 | # and it allows interpolation of variables in the user's namespace. |
|
489 | # and it allows interpolation of variables in the user's namespace. | |
490 | self.system = lambda cmd: \ |
|
490 | self.system = lambda cmd: \ | |
491 | self.hooks.shell_hook(self.var_expand(cmd,depth=2)) |
|
491 | self.hooks.shell_hook(self.var_expand(cmd,depth=2)) | |
492 |
|
492 | |||
493 | # These are for getoutput and getoutputerror: |
|
493 | # These are for getoutput and getoutputerror: | |
494 | self.getoutput = lambda cmd: \ |
|
494 | self.getoutput = lambda cmd: \ | |
495 | getoutput(self.var_expand(cmd,depth=2), |
|
495 | getoutput(self.var_expand(cmd,depth=2), | |
496 | header=self.rc.system_header, |
|
496 | header=self.rc.system_header, | |
497 | verbose=self.rc.system_verbose) |
|
497 | verbose=self.rc.system_verbose) | |
498 |
|
498 | |||
499 | self.getoutputerror = lambda cmd: \ |
|
499 | self.getoutputerror = lambda cmd: \ | |
500 | getoutputerror(self.var_expand(cmd,depth=2), |
|
500 | getoutputerror(self.var_expand(cmd,depth=2), | |
501 | header=self.rc.system_header, |
|
501 | header=self.rc.system_header, | |
502 | verbose=self.rc.system_verbose) |
|
502 | verbose=self.rc.system_verbose) | |
503 |
|
503 | |||
504 |
|
504 | |||
505 | # keep track of where we started running (mainly for crash post-mortem) |
|
505 | # keep track of where we started running (mainly for crash post-mortem) | |
506 | self.starting_dir = os.getcwd() |
|
506 | self.starting_dir = os.getcwd() | |
507 |
|
507 | |||
508 | # Various switches which can be set |
|
508 | # Various switches which can be set | |
509 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
509 | self.CACHELENGTH = 5000 # this is cheap, it's just text | |
510 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
510 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ | |
511 | self.banner2 = banner2 |
|
511 | self.banner2 = banner2 | |
512 |
|
512 | |||
513 | # TraceBack handlers: |
|
513 | # TraceBack handlers: | |
514 |
|
514 | |||
515 | # Syntax error handler. |
|
515 | # Syntax error handler. | |
516 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
516 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
517 |
|
517 | |||
518 | # The interactive one is initialized with an offset, meaning we always |
|
518 | # The interactive one is initialized with an offset, meaning we always | |
519 | # want to remove the topmost item in the traceback, which is our own |
|
519 | # want to remove the topmost item in the traceback, which is our own | |
520 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
520 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
521 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
521 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', | |
522 | color_scheme='NoColor', |
|
522 | color_scheme='NoColor', | |
523 | tb_offset = 1) |
|
523 | tb_offset = 1) | |
524 |
|
524 | |||
525 | # IPython itself shouldn't crash. This will produce a detailed |
|
525 | # IPython itself shouldn't crash. This will produce a detailed | |
526 | # post-mortem if it does. But we only install the crash handler for |
|
526 | # post-mortem if it does. But we only install the crash handler for | |
527 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
527 | # non-threaded shells, the threaded ones use a normal verbose reporter | |
528 | # and lose the crash handler. This is because exceptions in the main |
|
528 | # and lose the crash handler. This is because exceptions in the main | |
529 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
529 | # thread (such as in GUI code) propagate directly to sys.excepthook, | |
530 | # and there's no point in printing crash dumps for every user exception. |
|
530 | # and there's no point in printing crash dumps for every user exception. | |
531 | if self.isthreaded: |
|
531 | if self.isthreaded: | |
532 | ipCrashHandler = ultraTB.FormattedTB() |
|
532 | ipCrashHandler = ultraTB.FormattedTB() | |
533 | else: |
|
533 | else: | |
534 | from IPython import CrashHandler |
|
534 | from IPython import CrashHandler | |
535 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
535 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) | |
536 | self.set_crash_handler(ipCrashHandler) |
|
536 | self.set_crash_handler(ipCrashHandler) | |
537 |
|
537 | |||
538 | # and add any custom exception handlers the user may have specified |
|
538 | # and add any custom exception handlers the user may have specified | |
539 | self.set_custom_exc(*custom_exceptions) |
|
539 | self.set_custom_exc(*custom_exceptions) | |
540 |
|
540 | |||
541 | # indentation management |
|
541 | # indentation management | |
542 | self.autoindent = False |
|
542 | self.autoindent = False | |
543 | self.indent_current_nsp = 0 |
|
543 | self.indent_current_nsp = 0 | |
544 |
|
544 | |||
545 | # Make some aliases automatically |
|
545 | # Make some aliases automatically | |
546 | # Prepare list of shell aliases to auto-define |
|
546 | # Prepare list of shell aliases to auto-define | |
547 | if os.name == 'posix': |
|
547 | if os.name == 'posix': | |
548 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
548 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', | |
549 | 'mv mv -i','rm rm -i','cp cp -i', |
|
549 | 'mv mv -i','rm rm -i','cp cp -i', | |
550 | 'cat cat','less less','clear clear', |
|
550 | 'cat cat','less less','clear clear', | |
551 | # a better ls |
|
551 | # a better ls | |
552 | 'ls ls -F', |
|
552 | 'ls ls -F', | |
553 | # long ls |
|
553 | # long ls | |
554 | 'll ls -lF') |
|
554 | 'll ls -lF') | |
555 | # Extra ls aliases with color, which need special treatment on BSD |
|
555 | # Extra ls aliases with color, which need special treatment on BSD | |
556 | # variants |
|
556 | # variants | |
557 | ls_extra = ( # color ls |
|
557 | ls_extra = ( # color ls | |
558 | 'lc ls -F -o --color', |
|
558 | 'lc ls -F -o --color', | |
559 | # ls normal files only |
|
559 | # ls normal files only | |
560 | 'lf ls -F -o --color %l | grep ^-', |
|
560 | 'lf ls -F -o --color %l | grep ^-', | |
561 | # ls symbolic links |
|
561 | # ls symbolic links | |
562 | 'lk ls -F -o --color %l | grep ^l', |
|
562 | 'lk ls -F -o --color %l | grep ^l', | |
563 | # directories or links to directories, |
|
563 | # directories or links to directories, | |
564 | 'ldir ls -F -o --color %l | grep /$', |
|
564 | 'ldir ls -F -o --color %l | grep /$', | |
565 | # things which are executable |
|
565 | # things which are executable | |
566 | 'lx ls -F -o --color %l | grep ^-..x', |
|
566 | 'lx ls -F -o --color %l | grep ^-..x', | |
567 | ) |
|
567 | ) | |
568 | # The BSDs don't ship GNU ls, so they don't understand the |
|
568 | # The BSDs don't ship GNU ls, so they don't understand the | |
569 | # --color switch out of the box |
|
569 | # --color switch out of the box | |
570 | if 'bsd' in sys.platform: |
|
570 | if 'bsd' in sys.platform: | |
571 | ls_extra = ( # ls normal files only |
|
571 | ls_extra = ( # ls normal files only | |
572 | 'lf ls -lF | grep ^-', |
|
572 | 'lf ls -lF | grep ^-', | |
573 | # ls symbolic links |
|
573 | # ls symbolic links | |
574 | 'lk ls -lF | grep ^l', |
|
574 | 'lk ls -lF | grep ^l', | |
575 | # directories or links to directories, |
|
575 | # directories or links to directories, | |
576 | 'ldir ls -lF | grep /$', |
|
576 | 'ldir ls -lF | grep /$', | |
577 | # things which are executable |
|
577 | # things which are executable | |
578 | 'lx ls -lF | grep ^-..x', |
|
578 | 'lx ls -lF | grep ^-..x', | |
579 | ) |
|
579 | ) | |
580 | auto_alias = auto_alias + ls_extra |
|
580 | auto_alias = auto_alias + ls_extra | |
581 | elif os.name in ['nt','dos']: |
|
581 | elif os.name in ['nt','dos']: | |
582 | auto_alias = ('ls dir /on', |
|
582 | auto_alias = ('ls dir /on', | |
583 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
583 | 'ddir dir /ad /on', 'ldir dir /ad /on', | |
584 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
584 | 'mkdir mkdir','rmdir rmdir','echo echo', | |
585 | 'ren ren','cls cls','copy copy') |
|
585 | 'ren ren','cls cls','copy copy') | |
586 | else: |
|
586 | else: | |
587 | auto_alias = () |
|
587 | auto_alias = () | |
588 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
588 | self.auto_alias = [s.split(None,1) for s in auto_alias] | |
589 |
|
589 | |||
590 |
|
590 | |||
591 | # Produce a public API instance |
|
591 | # Produce a public API instance | |
592 | self.api = IPython.ipapi.IPApi(self) |
|
592 | self.api = IPython.ipapi.IPApi(self) | |
593 |
|
593 | |||
594 | # Call the actual (public) initializer |
|
594 | # Call the actual (public) initializer | |
595 | self.init_auto_alias() |
|
595 | self.init_auto_alias() | |
596 |
|
596 | |||
597 | # track which builtins we add, so we can clean up later |
|
597 | # track which builtins we add, so we can clean up later | |
598 | self.builtins_added = {} |
|
598 | self.builtins_added = {} | |
599 | # This method will add the necessary builtins for operation, but |
|
599 | # This method will add the necessary builtins for operation, but | |
600 | # tracking what it did via the builtins_added dict. |
|
600 | # tracking what it did via the builtins_added dict. | |
601 |
|
601 | |||
602 | #TODO: remove this, redundant |
|
602 | #TODO: remove this, redundant | |
603 | self.add_builtins() |
|
603 | self.add_builtins() | |
604 |
|
604 | |||
605 |
|
605 | |||
606 |
|
606 | |||
607 |
|
607 | |||
608 | # end __init__ |
|
608 | # end __init__ | |
609 |
|
609 | |||
610 | def var_expand(self,cmd,depth=0): |
|
610 | def var_expand(self,cmd,depth=0): | |
611 | """Expand python variables in a string. |
|
611 | """Expand python variables in a string. | |
612 |
|
612 | |||
613 | The depth argument indicates how many frames above the caller should |
|
613 | The depth argument indicates how many frames above the caller should | |
614 | be walked to look for the local namespace where to expand variables. |
|
614 | be walked to look for the local namespace where to expand variables. | |
615 |
|
615 | |||
616 | The global namespace for expansion is always the user's interactive |
|
616 | The global namespace for expansion is always the user's interactive | |
617 | namespace. |
|
617 | namespace. | |
618 | """ |
|
618 | """ | |
619 |
|
619 | |||
620 | return str(ItplNS(cmd, |
|
620 | return str(ItplNS(cmd, | |
621 | self.user_ns, # globals |
|
621 | self.user_ns, # globals | |
622 | # Skip our own frame in searching for locals: |
|
622 | # Skip our own frame in searching for locals: | |
623 | sys._getframe(depth+1).f_locals # locals |
|
623 | sys._getframe(depth+1).f_locals # locals | |
624 | )) |
|
624 | )) | |
625 |
|
625 | |||
626 | def pre_config_initialization(self): |
|
626 | def pre_config_initialization(self): | |
627 | """Pre-configuration init method |
|
627 | """Pre-configuration init method | |
628 |
|
628 | |||
629 | This is called before the configuration files are processed to |
|
629 | This is called before the configuration files are processed to | |
630 | prepare the services the config files might need. |
|
630 | prepare the services the config files might need. | |
631 |
|
631 | |||
632 | self.rc already has reasonable default values at this point. |
|
632 | self.rc already has reasonable default values at this point. | |
633 | """ |
|
633 | """ | |
634 | rc = self.rc |
|
634 | rc = self.rc | |
635 | try: |
|
635 | try: | |
636 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
636 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") | |
637 | except exceptions.UnicodeDecodeError: |
|
637 | except exceptions.UnicodeDecodeError: | |
638 | print "Your ipythondir can't be decoded to unicode!" |
|
638 | print "Your ipythondir can't be decoded to unicode!" | |
639 | print "Please set HOME environment variable to something that" |
|
639 | print "Please set HOME environment variable to something that" | |
640 | print r"only has ASCII characters, e.g. c:\home" |
|
640 | print r"only has ASCII characters, e.g. c:\home" | |
641 | print "Now it is",rc.ipythondir |
|
641 | print "Now it is",rc.ipythondir | |
642 | sys.exit() |
|
642 | sys.exit() | |
643 | self.shadowhist = IPython.history.ShadowHist(self.db) |
|
643 | self.shadowhist = IPython.history.ShadowHist(self.db) | |
644 |
|
644 | |||
645 |
|
645 | |||
646 | def post_config_initialization(self): |
|
646 | def post_config_initialization(self): | |
647 | """Post configuration init method |
|
647 | """Post configuration init method | |
648 |
|
648 | |||
649 | This is called after the configuration files have been processed to |
|
649 | This is called after the configuration files have been processed to | |
650 | 'finalize' the initialization.""" |
|
650 | 'finalize' the initialization.""" | |
651 |
|
651 | |||
652 | rc = self.rc |
|
652 | rc = self.rc | |
653 |
|
653 | |||
654 | # Object inspector |
|
654 | # Object inspector | |
655 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
655 | self.inspector = OInspect.Inspector(OInspect.InspectColors, | |
656 | PyColorize.ANSICodeColors, |
|
656 | PyColorize.ANSICodeColors, | |
657 | 'NoColor', |
|
657 | 'NoColor', | |
658 | rc.object_info_string_level) |
|
658 | rc.object_info_string_level) | |
659 |
|
659 | |||
660 | self.rl_next_input = None |
|
660 | self.rl_next_input = None | |
661 | self.rl_do_indent = False |
|
661 | self.rl_do_indent = False | |
662 | # Load readline proper |
|
662 | # Load readline proper | |
663 | if rc.readline: |
|
663 | if rc.readline: | |
664 | self.init_readline() |
|
664 | self.init_readline() | |
665 |
|
665 | |||
666 |
|
666 | |||
667 | # local shortcut, this is used a LOT |
|
667 | # local shortcut, this is used a LOT | |
668 | self.log = self.logger.log |
|
668 | self.log = self.logger.log | |
669 |
|
669 | |||
670 | # Initialize cache, set in/out prompts and printing system |
|
670 | # Initialize cache, set in/out prompts and printing system | |
671 | self.outputcache = CachedOutput(self, |
|
671 | self.outputcache = CachedOutput(self, | |
672 | rc.cache_size, |
|
672 | rc.cache_size, | |
673 | rc.pprint, |
|
673 | rc.pprint, | |
674 | input_sep = rc.separate_in, |
|
674 | input_sep = rc.separate_in, | |
675 | output_sep = rc.separate_out, |
|
675 | output_sep = rc.separate_out, | |
676 | output_sep2 = rc.separate_out2, |
|
676 | output_sep2 = rc.separate_out2, | |
677 | ps1 = rc.prompt_in1, |
|
677 | ps1 = rc.prompt_in1, | |
678 | ps2 = rc.prompt_in2, |
|
678 | ps2 = rc.prompt_in2, | |
679 | ps_out = rc.prompt_out, |
|
679 | ps_out = rc.prompt_out, | |
680 | pad_left = rc.prompts_pad_left) |
|
680 | pad_left = rc.prompts_pad_left) | |
681 |
|
681 | |||
682 | # user may have over-ridden the default print hook: |
|
682 | # user may have over-ridden the default print hook: | |
683 | try: |
|
683 | try: | |
684 | self.outputcache.__class__.display = self.hooks.display |
|
684 | self.outputcache.__class__.display = self.hooks.display | |
685 | except AttributeError: |
|
685 | except AttributeError: | |
686 | pass |
|
686 | pass | |
687 |
|
687 | |||
688 | # I don't like assigning globally to sys, because it means when |
|
688 | # I don't like assigning globally to sys, because it means when | |
689 | # embedding instances, each embedded instance overrides the previous |
|
689 | # embedding instances, each embedded instance overrides the previous | |
690 | # choice. But sys.displayhook seems to be called internally by exec, |
|
690 | # choice. But sys.displayhook seems to be called internally by exec, | |
691 | # so I don't see a way around it. We first save the original and then |
|
691 | # so I don't see a way around it. We first save the original and then | |
692 | # overwrite it. |
|
692 | # overwrite it. | |
693 | self.sys_displayhook = sys.displayhook |
|
693 | self.sys_displayhook = sys.displayhook | |
694 | sys.displayhook = self.outputcache |
|
694 | sys.displayhook = self.outputcache | |
695 |
|
695 | |||
696 | # Do a proper resetting of doctest, including the necessary displayhook |
|
696 | # Do a proper resetting of doctest, including the necessary displayhook | |
697 | # monkeypatching |
|
697 | # monkeypatching | |
698 | try: |
|
698 | try: | |
699 | doctest_reload() |
|
699 | doctest_reload() | |
700 | except ImportError: |
|
700 | except ImportError: | |
701 | warn("doctest module does not exist.") |
|
701 | warn("doctest module does not exist.") | |
702 |
|
702 | |||
703 | # Set user colors (don't do it in the constructor above so that it |
|
703 | # Set user colors (don't do it in the constructor above so that it | |
704 | # doesn't crash if colors option is invalid) |
|
704 | # doesn't crash if colors option is invalid) | |
705 | self.magic_colors(rc.colors) |
|
705 | self.magic_colors(rc.colors) | |
706 |
|
706 | |||
707 | # Set calling of pdb on exceptions |
|
707 | # Set calling of pdb on exceptions | |
708 | self.call_pdb = rc.pdb |
|
708 | self.call_pdb = rc.pdb | |
709 |
|
709 | |||
710 | # Load user aliases |
|
710 | # Load user aliases | |
711 | for alias in rc.alias: |
|
711 | for alias in rc.alias: | |
712 | self.magic_alias(alias) |
|
712 | self.magic_alias(alias) | |
713 |
|
713 | |||
714 | self.hooks.late_startup_hook() |
|
714 | self.hooks.late_startup_hook() | |
715 |
|
715 | |||
716 | for cmd in self.rc.autoexec: |
|
716 | for cmd in self.rc.autoexec: | |
717 | #print "autoexec>",cmd #dbg |
|
717 | #print "autoexec>",cmd #dbg | |
718 | self.api.runlines(cmd) |
|
718 | self.api.runlines(cmd) | |
719 |
|
719 | |||
720 | batchrun = False |
|
720 | batchrun = False | |
721 | for batchfile in [path(arg) for arg in self.rc.args |
|
721 | for batchfile in [path(arg) for arg in self.rc.args | |
722 | if arg.lower().endswith('.ipy')]: |
|
722 | if arg.lower().endswith('.ipy')]: | |
723 | if not batchfile.isfile(): |
|
723 | if not batchfile.isfile(): | |
724 | print "No such batch file:", batchfile |
|
724 | print "No such batch file:", batchfile | |
725 | continue |
|
725 | continue | |
726 | self.api.runlines(batchfile.text()) |
|
726 | self.api.runlines(batchfile.text()) | |
727 | batchrun = True |
|
727 | batchrun = True | |
728 | # without -i option, exit after running the batch file |
|
728 | # without -i option, exit after running the batch file | |
729 | if batchrun and not self.rc.interact: |
|
729 | if batchrun and not self.rc.interact: | |
730 | self.ask_exit() |
|
730 | self.ask_exit() | |
731 |
|
731 | |||
732 | def add_builtins(self): |
|
732 | def add_builtins(self): | |
733 | """Store ipython references into the builtin namespace. |
|
733 | """Store ipython references into the builtin namespace. | |
734 |
|
734 | |||
735 | Some parts of ipython operate via builtins injected here, which hold a |
|
735 | Some parts of ipython operate via builtins injected here, which hold a | |
736 | reference to IPython itself.""" |
|
736 | reference to IPython itself.""" | |
737 |
|
737 | |||
738 | # TODO: deprecate all of these, they are unsafe |
|
738 | # TODO: deprecate all of these, they are unsafe | |
739 | builtins_new = dict(__IPYTHON__ = self, |
|
739 | builtins_new = dict(__IPYTHON__ = self, | |
740 | ip_set_hook = self.set_hook, |
|
740 | ip_set_hook = self.set_hook, | |
741 | jobs = self.jobs, |
|
741 | jobs = self.jobs, | |
742 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), |
|
742 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), | |
743 | ipalias = wrap_deprecated(self.ipalias), |
|
743 | ipalias = wrap_deprecated(self.ipalias), | |
744 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), |
|
744 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), | |
745 | #_ip = self.api |
|
745 | #_ip = self.api | |
746 | ) |
|
746 | ) | |
747 | for biname,bival in builtins_new.items(): |
|
747 | for biname,bival in builtins_new.items(): | |
748 | try: |
|
748 | try: | |
749 | # store the orignal value so we can restore it |
|
749 | # store the orignal value so we can restore it | |
750 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
750 | self.builtins_added[biname] = __builtin__.__dict__[biname] | |
751 | except KeyError: |
|
751 | except KeyError: | |
752 | # or mark that it wasn't defined, and we'll just delete it at |
|
752 | # or mark that it wasn't defined, and we'll just delete it at | |
753 | # cleanup |
|
753 | # cleanup | |
754 | self.builtins_added[biname] = Undefined |
|
754 | self.builtins_added[biname] = Undefined | |
755 | __builtin__.__dict__[biname] = bival |
|
755 | __builtin__.__dict__[biname] = bival | |
756 |
|
756 | |||
757 | # Keep in the builtins a flag for when IPython is active. We set it |
|
757 | # Keep in the builtins a flag for when IPython is active. We set it | |
758 | # with setdefault so that multiple nested IPythons don't clobber one |
|
758 | # with setdefault so that multiple nested IPythons don't clobber one | |
759 | # another. Each will increase its value by one upon being activated, |
|
759 | # another. Each will increase its value by one upon being activated, | |
760 | # which also gives us a way to determine the nesting level. |
|
760 | # which also gives us a way to determine the nesting level. | |
761 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
761 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |
762 |
|
762 | |||
763 | def clean_builtins(self): |
|
763 | def clean_builtins(self): | |
764 | """Remove any builtins which might have been added by add_builtins, or |
|
764 | """Remove any builtins which might have been added by add_builtins, or | |
765 | restore overwritten ones to their previous values.""" |
|
765 | restore overwritten ones to their previous values.""" | |
766 | for biname,bival in self.builtins_added.items(): |
|
766 | for biname,bival in self.builtins_added.items(): | |
767 | if bival is Undefined: |
|
767 | if bival is Undefined: | |
768 | del __builtin__.__dict__[biname] |
|
768 | del __builtin__.__dict__[biname] | |
769 | else: |
|
769 | else: | |
770 | __builtin__.__dict__[biname] = bival |
|
770 | __builtin__.__dict__[biname] = bival | |
771 | self.builtins_added.clear() |
|
771 | self.builtins_added.clear() | |
772 |
|
772 | |||
773 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
773 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
774 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
774 | """set_hook(name,hook) -> sets an internal IPython hook. | |
775 |
|
775 | |||
776 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
776 | IPython exposes some of its internal API as user-modifiable hooks. By | |
777 | adding your function to one of these hooks, you can modify IPython's |
|
777 | adding your function to one of these hooks, you can modify IPython's | |
778 | behavior to call at runtime your own routines.""" |
|
778 | behavior to call at runtime your own routines.""" | |
779 |
|
779 | |||
780 | # At some point in the future, this should validate the hook before it |
|
780 | # At some point in the future, this should validate the hook before it | |
781 | # accepts it. Probably at least check that the hook takes the number |
|
781 | # accepts it. Probably at least check that the hook takes the number | |
782 | # of args it's supposed to. |
|
782 | # of args it's supposed to. | |
783 |
|
783 | |||
784 | f = new.instancemethod(hook,self,self.__class__) |
|
784 | f = new.instancemethod(hook,self,self.__class__) | |
785 |
|
785 | |||
786 | # check if the hook is for strdispatcher first |
|
786 | # check if the hook is for strdispatcher first | |
787 | if str_key is not None: |
|
787 | if str_key is not None: | |
788 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
788 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
789 | sdp.add_s(str_key, f, priority ) |
|
789 | sdp.add_s(str_key, f, priority ) | |
790 | self.strdispatchers[name] = sdp |
|
790 | self.strdispatchers[name] = sdp | |
791 | return |
|
791 | return | |
792 | if re_key is not None: |
|
792 | if re_key is not None: | |
793 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
793 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
794 | sdp.add_re(re.compile(re_key), f, priority ) |
|
794 | sdp.add_re(re.compile(re_key), f, priority ) | |
795 | self.strdispatchers[name] = sdp |
|
795 | self.strdispatchers[name] = sdp | |
796 | return |
|
796 | return | |
797 |
|
797 | |||
798 | dp = getattr(self.hooks, name, None) |
|
798 | dp = getattr(self.hooks, name, None) | |
799 | if name not in IPython.hooks.__all__: |
|
799 | if name not in IPython.hooks.__all__: | |
800 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
800 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) | |
801 | if not dp: |
|
801 | if not dp: | |
802 | dp = IPython.hooks.CommandChainDispatcher() |
|
802 | dp = IPython.hooks.CommandChainDispatcher() | |
803 |
|
803 | |||
804 | try: |
|
804 | try: | |
805 | dp.add(f,priority) |
|
805 | dp.add(f,priority) | |
806 | except AttributeError: |
|
806 | except AttributeError: | |
807 | # it was not commandchain, plain old func - replace |
|
807 | # it was not commandchain, plain old func - replace | |
808 | dp = f |
|
808 | dp = f | |
809 |
|
809 | |||
810 | setattr(self.hooks,name, dp) |
|
810 | setattr(self.hooks,name, dp) | |
811 |
|
811 | |||
812 |
|
812 | |||
813 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
813 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) | |
814 |
|
814 | |||
815 | def set_crash_handler(self,crashHandler): |
|
815 | def set_crash_handler(self,crashHandler): | |
816 | """Set the IPython crash handler. |
|
816 | """Set the IPython crash handler. | |
817 |
|
817 | |||
818 | This must be a callable with a signature suitable for use as |
|
818 | This must be a callable with a signature suitable for use as | |
819 | sys.excepthook.""" |
|
819 | sys.excepthook.""" | |
820 |
|
820 | |||
821 | # Install the given crash handler as the Python exception hook |
|
821 | # Install the given crash handler as the Python exception hook | |
822 | sys.excepthook = crashHandler |
|
822 | sys.excepthook = crashHandler | |
823 |
|
823 | |||
824 | # The instance will store a pointer to this, so that runtime code |
|
824 | # The instance will store a pointer to this, so that runtime code | |
825 | # (such as magics) can access it. This is because during the |
|
825 | # (such as magics) can access it. This is because during the | |
826 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
826 | # read-eval loop, it gets temporarily overwritten (to deal with GUI | |
827 | # frameworks). |
|
827 | # frameworks). | |
828 | self.sys_excepthook = sys.excepthook |
|
828 | self.sys_excepthook = sys.excepthook | |
829 |
|
829 | |||
830 |
|
830 | |||
831 | def set_custom_exc(self,exc_tuple,handler): |
|
831 | def set_custom_exc(self,exc_tuple,handler): | |
832 | """set_custom_exc(exc_tuple,handler) |
|
832 | """set_custom_exc(exc_tuple,handler) | |
833 |
|
833 | |||
834 | Set a custom exception handler, which will be called if any of the |
|
834 | Set a custom exception handler, which will be called if any of the | |
835 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
835 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
836 | runcode() method. |
|
836 | runcode() method. | |
837 |
|
837 | |||
838 | Inputs: |
|
838 | Inputs: | |
839 |
|
839 | |||
840 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
840 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
841 | handler for. It is very important that you use a tuple, and NOT A |
|
841 | handler for. It is very important that you use a tuple, and NOT A | |
842 | LIST here, because of the way Python's except statement works. If |
|
842 | LIST here, because of the way Python's except statement works. If | |
843 | you only want to trap a single exception, use a singleton tuple: |
|
843 | you only want to trap a single exception, use a singleton tuple: | |
844 |
|
844 | |||
845 | exc_tuple == (MyCustomException,) |
|
845 | exc_tuple == (MyCustomException,) | |
846 |
|
846 | |||
847 | - handler: this must be defined as a function with the following |
|
847 | - handler: this must be defined as a function with the following | |
848 | basic interface: def my_handler(self,etype,value,tb). |
|
848 | basic interface: def my_handler(self,etype,value,tb). | |
849 |
|
849 | |||
850 | This will be made into an instance method (via new.instancemethod) |
|
850 | This will be made into an instance method (via new.instancemethod) | |
851 | of IPython itself, and it will be called if any of the exceptions |
|
851 | of IPython itself, and it will be called if any of the exceptions | |
852 | listed in the exc_tuple are caught. If the handler is None, an |
|
852 | listed in the exc_tuple are caught. If the handler is None, an | |
853 | internal basic one is used, which just prints basic info. |
|
853 | internal basic one is used, which just prints basic info. | |
854 |
|
854 | |||
855 | WARNING: by putting in your own exception handler into IPython's main |
|
855 | WARNING: by putting in your own exception handler into IPython's main | |
856 | execution loop, you run a very good chance of nasty crashes. This |
|
856 | execution loop, you run a very good chance of nasty crashes. This | |
857 | facility should only be used if you really know what you are doing.""" |
|
857 | facility should only be used if you really know what you are doing.""" | |
858 |
|
858 | |||
859 | assert type(exc_tuple)==type(()) , \ |
|
859 | assert type(exc_tuple)==type(()) , \ | |
860 | "The custom exceptions must be given AS A TUPLE." |
|
860 | "The custom exceptions must be given AS A TUPLE." | |
861 |
|
861 | |||
862 | def dummy_handler(self,etype,value,tb): |
|
862 | def dummy_handler(self,etype,value,tb): | |
863 | print '*** Simple custom exception handler ***' |
|
863 | print '*** Simple custom exception handler ***' | |
864 | print 'Exception type :',etype |
|
864 | print 'Exception type :',etype | |
865 | print 'Exception value:',value |
|
865 | print 'Exception value:',value | |
866 | print 'Traceback :',tb |
|
866 | print 'Traceback :',tb | |
867 | print 'Source code :','\n'.join(self.buffer) |
|
867 | print 'Source code :','\n'.join(self.buffer) | |
868 |
|
868 | |||
869 | if handler is None: handler = dummy_handler |
|
869 | if handler is None: handler = dummy_handler | |
870 |
|
870 | |||
871 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
871 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
872 | self.custom_exceptions = exc_tuple |
|
872 | self.custom_exceptions = exc_tuple | |
873 |
|
873 | |||
874 | def set_custom_completer(self,completer,pos=0): |
|
874 | def set_custom_completer(self,completer,pos=0): | |
875 | """set_custom_completer(completer,pos=0) |
|
875 | """set_custom_completer(completer,pos=0) | |
876 |
|
876 | |||
877 | Adds a new custom completer function. |
|
877 | Adds a new custom completer function. | |
878 |
|
878 | |||
879 | The position argument (defaults to 0) is the index in the completers |
|
879 | The position argument (defaults to 0) is the index in the completers | |
880 | list where you want the completer to be inserted.""" |
|
880 | list where you want the completer to be inserted.""" | |
881 |
|
881 | |||
882 | newcomp = new.instancemethod(completer,self.Completer, |
|
882 | newcomp = new.instancemethod(completer,self.Completer, | |
883 | self.Completer.__class__) |
|
883 | self.Completer.__class__) | |
884 | self.Completer.matchers.insert(pos,newcomp) |
|
884 | self.Completer.matchers.insert(pos,newcomp) | |
885 |
|
885 | |||
886 | def set_completer(self): |
|
886 | def set_completer(self): | |
887 | """reset readline's completer to be our own.""" |
|
887 | """reset readline's completer to be our own.""" | |
888 | self.readline.set_completer(self.Completer.complete) |
|
888 | self.readline.set_completer(self.Completer.complete) | |
889 |
|
889 | |||
890 | def _get_call_pdb(self): |
|
890 | def _get_call_pdb(self): | |
891 | return self._call_pdb |
|
891 | return self._call_pdb | |
892 |
|
892 | |||
893 | def _set_call_pdb(self,val): |
|
893 | def _set_call_pdb(self,val): | |
894 |
|
894 | |||
895 | if val not in (0,1,False,True): |
|
895 | if val not in (0,1,False,True): | |
896 | raise ValueError,'new call_pdb value must be boolean' |
|
896 | raise ValueError,'new call_pdb value must be boolean' | |
897 |
|
897 | |||
898 | # store value in instance |
|
898 | # store value in instance | |
899 | self._call_pdb = val |
|
899 | self._call_pdb = val | |
900 |
|
900 | |||
901 | # notify the actual exception handlers |
|
901 | # notify the actual exception handlers | |
902 | self.InteractiveTB.call_pdb = val |
|
902 | self.InteractiveTB.call_pdb = val | |
903 | if self.isthreaded: |
|
903 | if self.isthreaded: | |
904 | try: |
|
904 | try: | |
905 | self.sys_excepthook.call_pdb = val |
|
905 | self.sys_excepthook.call_pdb = val | |
906 | except: |
|
906 | except: | |
907 | warn('Failed to activate pdb for threaded exception handler') |
|
907 | warn('Failed to activate pdb for threaded exception handler') | |
908 |
|
908 | |||
909 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
909 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
910 | 'Control auto-activation of pdb at exceptions') |
|
910 | 'Control auto-activation of pdb at exceptions') | |
911 |
|
911 | |||
912 |
|
912 | |||
913 | # These special functions get installed in the builtin namespace, to |
|
913 | # These special functions get installed in the builtin namespace, to | |
914 | # provide programmatic (pure python) access to magics, aliases and system |
|
914 | # provide programmatic (pure python) access to magics, aliases and system | |
915 | # calls. This is important for logging, user scripting, and more. |
|
915 | # calls. This is important for logging, user scripting, and more. | |
916 |
|
916 | |||
917 | # We are basically exposing, via normal python functions, the three |
|
917 | # We are basically exposing, via normal python functions, the three | |
918 | # mechanisms in which ipython offers special call modes (magics for |
|
918 | # mechanisms in which ipython offers special call modes (magics for | |
919 | # internal control, aliases for direct system access via pre-selected |
|
919 | # internal control, aliases for direct system access via pre-selected | |
920 | # names, and !cmd for calling arbitrary system commands). |
|
920 | # names, and !cmd for calling arbitrary system commands). | |
921 |
|
921 | |||
922 | def ipmagic(self,arg_s): |
|
922 | def ipmagic(self,arg_s): | |
923 | """Call a magic function by name. |
|
923 | """Call a magic function by name. | |
924 |
|
924 | |||
925 | Input: a string containing the name of the magic function to call and any |
|
925 | Input: a string containing the name of the magic function to call and any | |
926 | additional arguments to be passed to the magic. |
|
926 | additional arguments to be passed to the magic. | |
927 |
|
927 | |||
928 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
928 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython | |
929 | prompt: |
|
929 | prompt: | |
930 |
|
930 | |||
931 | In[1]: %name -opt foo bar |
|
931 | In[1]: %name -opt foo bar | |
932 |
|
932 | |||
933 | To call a magic without arguments, simply use ipmagic('name'). |
|
933 | To call a magic without arguments, simply use ipmagic('name'). | |
934 |
|
934 | |||
935 | This provides a proper Python function to call IPython's magics in any |
|
935 | This provides a proper Python function to call IPython's magics in any | |
936 | valid Python code you can type at the interpreter, including loops and |
|
936 | valid Python code you can type at the interpreter, including loops and | |
937 | compound statements. It is added by IPython to the Python builtin |
|
937 | compound statements. It is added by IPython to the Python builtin | |
938 | namespace upon initialization.""" |
|
938 | namespace upon initialization.""" | |
939 |
|
939 | |||
940 | args = arg_s.split(' ',1) |
|
940 | args = arg_s.split(' ',1) | |
941 | magic_name = args[0] |
|
941 | magic_name = args[0] | |
942 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
942 | magic_name = magic_name.lstrip(self.ESC_MAGIC) | |
943 |
|
943 | |||
944 | try: |
|
944 | try: | |
945 | magic_args = args[1] |
|
945 | magic_args = args[1] | |
946 | except IndexError: |
|
946 | except IndexError: | |
947 | magic_args = '' |
|
947 | magic_args = '' | |
948 | fn = getattr(self,'magic_'+magic_name,None) |
|
948 | fn = getattr(self,'magic_'+magic_name,None) | |
949 | if fn is None: |
|
949 | if fn is None: | |
950 | error("Magic function `%s` not found." % magic_name) |
|
950 | error("Magic function `%s` not found." % magic_name) | |
951 | else: |
|
951 | else: | |
952 | magic_args = self.var_expand(magic_args,1) |
|
952 | magic_args = self.var_expand(magic_args,1) | |
953 | return fn(magic_args) |
|
953 | return fn(magic_args) | |
954 |
|
954 | |||
955 | def ipalias(self,arg_s): |
|
955 | def ipalias(self,arg_s): | |
956 | """Call an alias by name. |
|
956 | """Call an alias by name. | |
957 |
|
957 | |||
958 | Input: a string containing the name of the alias to call and any |
|
958 | Input: a string containing the name of the alias to call and any | |
959 | additional arguments to be passed to the magic. |
|
959 | additional arguments to be passed to the magic. | |
960 |
|
960 | |||
961 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
961 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |
962 | prompt: |
|
962 | prompt: | |
963 |
|
963 | |||
964 | In[1]: name -opt foo bar |
|
964 | In[1]: name -opt foo bar | |
965 |
|
965 | |||
966 | To call an alias without arguments, simply use ipalias('name'). |
|
966 | To call an alias without arguments, simply use ipalias('name'). | |
967 |
|
967 | |||
968 | This provides a proper Python function to call IPython's aliases in any |
|
968 | This provides a proper Python function to call IPython's aliases in any | |
969 | valid Python code you can type at the interpreter, including loops and |
|
969 | valid Python code you can type at the interpreter, including loops and | |
970 | compound statements. It is added by IPython to the Python builtin |
|
970 | compound statements. It is added by IPython to the Python builtin | |
971 | namespace upon initialization.""" |
|
971 | namespace upon initialization.""" | |
972 |
|
972 | |||
973 | args = arg_s.split(' ',1) |
|
973 | args = arg_s.split(' ',1) | |
974 | alias_name = args[0] |
|
974 | alias_name = args[0] | |
975 | try: |
|
975 | try: | |
976 | alias_args = args[1] |
|
976 | alias_args = args[1] | |
977 | except IndexError: |
|
977 | except IndexError: | |
978 | alias_args = '' |
|
978 | alias_args = '' | |
979 | if alias_name in self.alias_table: |
|
979 | if alias_name in self.alias_table: | |
980 | self.call_alias(alias_name,alias_args) |
|
980 | self.call_alias(alias_name,alias_args) | |
981 | else: |
|
981 | else: | |
982 | error("Alias `%s` not found." % alias_name) |
|
982 | error("Alias `%s` not found." % alias_name) | |
983 |
|
983 | |||
984 | def ipsystem(self,arg_s): |
|
984 | def ipsystem(self,arg_s): | |
985 | """Make a system call, using IPython.""" |
|
985 | """Make a system call, using IPython.""" | |
986 |
|
986 | |||
987 | self.system(arg_s) |
|
987 | self.system(arg_s) | |
988 |
|
988 | |||
989 | def complete(self,text): |
|
989 | def complete(self,text): | |
990 | """Return a sorted list of all possible completions on text. |
|
990 | """Return a sorted list of all possible completions on text. | |
991 |
|
991 | |||
992 | Inputs: |
|
992 | Inputs: | |
993 |
|
993 | |||
994 | - text: a string of text to be completed on. |
|
994 | - text: a string of text to be completed on. | |
995 |
|
995 | |||
996 | This is a wrapper around the completion mechanism, similar to what |
|
996 | This is a wrapper around the completion mechanism, similar to what | |
997 | readline does at the command line when the TAB key is hit. By |
|
997 | readline does at the command line when the TAB key is hit. By | |
998 | exposing it as a method, it can be used by other non-readline |
|
998 | exposing it as a method, it can be used by other non-readline | |
999 | environments (such as GUIs) for text completion. |
|
999 | environments (such as GUIs) for text completion. | |
1000 |
|
1000 | |||
1001 | Simple usage example: |
|
1001 | Simple usage example: | |
1002 |
|
1002 | |||
1003 | In [7]: x = 'hello' |
|
1003 | In [7]: x = 'hello' | |
1004 |
|
1004 | |||
1005 | In [8]: x |
|
1005 | In [8]: x | |
1006 | Out[8]: 'hello' |
|
1006 | Out[8]: 'hello' | |
1007 |
|
1007 | |||
1008 | In [9]: print x |
|
1008 | In [9]: print x | |
1009 | hello |
|
1009 | hello | |
1010 |
|
1010 | |||
1011 | In [10]: _ip.IP.complete('x.l') |
|
1011 | In [10]: _ip.IP.complete('x.l') | |
1012 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1012 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] | |
1013 | """ |
|
1013 | """ | |
1014 |
|
1014 | |||
1015 | complete = self.Completer.complete |
|
1015 | complete = self.Completer.complete | |
1016 | state = 0 |
|
1016 | state = 0 | |
1017 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1017 | # use a dict so we get unique keys, since ipyhton's multiple | |
1018 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1018 | # completers can return duplicates. When we make 2.4 a requirement, | |
1019 | # start using sets instead, which are faster. |
|
1019 | # start using sets instead, which are faster. | |
1020 | comps = {} |
|
1020 | comps = {} | |
1021 | while True: |
|
1021 | while True: | |
1022 | newcomp = complete(text,state,line_buffer=text) |
|
1022 | newcomp = complete(text,state,line_buffer=text) | |
1023 | if newcomp is None: |
|
1023 | if newcomp is None: | |
1024 | break |
|
1024 | break | |
1025 | comps[newcomp] = 1 |
|
1025 | comps[newcomp] = 1 | |
1026 | state += 1 |
|
1026 | state += 1 | |
1027 | outcomps = comps.keys() |
|
1027 | outcomps = comps.keys() | |
1028 | outcomps.sort() |
|
1028 | outcomps.sort() | |
1029 | #print "T:",text,"OC:",outcomps # dbg |
|
1029 | #print "T:",text,"OC:",outcomps # dbg | |
1030 | #print "vars:",self.user_ns.keys() |
|
1030 | #print "vars:",self.user_ns.keys() | |
1031 | return outcomps |
|
1031 | return outcomps | |
1032 |
|
1032 | |||
1033 | def set_completer_frame(self, frame=None): |
|
1033 | def set_completer_frame(self, frame=None): | |
1034 | if frame: |
|
1034 | if frame: | |
1035 | self.Completer.namespace = frame.f_locals |
|
1035 | self.Completer.namespace = frame.f_locals | |
1036 | self.Completer.global_namespace = frame.f_globals |
|
1036 | self.Completer.global_namespace = frame.f_globals | |
1037 | else: |
|
1037 | else: | |
1038 | self.Completer.namespace = self.user_ns |
|
1038 | self.Completer.namespace = self.user_ns | |
1039 | self.Completer.global_namespace = self.user_global_ns |
|
1039 | self.Completer.global_namespace = self.user_global_ns | |
1040 |
|
1040 | |||
1041 | def init_auto_alias(self): |
|
1041 | def init_auto_alias(self): | |
1042 | """Define some aliases automatically. |
|
1042 | """Define some aliases automatically. | |
1043 |
|
1043 | |||
1044 | These are ALL parameter-less aliases""" |
|
1044 | These are ALL parameter-less aliases""" | |
1045 |
|
1045 | |||
1046 | for alias,cmd in self.auto_alias: |
|
1046 | for alias,cmd in self.auto_alias: | |
1047 | self.getapi().defalias(alias,cmd) |
|
1047 | self.getapi().defalias(alias,cmd) | |
1048 |
|
1048 | |||
1049 |
|
1049 | |||
1050 | def alias_table_validate(self,verbose=0): |
|
1050 | def alias_table_validate(self,verbose=0): | |
1051 | """Update information about the alias table. |
|
1051 | """Update information about the alias table. | |
1052 |
|
1052 | |||
1053 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1053 | In particular, make sure no Python keywords/builtins are in it.""" | |
1054 |
|
1054 | |||
1055 | no_alias = self.no_alias |
|
1055 | no_alias = self.no_alias | |
1056 | for k in self.alias_table.keys(): |
|
1056 | for k in self.alias_table.keys(): | |
1057 | if k in no_alias: |
|
1057 | if k in no_alias: | |
1058 | del self.alias_table[k] |
|
1058 | del self.alias_table[k] | |
1059 | if verbose: |
|
1059 | if verbose: | |
1060 | print ("Deleting alias <%s>, it's a Python " |
|
1060 | print ("Deleting alias <%s>, it's a Python " | |
1061 | "keyword or builtin." % k) |
|
1061 | "keyword or builtin." % k) | |
1062 |
|
1062 | |||
1063 | def set_autoindent(self,value=None): |
|
1063 | def set_autoindent(self,value=None): | |
1064 | """Set the autoindent flag, checking for readline support. |
|
1064 | """Set the autoindent flag, checking for readline support. | |
1065 |
|
1065 | |||
1066 | If called with no arguments, it acts as a toggle.""" |
|
1066 | If called with no arguments, it acts as a toggle.""" | |
1067 |
|
1067 | |||
1068 | if not self.has_readline: |
|
1068 | if not self.has_readline: | |
1069 | if os.name == 'posix': |
|
1069 | if os.name == 'posix': | |
1070 | warn("The auto-indent feature requires the readline library") |
|
1070 | warn("The auto-indent feature requires the readline library") | |
1071 | self.autoindent = 0 |
|
1071 | self.autoindent = 0 | |
1072 | return |
|
1072 | return | |
1073 | if value is None: |
|
1073 | if value is None: | |
1074 | self.autoindent = not self.autoindent |
|
1074 | self.autoindent = not self.autoindent | |
1075 | else: |
|
1075 | else: | |
1076 | self.autoindent = value |
|
1076 | self.autoindent = value | |
1077 |
|
1077 | |||
1078 | def rc_set_toggle(self,rc_field,value=None): |
|
1078 | def rc_set_toggle(self,rc_field,value=None): | |
1079 | """Set or toggle a field in IPython's rc config. structure. |
|
1079 | """Set or toggle a field in IPython's rc config. structure. | |
1080 |
|
1080 | |||
1081 | If called with no arguments, it acts as a toggle. |
|
1081 | If called with no arguments, it acts as a toggle. | |
1082 |
|
1082 | |||
1083 | If called with a non-existent field, the resulting AttributeError |
|
1083 | If called with a non-existent field, the resulting AttributeError | |
1084 | exception will propagate out.""" |
|
1084 | exception will propagate out.""" | |
1085 |
|
1085 | |||
1086 | rc_val = getattr(self.rc,rc_field) |
|
1086 | rc_val = getattr(self.rc,rc_field) | |
1087 | if value is None: |
|
1087 | if value is None: | |
1088 | value = not rc_val |
|
1088 | value = not rc_val | |
1089 | setattr(self.rc,rc_field,value) |
|
1089 | setattr(self.rc,rc_field,value) | |
1090 |
|
1090 | |||
1091 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1091 | def user_setup(self,ipythondir,rc_suffix,mode='install'): | |
1092 | """Install the user configuration directory. |
|
1092 | """Install the user configuration directory. | |
1093 |
|
1093 | |||
1094 | Can be called when running for the first time or to upgrade the user's |
|
1094 | Can be called when running for the first time or to upgrade the user's | |
1095 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1095 | .ipython/ directory with the mode parameter. Valid modes are 'install' | |
1096 | and 'upgrade'.""" |
|
1096 | and 'upgrade'.""" | |
1097 |
|
1097 | |||
1098 | def wait(): |
|
1098 | def wait(): | |
1099 | try: |
|
1099 | try: | |
1100 | raw_input("Please press <RETURN> to start IPython.") |
|
1100 | raw_input("Please press <RETURN> to start IPython.") | |
1101 | except EOFError: |
|
1101 | except EOFError: | |
1102 | print >> Term.cout |
|
1102 | print >> Term.cout | |
1103 | print '*'*70 |
|
1103 | print '*'*70 | |
1104 |
|
1104 | |||
1105 | cwd = os.getcwd() # remember where we started |
|
1105 | cwd = os.getcwd() # remember where we started | |
1106 | glb = glob.glob |
|
1106 | glb = glob.glob | |
1107 | print '*'*70 |
|
1107 | print '*'*70 | |
1108 | if mode == 'install': |
|
1108 | if mode == 'install': | |
1109 | print \ |
|
1109 | print \ | |
1110 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1110 | """Welcome to IPython. I will try to create a personal configuration directory | |
1111 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1111 | where you can customize many aspects of IPython's functionality in:\n""" | |
1112 | else: |
|
1112 | else: | |
1113 | print 'I am going to upgrade your configuration in:' |
|
1113 | print 'I am going to upgrade your configuration in:' | |
1114 |
|
1114 | |||
1115 | print ipythondir |
|
1115 | print ipythondir | |
1116 |
|
1116 | |||
1117 | rcdirend = os.path.join('IPython','UserConfig') |
|
1117 | rcdirend = os.path.join('IPython','UserConfig') | |
1118 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1118 | cfg = lambda d: os.path.join(d,rcdirend) | |
1119 | try: |
|
1119 | try: | |
1120 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1120 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] | |
1121 | print "Initializing from configuration",rcdir |
|
1121 | print "Initializing from configuration",rcdir | |
1122 | except IndexError: |
|
1122 | except IndexError: | |
1123 | warning = """ |
|
1123 | warning = """ | |
1124 | Installation error. IPython's directory was not found. |
|
1124 | Installation error. IPython's directory was not found. | |
1125 |
|
1125 | |||
1126 | Check the following: |
|
1126 | Check the following: | |
1127 |
|
1127 | |||
1128 | The ipython/IPython directory should be in a directory belonging to your |
|
1128 | The ipython/IPython directory should be in a directory belonging to your | |
1129 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1129 | PYTHONPATH environment variable (that is, it should be in a directory | |
1130 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1130 | belonging to sys.path). You can copy it explicitly there or just link to it. | |
1131 |
|
1131 | |||
1132 | IPython will create a minimal default configuration for you. |
|
1132 | IPython will create a minimal default configuration for you. | |
1133 |
|
1133 | |||
1134 | """ |
|
1134 | """ | |
1135 | warn(warning) |
|
1135 | warn(warning) | |
1136 | wait() |
|
1136 | wait() | |
1137 |
|
1137 | |||
1138 | if sys.platform =='win32': |
|
1138 | if sys.platform =='win32': | |
1139 | inif = 'ipythonrc.ini' |
|
1139 | inif = 'ipythonrc.ini' | |
1140 | else: |
|
1140 | else: | |
1141 | inif = 'ipythonrc' |
|
1141 | inif = 'ipythonrc' | |
1142 | minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' } |
|
1142 | minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' } | |
1143 | os.makedirs(ipythondir, mode = 0777) |
|
1143 | os.makedirs(ipythondir, mode = 0777) | |
1144 | for f, cont in minimal_setup.items(): |
|
1144 | for f, cont in minimal_setup.items(): | |
1145 | open(ipythondir + '/' + f,'w').write(cont) |
|
1145 | open(ipythondir + '/' + f,'w').write(cont) | |
1146 |
|
1146 | |||
1147 | return |
|
1147 | return | |
1148 |
|
1148 | |||
1149 | if mode == 'install': |
|
1149 | if mode == 'install': | |
1150 | try: |
|
1150 | try: | |
1151 | shutil.copytree(rcdir,ipythondir) |
|
1151 | shutil.copytree(rcdir,ipythondir) | |
1152 | os.chdir(ipythondir) |
|
1152 | os.chdir(ipythondir) | |
1153 | rc_files = glb("ipythonrc*") |
|
1153 | rc_files = glb("ipythonrc*") | |
1154 | for rc_file in rc_files: |
|
1154 | for rc_file in rc_files: | |
1155 | os.rename(rc_file,rc_file+rc_suffix) |
|
1155 | os.rename(rc_file,rc_file+rc_suffix) | |
1156 | except: |
|
1156 | except: | |
1157 | warning = """ |
|
1157 | warning = """ | |
1158 |
|
1158 | |||
1159 | There was a problem with the installation: |
|
1159 | There was a problem with the installation: | |
1160 | %s |
|
1160 | %s | |
1161 | Try to correct it or contact the developers if you think it's a bug. |
|
1161 | Try to correct it or contact the developers if you think it's a bug. | |
1162 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1162 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] | |
1163 | warn(warning) |
|
1163 | warn(warning) | |
1164 | wait() |
|
1164 | wait() | |
1165 | return |
|
1165 | return | |
1166 |
|
1166 | |||
1167 | elif mode == 'upgrade': |
|
1167 | elif mode == 'upgrade': | |
1168 | try: |
|
1168 | try: | |
1169 | os.chdir(ipythondir) |
|
1169 | os.chdir(ipythondir) | |
1170 | except: |
|
1170 | except: | |
1171 | print """ |
|
1171 | print """ | |
1172 | Can not upgrade: changing to directory %s failed. Details: |
|
1172 | Can not upgrade: changing to directory %s failed. Details: | |
1173 | %s |
|
1173 | %s | |
1174 | """ % (ipythondir,sys.exc_info()[1]) |
|
1174 | """ % (ipythondir,sys.exc_info()[1]) | |
1175 | wait() |
|
1175 | wait() | |
1176 | return |
|
1176 | return | |
1177 | else: |
|
1177 | else: | |
1178 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1178 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) | |
1179 | for new_full_path in sources: |
|
1179 | for new_full_path in sources: | |
1180 | new_filename = os.path.basename(new_full_path) |
|
1180 | new_filename = os.path.basename(new_full_path) | |
1181 | if new_filename.startswith('ipythonrc'): |
|
1181 | if new_filename.startswith('ipythonrc'): | |
1182 | new_filename = new_filename + rc_suffix |
|
1182 | new_filename = new_filename + rc_suffix | |
1183 | # The config directory should only contain files, skip any |
|
1183 | # The config directory should only contain files, skip any | |
1184 | # directories which may be there (like CVS) |
|
1184 | # directories which may be there (like CVS) | |
1185 | if os.path.isdir(new_full_path): |
|
1185 | if os.path.isdir(new_full_path): | |
1186 | continue |
|
1186 | continue | |
1187 | if os.path.exists(new_filename): |
|
1187 | if os.path.exists(new_filename): | |
1188 | old_file = new_filename+'.old' |
|
1188 | old_file = new_filename+'.old' | |
1189 | if os.path.exists(old_file): |
|
1189 | if os.path.exists(old_file): | |
1190 | os.remove(old_file) |
|
1190 | os.remove(old_file) | |
1191 | os.rename(new_filename,old_file) |
|
1191 | os.rename(new_filename,old_file) | |
1192 | shutil.copy(new_full_path,new_filename) |
|
1192 | shutil.copy(new_full_path,new_filename) | |
1193 | else: |
|
1193 | else: | |
1194 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1194 | raise ValueError,'unrecognized mode for install:',`mode` | |
1195 |
|
1195 | |||
1196 | # Fix line-endings to those native to each platform in the config |
|
1196 | # Fix line-endings to those native to each platform in the config | |
1197 | # directory. |
|
1197 | # directory. | |
1198 | try: |
|
1198 | try: | |
1199 | os.chdir(ipythondir) |
|
1199 | os.chdir(ipythondir) | |
1200 | except: |
|
1200 | except: | |
1201 | print """ |
|
1201 | print """ | |
1202 | Problem: changing to directory %s failed. |
|
1202 | Problem: changing to directory %s failed. | |
1203 | Details: |
|
1203 | Details: | |
1204 | %s |
|
1204 | %s | |
1205 |
|
1205 | |||
1206 | Some configuration files may have incorrect line endings. This should not |
|
1206 | Some configuration files may have incorrect line endings. This should not | |
1207 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1207 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) | |
1208 | wait() |
|
1208 | wait() | |
1209 | else: |
|
1209 | else: | |
1210 | for fname in glb('ipythonrc*'): |
|
1210 | for fname in glb('ipythonrc*'): | |
1211 | try: |
|
1211 | try: | |
1212 | native_line_ends(fname,backup=0) |
|
1212 | native_line_ends(fname,backup=0) | |
1213 | except IOError: |
|
1213 | except IOError: | |
1214 | pass |
|
1214 | pass | |
1215 |
|
1215 | |||
1216 | if mode == 'install': |
|
1216 | if mode == 'install': | |
1217 | print """ |
|
1217 | print """ | |
1218 | Successful installation! |
|
1218 | Successful installation! | |
1219 |
|
1219 | |||
1220 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1220 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the | |
1221 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1221 | IPython manual (there are both HTML and PDF versions supplied with the | |
1222 | distribution) to make sure that your system environment is properly configured |
|
1222 | distribution) to make sure that your system environment is properly configured | |
1223 | to take advantage of IPython's features. |
|
1223 | to take advantage of IPython's features. | |
1224 |
|
1224 | |||
1225 | Important note: the configuration system has changed! The old system is |
|
1225 | Important note: the configuration system has changed! The old system is | |
1226 | still in place, but its setting may be partly overridden by the settings in |
|
1226 | still in place, but its setting may be partly overridden by the settings in | |
1227 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1227 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file | |
1228 | if some of the new settings bother you. |
|
1228 | if some of the new settings bother you. | |
1229 |
|
1229 | |||
1230 | """ |
|
1230 | """ | |
1231 | else: |
|
1231 | else: | |
1232 | print """ |
|
1232 | print """ | |
1233 | Successful upgrade! |
|
1233 | Successful upgrade! | |
1234 |
|
1234 | |||
1235 | All files in your directory: |
|
1235 | All files in your directory: | |
1236 | %(ipythondir)s |
|
1236 | %(ipythondir)s | |
1237 | which would have been overwritten by the upgrade were backed up with a .old |
|
1237 | which would have been overwritten by the upgrade were backed up with a .old | |
1238 | extension. If you had made particular customizations in those files you may |
|
1238 | extension. If you had made particular customizations in those files you may | |
1239 | want to merge them back into the new files.""" % locals() |
|
1239 | want to merge them back into the new files.""" % locals() | |
1240 | wait() |
|
1240 | wait() | |
1241 | os.chdir(cwd) |
|
1241 | os.chdir(cwd) | |
1242 | # end user_setup() |
|
1242 | # end user_setup() | |
1243 |
|
1243 | |||
1244 | def atexit_operations(self): |
|
1244 | def atexit_operations(self): | |
1245 | """This will be executed at the time of exit. |
|
1245 | """This will be executed at the time of exit. | |
1246 |
|
1246 | |||
1247 | Saving of persistent data should be performed here. """ |
|
1247 | Saving of persistent data should be performed here. """ | |
1248 |
|
1248 | |||
1249 | #print '*** IPython exit cleanup ***' # dbg |
|
1249 | #print '*** IPython exit cleanup ***' # dbg | |
1250 | # input history |
|
1250 | # input history | |
1251 | self.savehist() |
|
1251 | self.savehist() | |
1252 |
|
1252 | |||
1253 | # Cleanup all tempfiles left around |
|
1253 | # Cleanup all tempfiles left around | |
1254 | for tfile in self.tempfiles: |
|
1254 | for tfile in self.tempfiles: | |
1255 | try: |
|
1255 | try: | |
1256 | os.unlink(tfile) |
|
1256 | os.unlink(tfile) | |
1257 | except OSError: |
|
1257 | except OSError: | |
1258 | pass |
|
1258 | pass | |
1259 |
|
1259 | |||
1260 | self.hooks.shutdown_hook() |
|
1260 | self.hooks.shutdown_hook() | |
1261 |
|
1261 | |||
1262 | def savehist(self): |
|
1262 | def savehist(self): | |
1263 | """Save input history to a file (via readline library).""" |
|
1263 | """Save input history to a file (via readline library).""" | |
1264 |
|
1264 | |||
1265 | if not self.has_readline: |
|
1265 | if not self.has_readline: | |
1266 | return |
|
1266 | return | |
1267 |
|
1267 | |||
1268 | try: |
|
1268 | try: | |
1269 | self.readline.write_history_file(self.histfile) |
|
1269 | self.readline.write_history_file(self.histfile) | |
1270 | except: |
|
1270 | except: | |
1271 | print 'Unable to save IPython command history to file: ' + \ |
|
1271 | print 'Unable to save IPython command history to file: ' + \ | |
1272 | `self.histfile` |
|
1272 | `self.histfile` | |
1273 |
|
1273 | |||
1274 | def reloadhist(self): |
|
1274 | def reloadhist(self): | |
1275 | """Reload the input history from disk file.""" |
|
1275 | """Reload the input history from disk file.""" | |
1276 |
|
1276 | |||
1277 | if self.has_readline: |
|
1277 | if self.has_readline: | |
1278 | try: |
|
1278 | try: | |
1279 | self.readline.clear_history() |
|
1279 | self.readline.clear_history() | |
1280 | self.readline.read_history_file(self.shell.histfile) |
|
1280 | self.readline.read_history_file(self.shell.histfile) | |
1281 | except AttributeError: |
|
1281 | except AttributeError: | |
1282 | pass |
|
1282 | pass | |
1283 |
|
1283 | |||
1284 |
|
1284 | |||
1285 | def history_saving_wrapper(self, func): |
|
1285 | def history_saving_wrapper(self, func): | |
1286 | """ Wrap func for readline history saving |
|
1286 | """ Wrap func for readline history saving | |
1287 |
|
1287 | |||
1288 | Convert func into callable that saves & restores |
|
1288 | Convert func into callable that saves & restores | |
1289 | history around the call """ |
|
1289 | history around the call """ | |
1290 |
|
1290 | |||
1291 | if not self.has_readline: |
|
1291 | if not self.has_readline: | |
1292 | return func |
|
1292 | return func | |
1293 |
|
1293 | |||
1294 | def wrapper(): |
|
1294 | def wrapper(): | |
1295 | self.savehist() |
|
1295 | self.savehist() | |
1296 | try: |
|
1296 | try: | |
1297 | func() |
|
1297 | func() | |
1298 | finally: |
|
1298 | finally: | |
1299 | readline.read_history_file(self.histfile) |
|
1299 | readline.read_history_file(self.histfile) | |
1300 | return wrapper |
|
1300 | return wrapper | |
1301 |
|
1301 | |||
1302 |
|
1302 | |||
1303 | def pre_readline(self): |
|
1303 | def pre_readline(self): | |
1304 | """readline hook to be used at the start of each line. |
|
1304 | """readline hook to be used at the start of each line. | |
1305 |
|
1305 | |||
1306 | Currently it handles auto-indent only.""" |
|
1306 | Currently it handles auto-indent only.""" | |
1307 |
|
1307 | |||
1308 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1308 | #debugx('self.indent_current_nsp','pre_readline:') | |
1309 |
|
1309 | |||
1310 | if self.rl_do_indent: |
|
1310 | if self.rl_do_indent: | |
1311 | self.readline.insert_text(self.indent_current_str()) |
|
1311 | self.readline.insert_text(self.indent_current_str()) | |
1312 | if self.rl_next_input is not None: |
|
1312 | if self.rl_next_input is not None: | |
1313 | self.readline.insert_text(self.rl_next_input) |
|
1313 | self.readline.insert_text(self.rl_next_input) | |
1314 | self.rl_next_input = None |
|
1314 | self.rl_next_input = None | |
1315 |
|
1315 | |||
1316 | def init_readline(self): |
|
1316 | def init_readline(self): | |
1317 | """Command history completion/saving/reloading.""" |
|
1317 | """Command history completion/saving/reloading.""" | |
1318 |
|
1318 | |||
1319 |
|
1319 | |||
1320 | import IPython.rlineimpl as readline |
|
1320 | import IPython.rlineimpl as readline | |
1321 |
|
1321 | |||
1322 | if not readline.have_readline: |
|
1322 | if not readline.have_readline: | |
1323 | self.has_readline = 0 |
|
1323 | self.has_readline = 0 | |
1324 | self.readline = None |
|
1324 | self.readline = None | |
1325 | # no point in bugging windows users with this every time: |
|
1325 | # no point in bugging windows users with this every time: | |
1326 | warn('Readline services not available on this platform.') |
|
1326 | warn('Readline services not available on this platform.') | |
1327 | else: |
|
1327 | else: | |
1328 | sys.modules['readline'] = readline |
|
1328 | sys.modules['readline'] = readline | |
1329 | import atexit |
|
1329 | import atexit | |
1330 | from IPython.completer import IPCompleter |
|
1330 | from IPython.completer import IPCompleter | |
1331 | self.Completer = IPCompleter(self, |
|
1331 | self.Completer = IPCompleter(self, | |
1332 | self.user_ns, |
|
1332 | self.user_ns, | |
1333 | self.user_global_ns, |
|
1333 | self.user_global_ns, | |
1334 | self.rc.readline_omit__names, |
|
1334 | self.rc.readline_omit__names, | |
1335 | self.alias_table) |
|
1335 | self.alias_table) | |
1336 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1336 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1337 | self.strdispatchers['complete_command'] = sdisp |
|
1337 | self.strdispatchers['complete_command'] = sdisp | |
1338 | self.Completer.custom_completers = sdisp |
|
1338 | self.Completer.custom_completers = sdisp | |
1339 | # Platform-specific configuration |
|
1339 | # Platform-specific configuration | |
1340 | if os.name == 'nt': |
|
1340 | if os.name == 'nt': | |
1341 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1341 | self.readline_startup_hook = readline.set_pre_input_hook | |
1342 | else: |
|
1342 | else: | |
1343 | self.readline_startup_hook = readline.set_startup_hook |
|
1343 | self.readline_startup_hook = readline.set_startup_hook | |
1344 |
|
1344 | |||
1345 | # Load user's initrc file (readline config) |
|
1345 | # Load user's initrc file (readline config) | |
1346 | # Or if libedit is used, load editrc. |
|
1346 | # Or if libedit is used, load editrc. | |
1347 | inputrc_name = os.environ.get('INPUTRC') |
|
1347 | inputrc_name = os.environ.get('INPUTRC') | |
1348 | if inputrc_name is None: |
|
1348 | if inputrc_name is None: | |
1349 | home_dir = get_home_dir() |
|
1349 | home_dir = get_home_dir() | |
1350 | if home_dir is not None: |
|
1350 | if home_dir is not None: | |
1351 | inputrc_name = '.inputrc' |
|
1351 | inputrc_name = '.inputrc' | |
1352 | if readline.uses_libedit: |
|
1352 | if readline.uses_libedit: | |
1353 | inputrc_name = '.editrc' |
|
1353 | inputrc_name = '.editrc' | |
1354 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1354 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
1355 | if os.path.isfile(inputrc_name): |
|
1355 | if os.path.isfile(inputrc_name): | |
1356 | try: |
|
1356 | try: | |
1357 | readline.read_init_file(inputrc_name) |
|
1357 | readline.read_init_file(inputrc_name) | |
1358 | except: |
|
1358 | except: | |
1359 | warn('Problems reading readline initialization file <%s>' |
|
1359 | warn('Problems reading readline initialization file <%s>' | |
1360 | % inputrc_name) |
|
1360 | % inputrc_name) | |
1361 |
|
1361 | |||
1362 | self.has_readline = 1 |
|
1362 | self.has_readline = 1 | |
1363 | self.readline = readline |
|
1363 | self.readline = readline | |
1364 | # save this in sys so embedded copies can restore it properly |
|
1364 | # save this in sys so embedded copies can restore it properly | |
1365 | sys.ipcompleter = self.Completer.complete |
|
1365 | sys.ipcompleter = self.Completer.complete | |
1366 | self.set_completer() |
|
1366 | self.set_completer() | |
1367 |
|
1367 | |||
1368 | # Configure readline according to user's prefs |
|
1368 | # Configure readline according to user's prefs | |
1369 | # This is only done if GNU readline is being used. If libedit |
|
1369 | # This is only done if GNU readline is being used. If libedit | |
1370 | # is being used (as on Leopard) the readline config is |
|
1370 | # is being used (as on Leopard) the readline config is | |
1371 | # not run as the syntax for libedit is different. |
|
1371 | # not run as the syntax for libedit is different. | |
1372 | if not readline.uses_libedit: |
|
1372 | if not readline.uses_libedit: | |
1373 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1373 | for rlcommand in self.rc.readline_parse_and_bind: | |
|
1374 | #print "loading rl:",rlcommand # dbg | |||
1374 | readline.parse_and_bind(rlcommand) |
|
1375 | readline.parse_and_bind(rlcommand) | |
1375 |
|
1376 | |||
1376 | # remove some chars from the delimiters list |
|
1377 | # remove some chars from the delimiters list | |
1377 | delims = readline.get_completer_delims() |
|
1378 | delims = readline.get_completer_delims() | |
1378 | delims = delims.translate(string._idmap, |
|
1379 | delims = delims.translate(string._idmap, | |
1379 | self.rc.readline_remove_delims) |
|
1380 | self.rc.readline_remove_delims) | |
1380 | readline.set_completer_delims(delims) |
|
1381 | readline.set_completer_delims(delims) | |
1381 | # otherwise we end up with a monster history after a while: |
|
1382 | # otherwise we end up with a monster history after a while: | |
1382 | readline.set_history_length(1000) |
|
1383 | readline.set_history_length(1000) | |
1383 | try: |
|
1384 | try: | |
1384 | #print '*** Reading readline history' # dbg |
|
1385 | #print '*** Reading readline history' # dbg | |
1385 | readline.read_history_file(self.histfile) |
|
1386 | readline.read_history_file(self.histfile) | |
1386 | except IOError: |
|
1387 | except IOError: | |
1387 | pass # It doesn't exist yet. |
|
1388 | pass # It doesn't exist yet. | |
1388 |
|
1389 | |||
1389 | atexit.register(self.atexit_operations) |
|
1390 | atexit.register(self.atexit_operations) | |
1390 | del atexit |
|
1391 | del atexit | |
1391 |
|
1392 | |||
1392 | # Configure auto-indent for all platforms |
|
1393 | # Configure auto-indent for all platforms | |
1393 | self.set_autoindent(self.rc.autoindent) |
|
1394 | self.set_autoindent(self.rc.autoindent) | |
1394 |
|
1395 | |||
1395 | def ask_yes_no(self,prompt,default=True): |
|
1396 | def ask_yes_no(self,prompt,default=True): | |
1396 | if self.rc.quiet: |
|
1397 | if self.rc.quiet: | |
1397 | return True |
|
1398 | return True | |
1398 | return ask_yes_no(prompt,default) |
|
1399 | return ask_yes_no(prompt,default) | |
1399 |
|
1400 | |||
1400 | def _should_recompile(self,e): |
|
1401 | def _should_recompile(self,e): | |
1401 | """Utility routine for edit_syntax_error""" |
|
1402 | """Utility routine for edit_syntax_error""" | |
1402 |
|
1403 | |||
1403 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1404 | if e.filename in ('<ipython console>','<input>','<string>', | |
1404 | '<console>','<BackgroundJob compilation>', |
|
1405 | '<console>','<BackgroundJob compilation>', | |
1405 | None): |
|
1406 | None): | |
1406 |
|
1407 | |||
1407 | return False |
|
1408 | return False | |
1408 | try: |
|
1409 | try: | |
1409 | if (self.rc.autoedit_syntax and |
|
1410 | if (self.rc.autoedit_syntax and | |
1410 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1411 | not self.ask_yes_no('Return to editor to correct syntax error? ' | |
1411 | '[Y/n] ','y')): |
|
1412 | '[Y/n] ','y')): | |
1412 | return False |
|
1413 | return False | |
1413 | except EOFError: |
|
1414 | except EOFError: | |
1414 | return False |
|
1415 | return False | |
1415 |
|
1416 | |||
1416 | def int0(x): |
|
1417 | def int0(x): | |
1417 | try: |
|
1418 | try: | |
1418 | return int(x) |
|
1419 | return int(x) | |
1419 | except TypeError: |
|
1420 | except TypeError: | |
1420 | return 0 |
|
1421 | return 0 | |
1421 | # always pass integer line and offset values to editor hook |
|
1422 | # always pass integer line and offset values to editor hook | |
1422 | try: |
|
1423 | try: | |
1423 | self.hooks.fix_error_editor(e.filename, |
|
1424 | self.hooks.fix_error_editor(e.filename, | |
1424 | int0(e.lineno),int0(e.offset),e.msg) |
|
1425 | int0(e.lineno),int0(e.offset),e.msg) | |
1425 | except IPython.ipapi.TryNext: |
|
1426 | except IPython.ipapi.TryNext: | |
1426 | warn('Could not open editor') |
|
1427 | warn('Could not open editor') | |
1427 | return False |
|
1428 | return False | |
1428 | return True |
|
1429 | return True | |
1429 |
|
1430 | |||
1430 | def edit_syntax_error(self): |
|
1431 | def edit_syntax_error(self): | |
1431 | """The bottom half of the syntax error handler called in the main loop. |
|
1432 | """The bottom half of the syntax error handler called in the main loop. | |
1432 |
|
1433 | |||
1433 | Loop until syntax error is fixed or user cancels. |
|
1434 | Loop until syntax error is fixed or user cancels. | |
1434 | """ |
|
1435 | """ | |
1435 |
|
1436 | |||
1436 | while self.SyntaxTB.last_syntax_error: |
|
1437 | while self.SyntaxTB.last_syntax_error: | |
1437 | # copy and clear last_syntax_error |
|
1438 | # copy and clear last_syntax_error | |
1438 | err = self.SyntaxTB.clear_err_state() |
|
1439 | err = self.SyntaxTB.clear_err_state() | |
1439 | if not self._should_recompile(err): |
|
1440 | if not self._should_recompile(err): | |
1440 | return |
|
1441 | return | |
1441 | try: |
|
1442 | try: | |
1442 | # may set last_syntax_error again if a SyntaxError is raised |
|
1443 | # may set last_syntax_error again if a SyntaxError is raised | |
1443 | self.safe_execfile(err.filename,self.user_ns) |
|
1444 | self.safe_execfile(err.filename,self.user_ns) | |
1444 | except: |
|
1445 | except: | |
1445 | self.showtraceback() |
|
1446 | self.showtraceback() | |
1446 | else: |
|
1447 | else: | |
1447 | try: |
|
1448 | try: | |
1448 | f = file(err.filename) |
|
1449 | f = file(err.filename) | |
1449 | try: |
|
1450 | try: | |
1450 | sys.displayhook(f.read()) |
|
1451 | sys.displayhook(f.read()) | |
1451 | finally: |
|
1452 | finally: | |
1452 | f.close() |
|
1453 | f.close() | |
1453 | except: |
|
1454 | except: | |
1454 | self.showtraceback() |
|
1455 | self.showtraceback() | |
1455 |
|
1456 | |||
1456 | def showsyntaxerror(self, filename=None): |
|
1457 | def showsyntaxerror(self, filename=None): | |
1457 | """Display the syntax error that just occurred. |
|
1458 | """Display the syntax error that just occurred. | |
1458 |
|
1459 | |||
1459 | This doesn't display a stack trace because there isn't one. |
|
1460 | This doesn't display a stack trace because there isn't one. | |
1460 |
|
1461 | |||
1461 | If a filename is given, it is stuffed in the exception instead |
|
1462 | If a filename is given, it is stuffed in the exception instead | |
1462 | of what was there before (because Python's parser always uses |
|
1463 | of what was there before (because Python's parser always uses | |
1463 | "<string>" when reading from a string). |
|
1464 | "<string>" when reading from a string). | |
1464 | """ |
|
1465 | """ | |
1465 | etype, value, last_traceback = sys.exc_info() |
|
1466 | etype, value, last_traceback = sys.exc_info() | |
1466 |
|
1467 | |||
1467 | # See note about these variables in showtraceback() below |
|
1468 | # See note about these variables in showtraceback() below | |
1468 | sys.last_type = etype |
|
1469 | sys.last_type = etype | |
1469 | sys.last_value = value |
|
1470 | sys.last_value = value | |
1470 | sys.last_traceback = last_traceback |
|
1471 | sys.last_traceback = last_traceback | |
1471 |
|
1472 | |||
1472 | if filename and etype is SyntaxError: |
|
1473 | if filename and etype is SyntaxError: | |
1473 | # Work hard to stuff the correct filename in the exception |
|
1474 | # Work hard to stuff the correct filename in the exception | |
1474 | try: |
|
1475 | try: | |
1475 | msg, (dummy_filename, lineno, offset, line) = value |
|
1476 | msg, (dummy_filename, lineno, offset, line) = value | |
1476 | except: |
|
1477 | except: | |
1477 | # Not the format we expect; leave it alone |
|
1478 | # Not the format we expect; leave it alone | |
1478 | pass |
|
1479 | pass | |
1479 | else: |
|
1480 | else: | |
1480 | # Stuff in the right filename |
|
1481 | # Stuff in the right filename | |
1481 | try: |
|
1482 | try: | |
1482 | # Assume SyntaxError is a class exception |
|
1483 | # Assume SyntaxError is a class exception | |
1483 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1484 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1484 | except: |
|
1485 | except: | |
1485 | # If that failed, assume SyntaxError is a string |
|
1486 | # If that failed, assume SyntaxError is a string | |
1486 | value = msg, (filename, lineno, offset, line) |
|
1487 | value = msg, (filename, lineno, offset, line) | |
1487 | self.SyntaxTB(etype,value,[]) |
|
1488 | self.SyntaxTB(etype,value,[]) | |
1488 |
|
1489 | |||
1489 | def debugger(self,force=False): |
|
1490 | def debugger(self,force=False): | |
1490 | """Call the pydb/pdb debugger. |
|
1491 | """Call the pydb/pdb debugger. | |
1491 |
|
1492 | |||
1492 | Keywords: |
|
1493 | Keywords: | |
1493 |
|
1494 | |||
1494 | - force(False): by default, this routine checks the instance call_pdb |
|
1495 | - force(False): by default, this routine checks the instance call_pdb | |
1495 | flag and does not actually invoke the debugger if the flag is false. |
|
1496 | flag and does not actually invoke the debugger if the flag is false. | |
1496 | The 'force' option forces the debugger to activate even if the flag |
|
1497 | The 'force' option forces the debugger to activate even if the flag | |
1497 | is false. |
|
1498 | is false. | |
1498 | """ |
|
1499 | """ | |
1499 |
|
1500 | |||
1500 | if not (force or self.call_pdb): |
|
1501 | if not (force or self.call_pdb): | |
1501 | return |
|
1502 | return | |
1502 |
|
1503 | |||
1503 | if not hasattr(sys,'last_traceback'): |
|
1504 | if not hasattr(sys,'last_traceback'): | |
1504 | error('No traceback has been produced, nothing to debug.') |
|
1505 | error('No traceback has been produced, nothing to debug.') | |
1505 | return |
|
1506 | return | |
1506 |
|
1507 | |||
1507 | # use pydb if available |
|
1508 | # use pydb if available | |
1508 | if Debugger.has_pydb: |
|
1509 | if Debugger.has_pydb: | |
1509 | from pydb import pm |
|
1510 | from pydb import pm | |
1510 | else: |
|
1511 | else: | |
1511 | # fallback to our internal debugger |
|
1512 | # fallback to our internal debugger | |
1512 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1513 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
1513 | self.history_saving_wrapper(pm)() |
|
1514 | self.history_saving_wrapper(pm)() | |
1514 |
|
1515 | |||
1515 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1516 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): | |
1516 | """Display the exception that just occurred. |
|
1517 | """Display the exception that just occurred. | |
1517 |
|
1518 | |||
1518 | If nothing is known about the exception, this is the method which |
|
1519 | If nothing is known about the exception, this is the method which | |
1519 | should be used throughout the code for presenting user tracebacks, |
|
1520 | should be used throughout the code for presenting user tracebacks, | |
1520 | rather than directly invoking the InteractiveTB object. |
|
1521 | rather than directly invoking the InteractiveTB object. | |
1521 |
|
1522 | |||
1522 | A specific showsyntaxerror() also exists, but this method can take |
|
1523 | A specific showsyntaxerror() also exists, but this method can take | |
1523 | care of calling it if needed, so unless you are explicitly catching a |
|
1524 | care of calling it if needed, so unless you are explicitly catching a | |
1524 | SyntaxError exception, don't try to analyze the stack manually and |
|
1525 | SyntaxError exception, don't try to analyze the stack manually and | |
1525 | simply call this method.""" |
|
1526 | simply call this method.""" | |
1526 |
|
1527 | |||
1527 |
|
1528 | |||
1528 | # Though this won't be called by syntax errors in the input line, |
|
1529 | # Though this won't be called by syntax errors in the input line, | |
1529 | # there may be SyntaxError cases whith imported code. |
|
1530 | # there may be SyntaxError cases whith imported code. | |
1530 |
|
1531 | |||
1531 | try: |
|
1532 | try: | |
1532 | if exc_tuple is None: |
|
1533 | if exc_tuple is None: | |
1533 | etype, value, tb = sys.exc_info() |
|
1534 | etype, value, tb = sys.exc_info() | |
1534 | else: |
|
1535 | else: | |
1535 | etype, value, tb = exc_tuple |
|
1536 | etype, value, tb = exc_tuple | |
1536 |
|
1537 | |||
1537 | if etype is SyntaxError: |
|
1538 | if etype is SyntaxError: | |
1538 | self.showsyntaxerror(filename) |
|
1539 | self.showsyntaxerror(filename) | |
1539 | elif etype is IPython.ipapi.UsageError: |
|
1540 | elif etype is IPython.ipapi.UsageError: | |
1540 | print "UsageError:", value |
|
1541 | print "UsageError:", value | |
1541 | else: |
|
1542 | else: | |
1542 | # WARNING: these variables are somewhat deprecated and not |
|
1543 | # WARNING: these variables are somewhat deprecated and not | |
1543 | # necessarily safe to use in a threaded environment, but tools |
|
1544 | # necessarily safe to use in a threaded environment, but tools | |
1544 | # like pdb depend on their existence, so let's set them. If we |
|
1545 | # like pdb depend on their existence, so let's set them. If we | |
1545 | # find problems in the field, we'll need to revisit their use. |
|
1546 | # find problems in the field, we'll need to revisit their use. | |
1546 | sys.last_type = etype |
|
1547 | sys.last_type = etype | |
1547 | sys.last_value = value |
|
1548 | sys.last_value = value | |
1548 | sys.last_traceback = tb |
|
1549 | sys.last_traceback = tb | |
1549 |
|
1550 | |||
1550 | if etype in self.custom_exceptions: |
|
1551 | if etype in self.custom_exceptions: | |
1551 | self.CustomTB(etype,value,tb) |
|
1552 | self.CustomTB(etype,value,tb) | |
1552 | else: |
|
1553 | else: | |
1553 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1554 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) | |
1554 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1555 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1555 | # pdb mucks up readline, fix it back |
|
1556 | # pdb mucks up readline, fix it back | |
1556 | self.set_completer() |
|
1557 | self.set_completer() | |
1557 | except KeyboardInterrupt: |
|
1558 | except KeyboardInterrupt: | |
1558 | self.write("\nKeyboardInterrupt\n") |
|
1559 | self.write("\nKeyboardInterrupt\n") | |
1559 |
|
1560 | |||
1560 |
|
1561 | |||
1561 |
|
1562 | |||
1562 | def mainloop(self,banner=None): |
|
1563 | def mainloop(self,banner=None): | |
1563 | """Creates the local namespace and starts the mainloop. |
|
1564 | """Creates the local namespace and starts the mainloop. | |
1564 |
|
1565 | |||
1565 | If an optional banner argument is given, it will override the |
|
1566 | If an optional banner argument is given, it will override the | |
1566 | internally created default banner.""" |
|
1567 | internally created default banner.""" | |
1567 |
|
1568 | |||
1568 | if self.rc.c: # Emulate Python's -c option |
|
1569 | if self.rc.c: # Emulate Python's -c option | |
1569 | self.exec_init_cmd() |
|
1570 | self.exec_init_cmd() | |
1570 | if banner is None: |
|
1571 | if banner is None: | |
1571 | if not self.rc.banner: |
|
1572 | if not self.rc.banner: | |
1572 | banner = '' |
|
1573 | banner = '' | |
1573 | # banner is string? Use it directly! |
|
1574 | # banner is string? Use it directly! | |
1574 | elif isinstance(self.rc.banner,basestring): |
|
1575 | elif isinstance(self.rc.banner,basestring): | |
1575 | banner = self.rc.banner |
|
1576 | banner = self.rc.banner | |
1576 | else: |
|
1577 | else: | |
1577 | banner = self.BANNER+self.banner2 |
|
1578 | banner = self.BANNER+self.banner2 | |
1578 |
|
1579 | |||
1579 | # if you run stuff with -c <cmd>, raw hist is not updated |
|
1580 | # if you run stuff with -c <cmd>, raw hist is not updated | |
1580 | # ensure that it's in sync |
|
1581 | # ensure that it's in sync | |
1581 | if len(self.input_hist) != len (self.input_hist_raw): |
|
1582 | if len(self.input_hist) != len (self.input_hist_raw): | |
1582 | self.input_hist_raw = InputList(self.input_hist) |
|
1583 | self.input_hist_raw = InputList(self.input_hist) | |
1583 |
|
1584 | |||
1584 | while 1: |
|
1585 | while 1: | |
1585 | try: |
|
1586 | try: | |
1586 | self.interact(banner) |
|
1587 | self.interact(banner) | |
1587 | #self.interact_with_readline() |
|
1588 | #self.interact_with_readline() | |
1588 | # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above |
|
1589 | # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above | |
1589 |
|
1590 | |||
1590 | break |
|
1591 | break | |
1591 | except KeyboardInterrupt: |
|
1592 | except KeyboardInterrupt: | |
1592 | # this should not be necessary, but KeyboardInterrupt |
|
1593 | # this should not be necessary, but KeyboardInterrupt | |
1593 | # handling seems rather unpredictable... |
|
1594 | # handling seems rather unpredictable... | |
1594 | self.write("\nKeyboardInterrupt in interact()\n") |
|
1595 | self.write("\nKeyboardInterrupt in interact()\n") | |
1595 |
|
1596 | |||
1596 | def exec_init_cmd(self): |
|
1597 | def exec_init_cmd(self): | |
1597 | """Execute a command given at the command line. |
|
1598 | """Execute a command given at the command line. | |
1598 |
|
1599 | |||
1599 | This emulates Python's -c option.""" |
|
1600 | This emulates Python's -c option.""" | |
1600 |
|
1601 | |||
1601 | #sys.argv = ['-c'] |
|
1602 | #sys.argv = ['-c'] | |
1602 | self.push(self.prefilter(self.rc.c, False)) |
|
1603 | self.push(self.prefilter(self.rc.c, False)) | |
1603 | if not self.rc.interact: |
|
1604 | if not self.rc.interact: | |
1604 | self.ask_exit() |
|
1605 | self.ask_exit() | |
1605 |
|
1606 | |||
1606 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1607 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): | |
1607 | """Embeds IPython into a running python program. |
|
1608 | """Embeds IPython into a running python program. | |
1608 |
|
1609 | |||
1609 | Input: |
|
1610 | Input: | |
1610 |
|
1611 | |||
1611 | - header: An optional header message can be specified. |
|
1612 | - header: An optional header message can be specified. | |
1612 |
|
1613 | |||
1613 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1614 | - local_ns, global_ns: working namespaces. If given as None, the | |
1614 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1615 | IPython-initialized one is updated with __main__.__dict__, so that | |
1615 | program variables become visible but user-specific configuration |
|
1616 | program variables become visible but user-specific configuration | |
1616 | remains possible. |
|
1617 | remains possible. | |
1617 |
|
1618 | |||
1618 | - stack_depth: specifies how many levels in the stack to go to |
|
1619 | - stack_depth: specifies how many levels in the stack to go to | |
1619 | looking for namespaces (when local_ns and global_ns are None). This |
|
1620 | looking for namespaces (when local_ns and global_ns are None). This | |
1620 | allows an intermediate caller to make sure that this function gets |
|
1621 | allows an intermediate caller to make sure that this function gets | |
1621 | the namespace from the intended level in the stack. By default (0) |
|
1622 | the namespace from the intended level in the stack. By default (0) | |
1622 | it will get its locals and globals from the immediate caller. |
|
1623 | it will get its locals and globals from the immediate caller. | |
1623 |
|
1624 | |||
1624 | Warning: it's possible to use this in a program which is being run by |
|
1625 | Warning: it's possible to use this in a program which is being run by | |
1625 | IPython itself (via %run), but some funny things will happen (a few |
|
1626 | IPython itself (via %run), but some funny things will happen (a few | |
1626 | globals get overwritten). In the future this will be cleaned up, as |
|
1627 | globals get overwritten). In the future this will be cleaned up, as | |
1627 | there is no fundamental reason why it can't work perfectly.""" |
|
1628 | there is no fundamental reason why it can't work perfectly.""" | |
1628 |
|
1629 | |||
1629 | # Get locals and globals from caller |
|
1630 | # Get locals and globals from caller | |
1630 | if local_ns is None or global_ns is None: |
|
1631 | if local_ns is None or global_ns is None: | |
1631 | call_frame = sys._getframe(stack_depth).f_back |
|
1632 | call_frame = sys._getframe(stack_depth).f_back | |
1632 |
|
1633 | |||
1633 | if local_ns is None: |
|
1634 | if local_ns is None: | |
1634 | local_ns = call_frame.f_locals |
|
1635 | local_ns = call_frame.f_locals | |
1635 | if global_ns is None: |
|
1636 | if global_ns is None: | |
1636 | global_ns = call_frame.f_globals |
|
1637 | global_ns = call_frame.f_globals | |
1637 |
|
1638 | |||
1638 | # Update namespaces and fire up interpreter |
|
1639 | # Update namespaces and fire up interpreter | |
1639 |
|
1640 | |||
1640 | # The global one is easy, we can just throw it in |
|
1641 | # The global one is easy, we can just throw it in | |
1641 | self.user_global_ns = global_ns |
|
1642 | self.user_global_ns = global_ns | |
1642 |
|
1643 | |||
1643 | # but the user/local one is tricky: ipython needs it to store internal |
|
1644 | # but the user/local one is tricky: ipython needs it to store internal | |
1644 | # data, but we also need the locals. We'll copy locals in the user |
|
1645 | # data, but we also need the locals. We'll copy locals in the user | |
1645 | # one, but will track what got copied so we can delete them at exit. |
|
1646 | # one, but will track what got copied so we can delete them at exit. | |
1646 | # This is so that a later embedded call doesn't see locals from a |
|
1647 | # This is so that a later embedded call doesn't see locals from a | |
1647 | # previous call (which most likely existed in a separate scope). |
|
1648 | # previous call (which most likely existed in a separate scope). | |
1648 | local_varnames = local_ns.keys() |
|
1649 | local_varnames = local_ns.keys() | |
1649 | self.user_ns.update(local_ns) |
|
1650 | self.user_ns.update(local_ns) | |
1650 | #self.user_ns['local_ns'] = local_ns # dbg |
|
1651 | #self.user_ns['local_ns'] = local_ns # dbg | |
1651 |
|
1652 | |||
1652 | # Patch for global embedding to make sure that things don't overwrite |
|
1653 | # Patch for global embedding to make sure that things don't overwrite | |
1653 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1654 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
1654 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1655 | # FIXME. Test this a bit more carefully (the if.. is new) | |
1655 | if local_ns is None and global_ns is None: |
|
1656 | if local_ns is None and global_ns is None: | |
1656 | self.user_global_ns.update(__main__.__dict__) |
|
1657 | self.user_global_ns.update(__main__.__dict__) | |
1657 |
|
1658 | |||
1658 | # make sure the tab-completer has the correct frame information, so it |
|
1659 | # make sure the tab-completer has the correct frame information, so it | |
1659 | # actually completes using the frame's locals/globals |
|
1660 | # actually completes using the frame's locals/globals | |
1660 | self.set_completer_frame() |
|
1661 | self.set_completer_frame() | |
1661 |
|
1662 | |||
1662 | # before activating the interactive mode, we need to make sure that |
|
1663 | # before activating the interactive mode, we need to make sure that | |
1663 | # all names in the builtin namespace needed by ipython point to |
|
1664 | # all names in the builtin namespace needed by ipython point to | |
1664 | # ourselves, and not to other instances. |
|
1665 | # ourselves, and not to other instances. | |
1665 | self.add_builtins() |
|
1666 | self.add_builtins() | |
1666 |
|
1667 | |||
1667 | self.interact(header) |
|
1668 | self.interact(header) | |
1668 |
|
1669 | |||
1669 | # now, purge out the user namespace from anything we might have added |
|
1670 | # now, purge out the user namespace from anything we might have added | |
1670 | # from the caller's local namespace |
|
1671 | # from the caller's local namespace | |
1671 | delvar = self.user_ns.pop |
|
1672 | delvar = self.user_ns.pop | |
1672 | for var in local_varnames: |
|
1673 | for var in local_varnames: | |
1673 | delvar(var,None) |
|
1674 | delvar(var,None) | |
1674 | # and clean builtins we may have overridden |
|
1675 | # and clean builtins we may have overridden | |
1675 | self.clean_builtins() |
|
1676 | self.clean_builtins() | |
1676 |
|
1677 | |||
1677 | def interact_prompt(self): |
|
1678 | def interact_prompt(self): | |
1678 | """ Print the prompt (in read-eval-print loop) |
|
1679 | """ Print the prompt (in read-eval-print loop) | |
1679 |
|
1680 | |||
1680 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1681 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1681 | used in standard IPython flow. |
|
1682 | used in standard IPython flow. | |
1682 | """ |
|
1683 | """ | |
1683 | if self.more: |
|
1684 | if self.more: | |
1684 | try: |
|
1685 | try: | |
1685 | prompt = self.hooks.generate_prompt(True) |
|
1686 | prompt = self.hooks.generate_prompt(True) | |
1686 | except: |
|
1687 | except: | |
1687 | self.showtraceback() |
|
1688 | self.showtraceback() | |
1688 | if self.autoindent: |
|
1689 | if self.autoindent: | |
1689 | self.rl_do_indent = True |
|
1690 | self.rl_do_indent = True | |
1690 |
|
1691 | |||
1691 | else: |
|
1692 | else: | |
1692 | try: |
|
1693 | try: | |
1693 | prompt = self.hooks.generate_prompt(False) |
|
1694 | prompt = self.hooks.generate_prompt(False) | |
1694 | except: |
|
1695 | except: | |
1695 | self.showtraceback() |
|
1696 | self.showtraceback() | |
1696 | self.write(prompt) |
|
1697 | self.write(prompt) | |
1697 |
|
1698 | |||
1698 | def interact_handle_input(self,line): |
|
1699 | def interact_handle_input(self,line): | |
1699 | """ Handle the input line (in read-eval-print loop) |
|
1700 | """ Handle the input line (in read-eval-print loop) | |
1700 |
|
1701 | |||
1701 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1702 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1702 | used in standard IPython flow. |
|
1703 | used in standard IPython flow. | |
1703 | """ |
|
1704 | """ | |
1704 | if line.lstrip() == line: |
|
1705 | if line.lstrip() == line: | |
1705 | self.shadowhist.add(line.strip()) |
|
1706 | self.shadowhist.add(line.strip()) | |
1706 | lineout = self.prefilter(line,self.more) |
|
1707 | lineout = self.prefilter(line,self.more) | |
1707 |
|
1708 | |||
1708 | if line.strip(): |
|
1709 | if line.strip(): | |
1709 | if self.more: |
|
1710 | if self.more: | |
1710 | self.input_hist_raw[-1] += '%s\n' % line |
|
1711 | self.input_hist_raw[-1] += '%s\n' % line | |
1711 | else: |
|
1712 | else: | |
1712 | self.input_hist_raw.append('%s\n' % line) |
|
1713 | self.input_hist_raw.append('%s\n' % line) | |
1713 |
|
1714 | |||
1714 |
|
1715 | |||
1715 | self.more = self.push(lineout) |
|
1716 | self.more = self.push(lineout) | |
1716 | if (self.SyntaxTB.last_syntax_error and |
|
1717 | if (self.SyntaxTB.last_syntax_error and | |
1717 | self.rc.autoedit_syntax): |
|
1718 | self.rc.autoedit_syntax): | |
1718 | self.edit_syntax_error() |
|
1719 | self.edit_syntax_error() | |
1719 |
|
1720 | |||
1720 | def interact_with_readline(self): |
|
1721 | def interact_with_readline(self): | |
1721 | """ Demo of using interact_handle_input, interact_prompt |
|
1722 | """ Demo of using interact_handle_input, interact_prompt | |
1722 |
|
1723 | |||
1723 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), |
|
1724 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), | |
1724 | it should work like this. |
|
1725 | it should work like this. | |
1725 | """ |
|
1726 | """ | |
1726 | self.readline_startup_hook(self.pre_readline) |
|
1727 | self.readline_startup_hook(self.pre_readline) | |
1727 | while not self.exit_now: |
|
1728 | while not self.exit_now: | |
1728 | self.interact_prompt() |
|
1729 | self.interact_prompt() | |
1729 | if self.more: |
|
1730 | if self.more: | |
1730 | self.rl_do_indent = True |
|
1731 | self.rl_do_indent = True | |
1731 | else: |
|
1732 | else: | |
1732 | self.rl_do_indent = False |
|
1733 | self.rl_do_indent = False | |
1733 | line = raw_input_original().decode(self.stdin_encoding) |
|
1734 | line = raw_input_original().decode(self.stdin_encoding) | |
1734 | self.interact_handle_input(line) |
|
1735 | self.interact_handle_input(line) | |
1735 |
|
1736 | |||
1736 |
|
1737 | |||
1737 | def interact(self, banner=None): |
|
1738 | def interact(self, banner=None): | |
1738 | """Closely emulate the interactive Python console. |
|
1739 | """Closely emulate the interactive Python console. | |
1739 |
|
1740 | |||
1740 | The optional banner argument specify the banner to print |
|
1741 | The optional banner argument specify the banner to print | |
1741 | before the first interaction; by default it prints a banner |
|
1742 | before the first interaction; by default it prints a banner | |
1742 | similar to the one printed by the real Python interpreter, |
|
1743 | similar to the one printed by the real Python interpreter, | |
1743 | followed by the current class name in parentheses (so as not |
|
1744 | followed by the current class name in parentheses (so as not | |
1744 | to confuse this with the real interpreter -- since it's so |
|
1745 | to confuse this with the real interpreter -- since it's so | |
1745 | close!). |
|
1746 | close!). | |
1746 |
|
1747 | |||
1747 | """ |
|
1748 | """ | |
1748 |
|
1749 | |||
1749 | if self.exit_now: |
|
1750 | if self.exit_now: | |
1750 | # batch run -> do not interact |
|
1751 | # batch run -> do not interact | |
1751 | return |
|
1752 | return | |
1752 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1753 | cprt = 'Type "copyright", "credits" or "license" for more information.' | |
1753 | if banner is None: |
|
1754 | if banner is None: | |
1754 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1755 | self.write("Python %s on %s\n%s\n(%s)\n" % | |
1755 | (sys.version, sys.platform, cprt, |
|
1756 | (sys.version, sys.platform, cprt, | |
1756 | self.__class__.__name__)) |
|
1757 | self.__class__.__name__)) | |
1757 | else: |
|
1758 | else: | |
1758 | self.write(banner) |
|
1759 | self.write(banner) | |
1759 |
|
1760 | |||
1760 | more = 0 |
|
1761 | more = 0 | |
1761 |
|
1762 | |||
1762 | # Mark activity in the builtins |
|
1763 | # Mark activity in the builtins | |
1763 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1764 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1764 |
|
1765 | |||
1765 | if self.has_readline: |
|
1766 | if self.has_readline: | |
1766 | self.readline_startup_hook(self.pre_readline) |
|
1767 | self.readline_startup_hook(self.pre_readline) | |
1767 | # exit_now is set by a call to %Exit or %Quit, through the |
|
1768 | # exit_now is set by a call to %Exit or %Quit, through the | |
1768 | # ask_exit callback. |
|
1769 | # ask_exit callback. | |
1769 |
|
1770 | |||
1770 | while not self.exit_now: |
|
1771 | while not self.exit_now: | |
1771 | self.hooks.pre_prompt_hook() |
|
1772 | self.hooks.pre_prompt_hook() | |
1772 | if more: |
|
1773 | if more: | |
1773 | try: |
|
1774 | try: | |
1774 | prompt = self.hooks.generate_prompt(True) |
|
1775 | prompt = self.hooks.generate_prompt(True) | |
1775 | except: |
|
1776 | except: | |
1776 | self.showtraceback() |
|
1777 | self.showtraceback() | |
1777 | if self.autoindent: |
|
1778 | if self.autoindent: | |
1778 | self.rl_do_indent = True |
|
1779 | self.rl_do_indent = True | |
1779 |
|
1780 | |||
1780 | else: |
|
1781 | else: | |
1781 | try: |
|
1782 | try: | |
1782 | prompt = self.hooks.generate_prompt(False) |
|
1783 | prompt = self.hooks.generate_prompt(False) | |
1783 | except: |
|
1784 | except: | |
1784 | self.showtraceback() |
|
1785 | self.showtraceback() | |
1785 | try: |
|
1786 | try: | |
1786 | line = self.raw_input(prompt,more) |
|
1787 | line = self.raw_input(prompt,more) | |
1787 | if self.exit_now: |
|
1788 | if self.exit_now: | |
1788 | # quick exit on sys.std[in|out] close |
|
1789 | # quick exit on sys.std[in|out] close | |
1789 | break |
|
1790 | break | |
1790 | if self.autoindent: |
|
1791 | if self.autoindent: | |
1791 | self.rl_do_indent = False |
|
1792 | self.rl_do_indent = False | |
1792 |
|
1793 | |||
1793 | except KeyboardInterrupt: |
|
1794 | except KeyboardInterrupt: | |
1794 | #double-guard against keyboardinterrupts during kbdint handling |
|
1795 | #double-guard against keyboardinterrupts during kbdint handling | |
1795 | try: |
|
1796 | try: | |
1796 | self.write('\nKeyboardInterrupt\n') |
|
1797 | self.write('\nKeyboardInterrupt\n') | |
1797 | self.resetbuffer() |
|
1798 | self.resetbuffer() | |
1798 | # keep cache in sync with the prompt counter: |
|
1799 | # keep cache in sync with the prompt counter: | |
1799 | self.outputcache.prompt_count -= 1 |
|
1800 | self.outputcache.prompt_count -= 1 | |
1800 |
|
1801 | |||
1801 | if self.autoindent: |
|
1802 | if self.autoindent: | |
1802 | self.indent_current_nsp = 0 |
|
1803 | self.indent_current_nsp = 0 | |
1803 | more = 0 |
|
1804 | more = 0 | |
1804 | except KeyboardInterrupt: |
|
1805 | except KeyboardInterrupt: | |
1805 | pass |
|
1806 | pass | |
1806 | except EOFError: |
|
1807 | except EOFError: | |
1807 | if self.autoindent: |
|
1808 | if self.autoindent: | |
1808 | self.rl_do_indent = False |
|
1809 | self.rl_do_indent = False | |
1809 | self.readline_startup_hook(None) |
|
1810 | self.readline_startup_hook(None) | |
1810 | self.write('\n') |
|
1811 | self.write('\n') | |
1811 | self.exit() |
|
1812 | self.exit() | |
1812 | except bdb.BdbQuit: |
|
1813 | except bdb.BdbQuit: | |
1813 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1814 | warn('The Python debugger has exited with a BdbQuit exception.\n' | |
1814 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1815 | 'Because of how pdb handles the stack, it is impossible\n' | |
1815 | 'for IPython to properly format this particular exception.\n' |
|
1816 | 'for IPython to properly format this particular exception.\n' | |
1816 | 'IPython will resume normal operation.') |
|
1817 | 'IPython will resume normal operation.') | |
1817 | except: |
|
1818 | except: | |
1818 | # exceptions here are VERY RARE, but they can be triggered |
|
1819 | # exceptions here are VERY RARE, but they can be triggered | |
1819 | # asynchronously by signal handlers, for example. |
|
1820 | # asynchronously by signal handlers, for example. | |
1820 | self.showtraceback() |
|
1821 | self.showtraceback() | |
1821 | else: |
|
1822 | else: | |
1822 | more = self.push(line) |
|
1823 | more = self.push(line) | |
1823 | if (self.SyntaxTB.last_syntax_error and |
|
1824 | if (self.SyntaxTB.last_syntax_error and | |
1824 | self.rc.autoedit_syntax): |
|
1825 | self.rc.autoedit_syntax): | |
1825 | self.edit_syntax_error() |
|
1826 | self.edit_syntax_error() | |
1826 |
|
1827 | |||
1827 | # We are off again... |
|
1828 | # We are off again... | |
1828 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1829 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1829 |
|
1830 | |||
1830 | def excepthook(self, etype, value, tb): |
|
1831 | def excepthook(self, etype, value, tb): | |
1831 | """One more defense for GUI apps that call sys.excepthook. |
|
1832 | """One more defense for GUI apps that call sys.excepthook. | |
1832 |
|
1833 | |||
1833 | GUI frameworks like wxPython trap exceptions and call |
|
1834 | GUI frameworks like wxPython trap exceptions and call | |
1834 | sys.excepthook themselves. I guess this is a feature that |
|
1835 | sys.excepthook themselves. I guess this is a feature that | |
1835 | enables them to keep running after exceptions that would |
|
1836 | enables them to keep running after exceptions that would | |
1836 | otherwise kill their mainloop. This is a bother for IPython |
|
1837 | otherwise kill their mainloop. This is a bother for IPython | |
1837 | which excepts to catch all of the program exceptions with a try: |
|
1838 | which excepts to catch all of the program exceptions with a try: | |
1838 | except: statement. |
|
1839 | except: statement. | |
1839 |
|
1840 | |||
1840 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1841 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1841 | any app directly invokes sys.excepthook, it will look to the user like |
|
1842 | any app directly invokes sys.excepthook, it will look to the user like | |
1842 | IPython crashed. In order to work around this, we can disable the |
|
1843 | IPython crashed. In order to work around this, we can disable the | |
1843 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1844 | CrashHandler and replace it with this excepthook instead, which prints a | |
1844 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1845 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1845 | call sys.excepthook will generate a regular-looking exception from |
|
1846 | call sys.excepthook will generate a regular-looking exception from | |
1846 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1847 | IPython, and the CrashHandler will only be triggered by real IPython | |
1847 | crashes. |
|
1848 | crashes. | |
1848 |
|
1849 | |||
1849 | This hook should be used sparingly, only in places which are not likely |
|
1850 | This hook should be used sparingly, only in places which are not likely | |
1850 | to be true IPython errors. |
|
1851 | to be true IPython errors. | |
1851 | """ |
|
1852 | """ | |
1852 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1853 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1853 |
|
1854 | |||
1854 | def expand_aliases(self,fn,rest): |
|
1855 | def expand_aliases(self,fn,rest): | |
1855 | """ Expand multiple levels of aliases: |
|
1856 | """ Expand multiple levels of aliases: | |
1856 |
|
1857 | |||
1857 | if: |
|
1858 | if: | |
1858 |
|
1859 | |||
1859 | alias foo bar /tmp |
|
1860 | alias foo bar /tmp | |
1860 | alias baz foo |
|
1861 | alias baz foo | |
1861 |
|
1862 | |||
1862 | then: |
|
1863 | then: | |
1863 |
|
1864 | |||
1864 | baz huhhahhei -> bar /tmp huhhahhei |
|
1865 | baz huhhahhei -> bar /tmp huhhahhei | |
1865 |
|
1866 | |||
1866 | """ |
|
1867 | """ | |
1867 | line = fn + " " + rest |
|
1868 | line = fn + " " + rest | |
1868 |
|
1869 | |||
1869 | done = Set() |
|
1870 | done = Set() | |
1870 | while 1: |
|
1871 | while 1: | |
1871 | pre,fn,rest = prefilter.splitUserInput(line, |
|
1872 | pre,fn,rest = prefilter.splitUserInput(line, | |
1872 | prefilter.shell_line_split) |
|
1873 | prefilter.shell_line_split) | |
1873 | if fn in self.alias_table: |
|
1874 | if fn in self.alias_table: | |
1874 | if fn in done: |
|
1875 | if fn in done: | |
1875 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1876 | warn("Cyclic alias definition, repeated '%s'" % fn) | |
1876 | return "" |
|
1877 | return "" | |
1877 | done.add(fn) |
|
1878 | done.add(fn) | |
1878 |
|
1879 | |||
1879 | l2 = self.transform_alias(fn,rest) |
|
1880 | l2 = self.transform_alias(fn,rest) | |
1880 | # dir -> dir |
|
1881 | # dir -> dir | |
1881 | # print "alias",line, "->",l2 #dbg |
|
1882 | # print "alias",line, "->",l2 #dbg | |
1882 | if l2 == line: |
|
1883 | if l2 == line: | |
1883 | break |
|
1884 | break | |
1884 | # ls -> ls -F should not recurse forever |
|
1885 | # ls -> ls -F should not recurse forever | |
1885 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1886 | if l2.split(None,1)[0] == line.split(None,1)[0]: | |
1886 | line = l2 |
|
1887 | line = l2 | |
1887 | break |
|
1888 | break | |
1888 |
|
1889 | |||
1889 | line=l2 |
|
1890 | line=l2 | |
1890 |
|
1891 | |||
1891 |
|
1892 | |||
1892 | # print "al expand to",line #dbg |
|
1893 | # print "al expand to",line #dbg | |
1893 | else: |
|
1894 | else: | |
1894 | break |
|
1895 | break | |
1895 |
|
1896 | |||
1896 | return line |
|
1897 | return line | |
1897 |
|
1898 | |||
1898 | def transform_alias(self, alias,rest=''): |
|
1899 | def transform_alias(self, alias,rest=''): | |
1899 | """ Transform alias to system command string. |
|
1900 | """ Transform alias to system command string. | |
1900 | """ |
|
1901 | """ | |
1901 | trg = self.alias_table[alias] |
|
1902 | trg = self.alias_table[alias] | |
1902 |
|
1903 | |||
1903 | nargs,cmd = trg |
|
1904 | nargs,cmd = trg | |
1904 | # print trg #dbg |
|
1905 | # print trg #dbg | |
1905 | if ' ' in cmd and os.path.isfile(cmd): |
|
1906 | if ' ' in cmd and os.path.isfile(cmd): | |
1906 | cmd = '"%s"' % cmd |
|
1907 | cmd = '"%s"' % cmd | |
1907 |
|
1908 | |||
1908 | # Expand the %l special to be the user's input line |
|
1909 | # Expand the %l special to be the user's input line | |
1909 | if cmd.find('%l') >= 0: |
|
1910 | if cmd.find('%l') >= 0: | |
1910 | cmd = cmd.replace('%l',rest) |
|
1911 | cmd = cmd.replace('%l',rest) | |
1911 | rest = '' |
|
1912 | rest = '' | |
1912 | if nargs==0: |
|
1913 | if nargs==0: | |
1913 | # Simple, argument-less aliases |
|
1914 | # Simple, argument-less aliases | |
1914 | cmd = '%s %s' % (cmd,rest) |
|
1915 | cmd = '%s %s' % (cmd,rest) | |
1915 | else: |
|
1916 | else: | |
1916 | # Handle aliases with positional arguments |
|
1917 | # Handle aliases with positional arguments | |
1917 | args = rest.split(None,nargs) |
|
1918 | args = rest.split(None,nargs) | |
1918 | if len(args)< nargs: |
|
1919 | if len(args)< nargs: | |
1919 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1920 | error('Alias <%s> requires %s arguments, %s given.' % | |
1920 | (alias,nargs,len(args))) |
|
1921 | (alias,nargs,len(args))) | |
1921 | return None |
|
1922 | return None | |
1922 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1923 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) | |
1923 | # Now call the macro, evaluating in the user's namespace |
|
1924 | # Now call the macro, evaluating in the user's namespace | |
1924 | #print 'new command: <%r>' % cmd # dbg |
|
1925 | #print 'new command: <%r>' % cmd # dbg | |
1925 | return cmd |
|
1926 | return cmd | |
1926 |
|
1927 | |||
1927 | def call_alias(self,alias,rest=''): |
|
1928 | def call_alias(self,alias,rest=''): | |
1928 | """Call an alias given its name and the rest of the line. |
|
1929 | """Call an alias given its name and the rest of the line. | |
1929 |
|
1930 | |||
1930 | This is only used to provide backwards compatibility for users of |
|
1931 | This is only used to provide backwards compatibility for users of | |
1931 | ipalias(), use of which is not recommended for anymore.""" |
|
1932 | ipalias(), use of which is not recommended for anymore.""" | |
1932 |
|
1933 | |||
1933 | # Now call the macro, evaluating in the user's namespace |
|
1934 | # Now call the macro, evaluating in the user's namespace | |
1934 | cmd = self.transform_alias(alias, rest) |
|
1935 | cmd = self.transform_alias(alias, rest) | |
1935 | try: |
|
1936 | try: | |
1936 | self.system(cmd) |
|
1937 | self.system(cmd) | |
1937 | except: |
|
1938 | except: | |
1938 | self.showtraceback() |
|
1939 | self.showtraceback() | |
1939 |
|
1940 | |||
1940 | def indent_current_str(self): |
|
1941 | def indent_current_str(self): | |
1941 | """return the current level of indentation as a string""" |
|
1942 | """return the current level of indentation as a string""" | |
1942 | return self.indent_current_nsp * ' ' |
|
1943 | return self.indent_current_nsp * ' ' | |
1943 |
|
1944 | |||
1944 | def autoindent_update(self,line): |
|
1945 | def autoindent_update(self,line): | |
1945 | """Keep track of the indent level.""" |
|
1946 | """Keep track of the indent level.""" | |
1946 |
|
1947 | |||
1947 | #debugx('line') |
|
1948 | #debugx('line') | |
1948 | #debugx('self.indent_current_nsp') |
|
1949 | #debugx('self.indent_current_nsp') | |
1949 | if self.autoindent: |
|
1950 | if self.autoindent: | |
1950 | if line: |
|
1951 | if line: | |
1951 | inisp = num_ini_spaces(line) |
|
1952 | inisp = num_ini_spaces(line) | |
1952 | if inisp < self.indent_current_nsp: |
|
1953 | if inisp < self.indent_current_nsp: | |
1953 | self.indent_current_nsp = inisp |
|
1954 | self.indent_current_nsp = inisp | |
1954 |
|
1955 | |||
1955 | if line[-1] == ':': |
|
1956 | if line[-1] == ':': | |
1956 | self.indent_current_nsp += 4 |
|
1957 | self.indent_current_nsp += 4 | |
1957 | elif dedent_re.match(line): |
|
1958 | elif dedent_re.match(line): | |
1958 | self.indent_current_nsp -= 4 |
|
1959 | self.indent_current_nsp -= 4 | |
1959 | else: |
|
1960 | else: | |
1960 | self.indent_current_nsp = 0 |
|
1961 | self.indent_current_nsp = 0 | |
1961 |
|
1962 | |||
1962 | def runlines(self,lines): |
|
1963 | def runlines(self,lines): | |
1963 | """Run a string of one or more lines of source. |
|
1964 | """Run a string of one or more lines of source. | |
1964 |
|
1965 | |||
1965 | This method is capable of running a string containing multiple source |
|
1966 | This method is capable of running a string containing multiple source | |
1966 | lines, as if they had been entered at the IPython prompt. Since it |
|
1967 | lines, as if they had been entered at the IPython prompt. Since it | |
1967 | exposes IPython's processing machinery, the given strings can contain |
|
1968 | exposes IPython's processing machinery, the given strings can contain | |
1968 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1969 | magic calls (%magic), special shell access (!cmd), etc.""" | |
1969 |
|
1970 | |||
1970 | # We must start with a clean buffer, in case this is run from an |
|
1971 | # We must start with a clean buffer, in case this is run from an | |
1971 | # interactive IPython session (via a magic, for example). |
|
1972 | # interactive IPython session (via a magic, for example). | |
1972 | self.resetbuffer() |
|
1973 | self.resetbuffer() | |
1973 | lines = lines.split('\n') |
|
1974 | lines = lines.split('\n') | |
1974 | more = 0 |
|
1975 | more = 0 | |
1975 |
|
1976 | |||
1976 | for line in lines: |
|
1977 | for line in lines: | |
1977 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1978 | # skip blank lines so we don't mess up the prompt counter, but do | |
1978 | # NOT skip even a blank line if we are in a code block (more is |
|
1979 | # NOT skip even a blank line if we are in a code block (more is | |
1979 | # true) |
|
1980 | # true) | |
1980 |
|
1981 | |||
1981 |
|
1982 | |||
1982 | if line or more: |
|
1983 | if line or more: | |
1983 | # push to raw history, so hist line numbers stay in sync |
|
1984 | # push to raw history, so hist line numbers stay in sync | |
1984 | self.input_hist_raw.append("# " + line + "\n") |
|
1985 | self.input_hist_raw.append("# " + line + "\n") | |
1985 | more = self.push(self.prefilter(line,more)) |
|
1986 | more = self.push(self.prefilter(line,more)) | |
1986 | # IPython's runsource returns None if there was an error |
|
1987 | # IPython's runsource returns None if there was an error | |
1987 | # compiling the code. This allows us to stop processing right |
|
1988 | # compiling the code. This allows us to stop processing right | |
1988 | # away, so the user gets the error message at the right place. |
|
1989 | # away, so the user gets the error message at the right place. | |
1989 | if more is None: |
|
1990 | if more is None: | |
1990 | break |
|
1991 | break | |
1991 | else: |
|
1992 | else: | |
1992 | self.input_hist_raw.append("\n") |
|
1993 | self.input_hist_raw.append("\n") | |
1993 | # final newline in case the input didn't have it, so that the code |
|
1994 | # final newline in case the input didn't have it, so that the code | |
1994 | # actually does get executed |
|
1995 | # actually does get executed | |
1995 | if more: |
|
1996 | if more: | |
1996 | self.push('\n') |
|
1997 | self.push('\n') | |
1997 |
|
1998 | |||
1998 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1999 | def runsource(self, source, filename='<input>', symbol='single'): | |
1999 | """Compile and run some source in the interpreter. |
|
2000 | """Compile and run some source in the interpreter. | |
2000 |
|
2001 | |||
2001 | Arguments are as for compile_command(). |
|
2002 | Arguments are as for compile_command(). | |
2002 |
|
2003 | |||
2003 | One several things can happen: |
|
2004 | One several things can happen: | |
2004 |
|
2005 | |||
2005 | 1) The input is incorrect; compile_command() raised an |
|
2006 | 1) The input is incorrect; compile_command() raised an | |
2006 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2007 | exception (SyntaxError or OverflowError). A syntax traceback | |
2007 | will be printed by calling the showsyntaxerror() method. |
|
2008 | will be printed by calling the showsyntaxerror() method. | |
2008 |
|
2009 | |||
2009 | 2) The input is incomplete, and more input is required; |
|
2010 | 2) The input is incomplete, and more input is required; | |
2010 | compile_command() returned None. Nothing happens. |
|
2011 | compile_command() returned None. Nothing happens. | |
2011 |
|
2012 | |||
2012 | 3) The input is complete; compile_command() returned a code |
|
2013 | 3) The input is complete; compile_command() returned a code | |
2013 | object. The code is executed by calling self.runcode() (which |
|
2014 | object. The code is executed by calling self.runcode() (which | |
2014 | also handles run-time exceptions, except for SystemExit). |
|
2015 | also handles run-time exceptions, except for SystemExit). | |
2015 |
|
2016 | |||
2016 | The return value is: |
|
2017 | The return value is: | |
2017 |
|
2018 | |||
2018 | - True in case 2 |
|
2019 | - True in case 2 | |
2019 |
|
2020 | |||
2020 | - False in the other cases, unless an exception is raised, where |
|
2021 | - False in the other cases, unless an exception is raised, where | |
2021 | None is returned instead. This can be used by external callers to |
|
2022 | None is returned instead. This can be used by external callers to | |
2022 | know whether to continue feeding input or not. |
|
2023 | know whether to continue feeding input or not. | |
2023 |
|
2024 | |||
2024 | The return value can be used to decide whether to use sys.ps1 or |
|
2025 | The return value can be used to decide whether to use sys.ps1 or | |
2025 | sys.ps2 to prompt the next line.""" |
|
2026 | sys.ps2 to prompt the next line.""" | |
2026 |
|
2027 | |||
2027 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
2028 | # if the source code has leading blanks, add 'if 1:\n' to it | |
2028 | # this allows execution of indented pasted code. It is tempting |
|
2029 | # this allows execution of indented pasted code. It is tempting | |
2029 | # to add '\n' at the end of source to run commands like ' a=1' |
|
2030 | # to add '\n' at the end of source to run commands like ' a=1' | |
2030 | # directly, but this fails for more complicated scenarios |
|
2031 | # directly, but this fails for more complicated scenarios | |
2031 | source=source.encode(self.stdin_encoding) |
|
2032 | source=source.encode(self.stdin_encoding) | |
2032 | if source[:1] in [' ', '\t']: |
|
2033 | if source[:1] in [' ', '\t']: | |
2033 | source = 'if 1:\n%s' % source |
|
2034 | source = 'if 1:\n%s' % source | |
2034 |
|
2035 | |||
2035 | try: |
|
2036 | try: | |
2036 | code = self.compile(source,filename,symbol) |
|
2037 | code = self.compile(source,filename,symbol) | |
2037 | except (OverflowError, SyntaxError, ValueError, TypeError): |
|
2038 | except (OverflowError, SyntaxError, ValueError, TypeError): | |
2038 | # Case 1 |
|
2039 | # Case 1 | |
2039 | self.showsyntaxerror(filename) |
|
2040 | self.showsyntaxerror(filename) | |
2040 | return None |
|
2041 | return None | |
2041 |
|
2042 | |||
2042 | if code is None: |
|
2043 | if code is None: | |
2043 | # Case 2 |
|
2044 | # Case 2 | |
2044 | return True |
|
2045 | return True | |
2045 |
|
2046 | |||
2046 | # Case 3 |
|
2047 | # Case 3 | |
2047 | # We store the code object so that threaded shells and |
|
2048 | # We store the code object so that threaded shells and | |
2048 | # custom exception handlers can access all this info if needed. |
|
2049 | # custom exception handlers can access all this info if needed. | |
2049 | # The source corresponding to this can be obtained from the |
|
2050 | # The source corresponding to this can be obtained from the | |
2050 | # buffer attribute as '\n'.join(self.buffer). |
|
2051 | # buffer attribute as '\n'.join(self.buffer). | |
2051 | self.code_to_run = code |
|
2052 | self.code_to_run = code | |
2052 | # now actually execute the code object |
|
2053 | # now actually execute the code object | |
2053 | if self.runcode(code) == 0: |
|
2054 | if self.runcode(code) == 0: | |
2054 | return False |
|
2055 | return False | |
2055 | else: |
|
2056 | else: | |
2056 | return None |
|
2057 | return None | |
2057 |
|
2058 | |||
2058 | def runcode(self,code_obj): |
|
2059 | def runcode(self,code_obj): | |
2059 | """Execute a code object. |
|
2060 | """Execute a code object. | |
2060 |
|
2061 | |||
2061 | When an exception occurs, self.showtraceback() is called to display a |
|
2062 | When an exception occurs, self.showtraceback() is called to display a | |
2062 | traceback. |
|
2063 | traceback. | |
2063 |
|
2064 | |||
2064 | Return value: a flag indicating whether the code to be run completed |
|
2065 | Return value: a flag indicating whether the code to be run completed | |
2065 | successfully: |
|
2066 | successfully: | |
2066 |
|
2067 | |||
2067 | - 0: successful execution. |
|
2068 | - 0: successful execution. | |
2068 | - 1: an error occurred. |
|
2069 | - 1: an error occurred. | |
2069 | """ |
|
2070 | """ | |
2070 |
|
2071 | |||
2071 | # Set our own excepthook in case the user code tries to call it |
|
2072 | # Set our own excepthook in case the user code tries to call it | |
2072 | # directly, so that the IPython crash handler doesn't get triggered |
|
2073 | # directly, so that the IPython crash handler doesn't get triggered | |
2073 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2074 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2074 |
|
2075 | |||
2075 | # we save the original sys.excepthook in the instance, in case config |
|
2076 | # we save the original sys.excepthook in the instance, in case config | |
2076 | # code (such as magics) needs access to it. |
|
2077 | # code (such as magics) needs access to it. | |
2077 | self.sys_excepthook = old_excepthook |
|
2078 | self.sys_excepthook = old_excepthook | |
2078 | outflag = 1 # happens in more places, so it's easier as default |
|
2079 | outflag = 1 # happens in more places, so it's easier as default | |
2079 | try: |
|
2080 | try: | |
2080 | try: |
|
2081 | try: | |
2081 | self.hooks.pre_runcode_hook() |
|
2082 | self.hooks.pre_runcode_hook() | |
2082 | exec code_obj in self.user_global_ns, self.user_ns |
|
2083 | exec code_obj in self.user_global_ns, self.user_ns | |
2083 | finally: |
|
2084 | finally: | |
2084 | # Reset our crash handler in place |
|
2085 | # Reset our crash handler in place | |
2085 | sys.excepthook = old_excepthook |
|
2086 | sys.excepthook = old_excepthook | |
2086 | except SystemExit: |
|
2087 | except SystemExit: | |
2087 | self.resetbuffer() |
|
2088 | self.resetbuffer() | |
2088 | self.showtraceback() |
|
2089 | self.showtraceback() | |
2089 | warn("Type %exit or %quit to exit IPython " |
|
2090 | warn("Type %exit or %quit to exit IPython " | |
2090 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
2091 | "(%Exit or %Quit do so unconditionally).",level=1) | |
2091 | except self.custom_exceptions: |
|
2092 | except self.custom_exceptions: | |
2092 | etype,value,tb = sys.exc_info() |
|
2093 | etype,value,tb = sys.exc_info() | |
2093 | self.CustomTB(etype,value,tb) |
|
2094 | self.CustomTB(etype,value,tb) | |
2094 | except: |
|
2095 | except: | |
2095 | self.showtraceback() |
|
2096 | self.showtraceback() | |
2096 | else: |
|
2097 | else: | |
2097 | outflag = 0 |
|
2098 | outflag = 0 | |
2098 | if softspace(sys.stdout, 0): |
|
2099 | if softspace(sys.stdout, 0): | |
2099 |
|
2100 | |||
2100 | # Flush out code object which has been run (and source) |
|
2101 | # Flush out code object which has been run (and source) | |
2101 | self.code_to_run = None |
|
2102 | self.code_to_run = None | |
2102 | return outflag |
|
2103 | return outflag | |
2103 |
|
2104 | |||
2104 | def push(self, line): |
|
2105 | def push(self, line): | |
2105 | """Push a line to the interpreter. |
|
2106 | """Push a line to the interpreter. | |
2106 |
|
2107 | |||
2107 | The line should not have a trailing newline; it may have |
|
2108 | The line should not have a trailing newline; it may have | |
2108 | internal newlines. The line is appended to a buffer and the |
|
2109 | internal newlines. The line is appended to a buffer and the | |
2109 | interpreter's runsource() method is called with the |
|
2110 | interpreter's runsource() method is called with the | |
2110 | concatenated contents of the buffer as source. If this |
|
2111 | concatenated contents of the buffer as source. If this | |
2111 | indicates that the command was executed or invalid, the buffer |
|
2112 | indicates that the command was executed or invalid, the buffer | |
2112 | is reset; otherwise, the command is incomplete, and the buffer |
|
2113 | is reset; otherwise, the command is incomplete, and the buffer | |
2113 | is left as it was after the line was appended. The return |
|
2114 | is left as it was after the line was appended. The return | |
2114 | value is 1 if more input is required, 0 if the line was dealt |
|
2115 | value is 1 if more input is required, 0 if the line was dealt | |
2115 | with in some way (this is the same as runsource()). |
|
2116 | with in some way (this is the same as runsource()). | |
2116 | """ |
|
2117 | """ | |
2117 |
|
2118 | |||
2118 | # autoindent management should be done here, and not in the |
|
2119 | # autoindent management should be done here, and not in the | |
2119 | # interactive loop, since that one is only seen by keyboard input. We |
|
2120 | # interactive loop, since that one is only seen by keyboard input. We | |
2120 | # need this done correctly even for code run via runlines (which uses |
|
2121 | # need this done correctly even for code run via runlines (which uses | |
2121 | # push). |
|
2122 | # push). | |
2122 |
|
2123 | |||
2123 | #print 'push line: <%s>' % line # dbg |
|
2124 | #print 'push line: <%s>' % line # dbg | |
2124 | for subline in line.splitlines(): |
|
2125 | for subline in line.splitlines(): | |
2125 | self.autoindent_update(subline) |
|
2126 | self.autoindent_update(subline) | |
2126 | self.buffer.append(line) |
|
2127 | self.buffer.append(line) | |
2127 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2128 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
2128 | if not more: |
|
2129 | if not more: | |
2129 | self.resetbuffer() |
|
2130 | self.resetbuffer() | |
2130 | return more |
|
2131 | return more | |
2131 |
|
2132 | |||
2132 | def split_user_input(self, line): |
|
2133 | def split_user_input(self, line): | |
2133 | # This is really a hold-over to support ipapi and some extensions |
|
2134 | # This is really a hold-over to support ipapi and some extensions | |
2134 | return prefilter.splitUserInput(line) |
|
2135 | return prefilter.splitUserInput(line) | |
2135 |
|
2136 | |||
2136 | def resetbuffer(self): |
|
2137 | def resetbuffer(self): | |
2137 | """Reset the input buffer.""" |
|
2138 | """Reset the input buffer.""" | |
2138 | self.buffer[:] = [] |
|
2139 | self.buffer[:] = [] | |
2139 |
|
2140 | |||
2140 | def raw_input(self,prompt='',continue_prompt=False): |
|
2141 | def raw_input(self,prompt='',continue_prompt=False): | |
2141 | """Write a prompt and read a line. |
|
2142 | """Write a prompt and read a line. | |
2142 |
|
2143 | |||
2143 | The returned line does not include the trailing newline. |
|
2144 | The returned line does not include the trailing newline. | |
2144 | When the user enters the EOF key sequence, EOFError is raised. |
|
2145 | When the user enters the EOF key sequence, EOFError is raised. | |
2145 |
|
2146 | |||
2146 | Optional inputs: |
|
2147 | Optional inputs: | |
2147 |
|
2148 | |||
2148 | - prompt(''): a string to be printed to prompt the user. |
|
2149 | - prompt(''): a string to be printed to prompt the user. | |
2149 |
|
2150 | |||
2150 | - continue_prompt(False): whether this line is the first one or a |
|
2151 | - continue_prompt(False): whether this line is the first one or a | |
2151 | continuation in a sequence of inputs. |
|
2152 | continuation in a sequence of inputs. | |
2152 | """ |
|
2153 | """ | |
2153 |
|
2154 | |||
2154 | # Code run by the user may have modified the readline completer state. |
|
2155 | # Code run by the user may have modified the readline completer state. | |
2155 | # We must ensure that our completer is back in place. |
|
2156 | # We must ensure that our completer is back in place. | |
2156 | if self.has_readline: |
|
2157 | if self.has_readline: | |
2157 | self.set_completer() |
|
2158 | self.set_completer() | |
2158 |
|
2159 | |||
2159 | try: |
|
2160 | try: | |
2160 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
2161 | line = raw_input_original(prompt).decode(self.stdin_encoding) | |
2161 | except ValueError: |
|
2162 | except ValueError: | |
2162 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
2163 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
2163 | " or sys.stdout.close()!\nExiting IPython!") |
|
2164 | " or sys.stdout.close()!\nExiting IPython!") | |
2164 | self.ask_exit() |
|
2165 | self.ask_exit() | |
2165 | return "" |
|
2166 | return "" | |
2166 |
|
2167 | |||
2167 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2168 | # Try to be reasonably smart about not re-indenting pasted input more | |
2168 | # than necessary. We do this by trimming out the auto-indent initial |
|
2169 | # than necessary. We do this by trimming out the auto-indent initial | |
2169 | # spaces, if the user's actual input started itself with whitespace. |
|
2170 | # spaces, if the user's actual input started itself with whitespace. | |
2170 | #debugx('self.buffer[-1]') |
|
2171 | #debugx('self.buffer[-1]') | |
2171 |
|
2172 | |||
2172 | if self.autoindent: |
|
2173 | if self.autoindent: | |
2173 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2174 | if num_ini_spaces(line) > self.indent_current_nsp: | |
2174 | line = line[self.indent_current_nsp:] |
|
2175 | line = line[self.indent_current_nsp:] | |
2175 | self.indent_current_nsp = 0 |
|
2176 | self.indent_current_nsp = 0 | |
2176 |
|
2177 | |||
2177 | # store the unfiltered input before the user has any chance to modify |
|
2178 | # store the unfiltered input before the user has any chance to modify | |
2178 | # it. |
|
2179 | # it. | |
2179 | if line.strip(): |
|
2180 | if line.strip(): | |
2180 | if continue_prompt: |
|
2181 | if continue_prompt: | |
2181 | self.input_hist_raw[-1] += '%s\n' % line |
|
2182 | self.input_hist_raw[-1] += '%s\n' % line | |
2182 | if self.has_readline: # and some config option is set? |
|
2183 | if self.has_readline: # and some config option is set? | |
2183 | try: |
|
2184 | try: | |
2184 | histlen = self.readline.get_current_history_length() |
|
2185 | histlen = self.readline.get_current_history_length() | |
2185 | if histlen > 1: |
|
2186 | if histlen > 1: | |
2186 | newhist = self.input_hist_raw[-1].rstrip() |
|
2187 | newhist = self.input_hist_raw[-1].rstrip() | |
2187 | self.readline.remove_history_item(histlen-1) |
|
2188 | self.readline.remove_history_item(histlen-1) | |
2188 | self.readline.replace_history_item(histlen-2, |
|
2189 | self.readline.replace_history_item(histlen-2, | |
2189 | newhist.encode(self.stdin_encoding)) |
|
2190 | newhist.encode(self.stdin_encoding)) | |
2190 | except AttributeError: |
|
2191 | except AttributeError: | |
2191 | pass # re{move,place}_history_item are new in 2.4. |
|
2192 | pass # re{move,place}_history_item are new in 2.4. | |
2192 | else: |
|
2193 | else: | |
2193 | self.input_hist_raw.append('%s\n' % line) |
|
2194 | self.input_hist_raw.append('%s\n' % line) | |
2194 | # only entries starting at first column go to shadow history |
|
2195 | # only entries starting at first column go to shadow history | |
2195 | if line.lstrip() == line: |
|
2196 | if line.lstrip() == line: | |
2196 | self.shadowhist.add(line.strip()) |
|
2197 | self.shadowhist.add(line.strip()) | |
2197 | elif not continue_prompt: |
|
2198 | elif not continue_prompt: | |
2198 | self.input_hist_raw.append('\n') |
|
2199 | self.input_hist_raw.append('\n') | |
2199 | try: |
|
2200 | try: | |
2200 | lineout = self.prefilter(line,continue_prompt) |
|
2201 | lineout = self.prefilter(line,continue_prompt) | |
2201 | except: |
|
2202 | except: | |
2202 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2203 | # blanket except, in case a user-defined prefilter crashes, so it | |
2203 | # can't take all of ipython with it. |
|
2204 | # can't take all of ipython with it. | |
2204 | self.showtraceback() |
|
2205 | self.showtraceback() | |
2205 | return '' |
|
2206 | return '' | |
2206 | else: |
|
2207 | else: | |
2207 | return lineout |
|
2208 | return lineout | |
2208 |
|
2209 | |||
2209 | def _prefilter(self, line, continue_prompt): |
|
2210 | def _prefilter(self, line, continue_prompt): | |
2210 | """Calls different preprocessors, depending on the form of line.""" |
|
2211 | """Calls different preprocessors, depending on the form of line.""" | |
2211 |
|
2212 | |||
2212 | # All handlers *must* return a value, even if it's blank (''). |
|
2213 | # All handlers *must* return a value, even if it's blank (''). | |
2213 |
|
2214 | |||
2214 | # Lines are NOT logged here. Handlers should process the line as |
|
2215 | # Lines are NOT logged here. Handlers should process the line as | |
2215 | # needed, update the cache AND log it (so that the input cache array |
|
2216 | # needed, update the cache AND log it (so that the input cache array | |
2216 | # stays synced). |
|
2217 | # stays synced). | |
2217 |
|
2218 | |||
2218 | #..................................................................... |
|
2219 | #..................................................................... | |
2219 | # Code begins |
|
2220 | # Code begins | |
2220 |
|
2221 | |||
2221 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2222 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg | |
2222 |
|
2223 | |||
2223 | # save the line away in case we crash, so the post-mortem handler can |
|
2224 | # save the line away in case we crash, so the post-mortem handler can | |
2224 | # record it |
|
2225 | # record it | |
2225 | self._last_input_line = line |
|
2226 | self._last_input_line = line | |
2226 |
|
2227 | |||
2227 | #print '***line: <%s>' % line # dbg |
|
2228 | #print '***line: <%s>' % line # dbg | |
2228 |
|
2229 | |||
2229 | if not line: |
|
2230 | if not line: | |
2230 | # Return immediately on purely empty lines, so that if the user |
|
2231 | # Return immediately on purely empty lines, so that if the user | |
2231 | # previously typed some whitespace that started a continuation |
|
2232 | # previously typed some whitespace that started a continuation | |
2232 | # prompt, he can break out of that loop with just an empty line. |
|
2233 | # prompt, he can break out of that loop with just an empty line. | |
2233 | # This is how the default python prompt works. |
|
2234 | # This is how the default python prompt works. | |
2234 |
|
2235 | |||
2235 | # Only return if the accumulated input buffer was just whitespace! |
|
2236 | # Only return if the accumulated input buffer was just whitespace! | |
2236 | if ''.join(self.buffer).isspace(): |
|
2237 | if ''.join(self.buffer).isspace(): | |
2237 | self.buffer[:] = [] |
|
2238 | self.buffer[:] = [] | |
2238 | return '' |
|
2239 | return '' | |
2239 |
|
2240 | |||
2240 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
2241 | line_info = prefilter.LineInfo(line, continue_prompt) | |
2241 |
|
2242 | |||
2242 | # the input history needs to track even empty lines |
|
2243 | # the input history needs to track even empty lines | |
2243 | stripped = line.strip() |
|
2244 | stripped = line.strip() | |
2244 |
|
2245 | |||
2245 | if not stripped: |
|
2246 | if not stripped: | |
2246 | if not continue_prompt: |
|
2247 | if not continue_prompt: | |
2247 | self.outputcache.prompt_count -= 1 |
|
2248 | self.outputcache.prompt_count -= 1 | |
2248 | return self.handle_normal(line_info) |
|
2249 | return self.handle_normal(line_info) | |
2249 |
|
2250 | |||
2250 | # print '***cont',continue_prompt # dbg |
|
2251 | # print '***cont',continue_prompt # dbg | |
2251 | # special handlers are only allowed for single line statements |
|
2252 | # special handlers are only allowed for single line statements | |
2252 | if continue_prompt and not self.rc.multi_line_specials: |
|
2253 | if continue_prompt and not self.rc.multi_line_specials: | |
2253 | return self.handle_normal(line_info) |
|
2254 | return self.handle_normal(line_info) | |
2254 |
|
2255 | |||
2255 |
|
2256 | |||
2256 | # See whether any pre-existing handler can take care of it |
|
2257 | # See whether any pre-existing handler can take care of it | |
2257 | rewritten = self.hooks.input_prefilter(stripped) |
|
2258 | rewritten = self.hooks.input_prefilter(stripped) | |
2258 | if rewritten != stripped: # ok, some prefilter did something |
|
2259 | if rewritten != stripped: # ok, some prefilter did something | |
2259 | rewritten = line_info.pre + rewritten # add indentation |
|
2260 | rewritten = line_info.pre + rewritten # add indentation | |
2260 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
2261 | return self.handle_normal(prefilter.LineInfo(rewritten, | |
2261 | continue_prompt)) |
|
2262 | continue_prompt)) | |
2262 |
|
2263 | |||
2263 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2264 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2264 |
|
2265 | |||
2265 | return prefilter.prefilter(line_info, self) |
|
2266 | return prefilter.prefilter(line_info, self) | |
2266 |
|
2267 | |||
2267 |
|
2268 | |||
2268 | def _prefilter_dumb(self, line, continue_prompt): |
|
2269 | def _prefilter_dumb(self, line, continue_prompt): | |
2269 | """simple prefilter function, for debugging""" |
|
2270 | """simple prefilter function, for debugging""" | |
2270 | return self.handle_normal(line,continue_prompt) |
|
2271 | return self.handle_normal(line,continue_prompt) | |
2271 |
|
2272 | |||
2272 |
|
2273 | |||
2273 | def multiline_prefilter(self, line, continue_prompt): |
|
2274 | def multiline_prefilter(self, line, continue_prompt): | |
2274 | """ Run _prefilter for each line of input |
|
2275 | """ Run _prefilter for each line of input | |
2275 |
|
2276 | |||
2276 | Covers cases where there are multiple lines in the user entry, |
|
2277 | Covers cases where there are multiple lines in the user entry, | |
2277 | which is the case when the user goes back to a multiline history |
|
2278 | which is the case when the user goes back to a multiline history | |
2278 | entry and presses enter. |
|
2279 | entry and presses enter. | |
2279 |
|
2280 | |||
2280 | """ |
|
2281 | """ | |
2281 | out = [] |
|
2282 | out = [] | |
2282 | for l in line.rstrip('\n').split('\n'): |
|
2283 | for l in line.rstrip('\n').split('\n'): | |
2283 | out.append(self._prefilter(l, continue_prompt)) |
|
2284 | out.append(self._prefilter(l, continue_prompt)) | |
2284 | return '\n'.join(out) |
|
2285 | return '\n'.join(out) | |
2285 |
|
2286 | |||
2286 | # Set the default prefilter() function (this can be user-overridden) |
|
2287 | # Set the default prefilter() function (this can be user-overridden) | |
2287 | prefilter = multiline_prefilter |
|
2288 | prefilter = multiline_prefilter | |
2288 |
|
2289 | |||
2289 | def handle_normal(self,line_info): |
|
2290 | def handle_normal(self,line_info): | |
2290 | """Handle normal input lines. Use as a template for handlers.""" |
|
2291 | """Handle normal input lines. Use as a template for handlers.""" | |
2291 |
|
2292 | |||
2292 | # With autoindent on, we need some way to exit the input loop, and I |
|
2293 | # With autoindent on, we need some way to exit the input loop, and I | |
2293 | # don't want to force the user to have to backspace all the way to |
|
2294 | # don't want to force the user to have to backspace all the way to | |
2294 | # clear the line. The rule will be in this case, that either two |
|
2295 | # clear the line. The rule will be in this case, that either two | |
2295 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2296 | # lines of pure whitespace in a row, or a line of pure whitespace but | |
2296 | # of a size different to the indent level, will exit the input loop. |
|
2297 | # of a size different to the indent level, will exit the input loop. | |
2297 | line = line_info.line |
|
2298 | line = line_info.line | |
2298 | continue_prompt = line_info.continue_prompt |
|
2299 | continue_prompt = line_info.continue_prompt | |
2299 |
|
2300 | |||
2300 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2301 | if (continue_prompt and self.autoindent and line.isspace() and | |
2301 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2302 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or | |
2302 | (self.buffer[-1]).isspace() )): |
|
2303 | (self.buffer[-1]).isspace() )): | |
2303 | line = '' |
|
2304 | line = '' | |
2304 |
|
2305 | |||
2305 | self.log(line,line,continue_prompt) |
|
2306 | self.log(line,line,continue_prompt) | |
2306 | return line |
|
2307 | return line | |
2307 |
|
2308 | |||
2308 | def handle_alias(self,line_info): |
|
2309 | def handle_alias(self,line_info): | |
2309 | """Handle alias input lines. """ |
|
2310 | """Handle alias input lines. """ | |
2310 | tgt = self.alias_table[line_info.iFun] |
|
2311 | tgt = self.alias_table[line_info.iFun] | |
2311 | # print "=>",tgt #dbg |
|
2312 | # print "=>",tgt #dbg | |
2312 | if callable(tgt): |
|
2313 | if callable(tgt): | |
2313 | if '$' in line_info.line: |
|
2314 | if '$' in line_info.line: | |
2314 | call_meth = '(_ip, _ip.itpl(%s))' |
|
2315 | call_meth = '(_ip, _ip.itpl(%s))' | |
2315 | else: |
|
2316 | else: | |
2316 | call_meth = '(_ip,%s)' |
|
2317 | call_meth = '(_ip,%s)' | |
2317 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, |
|
2318 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, | |
2318 | line_info.iFun, |
|
2319 | line_info.iFun, | |
2319 | make_quoted_expr(line_info.line)) |
|
2320 | make_quoted_expr(line_info.line)) | |
2320 | else: |
|
2321 | else: | |
2321 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2322 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) | |
2322 |
|
2323 | |||
2323 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2324 | # pre is needed, because it carries the leading whitespace. Otherwise | |
2324 | # aliases won't work in indented sections. |
|
2325 | # aliases won't work in indented sections. | |
2325 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2326 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2326 | make_quoted_expr( transformed )) |
|
2327 | make_quoted_expr( transformed )) | |
2327 |
|
2328 | |||
2328 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
2329 | self.log(line_info.line,line_out,line_info.continue_prompt) | |
2329 | #print 'line out:',line_out # dbg |
|
2330 | #print 'line out:',line_out # dbg | |
2330 | return line_out |
|
2331 | return line_out | |
2331 |
|
2332 | |||
2332 | def handle_shell_escape(self, line_info): |
|
2333 | def handle_shell_escape(self, line_info): | |
2333 | """Execute the line in a shell, empty return value""" |
|
2334 | """Execute the line in a shell, empty return value""" | |
2334 | #print 'line in :', `line` # dbg |
|
2335 | #print 'line in :', `line` # dbg | |
2335 | line = line_info.line |
|
2336 | line = line_info.line | |
2336 | if line.lstrip().startswith('!!'): |
|
2337 | if line.lstrip().startswith('!!'): | |
2337 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
2338 | # rewrite LineInfo's line, iFun and theRest to properly hold the | |
2338 | # call to %sx and the actual command to be executed, so |
|
2339 | # call to %sx and the actual command to be executed, so | |
2339 | # handle_magic can work correctly. Note that this works even if |
|
2340 | # handle_magic can work correctly. Note that this works even if | |
2340 | # the line is indented, so it handles multi_line_specials |
|
2341 | # the line is indented, so it handles multi_line_specials | |
2341 | # properly. |
|
2342 | # properly. | |
2342 | new_rest = line.lstrip()[2:] |
|
2343 | new_rest = line.lstrip()[2:] | |
2343 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
2344 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) | |
2344 | line_info.iFun = 'sx' |
|
2345 | line_info.iFun = 'sx' | |
2345 | line_info.theRest = new_rest |
|
2346 | line_info.theRest = new_rest | |
2346 | return self.handle_magic(line_info) |
|
2347 | return self.handle_magic(line_info) | |
2347 | else: |
|
2348 | else: | |
2348 | cmd = line.lstrip().lstrip('!') |
|
2349 | cmd = line.lstrip().lstrip('!') | |
2349 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2350 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2350 | make_quoted_expr(cmd)) |
|
2351 | make_quoted_expr(cmd)) | |
2351 | # update cache/log and return |
|
2352 | # update cache/log and return | |
2352 | self.log(line,line_out,line_info.continue_prompt) |
|
2353 | self.log(line,line_out,line_info.continue_prompt) | |
2353 | return line_out |
|
2354 | return line_out | |
2354 |
|
2355 | |||
2355 | def handle_magic(self, line_info): |
|
2356 | def handle_magic(self, line_info): | |
2356 | """Execute magic functions.""" |
|
2357 | """Execute magic functions.""" | |
2357 | iFun = line_info.iFun |
|
2358 | iFun = line_info.iFun | |
2358 | theRest = line_info.theRest |
|
2359 | theRest = line_info.theRest | |
2359 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
2360 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, | |
2360 | make_quoted_expr(iFun + " " + theRest)) |
|
2361 | make_quoted_expr(iFun + " " + theRest)) | |
2361 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
2362 | self.log(line_info.line,cmd,line_info.continue_prompt) | |
2362 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2363 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg | |
2363 | return cmd |
|
2364 | return cmd | |
2364 |
|
2365 | |||
2365 | def handle_auto(self, line_info): |
|
2366 | def handle_auto(self, line_info): | |
2366 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2367 | """Hande lines which can be auto-executed, quoting if requested.""" | |
2367 |
|
2368 | |||
2368 | line = line_info.line |
|
2369 | line = line_info.line | |
2369 | iFun = line_info.iFun |
|
2370 | iFun = line_info.iFun | |
2370 | theRest = line_info.theRest |
|
2371 | theRest = line_info.theRest | |
2371 | pre = line_info.pre |
|
2372 | pre = line_info.pre | |
2372 | continue_prompt = line_info.continue_prompt |
|
2373 | continue_prompt = line_info.continue_prompt | |
2373 | obj = line_info.ofind(self)['obj'] |
|
2374 | obj = line_info.ofind(self)['obj'] | |
2374 |
|
2375 | |||
2375 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2376 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2376 |
|
2377 | |||
2377 | # This should only be active for single-line input! |
|
2378 | # This should only be active for single-line input! | |
2378 | if continue_prompt: |
|
2379 | if continue_prompt: | |
2379 | self.log(line,line,continue_prompt) |
|
2380 | self.log(line,line,continue_prompt) | |
2380 | return line |
|
2381 | return line | |
2381 |
|
2382 | |||
2382 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) |
|
2383 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) | |
2383 | auto_rewrite = True |
|
2384 | auto_rewrite = True | |
2384 |
|
2385 | |||
2385 | if pre == self.ESC_QUOTE: |
|
2386 | if pre == self.ESC_QUOTE: | |
2386 | # Auto-quote splitting on whitespace |
|
2387 | # Auto-quote splitting on whitespace | |
2387 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2388 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) | |
2388 | elif pre == self.ESC_QUOTE2: |
|
2389 | elif pre == self.ESC_QUOTE2: | |
2389 | # Auto-quote whole string |
|
2390 | # Auto-quote whole string | |
2390 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2391 | newcmd = '%s("%s")' % (iFun,theRest) | |
2391 | elif pre == self.ESC_PAREN: |
|
2392 | elif pre == self.ESC_PAREN: | |
2392 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2393 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) | |
2393 | else: |
|
2394 | else: | |
2394 | # Auto-paren. |
|
2395 | # Auto-paren. | |
2395 | # We only apply it to argument-less calls if the autocall |
|
2396 | # We only apply it to argument-less calls if the autocall | |
2396 | # parameter is set to 2. We only need to check that autocall is < |
|
2397 | # parameter is set to 2. We only need to check that autocall is < | |
2397 | # 2, since this function isn't called unless it's at least 1. |
|
2398 | # 2, since this function isn't called unless it's at least 1. | |
2398 | if not theRest and (self.rc.autocall < 2) and not force_auto: |
|
2399 | if not theRest and (self.rc.autocall < 2) and not force_auto: | |
2399 | newcmd = '%s %s' % (iFun,theRest) |
|
2400 | newcmd = '%s %s' % (iFun,theRest) | |
2400 | auto_rewrite = False |
|
2401 | auto_rewrite = False | |
2401 | else: |
|
2402 | else: | |
2402 | if not force_auto and theRest.startswith('['): |
|
2403 | if not force_auto and theRest.startswith('['): | |
2403 | if hasattr(obj,'__getitem__'): |
|
2404 | if hasattr(obj,'__getitem__'): | |
2404 | # Don't autocall in this case: item access for an object |
|
2405 | # Don't autocall in this case: item access for an object | |
2405 | # which is BOTH callable and implements __getitem__. |
|
2406 | # which is BOTH callable and implements __getitem__. | |
2406 | newcmd = '%s %s' % (iFun,theRest) |
|
2407 | newcmd = '%s %s' % (iFun,theRest) | |
2407 | auto_rewrite = False |
|
2408 | auto_rewrite = False | |
2408 | else: |
|
2409 | else: | |
2409 | # if the object doesn't support [] access, go ahead and |
|
2410 | # if the object doesn't support [] access, go ahead and | |
2410 | # autocall |
|
2411 | # autocall | |
2411 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2412 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) | |
2412 | elif theRest.endswith(';'): |
|
2413 | elif theRest.endswith(';'): | |
2413 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2414 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) | |
2414 | else: |
|
2415 | else: | |
2415 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2416 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) | |
2416 |
|
2417 | |||
2417 | if auto_rewrite: |
|
2418 | if auto_rewrite: | |
2418 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2419 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd | |
2419 |
|
2420 | |||
2420 | try: |
|
2421 | try: | |
2421 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2422 | # plain ascii works better w/ pyreadline, on some machines, so | |
2422 | # we use it and only print uncolored rewrite if we have unicode |
|
2423 | # we use it and only print uncolored rewrite if we have unicode | |
2423 | rw = str(rw) |
|
2424 | rw = str(rw) | |
2424 | print >>Term.cout, rw |
|
2425 | print >>Term.cout, rw | |
2425 | except UnicodeEncodeError: |
|
2426 | except UnicodeEncodeError: | |
2426 | print "-------------->" + newcmd |
|
2427 | print "-------------->" + newcmd | |
2427 |
|
2428 | |||
2428 | # log what is now valid Python, not the actual user input (without the |
|
2429 | # log what is now valid Python, not the actual user input (without the | |
2429 | # final newline) |
|
2430 | # final newline) | |
2430 | self.log(line,newcmd,continue_prompt) |
|
2431 | self.log(line,newcmd,continue_prompt) | |
2431 | return newcmd |
|
2432 | return newcmd | |
2432 |
|
2433 | |||
2433 | def handle_help(self, line_info): |
|
2434 | def handle_help(self, line_info): | |
2434 | """Try to get some help for the object. |
|
2435 | """Try to get some help for the object. | |
2435 |
|
2436 | |||
2436 | obj? or ?obj -> basic information. |
|
2437 | obj? or ?obj -> basic information. | |
2437 | obj?? or ??obj -> more details. |
|
2438 | obj?? or ??obj -> more details. | |
2438 | """ |
|
2439 | """ | |
2439 |
|
2440 | |||
2440 | line = line_info.line |
|
2441 | line = line_info.line | |
2441 | # We need to make sure that we don't process lines which would be |
|
2442 | # We need to make sure that we don't process lines which would be | |
2442 | # otherwise valid python, such as "x=1 # what?" |
|
2443 | # otherwise valid python, such as "x=1 # what?" | |
2443 | try: |
|
2444 | try: | |
2444 | codeop.compile_command(line) |
|
2445 | codeop.compile_command(line) | |
2445 | except SyntaxError: |
|
2446 | except SyntaxError: | |
2446 | # We should only handle as help stuff which is NOT valid syntax |
|
2447 | # We should only handle as help stuff which is NOT valid syntax | |
2447 | if line[0]==self.ESC_HELP: |
|
2448 | if line[0]==self.ESC_HELP: | |
2448 | line = line[1:] |
|
2449 | line = line[1:] | |
2449 | elif line[-1]==self.ESC_HELP: |
|
2450 | elif line[-1]==self.ESC_HELP: | |
2450 | line = line[:-1] |
|
2451 | line = line[:-1] | |
2451 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
2452 | self.log(line,'#?'+line,line_info.continue_prompt) | |
2452 | if line: |
|
2453 | if line: | |
2453 | #print 'line:<%r>' % line # dbg |
|
2454 | #print 'line:<%r>' % line # dbg | |
2454 | self.magic_pinfo(line) |
|
2455 | self.magic_pinfo(line) | |
2455 | else: |
|
2456 | else: | |
2456 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2457 | page(self.usage,screen_lines=self.rc.screen_length) | |
2457 | return '' # Empty string is needed here! |
|
2458 | return '' # Empty string is needed here! | |
2458 | except: |
|
2459 | except: | |
2459 | # Pass any other exceptions through to the normal handler |
|
2460 | # Pass any other exceptions through to the normal handler | |
2460 | return self.handle_normal(line_info) |
|
2461 | return self.handle_normal(line_info) | |
2461 | else: |
|
2462 | else: | |
2462 | # If the code compiles ok, we should handle it normally |
|
2463 | # If the code compiles ok, we should handle it normally | |
2463 | return self.handle_normal(line_info) |
|
2464 | return self.handle_normal(line_info) | |
2464 |
|
2465 | |||
2465 | def getapi(self): |
|
2466 | def getapi(self): | |
2466 | """ Get an IPApi object for this shell instance |
|
2467 | """ Get an IPApi object for this shell instance | |
2467 |
|
2468 | |||
2468 | Getting an IPApi object is always preferable to accessing the shell |
|
2469 | Getting an IPApi object is always preferable to accessing the shell | |
2469 | directly, but this holds true especially for extensions. |
|
2470 | directly, but this holds true especially for extensions. | |
2470 |
|
2471 | |||
2471 | It should always be possible to implement an extension with IPApi |
|
2472 | It should always be possible to implement an extension with IPApi | |
2472 | alone. If not, contact maintainer to request an addition. |
|
2473 | alone. If not, contact maintainer to request an addition. | |
2473 |
|
2474 | |||
2474 | """ |
|
2475 | """ | |
2475 | return self.api |
|
2476 | return self.api | |
2476 |
|
2477 | |||
2477 | def handle_emacs(self, line_info): |
|
2478 | def handle_emacs(self, line_info): | |
2478 | """Handle input lines marked by python-mode.""" |
|
2479 | """Handle input lines marked by python-mode.""" | |
2479 |
|
2480 | |||
2480 | # Currently, nothing is done. Later more functionality can be added |
|
2481 | # Currently, nothing is done. Later more functionality can be added | |
2481 | # here if needed. |
|
2482 | # here if needed. | |
2482 |
|
2483 | |||
2483 | # The input cache shouldn't be updated |
|
2484 | # The input cache shouldn't be updated | |
2484 | return line_info.line |
|
2485 | return line_info.line | |
2485 |
|
2486 | |||
2486 |
|
2487 | |||
2487 | def mktempfile(self,data=None): |
|
2488 | def mktempfile(self,data=None): | |
2488 | """Make a new tempfile and return its filename. |
|
2489 | """Make a new tempfile and return its filename. | |
2489 |
|
2490 | |||
2490 | This makes a call to tempfile.mktemp, but it registers the created |
|
2491 | This makes a call to tempfile.mktemp, but it registers the created | |
2491 | filename internally so ipython cleans it up at exit time. |
|
2492 | filename internally so ipython cleans it up at exit time. | |
2492 |
|
2493 | |||
2493 | Optional inputs: |
|
2494 | Optional inputs: | |
2494 |
|
2495 | |||
2495 | - data(None): if data is given, it gets written out to the temp file |
|
2496 | - data(None): if data is given, it gets written out to the temp file | |
2496 | immediately, and the file is closed again.""" |
|
2497 | immediately, and the file is closed again.""" | |
2497 |
|
2498 | |||
2498 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2499 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2499 | self.tempfiles.append(filename) |
|
2500 | self.tempfiles.append(filename) | |
2500 |
|
2501 | |||
2501 | if data: |
|
2502 | if data: | |
2502 | tmp_file = open(filename,'w') |
|
2503 | tmp_file = open(filename,'w') | |
2503 | tmp_file.write(data) |
|
2504 | tmp_file.write(data) | |
2504 | tmp_file.close() |
|
2505 | tmp_file.close() | |
2505 | return filename |
|
2506 | return filename | |
2506 |
|
2507 | |||
2507 | def write(self,data): |
|
2508 | def write(self,data): | |
2508 | """Write a string to the default output""" |
|
2509 | """Write a string to the default output""" | |
2509 | Term.cout.write(data) |
|
2510 | Term.cout.write(data) | |
2510 |
|
2511 | |||
2511 | def write_err(self,data): |
|
2512 | def write_err(self,data): | |
2512 | """Write a string to the default error output""" |
|
2513 | """Write a string to the default error output""" | |
2513 | Term.cerr.write(data) |
|
2514 | Term.cerr.write(data) | |
2514 |
|
2515 | |||
2515 | def ask_exit(self): |
|
2516 | def ask_exit(self): | |
2516 | """ Call for exiting. Can be overiden and used as a callback. """ |
|
2517 | """ Call for exiting. Can be overiden and used as a callback. """ | |
2517 | self.exit_now = True |
|
2518 | self.exit_now = True | |
2518 |
|
2519 | |||
2519 | def exit(self): |
|
2520 | def exit(self): | |
2520 | """Handle interactive exit. |
|
2521 | """Handle interactive exit. | |
2521 |
|
2522 | |||
2522 | This method calls the ask_exit callback.""" |
|
2523 | This method calls the ask_exit callback.""" | |
2523 |
|
2524 | |||
2524 | if self.rc.confirm_exit: |
|
2525 | if self.rc.confirm_exit: | |
2525 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2526 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
2526 | self.ask_exit() |
|
2527 | self.ask_exit() | |
2527 | else: |
|
2528 | else: | |
2528 | self.ask_exit() |
|
2529 | self.ask_exit() | |
2529 |
|
2530 | |||
2530 | def safe_execfile(self,fname,*where,**kw): |
|
2531 | def safe_execfile(self,fname,*where,**kw): | |
2531 | """A safe version of the builtin execfile(). |
|
2532 | """A safe version of the builtin execfile(). | |
2532 |
|
2533 | |||
2533 | This version will never throw an exception, and knows how to handle |
|
2534 | This version will never throw an exception, and knows how to handle | |
2534 | ipython logs as well. |
|
2535 | ipython logs as well. | |
2535 |
|
2536 | |||
2536 | :Parameters: |
|
2537 | :Parameters: | |
2537 | fname : string |
|
2538 | fname : string | |
2538 | Name of the file to be executed. |
|
2539 | Name of the file to be executed. | |
2539 |
|
2540 | |||
2540 | where : tuple |
|
2541 | where : tuple | |
2541 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2542 | One or two namespaces, passed to execfile() as (globals,locals). | |
2542 | If only one is given, it is passed as both. |
|
2543 | If only one is given, it is passed as both. | |
2543 |
|
2544 | |||
2544 | :Keywords: |
|
2545 | :Keywords: | |
2545 | islog : boolean (False) |
|
2546 | islog : boolean (False) | |
2546 |
|
2547 | |||
2547 | quiet : boolean (True) |
|
2548 | quiet : boolean (True) | |
2548 |
|
2549 | |||
2549 | exit_ignore : boolean (False) |
|
2550 | exit_ignore : boolean (False) | |
2550 | """ |
|
2551 | """ | |
2551 |
|
2552 | |||
2552 | def syspath_cleanup(): |
|
2553 | def syspath_cleanup(): | |
2553 | """Internal cleanup routine for sys.path.""" |
|
2554 | """Internal cleanup routine for sys.path.""" | |
2554 | if add_dname: |
|
2555 | if add_dname: | |
2555 | try: |
|
2556 | try: | |
2556 | sys.path.remove(dname) |
|
2557 | sys.path.remove(dname) | |
2557 | except ValueError: |
|
2558 | except ValueError: | |
2558 | # For some reason the user has already removed it, ignore. |
|
2559 | # For some reason the user has already removed it, ignore. | |
2559 | pass |
|
2560 | pass | |
2560 |
|
2561 | |||
2561 | fname = os.path.expanduser(fname) |
|
2562 | fname = os.path.expanduser(fname) | |
2562 |
|
2563 | |||
2563 | # Find things also in current directory. This is needed to mimic the |
|
2564 | # Find things also in current directory. This is needed to mimic the | |
2564 | # behavior of running a script from the system command line, where |
|
2565 | # behavior of running a script from the system command line, where | |
2565 | # Python inserts the script's directory into sys.path |
|
2566 | # Python inserts the script's directory into sys.path | |
2566 | dname = os.path.dirname(os.path.abspath(fname)) |
|
2567 | dname = os.path.dirname(os.path.abspath(fname)) | |
2567 | add_dname = False |
|
2568 | add_dname = False | |
2568 | if dname not in sys.path: |
|
2569 | if dname not in sys.path: | |
2569 | sys.path.insert(0,dname) |
|
2570 | sys.path.insert(0,dname) | |
2570 | add_dname = True |
|
2571 | add_dname = True | |
2571 |
|
2572 | |||
2572 | try: |
|
2573 | try: | |
2573 | xfile = open(fname) |
|
2574 | xfile = open(fname) | |
2574 | except: |
|
2575 | except: | |
2575 | print >> Term.cerr, \ |
|
2576 | print >> Term.cerr, \ | |
2576 | 'Could not open file <%s> for safe execution.' % fname |
|
2577 | 'Could not open file <%s> for safe execution.' % fname | |
2577 | syspath_cleanup() |
|
2578 | syspath_cleanup() | |
2578 | return None |
|
2579 | return None | |
2579 |
|
2580 | |||
2580 | kw.setdefault('islog',0) |
|
2581 | kw.setdefault('islog',0) | |
2581 | kw.setdefault('quiet',1) |
|
2582 | kw.setdefault('quiet',1) | |
2582 | kw.setdefault('exit_ignore',0) |
|
2583 | kw.setdefault('exit_ignore',0) | |
2583 |
|
2584 | |||
2584 | first = xfile.readline() |
|
2585 | first = xfile.readline() | |
2585 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2586 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() | |
2586 | xfile.close() |
|
2587 | xfile.close() | |
2587 | # line by line execution |
|
2588 | # line by line execution | |
2588 | if first.startswith(loghead) or kw['islog']: |
|
2589 | if first.startswith(loghead) or kw['islog']: | |
2589 | print 'Loading log file <%s> one line at a time...' % fname |
|
2590 | print 'Loading log file <%s> one line at a time...' % fname | |
2590 | if kw['quiet']: |
|
2591 | if kw['quiet']: | |
2591 | stdout_save = sys.stdout |
|
2592 | stdout_save = sys.stdout | |
2592 | sys.stdout = StringIO.StringIO() |
|
2593 | sys.stdout = StringIO.StringIO() | |
2593 | try: |
|
2594 | try: | |
2594 | globs,locs = where[0:2] |
|
2595 | globs,locs = where[0:2] | |
2595 | except: |
|
2596 | except: | |
2596 | try: |
|
2597 | try: | |
2597 | globs = locs = where[0] |
|
2598 | globs = locs = where[0] | |
2598 | except: |
|
2599 | except: | |
2599 | globs = locs = globals() |
|
2600 | globs = locs = globals() | |
2600 | badblocks = [] |
|
2601 | badblocks = [] | |
2601 |
|
2602 | |||
2602 | # we also need to identify indented blocks of code when replaying |
|
2603 | # we also need to identify indented blocks of code when replaying | |
2603 | # logs and put them together before passing them to an exec |
|
2604 | # logs and put them together before passing them to an exec | |
2604 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2605 | # statement. This takes a bit of regexp and look-ahead work in the | |
2605 | # file. It's easiest if we swallow the whole thing in memory |
|
2606 | # file. It's easiest if we swallow the whole thing in memory | |
2606 | # first, and manually walk through the lines list moving the |
|
2607 | # first, and manually walk through the lines list moving the | |
2607 | # counter ourselves. |
|
2608 | # counter ourselves. | |
2608 | indent_re = re.compile('\s+\S') |
|
2609 | indent_re = re.compile('\s+\S') | |
2609 | xfile = open(fname) |
|
2610 | xfile = open(fname) | |
2610 | filelines = xfile.readlines() |
|
2611 | filelines = xfile.readlines() | |
2611 | xfile.close() |
|
2612 | xfile.close() | |
2612 | nlines = len(filelines) |
|
2613 | nlines = len(filelines) | |
2613 | lnum = 0 |
|
2614 | lnum = 0 | |
2614 | while lnum < nlines: |
|
2615 | while lnum < nlines: | |
2615 | line = filelines[lnum] |
|
2616 | line = filelines[lnum] | |
2616 | lnum += 1 |
|
2617 | lnum += 1 | |
2617 | # don't re-insert logger status info into cache |
|
2618 | # don't re-insert logger status info into cache | |
2618 | if line.startswith('#log#'): |
|
2619 | if line.startswith('#log#'): | |
2619 | continue |
|
2620 | continue | |
2620 | else: |
|
2621 | else: | |
2621 | # build a block of code (maybe a single line) for execution |
|
2622 | # build a block of code (maybe a single line) for execution | |
2622 | block = line |
|
2623 | block = line | |
2623 | try: |
|
2624 | try: | |
2624 | next = filelines[lnum] # lnum has already incremented |
|
2625 | next = filelines[lnum] # lnum has already incremented | |
2625 | except: |
|
2626 | except: | |
2626 | next = None |
|
2627 | next = None | |
2627 | while next and indent_re.match(next): |
|
2628 | while next and indent_re.match(next): | |
2628 | block += next |
|
2629 | block += next | |
2629 | lnum += 1 |
|
2630 | lnum += 1 | |
2630 | try: |
|
2631 | try: | |
2631 | next = filelines[lnum] |
|
2632 | next = filelines[lnum] | |
2632 | except: |
|
2633 | except: | |
2633 | next = None |
|
2634 | next = None | |
2634 | # now execute the block of one or more lines |
|
2635 | # now execute the block of one or more lines | |
2635 | try: |
|
2636 | try: | |
2636 | exec block in globs,locs |
|
2637 | exec block in globs,locs | |
2637 | except SystemExit: |
|
2638 | except SystemExit: | |
2638 | pass |
|
2639 | pass | |
2639 | except: |
|
2640 | except: | |
2640 | badblocks.append(block.rstrip()) |
|
2641 | badblocks.append(block.rstrip()) | |
2641 | if kw['quiet']: # restore stdout |
|
2642 | if kw['quiet']: # restore stdout | |
2642 | sys.stdout.close() |
|
2643 | sys.stdout.close() | |
2643 | sys.stdout = stdout_save |
|
2644 | sys.stdout = stdout_save | |
2644 | print 'Finished replaying log file <%s>' % fname |
|
2645 | print 'Finished replaying log file <%s>' % fname | |
2645 | if badblocks: |
|
2646 | if badblocks: | |
2646 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2647 | print >> sys.stderr, ('\nThe following lines/blocks in file ' | |
2647 | '<%s> reported errors:' % fname) |
|
2648 | '<%s> reported errors:' % fname) | |
2648 |
|
2649 | |||
2649 | for badline in badblocks: |
|
2650 | for badline in badblocks: | |
2650 | print >> sys.stderr, badline |
|
2651 | print >> sys.stderr, badline | |
2651 | else: # regular file execution |
|
2652 | else: # regular file execution | |
2652 | try: |
|
2653 | try: | |
2653 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
2654 | if sys.platform == 'win32' and sys.version_info < (2,5,1): | |
2654 | # Work around a bug in Python for Windows. The bug was |
|
2655 | # Work around a bug in Python for Windows. The bug was | |
2655 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
2656 | # fixed in in Python 2.5 r54159 and 54158, but that's still | |
2656 | # SVN Python as of March/07. For details, see: |
|
2657 | # SVN Python as of March/07. For details, see: | |
2657 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
2658 | # http://projects.scipy.org/ipython/ipython/ticket/123 | |
2658 | try: |
|
2659 | try: | |
2659 | globs,locs = where[0:2] |
|
2660 | globs,locs = where[0:2] | |
2660 | except: |
|
2661 | except: | |
2661 | try: |
|
2662 | try: | |
2662 | globs = locs = where[0] |
|
2663 | globs = locs = where[0] | |
2663 | except: |
|
2664 | except: | |
2664 | globs = locs = globals() |
|
2665 | globs = locs = globals() | |
2665 | exec file(fname) in globs,locs |
|
2666 | exec file(fname) in globs,locs | |
2666 | else: |
|
2667 | else: | |
2667 | execfile(fname,*where) |
|
2668 | execfile(fname,*where) | |
2668 | except SyntaxError: |
|
2669 | except SyntaxError: | |
2669 | self.showsyntaxerror() |
|
2670 | self.showsyntaxerror() | |
2670 | warn('Failure executing file: <%s>' % fname) |
|
2671 | warn('Failure executing file: <%s>' % fname) | |
2671 | except SystemExit,status: |
|
2672 | except SystemExit,status: | |
2672 | # Code that correctly sets the exit status flag to success (0) |
|
2673 | # Code that correctly sets the exit status flag to success (0) | |
2673 | # shouldn't be bothered with a traceback. Note that a plain |
|
2674 | # shouldn't be bothered with a traceback. Note that a plain | |
2674 | # sys.exit() does NOT set the message to 0 (it's empty) so that |
|
2675 | # sys.exit() does NOT set the message to 0 (it's empty) so that | |
2675 | # will still get a traceback. Note that the structure of the |
|
2676 | # will still get a traceback. Note that the structure of the | |
2676 | # SystemExit exception changed between Python 2.4 and 2.5, so |
|
2677 | # SystemExit exception changed between Python 2.4 and 2.5, so | |
2677 | # the checks must be done in a version-dependent way. |
|
2678 | # the checks must be done in a version-dependent way. | |
2678 | show = False |
|
2679 | show = False | |
2679 |
|
2680 | |||
2680 | if sys.version_info[:2] > (2,5): |
|
2681 | if sys.version_info[:2] > (2,5): | |
2681 | if status.message!=0 and not kw['exit_ignore']: |
|
2682 | if status.message!=0 and not kw['exit_ignore']: | |
2682 | show = True |
|
2683 | show = True | |
2683 | else: |
|
2684 | else: | |
2684 | if status.code and not kw['exit_ignore']: |
|
2685 | if status.code and not kw['exit_ignore']: | |
2685 | show = True |
|
2686 | show = True | |
2686 | if show: |
|
2687 | if show: | |
2687 | self.showtraceback() |
|
2688 | self.showtraceback() | |
2688 | warn('Failure executing file: <%s>' % fname) |
|
2689 | warn('Failure executing file: <%s>' % fname) | |
2689 | except: |
|
2690 | except: | |
2690 | self.showtraceback() |
|
2691 | self.showtraceback() | |
2691 | warn('Failure executing file: <%s>' % fname) |
|
2692 | warn('Failure executing file: <%s>' % fname) | |
2692 |
|
2693 | |||
2693 | syspath_cleanup() |
|
2694 | syspath_cleanup() | |
2694 |
|
2695 | |||
2695 | #************************* end of file <iplib.py> ***************************** |
|
2696 | #************************* end of file <iplib.py> ***************************** |
@@ -1,278 +1,291 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Tests for genutils.py""" |
|
3 | """Tests for genutils.py""" | |
4 |
|
4 | |||
5 | __docformat__ = "restructuredtext en" |
|
5 | __docformat__ = "restructuredtext en" | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008 The IPython Development Team |
|
8 | # Copyright (C) 2008 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | from IPython import genutils |
|
18 | # stdlib | |
19 | from IPython.testing.decorators import skipif, skip_if_not_win32 |
|
19 | import os | |
20 | from nose import with_setup |
|
20 | import shutil | |
21 | from nose.tools import raises |
|
21 | import sys | |
|
22 | import tempfile | |||
22 |
|
23 | |||
23 | from os.path import join, abspath, split |
|
24 | from os.path import join, abspath, split | |
24 | import os, sys, IPython |
|
25 | ||
|
26 | # third-party | |||
25 | import nose.tools as nt |
|
27 | import nose.tools as nt | |
26 |
|
28 | |||
27 | env = os.environ |
|
29 | from nose import with_setup | |
|
30 | from nose.tools import raises | |||
|
31 | ||||
|
32 | # Our own | |||
|
33 | import IPython | |||
|
34 | from IPython import genutils | |||
|
35 | from IPython.testing.decorators import skipif, skip_if_not_win32 | |||
28 |
|
36 | |||
|
37 | # Platform-dependent imports | |||
29 | try: |
|
38 | try: | |
30 | import _winreg as wreg |
|
39 | import _winreg as wreg | |
31 | except ImportError: |
|
40 | except ImportError: | |
32 | #Fake _winreg module on none windows platforms |
|
41 | #Fake _winreg module on none windows platforms | |
33 | import new |
|
42 | import new | |
34 | sys.modules["_winreg"] = new.module("_winreg") |
|
43 | sys.modules["_winreg"] = new.module("_winreg") | |
35 | import _winreg as wreg |
|
44 | import _winreg as wreg | |
36 | #Add entries that needs to be stubbed by the testing code |
|
45 | #Add entries that needs to be stubbed by the testing code | |
37 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
46 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
38 |
|
47 | |||
39 | test_file_path = split(abspath(__file__))[0] |
|
48 | #----------------------------------------------------------------------------- | |
40 |
|
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 | # Setup/teardown functions/decorators |
|
57 | # Setup/teardown functions/decorators | |
43 | # |
|
58 | # | |
44 |
|
59 | |||
45 |
|
||||
46 | def setup(): |
|
60 | def setup(): | |
47 | """Setup testenvironment for the module: |
|
61 | """Setup testenvironment for the module: | |
48 |
|
62 | |||
49 | - Adds dummy home dir tree |
|
63 | - Adds dummy home dir tree | |
50 | """ |
|
64 | """ | |
51 | try: |
|
65 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
52 | os.makedirs("home_test_dir/_ipython") |
|
66 | # problem because that exception is only defined on Windows... | |
53 | except WindowsError: |
|
67 | os.makedirs(IP_TEST_DIR) | |
54 | pass #Or should we complain that the test directory already exists?? |
|
|||
55 |
|
68 | |||
56 | def teardown(): |
|
69 | def teardown(): | |
57 | """Teardown testenvironment for the module: |
|
70 | """Teardown testenvironment for the module: | |
58 |
|
71 | |||
59 | - Remove dummy home dir tree |
|
72 | - Remove dummy home dir tree | |
60 | """ |
|
73 | """ | |
61 | try: |
|
74 | # Note: we remove the parent test dir, which is the root of all test | |
62 | os.removedirs("home_test_dir/_ipython") |
|
75 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
63 | except WindowsError: |
|
76 | # that non-empty directories are all recursively removed. | |
64 | pass #Or should we complain that the test directory already exists?? |
|
77 | shutil.rmtree(TMP_TEST_DIR) | |
65 |
|
78 | |||
66 |
|
79 | |||
67 | def setup_environment(): |
|
80 | def setup_environment(): | |
68 | """Setup testenvironment for some functions that are tested |
|
81 | """Setup testenvironment for some functions that are tested | |
69 | in this module. In particular this functions stores attributes |
|
82 | in this module. In particular this functions stores attributes | |
70 | and other things that we need to stub in some test functions. |
|
83 | and other things that we need to stub in some test functions. | |
71 | This needs to be done on a function level and not module level because |
|
84 | This needs to be done on a function level and not module level because | |
72 | each testfunction needs a pristine environment. |
|
85 | each testfunction needs a pristine environment. | |
73 | """ |
|
86 | """ | |
74 | global oldstuff, platformstuff |
|
87 | global oldstuff, platformstuff | |
75 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) |
|
88 | oldstuff = (env.copy(), os.name, genutils.get_home_dir, IPython.__file__,) | |
76 |
|
89 | |||
77 | if os.name == 'nt': |
|
90 | if os.name == 'nt': | |
78 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) |
|
91 | platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | |
79 |
|
92 | |||
80 | if 'IPYTHONDIR' in env: |
|
93 | if 'IPYTHONDIR' in env: | |
81 | del env['IPYTHONDIR'] |
|
94 | del env['IPYTHONDIR'] | |
82 |
|
95 | |||
83 | def teardown_environment(): |
|
96 | def teardown_environment(): | |
84 | """Restore things that were remebered by the setup_environment function |
|
97 | """Restore things that were remebered by the setup_environment function | |
85 | """ |
|
98 | """ | |
86 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff |
|
99 | (oldenv, os.name, genutils.get_home_dir, IPython.__file__,) = oldstuff | |
87 | for key in env.keys(): |
|
100 | for key in env.keys(): | |
88 | if key not in oldenv: |
|
101 | if key not in oldenv: | |
89 | del env[key] |
|
102 | del env[key] | |
90 | env.update(oldenv) |
|
103 | env.update(oldenv) | |
91 | if hasattr(sys, 'frozen'): |
|
104 | if hasattr(sys, 'frozen'): | |
92 | del sys.frozen |
|
105 | del sys.frozen | |
93 | if os.name == 'nt': |
|
106 | if os.name == 'nt': | |
94 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff |
|
107 | (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | |
95 |
|
108 | |||
96 | # Build decorator that uses the setup_environment/setup_environment |
|
109 | # Build decorator that uses the setup_environment/setup_environment | |
97 | with_enivronment = with_setup(setup_environment, teardown_environment) |
|
110 | with_enivronment = with_setup(setup_environment, teardown_environment) | |
98 |
|
111 | |||
99 |
|
112 | |||
100 | # |
|
113 | # | |
101 | # Tests for get_home_dir |
|
114 | # Tests for get_home_dir | |
102 | # |
|
115 | # | |
103 |
|
116 | |||
104 | @skip_if_not_win32 |
|
117 | @skip_if_not_win32 | |
105 | @with_enivronment |
|
118 | @with_enivronment | |
106 | def test_get_home_dir_1(): |
|
119 | def test_get_home_dir_1(): | |
107 | """Testcase for py2exe logic, un-compressed lib |
|
120 | """Testcase for py2exe logic, un-compressed lib | |
108 | """ |
|
121 | """ | |
109 | sys.frozen = True |
|
122 | sys.frozen = True | |
110 |
|
123 | |||
111 | #fake filename for IPython.__init__ |
|
124 | #fake filename for IPython.__init__ | |
112 |
IPython.__file__ = abspath(join( |
|
125 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | |
113 |
|
126 | |||
114 | home_dir = genutils.get_home_dir() |
|
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 | @skip_if_not_win32 |
|
130 | @skip_if_not_win32 | |
118 | @with_enivronment |
|
131 | @with_enivronment | |
119 | def test_get_home_dir_2(): |
|
132 | def test_get_home_dir_2(): | |
120 | """Testcase for py2exe logic, compressed lib |
|
133 | """Testcase for py2exe logic, compressed lib | |
121 | """ |
|
134 | """ | |
122 | sys.frozen = True |
|
135 | sys.frozen = True | |
123 | #fake filename for IPython.__init__ |
|
136 | #fake filename for IPython.__init__ | |
124 |
IPython.__file__ = abspath(join( |
|
137 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | |
125 |
|
138 | |||
126 | home_dir = genutils.get_home_dir() |
|
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 | @with_enivronment |
|
142 | @with_enivronment | |
130 | def test_get_home_dir_3(): |
|
143 | def test_get_home_dir_3(): | |
131 | """Testcase $HOME is set, then use its value as home directory.""" |
|
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 | home_dir = genutils.get_home_dir() |
|
146 | home_dir = genutils.get_home_dir() | |
134 | nt.assert_equal(home_dir, env["HOME"]) |
|
147 | nt.assert_equal(home_dir, env["HOME"]) | |
135 |
|
148 | |||
136 | @with_enivronment |
|
149 | @with_enivronment | |
137 | def test_get_home_dir_4(): |
|
150 | def test_get_home_dir_4(): | |
138 | """Testcase $HOME is not set, os=='poix'. |
|
151 | """Testcase $HOME is not set, os=='poix'. | |
139 | This should fail with HomeDirError""" |
|
152 | This should fail with HomeDirError""" | |
140 |
|
153 | |||
141 | os.name = 'posix' |
|
154 | os.name = 'posix' | |
142 | if 'HOME' in env: del env['HOME'] |
|
155 | if 'HOME' in env: del env['HOME'] | |
143 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) |
|
156 | nt.assert_raises(genutils.HomeDirError, genutils.get_home_dir) | |
144 |
|
157 | |||
145 | @skip_if_not_win32 |
|
158 | @skip_if_not_win32 | |
146 | @with_enivronment |
|
159 | @with_enivronment | |
147 | def test_get_home_dir_5(): |
|
160 | def test_get_home_dir_5(): | |
148 | """Testcase $HOME is not set, os=='nt' |
|
161 | """Testcase $HOME is not set, os=='nt' | |
149 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" |
|
162 | env['HOMEDRIVE'],env['HOMEPATH'] points to path.""" | |
150 |
|
163 | |||
151 | os.name = 'nt' |
|
164 | os.name = 'nt' | |
152 | if 'HOME' in env: del env['HOME'] |
|
165 | if 'HOME' in env: del env['HOME'] | |
153 |
env['HOMEDRIVE'], env['HOMEPATH'] = os.path. |
|
166 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) | |
154 |
|
167 | |||
155 | home_dir = genutils.get_home_dir() |
|
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 | @skip_if_not_win32 |
|
171 | @skip_if_not_win32 | |
159 | @with_enivronment |
|
172 | @with_enivronment | |
160 | def test_get_home_dir_6(): |
|
173 | def test_get_home_dir_6(): | |
161 | """Testcase $HOME is not set, os=='nt' |
|
174 | """Testcase $HOME is not set, os=='nt' | |
162 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. |
|
175 | env['HOMEDRIVE'],env['HOMEPATH'] do not point to path. | |
163 | env['USERPROFILE'] points to path |
|
176 | env['USERPROFILE'] points to path | |
164 | """ |
|
177 | """ | |
165 |
|
178 | |||
166 | os.name = 'nt' |
|
179 | os.name = 'nt' | |
167 | if 'HOME' in env: del env['HOME'] |
|
180 | if 'HOME' in env: del env['HOME'] | |
168 |
env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath( |
|
181 | env['HOMEDRIVE'], env['HOMEPATH'] = os.path.abspath(TEST_FILE_PATH), "DOES NOT EXIST" | |
169 |
env["USERPROFILE"] = abspath( |
|
182 | env["USERPROFILE"] = abspath(HOME_TEST_DIR) | |
170 |
|
183 | |||
171 | home_dir = genutils.get_home_dir() |
|
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 | # Should we stub wreg fully so we can run the test on all platforms? |
|
187 | # Should we stub wreg fully so we can run the test on all platforms? | |
175 | @skip_if_not_win32 |
|
188 | @skip_if_not_win32 | |
176 | @with_enivronment |
|
189 | @with_enivronment | |
177 | def test_get_home_dir_7(): |
|
190 | def test_get_home_dir_7(): | |
178 | """Testcase $HOME is not set, os=='nt' |
|
191 | """Testcase $HOME is not set, os=='nt' | |
179 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing |
|
192 | env['HOMEDRIVE'],env['HOMEPATH'], env['USERPROFILE'] missing | |
180 | """ |
|
193 | """ | |
181 | os.name = 'nt' |
|
194 | os.name = 'nt' | |
182 | if 'HOME' in env: del env['HOME'] |
|
195 | if 'HOME' in env: del env['HOME'] | |
183 | if 'HOMEDRIVE' in env: del env['HOMEDRIVE'] |
|
196 | if 'HOMEDRIVE' in env: del env['HOMEDRIVE'] | |
184 |
|
197 | |||
185 | #Stub windows registry functions |
|
198 | #Stub windows registry functions | |
186 | def OpenKey(x, y): |
|
199 | def OpenKey(x, y): | |
187 | class key: |
|
200 | class key: | |
188 | def Close(self): |
|
201 | def Close(self): | |
189 | pass |
|
202 | pass | |
190 | return key() |
|
203 | return key() | |
191 | def QueryValueEx(x, y): |
|
204 | def QueryValueEx(x, y): | |
192 | return [abspath(join(test_file_path, "home_test_dir"))] |
|
205 | return [abspath(HOME_TEST_DIR)] | |
193 |
|
206 | |||
194 | wreg.OpenKey = OpenKey |
|
207 | wreg.OpenKey = OpenKey | |
195 | wreg.QueryValueEx = QueryValueEx |
|
208 | wreg.QueryValueEx = QueryValueEx | |
196 |
|
209 | |||
197 | home_dir = genutils.get_home_dir() |
|
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 | # Tests for get_ipython_dir |
|
215 | # Tests for get_ipython_dir | |
203 | # |
|
216 | # | |
204 |
|
217 | |||
205 | @with_enivronment |
|
218 | @with_enivronment | |
206 | def test_get_ipython_dir_1(): |
|
219 | def test_get_ipython_dir_1(): | |
207 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
220 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
208 | env['IPYTHONDIR'] = "someplace/.ipython" |
|
221 | env['IPYTHONDIR'] = "someplace/.ipython" | |
209 | ipdir = genutils.get_ipython_dir() |
|
222 | ipdir = genutils.get_ipython_dir() | |
210 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) |
|
223 | nt.assert_equal(ipdir, os.path.abspath("someplace/.ipython")) | |
211 |
|
224 | |||
212 |
|
225 | |||
213 | @with_enivronment |
|
226 | @with_enivronment | |
214 | def test_get_ipython_dir_2(): |
|
227 | def test_get_ipython_dir_2(): | |
215 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
228 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
216 | genutils.get_home_dir = lambda : "someplace" |
|
229 | genutils.get_home_dir = lambda : "someplace" | |
217 | os.name = "posix" |
|
230 | os.name = "posix" | |
218 | ipdir = genutils.get_ipython_dir() |
|
231 | ipdir = genutils.get_ipython_dir() | |
219 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) |
|
232 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", ".ipython"))) | |
220 |
|
233 | |||
221 | @with_enivronment |
|
234 | @with_enivronment | |
222 | def test_get_ipython_dir_3(): |
|
235 | def test_get_ipython_dir_3(): | |
223 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
236 | """test_get_ipython_dir_3, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
224 | genutils.get_home_dir = lambda : "someplace" |
|
237 | genutils.get_home_dir = lambda : "someplace" | |
225 | os.name = "nt" |
|
238 | os.name = "nt" | |
226 | ipdir = genutils.get_ipython_dir() |
|
239 | ipdir = genutils.get_ipython_dir() | |
227 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) |
|
240 | nt.assert_equal(ipdir, os.path.abspath(os.path.join("someplace", "_ipython"))) | |
228 |
|
241 | |||
229 |
|
242 | |||
230 | # |
|
243 | # | |
231 | # Tests for get_security_dir |
|
244 | # Tests for get_security_dir | |
232 | # |
|
245 | # | |
233 |
|
246 | |||
234 | @with_enivronment |
|
247 | @with_enivronment | |
235 | def test_get_security_dir(): |
|
248 | def test_get_security_dir(): | |
236 | """Testcase to see if we can call get_security_dir without Exceptions.""" |
|
249 | """Testcase to see if we can call get_security_dir without Exceptions.""" | |
237 | sdir = genutils.get_security_dir() |
|
250 | sdir = genutils.get_security_dir() | |
238 |
|
251 | |||
239 |
|
252 | |||
240 | # |
|
253 | # | |
241 | # Tests for popkey |
|
254 | # Tests for popkey | |
242 | # |
|
255 | # | |
243 |
|
256 | |||
244 | def test_popkey_1(): |
|
257 | def test_popkey_1(): | |
245 | """test_popkey_1, Basic usage test of popkey |
|
258 | """test_popkey_1, Basic usage test of popkey | |
246 | """ |
|
259 | """ | |
247 | dct = dict(a=1, b=2, c=3) |
|
260 | dct = dict(a=1, b=2, c=3) | |
248 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
261 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
249 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
262 | nt.assert_equal(dct, dict(b=2, c=3)) | |
250 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
263 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
251 | nt.assert_equal(dct, dict(c=3)) |
|
264 | nt.assert_equal(dct, dict(c=3)) | |
252 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
265 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
253 | nt.assert_equal(dct, dict()) |
|
266 | nt.assert_equal(dct, dict()) | |
254 |
|
267 | |||
255 | def test_popkey_2(): |
|
268 | def test_popkey_2(): | |
256 | """test_popkey_2, Test to see that popkey of non occuring keys |
|
269 | """test_popkey_2, Test to see that popkey of non occuring keys | |
257 | generates a KeyError exception |
|
270 | generates a KeyError exception | |
258 | """ |
|
271 | """ | |
259 | dct = dict(a=1, b=2, c=3) |
|
272 | dct = dict(a=1, b=2, c=3) | |
260 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") |
|
273 | nt.assert_raises(KeyError, genutils.popkey, dct, "d") | |
261 |
|
274 | |||
262 | def test_popkey_3(): |
|
275 | def test_popkey_3(): | |
263 | """test_popkey_3, Tests to see that popkey calls returns the correct value |
|
276 | """test_popkey_3, Tests to see that popkey calls returns the correct value | |
264 | and that the key/value was removed from the dict. |
|
277 | and that the key/value was removed from the dict. | |
265 | """ |
|
278 | """ | |
266 | dct = dict(a=1, b=2, c=3) |
|
279 | dct = dict(a=1, b=2, c=3) | |
267 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) |
|
280 | nt.assert_equal(genutils.popkey(dct, "A", 13), 13) | |
268 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
281 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
269 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) |
|
282 | nt.assert_equal(genutils.popkey(dct, "B", 14), 14) | |
270 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
283 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
271 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) |
|
284 | nt.assert_equal(genutils.popkey(dct, "C", 15), 15) | |
272 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) |
|
285 | nt.assert_equal(dct, dict(a=1, b=2, c=3)) | |
273 | nt.assert_equal(genutils.popkey(dct, "a"), 1) |
|
286 | nt.assert_equal(genutils.popkey(dct, "a"), 1) | |
274 | nt.assert_equal(dct, dict(b=2, c=3)) |
|
287 | nt.assert_equal(dct, dict(b=2, c=3)) | |
275 | nt.assert_equal(genutils.popkey(dct, "b"), 2) |
|
288 | nt.assert_equal(genutils.popkey(dct, "b"), 2) | |
276 | nt.assert_equal(dct, dict(c=3)) |
|
289 | nt.assert_equal(dct, dict(c=3)) | |
277 | nt.assert_equal(genutils.popkey(dct, "c"), 3) |
|
290 | nt.assert_equal(genutils.popkey(dct, "c"), 3) | |
278 | nt.assert_equal(dct, dict()) |
|
291 | nt.assert_equal(dct, dict()) |
General Comments 0
You need to be logged in to leave comments.
Login now