Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,1116 +1,1143 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """IPython Shell classes. |
|
2 | """IPython Shell classes. | |
3 |
|
3 | |||
4 | All the matplotlib support code was co-developed with John Hunter, |
|
4 | All the matplotlib support code was co-developed with John Hunter, | |
5 | matplotlib's author. |
|
5 | matplotlib's author. | |
6 |
|
6 | |||
7 |
$Id: Shell.py 2 |
|
7 | $Id: Shell.py 2577 2007-08-02 23:50:02Z fperez $""" | |
8 |
|
8 | |||
9 | #***************************************************************************** |
|
9 | #***************************************************************************** | |
10 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
10 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #***************************************************************************** |
|
14 | #***************************************************************************** | |
15 |
|
15 | |||
16 | from IPython import Release |
|
16 | from IPython import Release | |
17 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
17 | __author__ = '%s <%s>' % Release.authors['Fernando'] | |
18 | __license__ = Release.license |
|
18 | __license__ = Release.license | |
19 |
|
19 | |||
20 | # Code begins |
|
20 | # Code begins | |
21 | # Stdlib imports |
|
21 | # Stdlib imports | |
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import __main__ |
|
23 | import __main__ | |
24 | import Queue |
|
24 | import Queue | |
25 | import inspect |
|
25 | import inspect | |
26 | import os |
|
26 | import os | |
27 | import sys |
|
27 | import sys | |
28 | import thread |
|
28 | import thread | |
29 | import threading |
|
29 | import threading | |
30 | import time |
|
30 | import time | |
31 |
|
31 | |||
32 | from signal import signal, SIGINT |
|
32 | from signal import signal, SIGINT | |
33 |
|
33 | |||
34 | try: |
|
34 | try: | |
35 | import ctypes |
|
35 | import ctypes | |
36 | HAS_CTYPES = True |
|
36 | HAS_CTYPES = True | |
37 | except ImportError: |
|
37 | except ImportError: | |
38 | HAS_CTYPES = False |
|
38 | HAS_CTYPES = False | |
39 |
|
39 | |||
40 | # IPython imports |
|
40 | # IPython imports | |
41 | import IPython |
|
41 | import IPython | |
42 | from IPython import ultraTB |
|
42 | from IPython import ultraTB, ipapi | |
43 | from IPython.genutils import Term,warn,error,flag_calls |
|
43 | from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no | |
44 | from IPython.iplib import InteractiveShell |
|
44 | from IPython.iplib import InteractiveShell | |
45 | from IPython.ipmaker import make_IPython |
|
45 | from IPython.ipmaker import make_IPython | |
46 | from IPython.Magic import Magic |
|
46 | from IPython.Magic import Magic | |
47 | from IPython.ipstruct import Struct |
|
47 | from IPython.ipstruct import Struct | |
48 |
|
48 | |||
49 | # Globals |
|
49 | # Globals | |
50 | # global flag to pass around information about Ctrl-C without exceptions |
|
50 | # global flag to pass around information about Ctrl-C without exceptions | |
51 | KBINT = False |
|
51 | KBINT = False | |
52 |
|
52 | |||
53 | # global flag to turn on/off Tk support. |
|
53 | # global flag to turn on/off Tk support. | |
54 | USE_TK = False |
|
54 | USE_TK = False | |
55 |
|
55 | |||
56 | # ID for the main thread, used for cross-thread exceptions |
|
56 | # ID for the main thread, used for cross-thread exceptions | |
57 | MAIN_THREAD_ID = thread.get_ident() |
|
57 | MAIN_THREAD_ID = thread.get_ident() | |
58 |
|
58 | |||
59 | # Tag when runcode() is active, for exception handling |
|
59 | # Tag when runcode() is active, for exception handling | |
60 | CODE_RUN = None |
|
60 | CODE_RUN = None | |
61 |
|
61 | |||
62 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
63 | # This class is trivial now, but I want to have it in to publish a clean |
|
63 | # This class is trivial now, but I want to have it in to publish a clean | |
64 | # interface. Later when the internals are reorganized, code that uses this |
|
64 | # interface. Later when the internals are reorganized, code that uses this | |
65 | # shouldn't have to change. |
|
65 | # shouldn't have to change. | |
66 |
|
66 | |||
67 | class IPShell: |
|
67 | class IPShell: | |
68 | """Create an IPython instance.""" |
|
68 | """Create an IPython instance.""" | |
69 |
|
69 | |||
70 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
70 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
71 | debug=1,shell_class=InteractiveShell): |
|
71 | debug=1,shell_class=InteractiveShell): | |
72 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
72 | self.IP = make_IPython(argv,user_ns=user_ns, | |
73 | user_global_ns=user_global_ns, |
|
73 | user_global_ns=user_global_ns, | |
74 | debug=debug,shell_class=shell_class) |
|
74 | debug=debug,shell_class=shell_class) | |
75 |
|
75 | |||
76 | def mainloop(self,sys_exit=0,banner=None): |
|
76 | def mainloop(self,sys_exit=0,banner=None): | |
77 | self.IP.mainloop(banner) |
|
77 | self.IP.mainloop(banner) | |
78 | if sys_exit: |
|
78 | if sys_exit: | |
79 | sys.exit() |
|
79 | sys.exit() | |
80 |
|
80 | |||
81 | #----------------------------------------------------------------------------- |
|
81 | #----------------------------------------------------------------------------- | |
|
82 | def kill_embedded(self,parameter_s=''): | |||
|
83 | """%kill_embedded : deactivate for good the current embedded IPython. | |||
|
84 | ||||
|
85 | This function (after asking for confirmation) sets an internal flag so that | |||
|
86 | an embedded IPython will never activate again. This is useful to | |||
|
87 | permanently disable a shell that is being called inside a loop: once you've | |||
|
88 | figured out what you needed from it, you may then kill it and the program | |||
|
89 | will then continue to run without the interactive shell interfering again. | |||
|
90 | """ | |||
|
91 | ||||
|
92 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " | |||
|
93 | "(y/n)? [y/N] ",'n') | |||
|
94 | if kill: | |||
|
95 | self.shell.embedded_active = False | |||
|
96 | print "This embedded IPython will not reactivate anymore once you exit." | |||
|
97 | ||||
82 | class IPShellEmbed: |
|
98 | class IPShellEmbed: | |
83 | """Allow embedding an IPython shell into a running program. |
|
99 | """Allow embedding an IPython shell into a running program. | |
84 |
|
100 | |||
85 | Instances of this class are callable, with the __call__ method being an |
|
101 | Instances of this class are callable, with the __call__ method being an | |
86 | alias to the embed() method of an InteractiveShell instance. |
|
102 | alias to the embed() method of an InteractiveShell instance. | |
87 |
|
103 | |||
88 | Usage (see also the example-embed.py file for a running example): |
|
104 | Usage (see also the example-embed.py file for a running example): | |
89 |
|
105 | |||
90 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) |
|
106 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) | |
91 |
|
107 | |||
92 | - argv: list containing valid command-line options for IPython, as they |
|
108 | - argv: list containing valid command-line options for IPython, as they | |
93 | would appear in sys.argv[1:]. |
|
109 | would appear in sys.argv[1:]. | |
94 |
|
110 | |||
95 | For example, the following command-line options: |
|
111 | For example, the following command-line options: | |
96 |
|
112 | |||
97 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG |
|
113 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG | |
98 |
|
114 | |||
99 | would be passed in the argv list as: |
|
115 | would be passed in the argv list as: | |
100 |
|
116 | |||
101 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] |
|
117 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] | |
102 |
|
118 | |||
103 | - banner: string which gets printed every time the interpreter starts. |
|
119 | - banner: string which gets printed every time the interpreter starts. | |
104 |
|
120 | |||
105 | - exit_msg: string which gets printed every time the interpreter exits. |
|
121 | - exit_msg: string which gets printed every time the interpreter exits. | |
106 |
|
122 | |||
107 | - rc_override: a dict or Struct of configuration options such as those |
|
123 | - rc_override: a dict or Struct of configuration options such as those | |
108 | used by IPython. These options are read from your ~/.ipython/ipythonrc |
|
124 | used by IPython. These options are read from your ~/.ipython/ipythonrc | |
109 | file when the Shell object is created. Passing an explicit rc_override |
|
125 | file when the Shell object is created. Passing an explicit rc_override | |
110 | dict with any options you want allows you to override those values at |
|
126 | dict with any options you want allows you to override those values at | |
111 | creation time without having to modify the file. This way you can create |
|
127 | creation time without having to modify the file. This way you can create | |
112 | embeddable instances configured in any way you want without editing any |
|
128 | embeddable instances configured in any way you want without editing any | |
113 | global files (thus keeping your interactive IPython configuration |
|
129 | global files (thus keeping your interactive IPython configuration | |
114 | unchanged). |
|
130 | unchanged). | |
115 |
|
131 | |||
116 | Then the ipshell instance can be called anywhere inside your code: |
|
132 | Then the ipshell instance can be called anywhere inside your code: | |
117 |
|
133 | |||
118 | ipshell(header='') -> Opens up an IPython shell. |
|
134 | ipshell(header='') -> Opens up an IPython shell. | |
119 |
|
135 | |||
120 | - header: string printed by the IPython shell upon startup. This can let |
|
136 | - header: string printed by the IPython shell upon startup. This can let | |
121 | you know where in your code you are when dropping into the shell. Note |
|
137 | you know where in your code you are when dropping into the shell. Note | |
122 | that 'banner' gets prepended to all calls, so header is used for |
|
138 | that 'banner' gets prepended to all calls, so header is used for | |
123 | location-specific information. |
|
139 | location-specific information. | |
124 |
|
140 | |||
125 | For more details, see the __call__ method below. |
|
141 | For more details, see the __call__ method below. | |
126 |
|
142 | |||
127 | When the IPython shell is exited with Ctrl-D, normal program execution |
|
143 | When the IPython shell is exited with Ctrl-D, normal program execution | |
128 | resumes. |
|
144 | resumes. | |
129 |
|
145 | |||
130 | This functionality was inspired by a posting on comp.lang.python by cmkl |
|
146 | This functionality was inspired by a posting on comp.lang.python by cmkl | |
131 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and |
|
147 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and | |
132 | by the IDL stop/continue commands.""" |
|
148 | by the IDL stop/continue commands.""" | |
133 |
|
149 | |||
134 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None, |
|
150 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None, | |
135 | user_ns=None): |
|
151 | user_ns=None): | |
136 | """Note that argv here is a string, NOT a list.""" |
|
152 | """Note that argv here is a string, NOT a list.""" | |
137 | self.set_banner(banner) |
|
153 | self.set_banner(banner) | |
138 | self.set_exit_msg(exit_msg) |
|
154 | self.set_exit_msg(exit_msg) | |
139 | self.set_dummy_mode(0) |
|
155 | self.set_dummy_mode(0) | |
140 |
|
156 | |||
141 | # sys.displayhook is a global, we need to save the user's original |
|
157 | # sys.displayhook is a global, we need to save the user's original | |
142 | # Don't rely on __displayhook__, as the user may have changed that. |
|
158 | # Don't rely on __displayhook__, as the user may have changed that. | |
143 | self.sys_displayhook_ori = sys.displayhook |
|
159 | self.sys_displayhook_ori = sys.displayhook | |
144 |
|
160 | |||
145 | # save readline completer status |
|
161 | # save readline completer status | |
146 | try: |
|
162 | try: | |
147 | #print 'Save completer',sys.ipcompleter # dbg |
|
163 | #print 'Save completer',sys.ipcompleter # dbg | |
148 | self.sys_ipcompleter_ori = sys.ipcompleter |
|
164 | self.sys_ipcompleter_ori = sys.ipcompleter | |
149 | except: |
|
165 | except: | |
150 | pass # not nested with IPython |
|
166 | pass # not nested with IPython | |
151 |
|
167 | |||
152 | self.IP = make_IPython(argv,rc_override=rc_override, |
|
168 | self.IP = make_IPython(argv,rc_override=rc_override, | |
153 | embedded=True, |
|
169 | embedded=True, | |
154 | user_ns=user_ns) |
|
170 | user_ns=user_ns) | |
155 |
|
171 | |||
|
172 | ip = ipapi.IPApi(self.IP) | |||
|
173 | ip.expose_magic("kill_embedded",kill_embedded) | |||
|
174 | ||||
156 | # copy our own displayhook also |
|
175 | # copy our own displayhook also | |
157 | self.sys_displayhook_embed = sys.displayhook |
|
176 | self.sys_displayhook_embed = sys.displayhook | |
158 | # and leave the system's display hook clean |
|
177 | # and leave the system's display hook clean | |
159 | sys.displayhook = self.sys_displayhook_ori |
|
178 | sys.displayhook = self.sys_displayhook_ori | |
160 | # don't use the ipython crash handler so that user exceptions aren't |
|
179 | # don't use the ipython crash handler so that user exceptions aren't | |
161 | # trapped |
|
180 | # trapped | |
162 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, |
|
181 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, | |
163 | mode = self.IP.rc.xmode, |
|
182 | mode = self.IP.rc.xmode, | |
164 | call_pdb = self.IP.rc.pdb) |
|
183 | call_pdb = self.IP.rc.pdb) | |
165 | self.restore_system_completer() |
|
184 | self.restore_system_completer() | |
166 |
|
185 | |||
167 | def restore_system_completer(self): |
|
186 | def restore_system_completer(self): | |
168 | """Restores the readline completer which was in place. |
|
187 | """Restores the readline completer which was in place. | |
169 |
|
188 | |||
170 | This allows embedded IPython within IPython not to disrupt the |
|
189 | This allows embedded IPython within IPython not to disrupt the | |
171 | parent's completion. |
|
190 | parent's completion. | |
172 | """ |
|
191 | """ | |
173 |
|
192 | |||
174 | try: |
|
193 | try: | |
175 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) |
|
194 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) | |
176 | sys.ipcompleter = self.sys_ipcompleter_ori |
|
195 | sys.ipcompleter = self.sys_ipcompleter_ori | |
177 | except: |
|
196 | except: | |
178 | pass |
|
197 | pass | |
179 |
|
198 | |||
180 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): |
|
199 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): | |
181 | """Activate the interactive interpreter. |
|
200 | """Activate the interactive interpreter. | |
182 |
|
201 | |||
183 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
202 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start | |
184 | the interpreter shell with the given local and global namespaces, and |
|
203 | the interpreter shell with the given local and global namespaces, and | |
185 | optionally print a header string at startup. |
|
204 | optionally print a header string at startup. | |
186 |
|
205 | |||
187 | The shell can be globally activated/deactivated using the |
|
206 | The shell can be globally activated/deactivated using the | |
188 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
207 | set/get_dummy_mode methods. This allows you to turn off a shell used | |
189 | for debugging globally. |
|
208 | for debugging globally. | |
190 |
|
209 | |||
191 | However, *each* time you call the shell you can override the current |
|
210 | However, *each* time you call the shell you can override the current | |
192 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
211 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
193 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
212 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you | |
194 | can still have a specific call work by making it as IPShell(dummy=0). |
|
213 | can still have a specific call work by making it as IPShell(dummy=0). | |
195 |
|
214 | |||
196 | The optional keyword parameter dummy controls whether the call |
|
215 | The optional keyword parameter dummy controls whether the call | |
197 | actually does anything. """ |
|
216 | actually does anything. """ | |
198 |
|
217 | |||
|
218 | # If the user has turned it off, go away | |||
|
219 | if not self.IP.embedded_active: | |||
|
220 | return | |||
|
221 | ||||
|
222 | # Normal exits from interactive mode set this flag, so the shell can't | |||
|
223 | # re-enter (it checks this variable at the start of interactive mode). | |||
|
224 | self.IP.exit_now = False | |||
|
225 | ||||
199 | # Allow the dummy parameter to override the global __dummy_mode |
|
226 | # Allow the dummy parameter to override the global __dummy_mode | |
200 | if dummy or (dummy != 0 and self.__dummy_mode): |
|
227 | if dummy or (dummy != 0 and self.__dummy_mode): | |
201 | return |
|
228 | return | |
202 |
|
229 | |||
203 | # Set global subsystems (display,completions) to our values |
|
230 | # Set global subsystems (display,completions) to our values | |
204 | sys.displayhook = self.sys_displayhook_embed |
|
231 | sys.displayhook = self.sys_displayhook_embed | |
205 | if self.IP.has_readline: |
|
232 | if self.IP.has_readline: | |
206 | self.IP.set_completer() |
|
233 | self.IP.set_completer() | |
207 |
|
234 | |||
208 | if self.banner and header: |
|
235 | if self.banner and header: | |
209 | format = '%s\n%s\n' |
|
236 | format = '%s\n%s\n' | |
210 | else: |
|
237 | else: | |
211 | format = '%s%s\n' |
|
238 | format = '%s%s\n' | |
212 | banner = format % (self.banner,header) |
|
239 | banner = format % (self.banner,header) | |
213 |
|
240 | |||
214 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
241 | # Call the embedding code with a stack depth of 1 so it can skip over | |
215 | # our call and get the original caller's namespaces. |
|
242 | # our call and get the original caller's namespaces. | |
216 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) |
|
243 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) | |
217 |
|
244 | |||
218 | if self.exit_msg: |
|
245 | if self.exit_msg: | |
219 | print self.exit_msg |
|
246 | print self.exit_msg | |
220 |
|
247 | |||
221 | # Restore global systems (display, completion) |
|
248 | # Restore global systems (display, completion) | |
222 | sys.displayhook = self.sys_displayhook_ori |
|
249 | sys.displayhook = self.sys_displayhook_ori | |
223 | self.restore_system_completer() |
|
250 | self.restore_system_completer() | |
224 |
|
251 | |||
225 | def set_dummy_mode(self,dummy): |
|
252 | def set_dummy_mode(self,dummy): | |
226 | """Sets the embeddable shell's dummy mode parameter. |
|
253 | """Sets the embeddable shell's dummy mode parameter. | |
227 |
|
254 | |||
228 | set_dummy_mode(dummy): dummy = 0 or 1. |
|
255 | set_dummy_mode(dummy): dummy = 0 or 1. | |
229 |
|
256 | |||
230 | This parameter is persistent and makes calls to the embeddable shell |
|
257 | This parameter is persistent and makes calls to the embeddable shell | |
231 | silently return without performing any action. This allows you to |
|
258 | silently return without performing any action. This allows you to | |
232 | globally activate or deactivate a shell you're using with a single call. |
|
259 | globally activate or deactivate a shell you're using with a single call. | |
233 |
|
260 | |||
234 | If you need to manually""" |
|
261 | If you need to manually""" | |
235 |
|
262 | |||
236 | if dummy not in [0,1,False,True]: |
|
263 | if dummy not in [0,1,False,True]: | |
237 | raise ValueError,'dummy parameter must be boolean' |
|
264 | raise ValueError,'dummy parameter must be boolean' | |
238 | self.__dummy_mode = dummy |
|
265 | self.__dummy_mode = dummy | |
239 |
|
266 | |||
240 | def get_dummy_mode(self): |
|
267 | def get_dummy_mode(self): | |
241 | """Return the current value of the dummy mode parameter. |
|
268 | """Return the current value of the dummy mode parameter. | |
242 | """ |
|
269 | """ | |
243 | return self.__dummy_mode |
|
270 | return self.__dummy_mode | |
244 |
|
271 | |||
245 | def set_banner(self,banner): |
|
272 | def set_banner(self,banner): | |
246 | """Sets the global banner. |
|
273 | """Sets the global banner. | |
247 |
|
274 | |||
248 | This banner gets prepended to every header printed when the shell |
|
275 | This banner gets prepended to every header printed when the shell | |
249 | instance is called.""" |
|
276 | instance is called.""" | |
250 |
|
277 | |||
251 | self.banner = banner |
|
278 | self.banner = banner | |
252 |
|
279 | |||
253 | def set_exit_msg(self,exit_msg): |
|
280 | def set_exit_msg(self,exit_msg): | |
254 | """Sets the global exit_msg. |
|
281 | """Sets the global exit_msg. | |
255 |
|
282 | |||
256 | This exit message gets printed upon exiting every time the embedded |
|
283 | This exit message gets printed upon exiting every time the embedded | |
257 | shell is called. It is None by default. """ |
|
284 | shell is called. It is None by default. """ | |
258 |
|
285 | |||
259 | self.exit_msg = exit_msg |
|
286 | self.exit_msg = exit_msg | |
260 |
|
287 | |||
261 | #----------------------------------------------------------------------------- |
|
288 | #----------------------------------------------------------------------------- | |
262 | if HAS_CTYPES: |
|
289 | if HAS_CTYPES: | |
263 | # Add async exception support. Trick taken from: |
|
290 | # Add async exception support. Trick taken from: | |
264 | # http://sebulba.wikispaces.com/recipe+thread2 |
|
291 | # http://sebulba.wikispaces.com/recipe+thread2 | |
265 | def _async_raise(tid, exctype): |
|
292 | def _async_raise(tid, exctype): | |
266 | """raises the exception, performs cleanup if needed""" |
|
293 | """raises the exception, performs cleanup if needed""" | |
267 | if not inspect.isclass(exctype): |
|
294 | if not inspect.isclass(exctype): | |
268 | raise TypeError("Only types can be raised (not instances)") |
|
295 | raise TypeError("Only types can be raised (not instances)") | |
269 | res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, |
|
296 | res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, | |
270 | ctypes.py_object(exctype)) |
|
297 | ctypes.py_object(exctype)) | |
271 | if res == 0: |
|
298 | if res == 0: | |
272 | raise ValueError("invalid thread id") |
|
299 | raise ValueError("invalid thread id") | |
273 | elif res != 1: |
|
300 | elif res != 1: | |
274 | # """if it returns a number greater than one, you're in trouble, |
|
301 | # """if it returns a number greater than one, you're in trouble, | |
275 | # and you should call it again with exc=NULL to revert the effect""" |
|
302 | # and you should call it again with exc=NULL to revert the effect""" | |
276 | ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0) |
|
303 | ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0) | |
277 | raise SystemError("PyThreadState_SetAsyncExc failed") |
|
304 | raise SystemError("PyThreadState_SetAsyncExc failed") | |
278 |
|
305 | |||
279 | def sigint_handler (signum,stack_frame): |
|
306 | def sigint_handler (signum,stack_frame): | |
280 | """Sigint handler for threaded apps. |
|
307 | """Sigint handler for threaded apps. | |
281 |
|
308 | |||
282 | This is a horrible hack to pass information about SIGINT _without_ |
|
309 | This is a horrible hack to pass information about SIGINT _without_ | |
283 | using exceptions, since I haven't been able to properly manage |
|
310 | using exceptions, since I haven't been able to properly manage | |
284 | cross-thread exceptions in GTK/WX. In fact, I don't think it can be |
|
311 | cross-thread exceptions in GTK/WX. In fact, I don't think it can be | |
285 | done (or at least that's my understanding from a c.l.py thread where |
|
312 | done (or at least that's my understanding from a c.l.py thread where | |
286 | this was discussed).""" |
|
313 | this was discussed).""" | |
287 |
|
314 | |||
288 | global KBINT |
|
315 | global KBINT | |
289 |
|
316 | |||
290 | if CODE_RUN: |
|
317 | if CODE_RUN: | |
291 | _async_raise(MAIN_THREAD_ID,KeyboardInterrupt) |
|
318 | _async_raise(MAIN_THREAD_ID,KeyboardInterrupt) | |
292 | else: |
|
319 | else: | |
293 | KBINT = True |
|
320 | KBINT = True | |
294 | print '\nKeyboardInterrupt - Press <Enter> to continue.', |
|
321 | print '\nKeyboardInterrupt - Press <Enter> to continue.', | |
295 | Term.cout.flush() |
|
322 | Term.cout.flush() | |
296 |
|
323 | |||
297 | else: |
|
324 | else: | |
298 | def sigint_handler (signum,stack_frame): |
|
325 | def sigint_handler (signum,stack_frame): | |
299 | """Sigint handler for threaded apps. |
|
326 | """Sigint handler for threaded apps. | |
300 |
|
327 | |||
301 | This is a horrible hack to pass information about SIGINT _without_ |
|
328 | This is a horrible hack to pass information about SIGINT _without_ | |
302 | using exceptions, since I haven't been able to properly manage |
|
329 | using exceptions, since I haven't been able to properly manage | |
303 | cross-thread exceptions in GTK/WX. In fact, I don't think it can be |
|
330 | cross-thread exceptions in GTK/WX. In fact, I don't think it can be | |
304 | done (or at least that's my understanding from a c.l.py thread where |
|
331 | done (or at least that's my understanding from a c.l.py thread where | |
305 | this was discussed).""" |
|
332 | this was discussed).""" | |
306 |
|
333 | |||
307 | global KBINT |
|
334 | global KBINT | |
308 |
|
335 | |||
309 | print '\nKeyboardInterrupt - Press <Enter> to continue.', |
|
336 | print '\nKeyboardInterrupt - Press <Enter> to continue.', | |
310 | Term.cout.flush() |
|
337 | Term.cout.flush() | |
311 | # Set global flag so that runsource can know that Ctrl-C was hit |
|
338 | # Set global flag so that runsource can know that Ctrl-C was hit | |
312 | KBINT = True |
|
339 | KBINT = True | |
313 |
|
340 | |||
314 |
|
341 | |||
315 | class MTInteractiveShell(InteractiveShell): |
|
342 | class MTInteractiveShell(InteractiveShell): | |
316 | """Simple multi-threaded shell.""" |
|
343 | """Simple multi-threaded shell.""" | |
317 |
|
344 | |||
318 | # Threading strategy taken from: |
|
345 | # Threading strategy taken from: | |
319 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian |
|
346 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian | |
320 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, |
|
347 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, | |
321 | # from the pygtk mailing list, to avoid lockups with system calls. |
|
348 | # from the pygtk mailing list, to avoid lockups with system calls. | |
322 |
|
349 | |||
323 | # class attribute to indicate whether the class supports threads or not. |
|
350 | # class attribute to indicate whether the class supports threads or not. | |
324 | # Subclasses with thread support should override this as needed. |
|
351 | # Subclasses with thread support should override this as needed. | |
325 | isthreaded = True |
|
352 | isthreaded = True | |
326 |
|
353 | |||
327 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
354 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
328 | user_ns=None,user_global_ns=None,banner2='',**kw): |
|
355 | user_ns=None,user_global_ns=None,banner2='',**kw): | |
329 | """Similar to the normal InteractiveShell, but with threading control""" |
|
356 | """Similar to the normal InteractiveShell, but with threading control""" | |
330 |
|
357 | |||
331 | InteractiveShell.__init__(self,name,usage,rc,user_ns, |
|
358 | InteractiveShell.__init__(self,name,usage,rc,user_ns, | |
332 | user_global_ns,banner2) |
|
359 | user_global_ns,banner2) | |
333 |
|
360 | |||
334 | # Locking control variable. We need to use a norma lock, not an RLock |
|
361 | # Locking control variable. We need to use a norma lock, not an RLock | |
335 | # here. I'm not exactly sure why, it seems to me like it should be |
|
362 | # here. I'm not exactly sure why, it seems to me like it should be | |
336 | # the opposite, but we deadlock with an RLock. Puzzled... |
|
363 | # the opposite, but we deadlock with an RLock. Puzzled... | |
337 | self.thread_ready = threading.Condition(threading.Lock()) |
|
364 | self.thread_ready = threading.Condition(threading.Lock()) | |
338 |
|
365 | |||
339 | # A queue to hold the code to be executed. A scalar variable is NOT |
|
366 | # A queue to hold the code to be executed. A scalar variable is NOT | |
340 | # enough, because uses like macros cause reentrancy. |
|
367 | # enough, because uses like macros cause reentrancy. | |
341 | self.code_queue = Queue.Queue() |
|
368 | self.code_queue = Queue.Queue() | |
342 |
|
369 | |||
343 | # Stuff to do at closing time |
|
370 | # Stuff to do at closing time | |
344 | self._kill = False |
|
371 | self._kill = False | |
345 | on_kill = kw.get('on_kill') |
|
372 | on_kill = kw.get('on_kill') | |
346 | if on_kill is None: |
|
373 | if on_kill is None: | |
347 | on_kill = [] |
|
374 | on_kill = [] | |
348 | # Check that all things to kill are callable: |
|
375 | # Check that all things to kill are callable: | |
349 | for t in on_kill: |
|
376 | for t in on_kill: | |
350 | if not callable(t): |
|
377 | if not callable(t): | |
351 | raise TypeError,'on_kill must be a list of callables' |
|
378 | raise TypeError,'on_kill must be a list of callables' | |
352 | self.on_kill = on_kill |
|
379 | self.on_kill = on_kill | |
353 |
|
380 | |||
354 | def runsource(self, source, filename="<input>", symbol="single"): |
|
381 | def runsource(self, source, filename="<input>", symbol="single"): | |
355 | """Compile and run some source in the interpreter. |
|
382 | """Compile and run some source in the interpreter. | |
356 |
|
383 | |||
357 | Modified version of code.py's runsource(), to handle threading issues. |
|
384 | Modified version of code.py's runsource(), to handle threading issues. | |
358 | See the original for full docstring details.""" |
|
385 | See the original for full docstring details.""" | |
359 |
|
386 | |||
360 | global KBINT |
|
387 | global KBINT | |
361 |
|
388 | |||
362 | # If Ctrl-C was typed, we reset the flag and return right away |
|
389 | # If Ctrl-C was typed, we reset the flag and return right away | |
363 | if KBINT: |
|
390 | if KBINT: | |
364 | KBINT = False |
|
391 | KBINT = False | |
365 | return False |
|
392 | return False | |
366 |
|
393 | |||
367 | try: |
|
394 | try: | |
368 | code = self.compile(source, filename, symbol) |
|
395 | code = self.compile(source, filename, symbol) | |
369 | except (OverflowError, SyntaxError, ValueError): |
|
396 | except (OverflowError, SyntaxError, ValueError): | |
370 | # Case 1 |
|
397 | # Case 1 | |
371 | self.showsyntaxerror(filename) |
|
398 | self.showsyntaxerror(filename) | |
372 | return False |
|
399 | return False | |
373 |
|
400 | |||
374 | if code is None: |
|
401 | if code is None: | |
375 | # Case 2 |
|
402 | # Case 2 | |
376 | return True |
|
403 | return True | |
377 |
|
404 | |||
378 | # Case 3 |
|
405 | # Case 3 | |
379 | # Store code in queue, so the execution thread can handle it. |
|
406 | # Store code in queue, so the execution thread can handle it. | |
380 |
|
407 | |||
381 | # Note that with macros and other applications, we MAY re-enter this |
|
408 | # Note that with macros and other applications, we MAY re-enter this | |
382 | # section, so we have to acquire the lock with non-blocking semantics, |
|
409 | # section, so we have to acquire the lock with non-blocking semantics, | |
383 | # else we deadlock. |
|
410 | # else we deadlock. | |
384 | got_lock = self.thread_ready.acquire(False) |
|
411 | got_lock = self.thread_ready.acquire(False) | |
385 | self.code_queue.put(code) |
|
412 | self.code_queue.put(code) | |
386 | if got_lock: |
|
413 | if got_lock: | |
387 | self.thread_ready.wait() # Wait until processed in timeout interval |
|
414 | self.thread_ready.wait() # Wait until processed in timeout interval | |
388 | self.thread_ready.release() |
|
415 | self.thread_ready.release() | |
389 |
|
416 | |||
390 | return False |
|
417 | return False | |
391 |
|
418 | |||
392 | def runcode(self): |
|
419 | def runcode(self): | |
393 | """Execute a code object. |
|
420 | """Execute a code object. | |
394 |
|
421 | |||
395 | Multithreaded wrapper around IPython's runcode().""" |
|
422 | Multithreaded wrapper around IPython's runcode().""" | |
396 |
|
423 | |||
397 | global CODE_RUN |
|
424 | global CODE_RUN | |
398 |
|
425 | |||
399 | # Exceptions need to be raised differently depending on which thread is |
|
426 | # Exceptions need to be raised differently depending on which thread is | |
400 | # active |
|
427 | # active | |
401 | CODE_RUN = True |
|
428 | CODE_RUN = True | |
402 |
|
429 | |||
403 | # lock thread-protected stuff |
|
430 | # lock thread-protected stuff | |
404 | got_lock = self.thread_ready.acquire(False) |
|
431 | got_lock = self.thread_ready.acquire(False) | |
405 |
|
432 | |||
406 | if self._kill: |
|
433 | if self._kill: | |
407 | print >>Term.cout, 'Closing threads...', |
|
434 | print >>Term.cout, 'Closing threads...', | |
408 | Term.cout.flush() |
|
435 | Term.cout.flush() | |
409 | for tokill in self.on_kill: |
|
436 | for tokill in self.on_kill: | |
410 | tokill() |
|
437 | tokill() | |
411 | print >>Term.cout, 'Done.' |
|
438 | print >>Term.cout, 'Done.' | |
412 |
|
439 | |||
413 | # Install sigint handler. We do it every time to ensure that if user |
|
440 | # Install sigint handler. We do it every time to ensure that if user | |
414 | # code modifies it, we restore our own handling. |
|
441 | # code modifies it, we restore our own handling. | |
415 | try: |
|
442 | try: | |
416 | signal(SIGINT,sigint_handler) |
|
443 | signal(SIGINT,sigint_handler) | |
417 | except SystemError: |
|
444 | except SystemError: | |
418 | # This happens under Windows, which seems to have all sorts |
|
445 | # This happens under Windows, which seems to have all sorts | |
419 | # of problems with signal handling. Oh well... |
|
446 | # of problems with signal handling. Oh well... | |
420 | pass |
|
447 | pass | |
421 |
|
448 | |||
422 | # Flush queue of pending code by calling the run methood of the parent |
|
449 | # Flush queue of pending code by calling the run methood of the parent | |
423 | # class with all items which may be in the queue. |
|
450 | # class with all items which may be in the queue. | |
424 | while 1: |
|
451 | while 1: | |
425 | try: |
|
452 | try: | |
426 | code_to_run = self.code_queue.get_nowait() |
|
453 | code_to_run = self.code_queue.get_nowait() | |
427 | except Queue.Empty: |
|
454 | except Queue.Empty: | |
428 | break |
|
455 | break | |
429 | if got_lock: |
|
456 | if got_lock: | |
430 | self.thread_ready.notify() |
|
457 | self.thread_ready.notify() | |
431 | InteractiveShell.runcode(self,code_to_run) |
|
458 | InteractiveShell.runcode(self,code_to_run) | |
432 | else: |
|
459 | else: | |
433 | break |
|
460 | break | |
434 |
|
461 | |||
435 | # We're done with thread-protected variables |
|
462 | # We're done with thread-protected variables | |
436 | if got_lock: |
|
463 | if got_lock: | |
437 | self.thread_ready.release() |
|
464 | self.thread_ready.release() | |
438 |
|
465 | |||
439 | # We're done... |
|
466 | # We're done... | |
440 | CODE_RUN = False |
|
467 | CODE_RUN = False | |
441 | # This MUST return true for gtk threading to work |
|
468 | # This MUST return true for gtk threading to work | |
442 | return True |
|
469 | return True | |
443 |
|
470 | |||
444 | def kill(self): |
|
471 | def kill(self): | |
445 | """Kill the thread, returning when it has been shut down.""" |
|
472 | """Kill the thread, returning when it has been shut down.""" | |
446 | got_lock = self.thread_ready.acquire(False) |
|
473 | got_lock = self.thread_ready.acquire(False) | |
447 | self._kill = True |
|
474 | self._kill = True | |
448 | if got_lock: |
|
475 | if got_lock: | |
449 | self.thread_ready.release() |
|
476 | self.thread_ready.release() | |
450 |
|
477 | |||
451 | class MatplotlibShellBase: |
|
478 | class MatplotlibShellBase: | |
452 | """Mixin class to provide the necessary modifications to regular IPython |
|
479 | """Mixin class to provide the necessary modifications to regular IPython | |
453 | shell classes for matplotlib support. |
|
480 | shell classes for matplotlib support. | |
454 |
|
481 | |||
455 | Given Python's MRO, this should be used as the FIRST class in the |
|
482 | Given Python's MRO, this should be used as the FIRST class in the | |
456 | inheritance hierarchy, so that it overrides the relevant methods.""" |
|
483 | inheritance hierarchy, so that it overrides the relevant methods.""" | |
457 |
|
484 | |||
458 | def _matplotlib_config(self,name,user_ns): |
|
485 | def _matplotlib_config(self,name,user_ns): | |
459 | """Return items needed to setup the user's shell with matplotlib""" |
|
486 | """Return items needed to setup the user's shell with matplotlib""" | |
460 |
|
487 | |||
461 | # Initialize matplotlib to interactive mode always |
|
488 | # Initialize matplotlib to interactive mode always | |
462 | import matplotlib |
|
489 | import matplotlib | |
463 | from matplotlib import backends |
|
490 | from matplotlib import backends | |
464 | matplotlib.interactive(True) |
|
491 | matplotlib.interactive(True) | |
465 |
|
492 | |||
466 | def use(arg): |
|
493 | def use(arg): | |
467 | """IPython wrapper for matplotlib's backend switcher. |
|
494 | """IPython wrapper for matplotlib's backend switcher. | |
468 |
|
495 | |||
469 | In interactive use, we can not allow switching to a different |
|
496 | In interactive use, we can not allow switching to a different | |
470 | interactive backend, since thread conflicts will most likely crash |
|
497 | interactive backend, since thread conflicts will most likely crash | |
471 | the python interpreter. This routine does a safety check first, |
|
498 | the python interpreter. This routine does a safety check first, | |
472 | and refuses to perform a dangerous switch. It still allows |
|
499 | and refuses to perform a dangerous switch. It still allows | |
473 | switching to non-interactive backends.""" |
|
500 | switching to non-interactive backends.""" | |
474 |
|
501 | |||
475 | if arg in backends.interactive_bk and arg != self.mpl_backend: |
|
502 | if arg in backends.interactive_bk and arg != self.mpl_backend: | |
476 | m=('invalid matplotlib backend switch.\n' |
|
503 | m=('invalid matplotlib backend switch.\n' | |
477 | 'This script attempted to switch to the interactive ' |
|
504 | 'This script attempted to switch to the interactive ' | |
478 | 'backend: `%s`\n' |
|
505 | 'backend: `%s`\n' | |
479 | 'Your current choice of interactive backend is: `%s`\n\n' |
|
506 | 'Your current choice of interactive backend is: `%s`\n\n' | |
480 | 'Switching interactive matplotlib backends at runtime\n' |
|
507 | 'Switching interactive matplotlib backends at runtime\n' | |
481 | 'would crash the python interpreter, ' |
|
508 | 'would crash the python interpreter, ' | |
482 | 'and IPython has blocked it.\n\n' |
|
509 | 'and IPython has blocked it.\n\n' | |
483 | 'You need to either change your choice of matplotlib backend\n' |
|
510 | 'You need to either change your choice of matplotlib backend\n' | |
484 | 'by editing your .matplotlibrc file, or run this script as a \n' |
|
511 | 'by editing your .matplotlibrc file, or run this script as a \n' | |
485 | 'standalone file from the command line, not using IPython.\n' % |
|
512 | 'standalone file from the command line, not using IPython.\n' % | |
486 | (arg,self.mpl_backend) ) |
|
513 | (arg,self.mpl_backend) ) | |
487 | raise RuntimeError, m |
|
514 | raise RuntimeError, m | |
488 | else: |
|
515 | else: | |
489 | self.mpl_use(arg) |
|
516 | self.mpl_use(arg) | |
490 | self.mpl_use._called = True |
|
517 | self.mpl_use._called = True | |
491 |
|
518 | |||
492 | self.matplotlib = matplotlib |
|
519 | self.matplotlib = matplotlib | |
493 | self.mpl_backend = matplotlib.rcParams['backend'] |
|
520 | self.mpl_backend = matplotlib.rcParams['backend'] | |
494 |
|
521 | |||
495 | # we also need to block switching of interactive backends by use() |
|
522 | # we also need to block switching of interactive backends by use() | |
496 | self.mpl_use = matplotlib.use |
|
523 | self.mpl_use = matplotlib.use | |
497 | self.mpl_use._called = False |
|
524 | self.mpl_use._called = False | |
498 | # overwrite the original matplotlib.use with our wrapper |
|
525 | # overwrite the original matplotlib.use with our wrapper | |
499 | matplotlib.use = use |
|
526 | matplotlib.use = use | |
500 |
|
527 | |||
501 | # This must be imported last in the matplotlib series, after |
|
528 | # This must be imported last in the matplotlib series, after | |
502 | # backend/interactivity choices have been made |
|
529 | # backend/interactivity choices have been made | |
503 | import matplotlib.pylab as pylab |
|
530 | import matplotlib.pylab as pylab | |
504 | self.pylab = pylab |
|
531 | self.pylab = pylab | |
505 |
|
532 | |||
506 | self.pylab.show._needmain = False |
|
533 | self.pylab.show._needmain = False | |
507 | # We need to detect at runtime whether show() is called by the user. |
|
534 | # We need to detect at runtime whether show() is called by the user. | |
508 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
535 | # For this, we wrap it into a decorator which adds a 'called' flag. | |
509 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) |
|
536 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) | |
510 |
|
537 | |||
511 | # Build a user namespace initialized with matplotlib/matlab features. |
|
538 | # Build a user namespace initialized with matplotlib/matlab features. | |
512 | user_ns = IPython.ipapi.make_user_ns(user_ns) |
|
539 | user_ns = IPython.ipapi.make_user_ns(user_ns) | |
513 |
|
540 | |||
514 | exec ("import matplotlib\n" |
|
541 | exec ("import matplotlib\n" | |
515 | "import matplotlib.pylab as pylab\n") in user_ns |
|
542 | "import matplotlib.pylab as pylab\n") in user_ns | |
516 |
|
543 | |||
517 | # Build matplotlib info banner |
|
544 | # Build matplotlib info banner | |
518 | b=""" |
|
545 | b=""" | |
519 | Welcome to pylab, a matplotlib-based Python environment. |
|
546 | Welcome to pylab, a matplotlib-based Python environment. | |
520 | For more information, type 'help(pylab)'. |
|
547 | For more information, type 'help(pylab)'. | |
521 | """ |
|
548 | """ | |
522 | return user_ns,b |
|
549 | return user_ns,b | |
523 |
|
550 | |||
524 | def mplot_exec(self,fname,*where,**kw): |
|
551 | def mplot_exec(self,fname,*where,**kw): | |
525 | """Execute a matplotlib script. |
|
552 | """Execute a matplotlib script. | |
526 |
|
553 | |||
527 | This is a call to execfile(), but wrapped in safeties to properly |
|
554 | This is a call to execfile(), but wrapped in safeties to properly | |
528 | handle interactive rendering and backend switching.""" |
|
555 | handle interactive rendering and backend switching.""" | |
529 |
|
556 | |||
530 | #print '*** Matplotlib runner ***' # dbg |
|
557 | #print '*** Matplotlib runner ***' # dbg | |
531 | # turn off rendering until end of script |
|
558 | # turn off rendering until end of script | |
532 | isInteractive = self.matplotlib.rcParams['interactive'] |
|
559 | isInteractive = self.matplotlib.rcParams['interactive'] | |
533 | self.matplotlib.interactive(False) |
|
560 | self.matplotlib.interactive(False) | |
534 | self.safe_execfile(fname,*where,**kw) |
|
561 | self.safe_execfile(fname,*where,**kw) | |
535 | self.matplotlib.interactive(isInteractive) |
|
562 | self.matplotlib.interactive(isInteractive) | |
536 | # make rendering call now, if the user tried to do it |
|
563 | # make rendering call now, if the user tried to do it | |
537 | if self.pylab.draw_if_interactive.called: |
|
564 | if self.pylab.draw_if_interactive.called: | |
538 | self.pylab.draw() |
|
565 | self.pylab.draw() | |
539 | self.pylab.draw_if_interactive.called = False |
|
566 | self.pylab.draw_if_interactive.called = False | |
540 |
|
567 | |||
541 | # if a backend switch was performed, reverse it now |
|
568 | # if a backend switch was performed, reverse it now | |
542 | if self.mpl_use._called: |
|
569 | if self.mpl_use._called: | |
543 | self.matplotlib.rcParams['backend'] = self.mpl_backend |
|
570 | self.matplotlib.rcParams['backend'] = self.mpl_backend | |
544 |
|
571 | |||
545 | def magic_run(self,parameter_s=''): |
|
572 | def magic_run(self,parameter_s=''): | |
546 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) |
|
573 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) | |
547 |
|
574 | |||
548 | # Fix the docstring so users see the original as well |
|
575 | # Fix the docstring so users see the original as well | |
549 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, |
|
576 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, | |
550 | "\n *** Modified %run for Matplotlib," |
|
577 | "\n *** Modified %run for Matplotlib," | |
551 | " with proper interactive handling ***") |
|
578 | " with proper interactive handling ***") | |
552 |
|
579 | |||
553 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single |
|
580 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single | |
554 | # and multithreaded. Note that these are meant for internal use, the IPShell* |
|
581 | # and multithreaded. Note that these are meant for internal use, the IPShell* | |
555 | # classes below are the ones meant for public consumption. |
|
582 | # classes below are the ones meant for public consumption. | |
556 |
|
583 | |||
557 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): |
|
584 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): | |
558 | """Single-threaded shell with matplotlib support.""" |
|
585 | """Single-threaded shell with matplotlib support.""" | |
559 |
|
586 | |||
560 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
587 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
561 | user_ns=None,user_global_ns=None,**kw): |
|
588 | user_ns=None,user_global_ns=None,**kw): | |
562 | user_ns,b2 = self._matplotlib_config(name,user_ns) |
|
589 | user_ns,b2 = self._matplotlib_config(name,user_ns) | |
563 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
590 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, | |
564 | banner2=b2,**kw) |
|
591 | banner2=b2,**kw) | |
565 |
|
592 | |||
566 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): |
|
593 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): | |
567 | """Multi-threaded shell with matplotlib support.""" |
|
594 | """Multi-threaded shell with matplotlib support.""" | |
568 |
|
595 | |||
569 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
596 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
570 | user_ns=None,user_global_ns=None, **kw): |
|
597 | user_ns=None,user_global_ns=None, **kw): | |
571 | user_ns,b2 = self._matplotlib_config(name,user_ns) |
|
598 | user_ns,b2 = self._matplotlib_config(name,user_ns) | |
572 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
599 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, | |
573 | banner2=b2,**kw) |
|
600 | banner2=b2,**kw) | |
574 |
|
601 | |||
575 | #----------------------------------------------------------------------------- |
|
602 | #----------------------------------------------------------------------------- | |
576 | # Utility functions for the different GUI enabled IPShell* classes. |
|
603 | # Utility functions for the different GUI enabled IPShell* classes. | |
577 |
|
604 | |||
578 | def get_tk(): |
|
605 | def get_tk(): | |
579 | """Tries to import Tkinter and returns a withdrawn Tkinter root |
|
606 | """Tries to import Tkinter and returns a withdrawn Tkinter root | |
580 | window. If Tkinter is already imported or not available, this |
|
607 | window. If Tkinter is already imported or not available, this | |
581 | returns None. This function calls `hijack_tk` underneath. |
|
608 | returns None. This function calls `hijack_tk` underneath. | |
582 | """ |
|
609 | """ | |
583 | if not USE_TK or sys.modules.has_key('Tkinter'): |
|
610 | if not USE_TK or sys.modules.has_key('Tkinter'): | |
584 | return None |
|
611 | return None | |
585 | else: |
|
612 | else: | |
586 | try: |
|
613 | try: | |
587 | import Tkinter |
|
614 | import Tkinter | |
588 | except ImportError: |
|
615 | except ImportError: | |
589 | return None |
|
616 | return None | |
590 | else: |
|
617 | else: | |
591 | hijack_tk() |
|
618 | hijack_tk() | |
592 | r = Tkinter.Tk() |
|
619 | r = Tkinter.Tk() | |
593 | r.withdraw() |
|
620 | r.withdraw() | |
594 | return r |
|
621 | return r | |
595 |
|
622 | |||
596 | def hijack_tk(): |
|
623 | def hijack_tk(): | |
597 | """Modifies Tkinter's mainloop with a dummy so when a module calls |
|
624 | """Modifies Tkinter's mainloop with a dummy so when a module calls | |
598 | mainloop, it does not block. |
|
625 | mainloop, it does not block. | |
599 |
|
626 | |||
600 | """ |
|
627 | """ | |
601 | def misc_mainloop(self, n=0): |
|
628 | def misc_mainloop(self, n=0): | |
602 | pass |
|
629 | pass | |
603 | def tkinter_mainloop(n=0): |
|
630 | def tkinter_mainloop(n=0): | |
604 | pass |
|
631 | pass | |
605 |
|
632 | |||
606 | import Tkinter |
|
633 | import Tkinter | |
607 | Tkinter.Misc.mainloop = misc_mainloop |
|
634 | Tkinter.Misc.mainloop = misc_mainloop | |
608 | Tkinter.mainloop = tkinter_mainloop |
|
635 | Tkinter.mainloop = tkinter_mainloop | |
609 |
|
636 | |||
610 | def update_tk(tk): |
|
637 | def update_tk(tk): | |
611 | """Updates the Tkinter event loop. This is typically called from |
|
638 | """Updates the Tkinter event loop. This is typically called from | |
612 | the respective WX or GTK mainloops. |
|
639 | the respective WX or GTK mainloops. | |
613 | """ |
|
640 | """ | |
614 | if tk: |
|
641 | if tk: | |
615 | tk.update() |
|
642 | tk.update() | |
616 |
|
643 | |||
617 | def hijack_wx(): |
|
644 | def hijack_wx(): | |
618 | """Modifies wxPython's MainLoop with a dummy so user code does not |
|
645 | """Modifies wxPython's MainLoop with a dummy so user code does not | |
619 | block IPython. The hijacked mainloop function is returned. |
|
646 | block IPython. The hijacked mainloop function is returned. | |
620 | """ |
|
647 | """ | |
621 | def dummy_mainloop(*args, **kw): |
|
648 | def dummy_mainloop(*args, **kw): | |
622 | pass |
|
649 | pass | |
623 |
|
650 | |||
624 | try: |
|
651 | try: | |
625 | import wx |
|
652 | import wx | |
626 | except ImportError: |
|
653 | except ImportError: | |
627 | # For very old versions of WX |
|
654 | # For very old versions of WX | |
628 | import wxPython as wx |
|
655 | import wxPython as wx | |
629 |
|
656 | |||
630 | ver = wx.__version__ |
|
657 | ver = wx.__version__ | |
631 | orig_mainloop = None |
|
658 | orig_mainloop = None | |
632 | if ver[:3] >= '2.5': |
|
659 | if ver[:3] >= '2.5': | |
633 | import wx |
|
660 | import wx | |
634 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') |
|
661 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') | |
635 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') |
|
662 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') | |
636 | else: raise AttributeError('Could not find wx core module') |
|
663 | else: raise AttributeError('Could not find wx core module') | |
637 | orig_mainloop = core.PyApp_MainLoop |
|
664 | orig_mainloop = core.PyApp_MainLoop | |
638 | core.PyApp_MainLoop = dummy_mainloop |
|
665 | core.PyApp_MainLoop = dummy_mainloop | |
639 | elif ver[:3] == '2.4': |
|
666 | elif ver[:3] == '2.4': | |
640 | orig_mainloop = wx.wxc.wxPyApp_MainLoop |
|
667 | orig_mainloop = wx.wxc.wxPyApp_MainLoop | |
641 | wx.wxc.wxPyApp_MainLoop = dummy_mainloop |
|
668 | wx.wxc.wxPyApp_MainLoop = dummy_mainloop | |
642 | else: |
|
669 | else: | |
643 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") |
|
670 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") | |
644 | return orig_mainloop |
|
671 | return orig_mainloop | |
645 |
|
672 | |||
646 | def hijack_gtk(): |
|
673 | def hijack_gtk(): | |
647 | """Modifies pyGTK's mainloop with a dummy so user code does not |
|
674 | """Modifies pyGTK's mainloop with a dummy so user code does not | |
648 | block IPython. This function returns the original `gtk.mainloop` |
|
675 | block IPython. This function returns the original `gtk.mainloop` | |
649 | function that has been hijacked. |
|
676 | function that has been hijacked. | |
650 | """ |
|
677 | """ | |
651 | def dummy_mainloop(*args, **kw): |
|
678 | def dummy_mainloop(*args, **kw): | |
652 | pass |
|
679 | pass | |
653 | import gtk |
|
680 | import gtk | |
654 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main |
|
681 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main | |
655 | else: orig_mainloop = gtk.mainloop |
|
682 | else: orig_mainloop = gtk.mainloop | |
656 | gtk.mainloop = dummy_mainloop |
|
683 | gtk.mainloop = dummy_mainloop | |
657 | gtk.main = dummy_mainloop |
|
684 | gtk.main = dummy_mainloop | |
658 | return orig_mainloop |
|
685 | return orig_mainloop | |
659 |
|
686 | |||
660 | #----------------------------------------------------------------------------- |
|
687 | #----------------------------------------------------------------------------- | |
661 | # The IPShell* classes below are the ones meant to be run by external code as |
|
688 | # The IPShell* classes below are the ones meant to be run by external code as | |
662 | # IPython instances. Note that unless a specific threading strategy is |
|
689 | # IPython instances. Note that unless a specific threading strategy is | |
663 | # desired, the factory function start() below should be used instead (it |
|
690 | # desired, the factory function start() below should be used instead (it | |
664 | # selects the proper threaded class). |
|
691 | # selects the proper threaded class). | |
665 |
|
692 | |||
666 | class IPThread(threading.Thread): |
|
693 | class IPThread(threading.Thread): | |
667 | def run(self): |
|
694 | def run(self): | |
668 | self.IP.mainloop(self._banner) |
|
695 | self.IP.mainloop(self._banner) | |
669 | self.IP.kill() |
|
696 | self.IP.kill() | |
670 |
|
697 | |||
671 | class IPShellGTK(IPThread): |
|
698 | class IPShellGTK(IPThread): | |
672 | """Run a gtk mainloop() in a separate thread. |
|
699 | """Run a gtk mainloop() in a separate thread. | |
673 |
|
700 | |||
674 | Python commands can be passed to the thread where they will be executed. |
|
701 | Python commands can be passed to the thread where they will be executed. | |
675 | This is implemented by periodically checking for passed code using a |
|
702 | This is implemented by periodically checking for passed code using a | |
676 | GTK timeout callback.""" |
|
703 | GTK timeout callback.""" | |
677 |
|
704 | |||
678 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
705 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
679 |
|
706 | |||
680 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
707 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
681 | debug=1,shell_class=MTInteractiveShell): |
|
708 | debug=1,shell_class=MTInteractiveShell): | |
682 |
|
709 | |||
683 | import gtk |
|
710 | import gtk | |
684 |
|
711 | |||
685 | self.gtk = gtk |
|
712 | self.gtk = gtk | |
686 | self.gtk_mainloop = hijack_gtk() |
|
713 | self.gtk_mainloop = hijack_gtk() | |
687 |
|
714 | |||
688 | # Allows us to use both Tk and GTK. |
|
715 | # Allows us to use both Tk and GTK. | |
689 | self.tk = get_tk() |
|
716 | self.tk = get_tk() | |
690 |
|
717 | |||
691 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit |
|
718 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit | |
692 | else: mainquit = self.gtk.mainquit |
|
719 | else: mainquit = self.gtk.mainquit | |
693 |
|
720 | |||
694 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
721 | self.IP = make_IPython(argv,user_ns=user_ns, | |
695 | user_global_ns=user_global_ns, |
|
722 | user_global_ns=user_global_ns, | |
696 | debug=debug, |
|
723 | debug=debug, | |
697 | shell_class=shell_class, |
|
724 | shell_class=shell_class, | |
698 | on_kill=[mainquit]) |
|
725 | on_kill=[mainquit]) | |
699 |
|
726 | |||
700 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
727 | # HACK: slot for banner in self; it will be passed to the mainloop | |
701 | # method only and .run() needs it. The actual value will be set by |
|
728 | # method only and .run() needs it. The actual value will be set by | |
702 | # .mainloop(). |
|
729 | # .mainloop(). | |
703 | self._banner = None |
|
730 | self._banner = None | |
704 |
|
731 | |||
705 | threading.Thread.__init__(self) |
|
732 | threading.Thread.__init__(self) | |
706 |
|
733 | |||
707 | def mainloop(self,sys_exit=0,banner=None): |
|
734 | def mainloop(self,sys_exit=0,banner=None): | |
708 |
|
735 | |||
709 | self._banner = banner |
|
736 | self._banner = banner | |
710 |
|
737 | |||
711 | if self.gtk.pygtk_version >= (2,4,0): |
|
738 | if self.gtk.pygtk_version >= (2,4,0): | |
712 | import gobject |
|
739 | import gobject | |
713 | gobject.idle_add(self.on_timer) |
|
740 | gobject.idle_add(self.on_timer) | |
714 | else: |
|
741 | else: | |
715 | self.gtk.idle_add(self.on_timer) |
|
742 | self.gtk.idle_add(self.on_timer) | |
716 |
|
743 | |||
717 | if sys.platform != 'win32': |
|
744 | if sys.platform != 'win32': | |
718 | try: |
|
745 | try: | |
719 | if self.gtk.gtk_version[0] >= 2: |
|
746 | if self.gtk.gtk_version[0] >= 2: | |
720 | self.gtk.gdk.threads_init() |
|
747 | self.gtk.gdk.threads_init() | |
721 | except AttributeError: |
|
748 | except AttributeError: | |
722 | pass |
|
749 | pass | |
723 | except RuntimeError: |
|
750 | except RuntimeError: | |
724 | error('Your pyGTK likely has not been compiled with ' |
|
751 | error('Your pyGTK likely has not been compiled with ' | |
725 | 'threading support.\n' |
|
752 | 'threading support.\n' | |
726 | 'The exception printout is below.\n' |
|
753 | 'The exception printout is below.\n' | |
727 | 'You can either rebuild pyGTK with threads, or ' |
|
754 | 'You can either rebuild pyGTK with threads, or ' | |
728 | 'try using \n' |
|
755 | 'try using \n' | |
729 | 'matplotlib with a different backend (like Tk or WX).\n' |
|
756 | 'matplotlib with a different backend (like Tk or WX).\n' | |
730 | 'Note that matplotlib will most likely not work in its ' |
|
757 | 'Note that matplotlib will most likely not work in its ' | |
731 | 'current state!') |
|
758 | 'current state!') | |
732 | self.IP.InteractiveTB() |
|
759 | self.IP.InteractiveTB() | |
733 |
|
760 | |||
734 | self.start() |
|
761 | self.start() | |
735 | self.gtk.gdk.threads_enter() |
|
762 | self.gtk.gdk.threads_enter() | |
736 | self.gtk_mainloop() |
|
763 | self.gtk_mainloop() | |
737 | self.gtk.gdk.threads_leave() |
|
764 | self.gtk.gdk.threads_leave() | |
738 | self.join() |
|
765 | self.join() | |
739 |
|
766 | |||
740 | def on_timer(self): |
|
767 | def on_timer(self): | |
741 | """Called when GTK is idle. |
|
768 | """Called when GTK is idle. | |
742 |
|
769 | |||
743 | Must return True always, otherwise GTK stops calling it""" |
|
770 | Must return True always, otherwise GTK stops calling it""" | |
744 |
|
771 | |||
745 | update_tk(self.tk) |
|
772 | update_tk(self.tk) | |
746 | self.IP.runcode() |
|
773 | self.IP.runcode() | |
747 | time.sleep(0.01) |
|
774 | time.sleep(0.01) | |
748 | return True |
|
775 | return True | |
749 |
|
776 | |||
750 |
|
777 | |||
751 | class IPShellWX(IPThread): |
|
778 | class IPShellWX(IPThread): | |
752 | """Run a wx mainloop() in a separate thread. |
|
779 | """Run a wx mainloop() in a separate thread. | |
753 |
|
780 | |||
754 | Python commands can be passed to the thread where they will be executed. |
|
781 | Python commands can be passed to the thread where they will be executed. | |
755 | This is implemented by periodically checking for passed code using a |
|
782 | This is implemented by periodically checking for passed code using a | |
756 | GTK timeout callback.""" |
|
783 | GTK timeout callback.""" | |
757 |
|
784 | |||
758 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
785 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
759 |
|
786 | |||
760 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
787 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
761 | debug=1,shell_class=MTInteractiveShell): |
|
788 | debug=1,shell_class=MTInteractiveShell): | |
762 |
|
789 | |||
763 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
790 | self.IP = make_IPython(argv,user_ns=user_ns, | |
764 | user_global_ns=user_global_ns, |
|
791 | user_global_ns=user_global_ns, | |
765 | debug=debug, |
|
792 | debug=debug, | |
766 | shell_class=shell_class, |
|
793 | shell_class=shell_class, | |
767 | on_kill=[self.wxexit]) |
|
794 | on_kill=[self.wxexit]) | |
768 |
|
795 | |||
769 | wantedwxversion=self.IP.rc.wxversion |
|
796 | wantedwxversion=self.IP.rc.wxversion | |
770 | if wantedwxversion!="0": |
|
797 | if wantedwxversion!="0": | |
771 | try: |
|
798 | try: | |
772 | import wxversion |
|
799 | import wxversion | |
773 | except ImportError: |
|
800 | except ImportError: | |
774 | error('The wxversion module is needed for WX version selection') |
|
801 | error('The wxversion module is needed for WX version selection') | |
775 | else: |
|
802 | else: | |
776 | try: |
|
803 | try: | |
777 | wxversion.select(wantedwxversion) |
|
804 | wxversion.select(wantedwxversion) | |
778 | except: |
|
805 | except: | |
779 | self.IP.InteractiveTB() |
|
806 | self.IP.InteractiveTB() | |
780 | error('Requested wxPython version %s could not be loaded' % |
|
807 | error('Requested wxPython version %s could not be loaded' % | |
781 | wantedwxversion) |
|
808 | wantedwxversion) | |
782 |
|
809 | |||
783 | import wx |
|
810 | import wx | |
784 |
|
811 | |||
785 | threading.Thread.__init__(self) |
|
812 | threading.Thread.__init__(self) | |
786 | self.wx = wx |
|
813 | self.wx = wx | |
787 | self.wx_mainloop = hijack_wx() |
|
814 | self.wx_mainloop = hijack_wx() | |
788 |
|
815 | |||
789 | # Allows us to use both Tk and GTK. |
|
816 | # Allows us to use both Tk and GTK. | |
790 | self.tk = get_tk() |
|
817 | self.tk = get_tk() | |
791 |
|
818 | |||
792 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
819 | # HACK: slot for banner in self; it will be passed to the mainloop | |
793 | # method only and .run() needs it. The actual value will be set by |
|
820 | # method only and .run() needs it. The actual value will be set by | |
794 | # .mainloop(). |
|
821 | # .mainloop(). | |
795 | self._banner = None |
|
822 | self._banner = None | |
796 |
|
823 | |||
797 | self.app = None |
|
824 | self.app = None | |
798 |
|
825 | |||
799 | def wxexit(self, *args): |
|
826 | def wxexit(self, *args): | |
800 | if self.app is not None: |
|
827 | if self.app is not None: | |
801 | self.app.agent.timer.Stop() |
|
828 | self.app.agent.timer.Stop() | |
802 | self.app.ExitMainLoop() |
|
829 | self.app.ExitMainLoop() | |
803 |
|
830 | |||
804 | def mainloop(self,sys_exit=0,banner=None): |
|
831 | def mainloop(self,sys_exit=0,banner=None): | |
805 |
|
832 | |||
806 | self._banner = banner |
|
833 | self._banner = banner | |
807 |
|
834 | |||
808 | self.start() |
|
835 | self.start() | |
809 |
|
836 | |||
810 | class TimerAgent(self.wx.MiniFrame): |
|
837 | class TimerAgent(self.wx.MiniFrame): | |
811 | wx = self.wx |
|
838 | wx = self.wx | |
812 | IP = self.IP |
|
839 | IP = self.IP | |
813 | tk = self.tk |
|
840 | tk = self.tk | |
814 | def __init__(self, parent, interval): |
|
841 | def __init__(self, parent, interval): | |
815 | style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ |
|
842 | style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ | |
816 | self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), |
|
843 | self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), | |
817 | size=(100, 100),style=style) |
|
844 | size=(100, 100),style=style) | |
818 | self.Show(False) |
|
845 | self.Show(False) | |
819 | self.interval = interval |
|
846 | self.interval = interval | |
820 | self.timerId = self.wx.NewId() |
|
847 | self.timerId = self.wx.NewId() | |
821 |
|
848 | |||
822 | def StartWork(self): |
|
849 | def StartWork(self): | |
823 | self.timer = self.wx.Timer(self, self.timerId) |
|
850 | self.timer = self.wx.Timer(self, self.timerId) | |
824 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) |
|
851 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) | |
825 | self.timer.Start(self.interval) |
|
852 | self.timer.Start(self.interval) | |
826 |
|
853 | |||
827 | def OnTimer(self, event): |
|
854 | def OnTimer(self, event): | |
828 | update_tk(self.tk) |
|
855 | update_tk(self.tk) | |
829 | self.IP.runcode() |
|
856 | self.IP.runcode() | |
830 |
|
857 | |||
831 | class App(self.wx.App): |
|
858 | class App(self.wx.App): | |
832 | wx = self.wx |
|
859 | wx = self.wx | |
833 | TIMEOUT = self.TIMEOUT |
|
860 | TIMEOUT = self.TIMEOUT | |
834 | def OnInit(self): |
|
861 | def OnInit(self): | |
835 | 'Create the main window and insert the custom frame' |
|
862 | 'Create the main window and insert the custom frame' | |
836 | self.agent = TimerAgent(None, self.TIMEOUT) |
|
863 | self.agent = TimerAgent(None, self.TIMEOUT) | |
837 | self.agent.Show(False) |
|
864 | self.agent.Show(False) | |
838 | self.agent.StartWork() |
|
865 | self.agent.StartWork() | |
839 | return True |
|
866 | return True | |
840 |
|
867 | |||
841 | self.app = App(redirect=False) |
|
868 | self.app = App(redirect=False) | |
842 | self.wx_mainloop(self.app) |
|
869 | self.wx_mainloop(self.app) | |
843 | self.join() |
|
870 | self.join() | |
844 |
|
871 | |||
845 |
|
872 | |||
846 | class IPShellQt(IPThread): |
|
873 | class IPShellQt(IPThread): | |
847 | """Run a Qt event loop in a separate thread. |
|
874 | """Run a Qt event loop in a separate thread. | |
848 |
|
875 | |||
849 | Python commands can be passed to the thread where they will be executed. |
|
876 | Python commands can be passed to the thread where they will be executed. | |
850 | This is implemented by periodically checking for passed code using a |
|
877 | This is implemented by periodically checking for passed code using a | |
851 | Qt timer / slot.""" |
|
878 | Qt timer / slot.""" | |
852 |
|
879 | |||
853 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
880 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
854 |
|
881 | |||
855 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
882 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
856 | debug=0,shell_class=MTInteractiveShell): |
|
883 | debug=0,shell_class=MTInteractiveShell): | |
857 |
|
884 | |||
858 | import qt |
|
885 | import qt | |
859 |
|
886 | |||
860 | class newQApplication: |
|
887 | class newQApplication: | |
861 | def __init__( self ): |
|
888 | def __init__( self ): | |
862 | self.QApplication = qt.QApplication |
|
889 | self.QApplication = qt.QApplication | |
863 |
|
890 | |||
864 | def __call__( *args, **kwargs ): |
|
891 | def __call__( *args, **kwargs ): | |
865 | return qt.qApp |
|
892 | return qt.qApp | |
866 |
|
893 | |||
867 | def exec_loop( *args, **kwargs ): |
|
894 | def exec_loop( *args, **kwargs ): | |
868 | pass |
|
895 | pass | |
869 |
|
896 | |||
870 | def __getattr__( self, name ): |
|
897 | def __getattr__( self, name ): | |
871 | return getattr( self.QApplication, name ) |
|
898 | return getattr( self.QApplication, name ) | |
872 |
|
899 | |||
873 | qt.QApplication = newQApplication() |
|
900 | qt.QApplication = newQApplication() | |
874 |
|
901 | |||
875 | # Allows us to use both Tk and QT. |
|
902 | # Allows us to use both Tk and QT. | |
876 | self.tk = get_tk() |
|
903 | self.tk = get_tk() | |
877 |
|
904 | |||
878 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
905 | self.IP = make_IPython(argv,user_ns=user_ns, | |
879 | user_global_ns=user_global_ns, |
|
906 | user_global_ns=user_global_ns, | |
880 | debug=debug, |
|
907 | debug=debug, | |
881 | shell_class=shell_class, |
|
908 | shell_class=shell_class, | |
882 | on_kill=[qt.qApp.exit]) |
|
909 | on_kill=[qt.qApp.exit]) | |
883 |
|
910 | |||
884 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
911 | # HACK: slot for banner in self; it will be passed to the mainloop | |
885 | # method only and .run() needs it. The actual value will be set by |
|
912 | # method only and .run() needs it. The actual value will be set by | |
886 | # .mainloop(). |
|
913 | # .mainloop(). | |
887 | self._banner = None |
|
914 | self._banner = None | |
888 |
|
915 | |||
889 | threading.Thread.__init__(self) |
|
916 | threading.Thread.__init__(self) | |
890 |
|
917 | |||
891 | def mainloop(self,sys_exit=0,banner=None): |
|
918 | def mainloop(self,sys_exit=0,banner=None): | |
892 |
|
919 | |||
893 | import qt |
|
920 | import qt | |
894 |
|
921 | |||
895 | self._banner = banner |
|
922 | self._banner = banner | |
896 |
|
923 | |||
897 | if qt.QApplication.startingUp(): |
|
924 | if qt.QApplication.startingUp(): | |
898 | a = qt.QApplication.QApplication(sys.argv) |
|
925 | a = qt.QApplication.QApplication(sys.argv) | |
899 | self.timer = qt.QTimer() |
|
926 | self.timer = qt.QTimer() | |
900 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) |
|
927 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) | |
901 |
|
928 | |||
902 | self.start() |
|
929 | self.start() | |
903 | self.timer.start( self.TIMEOUT, True ) |
|
930 | self.timer.start( self.TIMEOUT, True ) | |
904 | while True: |
|
931 | while True: | |
905 | if self.IP._kill: break |
|
932 | if self.IP._kill: break | |
906 | qt.qApp.exec_loop() |
|
933 | qt.qApp.exec_loop() | |
907 | self.join() |
|
934 | self.join() | |
908 |
|
935 | |||
909 | def on_timer(self): |
|
936 | def on_timer(self): | |
910 | update_tk(self.tk) |
|
937 | update_tk(self.tk) | |
911 | result = self.IP.runcode() |
|
938 | result = self.IP.runcode() | |
912 | self.timer.start( self.TIMEOUT, True ) |
|
939 | self.timer.start( self.TIMEOUT, True ) | |
913 | return result |
|
940 | return result | |
914 |
|
941 | |||
915 |
|
942 | |||
916 | class IPShellQt4(IPThread): |
|
943 | class IPShellQt4(IPThread): | |
917 | """Run a Qt event loop in a separate thread. |
|
944 | """Run a Qt event loop in a separate thread. | |
918 |
|
945 | |||
919 | Python commands can be passed to the thread where they will be executed. |
|
946 | Python commands can be passed to the thread where they will be executed. | |
920 | This is implemented by periodically checking for passed code using a |
|
947 | This is implemented by periodically checking for passed code using a | |
921 | Qt timer / slot.""" |
|
948 | Qt timer / slot.""" | |
922 |
|
949 | |||
923 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
950 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
924 |
|
951 | |||
925 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
952 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
926 | debug=0,shell_class=MTInteractiveShell): |
|
953 | debug=0,shell_class=MTInteractiveShell): | |
927 |
|
954 | |||
928 | from PyQt4 import QtCore, QtGui |
|
955 | from PyQt4 import QtCore, QtGui | |
929 |
|
956 | |||
930 | class newQApplication: |
|
957 | class newQApplication: | |
931 | def __init__( self ): |
|
958 | def __init__( self ): | |
932 | self.QApplication = QtGui.QApplication |
|
959 | self.QApplication = QtGui.QApplication | |
933 |
|
960 | |||
934 | def __call__( *args, **kwargs ): |
|
961 | def __call__( *args, **kwargs ): | |
935 | return QtGui.qApp |
|
962 | return QtGui.qApp | |
936 |
|
963 | |||
937 | def exec_loop( *args, **kwargs ): |
|
964 | def exec_loop( *args, **kwargs ): | |
938 | pass |
|
965 | pass | |
939 |
|
966 | |||
940 | def __getattr__( self, name ): |
|
967 | def __getattr__( self, name ): | |
941 | return getattr( self.QApplication, name ) |
|
968 | return getattr( self.QApplication, name ) | |
942 |
|
969 | |||
943 | QtGui.QApplication = newQApplication() |
|
970 | QtGui.QApplication = newQApplication() | |
944 |
|
971 | |||
945 | # Allows us to use both Tk and QT. |
|
972 | # Allows us to use both Tk and QT. | |
946 | self.tk = get_tk() |
|
973 | self.tk = get_tk() | |
947 |
|
974 | |||
948 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
975 | self.IP = make_IPython(argv,user_ns=user_ns, | |
949 | user_global_ns=user_global_ns, |
|
976 | user_global_ns=user_global_ns, | |
950 | debug=debug, |
|
977 | debug=debug, | |
951 | shell_class=shell_class, |
|
978 | shell_class=shell_class, | |
952 | on_kill=[QtGui.qApp.exit]) |
|
979 | on_kill=[QtGui.qApp.exit]) | |
953 |
|
980 | |||
954 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
981 | # HACK: slot for banner in self; it will be passed to the mainloop | |
955 | # method only and .run() needs it. The actual value will be set by |
|
982 | # method only and .run() needs it. The actual value will be set by | |
956 | # .mainloop(). |
|
983 | # .mainloop(). | |
957 | self._banner = None |
|
984 | self._banner = None | |
958 |
|
985 | |||
959 | threading.Thread.__init__(self) |
|
986 | threading.Thread.__init__(self) | |
960 |
|
987 | |||
961 | def mainloop(self,sys_exit=0,banner=None): |
|
988 | def mainloop(self,sys_exit=0,banner=None): | |
962 |
|
989 | |||
963 | from PyQt4 import QtCore, QtGui |
|
990 | from PyQt4 import QtCore, QtGui | |
964 |
|
991 | |||
965 | self._banner = banner |
|
992 | self._banner = banner | |
966 |
|
993 | |||
967 | if QtGui.QApplication.startingUp(): |
|
994 | if QtGui.QApplication.startingUp(): | |
968 | a = QtGui.QApplication.QApplication(sys.argv) |
|
995 | a = QtGui.QApplication.QApplication(sys.argv) | |
969 | self.timer = QtCore.QTimer() |
|
996 | self.timer = QtCore.QTimer() | |
970 | QtCore.QObject.connect( self.timer, QtCore.SIGNAL( 'timeout()' ), self.on_timer ) |
|
997 | QtCore.QObject.connect( self.timer, QtCore.SIGNAL( 'timeout()' ), self.on_timer ) | |
971 |
|
998 | |||
972 | self.start() |
|
999 | self.start() | |
973 | self.timer.start( self.TIMEOUT ) |
|
1000 | self.timer.start( self.TIMEOUT ) | |
974 | while True: |
|
1001 | while True: | |
975 | if self.IP._kill: break |
|
1002 | if self.IP._kill: break | |
976 | QtGui.qApp.exec_() |
|
1003 | QtGui.qApp.exec_() | |
977 | self.join() |
|
1004 | self.join() | |
978 |
|
1005 | |||
979 | def on_timer(self): |
|
1006 | def on_timer(self): | |
980 | update_tk(self.tk) |
|
1007 | update_tk(self.tk) | |
981 | result = self.IP.runcode() |
|
1008 | result = self.IP.runcode() | |
982 | self.timer.start( self.TIMEOUT ) |
|
1009 | self.timer.start( self.TIMEOUT ) | |
983 | return result |
|
1010 | return result | |
984 |
|
1011 | |||
985 |
|
1012 | |||
986 | # A set of matplotlib public IPython shell classes, for single-threaded (Tk* |
|
1013 | # A set of matplotlib public IPython shell classes, for single-threaded (Tk* | |
987 | # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use. |
|
1014 | # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use. | |
988 | def _load_pylab(user_ns): |
|
1015 | def _load_pylab(user_ns): | |
989 | """Allow users to disable pulling all of pylab into the top-level |
|
1016 | """Allow users to disable pulling all of pylab into the top-level | |
990 | namespace. |
|
1017 | namespace. | |
991 |
|
1018 | |||
992 | This little utility must be called AFTER the actual ipython instance is |
|
1019 | This little utility must be called AFTER the actual ipython instance is | |
993 | running, since only then will the options file have been fully parsed.""" |
|
1020 | running, since only then will the options file have been fully parsed.""" | |
994 |
|
1021 | |||
995 | ip = IPython.ipapi.get() |
|
1022 | ip = IPython.ipapi.get() | |
996 | if ip.options.pylab_import_all: |
|
1023 | if ip.options.pylab_import_all: | |
997 | exec "from matplotlib.pylab import *" in user_ns |
|
1024 | exec "from matplotlib.pylab import *" in user_ns | |
998 |
|
1025 | |||
999 | class IPShellMatplotlib(IPShell): |
|
1026 | class IPShellMatplotlib(IPShell): | |
1000 | """Subclass IPShell with MatplotlibShell as the internal shell. |
|
1027 | """Subclass IPShell with MatplotlibShell as the internal shell. | |
1001 |
|
1028 | |||
1002 | Single-threaded class, meant for the Tk* and FLTK* backends. |
|
1029 | Single-threaded class, meant for the Tk* and FLTK* backends. | |
1003 |
|
1030 | |||
1004 | Having this on a separate class simplifies the external driver code.""" |
|
1031 | Having this on a separate class simplifies the external driver code.""" | |
1005 |
|
1032 | |||
1006 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
1033 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
1007 | IPShell.__init__(self,argv,user_ns,user_global_ns,debug, |
|
1034 | IPShell.__init__(self,argv,user_ns,user_global_ns,debug, | |
1008 | shell_class=MatplotlibShell) |
|
1035 | shell_class=MatplotlibShell) | |
1009 | _load_pylab(self.IP.user_ns) |
|
1036 | _load_pylab(self.IP.user_ns) | |
1010 |
|
1037 | |||
1011 | class IPShellMatplotlibGTK(IPShellGTK): |
|
1038 | class IPShellMatplotlibGTK(IPShellGTK): | |
1012 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. |
|
1039 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. | |
1013 |
|
1040 | |||
1014 | Multi-threaded class, meant for the GTK* backends.""" |
|
1041 | Multi-threaded class, meant for the GTK* backends.""" | |
1015 |
|
1042 | |||
1016 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
1043 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
1017 | IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug, |
|
1044 | IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug, | |
1018 | shell_class=MatplotlibMTShell) |
|
1045 | shell_class=MatplotlibMTShell) | |
1019 | _load_pylab(self.IP.user_ns) |
|
1046 | _load_pylab(self.IP.user_ns) | |
1020 |
|
1047 | |||
1021 | class IPShellMatplotlibWX(IPShellWX): |
|
1048 | class IPShellMatplotlibWX(IPShellWX): | |
1022 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. |
|
1049 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. | |
1023 |
|
1050 | |||
1024 | Multi-threaded class, meant for the WX* backends.""" |
|
1051 | Multi-threaded class, meant for the WX* backends.""" | |
1025 |
|
1052 | |||
1026 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
1053 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
1027 | IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug, |
|
1054 | IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug, | |
1028 | shell_class=MatplotlibMTShell) |
|
1055 | shell_class=MatplotlibMTShell) | |
1029 | _load_pylab(self.IP.user_ns) |
|
1056 | _load_pylab(self.IP.user_ns) | |
1030 |
|
1057 | |||
1031 | class IPShellMatplotlibQt(IPShellQt): |
|
1058 | class IPShellMatplotlibQt(IPShellQt): | |
1032 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. |
|
1059 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. | |
1033 |
|
1060 | |||
1034 | Multi-threaded class, meant for the Qt* backends.""" |
|
1061 | Multi-threaded class, meant for the Qt* backends.""" | |
1035 |
|
1062 | |||
1036 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
1063 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
1037 | IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug, |
|
1064 | IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug, | |
1038 | shell_class=MatplotlibMTShell) |
|
1065 | shell_class=MatplotlibMTShell) | |
1039 | _load_pylab(self.IP.user_ns) |
|
1066 | _load_pylab(self.IP.user_ns) | |
1040 |
|
1067 | |||
1041 | class IPShellMatplotlibQt4(IPShellQt4): |
|
1068 | class IPShellMatplotlibQt4(IPShellQt4): | |
1042 | """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell. |
|
1069 | """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell. | |
1043 |
|
1070 | |||
1044 | Multi-threaded class, meant for the Qt4* backends.""" |
|
1071 | Multi-threaded class, meant for the Qt4* backends.""" | |
1045 |
|
1072 | |||
1046 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
1073 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
1047 | IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug, |
|
1074 | IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug, | |
1048 | shell_class=MatplotlibMTShell) |
|
1075 | shell_class=MatplotlibMTShell) | |
1049 | _load_pylab(self.IP.user_ns) |
|
1076 | _load_pylab(self.IP.user_ns) | |
1050 |
|
1077 | |||
1051 | #----------------------------------------------------------------------------- |
|
1078 | #----------------------------------------------------------------------------- | |
1052 | # Factory functions to actually start the proper thread-aware shell |
|
1079 | # Factory functions to actually start the proper thread-aware shell | |
1053 |
|
1080 | |||
1054 | def _matplotlib_shell_class(): |
|
1081 | def _matplotlib_shell_class(): | |
1055 | """Factory function to handle shell class selection for matplotlib. |
|
1082 | """Factory function to handle shell class selection for matplotlib. | |
1056 |
|
1083 | |||
1057 | The proper shell class to use depends on the matplotlib backend, since |
|
1084 | The proper shell class to use depends on the matplotlib backend, since | |
1058 | each backend requires a different threading strategy.""" |
|
1085 | each backend requires a different threading strategy.""" | |
1059 |
|
1086 | |||
1060 | try: |
|
1087 | try: | |
1061 | import matplotlib |
|
1088 | import matplotlib | |
1062 | except ImportError: |
|
1089 | except ImportError: | |
1063 | error('matplotlib could NOT be imported! Starting normal IPython.') |
|
1090 | error('matplotlib could NOT be imported! Starting normal IPython.') | |
1064 | sh_class = IPShell |
|
1091 | sh_class = IPShell | |
1065 | else: |
|
1092 | else: | |
1066 | backend = matplotlib.rcParams['backend'] |
|
1093 | backend = matplotlib.rcParams['backend'] | |
1067 | if backend.startswith('GTK'): |
|
1094 | if backend.startswith('GTK'): | |
1068 | sh_class = IPShellMatplotlibGTK |
|
1095 | sh_class = IPShellMatplotlibGTK | |
1069 | elif backend.startswith('WX'): |
|
1096 | elif backend.startswith('WX'): | |
1070 | sh_class = IPShellMatplotlibWX |
|
1097 | sh_class = IPShellMatplotlibWX | |
1071 | elif backend.startswith('Qt4'): |
|
1098 | elif backend.startswith('Qt4'): | |
1072 | sh_class = IPShellMatplotlibQt4 |
|
1099 | sh_class = IPShellMatplotlibQt4 | |
1073 | elif backend.startswith('Qt'): |
|
1100 | elif backend.startswith('Qt'): | |
1074 | sh_class = IPShellMatplotlibQt |
|
1101 | sh_class = IPShellMatplotlibQt | |
1075 | else: |
|
1102 | else: | |
1076 | sh_class = IPShellMatplotlib |
|
1103 | sh_class = IPShellMatplotlib | |
1077 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg |
|
1104 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg | |
1078 | return sh_class |
|
1105 | return sh_class | |
1079 |
|
1106 | |||
1080 | # This is the one which should be called by external code. |
|
1107 | # This is the one which should be called by external code. | |
1081 | def start(user_ns = None): |
|
1108 | def start(user_ns = None): | |
1082 | """Return a running shell instance, dealing with threading options. |
|
1109 | """Return a running shell instance, dealing with threading options. | |
1083 |
|
1110 | |||
1084 | This is a factory function which will instantiate the proper IPython shell |
|
1111 | This is a factory function which will instantiate the proper IPython shell | |
1085 | based on the user's threading choice. Such a selector is needed because |
|
1112 | based on the user's threading choice. Such a selector is needed because | |
1086 | different GUI toolkits require different thread handling details.""" |
|
1113 | different GUI toolkits require different thread handling details.""" | |
1087 |
|
1114 | |||
1088 | global USE_TK |
|
1115 | global USE_TK | |
1089 | # Crude sys.argv hack to extract the threading options. |
|
1116 | # Crude sys.argv hack to extract the threading options. | |
1090 | argv = sys.argv |
|
1117 | argv = sys.argv | |
1091 | if len(argv) > 1: |
|
1118 | if len(argv) > 1: | |
1092 | if len(argv) > 2: |
|
1119 | if len(argv) > 2: | |
1093 | arg2 = argv[2] |
|
1120 | arg2 = argv[2] | |
1094 | if arg2.endswith('-tk'): |
|
1121 | if arg2.endswith('-tk'): | |
1095 | USE_TK = True |
|
1122 | USE_TK = True | |
1096 | arg1 = argv[1] |
|
1123 | arg1 = argv[1] | |
1097 | if arg1.endswith('-gthread'): |
|
1124 | if arg1.endswith('-gthread'): | |
1098 | shell = IPShellGTK |
|
1125 | shell = IPShellGTK | |
1099 | elif arg1.endswith( '-qthread' ): |
|
1126 | elif arg1.endswith( '-qthread' ): | |
1100 | shell = IPShellQt |
|
1127 | shell = IPShellQt | |
1101 | elif arg1.endswith( '-q4thread' ): |
|
1128 | elif arg1.endswith( '-q4thread' ): | |
1102 | shell = IPShellQt4 |
|
1129 | shell = IPShellQt4 | |
1103 | elif arg1.endswith('-wthread'): |
|
1130 | elif arg1.endswith('-wthread'): | |
1104 | shell = IPShellWX |
|
1131 | shell = IPShellWX | |
1105 | elif arg1.endswith('-pylab'): |
|
1132 | elif arg1.endswith('-pylab'): | |
1106 | shell = _matplotlib_shell_class() |
|
1133 | shell = _matplotlib_shell_class() | |
1107 | else: |
|
1134 | else: | |
1108 | shell = IPShell |
|
1135 | shell = IPShell | |
1109 | else: |
|
1136 | else: | |
1110 | shell = IPShell |
|
1137 | shell = IPShell | |
1111 | return shell(user_ns = user_ns) |
|
1138 | return shell(user_ns = user_ns) | |
1112 |
|
1139 | |||
1113 | # Some aliases for backwards compatibility |
|
1140 | # Some aliases for backwards compatibility | |
1114 | IPythonShell = IPShell |
|
1141 | IPythonShell = IPShell | |
1115 | IPythonShellEmbed = IPShellEmbed |
|
1142 | IPythonShellEmbed = IPShellEmbed | |
1116 | #************************ End of file <Shell.py> *************************** |
|
1143 | #************************ End of file <Shell.py> *************************** |
@@ -1,1850 +1,1850 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 25 |
|
8 | $Id: genutils.py 2577 2007-08-02 23:50:02Z fperez $""" | |
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 | import os |
|
25 | import os | |
26 | import re |
|
26 | import re | |
27 | import shlex |
|
27 | import shlex | |
28 | import shutil |
|
28 | import shutil | |
29 | import sys |
|
29 | import sys | |
30 | import tempfile |
|
30 | import tempfile | |
31 | import time |
|
31 | import time | |
32 | import types |
|
32 | import types | |
33 | import warnings |
|
33 | import warnings | |
34 |
|
34 | |||
35 | # Other IPython utilities |
|
35 | # Other IPython utilities | |
36 | import IPython |
|
36 | import IPython | |
37 | from IPython.Itpl import Itpl,itpl,printpl |
|
37 | from IPython.Itpl import Itpl,itpl,printpl | |
38 | from IPython import DPyGetOpt, platutils |
|
38 | from IPython import DPyGetOpt, platutils | |
39 | from IPython.generics import result_display |
|
39 | from IPython.generics import result_display | |
40 | from path import path |
|
40 | from path import path | |
41 | if os.name == "nt": |
|
41 | if os.name == "nt": | |
42 | from IPython.winconsole import get_console_size |
|
42 | from IPython.winconsole import get_console_size | |
43 |
|
43 | |||
44 | #**************************************************************************** |
|
44 | #**************************************************************************** | |
45 | # Exceptions |
|
45 | # Exceptions | |
46 | class Error(Exception): |
|
46 | class Error(Exception): | |
47 | """Base class for exceptions in this module.""" |
|
47 | """Base class for exceptions in this module.""" | |
48 | pass |
|
48 | pass | |
49 |
|
49 | |||
50 | #---------------------------------------------------------------------------- |
|
50 | #---------------------------------------------------------------------------- | |
51 | class IOStream: |
|
51 | class IOStream: | |
52 | def __init__(self,stream,fallback): |
|
52 | def __init__(self,stream,fallback): | |
53 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
53 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): | |
54 | stream = fallback |
|
54 | stream = fallback | |
55 | self.stream = stream |
|
55 | self.stream = stream | |
56 | self._swrite = stream.write |
|
56 | self._swrite = stream.write | |
57 | self.flush = stream.flush |
|
57 | self.flush = stream.flush | |
58 |
|
58 | |||
59 | def write(self,data): |
|
59 | def write(self,data): | |
60 | try: |
|
60 | try: | |
61 | self._swrite(data) |
|
61 | self._swrite(data) | |
62 | except: |
|
62 | except: | |
63 | try: |
|
63 | try: | |
64 | # print handles some unicode issues which may trip a plain |
|
64 | # print handles some unicode issues which may trip a plain | |
65 | # write() call. Attempt to emulate write() by using a |
|
65 | # write() call. Attempt to emulate write() by using a | |
66 | # trailing comma |
|
66 | # trailing comma | |
67 | print >> self.stream, data, |
|
67 | print >> self.stream, data, | |
68 | except: |
|
68 | except: | |
69 | # if we get here, something is seriously broken. |
|
69 | # if we get here, something is seriously broken. | |
70 | print >> sys.stderr, \ |
|
70 | print >> sys.stderr, \ | |
71 | 'ERROR - failed to write data to stream:', self.stream |
|
71 | 'ERROR - failed to write data to stream:', self.stream | |
72 |
|
72 | |||
73 | def close(self): |
|
73 | def close(self): | |
74 | pass |
|
74 | pass | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | class IOTerm: |
|
77 | class IOTerm: | |
78 | """ Term holds the file or file-like objects for handling I/O operations. |
|
78 | """ Term holds the file or file-like objects for handling I/O operations. | |
79 |
|
79 | |||
80 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
80 | These are normally just sys.stdin, sys.stdout and sys.stderr but for | |
81 | Windows they can can replaced to allow editing the strings before they are |
|
81 | Windows they can can replaced to allow editing the strings before they are | |
82 | displayed.""" |
|
82 | displayed.""" | |
83 |
|
83 | |||
84 | # In the future, having IPython channel all its I/O operations through |
|
84 | # In the future, having IPython channel all its I/O operations through | |
85 | # this class will make it easier to embed it into other environments which |
|
85 | # this class will make it easier to embed it into other environments which | |
86 | # are not a normal terminal (such as a GUI-based shell) |
|
86 | # are not a normal terminal (such as a GUI-based shell) | |
87 | def __init__(self,cin=None,cout=None,cerr=None): |
|
87 | def __init__(self,cin=None,cout=None,cerr=None): | |
88 | self.cin = IOStream(cin,sys.stdin) |
|
88 | self.cin = IOStream(cin,sys.stdin) | |
89 | self.cout = IOStream(cout,sys.stdout) |
|
89 | self.cout = IOStream(cout,sys.stdout) | |
90 | self.cerr = IOStream(cerr,sys.stderr) |
|
90 | self.cerr = IOStream(cerr,sys.stderr) | |
91 |
|
91 | |||
92 | # Global variable to be used for all I/O |
|
92 | # Global variable to be used for all I/O | |
93 | Term = IOTerm() |
|
93 | Term = IOTerm() | |
94 |
|
94 | |||
95 | import IPython.rlineimpl as readline |
|
95 | import IPython.rlineimpl as readline | |
96 | # Remake Term to use the readline i/o facilities |
|
96 | # Remake Term to use the readline i/o facilities | |
97 | if sys.platform == 'win32' and readline.have_readline: |
|
97 | if sys.platform == 'win32' and readline.have_readline: | |
98 |
|
98 | |||
99 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
99 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | #**************************************************************************** |
|
102 | #**************************************************************************** | |
103 | # Generic warning/error printer, used by everything else |
|
103 | # Generic warning/error printer, used by everything else | |
104 | def warn(msg,level=2,exit_val=1): |
|
104 | def warn(msg,level=2,exit_val=1): | |
105 | """Standard warning printer. Gives formatting consistency. |
|
105 | """Standard warning printer. Gives formatting consistency. | |
106 |
|
106 | |||
107 | Output is sent to Term.cerr (sys.stderr by default). |
|
107 | Output is sent to Term.cerr (sys.stderr by default). | |
108 |
|
108 | |||
109 | Options: |
|
109 | Options: | |
110 |
|
110 | |||
111 | -level(2): allows finer control: |
|
111 | -level(2): allows finer control: | |
112 | 0 -> Do nothing, dummy function. |
|
112 | 0 -> Do nothing, dummy function. | |
113 | 1 -> Print message. |
|
113 | 1 -> Print message. | |
114 | 2 -> Print 'WARNING:' + message. (Default level). |
|
114 | 2 -> Print 'WARNING:' + message. (Default level). | |
115 | 3 -> Print 'ERROR:' + message. |
|
115 | 3 -> Print 'ERROR:' + message. | |
116 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
116 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). | |
117 |
|
117 | |||
118 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
118 | -exit_val (1): exit value returned by sys.exit() for a level 4 | |
119 | warning. Ignored for all other levels.""" |
|
119 | warning. Ignored for all other levels.""" | |
120 |
|
120 | |||
121 | if level>0: |
|
121 | if level>0: | |
122 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
122 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] | |
123 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
123 | print >> Term.cerr, '%s%s' % (header[level],msg) | |
124 | if level == 4: |
|
124 | if level == 4: | |
125 | print >> Term.cerr,'Exiting.\n' |
|
125 | print >> Term.cerr,'Exiting.\n' | |
126 | sys.exit(exit_val) |
|
126 | sys.exit(exit_val) | |
127 |
|
127 | |||
128 | def info(msg): |
|
128 | def info(msg): | |
129 | """Equivalent to warn(msg,level=1).""" |
|
129 | """Equivalent to warn(msg,level=1).""" | |
130 |
|
130 | |||
131 | warn(msg,level=1) |
|
131 | warn(msg,level=1) | |
132 |
|
132 | |||
133 | def error(msg): |
|
133 | def error(msg): | |
134 | """Equivalent to warn(msg,level=3).""" |
|
134 | """Equivalent to warn(msg,level=3).""" | |
135 |
|
135 | |||
136 | warn(msg,level=3) |
|
136 | warn(msg,level=3) | |
137 |
|
137 | |||
138 | def fatal(msg,exit_val=1): |
|
138 | def fatal(msg,exit_val=1): | |
139 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
139 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" | |
140 |
|
140 | |||
141 | warn(msg,exit_val=exit_val,level=4) |
|
141 | warn(msg,exit_val=exit_val,level=4) | |
142 |
|
142 | |||
143 | #--------------------------------------------------------------------------- |
|
143 | #--------------------------------------------------------------------------- | |
144 | # Debugging routines |
|
144 | # Debugging routines | |
145 | # |
|
145 | # | |
146 | def debugx(expr,pre_msg=''): |
|
146 | def debugx(expr,pre_msg=''): | |
147 | """Print the value of an expression from the caller's frame. |
|
147 | """Print the value of an expression from the caller's frame. | |
148 |
|
148 | |||
149 | Takes an expression, evaluates it in the caller's frame and prints both |
|
149 | Takes an expression, evaluates it in the caller's frame and prints both | |
150 | the given expression and the resulting value (as well as a debug mark |
|
150 | the given expression and the resulting value (as well as a debug mark | |
151 | indicating the name of the calling function. The input must be of a form |
|
151 | indicating the name of the calling function. The input must be of a form | |
152 | suitable for eval(). |
|
152 | suitable for eval(). | |
153 |
|
153 | |||
154 | An optional message can be passed, which will be prepended to the printed |
|
154 | An optional message can be passed, which will be prepended to the printed | |
155 | expr->value pair.""" |
|
155 | expr->value pair.""" | |
156 |
|
156 | |||
157 | cf = sys._getframe(1) |
|
157 | cf = sys._getframe(1) | |
158 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
158 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, | |
159 | eval(expr,cf.f_globals,cf.f_locals)) |
|
159 | eval(expr,cf.f_globals,cf.f_locals)) | |
160 |
|
160 | |||
161 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
161 | # deactivate it by uncommenting the following line, which makes it a no-op | |
162 | #def debugx(expr,pre_msg=''): pass |
|
162 | #def debugx(expr,pre_msg=''): pass | |
163 |
|
163 | |||
164 | #---------------------------------------------------------------------------- |
|
164 | #---------------------------------------------------------------------------- | |
165 | StringTypes = types.StringTypes |
|
165 | StringTypes = types.StringTypes | |
166 |
|
166 | |||
167 | # Basic timing functionality |
|
167 | # Basic timing functionality | |
168 |
|
168 | |||
169 | # If possible (Unix), use the resource module instead of time.clock() |
|
169 | # If possible (Unix), use the resource module instead of time.clock() | |
170 | try: |
|
170 | try: | |
171 | import resource |
|
171 | import resource | |
172 | def clocku(): |
|
172 | def clocku(): | |
173 | """clocku() -> floating point number |
|
173 | """clocku() -> floating point number | |
174 |
|
174 | |||
175 | Return the *USER* CPU time in seconds since the start of the process. |
|
175 | Return the *USER* CPU time in seconds since the start of the process. | |
176 | This is done via a call to resource.getrusage, so it avoids the |
|
176 | This is done via a call to resource.getrusage, so it avoids the | |
177 | wraparound problems in time.clock().""" |
|
177 | wraparound problems in time.clock().""" | |
178 |
|
178 | |||
179 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
179 | return resource.getrusage(resource.RUSAGE_SELF)[0] | |
180 |
|
180 | |||
181 | def clocks(): |
|
181 | def clocks(): | |
182 | """clocks() -> floating point number |
|
182 | """clocks() -> floating point number | |
183 |
|
183 | |||
184 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
184 | Return the *SYSTEM* CPU time in seconds since the start of the process. | |
185 | This is done via a call to resource.getrusage, so it avoids the |
|
185 | This is done via a call to resource.getrusage, so it avoids the | |
186 | wraparound problems in time.clock().""" |
|
186 | wraparound problems in time.clock().""" | |
187 |
|
187 | |||
188 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
188 | return resource.getrusage(resource.RUSAGE_SELF)[1] | |
189 |
|
189 | |||
190 | def clock(): |
|
190 | def clock(): | |
191 | """clock() -> floating point number |
|
191 | """clock() -> floating point number | |
192 |
|
192 | |||
193 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
193 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of | |
194 | the process. This is done via a call to resource.getrusage, so it |
|
194 | the process. This is done via a call to resource.getrusage, so it | |
195 | avoids the wraparound problems in time.clock().""" |
|
195 | avoids the wraparound problems in time.clock().""" | |
196 |
|
196 | |||
197 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
197 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] | |
198 | return u+s |
|
198 | return u+s | |
199 |
|
199 | |||
200 | def clock2(): |
|
200 | def clock2(): | |
201 | """clock2() -> (t_user,t_system) |
|
201 | """clock2() -> (t_user,t_system) | |
202 |
|
202 | |||
203 | Similar to clock(), but return a tuple of user/system times.""" |
|
203 | Similar to clock(), but return a tuple of user/system times.""" | |
204 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
204 | return resource.getrusage(resource.RUSAGE_SELF)[:2] | |
205 |
|
205 | |||
206 | except ImportError: |
|
206 | except ImportError: | |
207 | # There is no distinction of user/system time under windows, so we just use |
|
207 | # There is no distinction of user/system time under windows, so we just use | |
208 | # time.clock() for everything... |
|
208 | # time.clock() for everything... | |
209 | clocku = clocks = clock = time.clock |
|
209 | clocku = clocks = clock = time.clock | |
210 | def clock2(): |
|
210 | def clock2(): | |
211 | """Under windows, system CPU time can't be measured. |
|
211 | """Under windows, system CPU time can't be measured. | |
212 |
|
212 | |||
213 | This just returns clock() and zero.""" |
|
213 | This just returns clock() and zero.""" | |
214 | return time.clock(),0.0 |
|
214 | return time.clock(),0.0 | |
215 |
|
215 | |||
216 | def timings_out(reps,func,*args,**kw): |
|
216 | def timings_out(reps,func,*args,**kw): | |
217 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
217 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) | |
218 |
|
218 | |||
219 | Execute a function reps times, return a tuple with the elapsed total |
|
219 | Execute a function reps times, return a tuple with the elapsed total | |
220 | CPU time in seconds, the time per call and the function's output. |
|
220 | CPU time in seconds, the time per call and the function's output. | |
221 |
|
221 | |||
222 | Under Unix, the return value is the sum of user+system time consumed by |
|
222 | Under Unix, the return value is the sum of user+system time consumed by | |
223 | the process, computed via the resource module. This prevents problems |
|
223 | the process, computed via the resource module. This prevents problems | |
224 | related to the wraparound effect which the time.clock() function has. |
|
224 | related to the wraparound effect which the time.clock() function has. | |
225 |
|
225 | |||
226 | Under Windows the return value is in wall clock seconds. See the |
|
226 | Under Windows the return value is in wall clock seconds. See the | |
227 | documentation for the time module for more details.""" |
|
227 | documentation for the time module for more details.""" | |
228 |
|
228 | |||
229 | reps = int(reps) |
|
229 | reps = int(reps) | |
230 | assert reps >=1, 'reps must be >= 1' |
|
230 | assert reps >=1, 'reps must be >= 1' | |
231 | if reps==1: |
|
231 | if reps==1: | |
232 | start = clock() |
|
232 | start = clock() | |
233 | out = func(*args,**kw) |
|
233 | out = func(*args,**kw) | |
234 | tot_time = clock()-start |
|
234 | tot_time = clock()-start | |
235 | else: |
|
235 | else: | |
236 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
236 | rng = xrange(reps-1) # the last time is executed separately to store output | |
237 | start = clock() |
|
237 | start = clock() | |
238 | for dummy in rng: func(*args,**kw) |
|
238 | for dummy in rng: func(*args,**kw) | |
239 | out = func(*args,**kw) # one last time |
|
239 | out = func(*args,**kw) # one last time | |
240 | tot_time = clock()-start |
|
240 | tot_time = clock()-start | |
241 | av_time = tot_time / reps |
|
241 | av_time = tot_time / reps | |
242 | return tot_time,av_time,out |
|
242 | return tot_time,av_time,out | |
243 |
|
243 | |||
244 | def timings(reps,func,*args,**kw): |
|
244 | def timings(reps,func,*args,**kw): | |
245 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
245 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) | |
246 |
|
246 | |||
247 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
247 | Execute a function reps times, return a tuple with the elapsed total CPU | |
248 | time in seconds and the time per call. These are just the first two values |
|
248 | time in seconds and the time per call. These are just the first two values | |
249 | in timings_out().""" |
|
249 | in timings_out().""" | |
250 |
|
250 | |||
251 | return timings_out(reps,func,*args,**kw)[0:2] |
|
251 | return timings_out(reps,func,*args,**kw)[0:2] | |
252 |
|
252 | |||
253 | def timing(func,*args,**kw): |
|
253 | def timing(func,*args,**kw): | |
254 | """timing(func,*args,**kw) -> t_total |
|
254 | """timing(func,*args,**kw) -> t_total | |
255 |
|
255 | |||
256 | Execute a function once, return the elapsed total CPU time in |
|
256 | Execute a function once, return the elapsed total CPU time in | |
257 | seconds. This is just the first value in timings_out().""" |
|
257 | seconds. This is just the first value in timings_out().""" | |
258 |
|
258 | |||
259 | return timings_out(1,func,*args,**kw)[0] |
|
259 | return timings_out(1,func,*args,**kw)[0] | |
260 |
|
260 | |||
261 | #**************************************************************************** |
|
261 | #**************************************************************************** | |
262 | # file and system |
|
262 | # file and system | |
263 |
|
263 | |||
264 | def arg_split(s,posix=False): |
|
264 | def arg_split(s,posix=False): | |
265 | """Split a command line's arguments in a shell-like manner. |
|
265 | """Split a command line's arguments in a shell-like manner. | |
266 |
|
266 | |||
267 | This is a modified version of the standard library's shlex.split() |
|
267 | This is a modified version of the standard library's shlex.split() | |
268 | function, but with a default of posix=False for splitting, so that quotes |
|
268 | function, but with a default of posix=False for splitting, so that quotes | |
269 | in inputs are respected.""" |
|
269 | in inputs are respected.""" | |
270 |
|
270 | |||
271 | # XXX - there may be unicode-related problems here!!! I'm not sure that |
|
271 | # XXX - there may be unicode-related problems here!!! I'm not sure that | |
272 | # shlex is truly unicode-safe, so it might be necessary to do |
|
272 | # shlex is truly unicode-safe, so it might be necessary to do | |
273 | # |
|
273 | # | |
274 | # s = s.encode(sys.stdin.encoding) |
|
274 | # s = s.encode(sys.stdin.encoding) | |
275 | # |
|
275 | # | |
276 | # first, to ensure that shlex gets a normal string. Input from anyone who |
|
276 | # first, to ensure that shlex gets a normal string. Input from anyone who | |
277 | # knows more about unicode and shlex than I would be good to have here... |
|
277 | # knows more about unicode and shlex than I would be good to have here... | |
278 | lex = shlex.shlex(s, posix=posix) |
|
278 | lex = shlex.shlex(s, posix=posix) | |
279 | lex.whitespace_split = True |
|
279 | lex.whitespace_split = True | |
280 | return list(lex) |
|
280 | return list(lex) | |
281 |
|
281 | |||
282 | def system(cmd,verbose=0,debug=0,header=''): |
|
282 | def system(cmd,verbose=0,debug=0,header=''): | |
283 | """Execute a system command, return its exit status. |
|
283 | """Execute a system command, return its exit status. | |
284 |
|
284 | |||
285 | Options: |
|
285 | Options: | |
286 |
|
286 | |||
287 | - verbose (0): print the command to be executed. |
|
287 | - verbose (0): print the command to be executed. | |
288 |
|
288 | |||
289 | - debug (0): only print, do not actually execute. |
|
289 | - debug (0): only print, do not actually execute. | |
290 |
|
290 | |||
291 | - header (''): Header to print on screen prior to the executed command (it |
|
291 | - header (''): Header to print on screen prior to the executed command (it | |
292 | is only prepended to the command, no newlines are added). |
|
292 | is only prepended to the command, no newlines are added). | |
293 |
|
293 | |||
294 | Note: a stateful version of this function is available through the |
|
294 | Note: a stateful version of this function is available through the | |
295 | SystemExec class.""" |
|
295 | SystemExec class.""" | |
296 |
|
296 | |||
297 | stat = 0 |
|
297 | stat = 0 | |
298 | if verbose or debug: print header+cmd |
|
298 | if verbose or debug: print header+cmd | |
299 | sys.stdout.flush() |
|
299 | sys.stdout.flush() | |
300 | if not debug: stat = os.system(cmd) |
|
300 | if not debug: stat = os.system(cmd) | |
301 | return stat |
|
301 | return stat | |
302 |
|
302 | |||
303 | # This function is used by ipython in a lot of places to make system calls. |
|
303 | # This function is used by ipython in a lot of places to make system calls. | |
304 | # We need it to be slightly different under win32, due to the vagaries of |
|
304 | # We need it to be slightly different under win32, due to the vagaries of | |
305 | # 'network shares'. A win32 override is below. |
|
305 | # 'network shares'. A win32 override is below. | |
306 |
|
306 | |||
307 | def shell(cmd,verbose=0,debug=0,header=''): |
|
307 | def shell(cmd,verbose=0,debug=0,header=''): | |
308 | """Execute a command in the system shell, always return None. |
|
308 | """Execute a command in the system shell, always return None. | |
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: this is similar to genutils.system(), but it returns None so it can |
|
319 | Note: this is similar to genutils.system(), but it returns None so it can | |
320 | be conveniently used in interactive loops without getting the return value |
|
320 | be conveniently used in interactive loops without getting the return value | |
321 | (typically 0) printed many times.""" |
|
321 | (typically 0) printed many times.""" | |
322 |
|
322 | |||
323 | stat = 0 |
|
323 | stat = 0 | |
324 | if verbose or debug: print header+cmd |
|
324 | if verbose or debug: print header+cmd | |
325 | # flush stdout so we don't mangle python's buffering |
|
325 | # flush stdout so we don't mangle python's buffering | |
326 | sys.stdout.flush() |
|
326 | sys.stdout.flush() | |
327 |
|
327 | |||
328 | if not debug: |
|
328 | if not debug: | |
329 | platutils.set_term_title("IPy:" + cmd) |
|
329 | platutils.set_term_title("IPy:" + cmd) | |
330 | os.system(cmd) |
|
330 | os.system(cmd) | |
331 | platutils.set_term_title("IPy:" + os.path.basename(os.getcwd())) |
|
331 | platutils.set_term_title("IPy:" + os.path.basename(os.getcwd())) | |
332 |
|
332 | |||
333 | # override shell() for win32 to deal with network shares |
|
333 | # override shell() for win32 to deal with network shares | |
334 | if os.name in ('nt','dos'): |
|
334 | if os.name in ('nt','dos'): | |
335 |
|
335 | |||
336 | shell_ori = shell |
|
336 | shell_ori = shell | |
337 |
|
337 | |||
338 | def shell(cmd,verbose=0,debug=0,header=''): |
|
338 | def shell(cmd,verbose=0,debug=0,header=''): | |
339 | if os.getcwd().startswith(r"\\"): |
|
339 | if os.getcwd().startswith(r"\\"): | |
340 | path = os.getcwd() |
|
340 | path = os.getcwd() | |
341 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
341 | # change to c drive (cannot be on UNC-share when issuing os.system, | |
342 | # as cmd.exe cannot handle UNC addresses) |
|
342 | # as cmd.exe cannot handle UNC addresses) | |
343 | os.chdir("c:") |
|
343 | os.chdir("c:") | |
344 | # issue pushd to the UNC-share and then run the command |
|
344 | # issue pushd to the UNC-share and then run the command | |
345 | try: |
|
345 | try: | |
346 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
346 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) | |
347 | finally: |
|
347 | finally: | |
348 | os.chdir(path) |
|
348 | os.chdir(path) | |
349 | else: |
|
349 | else: | |
350 | shell_ori(cmd,verbose,debug,header) |
|
350 | shell_ori(cmd,verbose,debug,header) | |
351 |
|
351 | |||
352 | shell.__doc__ = shell_ori.__doc__ |
|
352 | shell.__doc__ = shell_ori.__doc__ | |
353 |
|
353 | |||
354 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
354 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): | |
355 | """Dummy substitute for perl's backquotes. |
|
355 | """Dummy substitute for perl's backquotes. | |
356 |
|
356 | |||
357 | Executes a command and returns the output. |
|
357 | Executes a command and returns the output. | |
358 |
|
358 | |||
359 | Accepts the same arguments as system(), plus: |
|
359 | Accepts the same arguments as system(), plus: | |
360 |
|
360 | |||
361 | - split(0): if true, the output is returned as a list split on newlines. |
|
361 | - split(0): if true, the output is returned as a list split on newlines. | |
362 |
|
362 | |||
363 | Note: a stateful version of this function is available through the |
|
363 | Note: a stateful version of this function is available through the | |
364 | SystemExec class. |
|
364 | SystemExec class. | |
365 |
|
365 | |||
366 | This is pretty much deprecated and rarely used, |
|
366 | This is pretty much deprecated and rarely used, | |
367 | genutils.getoutputerror may be what you need. |
|
367 | genutils.getoutputerror may be what you need. | |
368 |
|
368 | |||
369 | """ |
|
369 | """ | |
370 |
|
370 | |||
371 | if verbose or debug: print header+cmd |
|
371 | if verbose or debug: print header+cmd | |
372 | if not debug: |
|
372 | if not debug: | |
373 | output = os.popen(cmd).read() |
|
373 | output = os.popen(cmd).read() | |
374 | # stipping last \n is here for backwards compat. |
|
374 | # stipping last \n is here for backwards compat. | |
375 | if output.endswith('\n'): |
|
375 | if output.endswith('\n'): | |
376 | output = output[:-1] |
|
376 | output = output[:-1] | |
377 | if split: |
|
377 | if split: | |
378 | return output.split('\n') |
|
378 | return output.split('\n') | |
379 | else: |
|
379 | else: | |
380 | return output |
|
380 | return output | |
381 |
|
381 | |||
382 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
382 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): | |
383 | """Return (standard output,standard error) of executing cmd in a shell. |
|
383 | """Return (standard output,standard error) of executing cmd in a shell. | |
384 |
|
384 | |||
385 | Accepts the same arguments as system(), plus: |
|
385 | Accepts the same arguments as system(), plus: | |
386 |
|
386 | |||
387 | - split(0): if true, each of stdout/err is returned as a list split on |
|
387 | - split(0): if true, each of stdout/err is returned as a list split on | |
388 | newlines. |
|
388 | newlines. | |
389 |
|
389 | |||
390 | Note: a stateful version of this function is available through the |
|
390 | Note: a stateful version of this function is available through the | |
391 | SystemExec class.""" |
|
391 | SystemExec class.""" | |
392 |
|
392 | |||
393 | if verbose or debug: print header+cmd |
|
393 | if verbose or debug: print header+cmd | |
394 | if not cmd: |
|
394 | if not cmd: | |
395 | if split: |
|
395 | if split: | |
396 | return [],[] |
|
396 | return [],[] | |
397 | else: |
|
397 | else: | |
398 | return '','' |
|
398 | return '','' | |
399 | if not debug: |
|
399 | if not debug: | |
400 | pin,pout,perr = os.popen3(cmd) |
|
400 | pin,pout,perr = os.popen3(cmd) | |
401 | tout = pout.read().rstrip() |
|
401 | tout = pout.read().rstrip() | |
402 | terr = perr.read().rstrip() |
|
402 | terr = perr.read().rstrip() | |
403 | pin.close() |
|
403 | pin.close() | |
404 | pout.close() |
|
404 | pout.close() | |
405 | perr.close() |
|
405 | perr.close() | |
406 | if split: |
|
406 | if split: | |
407 | return tout.split('\n'),terr.split('\n') |
|
407 | return tout.split('\n'),terr.split('\n') | |
408 | else: |
|
408 | else: | |
409 | return tout,terr |
|
409 | return tout,terr | |
410 |
|
410 | |||
411 | # for compatibility with older naming conventions |
|
411 | # for compatibility with older naming conventions | |
412 | xsys = system |
|
412 | xsys = system | |
413 | bq = getoutput |
|
413 | bq = getoutput | |
414 |
|
414 | |||
415 | class SystemExec: |
|
415 | class SystemExec: | |
416 | """Access the system and getoutput functions through a stateful interface. |
|
416 | """Access the system and getoutput functions through a stateful interface. | |
417 |
|
417 | |||
418 | Note: here we refer to the system and getoutput functions from this |
|
418 | Note: here we refer to the system and getoutput functions from this | |
419 | library, not the ones from the standard python library. |
|
419 | library, not the ones from the standard python library. | |
420 |
|
420 | |||
421 | This class offers the system and getoutput functions as methods, but the |
|
421 | This class offers the system and getoutput functions as methods, but the | |
422 | verbose, debug and header parameters can be set for the instance (at |
|
422 | verbose, debug and header parameters can be set for the instance (at | |
423 | creation time or later) so that they don't need to be specified on each |
|
423 | creation time or later) so that they don't need to be specified on each | |
424 | call. |
|
424 | call. | |
425 |
|
425 | |||
426 | For efficiency reasons, there's no way to override the parameters on a |
|
426 | For efficiency reasons, there's no way to override the parameters on a | |
427 | per-call basis other than by setting instance attributes. If you need |
|
427 | per-call basis other than by setting instance attributes. If you need | |
428 | local overrides, it's best to directly call system() or getoutput(). |
|
428 | local overrides, it's best to directly call system() or getoutput(). | |
429 |
|
429 | |||
430 | The following names are provided as alternate options: |
|
430 | The following names are provided as alternate options: | |
431 | - xsys: alias to system |
|
431 | - xsys: alias to system | |
432 | - bq: alias to getoutput |
|
432 | - bq: alias to getoutput | |
433 |
|
433 | |||
434 | An instance can then be created as: |
|
434 | An instance can then be created as: | |
435 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
435 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') | |
436 |
|
436 | |||
437 | And used as: |
|
437 | And used as: | |
438 | >>> sysexec.xsys('pwd') |
|
438 | >>> sysexec.xsys('pwd') | |
439 | >>> dirlist = sysexec.bq('ls -l') |
|
439 | >>> dirlist = sysexec.bq('ls -l') | |
440 | """ |
|
440 | """ | |
441 |
|
441 | |||
442 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
442 | def __init__(self,verbose=0,debug=0,header='',split=0): | |
443 | """Specify the instance's values for verbose, debug and header.""" |
|
443 | """Specify the instance's values for verbose, debug and header.""" | |
444 | setattr_list(self,'verbose debug header split') |
|
444 | setattr_list(self,'verbose debug header split') | |
445 |
|
445 | |||
446 | def system(self,cmd): |
|
446 | def system(self,cmd): | |
447 | """Stateful interface to system(), with the same keyword parameters.""" |
|
447 | """Stateful interface to system(), with the same keyword parameters.""" | |
448 |
|
448 | |||
449 | system(cmd,self.verbose,self.debug,self.header) |
|
449 | system(cmd,self.verbose,self.debug,self.header) | |
450 |
|
450 | |||
451 | def shell(self,cmd): |
|
451 | def shell(self,cmd): | |
452 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
452 | """Stateful interface to shell(), with the same keyword parameters.""" | |
453 |
|
453 | |||
454 | shell(cmd,self.verbose,self.debug,self.header) |
|
454 | shell(cmd,self.verbose,self.debug,self.header) | |
455 |
|
455 | |||
456 | xsys = system # alias |
|
456 | xsys = system # alias | |
457 |
|
457 | |||
458 | def getoutput(self,cmd): |
|
458 | def getoutput(self,cmd): | |
459 | """Stateful interface to getoutput().""" |
|
459 | """Stateful interface to getoutput().""" | |
460 |
|
460 | |||
461 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
461 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) | |
462 |
|
462 | |||
463 | def getoutputerror(self,cmd): |
|
463 | def getoutputerror(self,cmd): | |
464 | """Stateful interface to getoutputerror().""" |
|
464 | """Stateful interface to getoutputerror().""" | |
465 |
|
465 | |||
466 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
466 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) | |
467 |
|
467 | |||
468 | bq = getoutput # alias |
|
468 | bq = getoutput # alias | |
469 |
|
469 | |||
470 | #----------------------------------------------------------------------------- |
|
470 | #----------------------------------------------------------------------------- | |
471 | def mutex_opts(dict,ex_op): |
|
471 | def mutex_opts(dict,ex_op): | |
472 | """Check for presence of mutually exclusive keys in a dict. |
|
472 | """Check for presence of mutually exclusive keys in a dict. | |
473 |
|
473 | |||
474 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
474 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" | |
475 | for op1,op2 in ex_op: |
|
475 | for op1,op2 in ex_op: | |
476 | if op1 in dict and op2 in dict: |
|
476 | if op1 in dict and op2 in dict: | |
477 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
477 | raise ValueError,'\n*** ERROR in Arguments *** '\ | |
478 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
478 | 'Options '+op1+' and '+op2+' are mutually exclusive.' | |
479 |
|
479 | |||
480 | #----------------------------------------------------------------------------- |
|
480 | #----------------------------------------------------------------------------- | |
481 | def get_py_filename(name): |
|
481 | def get_py_filename(name): | |
482 | """Return a valid python filename in the current directory. |
|
482 | """Return a valid python filename in the current directory. | |
483 |
|
483 | |||
484 | If the given name is not a file, it adds '.py' and searches again. |
|
484 | If the given name is not a file, it adds '.py' and searches again. | |
485 | Raises IOError with an informative message if the file isn't found.""" |
|
485 | Raises IOError with an informative message if the file isn't found.""" | |
486 |
|
486 | |||
487 | name = os.path.expanduser(name) |
|
487 | name = os.path.expanduser(name) | |
488 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
488 | if not os.path.isfile(name) and not name.endswith('.py'): | |
489 | name += '.py' |
|
489 | name += '.py' | |
490 | if os.path.isfile(name): |
|
490 | if os.path.isfile(name): | |
491 | return name |
|
491 | return name | |
492 | else: |
|
492 | else: | |
493 | raise IOError,'File `%s` not found.' % name |
|
493 | raise IOError,'File `%s` not found.' % name | |
494 |
|
494 | |||
495 | #----------------------------------------------------------------------------- |
|
495 | #----------------------------------------------------------------------------- | |
496 | def filefind(fname,alt_dirs = None): |
|
496 | def filefind(fname,alt_dirs = None): | |
497 | """Return the given filename either in the current directory, if it |
|
497 | """Return the given filename either in the current directory, if it | |
498 | exists, or in a specified list of directories. |
|
498 | exists, or in a specified list of directories. | |
499 |
|
499 | |||
500 | ~ expansion is done on all file and directory names. |
|
500 | ~ expansion is done on all file and directory names. | |
501 |
|
501 | |||
502 | Upon an unsuccessful search, raise an IOError exception.""" |
|
502 | Upon an unsuccessful search, raise an IOError exception.""" | |
503 |
|
503 | |||
504 | if alt_dirs is None: |
|
504 | if alt_dirs is None: | |
505 | try: |
|
505 | try: | |
506 | alt_dirs = get_home_dir() |
|
506 | alt_dirs = get_home_dir() | |
507 | except HomeDirError: |
|
507 | except HomeDirError: | |
508 | alt_dirs = os.getcwd() |
|
508 | alt_dirs = os.getcwd() | |
509 | search = [fname] + list_strings(alt_dirs) |
|
509 | search = [fname] + list_strings(alt_dirs) | |
510 | search = map(os.path.expanduser,search) |
|
510 | search = map(os.path.expanduser,search) | |
511 | #print 'search list for',fname,'list:',search # dbg |
|
511 | #print 'search list for',fname,'list:',search # dbg | |
512 | fname = search[0] |
|
512 | fname = search[0] | |
513 | if os.path.isfile(fname): |
|
513 | if os.path.isfile(fname): | |
514 | return fname |
|
514 | return fname | |
515 | for direc in search[1:]: |
|
515 | for direc in search[1:]: | |
516 | testname = os.path.join(direc,fname) |
|
516 | testname = os.path.join(direc,fname) | |
517 | #print 'testname',testname # dbg |
|
517 | #print 'testname',testname # dbg | |
518 | if os.path.isfile(testname): |
|
518 | if os.path.isfile(testname): | |
519 | return testname |
|
519 | return testname | |
520 | raise IOError,'File' + `fname` + \ |
|
520 | raise IOError,'File' + `fname` + \ | |
521 | ' not found in current or supplied directories:' + `alt_dirs` |
|
521 | ' not found in current or supplied directories:' + `alt_dirs` | |
522 |
|
522 | |||
523 | #---------------------------------------------------------------------------- |
|
523 | #---------------------------------------------------------------------------- | |
524 | def file_read(filename): |
|
524 | def file_read(filename): | |
525 | """Read a file and close it. Returns the file source.""" |
|
525 | """Read a file and close it. Returns the file source.""" | |
526 | fobj = open(filename,'r'); |
|
526 | fobj = open(filename,'r'); | |
527 | source = fobj.read(); |
|
527 | source = fobj.read(); | |
528 | fobj.close() |
|
528 | fobj.close() | |
529 | return source |
|
529 | return source | |
530 |
|
530 | |||
531 | def file_readlines(filename): |
|
531 | def file_readlines(filename): | |
532 | """Read a file and close it. Returns the file source using readlines().""" |
|
532 | """Read a file and close it. Returns the file source using readlines().""" | |
533 | fobj = open(filename,'r'); |
|
533 | fobj = open(filename,'r'); | |
534 | lines = fobj.readlines(); |
|
534 | lines = fobj.readlines(); | |
535 | fobj.close() |
|
535 | fobj.close() | |
536 | return lines |
|
536 | return lines | |
537 |
|
537 | |||
538 | #---------------------------------------------------------------------------- |
|
538 | #---------------------------------------------------------------------------- | |
539 | def target_outdated(target,deps): |
|
539 | def target_outdated(target,deps): | |
540 | """Determine whether a target is out of date. |
|
540 | """Determine whether a target is out of date. | |
541 |
|
541 | |||
542 | target_outdated(target,deps) -> 1/0 |
|
542 | target_outdated(target,deps) -> 1/0 | |
543 |
|
543 | |||
544 | deps: list of filenames which MUST exist. |
|
544 | deps: list of filenames which MUST exist. | |
545 | target: single filename which may or may not exist. |
|
545 | target: single filename which may or may not exist. | |
546 |
|
546 | |||
547 | If target doesn't exist or is older than any file listed in deps, return |
|
547 | If target doesn't exist or is older than any file listed in deps, return | |
548 | true, otherwise return false. |
|
548 | true, otherwise return false. | |
549 | """ |
|
549 | """ | |
550 | try: |
|
550 | try: | |
551 | target_time = os.path.getmtime(target) |
|
551 | target_time = os.path.getmtime(target) | |
552 | except os.error: |
|
552 | except os.error: | |
553 | return 1 |
|
553 | return 1 | |
554 | for dep in deps: |
|
554 | for dep in deps: | |
555 | dep_time = os.path.getmtime(dep) |
|
555 | dep_time = os.path.getmtime(dep) | |
556 | if dep_time > target_time: |
|
556 | if dep_time > target_time: | |
557 | #print "For target",target,"Dep failed:",dep # dbg |
|
557 | #print "For target",target,"Dep failed:",dep # dbg | |
558 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
558 | #print "times (dep,tar):",dep_time,target_time # dbg | |
559 | return 1 |
|
559 | return 1 | |
560 | return 0 |
|
560 | return 0 | |
561 |
|
561 | |||
562 | #----------------------------------------------------------------------------- |
|
562 | #----------------------------------------------------------------------------- | |
563 | def target_update(target,deps,cmd): |
|
563 | def target_update(target,deps,cmd): | |
564 | """Update a target with a given command given a list of dependencies. |
|
564 | """Update a target with a given command given a list of dependencies. | |
565 |
|
565 | |||
566 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
566 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
567 |
|
567 | |||
568 | This is just a wrapper around target_outdated() which calls the given |
|
568 | This is just a wrapper around target_outdated() which calls the given | |
569 | command if target is outdated.""" |
|
569 | command if target is outdated.""" | |
570 |
|
570 | |||
571 | if target_outdated(target,deps): |
|
571 | if target_outdated(target,deps): | |
572 | xsys(cmd) |
|
572 | xsys(cmd) | |
573 |
|
573 | |||
574 | #---------------------------------------------------------------------------- |
|
574 | #---------------------------------------------------------------------------- | |
575 | def unquote_ends(istr): |
|
575 | def unquote_ends(istr): | |
576 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
576 | """Remove a single pair of quotes from the endpoints of a string.""" | |
577 |
|
577 | |||
578 | if not istr: |
|
578 | if not istr: | |
579 | return istr |
|
579 | return istr | |
580 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
580 | if (istr[0]=="'" and istr[-1]=="'") or \ | |
581 | (istr[0]=='"' and istr[-1]=='"'): |
|
581 | (istr[0]=='"' and istr[-1]=='"'): | |
582 | return istr[1:-1] |
|
582 | return istr[1:-1] | |
583 | else: |
|
583 | else: | |
584 | return istr |
|
584 | return istr | |
585 |
|
585 | |||
586 | #---------------------------------------------------------------------------- |
|
586 | #---------------------------------------------------------------------------- | |
587 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
587 | def process_cmdline(argv,names=[],defaults={},usage=''): | |
588 | """ Process command-line options and arguments. |
|
588 | """ Process command-line options and arguments. | |
589 |
|
589 | |||
590 | Arguments: |
|
590 | Arguments: | |
591 |
|
591 | |||
592 | - argv: list of arguments, typically sys.argv. |
|
592 | - argv: list of arguments, typically sys.argv. | |
593 |
|
593 | |||
594 | - names: list of option names. See DPyGetOpt docs for details on options |
|
594 | - names: list of option names. See DPyGetOpt docs for details on options | |
595 | syntax. |
|
595 | syntax. | |
596 |
|
596 | |||
597 | - defaults: dict of default values. |
|
597 | - defaults: dict of default values. | |
598 |
|
598 | |||
599 | - usage: optional usage notice to print if a wrong argument is passed. |
|
599 | - usage: optional usage notice to print if a wrong argument is passed. | |
600 |
|
600 | |||
601 | Return a dict of options and a list of free arguments.""" |
|
601 | Return a dict of options and a list of free arguments.""" | |
602 |
|
602 | |||
603 | getopt = DPyGetOpt.DPyGetOpt() |
|
603 | getopt = DPyGetOpt.DPyGetOpt() | |
604 | getopt.setIgnoreCase(0) |
|
604 | getopt.setIgnoreCase(0) | |
605 | getopt.parseConfiguration(names) |
|
605 | getopt.parseConfiguration(names) | |
606 |
|
606 | |||
607 | try: |
|
607 | try: | |
608 | getopt.processArguments(argv) |
|
608 | getopt.processArguments(argv) | |
609 | except: |
|
609 | except: | |
610 | print usage |
|
610 | print usage | |
611 | warn(`sys.exc_value`,level=4) |
|
611 | warn(`sys.exc_value`,level=4) | |
612 |
|
612 | |||
613 | defaults.update(getopt.optionValues) |
|
613 | defaults.update(getopt.optionValues) | |
614 | args = getopt.freeValues |
|
614 | args = getopt.freeValues | |
615 |
|
615 | |||
616 | return defaults,args |
|
616 | return defaults,args | |
617 |
|
617 | |||
618 | #---------------------------------------------------------------------------- |
|
618 | #---------------------------------------------------------------------------- | |
619 | def optstr2types(ostr): |
|
619 | def optstr2types(ostr): | |
620 | """Convert a string of option names to a dict of type mappings. |
|
620 | """Convert a string of option names to a dict of type mappings. | |
621 |
|
621 | |||
622 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
622 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} | |
623 |
|
623 | |||
624 | This is used to get the types of all the options in a string formatted |
|
624 | This is used to get the types of all the options in a string formatted | |
625 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
625 | with the conventions of DPyGetOpt. The 'type' None is used for options | |
626 | which are strings (they need no further conversion). This function's main |
|
626 | which are strings (they need no further conversion). This function's main | |
627 | use is to get a typemap for use with read_dict(). |
|
627 | use is to get a typemap for use with read_dict(). | |
628 | """ |
|
628 | """ | |
629 |
|
629 | |||
630 | typeconv = {None:'',int:'',float:''} |
|
630 | typeconv = {None:'',int:'',float:''} | |
631 | typemap = {'s':None,'i':int,'f':float} |
|
631 | typemap = {'s':None,'i':int,'f':float} | |
632 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
632 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') | |
633 |
|
633 | |||
634 | for w in ostr.split(): |
|
634 | for w in ostr.split(): | |
635 | oname,alias,otype = opt_re.match(w).groups() |
|
635 | oname,alias,otype = opt_re.match(w).groups() | |
636 | if otype == '' or alias == '!': # simple switches are integers too |
|
636 | if otype == '' or alias == '!': # simple switches are integers too | |
637 | otype = 'i' |
|
637 | otype = 'i' | |
638 | typeconv[typemap[otype]] += oname + ' ' |
|
638 | typeconv[typemap[otype]] += oname + ' ' | |
639 | return typeconv |
|
639 | return typeconv | |
640 |
|
640 | |||
641 | #---------------------------------------------------------------------------- |
|
641 | #---------------------------------------------------------------------------- | |
642 | def read_dict(filename,type_conv=None,**opt): |
|
642 | def read_dict(filename,type_conv=None,**opt): | |
643 |
|
643 | |||
644 | """Read a dictionary of key=value pairs from an input file, optionally |
|
644 | """Read a dictionary of key=value pairs from an input file, optionally | |
645 | performing conversions on the resulting values. |
|
645 | performing conversions on the resulting values. | |
646 |
|
646 | |||
647 | read_dict(filename,type_conv,**opt) -> dict |
|
647 | read_dict(filename,type_conv,**opt) -> dict | |
648 |
|
648 | |||
649 | Only one value per line is accepted, the format should be |
|
649 | Only one value per line is accepted, the format should be | |
650 | # optional comments are ignored |
|
650 | # optional comments are ignored | |
651 | key value\n |
|
651 | key value\n | |
652 |
|
652 | |||
653 | Args: |
|
653 | Args: | |
654 |
|
654 | |||
655 | - type_conv: A dictionary specifying which keys need to be converted to |
|
655 | - type_conv: A dictionary specifying which keys need to be converted to | |
656 | which types. By default all keys are read as strings. This dictionary |
|
656 | which types. By default all keys are read as strings. This dictionary | |
657 | should have as its keys valid conversion functions for strings |
|
657 | should have as its keys valid conversion functions for strings | |
658 | (int,long,float,complex, or your own). The value for each key |
|
658 | (int,long,float,complex, or your own). The value for each key | |
659 | (converter) should be a whitespace separated string containing the names |
|
659 | (converter) should be a whitespace separated string containing the names | |
660 | of all the entries in the file to be converted using that function. For |
|
660 | of all the entries in the file to be converted using that function. For | |
661 | keys to be left alone, use None as the conversion function (only needed |
|
661 | keys to be left alone, use None as the conversion function (only needed | |
662 | with purge=1, see below). |
|
662 | with purge=1, see below). | |
663 |
|
663 | |||
664 | - opt: dictionary with extra options as below (default in parens) |
|
664 | - opt: dictionary with extra options as below (default in parens) | |
665 |
|
665 | |||
666 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
666 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out | |
667 | of the dictionary to be returned. If purge is going to be used, the |
|
667 | of the dictionary to be returned. If purge is going to be used, the | |
668 | set of keys to be left as strings also has to be explicitly specified |
|
668 | set of keys to be left as strings also has to be explicitly specified | |
669 | using the (non-existent) conversion function None. |
|
669 | using the (non-existent) conversion function None. | |
670 |
|
670 | |||
671 | fs(None): field separator. This is the key/value separator to be used |
|
671 | fs(None): field separator. This is the key/value separator to be used | |
672 | when parsing the file. The None default means any whitespace [behavior |
|
672 | when parsing the file. The None default means any whitespace [behavior | |
673 | of string.split()]. |
|
673 | of string.split()]. | |
674 |
|
674 | |||
675 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
675 | strip(0): if 1, strip string values of leading/trailinig whitespace. | |
676 |
|
676 | |||
677 | warn(1): warning level if requested keys are not found in file. |
|
677 | warn(1): warning level if requested keys are not found in file. | |
678 | - 0: silently ignore. |
|
678 | - 0: silently ignore. | |
679 | - 1: inform but proceed. |
|
679 | - 1: inform but proceed. | |
680 | - 2: raise KeyError exception. |
|
680 | - 2: raise KeyError exception. | |
681 |
|
681 | |||
682 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
682 | no_empty(0): if 1, remove keys with whitespace strings as a value. | |
683 |
|
683 | |||
684 | unique([]): list of keys (or space separated string) which can't be |
|
684 | unique([]): list of keys (or space separated string) which can't be | |
685 | repeated. If one such key is found in the file, each new instance |
|
685 | repeated. If one such key is found in the file, each new instance | |
686 | overwrites the previous one. For keys not listed here, the behavior is |
|
686 | overwrites the previous one. For keys not listed here, the behavior is | |
687 | to make a list of all appearances. |
|
687 | to make a list of all appearances. | |
688 |
|
688 | |||
689 | Example: |
|
689 | Example: | |
690 | If the input file test.ini has: |
|
690 | If the input file test.ini has: | |
691 | i 3 |
|
691 | i 3 | |
692 | x 4.5 |
|
692 | x 4.5 | |
693 | y 5.5 |
|
693 | y 5.5 | |
694 | s hi ho |
|
694 | s hi ho | |
695 | Then: |
|
695 | Then: | |
696 |
|
696 | |||
697 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
697 | >>> type_conv={int:'i',float:'x',None:'s'} | |
698 | >>> read_dict('test.ini') |
|
698 | >>> read_dict('test.ini') | |
699 | {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'} |
|
699 | {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'} | |
700 | >>> read_dict('test.ini',type_conv) |
|
700 | >>> read_dict('test.ini',type_conv) | |
701 | {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'} |
|
701 | {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'} | |
702 | >>> read_dict('test.ini',type_conv,purge=1) |
|
702 | >>> read_dict('test.ini',type_conv,purge=1) | |
703 | {'i': 3, 's': 'hi ho', 'x': 4.5} |
|
703 | {'i': 3, 's': 'hi ho', 'x': 4.5} | |
704 | """ |
|
704 | """ | |
705 |
|
705 | |||
706 | # starting config |
|
706 | # starting config | |
707 | opt.setdefault('purge',0) |
|
707 | opt.setdefault('purge',0) | |
708 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
708 | opt.setdefault('fs',None) # field sep defaults to any whitespace | |
709 | opt.setdefault('strip',0) |
|
709 | opt.setdefault('strip',0) | |
710 | opt.setdefault('warn',1) |
|
710 | opt.setdefault('warn',1) | |
711 | opt.setdefault('no_empty',0) |
|
711 | opt.setdefault('no_empty',0) | |
712 | opt.setdefault('unique','') |
|
712 | opt.setdefault('unique','') | |
713 | if type(opt['unique']) in StringTypes: |
|
713 | if type(opt['unique']) in StringTypes: | |
714 | unique_keys = qw(opt['unique']) |
|
714 | unique_keys = qw(opt['unique']) | |
715 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
715 | elif type(opt['unique']) in (types.TupleType,types.ListType): | |
716 | unique_keys = opt['unique'] |
|
716 | unique_keys = opt['unique'] | |
717 | else: |
|
717 | else: | |
718 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
718 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' | |
719 |
|
719 | |||
720 | dict = {} |
|
720 | dict = {} | |
721 | # first read in table of values as strings |
|
721 | # first read in table of values as strings | |
722 | file = open(filename,'r') |
|
722 | file = open(filename,'r') | |
723 | for line in file.readlines(): |
|
723 | for line in file.readlines(): | |
724 | line = line.strip() |
|
724 | line = line.strip() | |
725 | if len(line) and line[0]=='#': continue |
|
725 | if len(line) and line[0]=='#': continue | |
726 | if len(line)>0: |
|
726 | if len(line)>0: | |
727 | lsplit = line.split(opt['fs'],1) |
|
727 | lsplit = line.split(opt['fs'],1) | |
728 | try: |
|
728 | try: | |
729 | key,val = lsplit |
|
729 | key,val = lsplit | |
730 | except ValueError: |
|
730 | except ValueError: | |
731 | key,val = lsplit[0],'' |
|
731 | key,val = lsplit[0],'' | |
732 | key = key.strip() |
|
732 | key = key.strip() | |
733 | if opt['strip']: val = val.strip() |
|
733 | if opt['strip']: val = val.strip() | |
734 | if val == "''" or val == '""': val = '' |
|
734 | if val == "''" or val == '""': val = '' | |
735 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
735 | if opt['no_empty'] and (val=='' or val.isspace()): | |
736 | continue |
|
736 | continue | |
737 | # if a key is found more than once in the file, build a list |
|
737 | # if a key is found more than once in the file, build a list | |
738 | # unless it's in the 'unique' list. In that case, last found in file |
|
738 | # unless it's in the 'unique' list. In that case, last found in file | |
739 | # takes precedence. User beware. |
|
739 | # takes precedence. User beware. | |
740 | try: |
|
740 | try: | |
741 | if dict[key] and key in unique_keys: |
|
741 | if dict[key] and key in unique_keys: | |
742 | dict[key] = val |
|
742 | dict[key] = val | |
743 | elif type(dict[key]) is types.ListType: |
|
743 | elif type(dict[key]) is types.ListType: | |
744 | dict[key].append(val) |
|
744 | dict[key].append(val) | |
745 | else: |
|
745 | else: | |
746 | dict[key] = [dict[key],val] |
|
746 | dict[key] = [dict[key],val] | |
747 | except KeyError: |
|
747 | except KeyError: | |
748 | dict[key] = val |
|
748 | dict[key] = val | |
749 | # purge if requested |
|
749 | # purge if requested | |
750 | if opt['purge']: |
|
750 | if opt['purge']: | |
751 | accepted_keys = qwflat(type_conv.values()) |
|
751 | accepted_keys = qwflat(type_conv.values()) | |
752 | for key in dict.keys(): |
|
752 | for key in dict.keys(): | |
753 | if key in accepted_keys: continue |
|
753 | if key in accepted_keys: continue | |
754 | del(dict[key]) |
|
754 | del(dict[key]) | |
755 | # now convert if requested |
|
755 | # now convert if requested | |
756 | if type_conv==None: return dict |
|
756 | if type_conv==None: return dict | |
757 | conversions = type_conv.keys() |
|
757 | conversions = type_conv.keys() | |
758 | try: conversions.remove(None) |
|
758 | try: conversions.remove(None) | |
759 | except: pass |
|
759 | except: pass | |
760 | for convert in conversions: |
|
760 | for convert in conversions: | |
761 | for val in qw(type_conv[convert]): |
|
761 | for val in qw(type_conv[convert]): | |
762 | try: |
|
762 | try: | |
763 | dict[val] = convert(dict[val]) |
|
763 | dict[val] = convert(dict[val]) | |
764 | except KeyError,e: |
|
764 | except KeyError,e: | |
765 | if opt['warn'] == 0: |
|
765 | if opt['warn'] == 0: | |
766 | pass |
|
766 | pass | |
767 | elif opt['warn'] == 1: |
|
767 | elif opt['warn'] == 1: | |
768 | print >>sys.stderr, 'Warning: key',val,\ |
|
768 | print >>sys.stderr, 'Warning: key',val,\ | |
769 | 'not found in file',filename |
|
769 | 'not found in file',filename | |
770 | elif opt['warn'] == 2: |
|
770 | elif opt['warn'] == 2: | |
771 | raise KeyError,e |
|
771 | raise KeyError,e | |
772 | else: |
|
772 | else: | |
773 | raise ValueError,'Warning level must be 0,1 or 2' |
|
773 | raise ValueError,'Warning level must be 0,1 or 2' | |
774 |
|
774 | |||
775 | return dict |
|
775 | return dict | |
776 |
|
776 | |||
777 | #---------------------------------------------------------------------------- |
|
777 | #---------------------------------------------------------------------------- | |
778 | def flag_calls(func): |
|
778 | def flag_calls(func): | |
779 | """Wrap a function to detect and flag when it gets called. |
|
779 | """Wrap a function to detect and flag when it gets called. | |
780 |
|
780 | |||
781 | This is a decorator which takes a function and wraps it in a function with |
|
781 | This is a decorator which takes a function and wraps it in a function with | |
782 | a 'called' attribute. wrapper.called is initialized to False. |
|
782 | a 'called' attribute. wrapper.called is initialized to False. | |
783 |
|
783 | |||
784 | The wrapper.called attribute is set to False right before each call to the |
|
784 | The wrapper.called attribute is set to False right before each call to the | |
785 | wrapped function, so if the call fails it remains False. After the call |
|
785 | wrapped function, so if the call fails it remains False. After the call | |
786 | completes, wrapper.called is set to True and the output is returned. |
|
786 | completes, wrapper.called is set to True and the output is returned. | |
787 |
|
787 | |||
788 | Testing for truth in wrapper.called allows you to determine if a call to |
|
788 | Testing for truth in wrapper.called allows you to determine if a call to | |
789 | func() was attempted and succeeded.""" |
|
789 | func() was attempted and succeeded.""" | |
790 |
|
790 | |||
791 | def wrapper(*args,**kw): |
|
791 | def wrapper(*args,**kw): | |
792 | wrapper.called = False |
|
792 | wrapper.called = False | |
793 | out = func(*args,**kw) |
|
793 | out = func(*args,**kw) | |
794 | wrapper.called = True |
|
794 | wrapper.called = True | |
795 | return out |
|
795 | return out | |
796 |
|
796 | |||
797 | wrapper.called = False |
|
797 | wrapper.called = False | |
798 | wrapper.__doc__ = func.__doc__ |
|
798 | wrapper.__doc__ = func.__doc__ | |
799 | return wrapper |
|
799 | return wrapper | |
800 |
|
800 | |||
801 | #---------------------------------------------------------------------------- |
|
801 | #---------------------------------------------------------------------------- | |
802 | class HomeDirError(Error): |
|
802 | class HomeDirError(Error): | |
803 | pass |
|
803 | pass | |
804 |
|
804 | |||
805 | def get_home_dir(): |
|
805 | def get_home_dir(): | |
806 | """Return the closest possible equivalent to a 'home' directory. |
|
806 | """Return the closest possible equivalent to a 'home' directory. | |
807 |
|
807 | |||
808 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
808 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. | |
809 |
|
809 | |||
810 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
810 | Currently only Posix and NT are implemented, a HomeDirError exception is | |
811 | raised for all other OSes. """ |
|
811 | raised for all other OSes. """ | |
812 |
|
812 | |||
813 | isdir = os.path.isdir |
|
813 | isdir = os.path.isdir | |
814 | env = os.environ |
|
814 | env = os.environ | |
815 |
|
815 | |||
816 | # first, check py2exe distribution root directory for _ipython. |
|
816 | # first, check py2exe distribution root directory for _ipython. | |
817 | # This overrides all. Normally does not exist. |
|
817 | # This overrides all. Normally does not exist. | |
818 |
|
818 | |||
819 | if '\\library.zip\\' in IPython.__file__.lower(): |
|
819 | if '\\library.zip\\' in IPython.__file__.lower(): | |
820 | root, rest = IPython.__file__.lower().split('library.zip') |
|
820 | root, rest = IPython.__file__.lower().split('library.zip') | |
821 | if isdir(root + '_ipython'): |
|
821 | if isdir(root + '_ipython'): | |
822 | os.environ["IPYKITROOT"] = root.rstrip('\\') |
|
822 | os.environ["IPYKITROOT"] = root.rstrip('\\') | |
823 | return root |
|
823 | return root | |
824 |
|
824 | |||
825 | try: |
|
825 | try: | |
826 | homedir = env['HOME'] |
|
826 | homedir = env['HOME'] | |
827 | if not isdir(homedir): |
|
827 | if not isdir(homedir): | |
828 | # in case a user stuck some string which does NOT resolve to a |
|
828 | # in case a user stuck some string which does NOT resolve to a | |
829 | # valid path, it's as good as if we hadn't foud it |
|
829 | # valid path, it's as good as if we hadn't foud it | |
830 | raise KeyError |
|
830 | raise KeyError | |
831 | return homedir |
|
831 | return homedir | |
832 | except KeyError: |
|
832 | except KeyError: | |
833 | if os.name == 'posix': |
|
833 | if os.name == 'posix': | |
834 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
834 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' | |
835 | elif os.name == 'nt': |
|
835 | elif os.name == 'nt': | |
836 | # For some strange reason, win9x returns 'nt' for os.name. |
|
836 | # For some strange reason, win9x returns 'nt' for os.name. | |
837 | try: |
|
837 | try: | |
838 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
838 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) | |
839 | if not isdir(homedir): |
|
839 | if not isdir(homedir): | |
840 | homedir = os.path.join(env['USERPROFILE']) |
|
840 | homedir = os.path.join(env['USERPROFILE']) | |
841 | if not isdir(homedir): |
|
841 | if not isdir(homedir): | |
842 | raise HomeDirError |
|
842 | raise HomeDirError | |
843 | return homedir |
|
843 | return homedir | |
844 | except: |
|
844 | except: | |
845 | try: |
|
845 | try: | |
846 | # Use the registry to get the 'My Documents' folder. |
|
846 | # Use the registry to get the 'My Documents' folder. | |
847 | import _winreg as wreg |
|
847 | import _winreg as wreg | |
848 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
848 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, | |
849 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
849 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") | |
850 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
850 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
851 | key.Close() |
|
851 | key.Close() | |
852 | if not isdir(homedir): |
|
852 | if not isdir(homedir): | |
853 | e = ('Invalid "Personal" folder registry key ' |
|
853 | e = ('Invalid "Personal" folder registry key ' | |
854 | 'typically "My Documents".\n' |
|
854 | 'typically "My Documents".\n' | |
855 | 'Value: %s\n' |
|
855 | 'Value: %s\n' | |
856 | 'This is not a valid directory on your system.' % |
|
856 | 'This is not a valid directory on your system.' % | |
857 | homedir) |
|
857 | homedir) | |
858 | raise HomeDirError(e) |
|
858 | raise HomeDirError(e) | |
859 | return homedir |
|
859 | return homedir | |
860 | except HomeDirError: |
|
860 | except HomeDirError: | |
861 | raise |
|
861 | raise | |
862 | except: |
|
862 | except: | |
863 | return 'C:\\' |
|
863 | return 'C:\\' | |
864 | elif os.name == 'dos': |
|
864 | elif os.name == 'dos': | |
865 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
865 | # Desperate, may do absurd things in classic MacOS. May work under DOS. | |
866 | return 'C:\\' |
|
866 | return 'C:\\' | |
867 | else: |
|
867 | else: | |
868 | raise HomeDirError,'support for your operating system not implemented.' |
|
868 | raise HomeDirError,'support for your operating system not implemented.' | |
869 |
|
869 | |||
870 | #**************************************************************************** |
|
870 | #**************************************************************************** | |
871 | # strings and text |
|
871 | # strings and text | |
872 |
|
872 | |||
873 | class LSString(str): |
|
873 | class LSString(str): | |
874 | """String derivative with a special access attributes. |
|
874 | """String derivative with a special access attributes. | |
875 |
|
875 | |||
876 | These are normal strings, but with the special attributes: |
|
876 | These are normal strings, but with the special attributes: | |
877 |
|
877 | |||
878 | .l (or .list) : value as list (split on newlines). |
|
878 | .l (or .list) : value as list (split on newlines). | |
879 | .n (or .nlstr): original value (the string itself). |
|
879 | .n (or .nlstr): original value (the string itself). | |
880 | .s (or .spstr): value as whitespace-separated string. |
|
880 | .s (or .spstr): value as whitespace-separated string. | |
881 | .p (or .paths): list of path objects |
|
881 | .p (or .paths): list of path objects | |
882 |
|
882 | |||
883 | Any values which require transformations are computed only once and |
|
883 | Any values which require transformations are computed only once and | |
884 | cached. |
|
884 | cached. | |
885 |
|
885 | |||
886 | Such strings are very useful to efficiently interact with the shell, which |
|
886 | Such strings are very useful to efficiently interact with the shell, which | |
887 | typically only understands whitespace-separated options for commands.""" |
|
887 | typically only understands whitespace-separated options for commands.""" | |
888 |
|
888 | |||
889 | def get_list(self): |
|
889 | def get_list(self): | |
890 | try: |
|
890 | try: | |
891 | return self.__list |
|
891 | return self.__list | |
892 | except AttributeError: |
|
892 | except AttributeError: | |
893 | self.__list = self.split('\n') |
|
893 | self.__list = self.split('\n') | |
894 | return self.__list |
|
894 | return self.__list | |
895 |
|
895 | |||
896 | l = list = property(get_list) |
|
896 | l = list = property(get_list) | |
897 |
|
897 | |||
898 | def get_spstr(self): |
|
898 | def get_spstr(self): | |
899 | try: |
|
899 | try: | |
900 | return self.__spstr |
|
900 | return self.__spstr | |
901 | except AttributeError: |
|
901 | except AttributeError: | |
902 | self.__spstr = self.replace('\n',' ') |
|
902 | self.__spstr = self.replace('\n',' ') | |
903 | return self.__spstr |
|
903 | return self.__spstr | |
904 |
|
904 | |||
905 | s = spstr = property(get_spstr) |
|
905 | s = spstr = property(get_spstr) | |
906 |
|
906 | |||
907 | def get_nlstr(self): |
|
907 | def get_nlstr(self): | |
908 | return self |
|
908 | return self | |
909 |
|
909 | |||
910 | n = nlstr = property(get_nlstr) |
|
910 | n = nlstr = property(get_nlstr) | |
911 |
|
911 | |||
912 | def get_paths(self): |
|
912 | def get_paths(self): | |
913 | try: |
|
913 | try: | |
914 | return self.__paths |
|
914 | return self.__paths | |
915 | except AttributeError: |
|
915 | except AttributeError: | |
916 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
916 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] | |
917 | return self.__paths |
|
917 | return self.__paths | |
918 |
|
918 | |||
919 | p = paths = property(get_paths) |
|
919 | p = paths = property(get_paths) | |
920 |
|
920 | |||
921 | def print_lsstring(arg): |
|
921 | def print_lsstring(arg): | |
922 | """ Prettier (non-repr-like) and more informative printer for LSString """ |
|
922 | """ Prettier (non-repr-like) and more informative printer for LSString """ | |
923 | print "LSString (.p, .n, .l, .s available). Value:" |
|
923 | print "LSString (.p, .n, .l, .s available). Value:" | |
924 | print arg |
|
924 | print arg | |
925 |
|
925 | |||
926 | print_lsstring = result_display.when_type(LSString)(print_lsstring) |
|
926 | print_lsstring = result_display.when_type(LSString)(print_lsstring) | |
927 |
|
927 | |||
928 | #---------------------------------------------------------------------------- |
|
928 | #---------------------------------------------------------------------------- | |
929 | class SList(list): |
|
929 | class SList(list): | |
930 | """List derivative with a special access attributes. |
|
930 | """List derivative with a special access attributes. | |
931 |
|
931 | |||
932 | These are normal lists, but with the special attributes: |
|
932 | These are normal lists, but with the special attributes: | |
933 |
|
933 | |||
934 | .l (or .list) : value as list (the list itself). |
|
934 | .l (or .list) : value as list (the list itself). | |
935 | .n (or .nlstr): value as a string, joined on newlines. |
|
935 | .n (or .nlstr): value as a string, joined on newlines. | |
936 | .s (or .spstr): value as a string, joined on spaces. |
|
936 | .s (or .spstr): value as a string, joined on spaces. | |
937 | .p (or .paths): list of path objects |
|
937 | .p (or .paths): list of path objects | |
938 |
|
938 | |||
939 | Any values which require transformations are computed only once and |
|
939 | Any values which require transformations are computed only once and | |
940 | cached.""" |
|
940 | cached.""" | |
941 |
|
941 | |||
942 | def get_list(self): |
|
942 | def get_list(self): | |
943 | return self |
|
943 | return self | |
944 |
|
944 | |||
945 | l = list = property(get_list) |
|
945 | l = list = property(get_list) | |
946 |
|
946 | |||
947 | def get_spstr(self): |
|
947 | def get_spstr(self): | |
948 | try: |
|
948 | try: | |
949 | return self.__spstr |
|
949 | return self.__spstr | |
950 | except AttributeError: |
|
950 | except AttributeError: | |
951 | self.__spstr = ' '.join(self) |
|
951 | self.__spstr = ' '.join(self) | |
952 | return self.__spstr |
|
952 | return self.__spstr | |
953 |
|
953 | |||
954 | s = spstr = property(get_spstr) |
|
954 | s = spstr = property(get_spstr) | |
955 |
|
955 | |||
956 | def get_nlstr(self): |
|
956 | def get_nlstr(self): | |
957 | try: |
|
957 | try: | |
958 | return self.__nlstr |
|
958 | return self.__nlstr | |
959 | except AttributeError: |
|
959 | except AttributeError: | |
960 | self.__nlstr = '\n'.join(self) |
|
960 | self.__nlstr = '\n'.join(self) | |
961 | return self.__nlstr |
|
961 | return self.__nlstr | |
962 |
|
962 | |||
963 | n = nlstr = property(get_nlstr) |
|
963 | n = nlstr = property(get_nlstr) | |
964 |
|
964 | |||
965 | def get_paths(self): |
|
965 | def get_paths(self): | |
966 | try: |
|
966 | try: | |
967 | return self.__paths |
|
967 | return self.__paths | |
968 | except AttributeError: |
|
968 | except AttributeError: | |
969 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
969 | self.__paths = [path(p) for p in self if os.path.exists(p)] | |
970 | return self.__paths |
|
970 | return self.__paths | |
971 |
|
971 | |||
972 | p = paths = property(get_paths) |
|
972 | p = paths = property(get_paths) | |
973 |
|
973 | |||
974 | #---------------------------------------------------------------------------- |
|
974 | #---------------------------------------------------------------------------- | |
975 | def esc_quotes(strng): |
|
975 | def esc_quotes(strng): | |
976 | """Return the input string with single and double quotes escaped out""" |
|
976 | """Return the input string with single and double quotes escaped out""" | |
977 |
|
977 | |||
978 | return strng.replace('"','\\"').replace("'","\\'") |
|
978 | return strng.replace('"','\\"').replace("'","\\'") | |
979 |
|
979 | |||
980 | #---------------------------------------------------------------------------- |
|
980 | #---------------------------------------------------------------------------- | |
981 | def make_quoted_expr(s): |
|
981 | def make_quoted_expr(s): | |
982 | """Return string s in appropriate quotes, using raw string if possible. |
|
982 | """Return string s in appropriate quotes, using raw string if possible. | |
983 |
|
983 | |||
984 | Effectively this turns string: cd \ao\ao\ |
|
984 | Effectively this turns string: cd \ao\ao\ | |
985 | to: r"cd \ao\ao\_"[:-1] |
|
985 | to: r"cd \ao\ao\_"[:-1] | |
986 |
|
986 | |||
987 | Note the use of raw string and padding at the end to allow trailing backslash. |
|
987 | Note the use of raw string and padding at the end to allow trailing backslash. | |
988 |
|
988 | |||
989 | """ |
|
989 | """ | |
990 |
|
990 | |||
991 | tail = '' |
|
991 | tail = '' | |
992 | tailpadding = '' |
|
992 | tailpadding = '' | |
993 | raw = '' |
|
993 | raw = '' | |
994 | if "\\" in s: |
|
994 | if "\\" in s: | |
995 | raw = 'r' |
|
995 | raw = 'r' | |
996 | if s.endswith('\\'): |
|
996 | if s.endswith('\\'): | |
997 | tail = '[:-1]' |
|
997 | tail = '[:-1]' | |
998 | tailpadding = '_' |
|
998 | tailpadding = '_' | |
999 | if '"' not in s: |
|
999 | if '"' not in s: | |
1000 | quote = '"' |
|
1000 | quote = '"' | |
1001 | elif "'" not in s: |
|
1001 | elif "'" not in s: | |
1002 | quote = "'" |
|
1002 | quote = "'" | |
1003 | elif '"""' not in s and not s.endswith('"'): |
|
1003 | elif '"""' not in s and not s.endswith('"'): | |
1004 | quote = '"""' |
|
1004 | quote = '"""' | |
1005 | elif "'''" not in s and not s.endswith("'"): |
|
1005 | elif "'''" not in s and not s.endswith("'"): | |
1006 | quote = "'''" |
|
1006 | quote = "'''" | |
1007 | else: |
|
1007 | else: | |
1008 | # give up, backslash-escaped string will do |
|
1008 | # give up, backslash-escaped string will do | |
1009 | return '"%s"' % esc_quotes(s) |
|
1009 | return '"%s"' % esc_quotes(s) | |
1010 | res = itpl("$raw$quote$s$tailpadding$quote$tail") |
|
1010 | res = itpl("$raw$quote$s$tailpadding$quote$tail") | |
1011 | return res |
|
1011 | return res | |
1012 |
|
1012 | |||
1013 |
|
1013 | |||
1014 | #---------------------------------------------------------------------------- |
|
1014 | #---------------------------------------------------------------------------- | |
1015 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
1015 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): | |
1016 | """Take multiple lines of input. |
|
1016 | """Take multiple lines of input. | |
1017 |
|
1017 | |||
1018 | A list with each line of input as a separate element is returned when a |
|
1018 | A list with each line of input as a separate element is returned when a | |
1019 | termination string is entered (defaults to a single '.'). Input can also |
|
1019 | termination string is entered (defaults to a single '.'). Input can also | |
1020 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
1020 | terminate via EOF (^D in Unix, ^Z-RET in Windows). | |
1021 |
|
1021 | |||
1022 | Lines of input which end in \\ are joined into single entries (and a |
|
1022 | Lines of input which end in \\ are joined into single entries (and a | |
1023 | secondary continuation prompt is issued as long as the user terminates |
|
1023 | secondary continuation prompt is issued as long as the user terminates | |
1024 | lines with \\). This allows entering very long strings which are still |
|
1024 | lines with \\). This allows entering very long strings which are still | |
1025 | meant to be treated as single entities. |
|
1025 | meant to be treated as single entities. | |
1026 | """ |
|
1026 | """ | |
1027 |
|
1027 | |||
1028 | try: |
|
1028 | try: | |
1029 | if header: |
|
1029 | if header: | |
1030 | header += '\n' |
|
1030 | header += '\n' | |
1031 | lines = [raw_input(header + ps1)] |
|
1031 | lines = [raw_input(header + ps1)] | |
1032 | except EOFError: |
|
1032 | except EOFError: | |
1033 | return [] |
|
1033 | return [] | |
1034 | terminate = [terminate_str] |
|
1034 | terminate = [terminate_str] | |
1035 | try: |
|
1035 | try: | |
1036 | while lines[-1:] != terminate: |
|
1036 | while lines[-1:] != terminate: | |
1037 | new_line = raw_input(ps1) |
|
1037 | new_line = raw_input(ps1) | |
1038 | while new_line.endswith('\\'): |
|
1038 | while new_line.endswith('\\'): | |
1039 | new_line = new_line[:-1] + raw_input(ps2) |
|
1039 | new_line = new_line[:-1] + raw_input(ps2) | |
1040 | lines.append(new_line) |
|
1040 | lines.append(new_line) | |
1041 |
|
1041 | |||
1042 | return lines[:-1] # don't return the termination command |
|
1042 | return lines[:-1] # don't return the termination command | |
1043 | except EOFError: |
|
1043 | except EOFError: | |
1044 |
|
1044 | |||
1045 | return lines |
|
1045 | return lines | |
1046 |
|
1046 | |||
1047 | #---------------------------------------------------------------------------- |
|
1047 | #---------------------------------------------------------------------------- | |
1048 | def raw_input_ext(prompt='', ps2='... '): |
|
1048 | def raw_input_ext(prompt='', ps2='... '): | |
1049 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
1049 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" | |
1050 |
|
1050 | |||
1051 | line = raw_input(prompt) |
|
1051 | line = raw_input(prompt) | |
1052 | while line.endswith('\\'): |
|
1052 | while line.endswith('\\'): | |
1053 | line = line[:-1] + raw_input(ps2) |
|
1053 | line = line[:-1] + raw_input(ps2) | |
1054 | return line |
|
1054 | return line | |
1055 |
|
1055 | |||
1056 | #---------------------------------------------------------------------------- |
|
1056 | #---------------------------------------------------------------------------- | |
1057 | def ask_yes_no(prompt,default=None): |
|
1057 | def ask_yes_no(prompt,default=None): | |
1058 |
"""Asks a question and returns a |
|
1058 | """Asks a question and returns a boolean (y/n) answer. | |
1059 |
|
1059 | |||
1060 | If default is given (one of 'y','n'), it is used if the user input is |
|
1060 | If default is given (one of 'y','n'), it is used if the user input is | |
1061 | empty. Otherwise the question is repeated until an answer is given. |
|
1061 | empty. Otherwise the question is repeated until an answer is given. | |
1062 |
|
1062 | |||
1063 | An EOF is treated as the default answer. If there is no default, an |
|
1063 | An EOF is treated as the default answer. If there is no default, an | |
1064 | exception is raised to prevent infinite loops. |
|
1064 | exception is raised to prevent infinite loops. | |
1065 |
|
1065 | |||
1066 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1066 | Valid answers are: y/yes/n/no (match is not case sensitive).""" | |
1067 |
|
1067 | |||
1068 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1068 | answers = {'y':True,'n':False,'yes':True,'no':False} | |
1069 | ans = None |
|
1069 | ans = None | |
1070 | while ans not in answers.keys(): |
|
1070 | while ans not in answers.keys(): | |
1071 | try: |
|
1071 | try: | |
1072 | ans = raw_input(prompt+' ').lower() |
|
1072 | ans = raw_input(prompt+' ').lower() | |
1073 | if not ans: # response was an empty string |
|
1073 | if not ans: # response was an empty string | |
1074 | ans = default |
|
1074 | ans = default | |
1075 | except KeyboardInterrupt: |
|
1075 | except KeyboardInterrupt: | |
1076 | pass |
|
1076 | pass | |
1077 | except EOFError: |
|
1077 | except EOFError: | |
1078 | if default in answers.keys(): |
|
1078 | if default in answers.keys(): | |
1079 | ans = default |
|
1079 | ans = default | |
1080 |
|
1080 | |||
1081 | else: |
|
1081 | else: | |
1082 | raise |
|
1082 | raise | |
1083 |
|
1083 | |||
1084 | return answers[ans] |
|
1084 | return answers[ans] | |
1085 |
|
1085 | |||
1086 | #---------------------------------------------------------------------------- |
|
1086 | #---------------------------------------------------------------------------- | |
1087 | def marquee(txt='',width=78,mark='*'): |
|
1087 | def marquee(txt='',width=78,mark='*'): | |
1088 | """Return the input string centered in a 'marquee'.""" |
|
1088 | """Return the input string centered in a 'marquee'.""" | |
1089 | if not txt: |
|
1089 | if not txt: | |
1090 | return (mark*width)[:width] |
|
1090 | return (mark*width)[:width] | |
1091 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1091 | nmark = (width-len(txt)-2)/len(mark)/2 | |
1092 | if nmark < 0: nmark =0 |
|
1092 | if nmark < 0: nmark =0 | |
1093 | marks = mark*nmark |
|
1093 | marks = mark*nmark | |
1094 | return '%s %s %s' % (marks,txt,marks) |
|
1094 | return '%s %s %s' % (marks,txt,marks) | |
1095 |
|
1095 | |||
1096 | #---------------------------------------------------------------------------- |
|
1096 | #---------------------------------------------------------------------------- | |
1097 | class EvalDict: |
|
1097 | class EvalDict: | |
1098 | """ |
|
1098 | """ | |
1099 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1099 | Emulate a dict which evaluates its contents in the caller's frame. | |
1100 |
|
1100 | |||
1101 | Usage: |
|
1101 | Usage: | |
1102 | >>>number = 19 |
|
1102 | >>>number = 19 | |
1103 | >>>text = "python" |
|
1103 | >>>text = "python" | |
1104 | >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1104 | >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() | |
1105 | """ |
|
1105 | """ | |
1106 |
|
1106 | |||
1107 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1107 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a | |
1108 | # modified (shorter) version of: |
|
1108 | # modified (shorter) version of: | |
1109 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1109 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by | |
1110 | # Skip Montanaro (skip@pobox.com). |
|
1110 | # Skip Montanaro (skip@pobox.com). | |
1111 |
|
1111 | |||
1112 | def __getitem__(self, name): |
|
1112 | def __getitem__(self, name): | |
1113 | frame = sys._getframe(1) |
|
1113 | frame = sys._getframe(1) | |
1114 | return eval(name, frame.f_globals, frame.f_locals) |
|
1114 | return eval(name, frame.f_globals, frame.f_locals) | |
1115 |
|
1115 | |||
1116 | EvalString = EvalDict # for backwards compatibility |
|
1116 | EvalString = EvalDict # for backwards compatibility | |
1117 | #---------------------------------------------------------------------------- |
|
1117 | #---------------------------------------------------------------------------- | |
1118 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1118 | def qw(words,flat=0,sep=None,maxsplit=-1): | |
1119 | """Similar to Perl's qw() operator, but with some more options. |
|
1119 | """Similar to Perl's qw() operator, but with some more options. | |
1120 |
|
1120 | |||
1121 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1121 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) | |
1122 |
|
1122 | |||
1123 | words can also be a list itself, and with flat=1, the output will be |
|
1123 | words can also be a list itself, and with flat=1, the output will be | |
1124 | recursively flattened. Examples: |
|
1124 | recursively flattened. Examples: | |
1125 |
|
1125 | |||
1126 | >>> qw('1 2') |
|
1126 | >>> qw('1 2') | |
1127 | ['1', '2'] |
|
1127 | ['1', '2'] | |
1128 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1128 | >>> qw(['a b','1 2',['m n','p q']]) | |
1129 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1129 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] | |
1130 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1130 | >>> qw(['a b','1 2',['m n','p q']],flat=1) | |
1131 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """ |
|
1131 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """ | |
1132 |
|
1132 | |||
1133 | if type(words) in StringTypes: |
|
1133 | if type(words) in StringTypes: | |
1134 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1134 | return [word.strip() for word in words.split(sep,maxsplit) | |
1135 | if word and not word.isspace() ] |
|
1135 | if word and not word.isspace() ] | |
1136 | if flat: |
|
1136 | if flat: | |
1137 | return flatten(map(qw,words,[1]*len(words))) |
|
1137 | return flatten(map(qw,words,[1]*len(words))) | |
1138 | return map(qw,words) |
|
1138 | return map(qw,words) | |
1139 |
|
1139 | |||
1140 | #---------------------------------------------------------------------------- |
|
1140 | #---------------------------------------------------------------------------- | |
1141 | def qwflat(words,sep=None,maxsplit=-1): |
|
1141 | def qwflat(words,sep=None,maxsplit=-1): | |
1142 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1142 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" | |
1143 | return qw(words,1,sep,maxsplit) |
|
1143 | return qw(words,1,sep,maxsplit) | |
1144 |
|
1144 | |||
1145 | #---------------------------------------------------------------------------- |
|
1145 | #---------------------------------------------------------------------------- | |
1146 | def qw_lol(indata): |
|
1146 | def qw_lol(indata): | |
1147 | """qw_lol('a b') -> [['a','b']], |
|
1147 | """qw_lol('a b') -> [['a','b']], | |
1148 | otherwise it's just a call to qw(). |
|
1148 | otherwise it's just a call to qw(). | |
1149 |
|
1149 | |||
1150 | We need this to make sure the modules_some keys *always* end up as a |
|
1150 | We need this to make sure the modules_some keys *always* end up as a | |
1151 | list of lists.""" |
|
1151 | list of lists.""" | |
1152 |
|
1152 | |||
1153 | if type(indata) in StringTypes: |
|
1153 | if type(indata) in StringTypes: | |
1154 | return [qw(indata)] |
|
1154 | return [qw(indata)] | |
1155 | else: |
|
1155 | else: | |
1156 | return qw(indata) |
|
1156 | return qw(indata) | |
1157 |
|
1157 | |||
1158 | #----------------------------------------------------------------------------- |
|
1158 | #----------------------------------------------------------------------------- | |
1159 | def list_strings(arg): |
|
1159 | def list_strings(arg): | |
1160 | """Always return a list of strings, given a string or list of strings |
|
1160 | """Always return a list of strings, given a string or list of strings | |
1161 | as input.""" |
|
1161 | as input.""" | |
1162 |
|
1162 | |||
1163 | if type(arg) in StringTypes: return [arg] |
|
1163 | if type(arg) in StringTypes: return [arg] | |
1164 | else: return arg |
|
1164 | else: return arg | |
1165 |
|
1165 | |||
1166 | #---------------------------------------------------------------------------- |
|
1166 | #---------------------------------------------------------------------------- | |
1167 | def grep(pat,list,case=1): |
|
1167 | def grep(pat,list,case=1): | |
1168 | """Simple minded grep-like function. |
|
1168 | """Simple minded grep-like function. | |
1169 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1169 | grep(pat,list) returns occurrences of pat in list, None on failure. | |
1170 |
|
1170 | |||
1171 | It only does simple string matching, with no support for regexps. Use the |
|
1171 | It only does simple string matching, with no support for regexps. Use the | |
1172 | option case=0 for case-insensitive matching.""" |
|
1172 | option case=0 for case-insensitive matching.""" | |
1173 |
|
1173 | |||
1174 | # This is pretty crude. At least it should implement copying only references |
|
1174 | # This is pretty crude. At least it should implement copying only references | |
1175 | # to the original data in case it's big. Now it copies the data for output. |
|
1175 | # to the original data in case it's big. Now it copies the data for output. | |
1176 | out=[] |
|
1176 | out=[] | |
1177 | if case: |
|
1177 | if case: | |
1178 | for term in list: |
|
1178 | for term in list: | |
1179 | if term.find(pat)>-1: out.append(term) |
|
1179 | if term.find(pat)>-1: out.append(term) | |
1180 | else: |
|
1180 | else: | |
1181 | lpat=pat.lower() |
|
1181 | lpat=pat.lower() | |
1182 | for term in list: |
|
1182 | for term in list: | |
1183 | if term.lower().find(lpat)>-1: out.append(term) |
|
1183 | if term.lower().find(lpat)>-1: out.append(term) | |
1184 |
|
1184 | |||
1185 | if len(out): return out |
|
1185 | if len(out): return out | |
1186 | else: return None |
|
1186 | else: return None | |
1187 |
|
1187 | |||
1188 | #---------------------------------------------------------------------------- |
|
1188 | #---------------------------------------------------------------------------- | |
1189 | def dgrep(pat,*opts): |
|
1189 | def dgrep(pat,*opts): | |
1190 | """Return grep() on dir()+dir(__builtins__). |
|
1190 | """Return grep() on dir()+dir(__builtins__). | |
1191 |
|
1191 | |||
1192 | A very common use of grep() when working interactively.""" |
|
1192 | A very common use of grep() when working interactively.""" | |
1193 |
|
1193 | |||
1194 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1194 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) | |
1195 |
|
1195 | |||
1196 | #---------------------------------------------------------------------------- |
|
1196 | #---------------------------------------------------------------------------- | |
1197 | def idgrep(pat): |
|
1197 | def idgrep(pat): | |
1198 | """Case-insensitive dgrep()""" |
|
1198 | """Case-insensitive dgrep()""" | |
1199 |
|
1199 | |||
1200 | return dgrep(pat,0) |
|
1200 | return dgrep(pat,0) | |
1201 |
|
1201 | |||
1202 | #---------------------------------------------------------------------------- |
|
1202 | #---------------------------------------------------------------------------- | |
1203 | def igrep(pat,list): |
|
1203 | def igrep(pat,list): | |
1204 | """Synonym for case-insensitive grep.""" |
|
1204 | """Synonym for case-insensitive grep.""" | |
1205 |
|
1205 | |||
1206 | return grep(pat,list,case=0) |
|
1206 | return grep(pat,list,case=0) | |
1207 |
|
1207 | |||
1208 | #---------------------------------------------------------------------------- |
|
1208 | #---------------------------------------------------------------------------- | |
1209 | def indent(str,nspaces=4,ntabs=0): |
|
1209 | def indent(str,nspaces=4,ntabs=0): | |
1210 | """Indent a string a given number of spaces or tabstops. |
|
1210 | """Indent a string a given number of spaces or tabstops. | |
1211 |
|
1211 | |||
1212 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1212 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. | |
1213 | """ |
|
1213 | """ | |
1214 | if str is None: |
|
1214 | if str is None: | |
1215 | return |
|
1215 | return | |
1216 | ind = '\t'*ntabs+' '*nspaces |
|
1216 | ind = '\t'*ntabs+' '*nspaces | |
1217 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1217 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) | |
1218 | if outstr.endswith(os.linesep+ind): |
|
1218 | if outstr.endswith(os.linesep+ind): | |
1219 | return outstr[:-len(ind)] |
|
1219 | return outstr[:-len(ind)] | |
1220 | else: |
|
1220 | else: | |
1221 | return outstr |
|
1221 | return outstr | |
1222 |
|
1222 | |||
1223 | #----------------------------------------------------------------------------- |
|
1223 | #----------------------------------------------------------------------------- | |
1224 | def native_line_ends(filename,backup=1): |
|
1224 | def native_line_ends(filename,backup=1): | |
1225 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1225 | """Convert (in-place) a file to line-ends native to the current OS. | |
1226 |
|
1226 | |||
1227 | If the optional backup argument is given as false, no backup of the |
|
1227 | If the optional backup argument is given as false, no backup of the | |
1228 | original file is left. """ |
|
1228 | original file is left. """ | |
1229 |
|
1229 | |||
1230 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1230 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} | |
1231 |
|
1231 | |||
1232 | bak_filename = filename + backup_suffixes[os.name] |
|
1232 | bak_filename = filename + backup_suffixes[os.name] | |
1233 |
|
1233 | |||
1234 | original = open(filename).read() |
|
1234 | original = open(filename).read() | |
1235 | shutil.copy2(filename,bak_filename) |
|
1235 | shutil.copy2(filename,bak_filename) | |
1236 | try: |
|
1236 | try: | |
1237 | new = open(filename,'wb') |
|
1237 | new = open(filename,'wb') | |
1238 | new.write(os.linesep.join(original.splitlines())) |
|
1238 | new.write(os.linesep.join(original.splitlines())) | |
1239 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1239 | new.write(os.linesep) # ALWAYS put an eol at the end of the file | |
1240 | new.close() |
|
1240 | new.close() | |
1241 | except: |
|
1241 | except: | |
1242 | os.rename(bak_filename,filename) |
|
1242 | os.rename(bak_filename,filename) | |
1243 | if not backup: |
|
1243 | if not backup: | |
1244 | try: |
|
1244 | try: | |
1245 | os.remove(bak_filename) |
|
1245 | os.remove(bak_filename) | |
1246 | except: |
|
1246 | except: | |
1247 | pass |
|
1247 | pass | |
1248 |
|
1248 | |||
1249 | #---------------------------------------------------------------------------- |
|
1249 | #---------------------------------------------------------------------------- | |
1250 | def get_pager_cmd(pager_cmd = None): |
|
1250 | def get_pager_cmd(pager_cmd = None): | |
1251 | """Return a pager command. |
|
1251 | """Return a pager command. | |
1252 |
|
1252 | |||
1253 | Makes some attempts at finding an OS-correct one.""" |
|
1253 | Makes some attempts at finding an OS-correct one.""" | |
1254 |
|
1254 | |||
1255 | if os.name == 'posix': |
|
1255 | if os.name == 'posix': | |
1256 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1256 | default_pager_cmd = 'less -r' # -r for color control sequences | |
1257 | elif os.name in ['nt','dos']: |
|
1257 | elif os.name in ['nt','dos']: | |
1258 | default_pager_cmd = 'type' |
|
1258 | default_pager_cmd = 'type' | |
1259 |
|
1259 | |||
1260 | if pager_cmd is None: |
|
1260 | if pager_cmd is None: | |
1261 | try: |
|
1261 | try: | |
1262 | pager_cmd = os.environ['PAGER'] |
|
1262 | pager_cmd = os.environ['PAGER'] | |
1263 | except: |
|
1263 | except: | |
1264 | pager_cmd = default_pager_cmd |
|
1264 | pager_cmd = default_pager_cmd | |
1265 | return pager_cmd |
|
1265 | return pager_cmd | |
1266 |
|
1266 | |||
1267 | #----------------------------------------------------------------------------- |
|
1267 | #----------------------------------------------------------------------------- | |
1268 | def get_pager_start(pager,start): |
|
1268 | def get_pager_start(pager,start): | |
1269 | """Return the string for paging files with an offset. |
|
1269 | """Return the string for paging files with an offset. | |
1270 |
|
1270 | |||
1271 | This is the '+N' argument which less and more (under Unix) accept. |
|
1271 | This is the '+N' argument which less and more (under Unix) accept. | |
1272 | """ |
|
1272 | """ | |
1273 |
|
1273 | |||
1274 | if pager in ['less','more']: |
|
1274 | if pager in ['less','more']: | |
1275 | if start: |
|
1275 | if start: | |
1276 | start_string = '+' + str(start) |
|
1276 | start_string = '+' + str(start) | |
1277 | else: |
|
1277 | else: | |
1278 | start_string = '' |
|
1278 | start_string = '' | |
1279 | else: |
|
1279 | else: | |
1280 | start_string = '' |
|
1280 | start_string = '' | |
1281 | return start_string |
|
1281 | return start_string | |
1282 |
|
1282 | |||
1283 | #---------------------------------------------------------------------------- |
|
1283 | #---------------------------------------------------------------------------- | |
1284 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() |
|
1284 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() | |
1285 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
1285 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': | |
1286 | import msvcrt |
|
1286 | import msvcrt | |
1287 | def page_more(): |
|
1287 | def page_more(): | |
1288 | """ Smart pausing between pages |
|
1288 | """ Smart pausing between pages | |
1289 |
|
1289 | |||
1290 | @return: True if need print more lines, False if quit |
|
1290 | @return: True if need print more lines, False if quit | |
1291 | """ |
|
1291 | """ | |
1292 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1292 | Term.cout.write('---Return to continue, q to quit--- ') | |
1293 | ans = msvcrt.getch() |
|
1293 | ans = msvcrt.getch() | |
1294 | if ans in ("q", "Q"): |
|
1294 | if ans in ("q", "Q"): | |
1295 | result = False |
|
1295 | result = False | |
1296 | else: |
|
1296 | else: | |
1297 | result = True |
|
1297 | result = True | |
1298 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1298 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) | |
1299 | return result |
|
1299 | return result | |
1300 | else: |
|
1300 | else: | |
1301 | def page_more(): |
|
1301 | def page_more(): | |
1302 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1302 | ans = raw_input('---Return to continue, q to quit--- ') | |
1303 | if ans.lower().startswith('q'): |
|
1303 | if ans.lower().startswith('q'): | |
1304 | return False |
|
1304 | return False | |
1305 | else: |
|
1305 | else: | |
1306 | return True |
|
1306 | return True | |
1307 |
|
1307 | |||
1308 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1308 | esc_re = re.compile(r"(\x1b[^m]+m)") | |
1309 |
|
1309 | |||
1310 | def page_dumb(strng,start=0,screen_lines=25): |
|
1310 | def page_dumb(strng,start=0,screen_lines=25): | |
1311 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1311 | """Very dumb 'pager' in Python, for when nothing else works. | |
1312 |
|
1312 | |||
1313 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1313 | Only moves forward, same interface as page(), except for pager_cmd and | |
1314 | mode.""" |
|
1314 | mode.""" | |
1315 |
|
1315 | |||
1316 | out_ln = strng.splitlines()[start:] |
|
1316 | out_ln = strng.splitlines()[start:] | |
1317 | screens = chop(out_ln,screen_lines-1) |
|
1317 | screens = chop(out_ln,screen_lines-1) | |
1318 | if len(screens) == 1: |
|
1318 | if len(screens) == 1: | |
1319 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1319 | print >>Term.cout, os.linesep.join(screens[0]) | |
1320 | else: |
|
1320 | else: | |
1321 | last_escape = "" |
|
1321 | last_escape = "" | |
1322 | for scr in screens[0:-1]: |
|
1322 | for scr in screens[0:-1]: | |
1323 | hunk = os.linesep.join(scr) |
|
1323 | hunk = os.linesep.join(scr) | |
1324 | print >>Term.cout, last_escape + hunk |
|
1324 | print >>Term.cout, last_escape + hunk | |
1325 | if not page_more(): |
|
1325 | if not page_more(): | |
1326 | return |
|
1326 | return | |
1327 | esc_list = esc_re.findall(hunk) |
|
1327 | esc_list = esc_re.findall(hunk) | |
1328 | if len(esc_list) > 0: |
|
1328 | if len(esc_list) > 0: | |
1329 | last_escape = esc_list[-1] |
|
1329 | last_escape = esc_list[-1] | |
1330 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1330 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) | |
1331 |
|
1331 | |||
1332 | #---------------------------------------------------------------------------- |
|
1332 | #---------------------------------------------------------------------------- | |
1333 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1333 | def page(strng,start=0,screen_lines=0,pager_cmd = None): | |
1334 | """Print a string, piping through a pager after a certain length. |
|
1334 | """Print a string, piping through a pager after a certain length. | |
1335 |
|
1335 | |||
1336 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1336 | The screen_lines parameter specifies the number of *usable* lines of your | |
1337 | terminal screen (total lines minus lines you need to reserve to show other |
|
1337 | terminal screen (total lines minus lines you need to reserve to show other | |
1338 | information). |
|
1338 | information). | |
1339 |
|
1339 | |||
1340 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1340 | If you set screen_lines to a number <=0, page() will try to auto-determine | |
1341 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1341 | your screen size and will only use up to (screen_size+screen_lines) for | |
1342 | printing, paging after that. That is, if you want auto-detection but need |
|
1342 | printing, paging after that. That is, if you want auto-detection but need | |
1343 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1343 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for | |
1344 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1344 | auto-detection without any lines reserved simply use screen_lines = 0. | |
1345 |
|
1345 | |||
1346 | If a string won't fit in the allowed lines, it is sent through the |
|
1346 | If a string won't fit in the allowed lines, it is sent through the | |
1347 | specified pager command. If none given, look for PAGER in the environment, |
|
1347 | specified pager command. If none given, look for PAGER in the environment, | |
1348 | and ultimately default to less. |
|
1348 | and ultimately default to less. | |
1349 |
|
1349 | |||
1350 | If no system pager works, the string is sent through a 'dumb pager' |
|
1350 | If no system pager works, the string is sent through a 'dumb pager' | |
1351 | written in python, very simplistic. |
|
1351 | written in python, very simplistic. | |
1352 | """ |
|
1352 | """ | |
1353 |
|
1353 | |||
1354 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1354 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs | |
1355 | TERM = os.environ.get('TERM','dumb') |
|
1355 | TERM = os.environ.get('TERM','dumb') | |
1356 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1356 | if TERM in ['dumb','emacs'] and os.name != 'nt': | |
1357 | print strng |
|
1357 | print strng | |
1358 | return |
|
1358 | return | |
1359 | # chop off the topmost part of the string we don't want to see |
|
1359 | # chop off the topmost part of the string we don't want to see | |
1360 | str_lines = strng.split(os.linesep)[start:] |
|
1360 | str_lines = strng.split(os.linesep)[start:] | |
1361 | str_toprint = os.linesep.join(str_lines) |
|
1361 | str_toprint = os.linesep.join(str_lines) | |
1362 | num_newlines = len(str_lines) |
|
1362 | num_newlines = len(str_lines) | |
1363 | len_str = len(str_toprint) |
|
1363 | len_str = len(str_toprint) | |
1364 |
|
1364 | |||
1365 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1365 | # Dumb heuristics to guesstimate number of on-screen lines the string | |
1366 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1366 | # takes. Very basic, but good enough for docstrings in reasonable | |
1367 | # terminals. If someone later feels like refining it, it's not hard. |
|
1367 | # terminals. If someone later feels like refining it, it's not hard. | |
1368 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1368 | numlines = max(num_newlines,int(len_str/80)+1) | |
1369 |
|
1369 | |||
1370 | if os.name == "nt": |
|
1370 | if os.name == "nt": | |
1371 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1371 | screen_lines_def = get_console_size(defaulty=25)[1] | |
1372 | else: |
|
1372 | else: | |
1373 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1373 | screen_lines_def = 25 # default value if we can't auto-determine | |
1374 |
|
1374 | |||
1375 | # auto-determine screen size |
|
1375 | # auto-determine screen size | |
1376 | if screen_lines <= 0: |
|
1376 | if screen_lines <= 0: | |
1377 | if TERM=='xterm': |
|
1377 | if TERM=='xterm': | |
1378 | try: |
|
1378 | try: | |
1379 | import curses |
|
1379 | import curses | |
1380 | if hasattr(curses,'initscr'): |
|
1380 | if hasattr(curses,'initscr'): | |
1381 | use_curses = 1 |
|
1381 | use_curses = 1 | |
1382 | else: |
|
1382 | else: | |
1383 | use_curses = 0 |
|
1383 | use_curses = 0 | |
1384 | except ImportError: |
|
1384 | except ImportError: | |
1385 | use_curses = 0 |
|
1385 | use_curses = 0 | |
1386 | else: |
|
1386 | else: | |
1387 | # curses causes problems on many terminals other than xterm. |
|
1387 | # curses causes problems on many terminals other than xterm. | |
1388 | use_curses = 0 |
|
1388 | use_curses = 0 | |
1389 | if use_curses: |
|
1389 | if use_curses: | |
1390 | scr = curses.initscr() |
|
1390 | scr = curses.initscr() | |
1391 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1391 | screen_lines_real,screen_cols = scr.getmaxyx() | |
1392 | curses.endwin() |
|
1392 | curses.endwin() | |
1393 | screen_lines += screen_lines_real |
|
1393 | screen_lines += screen_lines_real | |
1394 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1394 | #print '***Screen size:',screen_lines_real,'lines x',\ | |
1395 | #screen_cols,'columns.' # dbg |
|
1395 | #screen_cols,'columns.' # dbg | |
1396 | else: |
|
1396 | else: | |
1397 | screen_lines += screen_lines_def |
|
1397 | screen_lines += screen_lines_def | |
1398 |
|
1398 | |||
1399 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1399 | #print 'numlines',numlines,'screenlines',screen_lines # dbg | |
1400 | if numlines <= screen_lines : |
|
1400 | if numlines <= screen_lines : | |
1401 | #print '*** normal print' # dbg |
|
1401 | #print '*** normal print' # dbg | |
1402 | print >>Term.cout, str_toprint |
|
1402 | print >>Term.cout, str_toprint | |
1403 | else: |
|
1403 | else: | |
1404 | # Try to open pager and default to internal one if that fails. |
|
1404 | # Try to open pager and default to internal one if that fails. | |
1405 | # All failure modes are tagged as 'retval=1', to match the return |
|
1405 | # All failure modes are tagged as 'retval=1', to match the return | |
1406 | # value of a failed system command. If any intermediate attempt |
|
1406 | # value of a failed system command. If any intermediate attempt | |
1407 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1407 | # sets retval to 1, at the end we resort to our own page_dumb() pager. | |
1408 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1408 | pager_cmd = get_pager_cmd(pager_cmd) | |
1409 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1409 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1410 | if os.name == 'nt': |
|
1410 | if os.name == 'nt': | |
1411 | if pager_cmd.startswith('type'): |
|
1411 | if pager_cmd.startswith('type'): | |
1412 | # The default WinXP 'type' command is failing on complex strings. |
|
1412 | # The default WinXP 'type' command is failing on complex strings. | |
1413 | retval = 1 |
|
1413 | retval = 1 | |
1414 | else: |
|
1414 | else: | |
1415 | tmpname = tempfile.mktemp('.txt') |
|
1415 | tmpname = tempfile.mktemp('.txt') | |
1416 | tmpfile = file(tmpname,'wt') |
|
1416 | tmpfile = file(tmpname,'wt') | |
1417 | tmpfile.write(strng) |
|
1417 | tmpfile.write(strng) | |
1418 | tmpfile.close() |
|
1418 | tmpfile.close() | |
1419 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1419 | cmd = "%s < %s" % (pager_cmd,tmpname) | |
1420 | if os.system(cmd): |
|
1420 | if os.system(cmd): | |
1421 | retval = 1 |
|
1421 | retval = 1 | |
1422 | else: |
|
1422 | else: | |
1423 | retval = None |
|
1423 | retval = None | |
1424 | os.remove(tmpname) |
|
1424 | os.remove(tmpname) | |
1425 | else: |
|
1425 | else: | |
1426 | try: |
|
1426 | try: | |
1427 | retval = None |
|
1427 | retval = None | |
1428 | # if I use popen4, things hang. No idea why. |
|
1428 | # if I use popen4, things hang. No idea why. | |
1429 | #pager,shell_out = os.popen4(pager_cmd) |
|
1429 | #pager,shell_out = os.popen4(pager_cmd) | |
1430 | pager = os.popen(pager_cmd,'w') |
|
1430 | pager = os.popen(pager_cmd,'w') | |
1431 | pager.write(strng) |
|
1431 | pager.write(strng) | |
1432 | pager.close() |
|
1432 | pager.close() | |
1433 | retval = pager.close() # success returns None |
|
1433 | retval = pager.close() # success returns None | |
1434 | except IOError,msg: # broken pipe when user quits |
|
1434 | except IOError,msg: # broken pipe when user quits | |
1435 | if msg.args == (32,'Broken pipe'): |
|
1435 | if msg.args == (32,'Broken pipe'): | |
1436 | retval = None |
|
1436 | retval = None | |
1437 | else: |
|
1437 | else: | |
1438 | retval = 1 |
|
1438 | retval = 1 | |
1439 | except OSError: |
|
1439 | except OSError: | |
1440 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1440 | # Other strange problems, sometimes seen in Win2k/cygwin | |
1441 | retval = 1 |
|
1441 | retval = 1 | |
1442 | if retval is not None: |
|
1442 | if retval is not None: | |
1443 | page_dumb(strng,screen_lines=screen_lines) |
|
1443 | page_dumb(strng,screen_lines=screen_lines) | |
1444 |
|
1444 | |||
1445 | #---------------------------------------------------------------------------- |
|
1445 | #---------------------------------------------------------------------------- | |
1446 | def page_file(fname,start = 0, pager_cmd = None): |
|
1446 | def page_file(fname,start = 0, pager_cmd = None): | |
1447 | """Page a file, using an optional pager command and starting line. |
|
1447 | """Page a file, using an optional pager command and starting line. | |
1448 | """ |
|
1448 | """ | |
1449 |
|
1449 | |||
1450 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1450 | pager_cmd = get_pager_cmd(pager_cmd) | |
1451 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1451 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1452 |
|
1452 | |||
1453 | try: |
|
1453 | try: | |
1454 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1454 | if os.environ['TERM'] in ['emacs','dumb']: | |
1455 | raise EnvironmentError |
|
1455 | raise EnvironmentError | |
1456 | xsys(pager_cmd + ' ' + fname) |
|
1456 | xsys(pager_cmd + ' ' + fname) | |
1457 | except: |
|
1457 | except: | |
1458 | try: |
|
1458 | try: | |
1459 | if start > 0: |
|
1459 | if start > 0: | |
1460 | start -= 1 |
|
1460 | start -= 1 | |
1461 | page(open(fname).read(),start) |
|
1461 | page(open(fname).read(),start) | |
1462 | except: |
|
1462 | except: | |
1463 | print 'Unable to show file',`fname` |
|
1463 | print 'Unable to show file',`fname` | |
1464 |
|
1464 | |||
1465 | #---------------------------------------------------------------------------- |
|
1465 | #---------------------------------------------------------------------------- | |
1466 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1466 | def snip_print(str,width = 75,print_full = 0,header = ''): | |
1467 | """Print a string snipping the midsection to fit in width. |
|
1467 | """Print a string snipping the midsection to fit in width. | |
1468 |
|
1468 | |||
1469 | print_full: mode control: |
|
1469 | print_full: mode control: | |
1470 | - 0: only snip long strings |
|
1470 | - 0: only snip long strings | |
1471 | - 1: send to page() directly. |
|
1471 | - 1: send to page() directly. | |
1472 | - 2: snip long strings and ask for full length viewing with page() |
|
1472 | - 2: snip long strings and ask for full length viewing with page() | |
1473 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1473 | Return 1 if snipping was necessary, 0 otherwise.""" | |
1474 |
|
1474 | |||
1475 | if print_full == 1: |
|
1475 | if print_full == 1: | |
1476 | page(header+str) |
|
1476 | page(header+str) | |
1477 | return 0 |
|
1477 | return 0 | |
1478 |
|
1478 | |||
1479 | print header, |
|
1479 | print header, | |
1480 | if len(str) < width: |
|
1480 | if len(str) < width: | |
1481 | print str |
|
1481 | print str | |
1482 | snip = 0 |
|
1482 | snip = 0 | |
1483 | else: |
|
1483 | else: | |
1484 | whalf = int((width -5)/2) |
|
1484 | whalf = int((width -5)/2) | |
1485 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1485 | print str[:whalf] + ' <...> ' + str[-whalf:] | |
1486 | snip = 1 |
|
1486 | snip = 1 | |
1487 | if snip and print_full == 2: |
|
1487 | if snip and print_full == 2: | |
1488 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1488 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': | |
1489 | page(str) |
|
1489 | page(str) | |
1490 | return snip |
|
1490 | return snip | |
1491 |
|
1491 | |||
1492 | #**************************************************************************** |
|
1492 | #**************************************************************************** | |
1493 | # lists, dicts and structures |
|
1493 | # lists, dicts and structures | |
1494 |
|
1494 | |||
1495 | def belong(candidates,checklist): |
|
1495 | def belong(candidates,checklist): | |
1496 | """Check whether a list of items appear in a given list of options. |
|
1496 | """Check whether a list of items appear in a given list of options. | |
1497 |
|
1497 | |||
1498 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1498 | Returns a list of 1 and 0, one for each candidate given.""" | |
1499 |
|
1499 | |||
1500 | return [x in checklist for x in candidates] |
|
1500 | return [x in checklist for x in candidates] | |
1501 |
|
1501 | |||
1502 | #---------------------------------------------------------------------------- |
|
1502 | #---------------------------------------------------------------------------- | |
1503 | def uniq_stable(elems): |
|
1503 | def uniq_stable(elems): | |
1504 | """uniq_stable(elems) -> list |
|
1504 | """uniq_stable(elems) -> list | |
1505 |
|
1505 | |||
1506 | Return from an iterable, a list of all the unique elements in the input, |
|
1506 | Return from an iterable, a list of all the unique elements in the input, | |
1507 | but maintaining the order in which they first appear. |
|
1507 | but maintaining the order in which they first appear. | |
1508 |
|
1508 | |||
1509 | A naive solution to this problem which just makes a dictionary with the |
|
1509 | A naive solution to this problem which just makes a dictionary with the | |
1510 | elements as keys fails to respect the stability condition, since |
|
1510 | elements as keys fails to respect the stability condition, since | |
1511 | dictionaries are unsorted by nature. |
|
1511 | dictionaries are unsorted by nature. | |
1512 |
|
1512 | |||
1513 | Note: All elements in the input must be valid dictionary keys for this |
|
1513 | Note: All elements in the input must be valid dictionary keys for this | |
1514 | routine to work, as it internally uses a dictionary for efficiency |
|
1514 | routine to work, as it internally uses a dictionary for efficiency | |
1515 | reasons.""" |
|
1515 | reasons.""" | |
1516 |
|
1516 | |||
1517 | unique = [] |
|
1517 | unique = [] | |
1518 | unique_dict = {} |
|
1518 | unique_dict = {} | |
1519 | for nn in elems: |
|
1519 | for nn in elems: | |
1520 | if nn not in unique_dict: |
|
1520 | if nn not in unique_dict: | |
1521 | unique.append(nn) |
|
1521 | unique.append(nn) | |
1522 | unique_dict[nn] = None |
|
1522 | unique_dict[nn] = None | |
1523 | return unique |
|
1523 | return unique | |
1524 |
|
1524 | |||
1525 | #---------------------------------------------------------------------------- |
|
1525 | #---------------------------------------------------------------------------- | |
1526 | class NLprinter: |
|
1526 | class NLprinter: | |
1527 | """Print an arbitrarily nested list, indicating index numbers. |
|
1527 | """Print an arbitrarily nested list, indicating index numbers. | |
1528 |
|
1528 | |||
1529 | An instance of this class called nlprint is available and callable as a |
|
1529 | An instance of this class called nlprint is available and callable as a | |
1530 | function. |
|
1530 | function. | |
1531 |
|
1531 | |||
1532 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1532 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' | |
1533 | and using 'sep' to separate the index from the value. """ |
|
1533 | and using 'sep' to separate the index from the value. """ | |
1534 |
|
1534 | |||
1535 | def __init__(self): |
|
1535 | def __init__(self): | |
1536 | self.depth = 0 |
|
1536 | self.depth = 0 | |
1537 |
|
1537 | |||
1538 | def __call__(self,lst,pos='',**kw): |
|
1538 | def __call__(self,lst,pos='',**kw): | |
1539 | """Prints the nested list numbering levels.""" |
|
1539 | """Prints the nested list numbering levels.""" | |
1540 | kw.setdefault('indent',' ') |
|
1540 | kw.setdefault('indent',' ') | |
1541 | kw.setdefault('sep',': ') |
|
1541 | kw.setdefault('sep',': ') | |
1542 | kw.setdefault('start',0) |
|
1542 | kw.setdefault('start',0) | |
1543 | kw.setdefault('stop',len(lst)) |
|
1543 | kw.setdefault('stop',len(lst)) | |
1544 | # we need to remove start and stop from kw so they don't propagate |
|
1544 | # we need to remove start and stop from kw so they don't propagate | |
1545 | # into a recursive call for a nested list. |
|
1545 | # into a recursive call for a nested list. | |
1546 | start = kw['start']; del kw['start'] |
|
1546 | start = kw['start']; del kw['start'] | |
1547 | stop = kw['stop']; del kw['stop'] |
|
1547 | stop = kw['stop']; del kw['stop'] | |
1548 | if self.depth == 0 and 'header' in kw.keys(): |
|
1548 | if self.depth == 0 and 'header' in kw.keys(): | |
1549 | print kw['header'] |
|
1549 | print kw['header'] | |
1550 |
|
1550 | |||
1551 | for idx in range(start,stop): |
|
1551 | for idx in range(start,stop): | |
1552 | elem = lst[idx] |
|
1552 | elem = lst[idx] | |
1553 | if type(elem)==type([]): |
|
1553 | if type(elem)==type([]): | |
1554 | self.depth += 1 |
|
1554 | self.depth += 1 | |
1555 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1555 | self.__call__(elem,itpl('$pos$idx,'),**kw) | |
1556 | self.depth -= 1 |
|
1556 | self.depth -= 1 | |
1557 | else: |
|
1557 | else: | |
1558 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1558 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') | |
1559 |
|
1559 | |||
1560 | nlprint = NLprinter() |
|
1560 | nlprint = NLprinter() | |
1561 | #---------------------------------------------------------------------------- |
|
1561 | #---------------------------------------------------------------------------- | |
1562 | def all_belong(candidates,checklist): |
|
1562 | def all_belong(candidates,checklist): | |
1563 | """Check whether a list of items ALL appear in a given list of options. |
|
1563 | """Check whether a list of items ALL appear in a given list of options. | |
1564 |
|
1564 | |||
1565 | Returns a single 1 or 0 value.""" |
|
1565 | Returns a single 1 or 0 value.""" | |
1566 |
|
1566 | |||
1567 | return 1-(0 in [x in checklist for x in candidates]) |
|
1567 | return 1-(0 in [x in checklist for x in candidates]) | |
1568 |
|
1568 | |||
1569 | #---------------------------------------------------------------------------- |
|
1569 | #---------------------------------------------------------------------------- | |
1570 | def sort_compare(lst1,lst2,inplace = 1): |
|
1570 | def sort_compare(lst1,lst2,inplace = 1): | |
1571 | """Sort and compare two lists. |
|
1571 | """Sort and compare two lists. | |
1572 |
|
1572 | |||
1573 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1573 | By default it does it in place, thus modifying the lists. Use inplace = 0 | |
1574 | to avoid that (at the cost of temporary copy creation).""" |
|
1574 | to avoid that (at the cost of temporary copy creation).""" | |
1575 | if not inplace: |
|
1575 | if not inplace: | |
1576 | lst1 = lst1[:] |
|
1576 | lst1 = lst1[:] | |
1577 | lst2 = lst2[:] |
|
1577 | lst2 = lst2[:] | |
1578 | lst1.sort(); lst2.sort() |
|
1578 | lst1.sort(); lst2.sort() | |
1579 | return lst1 == lst2 |
|
1579 | return lst1 == lst2 | |
1580 |
|
1580 | |||
1581 | #---------------------------------------------------------------------------- |
|
1581 | #---------------------------------------------------------------------------- | |
1582 | def mkdict(**kwargs): |
|
1582 | def mkdict(**kwargs): | |
1583 | """Return a dict from a keyword list. |
|
1583 | """Return a dict from a keyword list. | |
1584 |
|
1584 | |||
1585 | It's just syntactic sugar for making ditcionary creation more convenient: |
|
1585 | It's just syntactic sugar for making ditcionary creation more convenient: | |
1586 | # the standard way |
|
1586 | # the standard way | |
1587 | >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 } |
|
1587 | >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 } | |
1588 | # a cleaner way |
|
1588 | # a cleaner way | |
1589 | >>>data = dict(red=1, green=2, blue=3) |
|
1589 | >>>data = dict(red=1, green=2, blue=3) | |
1590 |
|
1590 | |||
1591 | If you need more than this, look at the Struct() class.""" |
|
1591 | If you need more than this, look at the Struct() class.""" | |
1592 |
|
1592 | |||
1593 | return kwargs |
|
1593 | return kwargs | |
1594 |
|
1594 | |||
1595 | #---------------------------------------------------------------------------- |
|
1595 | #---------------------------------------------------------------------------- | |
1596 | def list2dict(lst): |
|
1596 | def list2dict(lst): | |
1597 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1597 | """Takes a list of (key,value) pairs and turns it into a dict.""" | |
1598 |
|
1598 | |||
1599 | dic = {} |
|
1599 | dic = {} | |
1600 | for k,v in lst: dic[k] = v |
|
1600 | for k,v in lst: dic[k] = v | |
1601 | return dic |
|
1601 | return dic | |
1602 |
|
1602 | |||
1603 | #---------------------------------------------------------------------------- |
|
1603 | #---------------------------------------------------------------------------- | |
1604 | def list2dict2(lst,default=''): |
|
1604 | def list2dict2(lst,default=''): | |
1605 | """Takes a list and turns it into a dict. |
|
1605 | """Takes a list and turns it into a dict. | |
1606 | Much slower than list2dict, but more versatile. This version can take |
|
1606 | Much slower than list2dict, but more versatile. This version can take | |
1607 | lists with sublists of arbitrary length (including sclars).""" |
|
1607 | lists with sublists of arbitrary length (including sclars).""" | |
1608 |
|
1608 | |||
1609 | dic = {} |
|
1609 | dic = {} | |
1610 | for elem in lst: |
|
1610 | for elem in lst: | |
1611 | if type(elem) in (types.ListType,types.TupleType): |
|
1611 | if type(elem) in (types.ListType,types.TupleType): | |
1612 | size = len(elem) |
|
1612 | size = len(elem) | |
1613 | if size == 0: |
|
1613 | if size == 0: | |
1614 | pass |
|
1614 | pass | |
1615 | elif size == 1: |
|
1615 | elif size == 1: | |
1616 | dic[elem] = default |
|
1616 | dic[elem] = default | |
1617 | else: |
|
1617 | else: | |
1618 | k,v = elem[0], elem[1:] |
|
1618 | k,v = elem[0], elem[1:] | |
1619 | if len(v) == 1: v = v[0] |
|
1619 | if len(v) == 1: v = v[0] | |
1620 | dic[k] = v |
|
1620 | dic[k] = v | |
1621 | else: |
|
1621 | else: | |
1622 | dic[elem] = default |
|
1622 | dic[elem] = default | |
1623 | return dic |
|
1623 | return dic | |
1624 |
|
1624 | |||
1625 | #---------------------------------------------------------------------------- |
|
1625 | #---------------------------------------------------------------------------- | |
1626 | def flatten(seq): |
|
1626 | def flatten(seq): | |
1627 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1627 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" | |
1628 |
|
1628 | |||
1629 | return [x for subseq in seq for x in subseq] |
|
1629 | return [x for subseq in seq for x in subseq] | |
1630 |
|
1630 | |||
1631 | #---------------------------------------------------------------------------- |
|
1631 | #---------------------------------------------------------------------------- | |
1632 | def get_slice(seq,start=0,stop=None,step=1): |
|
1632 | def get_slice(seq,start=0,stop=None,step=1): | |
1633 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1633 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" | |
1634 | if stop == None: |
|
1634 | if stop == None: | |
1635 | stop = len(seq) |
|
1635 | stop = len(seq) | |
1636 | item = lambda i: seq[i] |
|
1636 | item = lambda i: seq[i] | |
1637 | return map(item,xrange(start,stop,step)) |
|
1637 | return map(item,xrange(start,stop,step)) | |
1638 |
|
1638 | |||
1639 | #---------------------------------------------------------------------------- |
|
1639 | #---------------------------------------------------------------------------- | |
1640 | def chop(seq,size): |
|
1640 | def chop(seq,size): | |
1641 | """Chop a sequence into chunks of the given size.""" |
|
1641 | """Chop a sequence into chunks of the given size.""" | |
1642 | chunk = lambda i: seq[i:i+size] |
|
1642 | chunk = lambda i: seq[i:i+size] | |
1643 | return map(chunk,xrange(0,len(seq),size)) |
|
1643 | return map(chunk,xrange(0,len(seq),size)) | |
1644 |
|
1644 | |||
1645 | #---------------------------------------------------------------------------- |
|
1645 | #---------------------------------------------------------------------------- | |
1646 | # with is a keyword as of python 2.5, so this function is renamed to withobj |
|
1646 | # with is a keyword as of python 2.5, so this function is renamed to withobj | |
1647 | # from its old 'with' name. |
|
1647 | # from its old 'with' name. | |
1648 | def with_obj(object, **args): |
|
1648 | def with_obj(object, **args): | |
1649 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1649 | """Set multiple attributes for an object, similar to Pascal's with. | |
1650 |
|
1650 | |||
1651 | Example: |
|
1651 | Example: | |
1652 | with_obj(jim, |
|
1652 | with_obj(jim, | |
1653 | born = 1960, |
|
1653 | born = 1960, | |
1654 | haircolour = 'Brown', |
|
1654 | haircolour = 'Brown', | |
1655 | eyecolour = 'Green') |
|
1655 | eyecolour = 'Green') | |
1656 |
|
1656 | |||
1657 | Credit: Greg Ewing, in |
|
1657 | Credit: Greg Ewing, in | |
1658 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. |
|
1658 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. | |
1659 |
|
1659 | |||
1660 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' |
|
1660 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' | |
1661 | has become a keyword for Python 2.5, so we had to rename it.""" |
|
1661 | has become a keyword for Python 2.5, so we had to rename it.""" | |
1662 |
|
1662 | |||
1663 | object.__dict__.update(args) |
|
1663 | object.__dict__.update(args) | |
1664 |
|
1664 | |||
1665 | #---------------------------------------------------------------------------- |
|
1665 | #---------------------------------------------------------------------------- | |
1666 | def setattr_list(obj,alist,nspace = None): |
|
1666 | def setattr_list(obj,alist,nspace = None): | |
1667 | """Set a list of attributes for an object taken from a namespace. |
|
1667 | """Set a list of attributes for an object taken from a namespace. | |
1668 |
|
1668 | |||
1669 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1669 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in | |
1670 | alist with their values taken from nspace, which must be a dict (something |
|
1670 | alist with their values taken from nspace, which must be a dict (something | |
1671 | like locals() will often do) If nspace isn't given, locals() of the |
|
1671 | like locals() will often do) If nspace isn't given, locals() of the | |
1672 | *caller* is used, so in most cases you can omit it. |
|
1672 | *caller* is used, so in most cases you can omit it. | |
1673 |
|
1673 | |||
1674 | Note that alist can be given as a string, which will be automatically |
|
1674 | Note that alist can be given as a string, which will be automatically | |
1675 | split into a list on whitespace. If given as a list, it must be a list of |
|
1675 | split into a list on whitespace. If given as a list, it must be a list of | |
1676 | *strings* (the variable names themselves), not of variables.""" |
|
1676 | *strings* (the variable names themselves), not of variables.""" | |
1677 |
|
1677 | |||
1678 | # this grabs the local variables from the *previous* call frame -- that is |
|
1678 | # this grabs the local variables from the *previous* call frame -- that is | |
1679 | # the locals from the function that called setattr_list(). |
|
1679 | # the locals from the function that called setattr_list(). | |
1680 | # - snipped from weave.inline() |
|
1680 | # - snipped from weave.inline() | |
1681 | if nspace is None: |
|
1681 | if nspace is None: | |
1682 | call_frame = sys._getframe().f_back |
|
1682 | call_frame = sys._getframe().f_back | |
1683 | nspace = call_frame.f_locals |
|
1683 | nspace = call_frame.f_locals | |
1684 |
|
1684 | |||
1685 | if type(alist) in StringTypes: |
|
1685 | if type(alist) in StringTypes: | |
1686 | alist = alist.split() |
|
1686 | alist = alist.split() | |
1687 | for attr in alist: |
|
1687 | for attr in alist: | |
1688 | val = eval(attr,nspace) |
|
1688 | val = eval(attr,nspace) | |
1689 | setattr(obj,attr,val) |
|
1689 | setattr(obj,attr,val) | |
1690 |
|
1690 | |||
1691 | #---------------------------------------------------------------------------- |
|
1691 | #---------------------------------------------------------------------------- | |
1692 | def getattr_list(obj,alist,*args): |
|
1692 | def getattr_list(obj,alist,*args): | |
1693 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1693 | """getattr_list(obj,alist[, default]) -> attribute list. | |
1694 |
|
1694 | |||
1695 | Get a list of named attributes for an object. When a default argument is |
|
1695 | Get a list of named attributes for an object. When a default argument is | |
1696 | given, it is returned when the attribute doesn't exist; without it, an |
|
1696 | given, it is returned when the attribute doesn't exist; without it, an | |
1697 | exception is raised in that case. |
|
1697 | exception is raised in that case. | |
1698 |
|
1698 | |||
1699 | Note that alist can be given as a string, which will be automatically |
|
1699 | Note that alist can be given as a string, which will be automatically | |
1700 | split into a list on whitespace. If given as a list, it must be a list of |
|
1700 | split into a list on whitespace. If given as a list, it must be a list of | |
1701 | *strings* (the variable names themselves), not of variables.""" |
|
1701 | *strings* (the variable names themselves), not of variables.""" | |
1702 |
|
1702 | |||
1703 | if type(alist) in StringTypes: |
|
1703 | if type(alist) in StringTypes: | |
1704 | alist = alist.split() |
|
1704 | alist = alist.split() | |
1705 | if args: |
|
1705 | if args: | |
1706 | if len(args)==1: |
|
1706 | if len(args)==1: | |
1707 | default = args[0] |
|
1707 | default = args[0] | |
1708 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1708 | return map(lambda attr: getattr(obj,attr,default),alist) | |
1709 | else: |
|
1709 | else: | |
1710 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1710 | raise ValueError,'getattr_list() takes only one optional argument' | |
1711 | else: |
|
1711 | else: | |
1712 | return map(lambda attr: getattr(obj,attr),alist) |
|
1712 | return map(lambda attr: getattr(obj,attr),alist) | |
1713 |
|
1713 | |||
1714 | #---------------------------------------------------------------------------- |
|
1714 | #---------------------------------------------------------------------------- | |
1715 | def map_method(method,object_list,*argseq,**kw): |
|
1715 | def map_method(method,object_list,*argseq,**kw): | |
1716 | """map_method(method,object_list,*args,**kw) -> list |
|
1716 | """map_method(method,object_list,*args,**kw) -> list | |
1717 |
|
1717 | |||
1718 | Return a list of the results of applying the methods to the items of the |
|
1718 | Return a list of the results of applying the methods to the items of the | |
1719 | argument sequence(s). If more than one sequence is given, the method is |
|
1719 | argument sequence(s). If more than one sequence is given, the method is | |
1720 | called with an argument list consisting of the corresponding item of each |
|
1720 | called with an argument list consisting of the corresponding item of each | |
1721 | sequence. All sequences must be of the same length. |
|
1721 | sequence. All sequences must be of the same length. | |
1722 |
|
1722 | |||
1723 | Keyword arguments are passed verbatim to all objects called. |
|
1723 | Keyword arguments are passed verbatim to all objects called. | |
1724 |
|
1724 | |||
1725 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1725 | This is Python code, so it's not nearly as fast as the builtin map().""" | |
1726 |
|
1726 | |||
1727 | out_list = [] |
|
1727 | out_list = [] | |
1728 | idx = 0 |
|
1728 | idx = 0 | |
1729 | for object in object_list: |
|
1729 | for object in object_list: | |
1730 | try: |
|
1730 | try: | |
1731 | handler = getattr(object, method) |
|
1731 | handler = getattr(object, method) | |
1732 | except AttributeError: |
|
1732 | except AttributeError: | |
1733 | out_list.append(None) |
|
1733 | out_list.append(None) | |
1734 | else: |
|
1734 | else: | |
1735 | if argseq: |
|
1735 | if argseq: | |
1736 | args = map(lambda lst:lst[idx],argseq) |
|
1736 | args = map(lambda lst:lst[idx],argseq) | |
1737 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
1737 | #print 'ob',object,'hand',handler,'ar',args # dbg | |
1738 | out_list.append(handler(args,**kw)) |
|
1738 | out_list.append(handler(args,**kw)) | |
1739 | else: |
|
1739 | else: | |
1740 | out_list.append(handler(**kw)) |
|
1740 | out_list.append(handler(**kw)) | |
1741 | idx += 1 |
|
1741 | idx += 1 | |
1742 | return out_list |
|
1742 | return out_list | |
1743 |
|
1743 | |||
1744 | #---------------------------------------------------------------------------- |
|
1744 | #---------------------------------------------------------------------------- | |
1745 | def get_class_members(cls): |
|
1745 | def get_class_members(cls): | |
1746 | ret = dir(cls) |
|
1746 | ret = dir(cls) | |
1747 | if hasattr(cls,'__bases__'): |
|
1747 | if hasattr(cls,'__bases__'): | |
1748 | for base in cls.__bases__: |
|
1748 | for base in cls.__bases__: | |
1749 | ret.extend(get_class_members(base)) |
|
1749 | ret.extend(get_class_members(base)) | |
1750 | return ret |
|
1750 | return ret | |
1751 |
|
1751 | |||
1752 | #---------------------------------------------------------------------------- |
|
1752 | #---------------------------------------------------------------------------- | |
1753 | def dir2(obj): |
|
1753 | def dir2(obj): | |
1754 | """dir2(obj) -> list of strings |
|
1754 | """dir2(obj) -> list of strings | |
1755 |
|
1755 | |||
1756 | Extended version of the Python builtin dir(), which does a few extra |
|
1756 | Extended version of the Python builtin dir(), which does a few extra | |
1757 | checks, and supports common objects with unusual internals that confuse |
|
1757 | checks, and supports common objects with unusual internals that confuse | |
1758 | dir(), such as Traits and PyCrust. |
|
1758 | dir(), such as Traits and PyCrust. | |
1759 |
|
1759 | |||
1760 | This version is guaranteed to return only a list of true strings, whereas |
|
1760 | This version is guaranteed to return only a list of true strings, whereas | |
1761 | dir() returns anything that objects inject into themselves, even if they |
|
1761 | dir() returns anything that objects inject into themselves, even if they | |
1762 | are later not really valid for attribute access (many extension libraries |
|
1762 | are later not really valid for attribute access (many extension libraries | |
1763 | have such bugs). |
|
1763 | have such bugs). | |
1764 | """ |
|
1764 | """ | |
1765 |
|
1765 | |||
1766 | # Start building the attribute list via dir(), and then complete it |
|
1766 | # Start building the attribute list via dir(), and then complete it | |
1767 | # with a few extra special-purpose calls. |
|
1767 | # with a few extra special-purpose calls. | |
1768 | words = dir(obj) |
|
1768 | words = dir(obj) | |
1769 |
|
1769 | |||
1770 | if hasattr(obj,'__class__'): |
|
1770 | if hasattr(obj,'__class__'): | |
1771 | words.append('__class__') |
|
1771 | words.append('__class__') | |
1772 | words.extend(get_class_members(obj.__class__)) |
|
1772 | words.extend(get_class_members(obj.__class__)) | |
1773 | #if '__base__' in words: 1/0 |
|
1773 | #if '__base__' in words: 1/0 | |
1774 |
|
1774 | |||
1775 | # Some libraries (such as traits) may introduce duplicates, we want to |
|
1775 | # Some libraries (such as traits) may introduce duplicates, we want to | |
1776 | # track and clean this up if it happens |
|
1776 | # track and clean this up if it happens | |
1777 | may_have_dupes = False |
|
1777 | may_have_dupes = False | |
1778 |
|
1778 | |||
1779 | # this is the 'dir' function for objects with Enthought's traits |
|
1779 | # this is the 'dir' function for objects with Enthought's traits | |
1780 | if hasattr(obj, 'trait_names'): |
|
1780 | if hasattr(obj, 'trait_names'): | |
1781 | try: |
|
1781 | try: | |
1782 | words.extend(obj.trait_names()) |
|
1782 | words.extend(obj.trait_names()) | |
1783 | may_have_dupes = True |
|
1783 | may_have_dupes = True | |
1784 | except TypeError: |
|
1784 | except TypeError: | |
1785 | # This will happen if `obj` is a class and not an instance. |
|
1785 | # This will happen if `obj` is a class and not an instance. | |
1786 | pass |
|
1786 | pass | |
1787 |
|
1787 | |||
1788 | # Support for PyCrust-style _getAttributeNames magic method. |
|
1788 | # Support for PyCrust-style _getAttributeNames magic method. | |
1789 | if hasattr(obj, '_getAttributeNames'): |
|
1789 | if hasattr(obj, '_getAttributeNames'): | |
1790 | try: |
|
1790 | try: | |
1791 | words.extend(obj._getAttributeNames()) |
|
1791 | words.extend(obj._getAttributeNames()) | |
1792 | may_have_dupes = True |
|
1792 | may_have_dupes = True | |
1793 | except TypeError: |
|
1793 | except TypeError: | |
1794 | # `obj` is a class and not an instance. Ignore |
|
1794 | # `obj` is a class and not an instance. Ignore | |
1795 | # this error. |
|
1795 | # this error. | |
1796 | pass |
|
1796 | pass | |
1797 |
|
1797 | |||
1798 | if may_have_dupes: |
|
1798 | if may_have_dupes: | |
1799 | # eliminate possible duplicates, as some traits may also |
|
1799 | # eliminate possible duplicates, as some traits may also | |
1800 | # appear as normal attributes in the dir() call. |
|
1800 | # appear as normal attributes in the dir() call. | |
1801 | words = list(set(words)) |
|
1801 | words = list(set(words)) | |
1802 | words.sort() |
|
1802 | words.sort() | |
1803 |
|
1803 | |||
1804 | # filter out non-string attributes which may be stuffed by dir() calls |
|
1804 | # filter out non-string attributes which may be stuffed by dir() calls | |
1805 | # and poor coding in third-party modules |
|
1805 | # and poor coding in third-party modules | |
1806 | return [w for w in words if isinstance(w, basestring)] |
|
1806 | return [w for w in words if isinstance(w, basestring)] | |
1807 |
|
1807 | |||
1808 | #---------------------------------------------------------------------------- |
|
1808 | #---------------------------------------------------------------------------- | |
1809 | def import_fail_info(mod_name,fns=None): |
|
1809 | def import_fail_info(mod_name,fns=None): | |
1810 | """Inform load failure for a module.""" |
|
1810 | """Inform load failure for a module.""" | |
1811 |
|
1811 | |||
1812 | if fns == None: |
|
1812 | if fns == None: | |
1813 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
1813 | warn("Loading of %s failed.\n" % (mod_name,)) | |
1814 | else: |
|
1814 | else: | |
1815 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
1815 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) | |
1816 |
|
1816 | |||
1817 | #---------------------------------------------------------------------------- |
|
1817 | #---------------------------------------------------------------------------- | |
1818 | # Proposed popitem() extension, written as a method |
|
1818 | # Proposed popitem() extension, written as a method | |
1819 |
|
1819 | |||
1820 |
|
1820 | |||
1821 | class NotGiven: pass |
|
1821 | class NotGiven: pass | |
1822 |
|
1822 | |||
1823 | def popkey(dct,key,default=NotGiven): |
|
1823 | def popkey(dct,key,default=NotGiven): | |
1824 | """Return dct[key] and delete dct[key]. |
|
1824 | """Return dct[key] and delete dct[key]. | |
1825 |
|
1825 | |||
1826 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
1826 | If default is given, return it if dct[key] doesn't exist, otherwise raise | |
1827 | KeyError. """ |
|
1827 | KeyError. """ | |
1828 |
|
1828 | |||
1829 | try: |
|
1829 | try: | |
1830 | val = dct[key] |
|
1830 | val = dct[key] | |
1831 | except KeyError: |
|
1831 | except KeyError: | |
1832 | if default is NotGiven: |
|
1832 | if default is NotGiven: | |
1833 | raise |
|
1833 | raise | |
1834 | else: |
|
1834 | else: | |
1835 | return default |
|
1835 | return default | |
1836 | else: |
|
1836 | else: | |
1837 | del dct[key] |
|
1837 | del dct[key] | |
1838 | return val |
|
1838 | return val | |
1839 |
|
1839 | |||
1840 | def wrap_deprecated(func, suggest = '<nothing>'): |
|
1840 | def wrap_deprecated(func, suggest = '<nothing>'): | |
1841 | def newFunc(*args, **kwargs): |
|
1841 | def newFunc(*args, **kwargs): | |
1842 | warnings.warn("Call to deprecated function %s, use %s instead" % |
|
1842 | warnings.warn("Call to deprecated function %s, use %s instead" % | |
1843 | ( func.__name__, suggest), |
|
1843 | ( func.__name__, suggest), | |
1844 | category=DeprecationWarning, |
|
1844 | category=DeprecationWarning, | |
1845 | stacklevel = 2) |
|
1845 | stacklevel = 2) | |
1846 | return func(*args, **kwargs) |
|
1846 | return func(*args, **kwargs) | |
1847 | return newFunc |
|
1847 | return newFunc | |
1848 |
|
1848 | |||
1849 | #*************************** end of file <genutils.py> ********************** |
|
1849 | #*************************** end of file <genutils.py> ********************** | |
1850 |
|
1850 |
@@ -1,2471 +1,2475 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 |
$Id: iplib.py 257 |
|
9 | $Id: iplib.py 2577 2007-08-02 23:50:02Z fperez $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | # |
|
18 | # | |
19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
19 | # Note: this code originally subclassed code.InteractiveConsole from the | |
20 | # Python standard library. Over time, all of that class has been copied |
|
20 | # Python standard library. Over time, all of that class has been copied | |
21 | # verbatim here for modifications which could not be accomplished by |
|
21 | # verbatim here for modifications which could not be accomplished by | |
22 | # subclassing. At this point, there are no dependencies at all on the code |
|
22 | # subclassing. At this point, there are no dependencies at all on the code | |
23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
23 | # module anymore (it is not even imported). The Python License (sec. 2) | |
24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
24 | # allows for this, but it's always nice to acknowledge credit where credit is | |
25 | # due. |
|
25 | # due. | |
26 | #***************************************************************************** |
|
26 | #***************************************************************************** | |
27 |
|
27 | |||
28 | #**************************************************************************** |
|
28 | #**************************************************************************** | |
29 | # Modules and globals |
|
29 | # Modules and globals | |
30 |
|
30 | |||
31 | from IPython import Release |
|
31 | from IPython import Release | |
32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
32 | __author__ = '%s <%s>\n%s <%s>' % \ | |
33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) | |
34 | __license__ = Release.license |
|
34 | __license__ = Release.license | |
35 | __version__ = Release.version |
|
35 | __version__ = Release.version | |
36 |
|
36 | |||
37 | # Python standard modules |
|
37 | # Python standard modules | |
38 | import __main__ |
|
38 | import __main__ | |
39 | import __builtin__ |
|
39 | import __builtin__ | |
40 | import StringIO |
|
40 | import StringIO | |
41 | import bdb |
|
41 | import bdb | |
42 | import cPickle as pickle |
|
42 | import cPickle as pickle | |
43 | import codeop |
|
43 | import codeop | |
44 | import exceptions |
|
44 | import exceptions | |
45 | import glob |
|
45 | import glob | |
46 | import inspect |
|
46 | import inspect | |
47 | import keyword |
|
47 | import keyword | |
48 | import new |
|
48 | import new | |
49 | import os |
|
49 | import os | |
50 | import pydoc |
|
50 | import pydoc | |
51 | import re |
|
51 | import re | |
52 | import shutil |
|
52 | import shutil | |
53 | import string |
|
53 | import string | |
54 | import sys |
|
54 | import sys | |
55 | import tempfile |
|
55 | import tempfile | |
56 | import traceback |
|
56 | import traceback | |
57 | import types |
|
57 | import types | |
58 | import pickleshare |
|
58 | import pickleshare | |
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.FakeModule import FakeModule |
|
66 | from IPython.FakeModule import FakeModule | |
67 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
67 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns | |
68 | from IPython.Logger import Logger |
|
68 | from IPython.Logger import Logger | |
69 | from IPython.Magic import Magic |
|
69 | from IPython.Magic import Magic | |
70 | from IPython.Prompts import CachedOutput |
|
70 | from IPython.Prompts import CachedOutput | |
71 | from IPython.ipstruct import Struct |
|
71 | from IPython.ipstruct import Struct | |
72 | from IPython.background_jobs import BackgroundJobManager |
|
72 | from IPython.background_jobs import BackgroundJobManager | |
73 | from IPython.usage import cmd_line_usage,interactive_usage |
|
73 | from IPython.usage import cmd_line_usage,interactive_usage | |
74 | from IPython.genutils import * |
|
74 | from IPython.genutils import * | |
75 | from IPython.strdispatch import StrDispatch |
|
75 | from IPython.strdispatch import StrDispatch | |
76 | import IPython.ipapi |
|
76 | import IPython.ipapi | |
77 | import IPython.history |
|
77 | import IPython.history | |
78 | import IPython.prefilter as prefilter |
|
78 | import IPython.prefilter as prefilter | |
79 | import IPython.shadowns |
|
79 | import IPython.shadowns | |
80 | # Globals |
|
80 | # Globals | |
81 |
|
81 | |||
82 | # store the builtin raw_input globally, and use this always, in case user code |
|
82 | # store the builtin raw_input globally, and use this always, in case user code | |
83 | # overwrites it (like wx.py.PyShell does) |
|
83 | # overwrites it (like wx.py.PyShell does) | |
84 | raw_input_original = raw_input |
|
84 | raw_input_original = raw_input | |
85 |
|
85 | |||
86 | # compiled regexps for autoindent management |
|
86 | # compiled regexps for autoindent management | |
87 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
87 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | #**************************************************************************** |
|
90 | #**************************************************************************** | |
91 | # Some utility function definitions |
|
91 | # Some utility function definitions | |
92 |
|
92 | |||
93 | ini_spaces_re = re.compile(r'^(\s+)') |
|
93 | ini_spaces_re = re.compile(r'^(\s+)') | |
94 |
|
94 | |||
95 | def num_ini_spaces(strng): |
|
95 | def num_ini_spaces(strng): | |
96 | """Return the number of initial spaces in a string""" |
|
96 | """Return the number of initial spaces in a string""" | |
97 |
|
97 | |||
98 | ini_spaces = ini_spaces_re.match(strng) |
|
98 | ini_spaces = ini_spaces_re.match(strng) | |
99 | if ini_spaces: |
|
99 | if ini_spaces: | |
100 | return ini_spaces.end() |
|
100 | return ini_spaces.end() | |
101 | else: |
|
101 | else: | |
102 | return 0 |
|
102 | return 0 | |
103 |
|
103 | |||
104 | def softspace(file, newvalue): |
|
104 | def softspace(file, newvalue): | |
105 | """Copied from code.py, to remove the dependency""" |
|
105 | """Copied from code.py, to remove the dependency""" | |
106 |
|
106 | |||
107 | oldvalue = 0 |
|
107 | oldvalue = 0 | |
108 | try: |
|
108 | try: | |
109 | oldvalue = file.softspace |
|
109 | oldvalue = file.softspace | |
110 | except AttributeError: |
|
110 | except AttributeError: | |
111 | pass |
|
111 | pass | |
112 | try: |
|
112 | try: | |
113 | file.softspace = newvalue |
|
113 | file.softspace = newvalue | |
114 | except (AttributeError, TypeError): |
|
114 | except (AttributeError, TypeError): | |
115 | # "attribute-less object" or "read-only attributes" |
|
115 | # "attribute-less object" or "read-only attributes" | |
116 | pass |
|
116 | pass | |
117 | return oldvalue |
|
117 | return oldvalue | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | #**************************************************************************** |
|
120 | #**************************************************************************** | |
121 | # Local use exceptions |
|
121 | # Local use exceptions | |
122 | class SpaceInInput(exceptions.Exception): pass |
|
122 | class SpaceInInput(exceptions.Exception): pass | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | #**************************************************************************** |
|
125 | #**************************************************************************** | |
126 | # Local use classes |
|
126 | # Local use classes | |
127 | class Bunch: pass |
|
127 | class Bunch: pass | |
128 |
|
128 | |||
129 | class Undefined: pass |
|
129 | class Undefined: pass | |
130 |
|
130 | |||
131 | class Quitter(object): |
|
131 | class Quitter(object): | |
132 | """Simple class to handle exit, similar to Python 2.5's. |
|
132 | """Simple class to handle exit, similar to Python 2.5's. | |
133 |
|
133 | |||
134 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
134 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 | |
135 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
135 | doesn't do (obviously, since it doesn't know about ipython).""" | |
136 |
|
136 | |||
137 | def __init__(self,shell,name): |
|
137 | def __init__(self,shell,name): | |
138 | self.shell = shell |
|
138 | self.shell = shell | |
139 | self.name = name |
|
139 | self.name = name | |
140 |
|
140 | |||
141 | def __repr__(self): |
|
141 | def __repr__(self): | |
142 | return 'Type %s() to exit.' % self.name |
|
142 | return 'Type %s() to exit.' % self.name | |
143 | __str__ = __repr__ |
|
143 | __str__ = __repr__ | |
144 |
|
144 | |||
145 | def __call__(self): |
|
145 | def __call__(self): | |
146 | self.shell.exit() |
|
146 | self.shell.exit() | |
147 |
|
147 | |||
148 | class InputList(list): |
|
148 | class InputList(list): | |
149 | """Class to store user input. |
|
149 | """Class to store user input. | |
150 |
|
150 | |||
151 | It's basically a list, but slices return a string instead of a list, thus |
|
151 | It's basically a list, but slices return a string instead of a list, thus | |
152 | allowing things like (assuming 'In' is an instance): |
|
152 | allowing things like (assuming 'In' is an instance): | |
153 |
|
153 | |||
154 | exec In[4:7] |
|
154 | exec In[4:7] | |
155 |
|
155 | |||
156 | or |
|
156 | or | |
157 |
|
157 | |||
158 | exec In[5:9] + In[14] + In[21:25]""" |
|
158 | exec In[5:9] + In[14] + In[21:25]""" | |
159 |
|
159 | |||
160 | def __getslice__(self,i,j): |
|
160 | def __getslice__(self,i,j): | |
161 | return ''.join(list.__getslice__(self,i,j)) |
|
161 | return ''.join(list.__getslice__(self,i,j)) | |
162 |
|
162 | |||
163 | class SyntaxTB(ultraTB.ListTB): |
|
163 | class SyntaxTB(ultraTB.ListTB): | |
164 | """Extension which holds some state: the last exception value""" |
|
164 | """Extension which holds some state: the last exception value""" | |
165 |
|
165 | |||
166 | def __init__(self,color_scheme = 'NoColor'): |
|
166 | def __init__(self,color_scheme = 'NoColor'): | |
167 | ultraTB.ListTB.__init__(self,color_scheme) |
|
167 | ultraTB.ListTB.__init__(self,color_scheme) | |
168 | self.last_syntax_error = None |
|
168 | self.last_syntax_error = None | |
169 |
|
169 | |||
170 | def __call__(self, etype, value, elist): |
|
170 | def __call__(self, etype, value, elist): | |
171 | self.last_syntax_error = value |
|
171 | self.last_syntax_error = value | |
172 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
172 | ultraTB.ListTB.__call__(self,etype,value,elist) | |
173 |
|
173 | |||
174 | def clear_err_state(self): |
|
174 | def clear_err_state(self): | |
175 | """Return the current error state and clear it""" |
|
175 | """Return the current error state and clear it""" | |
176 | e = self.last_syntax_error |
|
176 | e = self.last_syntax_error | |
177 | self.last_syntax_error = None |
|
177 | self.last_syntax_error = None | |
178 | return e |
|
178 | return e | |
179 |
|
179 | |||
180 | #**************************************************************************** |
|
180 | #**************************************************************************** | |
181 | # Main IPython class |
|
181 | # Main IPython class | |
182 |
|
182 | |||
183 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
183 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so | |
184 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
184 | # until a full rewrite is made. I've cleaned all cross-class uses of | |
185 | # attributes and methods, but too much user code out there relies on the |
|
185 | # attributes and methods, but too much user code out there relies on the | |
186 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
186 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. | |
187 | # |
|
187 | # | |
188 | # But at least now, all the pieces have been separated and we could, in |
|
188 | # But at least now, all the pieces have been separated and we could, in | |
189 | # principle, stop using the mixin. This will ease the transition to the |
|
189 | # principle, stop using the mixin. This will ease the transition to the | |
190 | # chainsaw branch. |
|
190 | # chainsaw branch. | |
191 |
|
191 | |||
192 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
192 | # For reference, the following is the list of 'self.foo' uses in the Magic | |
193 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
193 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython | |
194 | # class, to prevent clashes. |
|
194 | # class, to prevent clashes. | |
195 |
|
195 | |||
196 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
196 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', | |
197 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
197 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', | |
198 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
198 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', | |
199 | # 'self.value'] |
|
199 | # 'self.value'] | |
200 |
|
200 | |||
201 | class InteractiveShell(object,Magic): |
|
201 | class InteractiveShell(object,Magic): | |
202 | """An enhanced console for Python.""" |
|
202 | """An enhanced console for Python.""" | |
203 |
|
203 | |||
204 | # class attribute to indicate whether the class supports threads or not. |
|
204 | # class attribute to indicate whether the class supports threads or not. | |
205 | # Subclasses with thread support should override this as needed. |
|
205 | # Subclasses with thread support should override this as needed. | |
206 | isthreaded = False |
|
206 | isthreaded = False | |
207 |
|
207 | |||
208 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
208 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
209 | user_ns = None,user_global_ns=None,banner2='', |
|
209 | user_ns = None,user_global_ns=None,banner2='', | |
210 | custom_exceptions=((),None),embedded=False): |
|
210 | custom_exceptions=((),None),embedded=False): | |
211 |
|
211 | |||
212 | # log system |
|
212 | # log system | |
213 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
213 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') | |
214 |
|
214 | |||
215 | # some minimal strict typechecks. For some core data structures, I |
|
215 | # some minimal strict typechecks. For some core data structures, I | |
216 | # want actual basic python types, not just anything that looks like |
|
216 | # want actual basic python types, not just anything that looks like | |
217 | # one. This is especially true for namespaces. |
|
217 | # one. This is especially true for namespaces. | |
218 | for ns in (user_ns,user_global_ns): |
|
218 | for ns in (user_ns,user_global_ns): | |
219 | if ns is not None and type(ns) != types.DictType: |
|
219 | if ns is not None and type(ns) != types.DictType: | |
220 | raise TypeError,'namespace must be a dictionary' |
|
220 | raise TypeError,'namespace must be a dictionary' | |
221 |
|
221 | |||
222 | # Job manager (for jobs run as background threads) |
|
222 | # Job manager (for jobs run as background threads) | |
223 | self.jobs = BackgroundJobManager() |
|
223 | self.jobs = BackgroundJobManager() | |
224 |
|
224 | |||
225 | # Store the actual shell's name |
|
225 | # Store the actual shell's name | |
226 | self.name = name |
|
226 | self.name = name | |
227 |
|
227 | |||
228 | # We need to know whether the instance is meant for embedding, since |
|
228 | # We need to know whether the instance is meant for embedding, since | |
229 | # global/local namespaces need to be handled differently in that case |
|
229 | # global/local namespaces need to be handled differently in that case | |
230 | self.embedded = embedded |
|
230 | self.embedded = embedded | |
|
231 | if embedded: | |||
|
232 | # Control variable so users can, from within the embedded instance, | |||
|
233 | # permanently deactivate it. | |||
|
234 | self.embedded_active = True | |||
231 |
|
235 | |||
232 | # command compiler |
|
236 | # command compiler | |
233 | self.compile = codeop.CommandCompiler() |
|
237 | self.compile = codeop.CommandCompiler() | |
234 |
|
238 | |||
235 | # User input buffer |
|
239 | # User input buffer | |
236 | self.buffer = [] |
|
240 | self.buffer = [] | |
237 |
|
241 | |||
238 | # Default name given in compilation of code |
|
242 | # Default name given in compilation of code | |
239 | self.filename = '<ipython console>' |
|
243 | self.filename = '<ipython console>' | |
240 |
|
244 | |||
241 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
245 | # Install our own quitter instead of the builtins. For python2.3-2.4, | |
242 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
246 | # this brings in behavior like 2.5, and for 2.5 it's identical. | |
243 | __builtin__.exit = Quitter(self,'exit') |
|
247 | __builtin__.exit = Quitter(self,'exit') | |
244 | __builtin__.quit = Quitter(self,'quit') |
|
248 | __builtin__.quit = Quitter(self,'quit') | |
245 |
|
249 | |||
246 | # Make an empty namespace, which extension writers can rely on both |
|
250 | # Make an empty namespace, which extension writers can rely on both | |
247 | # existing and NEVER being used by ipython itself. This gives them a |
|
251 | # existing and NEVER being used by ipython itself. This gives them a | |
248 | # convenient location for storing additional information and state |
|
252 | # convenient location for storing additional information and state | |
249 | # their extensions may require, without fear of collisions with other |
|
253 | # their extensions may require, without fear of collisions with other | |
250 | # ipython names that may develop later. |
|
254 | # ipython names that may develop later. | |
251 | self.meta = Struct() |
|
255 | self.meta = Struct() | |
252 |
|
256 | |||
253 | # Create the namespace where the user will operate. user_ns is |
|
257 | # Create the namespace where the user will operate. user_ns is | |
254 | # normally the only one used, and it is passed to the exec calls as |
|
258 | # normally the only one used, and it is passed to the exec calls as | |
255 | # the locals argument. But we do carry a user_global_ns namespace |
|
259 | # the locals argument. But we do carry a user_global_ns namespace | |
256 | # given as the exec 'globals' argument, This is useful in embedding |
|
260 | # given as the exec 'globals' argument, This is useful in embedding | |
257 | # situations where the ipython shell opens in a context where the |
|
261 | # situations where the ipython shell opens in a context where the | |
258 | # distinction between locals and globals is meaningful. |
|
262 | # distinction between locals and globals is meaningful. | |
259 |
|
263 | |||
260 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
264 | # 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 |
|
265 | # 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 |
|
266 | # should really track down where the problem is coming from. Alex | |
263 | # Schmolck reported this problem first. |
|
267 | # Schmolck reported this problem first. | |
264 |
|
268 | |||
265 | # A useful post by Alex Martelli on this topic: |
|
269 | # A useful post by Alex Martelli on this topic: | |
266 | # Re: inconsistent value from __builtins__ |
|
270 | # Re: inconsistent value from __builtins__ | |
267 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
271 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
268 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
272 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
269 | # Gruppen: comp.lang.python |
|
273 | # Gruppen: comp.lang.python | |
270 |
|
274 | |||
271 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
275 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
272 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
276 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
273 | # > <type 'dict'> |
|
277 | # > <type 'dict'> | |
274 | # > >>> print type(__builtins__) |
|
278 | # > >>> print type(__builtins__) | |
275 | # > <type 'module'> |
|
279 | # > <type 'module'> | |
276 | # > Is this difference in return value intentional? |
|
280 | # > Is this difference in return value intentional? | |
277 |
|
281 | |||
278 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
282 | # 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 |
|
283 | # 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 |
|
284 | # 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 |
|
285 | # that if you need to access the built-in namespace directly, you | |
282 | # should start with "import __builtin__" (note, no 's') which will |
|
286 | # should start with "import __builtin__" (note, no 's') which will | |
283 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
287 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
284 |
|
288 | |||
285 | # These routines return properly built dicts as needed by the rest of |
|
289 | # 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 |
|
290 | # the code, and can also be used by extension writers to generate | |
287 | # properly initialized namespaces. |
|
291 | # properly initialized namespaces. | |
288 | user_ns = IPython.ipapi.make_user_ns(user_ns) |
|
292 | user_ns = IPython.ipapi.make_user_ns(user_ns) | |
289 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) |
|
293 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) | |
290 |
|
294 | |||
291 | # Assign namespaces |
|
295 | # Assign namespaces | |
292 | # This is the namespace where all normal user variables live |
|
296 | # This is the namespace where all normal user variables live | |
293 | self.user_ns = user_ns |
|
297 | self.user_ns = user_ns | |
294 | # Embedded instances require a separate namespace for globals. |
|
298 | # Embedded instances require a separate namespace for globals. | |
295 | # Normally this one is unused by non-embedded instances. |
|
299 | # Normally this one is unused by non-embedded instances. | |
296 | self.user_global_ns = user_global_ns |
|
300 | self.user_global_ns = user_global_ns | |
297 | # A namespace to keep track of internal data structures to prevent |
|
301 | # A namespace to keep track of internal data structures to prevent | |
298 | # them from cluttering user-visible stuff. Will be updated later |
|
302 | # them from cluttering user-visible stuff. Will be updated later | |
299 | self.internal_ns = {} |
|
303 | self.internal_ns = {} | |
300 |
|
304 | |||
301 | # Namespace of system aliases. Each entry in the alias |
|
305 | # Namespace of system aliases. Each entry in the alias | |
302 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
306 | # table must be a 2-tuple of the form (N,name), where N is the number | |
303 | # of positional arguments of the alias. |
|
307 | # of positional arguments of the alias. | |
304 | self.alias_table = {} |
|
308 | self.alias_table = {} | |
305 |
|
309 | |||
306 | # A table holding all the namespaces IPython deals with, so that |
|
310 | # A table holding all the namespaces IPython deals with, so that | |
307 | # introspection facilities can search easily. |
|
311 | # introspection facilities can search easily. | |
308 | self.ns_table = {'user':user_ns, |
|
312 | self.ns_table = {'user':user_ns, | |
309 | 'user_global':user_global_ns, |
|
313 | 'user_global':user_global_ns, | |
310 | 'alias':self.alias_table, |
|
314 | 'alias':self.alias_table, | |
311 | 'internal':self.internal_ns, |
|
315 | 'internal':self.internal_ns, | |
312 | 'builtin':__builtin__.__dict__ |
|
316 | 'builtin':__builtin__.__dict__ | |
313 | } |
|
317 | } | |
314 |
|
318 | |||
315 | # The user namespace MUST have a pointer to the shell itself. |
|
319 | # The user namespace MUST have a pointer to the shell itself. | |
316 | self.user_ns[name] = self |
|
320 | self.user_ns[name] = self | |
317 |
|
321 | |||
318 | # We need to insert into sys.modules something that looks like a |
|
322 | # We need to insert into sys.modules something that looks like a | |
319 | # module but which accesses the IPython namespace, for shelve and |
|
323 | # module but which accesses the IPython namespace, for shelve and | |
320 | # pickle to work interactively. Normally they rely on getting |
|
324 | # pickle to work interactively. Normally they rely on getting | |
321 | # everything out of __main__, but for embedding purposes each IPython |
|
325 | # everything out of __main__, but for embedding purposes each IPython | |
322 | # instance has its own private namespace, so we can't go shoving |
|
326 | # instance has its own private namespace, so we can't go shoving | |
323 | # everything into __main__. |
|
327 | # everything into __main__. | |
324 |
|
328 | |||
325 | # note, however, that we should only do this for non-embedded |
|
329 | # note, however, that we should only do this for non-embedded | |
326 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
330 | # ipythons, which really mimic the __main__.__dict__ with their own | |
327 | # namespace. Embedded instances, on the other hand, should not do |
|
331 | # namespace. Embedded instances, on the other hand, should not do | |
328 | # this because they need to manage the user local/global namespaces |
|
332 | # this because they need to manage the user local/global namespaces | |
329 | # only, but they live within a 'normal' __main__ (meaning, they |
|
333 | # only, but they live within a 'normal' __main__ (meaning, they | |
330 | # shouldn't overtake the execution environment of the script they're |
|
334 | # shouldn't overtake the execution environment of the script they're | |
331 | # embedded in). |
|
335 | # embedded in). | |
332 |
|
336 | |||
333 | if not embedded: |
|
337 | if not embedded: | |
334 | try: |
|
338 | try: | |
335 | main_name = self.user_ns['__name__'] |
|
339 | main_name = self.user_ns['__name__'] | |
336 | except KeyError: |
|
340 | except KeyError: | |
337 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
341 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' | |
338 | else: |
|
342 | else: | |
339 | #print "pickle hack in place" # dbg |
|
343 | #print "pickle hack in place" # dbg | |
340 | #print 'main_name:',main_name # dbg |
|
344 | #print 'main_name:',main_name # dbg | |
341 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
345 | sys.modules[main_name] = FakeModule(self.user_ns) | |
342 |
|
346 | |||
343 | # List of input with multi-line handling. |
|
347 | # List of input with multi-line handling. | |
344 | # Fill its zero entry, user counter starts at 1 |
|
348 | # Fill its zero entry, user counter starts at 1 | |
345 | self.input_hist = InputList(['\n']) |
|
349 | self.input_hist = InputList(['\n']) | |
346 | # This one will hold the 'raw' input history, without any |
|
350 | # This one will hold the 'raw' input history, without any | |
347 | # pre-processing. This will allow users to retrieve the input just as |
|
351 | # pre-processing. This will allow users to retrieve the input just as | |
348 | # it was exactly typed in by the user, with %hist -r. |
|
352 | # it was exactly typed in by the user, with %hist -r. | |
349 | self.input_hist_raw = InputList(['\n']) |
|
353 | self.input_hist_raw = InputList(['\n']) | |
350 |
|
354 | |||
351 | # list of visited directories |
|
355 | # list of visited directories | |
352 | try: |
|
356 | try: | |
353 | self.dir_hist = [os.getcwd()] |
|
357 | self.dir_hist = [os.getcwd()] | |
354 | except OSError: |
|
358 | except OSError: | |
355 | self.dir_hist = [] |
|
359 | self.dir_hist = [] | |
356 |
|
360 | |||
357 | # dict of output history |
|
361 | # dict of output history | |
358 | self.output_hist = {} |
|
362 | self.output_hist = {} | |
359 |
|
363 | |||
360 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
364 | # Get system encoding at startup time. Certain terminals (like Emacs | |
361 | # under Win32 have it set to None, and we need to have a known valid |
|
365 | # under Win32 have it set to None, and we need to have a known valid | |
362 | # encoding to use in the raw_input() method |
|
366 | # encoding to use in the raw_input() method | |
363 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
367 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
364 |
|
368 | |||
365 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
369 | # dict of things NOT to alias (keywords, builtins and some magics) | |
366 | no_alias = {} |
|
370 | no_alias = {} | |
367 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
371 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] | |
368 | for key in keyword.kwlist + no_alias_magics: |
|
372 | for key in keyword.kwlist + no_alias_magics: | |
369 | no_alias[key] = 1 |
|
373 | no_alias[key] = 1 | |
370 | no_alias.update(__builtin__.__dict__) |
|
374 | no_alias.update(__builtin__.__dict__) | |
371 | self.no_alias = no_alias |
|
375 | self.no_alias = no_alias | |
372 |
|
376 | |||
373 | # make global variables for user access to these |
|
377 | # make global variables for user access to these | |
374 | self.user_ns['_ih'] = self.input_hist |
|
378 | self.user_ns['_ih'] = self.input_hist | |
375 | self.user_ns['_oh'] = self.output_hist |
|
379 | self.user_ns['_oh'] = self.output_hist | |
376 | self.user_ns['_dh'] = self.dir_hist |
|
380 | self.user_ns['_dh'] = self.dir_hist | |
377 |
|
381 | |||
378 | # user aliases to input and output histories |
|
382 | # user aliases to input and output histories | |
379 | self.user_ns['In'] = self.input_hist |
|
383 | self.user_ns['In'] = self.input_hist | |
380 | self.user_ns['Out'] = self.output_hist |
|
384 | self.user_ns['Out'] = self.output_hist | |
381 |
|
385 | |||
382 | self.user_ns['_sh'] = IPython.shadowns |
|
386 | self.user_ns['_sh'] = IPython.shadowns | |
383 | # Object variable to store code object waiting execution. This is |
|
387 | # Object variable to store code object waiting execution. This is | |
384 | # used mainly by the multithreaded shells, but it can come in handy in |
|
388 | # used mainly by the multithreaded shells, but it can come in handy in | |
385 | # other situations. No need to use a Queue here, since it's a single |
|
389 | # other situations. No need to use a Queue here, since it's a single | |
386 | # item which gets cleared once run. |
|
390 | # item which gets cleared once run. | |
387 | self.code_to_run = None |
|
391 | self.code_to_run = None | |
388 |
|
392 | |||
389 | # escapes for automatic behavior on the command line |
|
393 | # escapes for automatic behavior on the command line | |
390 | self.ESC_SHELL = '!' |
|
394 | self.ESC_SHELL = '!' | |
391 | self.ESC_SH_CAP = '!!' |
|
395 | self.ESC_SH_CAP = '!!' | |
392 | self.ESC_HELP = '?' |
|
396 | self.ESC_HELP = '?' | |
393 | self.ESC_MAGIC = '%' |
|
397 | self.ESC_MAGIC = '%' | |
394 | self.ESC_QUOTE = ',' |
|
398 | self.ESC_QUOTE = ',' | |
395 | self.ESC_QUOTE2 = ';' |
|
399 | self.ESC_QUOTE2 = ';' | |
396 | self.ESC_PAREN = '/' |
|
400 | self.ESC_PAREN = '/' | |
397 |
|
401 | |||
398 | # And their associated handlers |
|
402 | # And their associated handlers | |
399 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
403 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, | |
400 | self.ESC_QUOTE : self.handle_auto, |
|
404 | self.ESC_QUOTE : self.handle_auto, | |
401 | self.ESC_QUOTE2 : self.handle_auto, |
|
405 | self.ESC_QUOTE2 : self.handle_auto, | |
402 | self.ESC_MAGIC : self.handle_magic, |
|
406 | self.ESC_MAGIC : self.handle_magic, | |
403 | self.ESC_HELP : self.handle_help, |
|
407 | self.ESC_HELP : self.handle_help, | |
404 | self.ESC_SHELL : self.handle_shell_escape, |
|
408 | self.ESC_SHELL : self.handle_shell_escape, | |
405 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
409 | self.ESC_SH_CAP : self.handle_shell_escape, | |
406 | } |
|
410 | } | |
407 |
|
411 | |||
408 | # class initializations |
|
412 | # class initializations | |
409 | Magic.__init__(self,self) |
|
413 | Magic.__init__(self,self) | |
410 |
|
414 | |||
411 | # Python source parser/formatter for syntax highlighting |
|
415 | # Python source parser/formatter for syntax highlighting | |
412 | pyformat = PyColorize.Parser().format |
|
416 | pyformat = PyColorize.Parser().format | |
413 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
417 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) | |
414 |
|
418 | |||
415 | # hooks holds pointers used for user-side customizations |
|
419 | # hooks holds pointers used for user-side customizations | |
416 | self.hooks = Struct() |
|
420 | self.hooks = Struct() | |
417 |
|
421 | |||
418 | self.strdispatchers = {} |
|
422 | self.strdispatchers = {} | |
419 |
|
423 | |||
420 | # Set all default hooks, defined in the IPython.hooks module. |
|
424 | # Set all default hooks, defined in the IPython.hooks module. | |
421 | hooks = IPython.hooks |
|
425 | hooks = IPython.hooks | |
422 | for hook_name in hooks.__all__: |
|
426 | for hook_name in hooks.__all__: | |
423 | # default hooks have priority 100, i.e. low; user hooks should have |
|
427 | # default hooks have priority 100, i.e. low; user hooks should have | |
424 | # 0-100 priority |
|
428 | # 0-100 priority | |
425 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
429 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
426 | #print "bound hook",hook_name |
|
430 | #print "bound hook",hook_name | |
427 |
|
431 | |||
428 | # Flag to mark unconditional exit |
|
432 | # Flag to mark unconditional exit | |
429 | self.exit_now = False |
|
433 | self.exit_now = False | |
430 |
|
434 | |||
431 | self.usage_min = """\ |
|
435 | self.usage_min = """\ | |
432 | An enhanced console for Python. |
|
436 | An enhanced console for Python. | |
433 | Some of its features are: |
|
437 | Some of its features are: | |
434 | - Readline support if the readline library is present. |
|
438 | - Readline support if the readline library is present. | |
435 | - Tab completion in the local namespace. |
|
439 | - Tab completion in the local namespace. | |
436 | - Logging of input, see command-line options. |
|
440 | - Logging of input, see command-line options. | |
437 | - System shell escape via ! , eg !ls. |
|
441 | - System shell escape via ! , eg !ls. | |
438 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
442 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) | |
439 | - Keeps track of locally defined variables via %who, %whos. |
|
443 | - Keeps track of locally defined variables via %who, %whos. | |
440 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
444 | - Show object information with a ? eg ?x or x? (use ?? for more info). | |
441 | """ |
|
445 | """ | |
442 | if usage: self.usage = usage |
|
446 | if usage: self.usage = usage | |
443 | else: self.usage = self.usage_min |
|
447 | else: self.usage = self.usage_min | |
444 |
|
448 | |||
445 | # Storage |
|
449 | # Storage | |
446 | self.rc = rc # This will hold all configuration information |
|
450 | self.rc = rc # This will hold all configuration information | |
447 | self.pager = 'less' |
|
451 | self.pager = 'less' | |
448 | # temporary files used for various purposes. Deleted at exit. |
|
452 | # temporary files used for various purposes. Deleted at exit. | |
449 | self.tempfiles = [] |
|
453 | self.tempfiles = [] | |
450 |
|
454 | |||
451 | # Keep track of readline usage (later set by init_readline) |
|
455 | # Keep track of readline usage (later set by init_readline) | |
452 | self.has_readline = False |
|
456 | self.has_readline = False | |
453 |
|
457 | |||
454 | # template for logfile headers. It gets resolved at runtime by the |
|
458 | # template for logfile headers. It gets resolved at runtime by the | |
455 | # logstart method. |
|
459 | # logstart method. | |
456 | self.loghead_tpl = \ |
|
460 | self.loghead_tpl = \ | |
457 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
461 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** | |
458 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
462 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW | |
459 | #log# opts = %s |
|
463 | #log# opts = %s | |
460 | #log# args = %s |
|
464 | #log# args = %s | |
461 | #log# It is safe to make manual edits below here. |
|
465 | #log# It is safe to make manual edits below here. | |
462 | #log#----------------------------------------------------------------------- |
|
466 | #log#----------------------------------------------------------------------- | |
463 | """ |
|
467 | """ | |
464 | # for pushd/popd management |
|
468 | # for pushd/popd management | |
465 | try: |
|
469 | try: | |
466 | self.home_dir = get_home_dir() |
|
470 | self.home_dir = get_home_dir() | |
467 | except HomeDirError,msg: |
|
471 | except HomeDirError,msg: | |
468 | fatal(msg) |
|
472 | fatal(msg) | |
469 |
|
473 | |||
470 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
474 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] | |
471 |
|
475 | |||
472 | # Functions to call the underlying shell. |
|
476 | # Functions to call the underlying shell. | |
473 |
|
477 | |||
474 | # The first is similar to os.system, but it doesn't return a value, |
|
478 | # The first is similar to os.system, but it doesn't return a value, | |
475 | # and it allows interpolation of variables in the user's namespace. |
|
479 | # and it allows interpolation of variables in the user's namespace. | |
476 | self.system = lambda cmd: \ |
|
480 | self.system = lambda cmd: \ | |
477 | shell(self.var_expand(cmd,depth=2), |
|
481 | shell(self.var_expand(cmd,depth=2), | |
478 | header=self.rc.system_header, |
|
482 | header=self.rc.system_header, | |
479 | verbose=self.rc.system_verbose) |
|
483 | verbose=self.rc.system_verbose) | |
480 |
|
484 | |||
481 | # These are for getoutput and getoutputerror: |
|
485 | # These are for getoutput and getoutputerror: | |
482 | self.getoutput = lambda cmd: \ |
|
486 | self.getoutput = lambda cmd: \ | |
483 | getoutput(self.var_expand(cmd,depth=2), |
|
487 | getoutput(self.var_expand(cmd,depth=2), | |
484 | header=self.rc.system_header, |
|
488 | header=self.rc.system_header, | |
485 | verbose=self.rc.system_verbose) |
|
489 | verbose=self.rc.system_verbose) | |
486 |
|
490 | |||
487 | self.getoutputerror = lambda cmd: \ |
|
491 | self.getoutputerror = lambda cmd: \ | |
488 | getoutputerror(self.var_expand(cmd,depth=2), |
|
492 | getoutputerror(self.var_expand(cmd,depth=2), | |
489 | header=self.rc.system_header, |
|
493 | header=self.rc.system_header, | |
490 | verbose=self.rc.system_verbose) |
|
494 | verbose=self.rc.system_verbose) | |
491 |
|
495 | |||
492 |
|
496 | |||
493 | # keep track of where we started running (mainly for crash post-mortem) |
|
497 | # keep track of where we started running (mainly for crash post-mortem) | |
494 | self.starting_dir = os.getcwd() |
|
498 | self.starting_dir = os.getcwd() | |
495 |
|
499 | |||
496 | # Various switches which can be set |
|
500 | # Various switches which can be set | |
497 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
501 | self.CACHELENGTH = 5000 # this is cheap, it's just text | |
498 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
502 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ | |
499 | self.banner2 = banner2 |
|
503 | self.banner2 = banner2 | |
500 |
|
504 | |||
501 | # TraceBack handlers: |
|
505 | # TraceBack handlers: | |
502 |
|
506 | |||
503 | # Syntax error handler. |
|
507 | # Syntax error handler. | |
504 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
508 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
505 |
|
509 | |||
506 | # The interactive one is initialized with an offset, meaning we always |
|
510 | # The interactive one is initialized with an offset, meaning we always | |
507 | # want to remove the topmost item in the traceback, which is our own |
|
511 | # want to remove the topmost item in the traceback, which is our own | |
508 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
512 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
509 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
513 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', | |
510 | color_scheme='NoColor', |
|
514 | color_scheme='NoColor', | |
511 | tb_offset = 1) |
|
515 | tb_offset = 1) | |
512 |
|
516 | |||
513 | # IPython itself shouldn't crash. This will produce a detailed |
|
517 | # IPython itself shouldn't crash. This will produce a detailed | |
514 | # post-mortem if it does. But we only install the crash handler for |
|
518 | # post-mortem if it does. But we only install the crash handler for | |
515 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
519 | # non-threaded shells, the threaded ones use a normal verbose reporter | |
516 | # and lose the crash handler. This is because exceptions in the main |
|
520 | # and lose the crash handler. This is because exceptions in the main | |
517 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
521 | # thread (such as in GUI code) propagate directly to sys.excepthook, | |
518 | # and there's no point in printing crash dumps for every user exception. |
|
522 | # and there's no point in printing crash dumps for every user exception. | |
519 | if self.isthreaded: |
|
523 | if self.isthreaded: | |
520 | ipCrashHandler = ultraTB.FormattedTB() |
|
524 | ipCrashHandler = ultraTB.FormattedTB() | |
521 | else: |
|
525 | else: | |
522 | from IPython import CrashHandler |
|
526 | from IPython import CrashHandler | |
523 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
527 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) | |
524 | self.set_crash_handler(ipCrashHandler) |
|
528 | self.set_crash_handler(ipCrashHandler) | |
525 |
|
529 | |||
526 | # and add any custom exception handlers the user may have specified |
|
530 | # and add any custom exception handlers the user may have specified | |
527 | self.set_custom_exc(*custom_exceptions) |
|
531 | self.set_custom_exc(*custom_exceptions) | |
528 |
|
532 | |||
529 | # indentation management |
|
533 | # indentation management | |
530 | self.autoindent = False |
|
534 | self.autoindent = False | |
531 | self.indent_current_nsp = 0 |
|
535 | self.indent_current_nsp = 0 | |
532 |
|
536 | |||
533 | # Make some aliases automatically |
|
537 | # Make some aliases automatically | |
534 | # Prepare list of shell aliases to auto-define |
|
538 | # Prepare list of shell aliases to auto-define | |
535 | if os.name == 'posix': |
|
539 | if os.name == 'posix': | |
536 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
540 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', | |
537 | 'mv mv -i','rm rm -i','cp cp -i', |
|
541 | 'mv mv -i','rm rm -i','cp cp -i', | |
538 | 'cat cat','less less','clear clear', |
|
542 | 'cat cat','less less','clear clear', | |
539 | # a better ls |
|
543 | # a better ls | |
540 | 'ls ls -F', |
|
544 | 'ls ls -F', | |
541 | # long ls |
|
545 | # long ls | |
542 | 'll ls -lF') |
|
546 | 'll ls -lF') | |
543 | # Extra ls aliases with color, which need special treatment on BSD |
|
547 | # Extra ls aliases with color, which need special treatment on BSD | |
544 | # variants |
|
548 | # variants | |
545 | ls_extra = ( # color ls |
|
549 | ls_extra = ( # color ls | |
546 | 'lc ls -F -o --color', |
|
550 | 'lc ls -F -o --color', | |
547 | # ls normal files only |
|
551 | # ls normal files only | |
548 | 'lf ls -F -o --color %l | grep ^-', |
|
552 | 'lf ls -F -o --color %l | grep ^-', | |
549 | # ls symbolic links |
|
553 | # ls symbolic links | |
550 | 'lk ls -F -o --color %l | grep ^l', |
|
554 | 'lk ls -F -o --color %l | grep ^l', | |
551 | # directories or links to directories, |
|
555 | # directories or links to directories, | |
552 | 'ldir ls -F -o --color %l | grep /$', |
|
556 | 'ldir ls -F -o --color %l | grep /$', | |
553 | # things which are executable |
|
557 | # things which are executable | |
554 | 'lx ls -F -o --color %l | grep ^-..x', |
|
558 | 'lx ls -F -o --color %l | grep ^-..x', | |
555 | ) |
|
559 | ) | |
556 | # The BSDs don't ship GNU ls, so they don't understand the |
|
560 | # The BSDs don't ship GNU ls, so they don't understand the | |
557 | # --color switch out of the box |
|
561 | # --color switch out of the box | |
558 | if 'bsd' in sys.platform: |
|
562 | if 'bsd' in sys.platform: | |
559 | ls_extra = ( # ls normal files only |
|
563 | ls_extra = ( # ls normal files only | |
560 | 'lf ls -lF | grep ^-', |
|
564 | 'lf ls -lF | grep ^-', | |
561 | # ls symbolic links |
|
565 | # ls symbolic links | |
562 | 'lk ls -lF | grep ^l', |
|
566 | 'lk ls -lF | grep ^l', | |
563 | # directories or links to directories, |
|
567 | # directories or links to directories, | |
564 | 'ldir ls -lF | grep /$', |
|
568 | 'ldir ls -lF | grep /$', | |
565 | # things which are executable |
|
569 | # things which are executable | |
566 | 'lx ls -lF | grep ^-..x', |
|
570 | 'lx ls -lF | grep ^-..x', | |
567 | ) |
|
571 | ) | |
568 | auto_alias = auto_alias + ls_extra |
|
572 | auto_alias = auto_alias + ls_extra | |
569 | elif os.name in ['nt','dos']: |
|
573 | elif os.name in ['nt','dos']: | |
570 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
574 | auto_alias = ('dir dir /on', 'ls dir /on', | |
571 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
575 | 'ddir dir /ad /on', 'ldir dir /ad /on', | |
572 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
576 | 'mkdir mkdir','rmdir rmdir','echo echo', | |
573 | 'ren ren','cls cls','copy copy') |
|
577 | 'ren ren','cls cls','copy copy') | |
574 | else: |
|
578 | else: | |
575 | auto_alias = () |
|
579 | auto_alias = () | |
576 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
580 | self.auto_alias = [s.split(None,1) for s in auto_alias] | |
577 | # Call the actual (public) initializer |
|
581 | # Call the actual (public) initializer | |
578 | self.init_auto_alias() |
|
582 | self.init_auto_alias() | |
579 |
|
583 | |||
580 | # Produce a public API instance |
|
584 | # Produce a public API instance | |
581 | self.api = IPython.ipapi.IPApi(self) |
|
585 | self.api = IPython.ipapi.IPApi(self) | |
582 |
|
586 | |||
583 | # track which builtins we add, so we can clean up later |
|
587 | # track which builtins we add, so we can clean up later | |
584 | self.builtins_added = {} |
|
588 | self.builtins_added = {} | |
585 | # This method will add the necessary builtins for operation, but |
|
589 | # This method will add the necessary builtins for operation, but | |
586 | # tracking what it did via the builtins_added dict. |
|
590 | # tracking what it did via the builtins_added dict. | |
587 | self.add_builtins() |
|
591 | self.add_builtins() | |
588 |
|
592 | |||
589 | # end __init__ |
|
593 | # end __init__ | |
590 |
|
594 | |||
591 | def var_expand(self,cmd,depth=0): |
|
595 | def var_expand(self,cmd,depth=0): | |
592 | """Expand python variables in a string. |
|
596 | """Expand python variables in a string. | |
593 |
|
597 | |||
594 | The depth argument indicates how many frames above the caller should |
|
598 | The depth argument indicates how many frames above the caller should | |
595 | be walked to look for the local namespace where to expand variables. |
|
599 | be walked to look for the local namespace where to expand variables. | |
596 |
|
600 | |||
597 | The global namespace for expansion is always the user's interactive |
|
601 | The global namespace for expansion is always the user's interactive | |
598 | namespace. |
|
602 | namespace. | |
599 | """ |
|
603 | """ | |
600 |
|
604 | |||
601 | return str(ItplNS(cmd.replace('#','\#'), |
|
605 | return str(ItplNS(cmd.replace('#','\#'), | |
602 | self.user_ns, # globals |
|
606 | self.user_ns, # globals | |
603 | # Skip our own frame in searching for locals: |
|
607 | # Skip our own frame in searching for locals: | |
604 | sys._getframe(depth+1).f_locals # locals |
|
608 | sys._getframe(depth+1).f_locals # locals | |
605 | )) |
|
609 | )) | |
606 |
|
610 | |||
607 | def pre_config_initialization(self): |
|
611 | def pre_config_initialization(self): | |
608 | """Pre-configuration init method |
|
612 | """Pre-configuration init method | |
609 |
|
613 | |||
610 | This is called before the configuration files are processed to |
|
614 | This is called before the configuration files are processed to | |
611 | prepare the services the config files might need. |
|
615 | prepare the services the config files might need. | |
612 |
|
616 | |||
613 | self.rc already has reasonable default values at this point. |
|
617 | self.rc already has reasonable default values at this point. | |
614 | """ |
|
618 | """ | |
615 | rc = self.rc |
|
619 | rc = self.rc | |
616 | try: |
|
620 | try: | |
617 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
621 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") | |
618 | except exceptions.UnicodeDecodeError: |
|
622 | except exceptions.UnicodeDecodeError: | |
619 | print "Your ipythondir can't be decoded to unicode!" |
|
623 | print "Your ipythondir can't be decoded to unicode!" | |
620 | print "Please set HOME environment variable to something that" |
|
624 | print "Please set HOME environment variable to something that" | |
621 | print r"only has ASCII characters, e.g. c:\home" |
|
625 | print r"only has ASCII characters, e.g. c:\home" | |
622 | print "Now it is",rc.ipythondir |
|
626 | print "Now it is",rc.ipythondir | |
623 | sys.exit() |
|
627 | sys.exit() | |
624 | self.shadowhist = IPython.history.ShadowHist(self.db) |
|
628 | self.shadowhist = IPython.history.ShadowHist(self.db) | |
625 |
|
629 | |||
626 |
|
630 | |||
627 | def post_config_initialization(self): |
|
631 | def post_config_initialization(self): | |
628 | """Post configuration init method |
|
632 | """Post configuration init method | |
629 |
|
633 | |||
630 | This is called after the configuration files have been processed to |
|
634 | This is called after the configuration files have been processed to | |
631 | 'finalize' the initialization.""" |
|
635 | 'finalize' the initialization.""" | |
632 |
|
636 | |||
633 | rc = self.rc |
|
637 | rc = self.rc | |
634 |
|
638 | |||
635 | # Object inspector |
|
639 | # Object inspector | |
636 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
640 | self.inspector = OInspect.Inspector(OInspect.InspectColors, | |
637 | PyColorize.ANSICodeColors, |
|
641 | PyColorize.ANSICodeColors, | |
638 | 'NoColor', |
|
642 | 'NoColor', | |
639 | rc.object_info_string_level) |
|
643 | rc.object_info_string_level) | |
640 |
|
644 | |||
641 | self.rl_next_input = None |
|
645 | self.rl_next_input = None | |
642 | self.rl_do_indent = False |
|
646 | self.rl_do_indent = False | |
643 | # Load readline proper |
|
647 | # Load readline proper | |
644 | if rc.readline: |
|
648 | if rc.readline: | |
645 | self.init_readline() |
|
649 | self.init_readline() | |
646 |
|
650 | |||
647 |
|
651 | |||
648 | # local shortcut, this is used a LOT |
|
652 | # local shortcut, this is used a LOT | |
649 | self.log = self.logger.log |
|
653 | self.log = self.logger.log | |
650 |
|
654 | |||
651 | # Initialize cache, set in/out prompts and printing system |
|
655 | # Initialize cache, set in/out prompts and printing system | |
652 | self.outputcache = CachedOutput(self, |
|
656 | self.outputcache = CachedOutput(self, | |
653 | rc.cache_size, |
|
657 | rc.cache_size, | |
654 | rc.pprint, |
|
658 | rc.pprint, | |
655 | input_sep = rc.separate_in, |
|
659 | input_sep = rc.separate_in, | |
656 | output_sep = rc.separate_out, |
|
660 | output_sep = rc.separate_out, | |
657 | output_sep2 = rc.separate_out2, |
|
661 | output_sep2 = rc.separate_out2, | |
658 | ps1 = rc.prompt_in1, |
|
662 | ps1 = rc.prompt_in1, | |
659 | ps2 = rc.prompt_in2, |
|
663 | ps2 = rc.prompt_in2, | |
660 | ps_out = rc.prompt_out, |
|
664 | ps_out = rc.prompt_out, | |
661 | pad_left = rc.prompts_pad_left) |
|
665 | pad_left = rc.prompts_pad_left) | |
662 |
|
666 | |||
663 | # user may have over-ridden the default print hook: |
|
667 | # user may have over-ridden the default print hook: | |
664 | try: |
|
668 | try: | |
665 | self.outputcache.__class__.display = self.hooks.display |
|
669 | self.outputcache.__class__.display = self.hooks.display | |
666 | except AttributeError: |
|
670 | except AttributeError: | |
667 | pass |
|
671 | pass | |
668 |
|
672 | |||
669 | # I don't like assigning globally to sys, because it means when |
|
673 | # I don't like assigning globally to sys, because it means when | |
670 | # embedding instances, each embedded instance overrides the previous |
|
674 | # embedding instances, each embedded instance overrides the previous | |
671 | # choice. But sys.displayhook seems to be called internally by exec, |
|
675 | # choice. But sys.displayhook seems to be called internally by exec, | |
672 | # so I don't see a way around it. We first save the original and then |
|
676 | # so I don't see a way around it. We first save the original and then | |
673 | # overwrite it. |
|
677 | # overwrite it. | |
674 | self.sys_displayhook = sys.displayhook |
|
678 | self.sys_displayhook = sys.displayhook | |
675 | sys.displayhook = self.outputcache |
|
679 | sys.displayhook = self.outputcache | |
676 |
|
680 | |||
677 | # Set user colors (don't do it in the constructor above so that it |
|
681 | # Set user colors (don't do it in the constructor above so that it | |
678 | # doesn't crash if colors option is invalid) |
|
682 | # doesn't crash if colors option is invalid) | |
679 | self.magic_colors(rc.colors) |
|
683 | self.magic_colors(rc.colors) | |
680 |
|
684 | |||
681 | # Set calling of pdb on exceptions |
|
685 | # Set calling of pdb on exceptions | |
682 | self.call_pdb = rc.pdb |
|
686 | self.call_pdb = rc.pdb | |
683 |
|
687 | |||
684 | # Load user aliases |
|
688 | # Load user aliases | |
685 | for alias in rc.alias: |
|
689 | for alias in rc.alias: | |
686 | self.magic_alias(alias) |
|
690 | self.magic_alias(alias) | |
687 | self.hooks.late_startup_hook() |
|
691 | self.hooks.late_startup_hook() | |
688 |
|
692 | |||
689 | batchrun = False |
|
693 | batchrun = False | |
690 | for batchfile in [path(arg) for arg in self.rc.args |
|
694 | for batchfile in [path(arg) for arg in self.rc.args | |
691 | if arg.lower().endswith('.ipy')]: |
|
695 | if arg.lower().endswith('.ipy')]: | |
692 | if not batchfile.isfile(): |
|
696 | if not batchfile.isfile(): | |
693 | print "No such batch file:", batchfile |
|
697 | print "No such batch file:", batchfile | |
694 | continue |
|
698 | continue | |
695 | self.api.runlines(batchfile.text()) |
|
699 | self.api.runlines(batchfile.text()) | |
696 | batchrun = True |
|
700 | batchrun = True | |
697 | if batchrun: |
|
701 | if batchrun: | |
698 | self.exit_now = True |
|
702 | self.exit_now = True | |
699 |
|
703 | |||
700 | def add_builtins(self): |
|
704 | def add_builtins(self): | |
701 | """Store ipython references into the builtin namespace. |
|
705 | """Store ipython references into the builtin namespace. | |
702 |
|
706 | |||
703 | Some parts of ipython operate via builtins injected here, which hold a |
|
707 | Some parts of ipython operate via builtins injected here, which hold a | |
704 | reference to IPython itself.""" |
|
708 | reference to IPython itself.""" | |
705 |
|
709 | |||
706 | # TODO: deprecate all except _ip; 'jobs' should be installed |
|
710 | # TODO: deprecate all except _ip; 'jobs' should be installed | |
707 | # by an extension and the rest are under _ip, ipalias is redundant |
|
711 | # by an extension and the rest are under _ip, ipalias is redundant | |
708 | builtins_new = dict(__IPYTHON__ = self, |
|
712 | builtins_new = dict(__IPYTHON__ = self, | |
709 | ip_set_hook = self.set_hook, |
|
713 | ip_set_hook = self.set_hook, | |
710 | jobs = self.jobs, |
|
714 | jobs = self.jobs, | |
711 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), |
|
715 | ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'), | |
712 | ipalias = wrap_deprecated(self.ipalias), |
|
716 | ipalias = wrap_deprecated(self.ipalias), | |
713 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), |
|
717 | ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'), | |
714 | _ip = self.api |
|
718 | _ip = self.api | |
715 | ) |
|
719 | ) | |
716 | for biname,bival in builtins_new.items(): |
|
720 | for biname,bival in builtins_new.items(): | |
717 | try: |
|
721 | try: | |
718 | # store the orignal value so we can restore it |
|
722 | # store the orignal value so we can restore it | |
719 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
723 | self.builtins_added[biname] = __builtin__.__dict__[biname] | |
720 | except KeyError: |
|
724 | except KeyError: | |
721 | # or mark that it wasn't defined, and we'll just delete it at |
|
725 | # or mark that it wasn't defined, and we'll just delete it at | |
722 | # cleanup |
|
726 | # cleanup | |
723 | self.builtins_added[biname] = Undefined |
|
727 | self.builtins_added[biname] = Undefined | |
724 | __builtin__.__dict__[biname] = bival |
|
728 | __builtin__.__dict__[biname] = bival | |
725 |
|
729 | |||
726 | # Keep in the builtins a flag for when IPython is active. We set it |
|
730 | # Keep in the builtins a flag for when IPython is active. We set it | |
727 | # with setdefault so that multiple nested IPythons don't clobber one |
|
731 | # with setdefault so that multiple nested IPythons don't clobber one | |
728 | # another. Each will increase its value by one upon being activated, |
|
732 | # another. Each will increase its value by one upon being activated, | |
729 | # which also gives us a way to determine the nesting level. |
|
733 | # which also gives us a way to determine the nesting level. | |
730 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
734 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |
731 |
|
735 | |||
732 | def clean_builtins(self): |
|
736 | def clean_builtins(self): | |
733 | """Remove any builtins which might have been added by add_builtins, or |
|
737 | """Remove any builtins which might have been added by add_builtins, or | |
734 | restore overwritten ones to their previous values.""" |
|
738 | restore overwritten ones to their previous values.""" | |
735 | for biname,bival in self.builtins_added.items(): |
|
739 | for biname,bival in self.builtins_added.items(): | |
736 | if bival is Undefined: |
|
740 | if bival is Undefined: | |
737 | del __builtin__.__dict__[biname] |
|
741 | del __builtin__.__dict__[biname] | |
738 | else: |
|
742 | else: | |
739 | __builtin__.__dict__[biname] = bival |
|
743 | __builtin__.__dict__[biname] = bival | |
740 | self.builtins_added.clear() |
|
744 | self.builtins_added.clear() | |
741 |
|
745 | |||
742 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
746 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
743 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
747 | """set_hook(name,hook) -> sets an internal IPython hook. | |
744 |
|
748 | |||
745 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
749 | IPython exposes some of its internal API as user-modifiable hooks. By | |
746 | adding your function to one of these hooks, you can modify IPython's |
|
750 | adding your function to one of these hooks, you can modify IPython's | |
747 | behavior to call at runtime your own routines.""" |
|
751 | behavior to call at runtime your own routines.""" | |
748 |
|
752 | |||
749 | # At some point in the future, this should validate the hook before it |
|
753 | # At some point in the future, this should validate the hook before it | |
750 | # accepts it. Probably at least check that the hook takes the number |
|
754 | # accepts it. Probably at least check that the hook takes the number | |
751 | # of args it's supposed to. |
|
755 | # of args it's supposed to. | |
752 |
|
756 | |||
753 | f = new.instancemethod(hook,self,self.__class__) |
|
757 | f = new.instancemethod(hook,self,self.__class__) | |
754 |
|
758 | |||
755 | # check if the hook is for strdispatcher first |
|
759 | # check if the hook is for strdispatcher first | |
756 | if str_key is not None: |
|
760 | if str_key is not None: | |
757 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
761 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
758 | sdp.add_s(str_key, f, priority ) |
|
762 | sdp.add_s(str_key, f, priority ) | |
759 | self.strdispatchers[name] = sdp |
|
763 | self.strdispatchers[name] = sdp | |
760 | return |
|
764 | return | |
761 | if re_key is not None: |
|
765 | if re_key is not None: | |
762 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
766 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
763 | sdp.add_re(re.compile(re_key), f, priority ) |
|
767 | sdp.add_re(re.compile(re_key), f, priority ) | |
764 | self.strdispatchers[name] = sdp |
|
768 | self.strdispatchers[name] = sdp | |
765 | return |
|
769 | return | |
766 |
|
770 | |||
767 | dp = getattr(self.hooks, name, None) |
|
771 | dp = getattr(self.hooks, name, None) | |
768 | if name not in IPython.hooks.__all__: |
|
772 | if name not in IPython.hooks.__all__: | |
769 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
773 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) | |
770 | if not dp: |
|
774 | if not dp: | |
771 | dp = IPython.hooks.CommandChainDispatcher() |
|
775 | dp = IPython.hooks.CommandChainDispatcher() | |
772 |
|
776 | |||
773 | try: |
|
777 | try: | |
774 | dp.add(f,priority) |
|
778 | dp.add(f,priority) | |
775 | except AttributeError: |
|
779 | except AttributeError: | |
776 | # it was not commandchain, plain old func - replace |
|
780 | # it was not commandchain, plain old func - replace | |
777 | dp = f |
|
781 | dp = f | |
778 |
|
782 | |||
779 | setattr(self.hooks,name, dp) |
|
783 | setattr(self.hooks,name, dp) | |
780 |
|
784 | |||
781 |
|
785 | |||
782 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
786 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) | |
783 |
|
787 | |||
784 | def set_crash_handler(self,crashHandler): |
|
788 | def set_crash_handler(self,crashHandler): | |
785 | """Set the IPython crash handler. |
|
789 | """Set the IPython crash handler. | |
786 |
|
790 | |||
787 | This must be a callable with a signature suitable for use as |
|
791 | This must be a callable with a signature suitable for use as | |
788 | sys.excepthook.""" |
|
792 | sys.excepthook.""" | |
789 |
|
793 | |||
790 | # Install the given crash handler as the Python exception hook |
|
794 | # Install the given crash handler as the Python exception hook | |
791 | sys.excepthook = crashHandler |
|
795 | sys.excepthook = crashHandler | |
792 |
|
796 | |||
793 | # The instance will store a pointer to this, so that runtime code |
|
797 | # The instance will store a pointer to this, so that runtime code | |
794 | # (such as magics) can access it. This is because during the |
|
798 | # (such as magics) can access it. This is because during the | |
795 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
799 | # read-eval loop, it gets temporarily overwritten (to deal with GUI | |
796 | # frameworks). |
|
800 | # frameworks). | |
797 | self.sys_excepthook = sys.excepthook |
|
801 | self.sys_excepthook = sys.excepthook | |
798 |
|
802 | |||
799 |
|
803 | |||
800 | def set_custom_exc(self,exc_tuple,handler): |
|
804 | def set_custom_exc(self,exc_tuple,handler): | |
801 | """set_custom_exc(exc_tuple,handler) |
|
805 | """set_custom_exc(exc_tuple,handler) | |
802 |
|
806 | |||
803 | Set a custom exception handler, which will be called if any of the |
|
807 | Set a custom exception handler, which will be called if any of the | |
804 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
808 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
805 | runcode() method. |
|
809 | runcode() method. | |
806 |
|
810 | |||
807 | Inputs: |
|
811 | Inputs: | |
808 |
|
812 | |||
809 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
813 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
810 | handler for. It is very important that you use a tuple, and NOT A |
|
814 | handler for. It is very important that you use a tuple, and NOT A | |
811 | LIST here, because of the way Python's except statement works. If |
|
815 | LIST here, because of the way Python's except statement works. If | |
812 | you only want to trap a single exception, use a singleton tuple: |
|
816 | you only want to trap a single exception, use a singleton tuple: | |
813 |
|
817 | |||
814 | exc_tuple == (MyCustomException,) |
|
818 | exc_tuple == (MyCustomException,) | |
815 |
|
819 | |||
816 | - handler: this must be defined as a function with the following |
|
820 | - handler: this must be defined as a function with the following | |
817 | basic interface: def my_handler(self,etype,value,tb). |
|
821 | basic interface: def my_handler(self,etype,value,tb). | |
818 |
|
822 | |||
819 | This will be made into an instance method (via new.instancemethod) |
|
823 | This will be made into an instance method (via new.instancemethod) | |
820 | of IPython itself, and it will be called if any of the exceptions |
|
824 | of IPython itself, and it will be called if any of the exceptions | |
821 | listed in the exc_tuple are caught. If the handler is None, an |
|
825 | listed in the exc_tuple are caught. If the handler is None, an | |
822 | internal basic one is used, which just prints basic info. |
|
826 | internal basic one is used, which just prints basic info. | |
823 |
|
827 | |||
824 | WARNING: by putting in your own exception handler into IPython's main |
|
828 | WARNING: by putting in your own exception handler into IPython's main | |
825 | execution loop, you run a very good chance of nasty crashes. This |
|
829 | execution loop, you run a very good chance of nasty crashes. This | |
826 | facility should only be used if you really know what you are doing.""" |
|
830 | facility should only be used if you really know what you are doing.""" | |
827 |
|
831 | |||
828 | assert type(exc_tuple)==type(()) , \ |
|
832 | assert type(exc_tuple)==type(()) , \ | |
829 | "The custom exceptions must be given AS A TUPLE." |
|
833 | "The custom exceptions must be given AS A TUPLE." | |
830 |
|
834 | |||
831 | def dummy_handler(self,etype,value,tb): |
|
835 | def dummy_handler(self,etype,value,tb): | |
832 | print '*** Simple custom exception handler ***' |
|
836 | print '*** Simple custom exception handler ***' | |
833 | print 'Exception type :',etype |
|
837 | print 'Exception type :',etype | |
834 | print 'Exception value:',value |
|
838 | print 'Exception value:',value | |
835 | print 'Traceback :',tb |
|
839 | print 'Traceback :',tb | |
836 | print 'Source code :','\n'.join(self.buffer) |
|
840 | print 'Source code :','\n'.join(self.buffer) | |
837 |
|
841 | |||
838 | if handler is None: handler = dummy_handler |
|
842 | if handler is None: handler = dummy_handler | |
839 |
|
843 | |||
840 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
844 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
841 | self.custom_exceptions = exc_tuple |
|
845 | self.custom_exceptions = exc_tuple | |
842 |
|
846 | |||
843 | def set_custom_completer(self,completer,pos=0): |
|
847 | def set_custom_completer(self,completer,pos=0): | |
844 | """set_custom_completer(completer,pos=0) |
|
848 | """set_custom_completer(completer,pos=0) | |
845 |
|
849 | |||
846 | Adds a new custom completer function. |
|
850 | Adds a new custom completer function. | |
847 |
|
851 | |||
848 | The position argument (defaults to 0) is the index in the completers |
|
852 | The position argument (defaults to 0) is the index in the completers | |
849 | list where you want the completer to be inserted.""" |
|
853 | list where you want the completer to be inserted.""" | |
850 |
|
854 | |||
851 | newcomp = new.instancemethod(completer,self.Completer, |
|
855 | newcomp = new.instancemethod(completer,self.Completer, | |
852 | self.Completer.__class__) |
|
856 | self.Completer.__class__) | |
853 | self.Completer.matchers.insert(pos,newcomp) |
|
857 | self.Completer.matchers.insert(pos,newcomp) | |
854 |
|
858 | |||
855 | def set_completer(self): |
|
859 | def set_completer(self): | |
856 | """reset readline's completer to be our own.""" |
|
860 | """reset readline's completer to be our own.""" | |
857 | self.readline.set_completer(self.Completer.complete) |
|
861 | self.readline.set_completer(self.Completer.complete) | |
858 |
|
862 | |||
859 | def _get_call_pdb(self): |
|
863 | def _get_call_pdb(self): | |
860 | return self._call_pdb |
|
864 | return self._call_pdb | |
861 |
|
865 | |||
862 | def _set_call_pdb(self,val): |
|
866 | def _set_call_pdb(self,val): | |
863 |
|
867 | |||
864 | if val not in (0,1,False,True): |
|
868 | if val not in (0,1,False,True): | |
865 | raise ValueError,'new call_pdb value must be boolean' |
|
869 | raise ValueError,'new call_pdb value must be boolean' | |
866 |
|
870 | |||
867 | # store value in instance |
|
871 | # store value in instance | |
868 | self._call_pdb = val |
|
872 | self._call_pdb = val | |
869 |
|
873 | |||
870 | # notify the actual exception handlers |
|
874 | # notify the actual exception handlers | |
871 | self.InteractiveTB.call_pdb = val |
|
875 | self.InteractiveTB.call_pdb = val | |
872 | if self.isthreaded: |
|
876 | if self.isthreaded: | |
873 | try: |
|
877 | try: | |
874 | self.sys_excepthook.call_pdb = val |
|
878 | self.sys_excepthook.call_pdb = val | |
875 | except: |
|
879 | except: | |
876 | warn('Failed to activate pdb for threaded exception handler') |
|
880 | warn('Failed to activate pdb for threaded exception handler') | |
877 |
|
881 | |||
878 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
882 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
879 | 'Control auto-activation of pdb at exceptions') |
|
883 | 'Control auto-activation of pdb at exceptions') | |
880 |
|
884 | |||
881 |
|
885 | |||
882 | # These special functions get installed in the builtin namespace, to |
|
886 | # These special functions get installed in the builtin namespace, to | |
883 | # provide programmatic (pure python) access to magics, aliases and system |
|
887 | # provide programmatic (pure python) access to magics, aliases and system | |
884 | # calls. This is important for logging, user scripting, and more. |
|
888 | # calls. This is important for logging, user scripting, and more. | |
885 |
|
889 | |||
886 | # We are basically exposing, via normal python functions, the three |
|
890 | # We are basically exposing, via normal python functions, the three | |
887 | # mechanisms in which ipython offers special call modes (magics for |
|
891 | # mechanisms in which ipython offers special call modes (magics for | |
888 | # internal control, aliases for direct system access via pre-selected |
|
892 | # internal control, aliases for direct system access via pre-selected | |
889 | # names, and !cmd for calling arbitrary system commands). |
|
893 | # names, and !cmd for calling arbitrary system commands). | |
890 |
|
894 | |||
891 | def ipmagic(self,arg_s): |
|
895 | def ipmagic(self,arg_s): | |
892 | """Call a magic function by name. |
|
896 | """Call a magic function by name. | |
893 |
|
897 | |||
894 | Input: a string containing the name of the magic function to call and any |
|
898 | Input: a string containing the name of the magic function to call and any | |
895 | additional arguments to be passed to the magic. |
|
899 | additional arguments to be passed to the magic. | |
896 |
|
900 | |||
897 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
901 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython | |
898 | prompt: |
|
902 | prompt: | |
899 |
|
903 | |||
900 | In[1]: %name -opt foo bar |
|
904 | In[1]: %name -opt foo bar | |
901 |
|
905 | |||
902 | To call a magic without arguments, simply use ipmagic('name'). |
|
906 | To call a magic without arguments, simply use ipmagic('name'). | |
903 |
|
907 | |||
904 | This provides a proper Python function to call IPython's magics in any |
|
908 | This provides a proper Python function to call IPython's magics in any | |
905 | valid Python code you can type at the interpreter, including loops and |
|
909 | valid Python code you can type at the interpreter, including loops and | |
906 | compound statements. It is added by IPython to the Python builtin |
|
910 | compound statements. It is added by IPython to the Python builtin | |
907 | namespace upon initialization.""" |
|
911 | namespace upon initialization.""" | |
908 |
|
912 | |||
909 | args = arg_s.split(' ',1) |
|
913 | args = arg_s.split(' ',1) | |
910 | magic_name = args[0] |
|
914 | magic_name = args[0] | |
911 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
915 | magic_name = magic_name.lstrip(self.ESC_MAGIC) | |
912 |
|
916 | |||
913 | try: |
|
917 | try: | |
914 | magic_args = args[1] |
|
918 | magic_args = args[1] | |
915 | except IndexError: |
|
919 | except IndexError: | |
916 | magic_args = '' |
|
920 | magic_args = '' | |
917 | fn = getattr(self,'magic_'+magic_name,None) |
|
921 | fn = getattr(self,'magic_'+magic_name,None) | |
918 | if fn is None: |
|
922 | if fn is None: | |
919 | error("Magic function `%s` not found." % magic_name) |
|
923 | error("Magic function `%s` not found." % magic_name) | |
920 | else: |
|
924 | else: | |
921 | magic_args = self.var_expand(magic_args,1) |
|
925 | magic_args = self.var_expand(magic_args,1) | |
922 | return fn(magic_args) |
|
926 | return fn(magic_args) | |
923 |
|
927 | |||
924 | def ipalias(self,arg_s): |
|
928 | def ipalias(self,arg_s): | |
925 | """Call an alias by name. |
|
929 | """Call an alias by name. | |
926 |
|
930 | |||
927 | Input: a string containing the name of the alias to call and any |
|
931 | Input: a string containing the name of the alias to call and any | |
928 | additional arguments to be passed to the magic. |
|
932 | additional arguments to be passed to the magic. | |
929 |
|
933 | |||
930 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
934 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |
931 | prompt: |
|
935 | prompt: | |
932 |
|
936 | |||
933 | In[1]: name -opt foo bar |
|
937 | In[1]: name -opt foo bar | |
934 |
|
938 | |||
935 | To call an alias without arguments, simply use ipalias('name'). |
|
939 | To call an alias without arguments, simply use ipalias('name'). | |
936 |
|
940 | |||
937 | This provides a proper Python function to call IPython's aliases in any |
|
941 | This provides a proper Python function to call IPython's aliases in any | |
938 | valid Python code you can type at the interpreter, including loops and |
|
942 | valid Python code you can type at the interpreter, including loops and | |
939 | compound statements. It is added by IPython to the Python builtin |
|
943 | compound statements. It is added by IPython to the Python builtin | |
940 | namespace upon initialization.""" |
|
944 | namespace upon initialization.""" | |
941 |
|
945 | |||
942 | args = arg_s.split(' ',1) |
|
946 | args = arg_s.split(' ',1) | |
943 | alias_name = args[0] |
|
947 | alias_name = args[0] | |
944 | try: |
|
948 | try: | |
945 | alias_args = args[1] |
|
949 | alias_args = args[1] | |
946 | except IndexError: |
|
950 | except IndexError: | |
947 | alias_args = '' |
|
951 | alias_args = '' | |
948 | if alias_name in self.alias_table: |
|
952 | if alias_name in self.alias_table: | |
949 | self.call_alias(alias_name,alias_args) |
|
953 | self.call_alias(alias_name,alias_args) | |
950 | else: |
|
954 | else: | |
951 | error("Alias `%s` not found." % alias_name) |
|
955 | error("Alias `%s` not found." % alias_name) | |
952 |
|
956 | |||
953 | def ipsystem(self,arg_s): |
|
957 | def ipsystem(self,arg_s): | |
954 | """Make a system call, using IPython.""" |
|
958 | """Make a system call, using IPython.""" | |
955 |
|
959 | |||
956 | self.system(arg_s) |
|
960 | self.system(arg_s) | |
957 |
|
961 | |||
958 | def complete(self,text): |
|
962 | def complete(self,text): | |
959 | """Return a sorted list of all possible completions on text. |
|
963 | """Return a sorted list of all possible completions on text. | |
960 |
|
964 | |||
961 | Inputs: |
|
965 | Inputs: | |
962 |
|
966 | |||
963 | - text: a string of text to be completed on. |
|
967 | - text: a string of text to be completed on. | |
964 |
|
968 | |||
965 | This is a wrapper around the completion mechanism, similar to what |
|
969 | This is a wrapper around the completion mechanism, similar to what | |
966 | readline does at the command line when the TAB key is hit. By |
|
970 | readline does at the command line when the TAB key is hit. By | |
967 | exposing it as a method, it can be used by other non-readline |
|
971 | exposing it as a method, it can be used by other non-readline | |
968 | environments (such as GUIs) for text completion. |
|
972 | environments (such as GUIs) for text completion. | |
969 |
|
973 | |||
970 | Simple usage example: |
|
974 | Simple usage example: | |
971 |
|
975 | |||
972 | In [1]: x = 'hello' |
|
976 | In [1]: x = 'hello' | |
973 |
|
977 | |||
974 | In [2]: __IP.complete('x.l') |
|
978 | In [2]: __IP.complete('x.l') | |
975 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
979 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" | |
976 |
|
980 | |||
977 | complete = self.Completer.complete |
|
981 | complete = self.Completer.complete | |
978 | state = 0 |
|
982 | state = 0 | |
979 | # use a dict so we get unique keys, since ipyhton's multiple |
|
983 | # use a dict so we get unique keys, since ipyhton's multiple | |
980 | # completers can return duplicates. When we make 2.4 a requirement, |
|
984 | # completers can return duplicates. When we make 2.4 a requirement, | |
981 | # start using sets instead, which are faster. |
|
985 | # start using sets instead, which are faster. | |
982 | comps = {} |
|
986 | comps = {} | |
983 | while True: |
|
987 | while True: | |
984 | newcomp = complete(text,state,line_buffer=text) |
|
988 | newcomp = complete(text,state,line_buffer=text) | |
985 | if newcomp is None: |
|
989 | if newcomp is None: | |
986 | break |
|
990 | break | |
987 | comps[newcomp] = 1 |
|
991 | comps[newcomp] = 1 | |
988 | state += 1 |
|
992 | state += 1 | |
989 | outcomps = comps.keys() |
|
993 | outcomps = comps.keys() | |
990 | outcomps.sort() |
|
994 | outcomps.sort() | |
991 | return outcomps |
|
995 | return outcomps | |
992 |
|
996 | |||
993 | def set_completer_frame(self, frame=None): |
|
997 | def set_completer_frame(self, frame=None): | |
994 | if frame: |
|
998 | if frame: | |
995 | self.Completer.namespace = frame.f_locals |
|
999 | self.Completer.namespace = frame.f_locals | |
996 | self.Completer.global_namespace = frame.f_globals |
|
1000 | self.Completer.global_namespace = frame.f_globals | |
997 | else: |
|
1001 | else: | |
998 | self.Completer.namespace = self.user_ns |
|
1002 | self.Completer.namespace = self.user_ns | |
999 | self.Completer.global_namespace = self.user_global_ns |
|
1003 | self.Completer.global_namespace = self.user_global_ns | |
1000 |
|
1004 | |||
1001 | def init_auto_alias(self): |
|
1005 | def init_auto_alias(self): | |
1002 | """Define some aliases automatically. |
|
1006 | """Define some aliases automatically. | |
1003 |
|
1007 | |||
1004 | These are ALL parameter-less aliases""" |
|
1008 | These are ALL parameter-less aliases""" | |
1005 |
|
1009 | |||
1006 | for alias,cmd in self.auto_alias: |
|
1010 | for alias,cmd in self.auto_alias: | |
1007 | self.alias_table[alias] = (0,cmd) |
|
1011 | self.alias_table[alias] = (0,cmd) | |
1008 |
|
1012 | |||
1009 | def alias_table_validate(self,verbose=0): |
|
1013 | def alias_table_validate(self,verbose=0): | |
1010 | """Update information about the alias table. |
|
1014 | """Update information about the alias table. | |
1011 |
|
1015 | |||
1012 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1016 | In particular, make sure no Python keywords/builtins are in it.""" | |
1013 |
|
1017 | |||
1014 | no_alias = self.no_alias |
|
1018 | no_alias = self.no_alias | |
1015 | for k in self.alias_table.keys(): |
|
1019 | for k in self.alias_table.keys(): | |
1016 | if k in no_alias: |
|
1020 | if k in no_alias: | |
1017 | del self.alias_table[k] |
|
1021 | del self.alias_table[k] | |
1018 | if verbose: |
|
1022 | if verbose: | |
1019 | print ("Deleting alias <%s>, it's a Python " |
|
1023 | print ("Deleting alias <%s>, it's a Python " | |
1020 | "keyword or builtin." % k) |
|
1024 | "keyword or builtin." % k) | |
1021 |
|
1025 | |||
1022 | def set_autoindent(self,value=None): |
|
1026 | def set_autoindent(self,value=None): | |
1023 | """Set the autoindent flag, checking for readline support. |
|
1027 | """Set the autoindent flag, checking for readline support. | |
1024 |
|
1028 | |||
1025 | If called with no arguments, it acts as a toggle.""" |
|
1029 | If called with no arguments, it acts as a toggle.""" | |
1026 |
|
1030 | |||
1027 | if not self.has_readline: |
|
1031 | if not self.has_readline: | |
1028 | if os.name == 'posix': |
|
1032 | if os.name == 'posix': | |
1029 | warn("The auto-indent feature requires the readline library") |
|
1033 | warn("The auto-indent feature requires the readline library") | |
1030 | self.autoindent = 0 |
|
1034 | self.autoindent = 0 | |
1031 | return |
|
1035 | return | |
1032 | if value is None: |
|
1036 | if value is None: | |
1033 | self.autoindent = not self.autoindent |
|
1037 | self.autoindent = not self.autoindent | |
1034 | else: |
|
1038 | else: | |
1035 | self.autoindent = value |
|
1039 | self.autoindent = value | |
1036 |
|
1040 | |||
1037 | def rc_set_toggle(self,rc_field,value=None): |
|
1041 | def rc_set_toggle(self,rc_field,value=None): | |
1038 | """Set or toggle a field in IPython's rc config. structure. |
|
1042 | """Set or toggle a field in IPython's rc config. structure. | |
1039 |
|
1043 | |||
1040 | If called with no arguments, it acts as a toggle. |
|
1044 | If called with no arguments, it acts as a toggle. | |
1041 |
|
1045 | |||
1042 | If called with a non-existent field, the resulting AttributeError |
|
1046 | If called with a non-existent field, the resulting AttributeError | |
1043 | exception will propagate out.""" |
|
1047 | exception will propagate out.""" | |
1044 |
|
1048 | |||
1045 | rc_val = getattr(self.rc,rc_field) |
|
1049 | rc_val = getattr(self.rc,rc_field) | |
1046 | if value is None: |
|
1050 | if value is None: | |
1047 | value = not rc_val |
|
1051 | value = not rc_val | |
1048 | setattr(self.rc,rc_field,value) |
|
1052 | setattr(self.rc,rc_field,value) | |
1049 |
|
1053 | |||
1050 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1054 | def user_setup(self,ipythondir,rc_suffix,mode='install'): | |
1051 | """Install the user configuration directory. |
|
1055 | """Install the user configuration directory. | |
1052 |
|
1056 | |||
1053 | Can be called when running for the first time or to upgrade the user's |
|
1057 | Can be called when running for the first time or to upgrade the user's | |
1054 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1058 | .ipython/ directory with the mode parameter. Valid modes are 'install' | |
1055 | and 'upgrade'.""" |
|
1059 | and 'upgrade'.""" | |
1056 |
|
1060 | |||
1057 | def wait(): |
|
1061 | def wait(): | |
1058 | try: |
|
1062 | try: | |
1059 | raw_input("Please press <RETURN> to start IPython.") |
|
1063 | raw_input("Please press <RETURN> to start IPython.") | |
1060 | except EOFError: |
|
1064 | except EOFError: | |
1061 | print >> Term.cout |
|
1065 | print >> Term.cout | |
1062 | print '*'*70 |
|
1066 | print '*'*70 | |
1063 |
|
1067 | |||
1064 | cwd = os.getcwd() # remember where we started |
|
1068 | cwd = os.getcwd() # remember where we started | |
1065 | glb = glob.glob |
|
1069 | glb = glob.glob | |
1066 | print '*'*70 |
|
1070 | print '*'*70 | |
1067 | if mode == 'install': |
|
1071 | if mode == 'install': | |
1068 | print \ |
|
1072 | print \ | |
1069 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1073 | """Welcome to IPython. I will try to create a personal configuration directory | |
1070 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1074 | where you can customize many aspects of IPython's functionality in:\n""" | |
1071 | else: |
|
1075 | else: | |
1072 | print 'I am going to upgrade your configuration in:' |
|
1076 | print 'I am going to upgrade your configuration in:' | |
1073 |
|
1077 | |||
1074 | print ipythondir |
|
1078 | print ipythondir | |
1075 |
|
1079 | |||
1076 | rcdirend = os.path.join('IPython','UserConfig') |
|
1080 | rcdirend = os.path.join('IPython','UserConfig') | |
1077 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1081 | cfg = lambda d: os.path.join(d,rcdirend) | |
1078 | try: |
|
1082 | try: | |
1079 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1083 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] | |
1080 | except IOError: |
|
1084 | except IOError: | |
1081 | warning = """ |
|
1085 | warning = """ | |
1082 | Installation error. IPython's directory was not found. |
|
1086 | Installation error. IPython's directory was not found. | |
1083 |
|
1087 | |||
1084 | Check the following: |
|
1088 | Check the following: | |
1085 |
|
1089 | |||
1086 | The ipython/IPython directory should be in a directory belonging to your |
|
1090 | The ipython/IPython directory should be in a directory belonging to your | |
1087 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1091 | PYTHONPATH environment variable (that is, it should be in a directory | |
1088 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1092 | belonging to sys.path). You can copy it explicitly there or just link to it. | |
1089 |
|
1093 | |||
1090 | IPython will proceed with builtin defaults. |
|
1094 | IPython will proceed with builtin defaults. | |
1091 | """ |
|
1095 | """ | |
1092 | warn(warning) |
|
1096 | warn(warning) | |
1093 | wait() |
|
1097 | wait() | |
1094 | return |
|
1098 | return | |
1095 |
|
1099 | |||
1096 | if mode == 'install': |
|
1100 | if mode == 'install': | |
1097 | try: |
|
1101 | try: | |
1098 | shutil.copytree(rcdir,ipythondir) |
|
1102 | shutil.copytree(rcdir,ipythondir) | |
1099 | os.chdir(ipythondir) |
|
1103 | os.chdir(ipythondir) | |
1100 | rc_files = glb("ipythonrc*") |
|
1104 | rc_files = glb("ipythonrc*") | |
1101 | for rc_file in rc_files: |
|
1105 | for rc_file in rc_files: | |
1102 | os.rename(rc_file,rc_file+rc_suffix) |
|
1106 | os.rename(rc_file,rc_file+rc_suffix) | |
1103 | except: |
|
1107 | except: | |
1104 | warning = """ |
|
1108 | warning = """ | |
1105 |
|
1109 | |||
1106 | There was a problem with the installation: |
|
1110 | There was a problem with the installation: | |
1107 | %s |
|
1111 | %s | |
1108 | Try to correct it or contact the developers if you think it's a bug. |
|
1112 | Try to correct it or contact the developers if you think it's a bug. | |
1109 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1113 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] | |
1110 | warn(warning) |
|
1114 | warn(warning) | |
1111 | wait() |
|
1115 | wait() | |
1112 | return |
|
1116 | return | |
1113 |
|
1117 | |||
1114 | elif mode == 'upgrade': |
|
1118 | elif mode == 'upgrade': | |
1115 | try: |
|
1119 | try: | |
1116 | os.chdir(ipythondir) |
|
1120 | os.chdir(ipythondir) | |
1117 | except: |
|
1121 | except: | |
1118 | print """ |
|
1122 | print """ | |
1119 | Can not upgrade: changing to directory %s failed. Details: |
|
1123 | Can not upgrade: changing to directory %s failed. Details: | |
1120 | %s |
|
1124 | %s | |
1121 | """ % (ipythondir,sys.exc_info()[1]) |
|
1125 | """ % (ipythondir,sys.exc_info()[1]) | |
1122 | wait() |
|
1126 | wait() | |
1123 | return |
|
1127 | return | |
1124 | else: |
|
1128 | else: | |
1125 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1129 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) | |
1126 | for new_full_path in sources: |
|
1130 | for new_full_path in sources: | |
1127 | new_filename = os.path.basename(new_full_path) |
|
1131 | new_filename = os.path.basename(new_full_path) | |
1128 | if new_filename.startswith('ipythonrc'): |
|
1132 | if new_filename.startswith('ipythonrc'): | |
1129 | new_filename = new_filename + rc_suffix |
|
1133 | new_filename = new_filename + rc_suffix | |
1130 | # The config directory should only contain files, skip any |
|
1134 | # The config directory should only contain files, skip any | |
1131 | # directories which may be there (like CVS) |
|
1135 | # directories which may be there (like CVS) | |
1132 | if os.path.isdir(new_full_path): |
|
1136 | if os.path.isdir(new_full_path): | |
1133 | continue |
|
1137 | continue | |
1134 | if os.path.exists(new_filename): |
|
1138 | if os.path.exists(new_filename): | |
1135 | old_file = new_filename+'.old' |
|
1139 | old_file = new_filename+'.old' | |
1136 | if os.path.exists(old_file): |
|
1140 | if os.path.exists(old_file): | |
1137 | os.remove(old_file) |
|
1141 | os.remove(old_file) | |
1138 | os.rename(new_filename,old_file) |
|
1142 | os.rename(new_filename,old_file) | |
1139 | shutil.copy(new_full_path,new_filename) |
|
1143 | shutil.copy(new_full_path,new_filename) | |
1140 | else: |
|
1144 | else: | |
1141 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1145 | raise ValueError,'unrecognized mode for install:',`mode` | |
1142 |
|
1146 | |||
1143 | # Fix line-endings to those native to each platform in the config |
|
1147 | # Fix line-endings to those native to each platform in the config | |
1144 | # directory. |
|
1148 | # directory. | |
1145 | try: |
|
1149 | try: | |
1146 | os.chdir(ipythondir) |
|
1150 | os.chdir(ipythondir) | |
1147 | except: |
|
1151 | except: | |
1148 | print """ |
|
1152 | print """ | |
1149 | Problem: changing to directory %s failed. |
|
1153 | Problem: changing to directory %s failed. | |
1150 | Details: |
|
1154 | Details: | |
1151 | %s |
|
1155 | %s | |
1152 |
|
1156 | |||
1153 | Some configuration files may have incorrect line endings. This should not |
|
1157 | Some configuration files may have incorrect line endings. This should not | |
1154 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1158 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) | |
1155 | wait() |
|
1159 | wait() | |
1156 | else: |
|
1160 | else: | |
1157 | for fname in glb('ipythonrc*'): |
|
1161 | for fname in glb('ipythonrc*'): | |
1158 | try: |
|
1162 | try: | |
1159 | native_line_ends(fname,backup=0) |
|
1163 | native_line_ends(fname,backup=0) | |
1160 | except IOError: |
|
1164 | except IOError: | |
1161 | pass |
|
1165 | pass | |
1162 |
|
1166 | |||
1163 | if mode == 'install': |
|
1167 | if mode == 'install': | |
1164 | print """ |
|
1168 | print """ | |
1165 | Successful installation! |
|
1169 | Successful installation! | |
1166 |
|
1170 | |||
1167 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1171 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the | |
1168 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1172 | IPython manual (there are both HTML and PDF versions supplied with the | |
1169 | distribution) to make sure that your system environment is properly configured |
|
1173 | distribution) to make sure that your system environment is properly configured | |
1170 | to take advantage of IPython's features. |
|
1174 | to take advantage of IPython's features. | |
1171 |
|
1175 | |||
1172 | Important note: the configuration system has changed! The old system is |
|
1176 | Important note: the configuration system has changed! The old system is | |
1173 | still in place, but its setting may be partly overridden by the settings in |
|
1177 | still in place, but its setting may be partly overridden by the settings in | |
1174 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1178 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file | |
1175 | if some of the new settings bother you. |
|
1179 | if some of the new settings bother you. | |
1176 |
|
1180 | |||
1177 | """ |
|
1181 | """ | |
1178 | else: |
|
1182 | else: | |
1179 | print """ |
|
1183 | print """ | |
1180 | Successful upgrade! |
|
1184 | Successful upgrade! | |
1181 |
|
1185 | |||
1182 | All files in your directory: |
|
1186 | All files in your directory: | |
1183 | %(ipythondir)s |
|
1187 | %(ipythondir)s | |
1184 | which would have been overwritten by the upgrade were backed up with a .old |
|
1188 | which would have been overwritten by the upgrade were backed up with a .old | |
1185 | extension. If you had made particular customizations in those files you may |
|
1189 | extension. If you had made particular customizations in those files you may | |
1186 | want to merge them back into the new files.""" % locals() |
|
1190 | want to merge them back into the new files.""" % locals() | |
1187 | wait() |
|
1191 | wait() | |
1188 | os.chdir(cwd) |
|
1192 | os.chdir(cwd) | |
1189 | # end user_setup() |
|
1193 | # end user_setup() | |
1190 |
|
1194 | |||
1191 | def atexit_operations(self): |
|
1195 | def atexit_operations(self): | |
1192 | """This will be executed at the time of exit. |
|
1196 | """This will be executed at the time of exit. | |
1193 |
|
1197 | |||
1194 | Saving of persistent data should be performed here. """ |
|
1198 | Saving of persistent data should be performed here. """ | |
1195 |
|
1199 | |||
1196 | #print '*** IPython exit cleanup ***' # dbg |
|
1200 | #print '*** IPython exit cleanup ***' # dbg | |
1197 | # input history |
|
1201 | # input history | |
1198 | self.savehist() |
|
1202 | self.savehist() | |
1199 |
|
1203 | |||
1200 | # Cleanup all tempfiles left around |
|
1204 | # Cleanup all tempfiles left around | |
1201 | for tfile in self.tempfiles: |
|
1205 | for tfile in self.tempfiles: | |
1202 | try: |
|
1206 | try: | |
1203 | os.unlink(tfile) |
|
1207 | os.unlink(tfile) | |
1204 | except OSError: |
|
1208 | except OSError: | |
1205 | pass |
|
1209 | pass | |
1206 |
|
1210 | |||
1207 | self.hooks.shutdown_hook() |
|
1211 | self.hooks.shutdown_hook() | |
1208 |
|
1212 | |||
1209 | def savehist(self): |
|
1213 | def savehist(self): | |
1210 | """Save input history to a file (via readline library).""" |
|
1214 | """Save input history to a file (via readline library).""" | |
1211 | try: |
|
1215 | try: | |
1212 | self.readline.write_history_file(self.histfile) |
|
1216 | self.readline.write_history_file(self.histfile) | |
1213 | except: |
|
1217 | except: | |
1214 | print 'Unable to save IPython command history to file: ' + \ |
|
1218 | print 'Unable to save IPython command history to file: ' + \ | |
1215 | `self.histfile` |
|
1219 | `self.histfile` | |
1216 |
|
1220 | |||
1217 | def reloadhist(self): |
|
1221 | def reloadhist(self): | |
1218 | """Reload the input history from disk file.""" |
|
1222 | """Reload the input history from disk file.""" | |
1219 |
|
1223 | |||
1220 | if self.has_readline: |
|
1224 | if self.has_readline: | |
1221 | self.readline.clear_history() |
|
1225 | self.readline.clear_history() | |
1222 | self.readline.read_history_file(self.shell.histfile) |
|
1226 | self.readline.read_history_file(self.shell.histfile) | |
1223 |
|
1227 | |||
1224 | def history_saving_wrapper(self, func): |
|
1228 | def history_saving_wrapper(self, func): | |
1225 | """ Wrap func for readline history saving |
|
1229 | """ Wrap func for readline history saving | |
1226 |
|
1230 | |||
1227 | Convert func into callable that saves & restores |
|
1231 | Convert func into callable that saves & restores | |
1228 | history around the call """ |
|
1232 | history around the call """ | |
1229 |
|
1233 | |||
1230 | if not self.has_readline: |
|
1234 | if not self.has_readline: | |
1231 | return func |
|
1235 | return func | |
1232 |
|
1236 | |||
1233 | def wrapper(): |
|
1237 | def wrapper(): | |
1234 | self.savehist() |
|
1238 | self.savehist() | |
1235 | try: |
|
1239 | try: | |
1236 | func() |
|
1240 | func() | |
1237 | finally: |
|
1241 | finally: | |
1238 | readline.read_history_file(self.histfile) |
|
1242 | readline.read_history_file(self.histfile) | |
1239 | return wrapper |
|
1243 | return wrapper | |
1240 |
|
1244 | |||
1241 |
|
1245 | |||
1242 | def pre_readline(self): |
|
1246 | def pre_readline(self): | |
1243 | """readline hook to be used at the start of each line. |
|
1247 | """readline hook to be used at the start of each line. | |
1244 |
|
1248 | |||
1245 | Currently it handles auto-indent only.""" |
|
1249 | Currently it handles auto-indent only.""" | |
1246 |
|
1250 | |||
1247 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1251 | #debugx('self.indent_current_nsp','pre_readline:') | |
1248 |
|
1252 | |||
1249 | if self.rl_do_indent: |
|
1253 | if self.rl_do_indent: | |
1250 | self.readline.insert_text(self.indent_current_str()) |
|
1254 | self.readline.insert_text(self.indent_current_str()) | |
1251 | if self.rl_next_input is not None: |
|
1255 | if self.rl_next_input is not None: | |
1252 | self.readline.insert_text(self.rl_next_input) |
|
1256 | self.readline.insert_text(self.rl_next_input) | |
1253 | self.rl_next_input = None |
|
1257 | self.rl_next_input = None | |
1254 |
|
1258 | |||
1255 | def init_readline(self): |
|
1259 | def init_readline(self): | |
1256 | """Command history completion/saving/reloading.""" |
|
1260 | """Command history completion/saving/reloading.""" | |
1257 |
|
1261 | |||
1258 | import IPython.rlineimpl as readline |
|
1262 | import IPython.rlineimpl as readline | |
1259 | if not readline.have_readline: |
|
1263 | if not readline.have_readline: | |
1260 | self.has_readline = 0 |
|
1264 | self.has_readline = 0 | |
1261 | self.readline = None |
|
1265 | self.readline = None | |
1262 | # no point in bugging windows users with this every time: |
|
1266 | # no point in bugging windows users with this every time: | |
1263 | warn('Readline services not available on this platform.') |
|
1267 | warn('Readline services not available on this platform.') | |
1264 | else: |
|
1268 | else: | |
1265 | sys.modules['readline'] = readline |
|
1269 | sys.modules['readline'] = readline | |
1266 | import atexit |
|
1270 | import atexit | |
1267 | from IPython.completer import IPCompleter |
|
1271 | from IPython.completer import IPCompleter | |
1268 | self.Completer = IPCompleter(self, |
|
1272 | self.Completer = IPCompleter(self, | |
1269 | self.user_ns, |
|
1273 | self.user_ns, | |
1270 | self.user_global_ns, |
|
1274 | self.user_global_ns, | |
1271 | self.rc.readline_omit__names, |
|
1275 | self.rc.readline_omit__names, | |
1272 | self.alias_table) |
|
1276 | self.alias_table) | |
1273 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1277 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1274 | self.strdispatchers['complete_command'] = sdisp |
|
1278 | self.strdispatchers['complete_command'] = sdisp | |
1275 | self.Completer.custom_completers = sdisp |
|
1279 | self.Completer.custom_completers = sdisp | |
1276 | # Platform-specific configuration |
|
1280 | # Platform-specific configuration | |
1277 | if os.name == 'nt': |
|
1281 | if os.name == 'nt': | |
1278 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1282 | self.readline_startup_hook = readline.set_pre_input_hook | |
1279 | else: |
|
1283 | else: | |
1280 | self.readline_startup_hook = readline.set_startup_hook |
|
1284 | self.readline_startup_hook = readline.set_startup_hook | |
1281 |
|
1285 | |||
1282 | # Load user's initrc file (readline config) |
|
1286 | # Load user's initrc file (readline config) | |
1283 | inputrc_name = os.environ.get('INPUTRC') |
|
1287 | inputrc_name = os.environ.get('INPUTRC') | |
1284 | if inputrc_name is None: |
|
1288 | if inputrc_name is None: | |
1285 | home_dir = get_home_dir() |
|
1289 | home_dir = get_home_dir() | |
1286 | if home_dir is not None: |
|
1290 | if home_dir is not None: | |
1287 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1291 | inputrc_name = os.path.join(home_dir,'.inputrc') | |
1288 | if os.path.isfile(inputrc_name): |
|
1292 | if os.path.isfile(inputrc_name): | |
1289 | try: |
|
1293 | try: | |
1290 | readline.read_init_file(inputrc_name) |
|
1294 | readline.read_init_file(inputrc_name) | |
1291 | except: |
|
1295 | except: | |
1292 | warn('Problems reading readline initialization file <%s>' |
|
1296 | warn('Problems reading readline initialization file <%s>' | |
1293 | % inputrc_name) |
|
1297 | % inputrc_name) | |
1294 |
|
1298 | |||
1295 | self.has_readline = 1 |
|
1299 | self.has_readline = 1 | |
1296 | self.readline = readline |
|
1300 | self.readline = readline | |
1297 | # save this in sys so embedded copies can restore it properly |
|
1301 | # save this in sys so embedded copies can restore it properly | |
1298 | sys.ipcompleter = self.Completer.complete |
|
1302 | sys.ipcompleter = self.Completer.complete | |
1299 | self.set_completer() |
|
1303 | self.set_completer() | |
1300 |
|
1304 | |||
1301 | # Configure readline according to user's prefs |
|
1305 | # Configure readline according to user's prefs | |
1302 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1306 | for rlcommand in self.rc.readline_parse_and_bind: | |
1303 | readline.parse_and_bind(rlcommand) |
|
1307 | readline.parse_and_bind(rlcommand) | |
1304 |
|
1308 | |||
1305 | # remove some chars from the delimiters list |
|
1309 | # remove some chars from the delimiters list | |
1306 | delims = readline.get_completer_delims() |
|
1310 | delims = readline.get_completer_delims() | |
1307 | delims = delims.translate(string._idmap, |
|
1311 | delims = delims.translate(string._idmap, | |
1308 | self.rc.readline_remove_delims) |
|
1312 | self.rc.readline_remove_delims) | |
1309 | readline.set_completer_delims(delims) |
|
1313 | readline.set_completer_delims(delims) | |
1310 | # otherwise we end up with a monster history after a while: |
|
1314 | # otherwise we end up with a monster history after a while: | |
1311 | readline.set_history_length(1000) |
|
1315 | readline.set_history_length(1000) | |
1312 | try: |
|
1316 | try: | |
1313 | #print '*** Reading readline history' # dbg |
|
1317 | #print '*** Reading readline history' # dbg | |
1314 | readline.read_history_file(self.histfile) |
|
1318 | readline.read_history_file(self.histfile) | |
1315 | except IOError: |
|
1319 | except IOError: | |
1316 | pass # It doesn't exist yet. |
|
1320 | pass # It doesn't exist yet. | |
1317 |
|
1321 | |||
1318 | atexit.register(self.atexit_operations) |
|
1322 | atexit.register(self.atexit_operations) | |
1319 | del atexit |
|
1323 | del atexit | |
1320 |
|
1324 | |||
1321 | # Configure auto-indent for all platforms |
|
1325 | # Configure auto-indent for all platforms | |
1322 | self.set_autoindent(self.rc.autoindent) |
|
1326 | self.set_autoindent(self.rc.autoindent) | |
1323 |
|
1327 | |||
1324 | def ask_yes_no(self,prompt,default=True): |
|
1328 | def ask_yes_no(self,prompt,default=True): | |
1325 | if self.rc.quiet: |
|
1329 | if self.rc.quiet: | |
1326 | return True |
|
1330 | return True | |
1327 | return ask_yes_no(prompt,default) |
|
1331 | return ask_yes_no(prompt,default) | |
1328 |
|
1332 | |||
1329 | def _should_recompile(self,e): |
|
1333 | def _should_recompile(self,e): | |
1330 | """Utility routine for edit_syntax_error""" |
|
1334 | """Utility routine for edit_syntax_error""" | |
1331 |
|
1335 | |||
1332 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1336 | if e.filename in ('<ipython console>','<input>','<string>', | |
1333 | '<console>','<BackgroundJob compilation>', |
|
1337 | '<console>','<BackgroundJob compilation>', | |
1334 | None): |
|
1338 | None): | |
1335 |
|
1339 | |||
1336 | return False |
|
1340 | return False | |
1337 | try: |
|
1341 | try: | |
1338 | if (self.rc.autoedit_syntax and |
|
1342 | if (self.rc.autoedit_syntax and | |
1339 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1343 | not self.ask_yes_no('Return to editor to correct syntax error? ' | |
1340 | '[Y/n] ','y')): |
|
1344 | '[Y/n] ','y')): | |
1341 | return False |
|
1345 | return False | |
1342 | except EOFError: |
|
1346 | except EOFError: | |
1343 | return False |
|
1347 | return False | |
1344 |
|
1348 | |||
1345 | def int0(x): |
|
1349 | def int0(x): | |
1346 | try: |
|
1350 | try: | |
1347 | return int(x) |
|
1351 | return int(x) | |
1348 | except TypeError: |
|
1352 | except TypeError: | |
1349 | return 0 |
|
1353 | return 0 | |
1350 | # always pass integer line and offset values to editor hook |
|
1354 | # always pass integer line and offset values to editor hook | |
1351 | self.hooks.fix_error_editor(e.filename, |
|
1355 | self.hooks.fix_error_editor(e.filename, | |
1352 | int0(e.lineno),int0(e.offset),e.msg) |
|
1356 | int0(e.lineno),int0(e.offset),e.msg) | |
1353 | return True |
|
1357 | return True | |
1354 |
|
1358 | |||
1355 | def edit_syntax_error(self): |
|
1359 | def edit_syntax_error(self): | |
1356 | """The bottom half of the syntax error handler called in the main loop. |
|
1360 | """The bottom half of the syntax error handler called in the main loop. | |
1357 |
|
1361 | |||
1358 | Loop until syntax error is fixed or user cancels. |
|
1362 | Loop until syntax error is fixed or user cancels. | |
1359 | """ |
|
1363 | """ | |
1360 |
|
1364 | |||
1361 | while self.SyntaxTB.last_syntax_error: |
|
1365 | while self.SyntaxTB.last_syntax_error: | |
1362 | # copy and clear last_syntax_error |
|
1366 | # copy and clear last_syntax_error | |
1363 | err = self.SyntaxTB.clear_err_state() |
|
1367 | err = self.SyntaxTB.clear_err_state() | |
1364 | if not self._should_recompile(err): |
|
1368 | if not self._should_recompile(err): | |
1365 | return |
|
1369 | return | |
1366 | try: |
|
1370 | try: | |
1367 | # may set last_syntax_error again if a SyntaxError is raised |
|
1371 | # may set last_syntax_error again if a SyntaxError is raised | |
1368 | self.safe_execfile(err.filename,self.user_ns) |
|
1372 | self.safe_execfile(err.filename,self.user_ns) | |
1369 | except: |
|
1373 | except: | |
1370 | self.showtraceback() |
|
1374 | self.showtraceback() | |
1371 | else: |
|
1375 | else: | |
1372 | try: |
|
1376 | try: | |
1373 | f = file(err.filename) |
|
1377 | f = file(err.filename) | |
1374 | try: |
|
1378 | try: | |
1375 | sys.displayhook(f.read()) |
|
1379 | sys.displayhook(f.read()) | |
1376 | finally: |
|
1380 | finally: | |
1377 | f.close() |
|
1381 | f.close() | |
1378 | except: |
|
1382 | except: | |
1379 | self.showtraceback() |
|
1383 | self.showtraceback() | |
1380 |
|
1384 | |||
1381 | def showsyntaxerror(self, filename=None): |
|
1385 | def showsyntaxerror(self, filename=None): | |
1382 | """Display the syntax error that just occurred. |
|
1386 | """Display the syntax error that just occurred. | |
1383 |
|
1387 | |||
1384 | This doesn't display a stack trace because there isn't one. |
|
1388 | This doesn't display a stack trace because there isn't one. | |
1385 |
|
1389 | |||
1386 | If a filename is given, it is stuffed in the exception instead |
|
1390 | If a filename is given, it is stuffed in the exception instead | |
1387 | of what was there before (because Python's parser always uses |
|
1391 | of what was there before (because Python's parser always uses | |
1388 | "<string>" when reading from a string). |
|
1392 | "<string>" when reading from a string). | |
1389 | """ |
|
1393 | """ | |
1390 | etype, value, last_traceback = sys.exc_info() |
|
1394 | etype, value, last_traceback = sys.exc_info() | |
1391 |
|
1395 | |||
1392 | # See note about these variables in showtraceback() below |
|
1396 | # See note about these variables in showtraceback() below | |
1393 | sys.last_type = etype |
|
1397 | sys.last_type = etype | |
1394 | sys.last_value = value |
|
1398 | sys.last_value = value | |
1395 | sys.last_traceback = last_traceback |
|
1399 | sys.last_traceback = last_traceback | |
1396 |
|
1400 | |||
1397 | if filename and etype is SyntaxError: |
|
1401 | if filename and etype is SyntaxError: | |
1398 | # Work hard to stuff the correct filename in the exception |
|
1402 | # Work hard to stuff the correct filename in the exception | |
1399 | try: |
|
1403 | try: | |
1400 | msg, (dummy_filename, lineno, offset, line) = value |
|
1404 | msg, (dummy_filename, lineno, offset, line) = value | |
1401 | except: |
|
1405 | except: | |
1402 | # Not the format we expect; leave it alone |
|
1406 | # Not the format we expect; leave it alone | |
1403 | pass |
|
1407 | pass | |
1404 | else: |
|
1408 | else: | |
1405 | # Stuff in the right filename |
|
1409 | # Stuff in the right filename | |
1406 | try: |
|
1410 | try: | |
1407 | # Assume SyntaxError is a class exception |
|
1411 | # Assume SyntaxError is a class exception | |
1408 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1412 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1409 | except: |
|
1413 | except: | |
1410 | # If that failed, assume SyntaxError is a string |
|
1414 | # If that failed, assume SyntaxError is a string | |
1411 | value = msg, (filename, lineno, offset, line) |
|
1415 | value = msg, (filename, lineno, offset, line) | |
1412 | self.SyntaxTB(etype,value,[]) |
|
1416 | self.SyntaxTB(etype,value,[]) | |
1413 |
|
1417 | |||
1414 | def debugger(self,force=False): |
|
1418 | def debugger(self,force=False): | |
1415 | """Call the pydb/pdb debugger. |
|
1419 | """Call the pydb/pdb debugger. | |
1416 |
|
1420 | |||
1417 | Keywords: |
|
1421 | Keywords: | |
1418 |
|
1422 | |||
1419 | - force(False): by default, this routine checks the instance call_pdb |
|
1423 | - force(False): by default, this routine checks the instance call_pdb | |
1420 | flag and does not actually invoke the debugger if the flag is false. |
|
1424 | flag and does not actually invoke the debugger if the flag is false. | |
1421 | The 'force' option forces the debugger to activate even if the flag |
|
1425 | The 'force' option forces the debugger to activate even if the flag | |
1422 | is false. |
|
1426 | is false. | |
1423 | """ |
|
1427 | """ | |
1424 |
|
1428 | |||
1425 | if not (force or self.call_pdb): |
|
1429 | if not (force or self.call_pdb): | |
1426 | return |
|
1430 | return | |
1427 |
|
1431 | |||
1428 | if not hasattr(sys,'last_traceback'): |
|
1432 | if not hasattr(sys,'last_traceback'): | |
1429 | error('No traceback has been produced, nothing to debug.') |
|
1433 | error('No traceback has been produced, nothing to debug.') | |
1430 | return |
|
1434 | return | |
1431 |
|
1435 | |||
1432 | # use pydb if available |
|
1436 | # use pydb if available | |
1433 | if Debugger.has_pydb: |
|
1437 | if Debugger.has_pydb: | |
1434 | from pydb import pm |
|
1438 | from pydb import pm | |
1435 | else: |
|
1439 | else: | |
1436 | # fallback to our internal debugger |
|
1440 | # fallback to our internal debugger | |
1437 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1441 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
1438 | self.history_saving_wrapper(pm)() |
|
1442 | self.history_saving_wrapper(pm)() | |
1439 |
|
1443 | |||
1440 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1444 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): | |
1441 | """Display the exception that just occurred. |
|
1445 | """Display the exception that just occurred. | |
1442 |
|
1446 | |||
1443 | If nothing is known about the exception, this is the method which |
|
1447 | If nothing is known about the exception, this is the method which | |
1444 | should be used throughout the code for presenting user tracebacks, |
|
1448 | should be used throughout the code for presenting user tracebacks, | |
1445 | rather than directly invoking the InteractiveTB object. |
|
1449 | rather than directly invoking the InteractiveTB object. | |
1446 |
|
1450 | |||
1447 | A specific showsyntaxerror() also exists, but this method can take |
|
1451 | A specific showsyntaxerror() also exists, but this method can take | |
1448 | care of calling it if needed, so unless you are explicitly catching a |
|
1452 | care of calling it if needed, so unless you are explicitly catching a | |
1449 | SyntaxError exception, don't try to analyze the stack manually and |
|
1453 | SyntaxError exception, don't try to analyze the stack manually and | |
1450 | simply call this method.""" |
|
1454 | simply call this method.""" | |
1451 |
|
1455 | |||
1452 |
|
1456 | |||
1453 | # Though this won't be called by syntax errors in the input line, |
|
1457 | # Though this won't be called by syntax errors in the input line, | |
1454 | # there may be SyntaxError cases whith imported code. |
|
1458 | # there may be SyntaxError cases whith imported code. | |
1455 |
|
1459 | |||
1456 |
|
1460 | |||
1457 | if exc_tuple is None: |
|
1461 | if exc_tuple is None: | |
1458 | etype, value, tb = sys.exc_info() |
|
1462 | etype, value, tb = sys.exc_info() | |
1459 | else: |
|
1463 | else: | |
1460 | etype, value, tb = exc_tuple |
|
1464 | etype, value, tb = exc_tuple | |
1461 |
|
1465 | |||
1462 | if etype is SyntaxError: |
|
1466 | if etype is SyntaxError: | |
1463 | self.showsyntaxerror(filename) |
|
1467 | self.showsyntaxerror(filename) | |
1464 | else: |
|
1468 | else: | |
1465 | # WARNING: these variables are somewhat deprecated and not |
|
1469 | # WARNING: these variables are somewhat deprecated and not | |
1466 | # necessarily safe to use in a threaded environment, but tools |
|
1470 | # necessarily safe to use in a threaded environment, but tools | |
1467 | # like pdb depend on their existence, so let's set them. If we |
|
1471 | # like pdb depend on their existence, so let's set them. If we | |
1468 | # find problems in the field, we'll need to revisit their use. |
|
1472 | # find problems in the field, we'll need to revisit their use. | |
1469 | sys.last_type = etype |
|
1473 | sys.last_type = etype | |
1470 | sys.last_value = value |
|
1474 | sys.last_value = value | |
1471 | sys.last_traceback = tb |
|
1475 | sys.last_traceback = tb | |
1472 |
|
1476 | |||
1473 | if etype in self.custom_exceptions: |
|
1477 | if etype in self.custom_exceptions: | |
1474 | self.CustomTB(etype,value,tb) |
|
1478 | self.CustomTB(etype,value,tb) | |
1475 | else: |
|
1479 | else: | |
1476 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1480 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) | |
1477 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1481 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1478 | # pdb mucks up readline, fix it back |
|
1482 | # pdb mucks up readline, fix it back | |
1479 | self.set_completer() |
|
1483 | self.set_completer() | |
1480 |
|
1484 | |||
1481 |
|
1485 | |||
1482 | def mainloop(self,banner=None): |
|
1486 | def mainloop(self,banner=None): | |
1483 | """Creates the local namespace and starts the mainloop. |
|
1487 | """Creates the local namespace and starts the mainloop. | |
1484 |
|
1488 | |||
1485 | If an optional banner argument is given, it will override the |
|
1489 | If an optional banner argument is given, it will override the | |
1486 | internally created default banner.""" |
|
1490 | internally created default banner.""" | |
1487 |
|
1491 | |||
1488 | if self.rc.c: # Emulate Python's -c option |
|
1492 | if self.rc.c: # Emulate Python's -c option | |
1489 | self.exec_init_cmd() |
|
1493 | self.exec_init_cmd() | |
1490 | if banner is None: |
|
1494 | if banner is None: | |
1491 | if not self.rc.banner: |
|
1495 | if not self.rc.banner: | |
1492 | banner = '' |
|
1496 | banner = '' | |
1493 | # banner is string? Use it directly! |
|
1497 | # banner is string? Use it directly! | |
1494 | elif isinstance(self.rc.banner,basestring): |
|
1498 | elif isinstance(self.rc.banner,basestring): | |
1495 | banner = self.rc.banner |
|
1499 | banner = self.rc.banner | |
1496 | else: |
|
1500 | else: | |
1497 | banner = self.BANNER+self.banner2 |
|
1501 | banner = self.BANNER+self.banner2 | |
1498 |
|
1502 | |||
1499 | self.interact(banner) |
|
1503 | self.interact(banner) | |
1500 |
|
1504 | |||
1501 | def exec_init_cmd(self): |
|
1505 | def exec_init_cmd(self): | |
1502 | """Execute a command given at the command line. |
|
1506 | """Execute a command given at the command line. | |
1503 |
|
1507 | |||
1504 | This emulates Python's -c option.""" |
|
1508 | This emulates Python's -c option.""" | |
1505 |
|
1509 | |||
1506 | #sys.argv = ['-c'] |
|
1510 | #sys.argv = ['-c'] | |
1507 | self.push(self.prefilter(self.rc.c, False)) |
|
1511 | self.push(self.prefilter(self.rc.c, False)) | |
1508 | self.exit_now = True |
|
1512 | self.exit_now = True | |
1509 |
|
1513 | |||
1510 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1514 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): | |
1511 | """Embeds IPython into a running python program. |
|
1515 | """Embeds IPython into a running python program. | |
1512 |
|
1516 | |||
1513 | Input: |
|
1517 | Input: | |
1514 |
|
1518 | |||
1515 | - header: An optional header message can be specified. |
|
1519 | - header: An optional header message can be specified. | |
1516 |
|
1520 | |||
1517 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1521 | - local_ns, global_ns: working namespaces. If given as None, the | |
1518 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1522 | IPython-initialized one is updated with __main__.__dict__, so that | |
1519 | program variables become visible but user-specific configuration |
|
1523 | program variables become visible but user-specific configuration | |
1520 | remains possible. |
|
1524 | remains possible. | |
1521 |
|
1525 | |||
1522 | - stack_depth: specifies how many levels in the stack to go to |
|
1526 | - stack_depth: specifies how many levels in the stack to go to | |
1523 | looking for namespaces (when local_ns and global_ns are None). This |
|
1527 | looking for namespaces (when local_ns and global_ns are None). This | |
1524 | allows an intermediate caller to make sure that this function gets |
|
1528 | allows an intermediate caller to make sure that this function gets | |
1525 | the namespace from the intended level in the stack. By default (0) |
|
1529 | the namespace from the intended level in the stack. By default (0) | |
1526 | it will get its locals and globals from the immediate caller. |
|
1530 | it will get its locals and globals from the immediate caller. | |
1527 |
|
1531 | |||
1528 | Warning: it's possible to use this in a program which is being run by |
|
1532 | Warning: it's possible to use this in a program which is being run by | |
1529 | IPython itself (via %run), but some funny things will happen (a few |
|
1533 | IPython itself (via %run), but some funny things will happen (a few | |
1530 | globals get overwritten). In the future this will be cleaned up, as |
|
1534 | globals get overwritten). In the future this will be cleaned up, as | |
1531 | there is no fundamental reason why it can't work perfectly.""" |
|
1535 | there is no fundamental reason why it can't work perfectly.""" | |
1532 |
|
1536 | |||
1533 | # Get locals and globals from caller |
|
1537 | # Get locals and globals from caller | |
1534 | if local_ns is None or global_ns is None: |
|
1538 | if local_ns is None or global_ns is None: | |
1535 | call_frame = sys._getframe(stack_depth).f_back |
|
1539 | call_frame = sys._getframe(stack_depth).f_back | |
1536 |
|
1540 | |||
1537 | if local_ns is None: |
|
1541 | if local_ns is None: | |
1538 | local_ns = call_frame.f_locals |
|
1542 | local_ns = call_frame.f_locals | |
1539 | if global_ns is None: |
|
1543 | if global_ns is None: | |
1540 | global_ns = call_frame.f_globals |
|
1544 | global_ns = call_frame.f_globals | |
1541 |
|
1545 | |||
1542 | # Update namespaces and fire up interpreter |
|
1546 | # Update namespaces and fire up interpreter | |
1543 |
|
1547 | |||
1544 | # The global one is easy, we can just throw it in |
|
1548 | # The global one is easy, we can just throw it in | |
1545 | self.user_global_ns = global_ns |
|
1549 | self.user_global_ns = global_ns | |
1546 |
|
1550 | |||
1547 | # but the user/local one is tricky: ipython needs it to store internal |
|
1551 | # but the user/local one is tricky: ipython needs it to store internal | |
1548 | # data, but we also need the locals. We'll copy locals in the user |
|
1552 | # data, but we also need the locals. We'll copy locals in the user | |
1549 | # one, but will track what got copied so we can delete them at exit. |
|
1553 | # one, but will track what got copied so we can delete them at exit. | |
1550 | # This is so that a later embedded call doesn't see locals from a |
|
1554 | # This is so that a later embedded call doesn't see locals from a | |
1551 | # previous call (which most likely existed in a separate scope). |
|
1555 | # previous call (which most likely existed in a separate scope). | |
1552 | local_varnames = local_ns.keys() |
|
1556 | local_varnames = local_ns.keys() | |
1553 | self.user_ns.update(local_ns) |
|
1557 | self.user_ns.update(local_ns) | |
1554 |
|
1558 | |||
1555 | # Patch for global embedding to make sure that things don't overwrite |
|
1559 | # Patch for global embedding to make sure that things don't overwrite | |
1556 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1560 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
1557 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1561 | # FIXME. Test this a bit more carefully (the if.. is new) | |
1558 | if local_ns is None and global_ns is None: |
|
1562 | if local_ns is None and global_ns is None: | |
1559 | self.user_global_ns.update(__main__.__dict__) |
|
1563 | self.user_global_ns.update(__main__.__dict__) | |
1560 |
|
1564 | |||
1561 | # make sure the tab-completer has the correct frame information, so it |
|
1565 | # make sure the tab-completer has the correct frame information, so it | |
1562 | # actually completes using the frame's locals/globals |
|
1566 | # actually completes using the frame's locals/globals | |
1563 | self.set_completer_frame() |
|
1567 | self.set_completer_frame() | |
1564 |
|
1568 | |||
1565 | # before activating the interactive mode, we need to make sure that |
|
1569 | # before activating the interactive mode, we need to make sure that | |
1566 | # all names in the builtin namespace needed by ipython point to |
|
1570 | # all names in the builtin namespace needed by ipython point to | |
1567 | # ourselves, and not to other instances. |
|
1571 | # ourselves, and not to other instances. | |
1568 | self.add_builtins() |
|
1572 | self.add_builtins() | |
1569 |
|
1573 | |||
1570 | self.interact(header) |
|
1574 | self.interact(header) | |
1571 |
|
1575 | |||
1572 | # now, purge out the user namespace from anything we might have added |
|
1576 | # now, purge out the user namespace from anything we might have added | |
1573 | # from the caller's local namespace |
|
1577 | # from the caller's local namespace | |
1574 | delvar = self.user_ns.pop |
|
1578 | delvar = self.user_ns.pop | |
1575 | for var in local_varnames: |
|
1579 | for var in local_varnames: | |
1576 | delvar(var,None) |
|
1580 | delvar(var,None) | |
1577 | # and clean builtins we may have overridden |
|
1581 | # and clean builtins we may have overridden | |
1578 | self.clean_builtins() |
|
1582 | self.clean_builtins() | |
1579 |
|
1583 | |||
1580 | def interact(self, banner=None): |
|
1584 | def interact(self, banner=None): | |
1581 | """Closely emulate the interactive Python console. |
|
1585 | """Closely emulate the interactive Python console. | |
1582 |
|
1586 | |||
1583 | The optional banner argument specify the banner to print |
|
1587 | The optional banner argument specify the banner to print | |
1584 | before the first interaction; by default it prints a banner |
|
1588 | before the first interaction; by default it prints a banner | |
1585 | similar to the one printed by the real Python interpreter, |
|
1589 | similar to the one printed by the real Python interpreter, | |
1586 | followed by the current class name in parentheses (so as not |
|
1590 | followed by the current class name in parentheses (so as not | |
1587 | to confuse this with the real interpreter -- since it's so |
|
1591 | to confuse this with the real interpreter -- since it's so | |
1588 | close!). |
|
1592 | close!). | |
1589 |
|
1593 | |||
1590 | """ |
|
1594 | """ | |
1591 |
|
1595 | |||
1592 | if self.exit_now: |
|
1596 | if self.exit_now: | |
1593 | # batch run -> do not interact |
|
1597 | # batch run -> do not interact | |
1594 | return |
|
1598 | return | |
1595 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1599 | cprt = 'Type "copyright", "credits" or "license" for more information.' | |
1596 | if banner is None: |
|
1600 | if banner is None: | |
1597 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1601 | self.write("Python %s on %s\n%s\n(%s)\n" % | |
1598 | (sys.version, sys.platform, cprt, |
|
1602 | (sys.version, sys.platform, cprt, | |
1599 | self.__class__.__name__)) |
|
1603 | self.__class__.__name__)) | |
1600 | else: |
|
1604 | else: | |
1601 | self.write(banner) |
|
1605 | self.write(banner) | |
1602 |
|
1606 | |||
1603 | more = 0 |
|
1607 | more = 0 | |
1604 |
|
1608 | |||
1605 | # Mark activity in the builtins |
|
1609 | # Mark activity in the builtins | |
1606 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1610 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1607 |
|
1611 | |||
1608 | if readline.have_readline: |
|
1612 | if readline.have_readline: | |
1609 | self.readline_startup_hook(self.pre_readline) |
|
1613 | self.readline_startup_hook(self.pre_readline) | |
1610 | # exit_now is set by a call to %Exit or %Quit |
|
1614 | # exit_now is set by a call to %Exit or %Quit | |
1611 |
|
1615 | |||
1612 | while not self.exit_now: |
|
1616 | while not self.exit_now: | |
1613 | if more: |
|
1617 | if more: | |
1614 | prompt = self.hooks.generate_prompt(True) |
|
1618 | prompt = self.hooks.generate_prompt(True) | |
1615 | if self.autoindent: |
|
1619 | if self.autoindent: | |
1616 | self.rl_do_indent = True |
|
1620 | self.rl_do_indent = True | |
1617 |
|
1621 | |||
1618 | else: |
|
1622 | else: | |
1619 | prompt = self.hooks.generate_prompt(False) |
|
1623 | prompt = self.hooks.generate_prompt(False) | |
1620 | try: |
|
1624 | try: | |
1621 | line = self.raw_input(prompt,more) |
|
1625 | line = self.raw_input(prompt,more) | |
1622 | if self.exit_now: |
|
1626 | if self.exit_now: | |
1623 | # quick exit on sys.std[in|out] close |
|
1627 | # quick exit on sys.std[in|out] close | |
1624 | break |
|
1628 | break | |
1625 | if self.autoindent: |
|
1629 | if self.autoindent: | |
1626 | self.rl_do_indent = False |
|
1630 | self.rl_do_indent = False | |
1627 |
|
1631 | |||
1628 | except KeyboardInterrupt: |
|
1632 | except KeyboardInterrupt: | |
1629 | self.write('\nKeyboardInterrupt\n') |
|
1633 | self.write('\nKeyboardInterrupt\n') | |
1630 | self.resetbuffer() |
|
1634 | self.resetbuffer() | |
1631 | # keep cache in sync with the prompt counter: |
|
1635 | # keep cache in sync with the prompt counter: | |
1632 | self.outputcache.prompt_count -= 1 |
|
1636 | self.outputcache.prompt_count -= 1 | |
1633 |
|
1637 | |||
1634 | if self.autoindent: |
|
1638 | if self.autoindent: | |
1635 | self.indent_current_nsp = 0 |
|
1639 | self.indent_current_nsp = 0 | |
1636 | more = 0 |
|
1640 | more = 0 | |
1637 | except EOFError: |
|
1641 | except EOFError: | |
1638 | if self.autoindent: |
|
1642 | if self.autoindent: | |
1639 | self.rl_do_indent = False |
|
1643 | self.rl_do_indent = False | |
1640 | self.readline_startup_hook(None) |
|
1644 | self.readline_startup_hook(None) | |
1641 | self.write('\n') |
|
1645 | self.write('\n') | |
1642 | self.exit() |
|
1646 | self.exit() | |
1643 | except bdb.BdbQuit: |
|
1647 | except bdb.BdbQuit: | |
1644 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1648 | warn('The Python debugger has exited with a BdbQuit exception.\n' | |
1645 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1649 | 'Because of how pdb handles the stack, it is impossible\n' | |
1646 | 'for IPython to properly format this particular exception.\n' |
|
1650 | 'for IPython to properly format this particular exception.\n' | |
1647 | 'IPython will resume normal operation.') |
|
1651 | 'IPython will resume normal operation.') | |
1648 | except: |
|
1652 | except: | |
1649 | # exceptions here are VERY RARE, but they can be triggered |
|
1653 | # exceptions here are VERY RARE, but they can be triggered | |
1650 | # asynchronously by signal handlers, for example. |
|
1654 | # asynchronously by signal handlers, for example. | |
1651 | self.showtraceback() |
|
1655 | self.showtraceback() | |
1652 | else: |
|
1656 | else: | |
1653 | more = self.push(line) |
|
1657 | more = self.push(line) | |
1654 | if (self.SyntaxTB.last_syntax_error and |
|
1658 | if (self.SyntaxTB.last_syntax_error and | |
1655 | self.rc.autoedit_syntax): |
|
1659 | self.rc.autoedit_syntax): | |
1656 | self.edit_syntax_error() |
|
1660 | self.edit_syntax_error() | |
1657 |
|
1661 | |||
1658 | # We are off again... |
|
1662 | # We are off again... | |
1659 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1663 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1660 |
|
1664 | |||
1661 | def excepthook(self, etype, value, tb): |
|
1665 | def excepthook(self, etype, value, tb): | |
1662 | """One more defense for GUI apps that call sys.excepthook. |
|
1666 | """One more defense for GUI apps that call sys.excepthook. | |
1663 |
|
1667 | |||
1664 | GUI frameworks like wxPython trap exceptions and call |
|
1668 | GUI frameworks like wxPython trap exceptions and call | |
1665 | sys.excepthook themselves. I guess this is a feature that |
|
1669 | sys.excepthook themselves. I guess this is a feature that | |
1666 | enables them to keep running after exceptions that would |
|
1670 | enables them to keep running after exceptions that would | |
1667 | otherwise kill their mainloop. This is a bother for IPython |
|
1671 | otherwise kill their mainloop. This is a bother for IPython | |
1668 | which excepts to catch all of the program exceptions with a try: |
|
1672 | which excepts to catch all of the program exceptions with a try: | |
1669 | except: statement. |
|
1673 | except: statement. | |
1670 |
|
1674 | |||
1671 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1675 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1672 | any app directly invokes sys.excepthook, it will look to the user like |
|
1676 | any app directly invokes sys.excepthook, it will look to the user like | |
1673 | IPython crashed. In order to work around this, we can disable the |
|
1677 | IPython crashed. In order to work around this, we can disable the | |
1674 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1678 | CrashHandler and replace it with this excepthook instead, which prints a | |
1675 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1679 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1676 | call sys.excepthook will generate a regular-looking exception from |
|
1680 | call sys.excepthook will generate a regular-looking exception from | |
1677 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1681 | IPython, and the CrashHandler will only be triggered by real IPython | |
1678 | crashes. |
|
1682 | crashes. | |
1679 |
|
1683 | |||
1680 | This hook should be used sparingly, only in places which are not likely |
|
1684 | This hook should be used sparingly, only in places which are not likely | |
1681 | to be true IPython errors. |
|
1685 | to be true IPython errors. | |
1682 | """ |
|
1686 | """ | |
1683 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1687 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1684 |
|
1688 | |||
1685 | def expand_aliases(self,fn,rest): |
|
1689 | def expand_aliases(self,fn,rest): | |
1686 | """ Expand multiple levels of aliases: |
|
1690 | """ Expand multiple levels of aliases: | |
1687 |
|
1691 | |||
1688 | if: |
|
1692 | if: | |
1689 |
|
1693 | |||
1690 | alias foo bar /tmp |
|
1694 | alias foo bar /tmp | |
1691 | alias baz foo |
|
1695 | alias baz foo | |
1692 |
|
1696 | |||
1693 | then: |
|
1697 | then: | |
1694 |
|
1698 | |||
1695 | baz huhhahhei -> bar /tmp huhhahhei |
|
1699 | baz huhhahhei -> bar /tmp huhhahhei | |
1696 |
|
1700 | |||
1697 | """ |
|
1701 | """ | |
1698 | line = fn + " " + rest |
|
1702 | line = fn + " " + rest | |
1699 |
|
1703 | |||
1700 | done = Set() |
|
1704 | done = Set() | |
1701 | while 1: |
|
1705 | while 1: | |
1702 | pre,fn,rest = prefilter.splitUserInput(line, |
|
1706 | pre,fn,rest = prefilter.splitUserInput(line, | |
1703 | prefilter.shell_line_split) |
|
1707 | prefilter.shell_line_split) | |
1704 | if fn in self.alias_table: |
|
1708 | if fn in self.alias_table: | |
1705 | if fn in done: |
|
1709 | if fn in done: | |
1706 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1710 | warn("Cyclic alias definition, repeated '%s'" % fn) | |
1707 | return "" |
|
1711 | return "" | |
1708 | done.add(fn) |
|
1712 | done.add(fn) | |
1709 |
|
1713 | |||
1710 | l2 = self.transform_alias(fn,rest) |
|
1714 | l2 = self.transform_alias(fn,rest) | |
1711 | # dir -> dir |
|
1715 | # dir -> dir | |
1712 | # print "alias",line, "->",l2 #dbg |
|
1716 | # print "alias",line, "->",l2 #dbg | |
1713 | if l2 == line: |
|
1717 | if l2 == line: | |
1714 | break |
|
1718 | break | |
1715 | # ls -> ls -F should not recurse forever |
|
1719 | # ls -> ls -F should not recurse forever | |
1716 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1720 | if l2.split(None,1)[0] == line.split(None,1)[0]: | |
1717 | line = l2 |
|
1721 | line = l2 | |
1718 | break |
|
1722 | break | |
1719 |
|
1723 | |||
1720 | line=l2 |
|
1724 | line=l2 | |
1721 |
|
1725 | |||
1722 |
|
1726 | |||
1723 | # print "al expand to",line #dbg |
|
1727 | # print "al expand to",line #dbg | |
1724 | else: |
|
1728 | else: | |
1725 | break |
|
1729 | break | |
1726 |
|
1730 | |||
1727 | return line |
|
1731 | return line | |
1728 |
|
1732 | |||
1729 | def transform_alias(self, alias,rest=''): |
|
1733 | def transform_alias(self, alias,rest=''): | |
1730 | """ Transform alias to system command string. |
|
1734 | """ Transform alias to system command string. | |
1731 | """ |
|
1735 | """ | |
1732 | nargs,cmd = self.alias_table[alias] |
|
1736 | nargs,cmd = self.alias_table[alias] | |
1733 | if ' ' in cmd and os.path.isfile(cmd): |
|
1737 | if ' ' in cmd and os.path.isfile(cmd): | |
1734 | cmd = '"%s"' % cmd |
|
1738 | cmd = '"%s"' % cmd | |
1735 |
|
1739 | |||
1736 | # Expand the %l special to be the user's input line |
|
1740 | # Expand the %l special to be the user's input line | |
1737 | if cmd.find('%l') >= 0: |
|
1741 | if cmd.find('%l') >= 0: | |
1738 | cmd = cmd.replace('%l',rest) |
|
1742 | cmd = cmd.replace('%l',rest) | |
1739 | rest = '' |
|
1743 | rest = '' | |
1740 | if nargs==0: |
|
1744 | if nargs==0: | |
1741 | # Simple, argument-less aliases |
|
1745 | # Simple, argument-less aliases | |
1742 | cmd = '%s %s' % (cmd,rest) |
|
1746 | cmd = '%s %s' % (cmd,rest) | |
1743 | else: |
|
1747 | else: | |
1744 | # Handle aliases with positional arguments |
|
1748 | # Handle aliases with positional arguments | |
1745 | args = rest.split(None,nargs) |
|
1749 | args = rest.split(None,nargs) | |
1746 | if len(args)< nargs: |
|
1750 | if len(args)< nargs: | |
1747 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1751 | error('Alias <%s> requires %s arguments, %s given.' % | |
1748 | (alias,nargs,len(args))) |
|
1752 | (alias,nargs,len(args))) | |
1749 | return None |
|
1753 | return None | |
1750 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1754 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) | |
1751 | # Now call the macro, evaluating in the user's namespace |
|
1755 | # Now call the macro, evaluating in the user's namespace | |
1752 | #print 'new command: <%r>' % cmd # dbg |
|
1756 | #print 'new command: <%r>' % cmd # dbg | |
1753 | return cmd |
|
1757 | return cmd | |
1754 |
|
1758 | |||
1755 | def call_alias(self,alias,rest=''): |
|
1759 | def call_alias(self,alias,rest=''): | |
1756 | """Call an alias given its name and the rest of the line. |
|
1760 | """Call an alias given its name and the rest of the line. | |
1757 |
|
1761 | |||
1758 | This is only used to provide backwards compatibility for users of |
|
1762 | This is only used to provide backwards compatibility for users of | |
1759 | ipalias(), use of which is not recommended for anymore.""" |
|
1763 | ipalias(), use of which is not recommended for anymore.""" | |
1760 |
|
1764 | |||
1761 | # Now call the macro, evaluating in the user's namespace |
|
1765 | # Now call the macro, evaluating in the user's namespace | |
1762 | cmd = self.transform_alias(alias, rest) |
|
1766 | cmd = self.transform_alias(alias, rest) | |
1763 | try: |
|
1767 | try: | |
1764 | self.system(cmd) |
|
1768 | self.system(cmd) | |
1765 | except: |
|
1769 | except: | |
1766 | self.showtraceback() |
|
1770 | self.showtraceback() | |
1767 |
|
1771 | |||
1768 | def indent_current_str(self): |
|
1772 | def indent_current_str(self): | |
1769 | """return the current level of indentation as a string""" |
|
1773 | """return the current level of indentation as a string""" | |
1770 | return self.indent_current_nsp * ' ' |
|
1774 | return self.indent_current_nsp * ' ' | |
1771 |
|
1775 | |||
1772 | def autoindent_update(self,line): |
|
1776 | def autoindent_update(self,line): | |
1773 | """Keep track of the indent level.""" |
|
1777 | """Keep track of the indent level.""" | |
1774 |
|
1778 | |||
1775 | #debugx('line') |
|
1779 | #debugx('line') | |
1776 | #debugx('self.indent_current_nsp') |
|
1780 | #debugx('self.indent_current_nsp') | |
1777 | if self.autoindent: |
|
1781 | if self.autoindent: | |
1778 | if line: |
|
1782 | if line: | |
1779 | inisp = num_ini_spaces(line) |
|
1783 | inisp = num_ini_spaces(line) | |
1780 | if inisp < self.indent_current_nsp: |
|
1784 | if inisp < self.indent_current_nsp: | |
1781 | self.indent_current_nsp = inisp |
|
1785 | self.indent_current_nsp = inisp | |
1782 |
|
1786 | |||
1783 | if line[-1] == ':': |
|
1787 | if line[-1] == ':': | |
1784 | self.indent_current_nsp += 4 |
|
1788 | self.indent_current_nsp += 4 | |
1785 | elif dedent_re.match(line): |
|
1789 | elif dedent_re.match(line): | |
1786 | self.indent_current_nsp -= 4 |
|
1790 | self.indent_current_nsp -= 4 | |
1787 | else: |
|
1791 | else: | |
1788 | self.indent_current_nsp = 0 |
|
1792 | self.indent_current_nsp = 0 | |
1789 | def runlines(self,lines): |
|
1793 | def runlines(self,lines): | |
1790 | """Run a string of one or more lines of source. |
|
1794 | """Run a string of one or more lines of source. | |
1791 |
|
1795 | |||
1792 | This method is capable of running a string containing multiple source |
|
1796 | This method is capable of running a string containing multiple source | |
1793 | lines, as if they had been entered at the IPython prompt. Since it |
|
1797 | lines, as if they had been entered at the IPython prompt. Since it | |
1794 | exposes IPython's processing machinery, the given strings can contain |
|
1798 | exposes IPython's processing machinery, the given strings can contain | |
1795 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1799 | magic calls (%magic), special shell access (!cmd), etc.""" | |
1796 |
|
1800 | |||
1797 | # We must start with a clean buffer, in case this is run from an |
|
1801 | # We must start with a clean buffer, in case this is run from an | |
1798 | # interactive IPython session (via a magic, for example). |
|
1802 | # interactive IPython session (via a magic, for example). | |
1799 | self.resetbuffer() |
|
1803 | self.resetbuffer() | |
1800 | lines = lines.split('\n') |
|
1804 | lines = lines.split('\n') | |
1801 | more = 0 |
|
1805 | more = 0 | |
1802 |
|
1806 | |||
1803 | for line in lines: |
|
1807 | for line in lines: | |
1804 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1808 | # skip blank lines so we don't mess up the prompt counter, but do | |
1805 | # NOT skip even a blank line if we are in a code block (more is |
|
1809 | # NOT skip even a blank line if we are in a code block (more is | |
1806 | # true) |
|
1810 | # true) | |
1807 |
|
1811 | |||
1808 |
|
1812 | |||
1809 | if line or more: |
|
1813 | if line or more: | |
1810 | # push to raw history, so hist line numbers stay in sync |
|
1814 | # push to raw history, so hist line numbers stay in sync | |
1811 | self.input_hist_raw.append("# " + line + "\n") |
|
1815 | self.input_hist_raw.append("# " + line + "\n") | |
1812 | more = self.push(self.prefilter(line,more)) |
|
1816 | more = self.push(self.prefilter(line,more)) | |
1813 | # IPython's runsource returns None if there was an error |
|
1817 | # IPython's runsource returns None if there was an error | |
1814 | # compiling the code. This allows us to stop processing right |
|
1818 | # compiling the code. This allows us to stop processing right | |
1815 | # away, so the user gets the error message at the right place. |
|
1819 | # away, so the user gets the error message at the right place. | |
1816 | if more is None: |
|
1820 | if more is None: | |
1817 | break |
|
1821 | break | |
1818 | # final newline in case the input didn't have it, so that the code |
|
1822 | # final newline in case the input didn't have it, so that the code | |
1819 | # actually does get executed |
|
1823 | # actually does get executed | |
1820 | if more: |
|
1824 | if more: | |
1821 | self.push('\n') |
|
1825 | self.push('\n') | |
1822 |
|
1826 | |||
1823 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1827 | def runsource(self, source, filename='<input>', symbol='single'): | |
1824 | """Compile and run some source in the interpreter. |
|
1828 | """Compile and run some source in the interpreter. | |
1825 |
|
1829 | |||
1826 | Arguments are as for compile_command(). |
|
1830 | Arguments are as for compile_command(). | |
1827 |
|
1831 | |||
1828 | One several things can happen: |
|
1832 | One several things can happen: | |
1829 |
|
1833 | |||
1830 | 1) The input is incorrect; compile_command() raised an |
|
1834 | 1) The input is incorrect; compile_command() raised an | |
1831 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1835 | exception (SyntaxError or OverflowError). A syntax traceback | |
1832 | will be printed by calling the showsyntaxerror() method. |
|
1836 | will be printed by calling the showsyntaxerror() method. | |
1833 |
|
1837 | |||
1834 | 2) The input is incomplete, and more input is required; |
|
1838 | 2) The input is incomplete, and more input is required; | |
1835 | compile_command() returned None. Nothing happens. |
|
1839 | compile_command() returned None. Nothing happens. | |
1836 |
|
1840 | |||
1837 | 3) The input is complete; compile_command() returned a code |
|
1841 | 3) The input is complete; compile_command() returned a code | |
1838 | object. The code is executed by calling self.runcode() (which |
|
1842 | object. The code is executed by calling self.runcode() (which | |
1839 | also handles run-time exceptions, except for SystemExit). |
|
1843 | also handles run-time exceptions, except for SystemExit). | |
1840 |
|
1844 | |||
1841 | The return value is: |
|
1845 | The return value is: | |
1842 |
|
1846 | |||
1843 | - True in case 2 |
|
1847 | - True in case 2 | |
1844 |
|
1848 | |||
1845 | - False in the other cases, unless an exception is raised, where |
|
1849 | - False in the other cases, unless an exception is raised, where | |
1846 | None is returned instead. This can be used by external callers to |
|
1850 | None is returned instead. This can be used by external callers to | |
1847 | know whether to continue feeding input or not. |
|
1851 | know whether to continue feeding input or not. | |
1848 |
|
1852 | |||
1849 | The return value can be used to decide whether to use sys.ps1 or |
|
1853 | The return value can be used to decide whether to use sys.ps1 or | |
1850 | sys.ps2 to prompt the next line.""" |
|
1854 | sys.ps2 to prompt the next line.""" | |
1851 |
|
1855 | |||
1852 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
1856 | # if the source code has leading blanks, add 'if 1:\n' to it | |
1853 | # this allows execution of indented pasted code. It is tempting |
|
1857 | # this allows execution of indented pasted code. It is tempting | |
1854 | # to add '\n' at the end of source to run commands like ' a=1' |
|
1858 | # to add '\n' at the end of source to run commands like ' a=1' | |
1855 | # directly, but this fails for more complicated scenarios |
|
1859 | # directly, but this fails for more complicated scenarios | |
1856 | if source[:1] in [' ', '\t']: |
|
1860 | if source[:1] in [' ', '\t']: | |
1857 | source = 'if 1:\n%s' % source |
|
1861 | source = 'if 1:\n%s' % source | |
1858 |
|
1862 | |||
1859 | try: |
|
1863 | try: | |
1860 | code = self.compile(source,filename,symbol) |
|
1864 | code = self.compile(source,filename,symbol) | |
1861 | except (OverflowError, SyntaxError, ValueError): |
|
1865 | except (OverflowError, SyntaxError, ValueError): | |
1862 | # Case 1 |
|
1866 | # Case 1 | |
1863 | self.showsyntaxerror(filename) |
|
1867 | self.showsyntaxerror(filename) | |
1864 | return None |
|
1868 | return None | |
1865 |
|
1869 | |||
1866 | if code is None: |
|
1870 | if code is None: | |
1867 | # Case 2 |
|
1871 | # Case 2 | |
1868 | return True |
|
1872 | return True | |
1869 |
|
1873 | |||
1870 | # Case 3 |
|
1874 | # Case 3 | |
1871 | # We store the code object so that threaded shells and |
|
1875 | # We store the code object so that threaded shells and | |
1872 | # custom exception handlers can access all this info if needed. |
|
1876 | # custom exception handlers can access all this info if needed. | |
1873 | # The source corresponding to this can be obtained from the |
|
1877 | # The source corresponding to this can be obtained from the | |
1874 | # buffer attribute as '\n'.join(self.buffer). |
|
1878 | # buffer attribute as '\n'.join(self.buffer). | |
1875 | self.code_to_run = code |
|
1879 | self.code_to_run = code | |
1876 | # now actually execute the code object |
|
1880 | # now actually execute the code object | |
1877 | if self.runcode(code) == 0: |
|
1881 | if self.runcode(code) == 0: | |
1878 | return False |
|
1882 | return False | |
1879 | else: |
|
1883 | else: | |
1880 | return None |
|
1884 | return None | |
1881 |
|
1885 | |||
1882 | def runcode(self,code_obj): |
|
1886 | def runcode(self,code_obj): | |
1883 | """Execute a code object. |
|
1887 | """Execute a code object. | |
1884 |
|
1888 | |||
1885 | When an exception occurs, self.showtraceback() is called to display a |
|
1889 | When an exception occurs, self.showtraceback() is called to display a | |
1886 | traceback. |
|
1890 | traceback. | |
1887 |
|
1891 | |||
1888 | Return value: a flag indicating whether the code to be run completed |
|
1892 | Return value: a flag indicating whether the code to be run completed | |
1889 | successfully: |
|
1893 | successfully: | |
1890 |
|
1894 | |||
1891 | - 0: successful execution. |
|
1895 | - 0: successful execution. | |
1892 | - 1: an error occurred. |
|
1896 | - 1: an error occurred. | |
1893 | """ |
|
1897 | """ | |
1894 |
|
1898 | |||
1895 | # Set our own excepthook in case the user code tries to call it |
|
1899 | # Set our own excepthook in case the user code tries to call it | |
1896 | # directly, so that the IPython crash handler doesn't get triggered |
|
1900 | # directly, so that the IPython crash handler doesn't get triggered | |
1897 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1901 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
1898 |
|
1902 | |||
1899 | # we save the original sys.excepthook in the instance, in case config |
|
1903 | # we save the original sys.excepthook in the instance, in case config | |
1900 | # code (such as magics) needs access to it. |
|
1904 | # code (such as magics) needs access to it. | |
1901 | self.sys_excepthook = old_excepthook |
|
1905 | self.sys_excepthook = old_excepthook | |
1902 | outflag = 1 # happens in more places, so it's easier as default |
|
1906 | outflag = 1 # happens in more places, so it's easier as default | |
1903 | try: |
|
1907 | try: | |
1904 | try: |
|
1908 | try: | |
1905 | # Embedded instances require separate global/local namespaces |
|
1909 | # Embedded instances require separate global/local namespaces | |
1906 | # so they can see both the surrounding (local) namespace and |
|
1910 | # so they can see both the surrounding (local) namespace and | |
1907 | # the module-level globals when called inside another function. |
|
1911 | # the module-level globals when called inside another function. | |
1908 | if self.embedded: |
|
1912 | if self.embedded: | |
1909 | exec code_obj in self.user_global_ns, self.user_ns |
|
1913 | exec code_obj in self.user_global_ns, self.user_ns | |
1910 | # Normal (non-embedded) instances should only have a single |
|
1914 | # Normal (non-embedded) instances should only have a single | |
1911 | # namespace for user code execution, otherwise functions won't |
|
1915 | # namespace for user code execution, otherwise functions won't | |
1912 | # see interactive top-level globals. |
|
1916 | # see interactive top-level globals. | |
1913 | else: |
|
1917 | else: | |
1914 | exec code_obj in self.user_ns |
|
1918 | exec code_obj in self.user_ns | |
1915 | finally: |
|
1919 | finally: | |
1916 | # Reset our crash handler in place |
|
1920 | # Reset our crash handler in place | |
1917 | sys.excepthook = old_excepthook |
|
1921 | sys.excepthook = old_excepthook | |
1918 | except SystemExit: |
|
1922 | except SystemExit: | |
1919 | self.resetbuffer() |
|
1923 | self.resetbuffer() | |
1920 | self.showtraceback() |
|
1924 | self.showtraceback() | |
1921 | warn("Type %exit or %quit to exit IPython " |
|
1925 | warn("Type %exit or %quit to exit IPython " | |
1922 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1926 | "(%Exit or %Quit do so unconditionally).",level=1) | |
1923 | except self.custom_exceptions: |
|
1927 | except self.custom_exceptions: | |
1924 | etype,value,tb = sys.exc_info() |
|
1928 | etype,value,tb = sys.exc_info() | |
1925 | self.CustomTB(etype,value,tb) |
|
1929 | self.CustomTB(etype,value,tb) | |
1926 | except: |
|
1930 | except: | |
1927 | self.showtraceback() |
|
1931 | self.showtraceback() | |
1928 | else: |
|
1932 | else: | |
1929 | outflag = 0 |
|
1933 | outflag = 0 | |
1930 | if softspace(sys.stdout, 0): |
|
1934 | if softspace(sys.stdout, 0): | |
1931 |
|
1935 | |||
1932 | # Flush out code object which has been run (and source) |
|
1936 | # Flush out code object which has been run (and source) | |
1933 | self.code_to_run = None |
|
1937 | self.code_to_run = None | |
1934 | return outflag |
|
1938 | return outflag | |
1935 |
|
1939 | |||
1936 | def push(self, line): |
|
1940 | def push(self, line): | |
1937 | """Push a line to the interpreter. |
|
1941 | """Push a line to the interpreter. | |
1938 |
|
1942 | |||
1939 | The line should not have a trailing newline; it may have |
|
1943 | The line should not have a trailing newline; it may have | |
1940 | internal newlines. The line is appended to a buffer and the |
|
1944 | internal newlines. The line is appended to a buffer and the | |
1941 | interpreter's runsource() method is called with the |
|
1945 | interpreter's runsource() method is called with the | |
1942 | concatenated contents of the buffer as source. If this |
|
1946 | concatenated contents of the buffer as source. If this | |
1943 | indicates that the command was executed or invalid, the buffer |
|
1947 | indicates that the command was executed or invalid, the buffer | |
1944 | is reset; otherwise, the command is incomplete, and the buffer |
|
1948 | is reset; otherwise, the command is incomplete, and the buffer | |
1945 | is left as it was after the line was appended. The return |
|
1949 | is left as it was after the line was appended. The return | |
1946 | value is 1 if more input is required, 0 if the line was dealt |
|
1950 | value is 1 if more input is required, 0 if the line was dealt | |
1947 | with in some way (this is the same as runsource()). |
|
1951 | with in some way (this is the same as runsource()). | |
1948 | """ |
|
1952 | """ | |
1949 |
|
1953 | |||
1950 | # autoindent management should be done here, and not in the |
|
1954 | # autoindent management should be done here, and not in the | |
1951 | # interactive loop, since that one is only seen by keyboard input. We |
|
1955 | # interactive loop, since that one is only seen by keyboard input. We | |
1952 | # need this done correctly even for code run via runlines (which uses |
|
1956 | # need this done correctly even for code run via runlines (which uses | |
1953 | # push). |
|
1957 | # push). | |
1954 |
|
1958 | |||
1955 | #print 'push line: <%s>' % line # dbg |
|
1959 | #print 'push line: <%s>' % line # dbg | |
1956 | for subline in line.splitlines(): |
|
1960 | for subline in line.splitlines(): | |
1957 | self.autoindent_update(subline) |
|
1961 | self.autoindent_update(subline) | |
1958 | self.buffer.append(line) |
|
1962 | self.buffer.append(line) | |
1959 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1963 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
1960 | if not more: |
|
1964 | if not more: | |
1961 | self.resetbuffer() |
|
1965 | self.resetbuffer() | |
1962 | return more |
|
1966 | return more | |
1963 |
|
1967 | |||
1964 | def split_user_input(self, line): |
|
1968 | def split_user_input(self, line): | |
1965 | # This is really a hold-over to support ipapi and some extensions |
|
1969 | # This is really a hold-over to support ipapi and some extensions | |
1966 | return prefilter.splitUserInput(line) |
|
1970 | return prefilter.splitUserInput(line) | |
1967 |
|
1971 | |||
1968 | def resetbuffer(self): |
|
1972 | def resetbuffer(self): | |
1969 | """Reset the input buffer.""" |
|
1973 | """Reset the input buffer.""" | |
1970 | self.buffer[:] = [] |
|
1974 | self.buffer[:] = [] | |
1971 |
|
1975 | |||
1972 | def raw_input(self,prompt='',continue_prompt=False): |
|
1976 | def raw_input(self,prompt='',continue_prompt=False): | |
1973 | """Write a prompt and read a line. |
|
1977 | """Write a prompt and read a line. | |
1974 |
|
1978 | |||
1975 | The returned line does not include the trailing newline. |
|
1979 | The returned line does not include the trailing newline. | |
1976 | When the user enters the EOF key sequence, EOFError is raised. |
|
1980 | When the user enters the EOF key sequence, EOFError is raised. | |
1977 |
|
1981 | |||
1978 | Optional inputs: |
|
1982 | Optional inputs: | |
1979 |
|
1983 | |||
1980 | - prompt(''): a string to be printed to prompt the user. |
|
1984 | - prompt(''): a string to be printed to prompt the user. | |
1981 |
|
1985 | |||
1982 | - continue_prompt(False): whether this line is the first one or a |
|
1986 | - continue_prompt(False): whether this line is the first one or a | |
1983 | continuation in a sequence of inputs. |
|
1987 | continuation in a sequence of inputs. | |
1984 | """ |
|
1988 | """ | |
1985 |
|
1989 | |||
1986 | # Code run by the user may have modified the readline completer state. |
|
1990 | # Code run by the user may have modified the readline completer state. | |
1987 | # We must ensure that our completer is back in place. |
|
1991 | # We must ensure that our completer is back in place. | |
1988 | if self.has_readline: |
|
1992 | if self.has_readline: | |
1989 | self.set_completer() |
|
1993 | self.set_completer() | |
1990 |
|
1994 | |||
1991 | try: |
|
1995 | try: | |
1992 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
1996 | line = raw_input_original(prompt).decode(self.stdin_encoding) | |
1993 | except ValueError: |
|
1997 | except ValueError: | |
1994 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
1998 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
1995 | " or sys.stdout.close()!\nExiting IPython!") |
|
1999 | " or sys.stdout.close()!\nExiting IPython!") | |
1996 | self.exit_now = True |
|
2000 | self.exit_now = True | |
1997 | return "" |
|
2001 | return "" | |
1998 |
|
2002 | |||
1999 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2003 | # Try to be reasonably smart about not re-indenting pasted input more | |
2000 | # than necessary. We do this by trimming out the auto-indent initial |
|
2004 | # than necessary. We do this by trimming out the auto-indent initial | |
2001 | # spaces, if the user's actual input started itself with whitespace. |
|
2005 | # spaces, if the user's actual input started itself with whitespace. | |
2002 | #debugx('self.buffer[-1]') |
|
2006 | #debugx('self.buffer[-1]') | |
2003 |
|
2007 | |||
2004 | if self.autoindent: |
|
2008 | if self.autoindent: | |
2005 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2009 | if num_ini_spaces(line) > self.indent_current_nsp: | |
2006 | line = line[self.indent_current_nsp:] |
|
2010 | line = line[self.indent_current_nsp:] | |
2007 | self.indent_current_nsp = 0 |
|
2011 | self.indent_current_nsp = 0 | |
2008 |
|
2012 | |||
2009 | # store the unfiltered input before the user has any chance to modify |
|
2013 | # store the unfiltered input before the user has any chance to modify | |
2010 | # it. |
|
2014 | # it. | |
2011 | if line.strip(): |
|
2015 | if line.strip(): | |
2012 | if continue_prompt: |
|
2016 | if continue_prompt: | |
2013 | self.input_hist_raw[-1] += '%s\n' % line |
|
2017 | self.input_hist_raw[-1] += '%s\n' % line | |
2014 | if self.has_readline: # and some config option is set? |
|
2018 | if self.has_readline: # and some config option is set? | |
2015 | try: |
|
2019 | try: | |
2016 | histlen = self.readline.get_current_history_length() |
|
2020 | histlen = self.readline.get_current_history_length() | |
2017 | newhist = self.input_hist_raw[-1].rstrip() |
|
2021 | newhist = self.input_hist_raw[-1].rstrip() | |
2018 | self.readline.remove_history_item(histlen-1) |
|
2022 | self.readline.remove_history_item(histlen-1) | |
2019 | self.readline.replace_history_item(histlen-2,newhist) |
|
2023 | self.readline.replace_history_item(histlen-2,newhist) | |
2020 | except AttributeError: |
|
2024 | except AttributeError: | |
2021 | pass # re{move,place}_history_item are new in 2.4. |
|
2025 | pass # re{move,place}_history_item are new in 2.4. | |
2022 | else: |
|
2026 | else: | |
2023 | self.input_hist_raw.append('%s\n' % line) |
|
2027 | self.input_hist_raw.append('%s\n' % line) | |
2024 |
|
2028 | |||
2025 | if line.lstrip() == line: |
|
2029 | if line.lstrip() == line: | |
2026 | self.shadowhist.add(line.strip()) |
|
2030 | self.shadowhist.add(line.strip()) | |
2027 |
|
2031 | |||
2028 | try: |
|
2032 | try: | |
2029 | lineout = self.prefilter(line,continue_prompt) |
|
2033 | lineout = self.prefilter(line,continue_prompt) | |
2030 | except: |
|
2034 | except: | |
2031 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2035 | # blanket except, in case a user-defined prefilter crashes, so it | |
2032 | # can't take all of ipython with it. |
|
2036 | # can't take all of ipython with it. | |
2033 | self.showtraceback() |
|
2037 | self.showtraceback() | |
2034 | return '' |
|
2038 | return '' | |
2035 | else: |
|
2039 | else: | |
2036 | return lineout |
|
2040 | return lineout | |
2037 |
|
2041 | |||
2038 | def _prefilter(self, line, continue_prompt): |
|
2042 | def _prefilter(self, line, continue_prompt): | |
2039 | """Calls different preprocessors, depending on the form of line.""" |
|
2043 | """Calls different preprocessors, depending on the form of line.""" | |
2040 |
|
2044 | |||
2041 | # All handlers *must* return a value, even if it's blank (''). |
|
2045 | # All handlers *must* return a value, even if it's blank (''). | |
2042 |
|
2046 | |||
2043 | # Lines are NOT logged here. Handlers should process the line as |
|
2047 | # Lines are NOT logged here. Handlers should process the line as | |
2044 | # needed, update the cache AND log it (so that the input cache array |
|
2048 | # needed, update the cache AND log it (so that the input cache array | |
2045 | # stays synced). |
|
2049 | # stays synced). | |
2046 |
|
2050 | |||
2047 | #..................................................................... |
|
2051 | #..................................................................... | |
2048 | # Code begins |
|
2052 | # Code begins | |
2049 |
|
2053 | |||
2050 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2054 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg | |
2051 |
|
2055 | |||
2052 | # save the line away in case we crash, so the post-mortem handler can |
|
2056 | # save the line away in case we crash, so the post-mortem handler can | |
2053 | # record it |
|
2057 | # record it | |
2054 | self._last_input_line = line |
|
2058 | self._last_input_line = line | |
2055 |
|
2059 | |||
2056 | #print '***line: <%s>' % line # dbg |
|
2060 | #print '***line: <%s>' % line # dbg | |
2057 |
|
2061 | |||
2058 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
2062 | line_info = prefilter.LineInfo(line, continue_prompt) | |
2059 |
|
2063 | |||
2060 | # the input history needs to track even empty lines |
|
2064 | # the input history needs to track even empty lines | |
2061 | stripped = line.strip() |
|
2065 | stripped = line.strip() | |
2062 |
|
2066 | |||
2063 | if not stripped: |
|
2067 | if not stripped: | |
2064 | if not continue_prompt: |
|
2068 | if not continue_prompt: | |
2065 | self.outputcache.prompt_count -= 1 |
|
2069 | self.outputcache.prompt_count -= 1 | |
2066 | return self.handle_normal(line_info) |
|
2070 | return self.handle_normal(line_info) | |
2067 |
|
2071 | |||
2068 | # print '***cont',continue_prompt # dbg |
|
2072 | # print '***cont',continue_prompt # dbg | |
2069 | # special handlers are only allowed for single line statements |
|
2073 | # special handlers are only allowed for single line statements | |
2070 | if continue_prompt and not self.rc.multi_line_specials: |
|
2074 | if continue_prompt and not self.rc.multi_line_specials: | |
2071 | return self.handle_normal(line_info) |
|
2075 | return self.handle_normal(line_info) | |
2072 |
|
2076 | |||
2073 |
|
2077 | |||
2074 | # See whether any pre-existing handler can take care of it |
|
2078 | # See whether any pre-existing handler can take care of it | |
2075 | rewritten = self.hooks.input_prefilter(stripped) |
|
2079 | rewritten = self.hooks.input_prefilter(stripped) | |
2076 | if rewritten != stripped: # ok, some prefilter did something |
|
2080 | if rewritten != stripped: # ok, some prefilter did something | |
2077 | rewritten = line_info.pre + rewritten # add indentation |
|
2081 | rewritten = line_info.pre + rewritten # add indentation | |
2078 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
2082 | return self.handle_normal(prefilter.LineInfo(rewritten, | |
2079 | continue_prompt)) |
|
2083 | continue_prompt)) | |
2080 |
|
2084 | |||
2081 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2085 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2082 |
|
2086 | |||
2083 | return prefilter.prefilter(line_info, self) |
|
2087 | return prefilter.prefilter(line_info, self) | |
2084 |
|
2088 | |||
2085 |
|
2089 | |||
2086 | def _prefilter_dumb(self, line, continue_prompt): |
|
2090 | def _prefilter_dumb(self, line, continue_prompt): | |
2087 | """simple prefilter function, for debugging""" |
|
2091 | """simple prefilter function, for debugging""" | |
2088 | return self.handle_normal(line,continue_prompt) |
|
2092 | return self.handle_normal(line,continue_prompt) | |
2089 |
|
2093 | |||
2090 |
|
2094 | |||
2091 | def multiline_prefilter(self, line, continue_prompt): |
|
2095 | def multiline_prefilter(self, line, continue_prompt): | |
2092 | """ Run _prefilter for each line of input |
|
2096 | """ Run _prefilter for each line of input | |
2093 |
|
2097 | |||
2094 | Covers cases where there are multiple lines in the user entry, |
|
2098 | Covers cases where there are multiple lines in the user entry, | |
2095 | which is the case when the user goes back to a multiline history |
|
2099 | which is the case when the user goes back to a multiline history | |
2096 | entry and presses enter. |
|
2100 | entry and presses enter. | |
2097 |
|
2101 | |||
2098 | """ |
|
2102 | """ | |
2099 | out = [] |
|
2103 | out = [] | |
2100 | for l in line.rstrip('\n').split('\n'): |
|
2104 | for l in line.rstrip('\n').split('\n'): | |
2101 | out.append(self._prefilter(l, continue_prompt)) |
|
2105 | out.append(self._prefilter(l, continue_prompt)) | |
2102 | return '\n'.join(out) |
|
2106 | return '\n'.join(out) | |
2103 |
|
2107 | |||
2104 | # Set the default prefilter() function (this can be user-overridden) |
|
2108 | # Set the default prefilter() function (this can be user-overridden) | |
2105 | prefilter = multiline_prefilter |
|
2109 | prefilter = multiline_prefilter | |
2106 |
|
2110 | |||
2107 | def handle_normal(self,line_info): |
|
2111 | def handle_normal(self,line_info): | |
2108 | """Handle normal input lines. Use as a template for handlers.""" |
|
2112 | """Handle normal input lines. Use as a template for handlers.""" | |
2109 |
|
2113 | |||
2110 | # With autoindent on, we need some way to exit the input loop, and I |
|
2114 | # With autoindent on, we need some way to exit the input loop, and I | |
2111 | # don't want to force the user to have to backspace all the way to |
|
2115 | # don't want to force the user to have to backspace all the way to | |
2112 | # clear the line. The rule will be in this case, that either two |
|
2116 | # clear the line. The rule will be in this case, that either two | |
2113 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2117 | # lines of pure whitespace in a row, or a line of pure whitespace but | |
2114 | # of a size different to the indent level, will exit the input loop. |
|
2118 | # of a size different to the indent level, will exit the input loop. | |
2115 | line = line_info.line |
|
2119 | line = line_info.line | |
2116 | continue_prompt = line_info.continue_prompt |
|
2120 | continue_prompt = line_info.continue_prompt | |
2117 |
|
2121 | |||
2118 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2122 | if (continue_prompt and self.autoindent and line.isspace() and | |
2119 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2123 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or | |
2120 | (self.buffer[-1]).isspace() )): |
|
2124 | (self.buffer[-1]).isspace() )): | |
2121 | line = '' |
|
2125 | line = '' | |
2122 |
|
2126 | |||
2123 | self.log(line,line,continue_prompt) |
|
2127 | self.log(line,line,continue_prompt) | |
2124 | return line |
|
2128 | return line | |
2125 |
|
2129 | |||
2126 | def handle_alias(self,line_info): |
|
2130 | def handle_alias(self,line_info): | |
2127 | """Handle alias input lines. """ |
|
2131 | """Handle alias input lines. """ | |
2128 | tgt = self.alias_table[line_info.iFun] |
|
2132 | tgt = self.alias_table[line_info.iFun] | |
2129 | # print "=>",tgt #dbg |
|
2133 | # print "=>",tgt #dbg | |
2130 | if callable(tgt): |
|
2134 | if callable(tgt): | |
2131 | line_out = "_sh." + line_info.iFun + '(r"""' + line_info.theRest + '""")' |
|
2135 | line_out = "_sh." + line_info.iFun + '(r"""' + line_info.theRest + '""")' | |
2132 | else: |
|
2136 | else: | |
2133 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2137 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) | |
2134 |
|
2138 | |||
2135 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2139 | # pre is needed, because it carries the leading whitespace. Otherwise | |
2136 | # aliases won't work in indented sections. |
|
2140 | # aliases won't work in indented sections. | |
2137 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2141 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2138 | make_quoted_expr( transformed )) |
|
2142 | make_quoted_expr( transformed )) | |
2139 |
|
2143 | |||
2140 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
2144 | self.log(line_info.line,line_out,line_info.continue_prompt) | |
2141 | #print 'line out:',line_out # dbg |
|
2145 | #print 'line out:',line_out # dbg | |
2142 | return line_out |
|
2146 | return line_out | |
2143 |
|
2147 | |||
2144 | def handle_shell_escape(self, line_info): |
|
2148 | def handle_shell_escape(self, line_info): | |
2145 | """Execute the line in a shell, empty return value""" |
|
2149 | """Execute the line in a shell, empty return value""" | |
2146 | #print 'line in :', `line` # dbg |
|
2150 | #print 'line in :', `line` # dbg | |
2147 | line = line_info.line |
|
2151 | line = line_info.line | |
2148 | if line.lstrip().startswith('!!'): |
|
2152 | if line.lstrip().startswith('!!'): | |
2149 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
2153 | # rewrite LineInfo's line, iFun and theRest to properly hold the | |
2150 | # call to %sx and the actual command to be executed, so |
|
2154 | # call to %sx and the actual command to be executed, so | |
2151 | # handle_magic can work correctly. Note that this works even if |
|
2155 | # handle_magic can work correctly. Note that this works even if | |
2152 | # the line is indented, so it handles multi_line_specials |
|
2156 | # the line is indented, so it handles multi_line_specials | |
2153 | # properly. |
|
2157 | # properly. | |
2154 | new_rest = line.lstrip()[2:] |
|
2158 | new_rest = line.lstrip()[2:] | |
2155 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
2159 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) | |
2156 | line_info.iFun = 'sx' |
|
2160 | line_info.iFun = 'sx' | |
2157 | line_info.theRest = new_rest |
|
2161 | line_info.theRest = new_rest | |
2158 | return self.handle_magic(line_info) |
|
2162 | return self.handle_magic(line_info) | |
2159 | else: |
|
2163 | else: | |
2160 | cmd = line.lstrip().lstrip('!') |
|
2164 | cmd = line.lstrip().lstrip('!') | |
2161 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2165 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2162 | make_quoted_expr(cmd)) |
|
2166 | make_quoted_expr(cmd)) | |
2163 | # update cache/log and return |
|
2167 | # update cache/log and return | |
2164 | self.log(line,line_out,line_info.continue_prompt) |
|
2168 | self.log(line,line_out,line_info.continue_prompt) | |
2165 | return line_out |
|
2169 | return line_out | |
2166 |
|
2170 | |||
2167 | def handle_magic(self, line_info): |
|
2171 | def handle_magic(self, line_info): | |
2168 | """Execute magic functions.""" |
|
2172 | """Execute magic functions.""" | |
2169 | iFun = line_info.iFun |
|
2173 | iFun = line_info.iFun | |
2170 | theRest = line_info.theRest |
|
2174 | theRest = line_info.theRest | |
2171 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
2175 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, | |
2172 | make_quoted_expr(iFun + " " + theRest)) |
|
2176 | make_quoted_expr(iFun + " " + theRest)) | |
2173 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
2177 | self.log(line_info.line,cmd,line_info.continue_prompt) | |
2174 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2178 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg | |
2175 | return cmd |
|
2179 | return cmd | |
2176 |
|
2180 | |||
2177 | def handle_auto(self, line_info): |
|
2181 | def handle_auto(self, line_info): | |
2178 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2182 | """Hande lines which can be auto-executed, quoting if requested.""" | |
2179 |
|
2183 | |||
2180 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2184 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2181 | line = line_info.line |
|
2185 | line = line_info.line | |
2182 | iFun = line_info.iFun |
|
2186 | iFun = line_info.iFun | |
2183 | theRest = line_info.theRest |
|
2187 | theRest = line_info.theRest | |
2184 | pre = line_info.pre |
|
2188 | pre = line_info.pre | |
2185 | continue_prompt = line_info.continue_prompt |
|
2189 | continue_prompt = line_info.continue_prompt | |
2186 | obj = line_info.ofind(self)['obj'] |
|
2190 | obj = line_info.ofind(self)['obj'] | |
2187 |
|
2191 | |||
2188 | # This should only be active for single-line input! |
|
2192 | # This should only be active for single-line input! | |
2189 | if continue_prompt: |
|
2193 | if continue_prompt: | |
2190 | self.log(line,line,continue_prompt) |
|
2194 | self.log(line,line,continue_prompt) | |
2191 | return line |
|
2195 | return line | |
2192 |
|
2196 | |||
2193 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) |
|
2197 | force_auto = isinstance(obj, IPython.ipapi.IPyAutocall) | |
2194 | auto_rewrite = True |
|
2198 | auto_rewrite = True | |
2195 |
|
2199 | |||
2196 | if pre == self.ESC_QUOTE: |
|
2200 | if pre == self.ESC_QUOTE: | |
2197 | # Auto-quote splitting on whitespace |
|
2201 | # Auto-quote splitting on whitespace | |
2198 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2202 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) | |
2199 | elif pre == self.ESC_QUOTE2: |
|
2203 | elif pre == self.ESC_QUOTE2: | |
2200 | # Auto-quote whole string |
|
2204 | # Auto-quote whole string | |
2201 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2205 | newcmd = '%s("%s")' % (iFun,theRest) | |
2202 | elif pre == self.ESC_PAREN: |
|
2206 | elif pre == self.ESC_PAREN: | |
2203 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2207 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) | |
2204 | else: |
|
2208 | else: | |
2205 | # Auto-paren. |
|
2209 | # Auto-paren. | |
2206 | # We only apply it to argument-less calls if the autocall |
|
2210 | # We only apply it to argument-less calls if the autocall | |
2207 | # parameter is set to 2. We only need to check that autocall is < |
|
2211 | # parameter is set to 2. We only need to check that autocall is < | |
2208 | # 2, since this function isn't called unless it's at least 1. |
|
2212 | # 2, since this function isn't called unless it's at least 1. | |
2209 | if not theRest and (self.rc.autocall < 2) and not force_auto: |
|
2213 | if not theRest and (self.rc.autocall < 2) and not force_auto: | |
2210 | newcmd = '%s %s' % (iFun,theRest) |
|
2214 | newcmd = '%s %s' % (iFun,theRest) | |
2211 | auto_rewrite = False |
|
2215 | auto_rewrite = False | |
2212 | else: |
|
2216 | else: | |
2213 | if not force_auto and theRest.startswith('['): |
|
2217 | if not force_auto and theRest.startswith('['): | |
2214 | if hasattr(obj,'__getitem__'): |
|
2218 | if hasattr(obj,'__getitem__'): | |
2215 | # Don't autocall in this case: item access for an object |
|
2219 | # Don't autocall in this case: item access for an object | |
2216 | # which is BOTH callable and implements __getitem__. |
|
2220 | # which is BOTH callable and implements __getitem__. | |
2217 | newcmd = '%s %s' % (iFun,theRest) |
|
2221 | newcmd = '%s %s' % (iFun,theRest) | |
2218 | auto_rewrite = False |
|
2222 | auto_rewrite = False | |
2219 | else: |
|
2223 | else: | |
2220 | # if the object doesn't support [] access, go ahead and |
|
2224 | # if the object doesn't support [] access, go ahead and | |
2221 | # autocall |
|
2225 | # autocall | |
2222 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2226 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) | |
2223 | elif theRest.endswith(';'): |
|
2227 | elif theRest.endswith(';'): | |
2224 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2228 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) | |
2225 | else: |
|
2229 | else: | |
2226 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2230 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) | |
2227 |
|
2231 | |||
2228 | if auto_rewrite: |
|
2232 | if auto_rewrite: | |
2229 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2233 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd | |
2230 |
|
2234 | |||
2231 | try: |
|
2235 | try: | |
2232 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2236 | # plain ascii works better w/ pyreadline, on some machines, so | |
2233 | # we use it and only print uncolored rewrite if we have unicode |
|
2237 | # we use it and only print uncolored rewrite if we have unicode | |
2234 | rw = str(rw) |
|
2238 | rw = str(rw) | |
2235 | print >>Term.cout, rw |
|
2239 | print >>Term.cout, rw | |
2236 | except UnicodeEncodeError: |
|
2240 | except UnicodeEncodeError: | |
2237 | print "-------------->" + newcmd |
|
2241 | print "-------------->" + newcmd | |
2238 |
|
2242 | |||
2239 | # log what is now valid Python, not the actual user input (without the |
|
2243 | # log what is now valid Python, not the actual user input (without the | |
2240 | # final newline) |
|
2244 | # final newline) | |
2241 | self.log(line,newcmd,continue_prompt) |
|
2245 | self.log(line,newcmd,continue_prompt) | |
2242 | return newcmd |
|
2246 | return newcmd | |
2243 |
|
2247 | |||
2244 | def handle_help(self, line_info): |
|
2248 | def handle_help(self, line_info): | |
2245 | """Try to get some help for the object. |
|
2249 | """Try to get some help for the object. | |
2246 |
|
2250 | |||
2247 | obj? or ?obj -> basic information. |
|
2251 | obj? or ?obj -> basic information. | |
2248 | obj?? or ??obj -> more details. |
|
2252 | obj?? or ??obj -> more details. | |
2249 | """ |
|
2253 | """ | |
2250 |
|
2254 | |||
2251 | line = line_info.line |
|
2255 | line = line_info.line | |
2252 | # We need to make sure that we don't process lines which would be |
|
2256 | # We need to make sure that we don't process lines which would be | |
2253 | # otherwise valid python, such as "x=1 # what?" |
|
2257 | # otherwise valid python, such as "x=1 # what?" | |
2254 | try: |
|
2258 | try: | |
2255 | codeop.compile_command(line) |
|
2259 | codeop.compile_command(line) | |
2256 | except SyntaxError: |
|
2260 | except SyntaxError: | |
2257 | # We should only handle as help stuff which is NOT valid syntax |
|
2261 | # We should only handle as help stuff which is NOT valid syntax | |
2258 | if line[0]==self.ESC_HELP: |
|
2262 | if line[0]==self.ESC_HELP: | |
2259 | line = line[1:] |
|
2263 | line = line[1:] | |
2260 | elif line[-1]==self.ESC_HELP: |
|
2264 | elif line[-1]==self.ESC_HELP: | |
2261 | line = line[:-1] |
|
2265 | line = line[:-1] | |
2262 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
2266 | self.log(line,'#?'+line,line_info.continue_prompt) | |
2263 | if line: |
|
2267 | if line: | |
2264 | #print 'line:<%r>' % line # dbg |
|
2268 | #print 'line:<%r>' % line # dbg | |
2265 | self.magic_pinfo(line) |
|
2269 | self.magic_pinfo(line) | |
2266 | else: |
|
2270 | else: | |
2267 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2271 | page(self.usage,screen_lines=self.rc.screen_length) | |
2268 | return '' # Empty string is needed here! |
|
2272 | return '' # Empty string is needed here! | |
2269 | except: |
|
2273 | except: | |
2270 | # Pass any other exceptions through to the normal handler |
|
2274 | # Pass any other exceptions through to the normal handler | |
2271 | return self.handle_normal(line_info) |
|
2275 | return self.handle_normal(line_info) | |
2272 | else: |
|
2276 | else: | |
2273 | # If the code compiles ok, we should handle it normally |
|
2277 | # If the code compiles ok, we should handle it normally | |
2274 | return self.handle_normal(line_info) |
|
2278 | return self.handle_normal(line_info) | |
2275 |
|
2279 | |||
2276 | def getapi(self): |
|
2280 | def getapi(self): | |
2277 | """ Get an IPApi object for this shell instance |
|
2281 | """ Get an IPApi object for this shell instance | |
2278 |
|
2282 | |||
2279 | Getting an IPApi object is always preferable to accessing the shell |
|
2283 | Getting an IPApi object is always preferable to accessing the shell | |
2280 | directly, but this holds true especially for extensions. |
|
2284 | directly, but this holds true especially for extensions. | |
2281 |
|
2285 | |||
2282 | It should always be possible to implement an extension with IPApi |
|
2286 | It should always be possible to implement an extension with IPApi | |
2283 | alone. If not, contact maintainer to request an addition. |
|
2287 | alone. If not, contact maintainer to request an addition. | |
2284 |
|
2288 | |||
2285 | """ |
|
2289 | """ | |
2286 | return self.api |
|
2290 | return self.api | |
2287 |
|
2291 | |||
2288 | def handle_emacs(self, line_info): |
|
2292 | def handle_emacs(self, line_info): | |
2289 | """Handle input lines marked by python-mode.""" |
|
2293 | """Handle input lines marked by python-mode.""" | |
2290 |
|
2294 | |||
2291 | # Currently, nothing is done. Later more functionality can be added |
|
2295 | # Currently, nothing is done. Later more functionality can be added | |
2292 | # here if needed. |
|
2296 | # here if needed. | |
2293 |
|
2297 | |||
2294 | # The input cache shouldn't be updated |
|
2298 | # The input cache shouldn't be updated | |
2295 | return line_info.line |
|
2299 | return line_info.line | |
2296 |
|
2300 | |||
2297 |
|
2301 | |||
2298 | def mktempfile(self,data=None): |
|
2302 | def mktempfile(self,data=None): | |
2299 | """Make a new tempfile and return its filename. |
|
2303 | """Make a new tempfile and return its filename. | |
2300 |
|
2304 | |||
2301 | This makes a call to tempfile.mktemp, but it registers the created |
|
2305 | This makes a call to tempfile.mktemp, but it registers the created | |
2302 | filename internally so ipython cleans it up at exit time. |
|
2306 | filename internally so ipython cleans it up at exit time. | |
2303 |
|
2307 | |||
2304 | Optional inputs: |
|
2308 | Optional inputs: | |
2305 |
|
2309 | |||
2306 | - data(None): if data is given, it gets written out to the temp file |
|
2310 | - data(None): if data is given, it gets written out to the temp file | |
2307 | immediately, and the file is closed again.""" |
|
2311 | immediately, and the file is closed again.""" | |
2308 |
|
2312 | |||
2309 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2313 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2310 | self.tempfiles.append(filename) |
|
2314 | self.tempfiles.append(filename) | |
2311 |
|
2315 | |||
2312 | if data: |
|
2316 | if data: | |
2313 | tmp_file = open(filename,'w') |
|
2317 | tmp_file = open(filename,'w') | |
2314 | tmp_file.write(data) |
|
2318 | tmp_file.write(data) | |
2315 | tmp_file.close() |
|
2319 | tmp_file.close() | |
2316 | return filename |
|
2320 | return filename | |
2317 |
|
2321 | |||
2318 | def write(self,data): |
|
2322 | def write(self,data): | |
2319 | """Write a string to the default output""" |
|
2323 | """Write a string to the default output""" | |
2320 | Term.cout.write(data) |
|
2324 | Term.cout.write(data) | |
2321 |
|
2325 | |||
2322 | def write_err(self,data): |
|
2326 | def write_err(self,data): | |
2323 | """Write a string to the default error output""" |
|
2327 | """Write a string to the default error output""" | |
2324 | Term.cerr.write(data) |
|
2328 | Term.cerr.write(data) | |
2325 |
|
2329 | |||
2326 | def exit(self): |
|
2330 | def exit(self): | |
2327 | """Handle interactive exit. |
|
2331 | """Handle interactive exit. | |
2328 |
|
2332 | |||
2329 | This method sets the exit_now attribute.""" |
|
2333 | This method sets the exit_now attribute.""" | |
2330 |
|
2334 | |||
2331 | if self.rc.confirm_exit: |
|
2335 | if self.rc.confirm_exit: | |
2332 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2336 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
2333 | self.exit_now = True |
|
2337 | self.exit_now = True | |
2334 | else: |
|
2338 | else: | |
2335 | self.exit_now = True |
|
2339 | self.exit_now = True | |
2336 |
|
2340 | |||
2337 | def safe_execfile(self,fname,*where,**kw): |
|
2341 | def safe_execfile(self,fname,*where,**kw): | |
2338 | """A safe version of the builtin execfile(). |
|
2342 | """A safe version of the builtin execfile(). | |
2339 |
|
2343 | |||
2340 | This version will never throw an exception, and knows how to handle |
|
2344 | This version will never throw an exception, and knows how to handle | |
2341 | ipython logs as well.""" |
|
2345 | ipython logs as well.""" | |
2342 |
|
2346 | |||
2343 | def syspath_cleanup(): |
|
2347 | def syspath_cleanup(): | |
2344 | """Internal cleanup routine for sys.path.""" |
|
2348 | """Internal cleanup routine for sys.path.""" | |
2345 | if add_dname: |
|
2349 | if add_dname: | |
2346 | try: |
|
2350 | try: | |
2347 | sys.path.remove(dname) |
|
2351 | sys.path.remove(dname) | |
2348 | except ValueError: |
|
2352 | except ValueError: | |
2349 | # For some reason the user has already removed it, ignore. |
|
2353 | # For some reason the user has already removed it, ignore. | |
2350 | pass |
|
2354 | pass | |
2351 |
|
2355 | |||
2352 | fname = os.path.expanduser(fname) |
|
2356 | fname = os.path.expanduser(fname) | |
2353 |
|
2357 | |||
2354 | # Find things also in current directory. This is needed to mimic the |
|
2358 | # Find things also in current directory. This is needed to mimic the | |
2355 | # behavior of running a script from the system command line, where |
|
2359 | # behavior of running a script from the system command line, where | |
2356 | # Python inserts the script's directory into sys.path |
|
2360 | # Python inserts the script's directory into sys.path | |
2357 | dname = os.path.dirname(os.path.abspath(fname)) |
|
2361 | dname = os.path.dirname(os.path.abspath(fname)) | |
2358 | add_dname = False |
|
2362 | add_dname = False | |
2359 | if dname not in sys.path: |
|
2363 | if dname not in sys.path: | |
2360 | sys.path.insert(0,dname) |
|
2364 | sys.path.insert(0,dname) | |
2361 | add_dname = True |
|
2365 | add_dname = True | |
2362 |
|
2366 | |||
2363 | try: |
|
2367 | try: | |
2364 | xfile = open(fname) |
|
2368 | xfile = open(fname) | |
2365 | except: |
|
2369 | except: | |
2366 | print >> Term.cerr, \ |
|
2370 | print >> Term.cerr, \ | |
2367 | 'Could not open file <%s> for safe execution.' % fname |
|
2371 | 'Could not open file <%s> for safe execution.' % fname | |
2368 | syspath_cleanup() |
|
2372 | syspath_cleanup() | |
2369 | return None |
|
2373 | return None | |
2370 |
|
2374 | |||
2371 | kw.setdefault('islog',0) |
|
2375 | kw.setdefault('islog',0) | |
2372 | kw.setdefault('quiet',1) |
|
2376 | kw.setdefault('quiet',1) | |
2373 | kw.setdefault('exit_ignore',0) |
|
2377 | kw.setdefault('exit_ignore',0) | |
2374 | first = xfile.readline() |
|
2378 | first = xfile.readline() | |
2375 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2379 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() | |
2376 | xfile.close() |
|
2380 | xfile.close() | |
2377 | # line by line execution |
|
2381 | # line by line execution | |
2378 | if first.startswith(loghead) or kw['islog']: |
|
2382 | if first.startswith(loghead) or kw['islog']: | |
2379 | print 'Loading log file <%s> one line at a time...' % fname |
|
2383 | print 'Loading log file <%s> one line at a time...' % fname | |
2380 | if kw['quiet']: |
|
2384 | if kw['quiet']: | |
2381 | stdout_save = sys.stdout |
|
2385 | stdout_save = sys.stdout | |
2382 | sys.stdout = StringIO.StringIO() |
|
2386 | sys.stdout = StringIO.StringIO() | |
2383 | try: |
|
2387 | try: | |
2384 | globs,locs = where[0:2] |
|
2388 | globs,locs = where[0:2] | |
2385 | except: |
|
2389 | except: | |
2386 | try: |
|
2390 | try: | |
2387 | globs = locs = where[0] |
|
2391 | globs = locs = where[0] | |
2388 | except: |
|
2392 | except: | |
2389 | globs = locs = globals() |
|
2393 | globs = locs = globals() | |
2390 | badblocks = [] |
|
2394 | badblocks = [] | |
2391 |
|
2395 | |||
2392 | # we also need to identify indented blocks of code when replaying |
|
2396 | # we also need to identify indented blocks of code when replaying | |
2393 | # logs and put them together before passing them to an exec |
|
2397 | # logs and put them together before passing them to an exec | |
2394 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2398 | # statement. This takes a bit of regexp and look-ahead work in the | |
2395 | # file. It's easiest if we swallow the whole thing in memory |
|
2399 | # file. It's easiest if we swallow the whole thing in memory | |
2396 | # first, and manually walk through the lines list moving the |
|
2400 | # first, and manually walk through the lines list moving the | |
2397 | # counter ourselves. |
|
2401 | # counter ourselves. | |
2398 | indent_re = re.compile('\s+\S') |
|
2402 | indent_re = re.compile('\s+\S') | |
2399 | xfile = open(fname) |
|
2403 | xfile = open(fname) | |
2400 | filelines = xfile.readlines() |
|
2404 | filelines = xfile.readlines() | |
2401 | xfile.close() |
|
2405 | xfile.close() | |
2402 | nlines = len(filelines) |
|
2406 | nlines = len(filelines) | |
2403 | lnum = 0 |
|
2407 | lnum = 0 | |
2404 | while lnum < nlines: |
|
2408 | while lnum < nlines: | |
2405 | line = filelines[lnum] |
|
2409 | line = filelines[lnum] | |
2406 | lnum += 1 |
|
2410 | lnum += 1 | |
2407 | # don't re-insert logger status info into cache |
|
2411 | # don't re-insert logger status info into cache | |
2408 | if line.startswith('#log#'): |
|
2412 | if line.startswith('#log#'): | |
2409 | continue |
|
2413 | continue | |
2410 | else: |
|
2414 | else: | |
2411 | # build a block of code (maybe a single line) for execution |
|
2415 | # build a block of code (maybe a single line) for execution | |
2412 | block = line |
|
2416 | block = line | |
2413 | try: |
|
2417 | try: | |
2414 | next = filelines[lnum] # lnum has already incremented |
|
2418 | next = filelines[lnum] # lnum has already incremented | |
2415 | except: |
|
2419 | except: | |
2416 | next = None |
|
2420 | next = None | |
2417 | while next and indent_re.match(next): |
|
2421 | while next and indent_re.match(next): | |
2418 | block += next |
|
2422 | block += next | |
2419 | lnum += 1 |
|
2423 | lnum += 1 | |
2420 | try: |
|
2424 | try: | |
2421 | next = filelines[lnum] |
|
2425 | next = filelines[lnum] | |
2422 | except: |
|
2426 | except: | |
2423 | next = None |
|
2427 | next = None | |
2424 | # now execute the block of one or more lines |
|
2428 | # now execute the block of one or more lines | |
2425 | try: |
|
2429 | try: | |
2426 | exec block in globs,locs |
|
2430 | exec block in globs,locs | |
2427 | except SystemExit: |
|
2431 | except SystemExit: | |
2428 | pass |
|
2432 | pass | |
2429 | except: |
|
2433 | except: | |
2430 | badblocks.append(block.rstrip()) |
|
2434 | badblocks.append(block.rstrip()) | |
2431 | if kw['quiet']: # restore stdout |
|
2435 | if kw['quiet']: # restore stdout | |
2432 | sys.stdout.close() |
|
2436 | sys.stdout.close() | |
2433 | sys.stdout = stdout_save |
|
2437 | sys.stdout = stdout_save | |
2434 | print 'Finished replaying log file <%s>' % fname |
|
2438 | print 'Finished replaying log file <%s>' % fname | |
2435 | if badblocks: |
|
2439 | if badblocks: | |
2436 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2440 | print >> sys.stderr, ('\nThe following lines/blocks in file ' | |
2437 | '<%s> reported errors:' % fname) |
|
2441 | '<%s> reported errors:' % fname) | |
2438 |
|
2442 | |||
2439 | for badline in badblocks: |
|
2443 | for badline in badblocks: | |
2440 | print >> sys.stderr, badline |
|
2444 | print >> sys.stderr, badline | |
2441 | else: # regular file execution |
|
2445 | else: # regular file execution | |
2442 | try: |
|
2446 | try: | |
2443 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
2447 | if sys.platform == 'win32' and sys.version_info < (2,5,1): | |
2444 | # Work around a bug in Python for Windows. The bug was |
|
2448 | # Work around a bug in Python for Windows. The bug was | |
2445 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
2449 | # fixed in in Python 2.5 r54159 and 54158, but that's still | |
2446 | # SVN Python as of March/07. For details, see: |
|
2450 | # SVN Python as of March/07. For details, see: | |
2447 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
2451 | # http://projects.scipy.org/ipython/ipython/ticket/123 | |
2448 | try: |
|
2452 | try: | |
2449 | globs,locs = where[0:2] |
|
2453 | globs,locs = where[0:2] | |
2450 | except: |
|
2454 | except: | |
2451 | try: |
|
2455 | try: | |
2452 | globs = locs = where[0] |
|
2456 | globs = locs = where[0] | |
2453 | except: |
|
2457 | except: | |
2454 | globs = locs = globals() |
|
2458 | globs = locs = globals() | |
2455 | exec file(fname) in globs,locs |
|
2459 | exec file(fname) in globs,locs | |
2456 | else: |
|
2460 | else: | |
2457 | execfile(fname,*where) |
|
2461 | execfile(fname,*where) | |
2458 | except SyntaxError: |
|
2462 | except SyntaxError: | |
2459 | self.showsyntaxerror() |
|
2463 | self.showsyntaxerror() | |
2460 | warn('Failure executing file: <%s>' % fname) |
|
2464 | warn('Failure executing file: <%s>' % fname) | |
2461 | except SystemExit,status: |
|
2465 | except SystemExit,status: | |
2462 | if not kw['exit_ignore']: |
|
2466 | if not kw['exit_ignore']: | |
2463 | self.showtraceback() |
|
2467 | self.showtraceback() | |
2464 | warn('Failure executing file: <%s>' % fname) |
|
2468 | warn('Failure executing file: <%s>' % fname) | |
2465 | except: |
|
2469 | except: | |
2466 | self.showtraceback() |
|
2470 | self.showtraceback() | |
2467 | warn('Failure executing file: <%s>' % fname) |
|
2471 | warn('Failure executing file: <%s>' % fname) | |
2468 |
|
2472 | |||
2469 | syspath_cleanup() |
|
2473 | syspath_cleanup() | |
2470 |
|
2474 | |||
2471 | #************************* end of file <iplib.py> ***************************** |
|
2475 | #************************* end of file <iplib.py> ***************************** |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,129 +1,131 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | """An example of how to embed an IPython shell into a running program. |
|
3 | """An example of how to embed an IPython shell into a running program. | |
4 |
|
4 | |||
5 | Please see the documentation in the IPython.Shell module for more details. |
|
5 | Please see the documentation in the IPython.Shell module for more details. | |
6 |
|
6 | |||
7 | The accompanying file example-embed-short.py has quick code fragments for |
|
7 | The accompanying file example-embed-short.py has quick code fragments for | |
8 | embedding which you can cut and paste in your code once you understand how |
|
8 | embedding which you can cut and paste in your code once you understand how | |
9 | things work. |
|
9 | things work. | |
10 |
|
10 | |||
11 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
11 | The code in this file is deliberately extra-verbose, meant for learning.""" | |
12 |
|
12 | |||
13 | # The basics to get you going: |
|
13 | # The basics to get you going: | |
14 |
|
14 | |||
15 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
15 | # IPython sets the __IPYTHON__ variable so you can know if you have nested | |
16 | # copies running. |
|
16 | # copies running. | |
17 |
|
17 | |||
18 | # Try running this code both at the command line and from inside IPython (with |
|
18 | # Try running this code both at the command line and from inside IPython (with | |
19 | # %run example-embed.py) |
|
19 | # %run example-embed.py) | |
20 | try: |
|
20 | try: | |
21 | __IPYTHON__ |
|
21 | __IPYTHON__ | |
22 | except NameError: |
|
22 | except NameError: | |
23 | nested = 0 |
|
23 | nested = 0 | |
24 | args = [''] |
|
24 | args = [''] | |
25 | else: |
|
25 | else: | |
26 | print "Running nested copies of IPython." |
|
26 | print "Running nested copies of IPython." | |
27 | print "The prompts for the nested copy have been modified" |
|
27 | print "The prompts for the nested copy have been modified" | |
28 | nested = 1 |
|
28 | nested = 1 | |
29 | # what the embedded instance will see as sys.argv: |
|
29 | # what the embedded instance will see as sys.argv: | |
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', |
|
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
31 | '-po','Out<\\#>: ','-nosep'] |
|
31 | '-po','Out<\\#>: ','-nosep'] | |
32 |
|
32 | |||
33 | # First import the embeddable shell class |
|
33 | # First import the embeddable shell class | |
34 | from IPython.Shell import IPShellEmbed |
|
34 | from IPython.Shell import IPShellEmbed | |
35 |
|
35 | |||
36 | # Now create an instance of the embeddable shell. The first argument is a |
|
36 | # Now create an instance of the embeddable shell. The first argument is a | |
37 | # string with options exactly as you would type them if you were starting |
|
37 | # string with options exactly as you would type them if you were starting | |
38 | # IPython at the system command line. Any parameters you want to define for |
|
38 | # IPython at the system command line. Any parameters you want to define for | |
39 | # configuration can thus be specified here. |
|
39 | # configuration can thus be specified here. | |
40 | ipshell = IPShellEmbed(args, |
|
40 | ipshell = IPShellEmbed(args, | |
41 | banner = 'Dropping into IPython', |
|
41 | banner = 'Dropping into IPython', | |
42 | exit_msg = 'Leaving Interpreter, back to program.') |
|
42 | exit_msg = 'Leaving Interpreter, back to program.') | |
43 |
|
43 | |||
44 | # Make a second instance, you can have as many as you want. |
|
44 | # Make a second instance, you can have as many as you want. | |
45 | if nested: |
|
45 | if nested: | |
46 | args[1] = 'In2<\\#>' |
|
46 | args[1] = 'In2<\\#>' | |
47 | else: |
|
47 | else: | |
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', |
|
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
49 | '-po','Out<\\#>: ','-nosep'] |
|
49 | '-po','Out<\\#>: ','-nosep'] | |
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
51 |
|
51 | |||
52 | print '\nHello. This is printed from the main controller program.\n' |
|
52 | print '\nHello. This is printed from the main controller program.\n' | |
53 |
|
53 | |||
54 | # You can then call ipshell() anywhere you need it (with an optional |
|
54 | # You can then call ipshell() anywhere you need it (with an optional | |
55 | # message): |
|
55 | # message): | |
56 | ipshell('***Called from top level. ' |
|
56 | ipshell('***Called from top level. ' | |
57 |
'Hit Ctrl-D to exit interpreter and continue program.' |
|
57 | 'Hit Ctrl-D to exit interpreter and continue program.\n' | |
|
58 | 'Note that if you use %kill_embedded, you can fully deactivate\n' | |||
|
59 | 'This embedded instance so it will never turn on again') | |||
58 |
|
60 | |||
59 | print '\nBack in caller program, moving along...\n' |
|
61 | print '\nBack in caller program, moving along...\n' | |
60 |
|
62 | |||
61 | #--------------------------------------------------------------------------- |
|
63 | #--------------------------------------------------------------------------- | |
62 | # More details: |
|
64 | # More details: | |
63 |
|
65 | |||
64 | # IPShellEmbed instances don't print the standard system banner and |
|
66 | # IPShellEmbed instances don't print the standard system banner and | |
65 | # messages. The IPython banner (which actually may contain initialization |
|
67 | # messages. The IPython banner (which actually may contain initialization | |
66 | # messages) is available as <instance>.IP.BANNER in case you want it. |
|
68 | # messages) is available as <instance>.IP.BANNER in case you want it. | |
67 |
|
69 | |||
68 | # IPShellEmbed instances print the following information everytime they |
|
70 | # IPShellEmbed instances print the following information everytime they | |
69 | # start: |
|
71 | # start: | |
70 |
|
72 | |||
71 | # - A global startup banner. |
|
73 | # - A global startup banner. | |
72 |
|
74 | |||
73 | # - A call-specific header string, which you can use to indicate where in the |
|
75 | # - A call-specific header string, which you can use to indicate where in the | |
74 | # execution flow the shell is starting. |
|
76 | # execution flow the shell is starting. | |
75 |
|
77 | |||
76 | # They also print an exit message every time they exit. |
|
78 | # They also print an exit message every time they exit. | |
77 |
|
79 | |||
78 | # Both the startup banner and the exit message default to None, and can be set |
|
80 | # Both the startup banner and the exit message default to None, and can be set | |
79 | # either at the instance constructor or at any other time with the |
|
81 | # either at the instance constructor or at any other time with the | |
80 | # set_banner() and set_exit_msg() methods. |
|
82 | # set_banner() and set_exit_msg() methods. | |
81 |
|
83 | |||
82 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
84 | # The shell instance can be also put in 'dummy' mode globally or on a per-call | |
83 | # basis. This gives you fine control for debugging without having to change |
|
85 | # basis. This gives you fine control for debugging without having to change | |
84 | # code all over the place. |
|
86 | # code all over the place. | |
85 |
|
87 | |||
86 | # The code below illustrates all this. |
|
88 | # The code below illustrates all this. | |
87 |
|
89 | |||
88 |
|
90 | |||
89 | # This is how the global banner and exit_msg can be reset at any point |
|
91 | # This is how the global banner and exit_msg can be reset at any point | |
90 | ipshell.set_banner('Entering interpreter - New Banner') |
|
92 | ipshell.set_banner('Entering interpreter - New Banner') | |
91 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') |
|
93 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') | |
92 |
|
94 | |||
93 | def foo(m): |
|
95 | def foo(m): | |
94 | s = 'spam' |
|
96 | s = 'spam' | |
95 | ipshell('***In foo(). Try @whos, or print s or m:') |
|
97 | ipshell('***In foo(). Try @whos, or print s or m:') | |
96 | print 'foo says m = ',m |
|
98 | print 'foo says m = ',m | |
97 |
|
99 | |||
98 | def bar(n): |
|
100 | def bar(n): | |
99 | s = 'eggs' |
|
101 | s = 'eggs' | |
100 | ipshell('***In bar(). Try @whos, or print s or n:') |
|
102 | ipshell('***In bar(). Try @whos, or print s or n:') | |
101 | print 'bar says n = ',n |
|
103 | print 'bar says n = ',n | |
102 |
|
104 | |||
103 | # Some calls to the above functions which will trigger IPython: |
|
105 | # Some calls to the above functions which will trigger IPython: | |
104 | print 'Main program calling foo("eggs")\n' |
|
106 | print 'Main program calling foo("eggs")\n' | |
105 | foo('eggs') |
|
107 | foo('eggs') | |
106 |
|
108 | |||
107 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
109 | # The shell can be put in 'dummy' mode where calls to it silently return. This | |
108 | # allows you, for example, to globally turn off debugging for a program with a |
|
110 | # allows you, for example, to globally turn off debugging for a program with a | |
109 | # single call. |
|
111 | # single call. | |
110 | ipshell.set_dummy_mode(1) |
|
112 | ipshell.set_dummy_mode(1) | |
111 | print '\nTrying to call IPython which is now "dummy":' |
|
113 | print '\nTrying to call IPython which is now "dummy":' | |
112 | ipshell() |
|
114 | ipshell() | |
113 | print 'Nothing happened...' |
|
115 | print 'Nothing happened...' | |
114 | # The global 'dummy' mode can still be overridden for a single call |
|
116 | # The global 'dummy' mode can still be overridden for a single call | |
115 | print '\nOverriding dummy mode manually:' |
|
117 | print '\nOverriding dummy mode manually:' | |
116 | ipshell(dummy=0) |
|
118 | ipshell(dummy=0) | |
117 |
|
119 | |||
118 | # Reactivate the IPython shell |
|
120 | # Reactivate the IPython shell | |
119 | ipshell.set_dummy_mode(0) |
|
121 | ipshell.set_dummy_mode(0) | |
120 |
|
122 | |||
121 | print 'You can even have multiple embedded instances:' |
|
123 | print 'You can even have multiple embedded instances:' | |
122 | ipshell2() |
|
124 | ipshell2() | |
123 |
|
125 | |||
124 | print '\nMain program calling bar("spam")\n' |
|
126 | print '\nMain program calling bar("spam")\n' | |
125 | bar('spam') |
|
127 | bar('spam') | |
126 |
|
128 | |||
127 | print 'Main program finished. Bye!' |
|
129 | print 'Main program finished. Bye!' | |
128 |
|
130 | |||
129 | #********************** End of file <example-embed.py> *********************** |
|
131 | #********************** End of file <example-embed.py> *********************** |
General Comments 0
You need to be logged in to leave comments.
Login now