Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,3021 +1,3070 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 18 |
|
|
4 | $Id: Magic.py 1879 2006-11-04 00:34:34Z fptest $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
8 | 8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | #**************************************************************************** |
|
15 | 15 | # Modules and globals |
|
16 | 16 | |
|
17 | 17 | from IPython import Release |
|
18 | 18 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
19 | 19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | |
|
22 | 22 | # Python standard modules |
|
23 | 23 | import __builtin__ |
|
24 | 24 | import bdb |
|
25 | 25 | import inspect |
|
26 | 26 | import os |
|
27 | 27 | import pdb |
|
28 | 28 | import pydoc |
|
29 | 29 | import sys |
|
30 | 30 | import re |
|
31 | 31 | import tempfile |
|
32 | 32 | import time |
|
33 | 33 | import cPickle as pickle |
|
34 | 34 | import textwrap |
|
35 | 35 | from cStringIO import StringIO |
|
36 | 36 | from getopt import getopt,GetoptError |
|
37 | 37 | from pprint import pprint, pformat |
|
38 | 38 | |
|
39 | 39 | # profile isn't bundled by default in Debian for license reasons |
|
40 | 40 | try: |
|
41 | 41 | import profile,pstats |
|
42 | 42 | except ImportError: |
|
43 | 43 | profile = pstats = None |
|
44 | 44 | |
|
45 | 45 | # Homebrewed |
|
46 | 46 | import IPython |
|
47 | 47 | from IPython import Debugger, OInspect, wildcard |
|
48 | 48 | from IPython.FakeModule import FakeModule |
|
49 | 49 | from IPython.Itpl import Itpl, itpl, printpl,itplns |
|
50 | 50 | from IPython.PyColorize import Parser |
|
51 | 51 | from IPython.ipstruct import Struct |
|
52 | 52 | from IPython.macro import Macro |
|
53 | 53 | from IPython.genutils import * |
|
54 | 54 | from IPython import platutils |
|
55 | 55 | |
|
56 | 56 | #*************************************************************************** |
|
57 | 57 | # Utility functions |
|
58 | 58 | def on_off(tag): |
|
59 | 59 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
60 | 60 | return ['OFF','ON'][tag] |
|
61 | 61 | |
|
62 | 62 | class Bunch: pass |
|
63 | 63 | |
|
64 | 64 | #*************************************************************************** |
|
65 | 65 | # Main class implementing Magic functionality |
|
66 | 66 | class Magic: |
|
67 | 67 | """Magic functions for InteractiveShell. |
|
68 | 68 | |
|
69 | 69 | Shell functions which can be reached as %function_name. All magic |
|
70 | 70 | functions should accept a string, which they can parse for their own |
|
71 | 71 | needs. This can make some functions easier to type, eg `%cd ../` |
|
72 | 72 | vs. `%cd("../")` |
|
73 | 73 | |
|
74 | 74 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
75 | 75 | at the command line, but it is is needed in the definition. """ |
|
76 | 76 | |
|
77 | 77 | # class globals |
|
78 | 78 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
79 | 79 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
80 | 80 | |
|
81 | 81 | #...................................................................... |
|
82 | 82 | # some utility functions |
|
83 | 83 | |
|
84 | 84 | def __init__(self,shell): |
|
85 | 85 | |
|
86 | 86 | self.options_table = {} |
|
87 | 87 | if profile is None: |
|
88 | 88 | self.magic_prun = self.profile_missing_notice |
|
89 | 89 | self.shell = shell |
|
90 | 90 | |
|
91 | 91 | # namespace for holding state we may need |
|
92 | 92 | self._magic_state = Bunch() |
|
93 | 93 | |
|
94 | 94 | def profile_missing_notice(self, *args, **kwargs): |
|
95 | 95 | error("""\ |
|
96 | 96 | The profile module could not be found. If you are a Debian user, |
|
97 | 97 | it has been removed from the standard Debian package because of its non-free |
|
98 | 98 | license. To use profiling, please install"python2.3-profiler" from non-free.""") |
|
99 | 99 | |
|
100 | 100 | def default_option(self,fn,optstr): |
|
101 | 101 | """Make an entry in the options_table for fn, with value optstr""" |
|
102 | 102 | |
|
103 | 103 | if fn not in self.lsmagic(): |
|
104 | 104 | error("%s is not a magic function" % fn) |
|
105 | 105 | self.options_table[fn] = optstr |
|
106 | 106 | |
|
107 | 107 | def lsmagic(self): |
|
108 | 108 | """Return a list of currently available magic functions. |
|
109 | 109 | |
|
110 | 110 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
111 | 111 | ['magic_ls','magic_cd',...]""" |
|
112 | 112 | |
|
113 | 113 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
114 | 114 | |
|
115 | 115 | # magics in class definition |
|
116 | 116 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
117 | 117 | callable(Magic.__dict__[fn]) |
|
118 | 118 | # in instance namespace (run-time user additions) |
|
119 | 119 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
120 | 120 | callable(self.__dict__[fn]) |
|
121 | 121 | # and bound magics by user (so they can access self): |
|
122 | 122 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
123 | 123 | callable(self.__class__.__dict__[fn]) |
|
124 | 124 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
125 | 125 | filter(inst_magic,self.__dict__.keys()) + \ |
|
126 | 126 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
127 | 127 | out = [] |
|
128 | 128 | for fn in magics: |
|
129 | 129 | out.append(fn.replace('magic_','',1)) |
|
130 | 130 | out.sort() |
|
131 | 131 | return out |
|
132 | 132 | |
|
133 | 133 | def extract_input_slices(self,slices,raw=False): |
|
134 | 134 | """Return as a string a set of input history slices. |
|
135 | 135 | |
|
136 | 136 | Inputs: |
|
137 | 137 | |
|
138 | 138 | - slices: the set of slices is given as a list of strings (like |
|
139 | 139 | ['1','4:8','9'], since this function is for use by magic functions |
|
140 | 140 | which get their arguments as strings. |
|
141 | 141 | |
|
142 | 142 | Optional inputs: |
|
143 | 143 | |
|
144 | 144 | - raw(False): by default, the processed input is used. If this is |
|
145 | 145 | true, the raw input history is used instead. |
|
146 | 146 | |
|
147 | 147 | Note that slices can be called with two notations: |
|
148 | 148 | |
|
149 | 149 | N:M -> standard python form, means including items N...(M-1). |
|
150 | 150 | |
|
151 | 151 | N-M -> include items N..M (closed endpoint).""" |
|
152 | 152 | |
|
153 | 153 | if raw: |
|
154 | 154 | hist = self.shell.input_hist_raw |
|
155 | 155 | else: |
|
156 | 156 | hist = self.shell.input_hist |
|
157 | 157 | |
|
158 | 158 | cmds = [] |
|
159 | 159 | for chunk in slices: |
|
160 | 160 | if ':' in chunk: |
|
161 | 161 | ini,fin = map(int,chunk.split(':')) |
|
162 | 162 | elif '-' in chunk: |
|
163 | 163 | ini,fin = map(int,chunk.split('-')) |
|
164 | 164 | fin += 1 |
|
165 | 165 | else: |
|
166 | 166 | ini = int(chunk) |
|
167 | 167 | fin = ini+1 |
|
168 | 168 | cmds.append(hist[ini:fin]) |
|
169 | 169 | return cmds |
|
170 | 170 | |
|
171 | 171 | def _ofind(self, oname, namespaces=None): |
|
172 | 172 | """Find an object in the available namespaces. |
|
173 | 173 | |
|
174 | 174 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
175 | 175 | |
|
176 | 176 | Has special code to detect magic functions. |
|
177 | 177 | """ |
|
178 | 178 | |
|
179 | 179 | oname = oname.strip() |
|
180 | 180 | |
|
181 | 181 | alias_ns = None |
|
182 | 182 | if namespaces is None: |
|
183 | 183 | # Namespaces to search in: |
|
184 | 184 | # Put them in a list. The order is important so that we |
|
185 | 185 | # find things in the same order that Python finds them. |
|
186 | 186 | namespaces = [ ('Interactive', self.shell.user_ns), |
|
187 | 187 | ('IPython internal', self.shell.internal_ns), |
|
188 | 188 | ('Python builtin', __builtin__.__dict__), |
|
189 | 189 | ('Alias', self.shell.alias_table), |
|
190 | 190 | ] |
|
191 | 191 | alias_ns = self.shell.alias_table |
|
192 | 192 | |
|
193 | 193 | # initialize results to 'null' |
|
194 | 194 | found = 0; obj = None; ospace = None; ds = None; |
|
195 | 195 | ismagic = 0; isalias = 0; parent = None |
|
196 | 196 | |
|
197 | 197 | # Look for the given name by splitting it in parts. If the head is |
|
198 | 198 | # found, then we look for all the remaining parts as members, and only |
|
199 | 199 | # declare success if we can find them all. |
|
200 | 200 | oname_parts = oname.split('.') |
|
201 | 201 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
202 | 202 | for nsname,ns in namespaces: |
|
203 | 203 | try: |
|
204 | 204 | obj = ns[oname_head] |
|
205 | 205 | except KeyError: |
|
206 | 206 | continue |
|
207 | 207 | else: |
|
208 | 208 | for part in oname_rest: |
|
209 | 209 | try: |
|
210 | 210 | parent = obj |
|
211 | 211 | obj = getattr(obj,part) |
|
212 | 212 | except: |
|
213 | 213 | # Blanket except b/c some badly implemented objects |
|
214 | 214 | # allow __getattr__ to raise exceptions other than |
|
215 | 215 | # AttributeError, which then crashes IPython. |
|
216 | 216 | break |
|
217 | 217 | else: |
|
218 | 218 | # If we finish the for loop (no break), we got all members |
|
219 | 219 | found = 1 |
|
220 | 220 | ospace = nsname |
|
221 | 221 | if ns == alias_ns: |
|
222 | 222 | isalias = 1 |
|
223 | 223 | break # namespace loop |
|
224 | 224 | |
|
225 | 225 | # Try to see if it's magic |
|
226 | 226 | if not found: |
|
227 | 227 | if oname.startswith(self.shell.ESC_MAGIC): |
|
228 | 228 | oname = oname[1:] |
|
229 | 229 | obj = getattr(self,'magic_'+oname,None) |
|
230 | 230 | if obj is not None: |
|
231 | 231 | found = 1 |
|
232 | 232 | ospace = 'IPython internal' |
|
233 | 233 | ismagic = 1 |
|
234 | 234 | |
|
235 | 235 | # Last try: special-case some literals like '', [], {}, etc: |
|
236 | 236 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
237 | 237 | obj = eval(oname_head) |
|
238 | 238 | found = 1 |
|
239 | 239 | ospace = 'Interactive' |
|
240 | 240 | |
|
241 | 241 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
242 | 242 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
243 | 243 | |
|
244 | 244 | def arg_err(self,func): |
|
245 | 245 | """Print docstring if incorrect arguments were passed""" |
|
246 | 246 | print 'Error in arguments:' |
|
247 | 247 | print OInspect.getdoc(func) |
|
248 | 248 | |
|
249 | 249 | def format_latex(self,strng): |
|
250 | 250 | """Format a string for latex inclusion.""" |
|
251 | 251 | |
|
252 | 252 | # Characters that need to be escaped for latex: |
|
253 | 253 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) |
|
254 | 254 | # Magic command names as headers: |
|
255 | 255 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, |
|
256 | 256 | re.MULTILINE) |
|
257 | 257 | # Magic commands |
|
258 | 258 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, |
|
259 | 259 | re.MULTILINE) |
|
260 | 260 | # Paragraph continue |
|
261 | 261 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
262 | 262 | |
|
263 | 263 | # The "\n" symbol |
|
264 | 264 | newline_re = re.compile(r'\\n') |
|
265 | 265 | |
|
266 | 266 | # Now build the string for output: |
|
267 | 267 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
268 | 268 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
269 | 269 | strng) |
|
270 | 270 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
271 | 271 | strng = par_re.sub(r'\\\\',strng) |
|
272 | 272 | strng = escape_re.sub(r'\\\1',strng) |
|
273 | 273 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
274 | 274 | return strng |
|
275 | 275 | |
|
276 | 276 | def format_screen(self,strng): |
|
277 | 277 | """Format a string for screen printing. |
|
278 | 278 | |
|
279 | 279 | This removes some latex-type format codes.""" |
|
280 | 280 | # Paragraph continue |
|
281 | 281 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
282 | 282 | strng = par_re.sub('',strng) |
|
283 | 283 | return strng |
|
284 | 284 | |
|
285 | 285 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
286 | 286 | """Parse options passed to an argument string. |
|
287 | 287 | |
|
288 | 288 | The interface is similar to that of getopt(), but it returns back a |
|
289 | 289 | Struct with the options as keys and the stripped argument string still |
|
290 | 290 | as a string. |
|
291 | 291 | |
|
292 | 292 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
293 | 293 | This allows us to easily expand variables, glob files, quote |
|
294 | 294 | arguments, etc. |
|
295 | 295 | |
|
296 | 296 | Options: |
|
297 | 297 | -mode: default 'string'. If given as 'list', the argument string is |
|
298 | 298 | returned as a list (split on whitespace) instead of a string. |
|
299 | 299 | |
|
300 | 300 | -list_all: put all option values in lists. Normally only options |
|
301 | 301 | appearing more than once are put in a list. |
|
302 | 302 | |
|
303 | 303 | -posix (True): whether to split the input line in POSIX mode or not, |
|
304 | 304 | as per the conventions outlined in the shlex module from the |
|
305 | 305 | standard library.""" |
|
306 | 306 | |
|
307 | 307 | # inject default options at the beginning of the input line |
|
308 | 308 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
309 | 309 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
310 | 310 | |
|
311 | 311 | mode = kw.get('mode','string') |
|
312 | 312 | if mode not in ['string','list']: |
|
313 | 313 | raise ValueError,'incorrect mode given: %s' % mode |
|
314 | 314 | # Get options |
|
315 | 315 | list_all = kw.get('list_all',0) |
|
316 | 316 | posix = kw.get('posix',True) |
|
317 | 317 | |
|
318 | 318 | # Check if we have more than one argument to warrant extra processing: |
|
319 | 319 | odict = {} # Dictionary with options |
|
320 | 320 | args = arg_str.split() |
|
321 | 321 | if len(args) >= 1: |
|
322 | 322 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
323 | 323 | # need to look for options |
|
324 | 324 | argv = arg_split(arg_str,posix) |
|
325 | 325 | # Do regular option processing |
|
326 | 326 | try: |
|
327 | 327 | opts,args = getopt(argv,opt_str,*long_opts) |
|
328 | 328 | except GetoptError,e: |
|
329 | 329 | raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |
|
330 | 330 | " ".join(long_opts))) |
|
331 | 331 | for o,a in opts: |
|
332 | 332 | if o.startswith('--'): |
|
333 | 333 | o = o[2:] |
|
334 | 334 | else: |
|
335 | 335 | o = o[1:] |
|
336 | 336 | try: |
|
337 | 337 | odict[o].append(a) |
|
338 | 338 | except AttributeError: |
|
339 | 339 | odict[o] = [odict[o],a] |
|
340 | 340 | except KeyError: |
|
341 | 341 | if list_all: |
|
342 | 342 | odict[o] = [a] |
|
343 | 343 | else: |
|
344 | 344 | odict[o] = a |
|
345 | 345 | |
|
346 | 346 | # Prepare opts,args for return |
|
347 | 347 | opts = Struct(odict) |
|
348 | 348 | if mode == 'string': |
|
349 | 349 | args = ' '.join(args) |
|
350 | 350 | |
|
351 | 351 | return opts,args |
|
352 | 352 | |
|
353 | 353 | #...................................................................... |
|
354 | 354 | # And now the actual magic functions |
|
355 | 355 | |
|
356 | 356 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
357 | 357 | def magic_lsmagic(self, parameter_s = ''): |
|
358 | 358 | """List currently available magic functions.""" |
|
359 | 359 | mesc = self.shell.ESC_MAGIC |
|
360 | 360 | print 'Available magic functions:\n'+mesc+\ |
|
361 | 361 | (' '+mesc).join(self.lsmagic()) |
|
362 | 362 | print '\n' + Magic.auto_status[self.shell.rc.automagic] |
|
363 | 363 | return None |
|
364 | 364 | |
|
365 | 365 | def magic_magic(self, parameter_s = ''): |
|
366 | 366 | """Print information about the magic function system.""" |
|
367 | 367 | |
|
368 | 368 | mode = '' |
|
369 | 369 | try: |
|
370 | 370 | if parameter_s.split()[0] == '-latex': |
|
371 | 371 | mode = 'latex' |
|
372 | 372 | if parameter_s.split()[0] == '-brief': |
|
373 | 373 | mode = 'brief' |
|
374 | 374 | except: |
|
375 | 375 | pass |
|
376 | 376 | |
|
377 | 377 | magic_docs = [] |
|
378 | 378 | for fname in self.lsmagic(): |
|
379 | 379 | mname = 'magic_' + fname |
|
380 | 380 | for space in (Magic,self,self.__class__): |
|
381 | 381 | try: |
|
382 | 382 | fn = space.__dict__[mname] |
|
383 | 383 | except KeyError: |
|
384 | 384 | pass |
|
385 | 385 | else: |
|
386 | 386 | break |
|
387 | 387 | if mode == 'brief': |
|
388 | 388 | # only first line |
|
389 | 389 | fndoc = fn.__doc__.split('\n',1)[0] |
|
390 | 390 | else: |
|
391 | 391 | fndoc = fn.__doc__ |
|
392 | 392 | |
|
393 | 393 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, |
|
394 | 394 | fname,fndoc)) |
|
395 | 395 | magic_docs = ''.join(magic_docs) |
|
396 | 396 | |
|
397 | 397 | if mode == 'latex': |
|
398 | 398 | print self.format_latex(magic_docs) |
|
399 | 399 | return |
|
400 | 400 | else: |
|
401 | 401 | magic_docs = self.format_screen(magic_docs) |
|
402 | 402 | if mode == 'brief': |
|
403 | 403 | return magic_docs |
|
404 | 404 | |
|
405 | 405 | outmsg = """ |
|
406 | 406 | IPython's 'magic' functions |
|
407 | 407 | =========================== |
|
408 | 408 | |
|
409 | 409 | The magic function system provides a series of functions which allow you to |
|
410 | 410 | control the behavior of IPython itself, plus a lot of system-type |
|
411 | 411 | features. All these functions are prefixed with a % character, but parameters |
|
412 | 412 | are given without parentheses or quotes. |
|
413 | 413 | |
|
414 | 414 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
415 | 415 | %automagic function), you don't need to type in the % explicitly. By default, |
|
416 | 416 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
417 | 417 | |
|
418 | 418 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
419 | 419 | to 'mydir', if it exists. |
|
420 | 420 | |
|
421 | 421 | You can define your own magic functions to extend the system. See the supplied |
|
422 | 422 | ipythonrc and example-magic.py files for details (in your ipython |
|
423 | 423 | configuration directory, typically $HOME/.ipython/). |
|
424 | 424 | |
|
425 | 425 | You can also define your own aliased names for magic functions. In your |
|
426 | 426 | ipythonrc file, placing a line like: |
|
427 | 427 | |
|
428 | 428 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
429 | 429 | |
|
430 | 430 | will define %pf as a new name for %profile. |
|
431 | 431 | |
|
432 | 432 | You can also call magics in code using the ipmagic() function, which IPython |
|
433 | 433 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. |
|
434 | 434 | |
|
435 | 435 | For a list of the available magic functions, use %lsmagic. For a description |
|
436 | 436 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
437 | 437 | |
|
438 | 438 | Currently the magic system has the following functions:\n""" |
|
439 | 439 | |
|
440 | 440 | mesc = self.shell.ESC_MAGIC |
|
441 | 441 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
442 | 442 | "\n\n%s%s\n\n%s" % (outmsg, |
|
443 | 443 | magic_docs,mesc,mesc, |
|
444 | 444 | (' '+mesc).join(self.lsmagic()), |
|
445 | 445 | Magic.auto_status[self.shell.rc.automagic] ) ) |
|
446 | 446 | |
|
447 | 447 | page(outmsg,screen_lines=self.shell.rc.screen_length) |
|
448 | 448 | |
|
449 | 449 | def magic_automagic(self, parameter_s = ''): |
|
450 | 450 | """Make magic functions callable without having to type the initial %. |
|
451 | 451 | |
|
452 | 452 | Toggles on/off (when off, you must call it as %automagic, of |
|
453 | 453 | course). Note that magic functions have lowest priority, so if there's |
|
454 | 454 | a variable whose name collides with that of a magic fn, automagic |
|
455 | 455 | won't work for that function (you get the variable instead). However, |
|
456 | 456 | if you delete the variable (del var), the previously shadowed magic |
|
457 | 457 | function becomes visible to automagic again.""" |
|
458 | 458 | |
|
459 | 459 | rc = self.shell.rc |
|
460 | 460 | rc.automagic = not rc.automagic |
|
461 | 461 | print '\n' + Magic.auto_status[rc.automagic] |
|
462 | 462 | |
|
463 | 463 | def magic_autocall(self, parameter_s = ''): |
|
464 | 464 | """Make functions callable without having to type parentheses. |
|
465 | 465 | |
|
466 | 466 | Usage: |
|
467 | 467 | |
|
468 | 468 | %autocall [mode] |
|
469 | 469 | |
|
470 | 470 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the |
|
471 | 471 | value is toggled on and off (remembering the previous state).""" |
|
472 | 472 | |
|
473 | 473 | rc = self.shell.rc |
|
474 | 474 | |
|
475 | 475 | if parameter_s: |
|
476 | 476 | arg = int(parameter_s) |
|
477 | 477 | else: |
|
478 | 478 | arg = 'toggle' |
|
479 | 479 | |
|
480 | 480 | if not arg in (0,1,2,'toggle'): |
|
481 | 481 | error('Valid modes: (0->Off, 1->Smart, 2->Full') |
|
482 | 482 | return |
|
483 | 483 | |
|
484 | 484 | if arg in (0,1,2): |
|
485 | 485 | rc.autocall = arg |
|
486 | 486 | else: # toggle |
|
487 | 487 | if rc.autocall: |
|
488 | 488 | self._magic_state.autocall_save = rc.autocall |
|
489 | 489 | rc.autocall = 0 |
|
490 | 490 | else: |
|
491 | 491 | try: |
|
492 | 492 | rc.autocall = self._magic_state.autocall_save |
|
493 | 493 | except AttributeError: |
|
494 | 494 | rc.autocall = self._magic_state.autocall_save = 1 |
|
495 | 495 | |
|
496 | 496 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] |
|
497 | 497 | |
|
498 | 498 | def magic_autoindent(self, parameter_s = ''): |
|
499 | 499 | """Toggle autoindent on/off (if available).""" |
|
500 | 500 | |
|
501 | 501 | self.shell.set_autoindent() |
|
502 | 502 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
503 | 503 | |
|
504 | 504 | def magic_system_verbose(self, parameter_s = ''): |
|
505 |
""" |
|
|
505 | """Set verbose printing of system calls. | |
|
506 | 506 |
|
|
507 | self.shell.rc_set_toggle('system_verbose') | |
|
507 | If called without an argument, act as a toggle""" | |
|
508 | ||
|
509 | if parameter_s: | |
|
510 | val = bool(eval(parameter_s)) | |
|
511 | else: | |
|
512 | val = None | |
|
513 | ||
|
514 | self.shell.rc_set_toggle('system_verbose',val) | |
|
508 | 515 | print "System verbose printing is:",\ |
|
509 | 516 | ['OFF','ON'][self.shell.rc.system_verbose] |
|
510 | 517 | |
|
511 | 518 | def magic_history(self, parameter_s = ''): |
|
512 | 519 | """Print input history (_i<n> variables), with most recent last. |
|
513 | 520 | |
|
514 |
%history |
|
|
521 | %history -> print at most 40 inputs (some may be multi-line)\\ | |
|
515 | 522 | %history n -> print at most n inputs\\ |
|
516 | 523 | %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
517 | 524 | |
|
518 | 525 | Each input's number <n> is shown, and is accessible as the |
|
519 | 526 | automatically generated variable _i<n>. Multi-line statements are |
|
520 | 527 | printed starting at a new line for easy copy/paste. |
|
521 | 528 | |
|
522 | 529 | |
|
523 | 530 | Options: |
|
524 | 531 | |
|
525 | 532 | -n: do NOT print line numbers. This is useful if you want to get a |
|
526 | 533 | printout of many lines which can be directly pasted into a text |
|
527 | 534 | editor. |
|
528 | 535 | |
|
529 | 536 | This feature is only available if numbered prompts are in use. |
|
530 | 537 | |
|
531 | 538 | -r: print the 'raw' history. IPython filters your input and |
|
532 | 539 | converts it all into valid Python source before executing it (things |
|
533 | 540 | like magics or aliases are turned into function calls, for |
|
534 | 541 | example). With this option, you'll see the unfiltered history |
|
535 | 542 | instead of the filtered version: '%cd /' will be seen as '%cd /' |
|
536 | 543 | instead of '_ip.magic("%cd /")'. |
|
537 | 544 | """ |
|
538 | 545 | |
|
539 | 546 | shell = self.shell |
|
540 | 547 | if not shell.outputcache.do_full_cache: |
|
541 | 548 | print 'This feature is only available if numbered prompts are in use.' |
|
542 | 549 | return |
|
543 | 550 | opts,args = self.parse_options(parameter_s,'nr',mode='list') |
|
544 | 551 | |
|
545 | 552 | if opts.has_key('r'): |
|
546 | 553 | input_hist = shell.input_hist_raw |
|
547 | 554 | else: |
|
548 | 555 | input_hist = shell.input_hist |
|
549 | 556 | |
|
550 | 557 | default_length = 40 |
|
551 | 558 | if len(args) == 0: |
|
552 | 559 | final = len(input_hist) |
|
553 | 560 | init = max(1,final-default_length) |
|
554 | 561 | elif len(args) == 1: |
|
555 | 562 | final = len(input_hist) |
|
556 | 563 | init = max(1,final-int(args[0])) |
|
557 | 564 | elif len(args) == 2: |
|
558 | 565 | init,final = map(int,args) |
|
559 | 566 | else: |
|
560 | 567 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
561 | 568 | print self.magic_hist.__doc__ |
|
562 | 569 | return |
|
563 | 570 | width = len(str(final)) |
|
564 | 571 | line_sep = ['','\n'] |
|
565 | 572 | print_nums = not opts.has_key('n') |
|
566 | 573 | for in_num in range(init,final): |
|
567 | 574 | inline = input_hist[in_num] |
|
568 | 575 | multiline = int(inline.count('\n') > 1) |
|
569 | 576 | if print_nums: |
|
570 | 577 | print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), |
|
571 | 578 | print inline, |
|
572 | 579 | |
|
573 | 580 | def magic_hist(self, parameter_s=''): |
|
574 | 581 | """Alternate name for %history.""" |
|
575 | 582 | return self.magic_history(parameter_s) |
|
576 | 583 | |
|
577 | 584 | def magic_p(self, parameter_s=''): |
|
578 | 585 | """Just a short alias for Python's 'print'.""" |
|
579 | 586 | exec 'print ' + parameter_s in self.shell.user_ns |
|
580 | 587 | |
|
581 | 588 | def magic_r(self, parameter_s=''): |
|
582 | 589 | """Repeat previous input. |
|
583 | 590 | |
|
584 | 591 | If given an argument, repeats the previous command which starts with |
|
585 | 592 | the same string, otherwise it just repeats the previous input. |
|
586 | 593 | |
|
587 | 594 | Shell escaped commands (with ! as first character) are not recognized |
|
588 | 595 | by this system, only pure python code and magic commands. |
|
589 | 596 | """ |
|
590 | 597 | |
|
591 | 598 | start = parameter_s.strip() |
|
592 | 599 | esc_magic = self.shell.ESC_MAGIC |
|
593 | 600 | # Identify magic commands even if automagic is on (which means |
|
594 | 601 | # the in-memory version is different from that typed by the user). |
|
595 | 602 | if self.shell.rc.automagic: |
|
596 | 603 | start_magic = esc_magic+start |
|
597 | 604 | else: |
|
598 | 605 | start_magic = start |
|
599 | 606 | # Look through the input history in reverse |
|
600 | 607 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
601 | 608 | input = self.shell.input_hist[n] |
|
602 | 609 | # skip plain 'r' lines so we don't recurse to infinity |
|
603 | 610 | if input != '_ip.magic("r")\n' and \ |
|
604 | 611 | (input.startswith(start) or input.startswith(start_magic)): |
|
605 | 612 | #print 'match',`input` # dbg |
|
606 | 613 | print 'Executing:',input, |
|
607 | 614 | self.shell.runlines(input) |
|
608 | 615 | return |
|
609 | 616 | print 'No previous input matching `%s` found.' % start |
|
610 | 617 | |
|
611 | 618 | def magic_page(self, parameter_s=''): |
|
612 | 619 | """Pretty print the object and display it through a pager. |
|
613 | 620 | |
|
614 | 621 | If no parameter is given, use _ (last output).""" |
|
615 | 622 | # After a function contributed by Olivier Aubert, slightly modified. |
|
616 | 623 | |
|
617 | 624 | oname = parameter_s and parameter_s or '_' |
|
618 | 625 | info = self._ofind(oname) |
|
619 | 626 | if info['found']: |
|
620 | 627 | page(pformat(info['obj'])) |
|
621 | 628 | else: |
|
622 | 629 | print 'Object `%s` not found' % oname |
|
623 | 630 | |
|
624 | 631 | def magic_profile(self, parameter_s=''): |
|
625 | 632 | """Print your currently active IPyhton profile.""" |
|
626 | 633 | if self.shell.rc.profile: |
|
627 | 634 | printpl('Current IPython profile: $self.shell.rc.profile.') |
|
628 | 635 | else: |
|
629 | 636 | print 'No profile active.' |
|
630 | 637 | |
|
631 | 638 | def _inspect(self,meth,oname,namespaces=None,**kw): |
|
632 | 639 | """Generic interface to the inspector system. |
|
633 | 640 | |
|
634 | 641 | This function is meant to be called by pdef, pdoc & friends.""" |
|
635 | 642 | |
|
636 | 643 | oname = oname.strip() |
|
637 | 644 | info = Struct(self._ofind(oname, namespaces)) |
|
638 | 645 | |
|
639 | 646 | if info.found: |
|
640 | 647 | # Get the docstring of the class property if it exists. |
|
641 | 648 | path = oname.split('.') |
|
642 | 649 | root = '.'.join(path[:-1]) |
|
643 | 650 | if info.parent is not None: |
|
644 | 651 | try: |
|
645 | 652 | target = getattr(info.parent, '__class__') |
|
646 | 653 | # The object belongs to a class instance. |
|
647 | 654 | try: |
|
648 | 655 | target = getattr(target, path[-1]) |
|
649 | 656 | # The class defines the object. |
|
650 | 657 | if isinstance(target, property): |
|
651 | 658 | oname = root + '.__class__.' + path[-1] |
|
652 | 659 | info = Struct(self._ofind(oname)) |
|
653 | 660 | except AttributeError: pass |
|
654 | 661 | except AttributeError: pass |
|
655 | 662 | |
|
656 | 663 | pmethod = getattr(self.shell.inspector,meth) |
|
657 | 664 | formatter = info.ismagic and self.format_screen or None |
|
658 | 665 | if meth == 'pdoc': |
|
659 | 666 | pmethod(info.obj,oname,formatter) |
|
660 | 667 | elif meth == 'pinfo': |
|
661 | 668 | pmethod(info.obj,oname,formatter,info,**kw) |
|
662 | 669 | else: |
|
663 | 670 | pmethod(info.obj,oname) |
|
664 | 671 | else: |
|
665 | 672 | print 'Object `%s` not found.' % oname |
|
666 | 673 | return 'not found' # so callers can take other action |
|
667 | 674 | |
|
668 | 675 | def magic_pdef(self, parameter_s='', namespaces=None): |
|
669 | 676 | """Print the definition header for any callable object. |
|
670 | 677 | |
|
671 | 678 | If the object is a class, print the constructor information.""" |
|
672 | 679 | print "+++" |
|
673 | 680 | self._inspect('pdef',parameter_s, namespaces) |
|
674 | 681 | |
|
675 | 682 | def magic_pdoc(self, parameter_s='', namespaces=None): |
|
676 | 683 | """Print the docstring for an object. |
|
677 | 684 | |
|
678 | 685 | If the given object is a class, it will print both the class and the |
|
679 | 686 | constructor docstrings.""" |
|
680 | 687 | self._inspect('pdoc',parameter_s, namespaces) |
|
681 | 688 | |
|
682 | 689 | def magic_psource(self, parameter_s='', namespaces=None): |
|
683 | 690 | """Print (or run through pager) the source code for an object.""" |
|
684 | 691 | self._inspect('psource',parameter_s, namespaces) |
|
685 | 692 | |
|
686 | 693 | def magic_pfile(self, parameter_s=''): |
|
687 | 694 | """Print (or run through pager) the file where an object is defined. |
|
688 | 695 | |
|
689 | 696 | The file opens at the line where the object definition begins. IPython |
|
690 | 697 | will honor the environment variable PAGER if set, and otherwise will |
|
691 | 698 | do its best to print the file in a convenient form. |
|
692 | 699 | |
|
693 | 700 | If the given argument is not an object currently defined, IPython will |
|
694 | 701 | try to interpret it as a filename (automatically adding a .py extension |
|
695 | 702 | if needed). You can thus use %pfile as a syntax highlighting code |
|
696 | 703 | viewer.""" |
|
697 | 704 | |
|
698 | 705 | # first interpret argument as an object name |
|
699 | 706 | out = self._inspect('pfile',parameter_s) |
|
700 | 707 | # if not, try the input as a filename |
|
701 | 708 | if out == 'not found': |
|
702 | 709 | try: |
|
703 | 710 | filename = get_py_filename(parameter_s) |
|
704 | 711 | except IOError,msg: |
|
705 | 712 | print msg |
|
706 | 713 | return |
|
707 | 714 | page(self.shell.inspector.format(file(filename).read())) |
|
708 | 715 | |
|
709 | 716 | def magic_pinfo(self, parameter_s='', namespaces=None): |
|
710 | 717 | """Provide detailed information about an object. |
|
711 | 718 | |
|
712 | 719 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
713 | 720 | |
|
714 | 721 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
715 | 722 | |
|
716 | 723 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
717 | 724 | detail_level = 0 |
|
718 | 725 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
719 | 726 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
720 | 727 | pinfo,qmark1,oname,qmark2 = \ |
|
721 | 728 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
722 | 729 | if pinfo or qmark1 or qmark2: |
|
723 | 730 | detail_level = 1 |
|
724 | 731 | if "*" in oname: |
|
725 | 732 | self.magic_psearch(oname) |
|
726 | 733 | else: |
|
727 | 734 | self._inspect('pinfo', oname, detail_level=detail_level, |
|
728 | 735 | namespaces=namespaces) |
|
729 | 736 | |
|
730 | 737 | def magic_psearch(self, parameter_s=''): |
|
731 | 738 | """Search for object in namespaces by wildcard. |
|
732 | 739 | |
|
733 | 740 | %psearch [options] PATTERN [OBJECT TYPE] |
|
734 | 741 | |
|
735 | 742 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
736 | 743 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
737 | 744 | rest of the command line must be unchanged (options come first), so |
|
738 | 745 | for example the following forms are equivalent |
|
739 | 746 | |
|
740 | 747 | %psearch -i a* function |
|
741 | 748 | -i a* function? |
|
742 | 749 | ?-i a* function |
|
743 | 750 | |
|
744 | 751 | Arguments: |
|
745 | 752 | |
|
746 | 753 | PATTERN |
|
747 | 754 | |
|
748 | 755 | where PATTERN is a string containing * as a wildcard similar to its |
|
749 | 756 | use in a shell. The pattern is matched in all namespaces on the |
|
750 | 757 | search path. By default objects starting with a single _ are not |
|
751 | 758 | matched, many IPython generated objects have a single |
|
752 | 759 | underscore. The default is case insensitive matching. Matching is |
|
753 | 760 | also done on the attributes of objects and not only on the objects |
|
754 | 761 | in a module. |
|
755 | 762 | |
|
756 | 763 | [OBJECT TYPE] |
|
757 | 764 | |
|
758 | 765 | Is the name of a python type from the types module. The name is |
|
759 | 766 | given in lowercase without the ending type, ex. StringType is |
|
760 | 767 | written string. By adding a type here only objects matching the |
|
761 | 768 | given type are matched. Using all here makes the pattern match all |
|
762 | 769 | types (this is the default). |
|
763 | 770 | |
|
764 | 771 | Options: |
|
765 | 772 | |
|
766 | 773 | -a: makes the pattern match even objects whose names start with a |
|
767 | 774 | single underscore. These names are normally ommitted from the |
|
768 | 775 | search. |
|
769 | 776 | |
|
770 | 777 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
771 | 778 | these options is given, the default is read from your ipythonrc |
|
772 | 779 | file. The option name which sets this value is |
|
773 | 780 | 'wildcards_case_sensitive'. If this option is not specified in your |
|
774 | 781 | ipythonrc file, IPython's internal default is to do a case sensitive |
|
775 | 782 | search. |
|
776 | 783 | |
|
777 | 784 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
778 | 785 | specifiy can be searched in any of the following namespaces: |
|
779 | 786 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
780 | 787 | 'builtin' and 'user' are the search defaults. Note that you should |
|
781 | 788 | not use quotes when specifying namespaces. |
|
782 | 789 | |
|
783 | 790 | 'Builtin' contains the python module builtin, 'user' contains all |
|
784 | 791 | user data, 'alias' only contain the shell aliases and no python |
|
785 | 792 | objects, 'internal' contains objects used by IPython. The |
|
786 | 793 | 'user_global' namespace is only used by embedded IPython instances, |
|
787 | 794 | and it contains module-level globals. You can add namespaces to the |
|
788 | 795 | search with -s or exclude them with -e (these options can be given |
|
789 | 796 | more than once). |
|
790 | 797 | |
|
791 | 798 | Examples: |
|
792 | 799 | |
|
793 | 800 | %psearch a* -> objects beginning with an a |
|
794 | 801 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
795 | 802 | %psearch a* function -> all functions beginning with an a |
|
796 | 803 | %psearch re.e* -> objects beginning with an e in module re |
|
797 | 804 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
798 | 805 | %psearch r*.* string -> all strings in modules beginning with r |
|
799 | 806 | |
|
800 | 807 | Case sensitve search: |
|
801 | 808 | |
|
802 | 809 | %psearch -c a* list all object beginning with lower case a |
|
803 | 810 | |
|
804 | 811 | Show objects beginning with a single _: |
|
805 | 812 | |
|
806 | 813 | %psearch -a _* list objects beginning with a single underscore""" |
|
807 | 814 | |
|
808 | 815 | # default namespaces to be searched |
|
809 | 816 | def_search = ['user','builtin'] |
|
810 | 817 | |
|
811 | 818 | # Process options/args |
|
812 | 819 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
813 | 820 | opt = opts.get |
|
814 | 821 | shell = self.shell |
|
815 | 822 | psearch = shell.inspector.psearch |
|
816 | 823 | |
|
817 | 824 | # select case options |
|
818 | 825 | if opts.has_key('i'): |
|
819 | 826 | ignore_case = True |
|
820 | 827 | elif opts.has_key('c'): |
|
821 | 828 | ignore_case = False |
|
822 | 829 | else: |
|
823 | 830 | ignore_case = not shell.rc.wildcards_case_sensitive |
|
824 | 831 | |
|
825 | 832 | # Build list of namespaces to search from user options |
|
826 | 833 | def_search.extend(opt('s',[])) |
|
827 | 834 | ns_exclude = ns_exclude=opt('e',[]) |
|
828 | 835 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
829 | 836 | |
|
830 | 837 | # Call the actual search |
|
831 | 838 | try: |
|
832 | 839 | psearch(args,shell.ns_table,ns_search, |
|
833 | 840 | show_all=opt('a'),ignore_case=ignore_case) |
|
834 | 841 | except: |
|
835 | 842 | shell.showtraceback() |
|
836 | 843 | |
|
837 | 844 | def magic_who_ls(self, parameter_s=''): |
|
838 | 845 | """Return a sorted list of all interactive variables. |
|
839 | 846 | |
|
840 | 847 | If arguments are given, only variables of types matching these |
|
841 | 848 | arguments are returned.""" |
|
842 | 849 | |
|
843 | 850 | user_ns = self.shell.user_ns |
|
844 | 851 | internal_ns = self.shell.internal_ns |
|
845 | 852 | user_config_ns = self.shell.user_config_ns |
|
846 | 853 | out = [] |
|
847 | 854 | typelist = parameter_s.split() |
|
848 | 855 | |
|
849 | 856 | for i in user_ns: |
|
850 | 857 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
851 | 858 | and not (i in internal_ns or i in user_config_ns): |
|
852 | 859 | if typelist: |
|
853 | 860 | if type(user_ns[i]).__name__ in typelist: |
|
854 | 861 | out.append(i) |
|
855 | 862 | else: |
|
856 | 863 | out.append(i) |
|
857 | 864 | out.sort() |
|
858 | 865 | return out |
|
859 | 866 | |
|
860 | 867 | def magic_who(self, parameter_s=''): |
|
861 | 868 | """Print all interactive variables, with some minimal formatting. |
|
862 | 869 | |
|
863 | 870 | If any arguments are given, only variables whose type matches one of |
|
864 | 871 | these are printed. For example: |
|
865 | 872 | |
|
866 | 873 | %who function str |
|
867 | 874 | |
|
868 | 875 | will only list functions and strings, excluding all other types of |
|
869 | 876 | variables. To find the proper type names, simply use type(var) at a |
|
870 | 877 | command line to see how python prints type names. For example: |
|
871 | 878 | |
|
872 | 879 | In [1]: type('hello')\\ |
|
873 | 880 | Out[1]: <type 'str'> |
|
874 | 881 | |
|
875 | 882 | indicates that the type name for strings is 'str'. |
|
876 | 883 | |
|
877 | 884 | %who always excludes executed names loaded through your configuration |
|
878 | 885 | file and things which are internal to IPython. |
|
879 | 886 | |
|
880 | 887 | This is deliberate, as typically you may load many modules and the |
|
881 | 888 | purpose of %who is to show you only what you've manually defined.""" |
|
882 | 889 | |
|
883 | 890 | varlist = self.magic_who_ls(parameter_s) |
|
884 | 891 | if not varlist: |
|
885 | 892 | print 'Interactive namespace is empty.' |
|
886 | 893 | return |
|
887 | 894 | |
|
888 | 895 | # if we have variables, move on... |
|
889 | 896 | |
|
890 | 897 | # stupid flushing problem: when prompts have no separators, stdout is |
|
891 | 898 | # getting lost. I'm starting to think this is a python bug. I'm having |
|
892 | 899 | # to force a flush with a print because even a sys.stdout.flush |
|
893 | 900 | # doesn't seem to do anything! |
|
894 | 901 | |
|
895 | 902 | count = 0 |
|
896 | 903 | for i in varlist: |
|
897 | 904 | print i+'\t', |
|
898 | 905 | count += 1 |
|
899 | 906 | if count > 8: |
|
900 | 907 | count = 0 |
|
901 | 908 | |
|
902 | 909 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? |
|
903 | 910 | |
|
904 | 911 | print # well, this does force a flush at the expense of an extra \n |
|
905 | 912 | |
|
906 | 913 | def magic_whos(self, parameter_s=''): |
|
907 | 914 | """Like %who, but gives some extra information about each variable. |
|
908 | 915 | |
|
909 | 916 | The same type filtering of %who can be applied here. |
|
910 | 917 | |
|
911 | 918 | For all variables, the type is printed. Additionally it prints: |
|
912 | 919 | |
|
913 | 920 | - For {},[],(): their length. |
|
914 | 921 | |
|
915 | 922 | - For Numeric arrays, a summary with shape, number of elements, |
|
916 | 923 | typecode and size in memory. |
|
917 | 924 | |
|
918 | 925 | - Everything else: a string representation, snipping their middle if |
|
919 | 926 | too long.""" |
|
920 | 927 | |
|
921 | 928 | varnames = self.magic_who_ls(parameter_s) |
|
922 | 929 | if not varnames: |
|
923 | 930 | print 'Interactive namespace is empty.' |
|
924 | 931 | return |
|
925 | 932 | |
|
926 | 933 | # if we have variables, move on... |
|
927 | 934 | |
|
928 | 935 | # for these types, show len() instead of data: |
|
929 | 936 | seq_types = [types.DictType,types.ListType,types.TupleType] |
|
930 | 937 | |
|
931 | 938 | # for Numeric arrays, display summary info |
|
932 | 939 | try: |
|
933 | 940 | import Numeric |
|
934 | 941 | except ImportError: |
|
935 | 942 | array_type = None |
|
936 | 943 | else: |
|
937 | 944 | array_type = Numeric.ArrayType.__name__ |
|
938 | 945 | |
|
939 | 946 | # Find all variable names and types so we can figure out column sizes |
|
940 | 947 | get_vars = lambda i: self.shell.user_ns[i] |
|
941 | 948 | type_name = lambda v: type(v).__name__ |
|
942 | 949 | varlist = map(get_vars,varnames) |
|
943 | 950 | |
|
944 | 951 | typelist = [] |
|
945 | 952 | for vv in varlist: |
|
946 | 953 | tt = type_name(vv) |
|
947 | 954 | if tt=='instance': |
|
948 | 955 | typelist.append(str(vv.__class__)) |
|
949 | 956 | else: |
|
950 | 957 | typelist.append(tt) |
|
951 | 958 | |
|
952 | 959 | # column labels and # of spaces as separator |
|
953 | 960 | varlabel = 'Variable' |
|
954 | 961 | typelabel = 'Type' |
|
955 | 962 | datalabel = 'Data/Info' |
|
956 | 963 | colsep = 3 |
|
957 | 964 | # variable format strings |
|
958 | 965 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" |
|
959 | 966 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' |
|
960 | 967 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
961 | 968 | # find the size of the columns to format the output nicely |
|
962 | 969 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
963 | 970 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
964 | 971 | # table header |
|
965 | 972 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
966 | 973 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
967 | 974 | # and the table itself |
|
968 | 975 | kb = 1024 |
|
969 | 976 | Mb = 1048576 # kb**2 |
|
970 | 977 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
971 | 978 | print itpl(vformat), |
|
972 | 979 | if vtype in seq_types: |
|
973 | 980 | print len(var) |
|
974 | 981 | elif vtype==array_type: |
|
975 | 982 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
976 | 983 | vsize = Numeric.size(var) |
|
977 | 984 | vbytes = vsize*var.itemsize() |
|
978 | 985 | if vbytes < 100000: |
|
979 | 986 | print aformat % (vshape,vsize,var.typecode(),vbytes) |
|
980 | 987 | else: |
|
981 | 988 | print aformat % (vshape,vsize,var.typecode(),vbytes), |
|
982 | 989 | if vbytes < Mb: |
|
983 | 990 | print '(%s kb)' % (vbytes/kb,) |
|
984 | 991 | else: |
|
985 | 992 | print '(%s Mb)' % (vbytes/Mb,) |
|
986 | 993 | else: |
|
987 | 994 | vstr = str(var).replace('\n','\\n') |
|
988 | 995 | if len(vstr) < 50: |
|
989 | 996 | print vstr |
|
990 | 997 | else: |
|
991 | 998 | printpl(vfmt_short) |
|
992 | 999 | |
|
993 | 1000 | def magic_reset(self, parameter_s=''): |
|
994 | 1001 | """Resets the namespace by removing all names defined by the user. |
|
995 | 1002 | |
|
996 | 1003 | Input/Output history are left around in case you need them.""" |
|
997 | 1004 | |
|
998 | 1005 | ans = self.shell.ask_yes_no( |
|
999 | 1006 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ") |
|
1000 | 1007 | if not ans: |
|
1001 | 1008 | print 'Nothing done.' |
|
1002 | 1009 | return |
|
1003 | 1010 | user_ns = self.shell.user_ns |
|
1004 | 1011 | for i in self.magic_who_ls(): |
|
1005 | 1012 | del(user_ns[i]) |
|
1006 | 1013 | |
|
1007 | 1014 | def magic_config(self,parameter_s=''): |
|
1008 |
""" |
|
|
1009 | ||
|
1010 | page('Current configuration structure:\n'+ | |
|
1011 | pformat(self.shell.rc.dict())) | |
|
1015 | """Handle IPython's internal configuration. | |
|
1016 | ||
|
1017 | If called without arguments, it will print IPython's complete internal | |
|
1018 | configuration. | |
|
1019 | ||
|
1020 | If called with one argument, it will print the value of that key in | |
|
1021 | the configuration. | |
|
1022 | ||
|
1023 | If called with more than one argument, the first is interpreted as a | |
|
1024 | key and the rest as a Python expression which gets eval()'d. | |
|
1025 | ||
|
1026 | Examples: | |
|
1027 | ||
|
1028 | In [1]: s='A Python string' | |
|
1029 | ||
|
1030 | In [2]: !echo $s | |
|
1031 | A Python string | |
|
1032 | ||
|
1033 | In [3]: config system_verbose True | |
|
1034 | ||
|
1035 | In [4]: !echo $s | |
|
1036 | IPython system call: echo A Python string | |
|
1037 | A Python string | |
|
1038 | ||
|
1039 | In [5]: %config system_header 'sys> ' | |
|
1040 | ||
|
1041 | In [6]: !echo $s | |
|
1042 | sys> echo A Python string | |
|
1043 | A Python string | |
|
1044 | ||
|
1045 | # Notice the extra quotes to protect the string after interpolation: | |
|
1046 | In [7]: header = "'sys2> '" | |
|
1047 | ||
|
1048 | In [8]: %config system_header $header | |
|
1049 | ||
|
1050 | In [9]: !echo $s | |
|
1051 | sys2> echo A Python string | |
|
1052 | A Python string | |
|
1053 | """ | |
|
1054 | ||
|
1055 | args = parameter_s.split(None,1) | |
|
1056 | key = args[0] | |
|
1057 | if len(args)==1: | |
|
1058 | self.shell.ipconfig(key) | |
|
1059 | else: | |
|
1060 | self.shell.ipconfig(key,eval(args[1])) | |
|
1012 | 1061 | |
|
1013 | 1062 | def magic_logstart(self,parameter_s=''): |
|
1014 | 1063 | """Start logging anywhere in a session. |
|
1015 | 1064 | |
|
1016 | 1065 | %logstart [-o|-r|-t] [log_name [log_mode]] |
|
1017 | 1066 | |
|
1018 | 1067 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
1019 | 1068 | current directory, in 'rotate' mode (see below). |
|
1020 | 1069 | |
|
1021 | 1070 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
1022 | 1071 | history up to that point and then continues logging. |
|
1023 | 1072 | |
|
1024 | 1073 | %logstart takes a second optional parameter: logging mode. This can be one |
|
1025 | 1074 | of (note that the modes are given unquoted):\\ |
|
1026 | 1075 | append: well, that says it.\\ |
|
1027 | 1076 | backup: rename (if exists) to name~ and start name.\\ |
|
1028 | 1077 | global: single logfile in your home dir, appended to.\\ |
|
1029 | 1078 | over : overwrite existing log.\\ |
|
1030 | 1079 | rotate: create rotating logs name.1~, name.2~, etc. |
|
1031 | 1080 | |
|
1032 | 1081 | Options: |
|
1033 | 1082 | |
|
1034 | 1083 | -o: log also IPython's output. In this mode, all commands which |
|
1035 | 1084 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
1036 | 1085 | their corresponding input line. The output lines are always |
|
1037 | 1086 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
1038 | 1087 | Python code. |
|
1039 | 1088 | |
|
1040 | 1089 | Since this marker is always the same, filtering only the output from |
|
1041 | 1090 | a log is very easy, using for example a simple awk call: |
|
1042 | 1091 | |
|
1043 | 1092 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
1044 | 1093 | |
|
1045 | 1094 | -r: log 'raw' input. Normally, IPython's logs contain the processed |
|
1046 | 1095 | input, so that user lines are logged in their final form, converted |
|
1047 | 1096 | into valid Python. For example, %Exit is logged as |
|
1048 | 1097 | '_ip.magic("Exit"). If the -r flag is given, all input is logged |
|
1049 | 1098 | exactly as typed, with no transformations applied. |
|
1050 | 1099 | |
|
1051 | 1100 | -t: put timestamps before each input line logged (these are put in |
|
1052 | 1101 | comments).""" |
|
1053 | 1102 | |
|
1054 | 1103 | opts,par = self.parse_options(parameter_s,'ort') |
|
1055 | 1104 | log_output = 'o' in opts |
|
1056 | 1105 | log_raw_input = 'r' in opts |
|
1057 | 1106 | timestamp = 't' in opts |
|
1058 | 1107 | |
|
1059 | 1108 | rc = self.shell.rc |
|
1060 | 1109 | logger = self.shell.logger |
|
1061 | 1110 | |
|
1062 | 1111 | # if no args are given, the defaults set in the logger constructor by |
|
1063 | 1112 | # ipytohn remain valid |
|
1064 | 1113 | if par: |
|
1065 | 1114 | try: |
|
1066 | 1115 | logfname,logmode = par.split() |
|
1067 | 1116 | except: |
|
1068 | 1117 | logfname = par |
|
1069 | 1118 | logmode = 'backup' |
|
1070 | 1119 | else: |
|
1071 | 1120 | logfname = logger.logfname |
|
1072 | 1121 | logmode = logger.logmode |
|
1073 | 1122 | # put logfname into rc struct as if it had been called on the command |
|
1074 | 1123 | # line, so it ends up saved in the log header Save it in case we need |
|
1075 | 1124 | # to restore it... |
|
1076 | 1125 | old_logfile = rc.opts.get('logfile','') |
|
1077 | 1126 | if logfname: |
|
1078 | 1127 | logfname = os.path.expanduser(logfname) |
|
1079 | 1128 | rc.opts.logfile = logfname |
|
1080 | 1129 | loghead = self.shell.loghead_tpl % (rc.opts,rc.args) |
|
1081 | 1130 | try: |
|
1082 | 1131 | started = logger.logstart(logfname,loghead,logmode, |
|
1083 | 1132 | log_output,timestamp,log_raw_input) |
|
1084 | 1133 | except: |
|
1085 | 1134 | rc.opts.logfile = old_logfile |
|
1086 | 1135 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
1087 | 1136 | else: |
|
1088 | 1137 | # log input history up to this point, optionally interleaving |
|
1089 | 1138 | # output if requested |
|
1090 | 1139 | |
|
1091 | 1140 | if timestamp: |
|
1092 | 1141 | # disable timestamping for the previous history, since we've |
|
1093 | 1142 | # lost those already (no time machine here). |
|
1094 | 1143 | logger.timestamp = False |
|
1095 | 1144 | |
|
1096 | 1145 | if log_raw_input: |
|
1097 | 1146 | input_hist = self.shell.input_hist_raw |
|
1098 | 1147 | else: |
|
1099 | 1148 | input_hist = self.shell.input_hist |
|
1100 | 1149 | |
|
1101 | 1150 | if log_output: |
|
1102 | 1151 | log_write = logger.log_write |
|
1103 | 1152 | output_hist = self.shell.output_hist |
|
1104 | 1153 | for n in range(1,len(input_hist)-1): |
|
1105 | 1154 | log_write(input_hist[n].rstrip()) |
|
1106 | 1155 | if n in output_hist: |
|
1107 | 1156 | log_write(repr(output_hist[n]),'output') |
|
1108 | 1157 | else: |
|
1109 | 1158 | logger.log_write(input_hist[1:]) |
|
1110 | 1159 | if timestamp: |
|
1111 | 1160 | # re-enable timestamping |
|
1112 | 1161 | logger.timestamp = True |
|
1113 | 1162 | |
|
1114 | 1163 | print ('Activating auto-logging. ' |
|
1115 | 1164 | 'Current session state plus future input saved.') |
|
1116 | 1165 | logger.logstate() |
|
1117 | 1166 | |
|
1118 | 1167 | def magic_logoff(self,parameter_s=''): |
|
1119 | 1168 | """Temporarily stop logging. |
|
1120 | 1169 | |
|
1121 | 1170 | You must have previously started logging.""" |
|
1122 | 1171 | self.shell.logger.switch_log(0) |
|
1123 | 1172 | |
|
1124 | 1173 | def magic_logon(self,parameter_s=''): |
|
1125 | 1174 | """Restart logging. |
|
1126 | 1175 | |
|
1127 | 1176 | This function is for restarting logging which you've temporarily |
|
1128 | 1177 | stopped with %logoff. For starting logging for the first time, you |
|
1129 | 1178 | must use the %logstart function, which allows you to specify an |
|
1130 | 1179 | optional log filename.""" |
|
1131 | 1180 | |
|
1132 | 1181 | self.shell.logger.switch_log(1) |
|
1133 | 1182 | |
|
1134 | 1183 | def magic_logstate(self,parameter_s=''): |
|
1135 | 1184 | """Print the status of the logging system.""" |
|
1136 | 1185 | |
|
1137 | 1186 | self.shell.logger.logstate() |
|
1138 | 1187 | |
|
1139 | 1188 | def magic_pdb(self, parameter_s=''): |
|
1140 | 1189 | """Control the calling of the pdb interactive debugger. |
|
1141 | 1190 | |
|
1142 | 1191 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
1143 | 1192 | argument it works as a toggle. |
|
1144 | 1193 | |
|
1145 | 1194 | When an exception is triggered, IPython can optionally call the |
|
1146 | 1195 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
1147 | 1196 | this feature on and off.""" |
|
1148 | 1197 | |
|
1149 | 1198 | par = parameter_s.strip().lower() |
|
1150 | 1199 | |
|
1151 | 1200 | if par: |
|
1152 | 1201 | try: |
|
1153 | 1202 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
1154 | 1203 | except KeyError: |
|
1155 | 1204 | print ('Incorrect argument. Use on/1, off/0, ' |
|
1156 | 1205 | 'or nothing for a toggle.') |
|
1157 | 1206 | return |
|
1158 | 1207 | else: |
|
1159 | 1208 | # toggle |
|
1160 | 1209 | new_pdb = not self.shell.InteractiveTB.call_pdb |
|
1161 | 1210 | |
|
1162 | 1211 | # set on the shell |
|
1163 | 1212 | self.shell.call_pdb = new_pdb |
|
1164 | 1213 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
1165 | 1214 | |
|
1166 | 1215 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
1167 | 1216 | opts=None,arg_lst=None,prog_ns=None): |
|
1168 | 1217 | |
|
1169 | 1218 | """Run a statement through the python code profiler. |
|
1170 | 1219 | |
|
1171 | 1220 | Usage:\\ |
|
1172 | 1221 | %prun [options] statement |
|
1173 | 1222 | |
|
1174 | 1223 | The given statement (which doesn't require quote marks) is run via the |
|
1175 | 1224 | python profiler in a manner similar to the profile.run() function. |
|
1176 | 1225 | Namespaces are internally managed to work correctly; profile.run |
|
1177 | 1226 | cannot be used in IPython because it makes certain assumptions about |
|
1178 | 1227 | namespaces which do not hold under IPython. |
|
1179 | 1228 | |
|
1180 | 1229 | Options: |
|
1181 | 1230 | |
|
1182 | 1231 | -l <limit>: you can place restrictions on what or how much of the |
|
1183 | 1232 | profile gets printed. The limit value can be: |
|
1184 | 1233 | |
|
1185 | 1234 | * A string: only information for function names containing this string |
|
1186 | 1235 | is printed. |
|
1187 | 1236 | |
|
1188 | 1237 | * An integer: only these many lines are printed. |
|
1189 | 1238 | |
|
1190 | 1239 | * A float (between 0 and 1): this fraction of the report is printed |
|
1191 | 1240 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
1192 | 1241 | |
|
1193 | 1242 | You can combine several limits with repeated use of the option. For |
|
1194 | 1243 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
1195 | 1244 | information about class constructors. |
|
1196 | 1245 | |
|
1197 | 1246 | -r: return the pstats.Stats object generated by the profiling. This |
|
1198 | 1247 | object has all the information about the profile in it, and you can |
|
1199 | 1248 | later use it for further analysis or in other functions. |
|
1200 | 1249 | |
|
1201 | 1250 | Since magic functions have a particular form of calling which prevents |
|
1202 | 1251 | you from writing something like:\\ |
|
1203 | 1252 | In [1]: p = %prun -r print 4 # invalid!\\ |
|
1204 | 1253 | you must instead use IPython's automatic variables to assign this:\\ |
|
1205 | 1254 | In [1]: %prun -r print 4 \\ |
|
1206 | 1255 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ |
|
1207 | 1256 | In [2]: stats = _ |
|
1208 | 1257 | |
|
1209 | 1258 | If you really need to assign this value via an explicit function call, |
|
1210 | 1259 | you can always tap directly into the true name of the magic function |
|
1211 | 1260 | by using the _ip.magic function:\\ |
|
1212 | 1261 | In [3]: stats = _ip.magic('prun','-r print 4') |
|
1213 | 1262 | |
|
1214 | 1263 | You can type _ip.magic? for more details. |
|
1215 | 1264 | |
|
1216 | 1265 | -s <key>: sort profile by given key. You can provide more than one key |
|
1217 | 1266 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
1218 | 1267 | default sorting key is 'time'. |
|
1219 | 1268 | |
|
1220 | 1269 | The following is copied verbatim from the profile documentation |
|
1221 | 1270 | referenced below: |
|
1222 | 1271 | |
|
1223 | 1272 | When more than one key is provided, additional keys are used as |
|
1224 | 1273 | secondary criteria when the there is equality in all keys selected |
|
1225 | 1274 | before them. |
|
1226 | 1275 | |
|
1227 | 1276 | Abbreviations can be used for any key names, as long as the |
|
1228 | 1277 | abbreviation is unambiguous. The following are the keys currently |
|
1229 | 1278 | defined: |
|
1230 | 1279 | |
|
1231 | 1280 | Valid Arg Meaning\\ |
|
1232 | 1281 | "calls" call count\\ |
|
1233 | 1282 | "cumulative" cumulative time\\ |
|
1234 | 1283 | "file" file name\\ |
|
1235 | 1284 | "module" file name\\ |
|
1236 | 1285 | "pcalls" primitive call count\\ |
|
1237 | 1286 | "line" line number\\ |
|
1238 | 1287 | "name" function name\\ |
|
1239 | 1288 | "nfl" name/file/line\\ |
|
1240 | 1289 | "stdname" standard name\\ |
|
1241 | 1290 | "time" internal time |
|
1242 | 1291 | |
|
1243 | 1292 | Note that all sorts on statistics are in descending order (placing |
|
1244 | 1293 | most time consuming items first), where as name, file, and line number |
|
1245 | 1294 | searches are in ascending order (i.e., alphabetical). The subtle |
|
1246 | 1295 | distinction between "nfl" and "stdname" is that the standard name is a |
|
1247 | 1296 | sort of the name as printed, which means that the embedded line |
|
1248 | 1297 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
1249 | 1298 | would (if the file names were the same) appear in the string order |
|
1250 | 1299 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
1251 | 1300 | line numbers. In fact, sort_stats("nfl") is the same as |
|
1252 | 1301 | sort_stats("name", "file", "line"). |
|
1253 | 1302 | |
|
1254 | 1303 | -T <filename>: save profile results as shown on screen to a text |
|
1255 | 1304 | file. The profile is still shown on screen. |
|
1256 | 1305 | |
|
1257 | 1306 | -D <filename>: save (via dump_stats) profile statistics to given |
|
1258 | 1307 | filename. This data is in a format understod by the pstats module, and |
|
1259 | 1308 | is generated by a call to the dump_stats() method of profile |
|
1260 | 1309 | objects. The profile is still shown on screen. |
|
1261 | 1310 | |
|
1262 | 1311 | If you want to run complete programs under the profiler's control, use |
|
1263 | 1312 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
1264 | 1313 | contains profiler specific options as described here. |
|
1265 | 1314 | |
|
1266 | 1315 | You can read the complete documentation for the profile module with:\\ |
|
1267 | 1316 | In [1]: import profile; profile.help() """ |
|
1268 | 1317 | |
|
1269 | 1318 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
1270 | 1319 | # protect user quote marks |
|
1271 | 1320 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") |
|
1272 | 1321 | |
|
1273 | 1322 | if user_mode: # regular user call |
|
1274 | 1323 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', |
|
1275 | 1324 | list_all=1) |
|
1276 | 1325 | namespace = self.shell.user_ns |
|
1277 | 1326 | else: # called to run a program by %run -p |
|
1278 | 1327 | try: |
|
1279 | 1328 | filename = get_py_filename(arg_lst[0]) |
|
1280 | 1329 | except IOError,msg: |
|
1281 | 1330 | error(msg) |
|
1282 | 1331 | return |
|
1283 | 1332 | |
|
1284 | 1333 | arg_str = 'execfile(filename,prog_ns)' |
|
1285 | 1334 | namespace = locals() |
|
1286 | 1335 | |
|
1287 | 1336 | opts.merge(opts_def) |
|
1288 | 1337 | |
|
1289 | 1338 | prof = profile.Profile() |
|
1290 | 1339 | try: |
|
1291 | 1340 | prof = prof.runctx(arg_str,namespace,namespace) |
|
1292 | 1341 | sys_exit = '' |
|
1293 | 1342 | except SystemExit: |
|
1294 | 1343 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
1295 | 1344 | |
|
1296 | 1345 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
1297 | 1346 | |
|
1298 | 1347 | lims = opts.l |
|
1299 | 1348 | if lims: |
|
1300 | 1349 | lims = [] # rebuild lims with ints/floats/strings |
|
1301 | 1350 | for lim in opts.l: |
|
1302 | 1351 | try: |
|
1303 | 1352 | lims.append(int(lim)) |
|
1304 | 1353 | except ValueError: |
|
1305 | 1354 | try: |
|
1306 | 1355 | lims.append(float(lim)) |
|
1307 | 1356 | except ValueError: |
|
1308 | 1357 | lims.append(lim) |
|
1309 | 1358 | |
|
1310 | 1359 | # trap output |
|
1311 | 1360 | sys_stdout = sys.stdout |
|
1312 | 1361 | stdout_trap = StringIO() |
|
1313 | 1362 | try: |
|
1314 | 1363 | sys.stdout = stdout_trap |
|
1315 | 1364 | stats.print_stats(*lims) |
|
1316 | 1365 | finally: |
|
1317 | 1366 | sys.stdout = sys_stdout |
|
1318 | 1367 | output = stdout_trap.getvalue() |
|
1319 | 1368 | output = output.rstrip() |
|
1320 | 1369 | |
|
1321 | 1370 | page(output,screen_lines=self.shell.rc.screen_length) |
|
1322 | 1371 | print sys_exit, |
|
1323 | 1372 | |
|
1324 | 1373 | dump_file = opts.D[0] |
|
1325 | 1374 | text_file = opts.T[0] |
|
1326 | 1375 | if dump_file: |
|
1327 | 1376 | prof.dump_stats(dump_file) |
|
1328 | 1377 | print '\n*** Profile stats marshalled to file',\ |
|
1329 | 1378 | `dump_file`+'.',sys_exit |
|
1330 | 1379 | if text_file: |
|
1331 | 1380 | file(text_file,'w').write(output) |
|
1332 | 1381 | print '\n*** Profile printout saved to text file',\ |
|
1333 | 1382 | `text_file`+'.',sys_exit |
|
1334 | 1383 | |
|
1335 | 1384 | if opts.has_key('r'): |
|
1336 | 1385 | return stats |
|
1337 | 1386 | else: |
|
1338 | 1387 | return None |
|
1339 | 1388 | |
|
1340 | 1389 | def magic_run(self, parameter_s ='',runner=None): |
|
1341 | 1390 | """Run the named file inside IPython as a program. |
|
1342 | 1391 | |
|
1343 | 1392 | Usage:\\ |
|
1344 | 1393 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
1345 | 1394 | |
|
1346 | 1395 | Parameters after the filename are passed as command-line arguments to |
|
1347 | 1396 | the program (put in sys.argv). Then, control returns to IPython's |
|
1348 | 1397 | prompt. |
|
1349 | 1398 | |
|
1350 | 1399 | This is similar to running at a system prompt:\\ |
|
1351 | 1400 | $ python file args\\ |
|
1352 | 1401 | but with the advantage of giving you IPython's tracebacks, and of |
|
1353 | 1402 | loading all variables into your interactive namespace for further use |
|
1354 | 1403 | (unless -p is used, see below). |
|
1355 | 1404 | |
|
1356 | 1405 | The file is executed in a namespace initially consisting only of |
|
1357 | 1406 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1358 | 1407 | sees its environment as if it were being run as a stand-alone |
|
1359 | 1408 | program. But after execution, the IPython interactive namespace gets |
|
1360 | 1409 | updated with all variables defined in the program (except for __name__ |
|
1361 | 1410 | and sys.argv). This allows for very convenient loading of code for |
|
1362 | 1411 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1363 | 1412 | |
|
1364 | 1413 | Options: |
|
1365 | 1414 | |
|
1366 | 1415 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
1367 | 1416 | without extension (as python does under import). This allows running |
|
1368 | 1417 | scripts and reloading the definitions in them without calling code |
|
1369 | 1418 | protected by an ' if __name__ == "__main__" ' clause. |
|
1370 | 1419 | |
|
1371 | 1420 | -i: run the file in IPython's namespace instead of an empty one. This |
|
1372 | 1421 | is useful if you are experimenting with code written in a text editor |
|
1373 | 1422 | which depends on variables defined interactively. |
|
1374 | 1423 | |
|
1375 | 1424 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
1376 | 1425 | being run. This is particularly useful if IPython is being used to |
|
1377 | 1426 | run unittests, which always exit with a sys.exit() call. In such |
|
1378 | 1427 | cases you are interested in the output of the test results, not in |
|
1379 | 1428 | seeing a traceback of the unittest module. |
|
1380 | 1429 | |
|
1381 | 1430 | -t: print timing information at the end of the run. IPython will give |
|
1382 | 1431 | you an estimated CPU time consumption for your script, which under |
|
1383 | 1432 | Unix uses the resource module to avoid the wraparound problems of |
|
1384 | 1433 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
1385 | 1434 | is also given (for Windows platforms this is reported as 0.0). |
|
1386 | 1435 | |
|
1387 | 1436 | If -t is given, an additional -N<N> option can be given, where <N> |
|
1388 | 1437 | must be an integer indicating how many times you want the script to |
|
1389 | 1438 | run. The final timing report will include total and per run results. |
|
1390 | 1439 | |
|
1391 | 1440 | For example (testing the script uniq_stable.py): |
|
1392 | 1441 | |
|
1393 | 1442 | In [1]: run -t uniq_stable |
|
1394 | 1443 | |
|
1395 | 1444 | IPython CPU timings (estimated):\\ |
|
1396 | 1445 | User : 0.19597 s.\\ |
|
1397 | 1446 | System: 0.0 s.\\ |
|
1398 | 1447 | |
|
1399 | 1448 | In [2]: run -t -N5 uniq_stable |
|
1400 | 1449 | |
|
1401 | 1450 | IPython CPU timings (estimated):\\ |
|
1402 | 1451 | Total runs performed: 5\\ |
|
1403 | 1452 | Times : Total Per run\\ |
|
1404 | 1453 | User : 0.910862 s, 0.1821724 s.\\ |
|
1405 | 1454 | System: 0.0 s, 0.0 s. |
|
1406 | 1455 | |
|
1407 | 1456 | -d: run your program under the control of pdb, the Python debugger. |
|
1408 | 1457 | This allows you to execute your program step by step, watch variables, |
|
1409 | 1458 | etc. Internally, what IPython does is similar to calling: |
|
1410 | 1459 | |
|
1411 | 1460 | pdb.run('execfile("YOURFILENAME")') |
|
1412 | 1461 | |
|
1413 | 1462 | with a breakpoint set on line 1 of your file. You can change the line |
|
1414 | 1463 | number for this automatic breakpoint to be <N> by using the -bN option |
|
1415 | 1464 | (where N must be an integer). For example: |
|
1416 | 1465 | |
|
1417 | 1466 | %run -d -b40 myscript |
|
1418 | 1467 | |
|
1419 | 1468 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
1420 | 1469 | the first breakpoint must be set on a line which actually does |
|
1421 | 1470 | something (not a comment or docstring) for it to stop execution. |
|
1422 | 1471 | |
|
1423 | 1472 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
1424 | 1473 | first enter 'c' (without qoutes) to start execution up to the first |
|
1425 | 1474 | breakpoint. |
|
1426 | 1475 | |
|
1427 | 1476 | Entering 'help' gives information about the use of the debugger. You |
|
1428 | 1477 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
1429 | 1478 | at a prompt. |
|
1430 | 1479 | |
|
1431 | 1480 | -p: run program under the control of the Python profiler module (which |
|
1432 | 1481 | prints a detailed report of execution times, function calls, etc). |
|
1433 | 1482 | |
|
1434 | 1483 | You can pass other options after -p which affect the behavior of the |
|
1435 | 1484 | profiler itself. See the docs for %prun for details. |
|
1436 | 1485 | |
|
1437 | 1486 | In this mode, the program's variables do NOT propagate back to the |
|
1438 | 1487 | IPython interactive namespace (because they remain in the namespace |
|
1439 | 1488 | where the profiler executes them). |
|
1440 | 1489 | |
|
1441 | 1490 | Internally this triggers a call to %prun, see its documentation for |
|
1442 | 1491 | details on the options available specifically for profiling.""" |
|
1443 | 1492 | |
|
1444 | 1493 | # get arguments and set sys.argv for program to be run. |
|
1445 | 1494 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', |
|
1446 | 1495 | mode='list',list_all=1) |
|
1447 | 1496 | |
|
1448 | 1497 | try: |
|
1449 | 1498 | filename = get_py_filename(arg_lst[0]) |
|
1450 | 1499 | except IndexError: |
|
1451 | 1500 | warn('you must provide at least a filename.') |
|
1452 | 1501 | print '\n%run:\n',OInspect.getdoc(self.magic_run) |
|
1453 | 1502 | return |
|
1454 | 1503 | except IOError,msg: |
|
1455 | 1504 | error(msg) |
|
1456 | 1505 | return |
|
1457 | 1506 | |
|
1458 | 1507 | # Control the response to exit() calls made by the script being run |
|
1459 | 1508 | exit_ignore = opts.has_key('e') |
|
1460 | 1509 | |
|
1461 | 1510 | # Make sure that the running script gets a proper sys.argv as if it |
|
1462 | 1511 | # were run from a system shell. |
|
1463 | 1512 | save_argv = sys.argv # save it for later restoring |
|
1464 | 1513 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename |
|
1465 | 1514 | |
|
1466 | 1515 | if opts.has_key('i'): |
|
1467 | 1516 | prog_ns = self.shell.user_ns |
|
1468 | 1517 | __name__save = self.shell.user_ns['__name__'] |
|
1469 | 1518 | prog_ns['__name__'] = '__main__' |
|
1470 | 1519 | else: |
|
1471 | 1520 | if opts.has_key('n'): |
|
1472 | 1521 | name = os.path.splitext(os.path.basename(filename))[0] |
|
1473 | 1522 | else: |
|
1474 | 1523 | name = '__main__' |
|
1475 | 1524 | prog_ns = {'__name__':name} |
|
1476 | 1525 | |
|
1477 | 1526 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
1478 | 1527 | # set the __file__ global in the script's namespace |
|
1479 | 1528 | prog_ns['__file__'] = filename |
|
1480 | 1529 | |
|
1481 | 1530 | # pickle fix. See iplib for an explanation. But we need to make sure |
|
1482 | 1531 | # that, if we overwrite __main__, we replace it at the end |
|
1483 | 1532 | if prog_ns['__name__'] == '__main__': |
|
1484 | 1533 | restore_main = sys.modules['__main__'] |
|
1485 | 1534 | else: |
|
1486 | 1535 | restore_main = False |
|
1487 | 1536 | |
|
1488 | 1537 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) |
|
1489 | 1538 | |
|
1490 | 1539 | stats = None |
|
1491 | 1540 | try: |
|
1492 | 1541 | if self.shell.has_readline: |
|
1493 | 1542 | self.shell.savehist() |
|
1494 | 1543 | |
|
1495 | 1544 | if opts.has_key('p'): |
|
1496 | 1545 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
|
1497 | 1546 | else: |
|
1498 | 1547 | if opts.has_key('d'): |
|
1499 | 1548 | deb = Debugger.Pdb(self.shell.rc.colors) |
|
1500 | 1549 | # reset Breakpoint state, which is moronically kept |
|
1501 | 1550 | # in a class |
|
1502 | 1551 | bdb.Breakpoint.next = 1 |
|
1503 | 1552 | bdb.Breakpoint.bplist = {} |
|
1504 | 1553 | bdb.Breakpoint.bpbynumber = [None] |
|
1505 | 1554 | # Set an initial breakpoint to stop execution |
|
1506 | 1555 | maxtries = 10 |
|
1507 | 1556 | bp = int(opts.get('b',[1])[0]) |
|
1508 | 1557 | checkline = deb.checkline(filename,bp) |
|
1509 | 1558 | if not checkline: |
|
1510 | 1559 | for bp in range(bp+1,bp+maxtries+1): |
|
1511 | 1560 | if deb.checkline(filename,bp): |
|
1512 | 1561 | break |
|
1513 | 1562 | else: |
|
1514 | 1563 | msg = ("\nI failed to find a valid line to set " |
|
1515 | 1564 | "a breakpoint\n" |
|
1516 | 1565 | "after trying up to line: %s.\n" |
|
1517 | 1566 | "Please set a valid breakpoint manually " |
|
1518 | 1567 | "with the -b option." % bp) |
|
1519 | 1568 | error(msg) |
|
1520 | 1569 | return |
|
1521 | 1570 | # if we find a good linenumber, set the breakpoint |
|
1522 | 1571 | deb.do_break('%s:%s' % (filename,bp)) |
|
1523 | 1572 | # Start file run |
|
1524 | 1573 | print "NOTE: Enter 'c' at the", |
|
1525 | 1574 | print "%s prompt to start your script." % deb.prompt |
|
1526 | 1575 | try: |
|
1527 | 1576 | deb.run('execfile("%s")' % filename,prog_ns) |
|
1528 | 1577 | |
|
1529 | 1578 | except: |
|
1530 | 1579 | etype, value, tb = sys.exc_info() |
|
1531 | 1580 | # Skip three frames in the traceback: the %run one, |
|
1532 | 1581 | # one inside bdb.py, and the command-line typed by the |
|
1533 | 1582 | # user (run by exec in pdb itself). |
|
1534 | 1583 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) |
|
1535 | 1584 | else: |
|
1536 | 1585 | if runner is None: |
|
1537 | 1586 | runner = self.shell.safe_execfile |
|
1538 | 1587 | if opts.has_key('t'): |
|
1539 | 1588 | try: |
|
1540 | 1589 | nruns = int(opts['N'][0]) |
|
1541 | 1590 | if nruns < 1: |
|
1542 | 1591 | error('Number of runs must be >=1') |
|
1543 | 1592 | return |
|
1544 | 1593 | except (KeyError): |
|
1545 | 1594 | nruns = 1 |
|
1546 | 1595 | if nruns == 1: |
|
1547 | 1596 | t0 = clock2() |
|
1548 | 1597 | runner(filename,prog_ns,prog_ns, |
|
1549 | 1598 | exit_ignore=exit_ignore) |
|
1550 | 1599 | t1 = clock2() |
|
1551 | 1600 | t_usr = t1[0]-t0[0] |
|
1552 | 1601 | t_sys = t1[1]-t1[1] |
|
1553 | 1602 | print "\nIPython CPU timings (estimated):" |
|
1554 | 1603 | print " User : %10s s." % t_usr |
|
1555 | 1604 | print " System: %10s s." % t_sys |
|
1556 | 1605 | else: |
|
1557 | 1606 | runs = range(nruns) |
|
1558 | 1607 | t0 = clock2() |
|
1559 | 1608 | for nr in runs: |
|
1560 | 1609 | runner(filename,prog_ns,prog_ns, |
|
1561 | 1610 | exit_ignore=exit_ignore) |
|
1562 | 1611 | t1 = clock2() |
|
1563 | 1612 | t_usr = t1[0]-t0[0] |
|
1564 | 1613 | t_sys = t1[1]-t1[1] |
|
1565 | 1614 | print "\nIPython CPU timings (estimated):" |
|
1566 | 1615 | print "Total runs performed:",nruns |
|
1567 | 1616 | print " Times : %10s %10s" % ('Total','Per run') |
|
1568 | 1617 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) |
|
1569 | 1618 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) |
|
1570 | 1619 | |
|
1571 | 1620 | else: |
|
1572 | 1621 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1573 | 1622 | if opts.has_key('i'): |
|
1574 | 1623 | self.shell.user_ns['__name__'] = __name__save |
|
1575 | 1624 | else: |
|
1576 | 1625 | # update IPython interactive namespace |
|
1577 | 1626 | del prog_ns['__name__'] |
|
1578 | 1627 | self.shell.user_ns.update(prog_ns) |
|
1579 | 1628 | finally: |
|
1580 | 1629 | sys.argv = save_argv |
|
1581 | 1630 | if restore_main: |
|
1582 | 1631 | sys.modules['__main__'] = restore_main |
|
1583 | 1632 | if self.shell.has_readline: |
|
1584 | 1633 | self.shell.readline.read_history_file(self.shell.histfile) |
|
1585 | 1634 | |
|
1586 | 1635 | return stats |
|
1587 | 1636 | |
|
1588 | 1637 | def magic_runlog(self, parameter_s =''): |
|
1589 | 1638 | """Run files as logs. |
|
1590 | 1639 | |
|
1591 | 1640 | Usage:\\ |
|
1592 | 1641 | %runlog file1 file2 ... |
|
1593 | 1642 | |
|
1594 | 1643 | Run the named files (treating them as log files) in sequence inside |
|
1595 | 1644 | the interpreter, and return to the prompt. This is much slower than |
|
1596 | 1645 | %run because each line is executed in a try/except block, but it |
|
1597 | 1646 | allows running files with syntax errors in them. |
|
1598 | 1647 | |
|
1599 | 1648 | Normally IPython will guess when a file is one of its own logfiles, so |
|
1600 | 1649 | you can typically use %run even for logs. This shorthand allows you to |
|
1601 | 1650 | force any file to be treated as a log file.""" |
|
1602 | 1651 | |
|
1603 | 1652 | for f in parameter_s.split(): |
|
1604 | 1653 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1605 | 1654 | self.shell.user_ns,islog=1) |
|
1606 | 1655 | |
|
1607 | 1656 | def magic_timeit(self, parameter_s =''): |
|
1608 | 1657 | """Time execution of a Python statement or expression |
|
1609 | 1658 | |
|
1610 | 1659 | Usage:\\ |
|
1611 | 1660 | %timeit [-n<N> -r<R> [-t|-c]] statement |
|
1612 | 1661 | |
|
1613 | 1662 | Time execution of a Python statement or expression using the timeit |
|
1614 | 1663 | module. |
|
1615 | 1664 | |
|
1616 | 1665 | Options: |
|
1617 | 1666 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
1618 | 1667 | is not given, a fitting value is chosen. |
|
1619 | 1668 | |
|
1620 | 1669 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
1621 | 1670 | Default: 3 |
|
1622 | 1671 | |
|
1623 | 1672 | -t: use time.time to measure the time, which is the default on Unix. |
|
1624 | 1673 | This function measures wall time. |
|
1625 | 1674 | |
|
1626 | 1675 | -c: use time.clock to measure the time, which is the default on |
|
1627 | 1676 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
1628 | 1677 | instead and returns the CPU user time. |
|
1629 | 1678 | |
|
1630 | 1679 | -p<P>: use a precision of <P> digits to display the timing result. |
|
1631 | 1680 | Default: 3 |
|
1632 | 1681 | |
|
1633 | 1682 | |
|
1634 | 1683 | Examples:\\ |
|
1635 | 1684 | In [1]: %timeit pass |
|
1636 | 1685 | 10000000 loops, best of 3: 53.3 ns per loop |
|
1637 | 1686 | |
|
1638 | 1687 | In [2]: u = None |
|
1639 | 1688 | |
|
1640 | 1689 | In [3]: %timeit u is None |
|
1641 | 1690 | 10000000 loops, best of 3: 184 ns per loop |
|
1642 | 1691 | |
|
1643 | 1692 | In [4]: %timeit -r 4 u == None |
|
1644 | 1693 | 1000000 loops, best of 4: 242 ns per loop |
|
1645 | 1694 | |
|
1646 | 1695 | In [5]: import time |
|
1647 | 1696 | |
|
1648 | 1697 | In [6]: %timeit -n1 time.sleep(2) |
|
1649 | 1698 | 1 loops, best of 3: 2 s per loop |
|
1650 | 1699 | |
|
1651 | 1700 | |
|
1652 | 1701 | The times reported by %timeit will be slightly higher than those |
|
1653 | 1702 | reported by the timeit.py script when variables are accessed. This is |
|
1654 | 1703 | due to the fact that %timeit executes the statement in the namespace |
|
1655 | 1704 | of the shell, compared with timeit.py, which uses a single setup |
|
1656 | 1705 | statement to import function or create variables. Generally, the bias |
|
1657 | 1706 | does not matter as long as results from timeit.py are not mixed with |
|
1658 | 1707 | those from %timeit.""" |
|
1659 | 1708 | |
|
1660 | 1709 | import timeit |
|
1661 | 1710 | import math |
|
1662 | 1711 | |
|
1663 | 1712 | units = ["s", "ms", "\xc2\xb5s", "ns"] |
|
1664 | 1713 | scaling = [1, 1e3, 1e6, 1e9] |
|
1665 | 1714 | |
|
1666 | 1715 | opts, stmt = self.parse_options(parameter_s,'n:r:tcp:', |
|
1667 | 1716 | posix=False) |
|
1668 | 1717 | if stmt == "": |
|
1669 | 1718 | return |
|
1670 | 1719 | timefunc = timeit.default_timer |
|
1671 | 1720 | number = int(getattr(opts, "n", 0)) |
|
1672 | 1721 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
1673 | 1722 | precision = int(getattr(opts, "p", 3)) |
|
1674 | 1723 | if hasattr(opts, "t"): |
|
1675 | 1724 | timefunc = time.time |
|
1676 | 1725 | if hasattr(opts, "c"): |
|
1677 | 1726 | timefunc = clock |
|
1678 | 1727 | |
|
1679 | 1728 | timer = timeit.Timer(timer=timefunc) |
|
1680 | 1729 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
1681 | 1730 | # but is there a better way to achieve that the code stmt has access |
|
1682 | 1731 | # to the shell namespace? |
|
1683 | 1732 | |
|
1684 | 1733 | src = timeit.template % {'stmt': timeit.reindent(stmt, 8), |
|
1685 | 1734 | 'setup': "pass"} |
|
1686 | 1735 | code = compile(src, "<magic-timeit>", "exec") |
|
1687 | 1736 | ns = {} |
|
1688 | 1737 | exec code in self.shell.user_ns, ns |
|
1689 | 1738 | timer.inner = ns["inner"] |
|
1690 | 1739 | |
|
1691 | 1740 | if number == 0: |
|
1692 | 1741 | # determine number so that 0.2 <= total time < 2.0 |
|
1693 | 1742 | number = 1 |
|
1694 | 1743 | for i in range(1, 10): |
|
1695 | 1744 | number *= 10 |
|
1696 | 1745 | if timer.timeit(number) >= 0.2: |
|
1697 | 1746 | break |
|
1698 | 1747 | |
|
1699 | 1748 | best = min(timer.repeat(repeat, number)) / number |
|
1700 | 1749 | |
|
1701 | 1750 | if best > 0.0: |
|
1702 | 1751 | order = min(-int(math.floor(math.log10(best)) // 3), 3) |
|
1703 | 1752 | else: |
|
1704 | 1753 | order = 3 |
|
1705 | 1754 | print "%d loops, best of %d: %.*g %s per loop" % (number, repeat, |
|
1706 | 1755 | precision, |
|
1707 | 1756 | best * scaling[order], |
|
1708 | 1757 | units[order]) |
|
1709 | 1758 | |
|
1710 | 1759 | def magic_time(self,parameter_s = ''): |
|
1711 | 1760 | """Time execution of a Python statement or expression. |
|
1712 | 1761 | |
|
1713 | 1762 | The CPU and wall clock times are printed, and the value of the |
|
1714 | 1763 | expression (if any) is returned. Note that under Win32, system time |
|
1715 | 1764 | is always reported as 0, since it can not be measured. |
|
1716 | 1765 | |
|
1717 | 1766 | This function provides very basic timing functionality. In Python |
|
1718 | 1767 | 2.3, the timeit module offers more control and sophistication, so this |
|
1719 | 1768 | could be rewritten to use it (patches welcome). |
|
1720 | 1769 | |
|
1721 | 1770 | Some examples: |
|
1722 | 1771 | |
|
1723 | 1772 | In [1]: time 2**128 |
|
1724 | 1773 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1725 | 1774 | Wall time: 0.00 |
|
1726 | 1775 | Out[1]: 340282366920938463463374607431768211456L |
|
1727 | 1776 | |
|
1728 | 1777 | In [2]: n = 1000000 |
|
1729 | 1778 | |
|
1730 | 1779 | In [3]: time sum(range(n)) |
|
1731 | 1780 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1732 | 1781 | Wall time: 1.37 |
|
1733 | 1782 | Out[3]: 499999500000L |
|
1734 | 1783 | |
|
1735 | 1784 | In [4]: time print 'hello world' |
|
1736 | 1785 | hello world |
|
1737 | 1786 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1738 | 1787 | Wall time: 0.00 |
|
1739 | 1788 | """ |
|
1740 | 1789 | |
|
1741 | 1790 | # fail immediately if the given expression can't be compiled |
|
1742 | 1791 | try: |
|
1743 | 1792 | mode = 'eval' |
|
1744 | 1793 | code = compile(parameter_s,'<timed eval>',mode) |
|
1745 | 1794 | except SyntaxError: |
|
1746 | 1795 | mode = 'exec' |
|
1747 | 1796 | code = compile(parameter_s,'<timed exec>',mode) |
|
1748 | 1797 | # skew measurement as little as possible |
|
1749 | 1798 | glob = self.shell.user_ns |
|
1750 | 1799 | clk = clock2 |
|
1751 | 1800 | wtime = time.time |
|
1752 | 1801 | # time execution |
|
1753 | 1802 | wall_st = wtime() |
|
1754 | 1803 | if mode=='eval': |
|
1755 | 1804 | st = clk() |
|
1756 | 1805 | out = eval(code,glob) |
|
1757 | 1806 | end = clk() |
|
1758 | 1807 | else: |
|
1759 | 1808 | st = clk() |
|
1760 | 1809 | exec code in glob |
|
1761 | 1810 | end = clk() |
|
1762 | 1811 | out = None |
|
1763 | 1812 | wall_end = wtime() |
|
1764 | 1813 | # Compute actual times and report |
|
1765 | 1814 | wall_time = wall_end-wall_st |
|
1766 | 1815 | cpu_user = end[0]-st[0] |
|
1767 | 1816 | cpu_sys = end[1]-st[1] |
|
1768 | 1817 | cpu_tot = cpu_user+cpu_sys |
|
1769 | 1818 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
1770 | 1819 | (cpu_user,cpu_sys,cpu_tot) |
|
1771 | 1820 | print "Wall time: %.2f" % wall_time |
|
1772 | 1821 | return out |
|
1773 | 1822 | |
|
1774 | 1823 | def magic_macro(self,parameter_s = ''): |
|
1775 | 1824 | """Define a set of input lines as a macro for future re-execution. |
|
1776 | 1825 | |
|
1777 | 1826 | Usage:\\ |
|
1778 | 1827 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1779 | 1828 | |
|
1780 | 1829 | Options: |
|
1781 | 1830 | |
|
1782 | 1831 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1783 | 1832 | so that magics are loaded in their transformed version to valid |
|
1784 | 1833 | Python. If this option is given, the raw input as typed as the |
|
1785 | 1834 | command line is used instead. |
|
1786 | 1835 | |
|
1787 | 1836 | This will define a global variable called `name` which is a string |
|
1788 | 1837 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1789 | 1838 | above) from your input history into a single string. This variable |
|
1790 | 1839 | acts like an automatic function which re-executes those lines as if |
|
1791 | 1840 | you had typed them. You just type 'name' at the prompt and the code |
|
1792 | 1841 | executes. |
|
1793 | 1842 | |
|
1794 | 1843 | The notation for indicating number ranges is: n1-n2 means 'use line |
|
1795 | 1844 | numbers n1,...n2' (the endpoint is included). That is, '5-7' means |
|
1796 | 1845 | using the lines numbered 5,6 and 7. |
|
1797 | 1846 | |
|
1798 | 1847 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1799 | 1848 | notation, where N:M means numbers N through M-1. |
|
1800 | 1849 | |
|
1801 | 1850 | For example, if your history contains (%hist prints it): |
|
1802 | 1851 | |
|
1803 | 1852 | 44: x=1\\ |
|
1804 | 1853 | 45: y=3\\ |
|
1805 | 1854 | 46: z=x+y\\ |
|
1806 | 1855 | 47: print x\\ |
|
1807 | 1856 | 48: a=5\\ |
|
1808 | 1857 | 49: print 'x',x,'y',y\\ |
|
1809 | 1858 | |
|
1810 | 1859 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1811 | 1860 | called my_macro with: |
|
1812 | 1861 | |
|
1813 | 1862 | In [51]: %macro my_macro 44-47 49 |
|
1814 | 1863 | |
|
1815 | 1864 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1816 | 1865 | in one pass. |
|
1817 | 1866 | |
|
1818 | 1867 | You don't need to give the line-numbers in order, and any given line |
|
1819 | 1868 | number can appear multiple times. You can assemble macros with any |
|
1820 | 1869 | lines from your input history in any order. |
|
1821 | 1870 | |
|
1822 | 1871 | The macro is a simple object which holds its value in an attribute, |
|
1823 | 1872 | but IPython's display system checks for macros and executes them as |
|
1824 | 1873 | code instead of printing them when you type their name. |
|
1825 | 1874 | |
|
1826 | 1875 | You can view a macro's contents by explicitly printing it with: |
|
1827 | 1876 | |
|
1828 | 1877 | 'print macro_name'. |
|
1829 | 1878 | |
|
1830 | 1879 | For one-off cases which DON'T contain magic function calls in them you |
|
1831 | 1880 | can obtain similar results by explicitly executing slices from your |
|
1832 | 1881 | input history with: |
|
1833 | 1882 | |
|
1834 | 1883 | In [60]: exec In[44:48]+In[49]""" |
|
1835 | 1884 | |
|
1836 | 1885 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
1837 | 1886 | name,ranges = args[0], args[1:] |
|
1838 | 1887 | #print 'rng',ranges # dbg |
|
1839 | 1888 | lines = self.extract_input_slices(ranges,opts.has_key('r')) |
|
1840 | 1889 | macro = Macro(lines) |
|
1841 | 1890 | self.shell.user_ns.update({name:macro}) |
|
1842 | 1891 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1843 | 1892 | print 'Macro contents:' |
|
1844 | 1893 | print macro, |
|
1845 | 1894 | |
|
1846 | 1895 | def magic_save(self,parameter_s = ''): |
|
1847 | 1896 | """Save a set of lines to a given filename. |
|
1848 | 1897 | |
|
1849 | 1898 | Usage:\\ |
|
1850 | 1899 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
1851 | 1900 | |
|
1852 | 1901 | Options: |
|
1853 | 1902 | |
|
1854 | 1903 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1855 | 1904 | so that magics are loaded in their transformed version to valid |
|
1856 | 1905 | Python. If this option is given, the raw input as typed as the |
|
1857 | 1906 | command line is used instead. |
|
1858 | 1907 | |
|
1859 | 1908 | This function uses the same syntax as %macro for line extraction, but |
|
1860 | 1909 | instead of creating a macro it saves the resulting string to the |
|
1861 | 1910 | filename you specify. |
|
1862 | 1911 | |
|
1863 | 1912 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
1864 | 1913 | it asks for confirmation before overwriting existing files.""" |
|
1865 | 1914 | |
|
1866 | 1915 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
1867 | 1916 | fname,ranges = args[0], args[1:] |
|
1868 | 1917 | if not fname.endswith('.py'): |
|
1869 | 1918 | fname += '.py' |
|
1870 | 1919 | if os.path.isfile(fname): |
|
1871 | 1920 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) |
|
1872 | 1921 | if ans.lower() not in ['y','yes']: |
|
1873 | 1922 | print 'Operation cancelled.' |
|
1874 | 1923 | return |
|
1875 | 1924 | cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r'))) |
|
1876 | 1925 | f = file(fname,'w') |
|
1877 | 1926 | f.write(cmds) |
|
1878 | 1927 | f.close() |
|
1879 | 1928 | print 'The following commands were written to file `%s`:' % fname |
|
1880 | 1929 | print cmds |
|
1881 | 1930 | |
|
1882 | 1931 | def _edit_macro(self,mname,macro): |
|
1883 | 1932 | """open an editor with the macro data in a file""" |
|
1884 | 1933 | filename = self.shell.mktempfile(macro.value) |
|
1885 | 1934 | self.shell.hooks.editor(filename) |
|
1886 | 1935 | |
|
1887 | 1936 | # and make a new macro object, to replace the old one |
|
1888 | 1937 | mfile = open(filename) |
|
1889 | 1938 | mvalue = mfile.read() |
|
1890 | 1939 | mfile.close() |
|
1891 | 1940 | self.shell.user_ns[mname] = Macro(mvalue) |
|
1892 | 1941 | |
|
1893 | 1942 | def magic_ed(self,parameter_s=''): |
|
1894 | 1943 | """Alias to %edit.""" |
|
1895 | 1944 | return self.magic_edit(parameter_s) |
|
1896 | 1945 | |
|
1897 | 1946 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
1898 | 1947 | """Bring up an editor and execute the resulting code. |
|
1899 | 1948 | |
|
1900 | 1949 | Usage: |
|
1901 | 1950 | %edit [options] [args] |
|
1902 | 1951 | |
|
1903 | 1952 | %edit runs IPython's editor hook. The default version of this hook is |
|
1904 | 1953 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
1905 | 1954 | environment variable $EDITOR. If this isn't found, it will default to |
|
1906 | 1955 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
1907 | 1956 | docstring for how to change the editor hook. |
|
1908 | 1957 | |
|
1909 | 1958 | You can also set the value of this editor via the command line option |
|
1910 | 1959 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
1911 | 1960 | specifically for IPython an editor different from your typical default |
|
1912 | 1961 | (and for Windows users who typically don't set environment variables). |
|
1913 | 1962 | |
|
1914 | 1963 | This command allows you to conveniently edit multi-line code right in |
|
1915 | 1964 | your IPython session. |
|
1916 | 1965 | |
|
1917 | 1966 | If called without arguments, %edit opens up an empty editor with a |
|
1918 | 1967 | temporary file and will execute the contents of this file when you |
|
1919 | 1968 | close it (don't forget to save it!). |
|
1920 | 1969 | |
|
1921 | 1970 | |
|
1922 | 1971 | Options: |
|
1923 | 1972 | |
|
1924 | 1973 | -n <number>: open the editor at a specified line number. By default, |
|
1925 | 1974 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
1926 | 1975 | you can configure this by providing your own modified hook if your |
|
1927 | 1976 | favorite editor supports line-number specifications with a different |
|
1928 | 1977 | syntax. |
|
1929 | 1978 | |
|
1930 | 1979 | -p: this will call the editor with the same data as the previous time |
|
1931 | 1980 | it was used, regardless of how long ago (in your current session) it |
|
1932 | 1981 | was. |
|
1933 | 1982 | |
|
1934 | 1983 | -r: use 'raw' input. This option only applies to input taken from the |
|
1935 | 1984 | user's history. By default, the 'processed' history is used, so that |
|
1936 | 1985 | magics are loaded in their transformed version to valid Python. If |
|
1937 | 1986 | this option is given, the raw input as typed as the command line is |
|
1938 | 1987 | used instead. When you exit the editor, it will be executed by |
|
1939 | 1988 | IPython's own processor. |
|
1940 | 1989 | |
|
1941 | 1990 | -x: do not execute the edited code immediately upon exit. This is |
|
1942 | 1991 | mainly useful if you are editing programs which need to be called with |
|
1943 | 1992 | command line arguments, which you can then do using %run. |
|
1944 | 1993 | |
|
1945 | 1994 | |
|
1946 | 1995 | Arguments: |
|
1947 | 1996 | |
|
1948 | 1997 | If arguments are given, the following possibilites exist: |
|
1949 | 1998 | |
|
1950 | 1999 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
1951 | 2000 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
1952 | 2001 | loaded into the editor. The syntax is the same of the %macro command. |
|
1953 | 2002 | |
|
1954 | 2003 | - If the argument doesn't start with a number, it is evaluated as a |
|
1955 | 2004 | variable and its contents loaded into the editor. You can thus edit |
|
1956 | 2005 | any string which contains python code (including the result of |
|
1957 | 2006 | previous edits). |
|
1958 | 2007 | |
|
1959 | 2008 | - If the argument is the name of an object (other than a string), |
|
1960 | 2009 | IPython will try to locate the file where it was defined and open the |
|
1961 | 2010 | editor at the point where it is defined. You can use `%edit function` |
|
1962 | 2011 | to load an editor exactly at the point where 'function' is defined, |
|
1963 | 2012 | edit it and have the file be executed automatically. |
|
1964 | 2013 | |
|
1965 | 2014 | If the object is a macro (see %macro for details), this opens up your |
|
1966 | 2015 | specified editor with a temporary file containing the macro's data. |
|
1967 | 2016 | Upon exit, the macro is reloaded with the contents of the file. |
|
1968 | 2017 | |
|
1969 | 2018 | Note: opening at an exact line is only supported under Unix, and some |
|
1970 | 2019 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1971 | 2020 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1972 | 2021 | (X)Emacs, vi, jed, pico and joe all do. |
|
1973 | 2022 | |
|
1974 | 2023 | - If the argument is not found as a variable, IPython will look for a |
|
1975 | 2024 | file with that name (adding .py if necessary) and load it into the |
|
1976 | 2025 | editor. It will execute its contents with execfile() when you exit, |
|
1977 | 2026 | loading any code in the file into your interactive namespace. |
|
1978 | 2027 | |
|
1979 | 2028 | After executing your code, %edit will return as output the code you |
|
1980 | 2029 | typed in the editor (except when it was an existing file). This way |
|
1981 | 2030 | you can reload the code in further invocations of %edit as a variable, |
|
1982 | 2031 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
1983 | 2032 | the output. |
|
1984 | 2033 | |
|
1985 | 2034 | Note that %edit is also available through the alias %ed. |
|
1986 | 2035 | |
|
1987 | 2036 | This is an example of creating a simple function inside the editor and |
|
1988 | 2037 | then modifying it. First, start up the editor: |
|
1989 | 2038 | |
|
1990 | 2039 | In [1]: ed\\ |
|
1991 | 2040 | Editing... done. Executing edited code...\\ |
|
1992 | 2041 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' |
|
1993 | 2042 | |
|
1994 | 2043 | We can then call the function foo(): |
|
1995 | 2044 | |
|
1996 | 2045 | In [2]: foo()\\ |
|
1997 | 2046 | foo() was defined in an editing session |
|
1998 | 2047 | |
|
1999 | 2048 | Now we edit foo. IPython automatically loads the editor with the |
|
2000 | 2049 | (temporary) file where foo() was previously defined: |
|
2001 | 2050 | |
|
2002 | 2051 | In [3]: ed foo\\ |
|
2003 | 2052 | Editing... done. Executing edited code... |
|
2004 | 2053 | |
|
2005 | 2054 | And if we call foo() again we get the modified version: |
|
2006 | 2055 | |
|
2007 | 2056 | In [4]: foo()\\ |
|
2008 | 2057 | foo() has now been changed! |
|
2009 | 2058 | |
|
2010 | 2059 | Here is an example of how to edit a code snippet successive |
|
2011 | 2060 | times. First we call the editor: |
|
2012 | 2061 | |
|
2013 | 2062 | In [8]: ed\\ |
|
2014 | 2063 | Editing... done. Executing edited code...\\ |
|
2015 | 2064 | hello\\ |
|
2016 | 2065 | Out[8]: "print 'hello'\\n" |
|
2017 | 2066 | |
|
2018 | 2067 | Now we call it again with the previous output (stored in _): |
|
2019 | 2068 | |
|
2020 | 2069 | In [9]: ed _\\ |
|
2021 | 2070 | Editing... done. Executing edited code...\\ |
|
2022 | 2071 | hello world\\ |
|
2023 | 2072 | Out[9]: "print 'hello world'\\n" |
|
2024 | 2073 | |
|
2025 | 2074 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
2026 | 2075 | |
|
2027 | 2076 | In [10]: ed _8\\ |
|
2028 | 2077 | Editing... done. Executing edited code...\\ |
|
2029 | 2078 | hello again\\ |
|
2030 | 2079 | Out[10]: "print 'hello again'\\n" |
|
2031 | 2080 | |
|
2032 | 2081 | |
|
2033 | 2082 | Changing the default editor hook: |
|
2034 | 2083 | |
|
2035 | 2084 | If you wish to write your own editor hook, you can put it in a |
|
2036 | 2085 | configuration file which you load at startup time. The default hook |
|
2037 | 2086 | is defined in the IPython.hooks module, and you can use that as a |
|
2038 | 2087 | starting example for further modifications. That file also has |
|
2039 | 2088 | general instructions on how to set a new hook for use once you've |
|
2040 | 2089 | defined it.""" |
|
2041 | 2090 | |
|
2042 | 2091 | # FIXME: This function has become a convoluted mess. It needs a |
|
2043 | 2092 | # ground-up rewrite with clean, simple logic. |
|
2044 | 2093 | |
|
2045 | 2094 | def make_filename(arg): |
|
2046 | 2095 | "Make a filename from the given args" |
|
2047 | 2096 | try: |
|
2048 | 2097 | filename = get_py_filename(arg) |
|
2049 | 2098 | except IOError: |
|
2050 | 2099 | if args.endswith('.py'): |
|
2051 | 2100 | filename = arg |
|
2052 | 2101 | else: |
|
2053 | 2102 | filename = None |
|
2054 | 2103 | return filename |
|
2055 | 2104 | |
|
2056 | 2105 | # custom exceptions |
|
2057 | 2106 | class DataIsObject(Exception): pass |
|
2058 | 2107 | |
|
2059 | 2108 | opts,args = self.parse_options(parameter_s,'prxn:') |
|
2060 | 2109 | # Set a few locals from the options for convenience: |
|
2061 | 2110 | opts_p = opts.has_key('p') |
|
2062 | 2111 | opts_r = opts.has_key('r') |
|
2063 | 2112 | |
|
2064 | 2113 | # Default line number value |
|
2065 | 2114 | lineno = opts.get('n',None) |
|
2066 | 2115 | |
|
2067 | 2116 | if opts_p: |
|
2068 | 2117 | args = '_%s' % last_call[0] |
|
2069 | 2118 | if not self.shell.user_ns.has_key(args): |
|
2070 | 2119 | args = last_call[1] |
|
2071 | 2120 | |
|
2072 | 2121 | # use last_call to remember the state of the previous call, but don't |
|
2073 | 2122 | # let it be clobbered by successive '-p' calls. |
|
2074 | 2123 | try: |
|
2075 | 2124 | last_call[0] = self.shell.outputcache.prompt_count |
|
2076 | 2125 | if not opts_p: |
|
2077 | 2126 | last_call[1] = parameter_s |
|
2078 | 2127 | except: |
|
2079 | 2128 | pass |
|
2080 | 2129 | |
|
2081 | 2130 | # by default this is done with temp files, except when the given |
|
2082 | 2131 | # arg is a filename |
|
2083 | 2132 | use_temp = 1 |
|
2084 | 2133 | |
|
2085 | 2134 | if re.match(r'\d',args): |
|
2086 | 2135 | # Mode where user specifies ranges of lines, like in %macro. |
|
2087 | 2136 | # This means that you can't edit files whose names begin with |
|
2088 | 2137 | # numbers this way. Tough. |
|
2089 | 2138 | ranges = args.split() |
|
2090 | 2139 | data = ''.join(self.extract_input_slices(ranges,opts_r)) |
|
2091 | 2140 | elif args.endswith('.py'): |
|
2092 | 2141 | filename = make_filename(args) |
|
2093 | 2142 | data = '' |
|
2094 | 2143 | use_temp = 0 |
|
2095 | 2144 | elif args: |
|
2096 | 2145 | try: |
|
2097 | 2146 | # Load the parameter given as a variable. If not a string, |
|
2098 | 2147 | # process it as an object instead (below) |
|
2099 | 2148 | |
|
2100 | 2149 | #print '*** args',args,'type',type(args) # dbg |
|
2101 | 2150 | data = eval(args,self.shell.user_ns) |
|
2102 | 2151 | if not type(data) in StringTypes: |
|
2103 | 2152 | raise DataIsObject |
|
2104 | 2153 | |
|
2105 | 2154 | except (NameError,SyntaxError): |
|
2106 | 2155 | # given argument is not a variable, try as a filename |
|
2107 | 2156 | filename = make_filename(args) |
|
2108 | 2157 | if filename is None: |
|
2109 | 2158 | warn("Argument given (%s) can't be found as a variable " |
|
2110 | 2159 | "or as a filename." % args) |
|
2111 | 2160 | return |
|
2112 | 2161 | |
|
2113 | 2162 | data = '' |
|
2114 | 2163 | use_temp = 0 |
|
2115 | 2164 | except DataIsObject: |
|
2116 | 2165 | |
|
2117 | 2166 | # macros have a special edit function |
|
2118 | 2167 | if isinstance(data,Macro): |
|
2119 | 2168 | self._edit_macro(args,data) |
|
2120 | 2169 | return |
|
2121 | 2170 | |
|
2122 | 2171 | # For objects, try to edit the file where they are defined |
|
2123 | 2172 | try: |
|
2124 | 2173 | filename = inspect.getabsfile(data) |
|
2125 | 2174 | datafile = 1 |
|
2126 | 2175 | except TypeError: |
|
2127 | 2176 | filename = make_filename(args) |
|
2128 | 2177 | datafile = 1 |
|
2129 | 2178 | warn('Could not find file where `%s` is defined.\n' |
|
2130 | 2179 | 'Opening a file named `%s`' % (args,filename)) |
|
2131 | 2180 | # Now, make sure we can actually read the source (if it was in |
|
2132 | 2181 | # a temp file it's gone by now). |
|
2133 | 2182 | if datafile: |
|
2134 | 2183 | try: |
|
2135 | 2184 | if lineno is None: |
|
2136 | 2185 | lineno = inspect.getsourcelines(data)[1] |
|
2137 | 2186 | except IOError: |
|
2138 | 2187 | filename = make_filename(args) |
|
2139 | 2188 | if filename is None: |
|
2140 | 2189 | warn('The file `%s` where `%s` was defined cannot ' |
|
2141 | 2190 | 'be read.' % (filename,data)) |
|
2142 | 2191 | return |
|
2143 | 2192 | use_temp = 0 |
|
2144 | 2193 | else: |
|
2145 | 2194 | data = '' |
|
2146 | 2195 | |
|
2147 | 2196 | if use_temp: |
|
2148 | 2197 | filename = self.shell.mktempfile(data) |
|
2149 | 2198 | print 'IPython will make a temporary file named:',filename |
|
2150 | 2199 | |
|
2151 | 2200 | # do actual editing here |
|
2152 | 2201 | print 'Editing...', |
|
2153 | 2202 | sys.stdout.flush() |
|
2154 | 2203 | self.shell.hooks.editor(filename,lineno) |
|
2155 | 2204 | if opts.has_key('x'): # -x prevents actual execution |
|
2156 | 2205 | |
|
2157 | 2206 | else: |
|
2158 | 2207 | print 'done. Executing edited code...' |
|
2159 | 2208 | if opts_r: |
|
2160 | 2209 | self.shell.runlines(file_read(filename)) |
|
2161 | 2210 | else: |
|
2162 | 2211 | self.shell.safe_execfile(filename,self.shell.user_ns) |
|
2163 | 2212 | if use_temp: |
|
2164 | 2213 | try: |
|
2165 | 2214 | return open(filename).read() |
|
2166 | 2215 | except IOError,msg: |
|
2167 | 2216 | if msg.filename == filename: |
|
2168 | 2217 | warn('File not found. Did you forget to save?') |
|
2169 | 2218 | return |
|
2170 | 2219 | else: |
|
2171 | 2220 | self.shell.showtraceback() |
|
2172 | 2221 | |
|
2173 | 2222 | def magic_xmode(self,parameter_s = ''): |
|
2174 | 2223 | """Switch modes for the exception handlers. |
|
2175 | 2224 | |
|
2176 | 2225 | Valid modes: Plain, Context and Verbose. |
|
2177 | 2226 | |
|
2178 | 2227 | If called without arguments, acts as a toggle.""" |
|
2179 | 2228 | |
|
2180 | 2229 | def xmode_switch_err(name): |
|
2181 | 2230 | warn('Error changing %s exception modes.\n%s' % |
|
2182 | 2231 | (name,sys.exc_info()[1])) |
|
2183 | 2232 | |
|
2184 | 2233 | shell = self.shell |
|
2185 | 2234 | new_mode = parameter_s.strip().capitalize() |
|
2186 | 2235 | try: |
|
2187 | 2236 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
2188 | 2237 | print 'Exception reporting mode:',shell.InteractiveTB.mode |
|
2189 | 2238 | except: |
|
2190 | 2239 | xmode_switch_err('user') |
|
2191 | 2240 | |
|
2192 | 2241 | # threaded shells use a special handler in sys.excepthook |
|
2193 | 2242 | if shell.isthreaded: |
|
2194 | 2243 | try: |
|
2195 | 2244 | shell.sys_excepthook.set_mode(mode=new_mode) |
|
2196 | 2245 | except: |
|
2197 | 2246 | xmode_switch_err('threaded') |
|
2198 | 2247 | |
|
2199 | 2248 | def magic_colors(self,parameter_s = ''): |
|
2200 | 2249 | """Switch color scheme for prompts, info system and exception handlers. |
|
2201 | 2250 | |
|
2202 | 2251 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
2203 | 2252 | |
|
2204 | 2253 | Color scheme names are not case-sensitive.""" |
|
2205 | 2254 | |
|
2206 | 2255 | def color_switch_err(name): |
|
2207 | 2256 | warn('Error changing %s color schemes.\n%s' % |
|
2208 | 2257 | (name,sys.exc_info()[1])) |
|
2209 | 2258 | |
|
2210 | 2259 | |
|
2211 | 2260 | new_scheme = parameter_s.strip() |
|
2212 | 2261 | if not new_scheme: |
|
2213 | 2262 | print 'You must specify a color scheme.' |
|
2214 | 2263 | return |
|
2215 | 2264 | import IPython.rlineimpl as readline |
|
2216 | 2265 | if not readline.have_readline: |
|
2217 | 2266 | msg = """\ |
|
2218 | 2267 | Proper color support under MS Windows requires the pyreadline library. |
|
2219 | 2268 | You can find it at: |
|
2220 | 2269 | http://ipython.scipy.org/moin/PyReadline/Intro |
|
2221 | 2270 | Gary's readline needs the ctypes module, from: |
|
2222 | 2271 | http://starship.python.net/crew/theller/ctypes |
|
2223 | 2272 | (Note that ctypes is already part of Python versions 2.5 and newer). |
|
2224 | 2273 | |
|
2225 | 2274 | Defaulting color scheme to 'NoColor'""" |
|
2226 | 2275 | new_scheme = 'NoColor' |
|
2227 | 2276 | warn(msg) |
|
2228 | 2277 | # local shortcut |
|
2229 | 2278 | shell = self.shell |
|
2230 | 2279 | |
|
2231 | 2280 | # Set prompt colors |
|
2232 | 2281 | try: |
|
2233 | 2282 | shell.outputcache.set_colors(new_scheme) |
|
2234 | 2283 | except: |
|
2235 | 2284 | color_switch_err('prompt') |
|
2236 | 2285 | else: |
|
2237 | 2286 | shell.rc.colors = \ |
|
2238 | 2287 | shell.outputcache.color_table.active_scheme_name |
|
2239 | 2288 | # Set exception colors |
|
2240 | 2289 | try: |
|
2241 | 2290 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
2242 | 2291 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
2243 | 2292 | except: |
|
2244 | 2293 | color_switch_err('exception') |
|
2245 | 2294 | |
|
2246 | 2295 | # threaded shells use a verbose traceback in sys.excepthook |
|
2247 | 2296 | if shell.isthreaded: |
|
2248 | 2297 | try: |
|
2249 | 2298 | shell.sys_excepthook.set_colors(scheme=new_scheme) |
|
2250 | 2299 | except: |
|
2251 | 2300 | color_switch_err('system exception handler') |
|
2252 | 2301 | |
|
2253 | 2302 | # Set info (for 'object?') colors |
|
2254 | 2303 | if shell.rc.color_info: |
|
2255 | 2304 | try: |
|
2256 | 2305 | shell.inspector.set_active_scheme(new_scheme) |
|
2257 | 2306 | except: |
|
2258 | 2307 | color_switch_err('object inspector') |
|
2259 | 2308 | else: |
|
2260 | 2309 | shell.inspector.set_active_scheme('NoColor') |
|
2261 | 2310 | |
|
2262 | 2311 | def magic_color_info(self,parameter_s = ''): |
|
2263 | 2312 | """Toggle color_info. |
|
2264 | 2313 | |
|
2265 | 2314 | The color_info configuration parameter controls whether colors are |
|
2266 | 2315 | used for displaying object details (by things like %psource, %pfile or |
|
2267 | 2316 | the '?' system). This function toggles this value with each call. |
|
2268 | 2317 | |
|
2269 | 2318 | Note that unless you have a fairly recent pager (less works better |
|
2270 | 2319 | than more) in your system, using colored object information displays |
|
2271 | 2320 | will not work properly. Test it and see.""" |
|
2272 | 2321 | |
|
2273 | 2322 | self.shell.rc.color_info = 1 - self.shell.rc.color_info |
|
2274 | 2323 | self.magic_colors(self.shell.rc.colors) |
|
2275 | 2324 | print 'Object introspection functions have now coloring:', |
|
2276 | 2325 | print ['OFF','ON'][self.shell.rc.color_info] |
|
2277 | 2326 | |
|
2278 | 2327 | def magic_Pprint(self, parameter_s=''): |
|
2279 | 2328 | """Toggle pretty printing on/off.""" |
|
2280 | 2329 | |
|
2281 | 2330 | self.shell.rc.pprint = 1 - self.shell.rc.pprint |
|
2282 | 2331 | print 'Pretty printing has been turned', \ |
|
2283 | 2332 | ['OFF','ON'][self.shell.rc.pprint] |
|
2284 | 2333 | |
|
2285 | 2334 | def magic_exit(self, parameter_s=''): |
|
2286 | 2335 | """Exit IPython, confirming if configured to do so. |
|
2287 | 2336 | |
|
2288 | 2337 | You can configure whether IPython asks for confirmation upon exit by |
|
2289 | 2338 | setting the confirm_exit flag in the ipythonrc file.""" |
|
2290 | 2339 | |
|
2291 | 2340 | self.shell.exit() |
|
2292 | 2341 | |
|
2293 | 2342 | def magic_quit(self, parameter_s=''): |
|
2294 | 2343 | """Exit IPython, confirming if configured to do so (like %exit)""" |
|
2295 | 2344 | |
|
2296 | 2345 | self.shell.exit() |
|
2297 | 2346 | |
|
2298 | 2347 | def magic_Exit(self, parameter_s=''): |
|
2299 | 2348 | """Exit IPython without confirmation.""" |
|
2300 | 2349 | |
|
2301 | 2350 | self.shell.exit_now = True |
|
2302 | 2351 | |
|
2303 | 2352 | def magic_Quit(self, parameter_s=''): |
|
2304 | 2353 | """Exit IPython without confirmation (like %Exit).""" |
|
2305 | 2354 | |
|
2306 | 2355 | self.shell.exit_now = True |
|
2307 | 2356 | |
|
2308 | 2357 | #...................................................................... |
|
2309 | 2358 | # Functions to implement unix shell-type things |
|
2310 | 2359 | |
|
2311 | 2360 | def magic_alias(self, parameter_s = ''): |
|
2312 | 2361 | """Define an alias for a system command. |
|
2313 | 2362 | |
|
2314 | 2363 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
2315 | 2364 | |
|
2316 | 2365 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
2317 | 2366 | params' (from your underlying operating system). |
|
2318 | 2367 | |
|
2319 | 2368 | Aliases have lower precedence than magic functions and Python normal |
|
2320 | 2369 | variables, so if 'foo' is both a Python variable and an alias, the |
|
2321 | 2370 | alias can not be executed until 'del foo' removes the Python variable. |
|
2322 | 2371 | |
|
2323 | 2372 | You can use the %l specifier in an alias definition to represent the |
|
2324 | 2373 | whole line when the alias is called. For example: |
|
2325 | 2374 | |
|
2326 | 2375 | In [2]: alias all echo "Input in brackets: <%l>"\\ |
|
2327 | 2376 | In [3]: all hello world\\ |
|
2328 | 2377 | Input in brackets: <hello world> |
|
2329 | 2378 | |
|
2330 | 2379 | You can also define aliases with parameters using %s specifiers (one |
|
2331 | 2380 | per parameter): |
|
2332 | 2381 | |
|
2333 | 2382 | In [1]: alias parts echo first %s second %s\\ |
|
2334 | 2383 | In [2]: %parts A B\\ |
|
2335 | 2384 | first A second B\\ |
|
2336 | 2385 | In [3]: %parts A\\ |
|
2337 | 2386 | Incorrect number of arguments: 2 expected.\\ |
|
2338 | 2387 | parts is an alias to: 'echo first %s second %s' |
|
2339 | 2388 | |
|
2340 | 2389 | Note that %l and %s are mutually exclusive. You can only use one or |
|
2341 | 2390 | the other in your aliases. |
|
2342 | 2391 | |
|
2343 | 2392 | Aliases expand Python variables just like system calls using ! or !! |
|
2344 | 2393 | do: all expressions prefixed with '$' get expanded. For details of |
|
2345 | 2394 | the semantic rules, see PEP-215: |
|
2346 | 2395 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
2347 | 2396 | IPython for variable expansion. If you want to access a true shell |
|
2348 | 2397 | variable, an extra $ is necessary to prevent its expansion by IPython: |
|
2349 | 2398 | |
|
2350 | 2399 | In [6]: alias show echo\\ |
|
2351 | 2400 | In [7]: PATH='A Python string'\\ |
|
2352 | 2401 | In [8]: show $PATH\\ |
|
2353 | 2402 | A Python string\\ |
|
2354 | 2403 | In [9]: show $$PATH\\ |
|
2355 | 2404 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
2356 | 2405 | |
|
2357 | 2406 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
2358 | 2407 | and %rehashx functions, which automatically create aliases for the |
|
2359 | 2408 | contents of your $PATH. |
|
2360 | 2409 | |
|
2361 | 2410 | If called with no parameters, %alias prints the current alias table.""" |
|
2362 | 2411 | |
|
2363 | 2412 | par = parameter_s.strip() |
|
2364 | 2413 | if not par: |
|
2365 | 2414 | stored = self.db.get('stored_aliases', {} ) |
|
2366 | 2415 | atab = self.shell.alias_table |
|
2367 | 2416 | aliases = atab.keys() |
|
2368 | 2417 | aliases.sort() |
|
2369 | 2418 | res = [] |
|
2370 | 2419 | showlast = [] |
|
2371 | 2420 | for alias in aliases: |
|
2372 | 2421 | tgt = atab[alias][1] |
|
2373 | 2422 | # 'interesting' aliases |
|
2374 | 2423 | if (alias in stored or |
|
2375 | 2424 | alias != os.path.splitext(tgt)[0] or |
|
2376 | 2425 | ' ' in tgt): |
|
2377 | 2426 | showlast.append((alias, tgt)) |
|
2378 | 2427 | else: |
|
2379 | 2428 | res.append((alias, tgt )) |
|
2380 | 2429 | |
|
2381 | 2430 | # show most interesting aliases last |
|
2382 | 2431 | res.extend(showlast) |
|
2383 | 2432 | print "Total number of aliases:",len(aliases) |
|
2384 | 2433 | return res |
|
2385 | 2434 | try: |
|
2386 | 2435 | alias,cmd = par.split(None,1) |
|
2387 | 2436 | except: |
|
2388 | 2437 | print OInspect.getdoc(self.magic_alias) |
|
2389 | 2438 | else: |
|
2390 | 2439 | nargs = cmd.count('%s') |
|
2391 | 2440 | if nargs>0 and cmd.find('%l')>=0: |
|
2392 | 2441 | error('The %s and %l specifiers are mutually exclusive ' |
|
2393 | 2442 | 'in alias definitions.') |
|
2394 | 2443 | else: # all looks OK |
|
2395 | 2444 | self.shell.alias_table[alias] = (nargs,cmd) |
|
2396 | 2445 | self.shell.alias_table_validate(verbose=0) |
|
2397 | 2446 | # end magic_alias |
|
2398 | 2447 | |
|
2399 | 2448 | def magic_unalias(self, parameter_s = ''): |
|
2400 | 2449 | """Remove an alias""" |
|
2401 | 2450 | |
|
2402 | 2451 | aname = parameter_s.strip() |
|
2403 | 2452 | if aname in self.shell.alias_table: |
|
2404 | 2453 | del self.shell.alias_table[aname] |
|
2405 | 2454 | stored = self.db.get('stored_aliases', {} ) |
|
2406 | 2455 | if aname in stored: |
|
2407 | 2456 | print "Removing %stored alias",aname |
|
2408 | 2457 | del stored[aname] |
|
2409 | 2458 | self.db['stored_aliases'] = stored |
|
2410 | 2459 | |
|
2411 | 2460 | def magic_rehash(self, parameter_s = ''): |
|
2412 | 2461 | """Update the alias table with all entries in $PATH. |
|
2413 | 2462 | |
|
2414 | 2463 | This version does no checks on execute permissions or whether the |
|
2415 | 2464 | contents of $PATH are truly files (instead of directories or something |
|
2416 | 2465 | else). For such a safer (but slower) version, use %rehashx.""" |
|
2417 | 2466 | |
|
2418 | 2467 | # This function (and rehashx) manipulate the alias_table directly |
|
2419 | 2468 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
2420 | 2469 | # typical Linux box involves several thousand entries, so efficiency |
|
2421 | 2470 | # here is a top concern. |
|
2422 | 2471 | |
|
2423 | 2472 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2424 | 2473 | alias_table = self.shell.alias_table |
|
2425 | 2474 | for pdir in path: |
|
2426 | 2475 | for ff in os.listdir(pdir): |
|
2427 | 2476 | # each entry in the alias table must be (N,name), where |
|
2428 | 2477 | # N is the number of positional arguments of the alias. |
|
2429 | 2478 | alias_table[ff] = (0,ff) |
|
2430 | 2479 | # Make sure the alias table doesn't contain keywords or builtins |
|
2431 | 2480 | self.shell.alias_table_validate() |
|
2432 | 2481 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
2433 | 2482 | # aliases since %rehash will probably clobber them |
|
2434 | 2483 | self.shell.init_auto_alias() |
|
2435 | 2484 | |
|
2436 | 2485 | def magic_rehashx(self, parameter_s = ''): |
|
2437 | 2486 | """Update the alias table with all executable files in $PATH. |
|
2438 | 2487 | |
|
2439 | 2488 | This version explicitly checks that every entry in $PATH is a file |
|
2440 | 2489 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
2441 | 2490 | |
|
2442 | 2491 | Under Windows, it checks executability as a match agains a |
|
2443 | 2492 | '|'-separated string of extensions, stored in the IPython config |
|
2444 | 2493 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ |
|
2445 | 2494 | |
|
2446 | 2495 | path = [os.path.abspath(os.path.expanduser(p)) for p in |
|
2447 | 2496 | os.environ['PATH'].split(os.pathsep)] |
|
2448 | 2497 | path = filter(os.path.isdir,path) |
|
2449 | 2498 | |
|
2450 | 2499 | alias_table = self.shell.alias_table |
|
2451 | 2500 | syscmdlist = [] |
|
2452 | 2501 | if os.name == 'posix': |
|
2453 | 2502 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
2454 | 2503 | os.access(fname,os.X_OK) |
|
2455 | 2504 | else: |
|
2456 | 2505 | |
|
2457 | 2506 | try: |
|
2458 | 2507 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
2459 | 2508 | except KeyError: |
|
2460 | 2509 | winext = 'exe|com|bat|py' |
|
2461 | 2510 | if 'py' not in winext: |
|
2462 | 2511 | winext += '|py' |
|
2463 | 2512 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
2464 | 2513 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
2465 | 2514 | savedir = os.getcwd() |
|
2466 | 2515 | try: |
|
2467 | 2516 | # write the whole loop for posix/Windows so we don't have an if in |
|
2468 | 2517 | # the innermost part |
|
2469 | 2518 | if os.name == 'posix': |
|
2470 | 2519 | for pdir in path: |
|
2471 | 2520 | os.chdir(pdir) |
|
2472 | 2521 | for ff in os.listdir(pdir): |
|
2473 | 2522 | if isexec(ff) and ff not in self.shell.no_alias: |
|
2474 | 2523 | # each entry in the alias table must be (N,name), |
|
2475 | 2524 | # where N is the number of positional arguments of the |
|
2476 | 2525 | # alias. |
|
2477 | 2526 | alias_table[ff] = (0,ff) |
|
2478 | 2527 | syscmdlist.append(ff) |
|
2479 | 2528 | else: |
|
2480 | 2529 | for pdir in path: |
|
2481 | 2530 | os.chdir(pdir) |
|
2482 | 2531 | for ff in os.listdir(pdir): |
|
2483 | 2532 | if isexec(ff) and os.path.splitext(ff)[0] not in self.shell.no_alias: |
|
2484 | 2533 | alias_table[execre.sub(r'\1',ff)] = (0,ff) |
|
2485 | 2534 | syscmdlist.append(ff) |
|
2486 | 2535 | # Make sure the alias table doesn't contain keywords or builtins |
|
2487 | 2536 | self.shell.alias_table_validate() |
|
2488 | 2537 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
2489 | 2538 | # modified aliases since %rehashx will probably clobber them |
|
2490 | 2539 | self.shell.init_auto_alias() |
|
2491 | 2540 | db = self.getapi().db |
|
2492 | 2541 | db['syscmdlist'] = syscmdlist |
|
2493 | 2542 | finally: |
|
2494 | 2543 | os.chdir(savedir) |
|
2495 | 2544 | |
|
2496 | 2545 | def magic_pwd(self, parameter_s = ''): |
|
2497 | 2546 | """Return the current working directory path.""" |
|
2498 | 2547 | return os.getcwd() |
|
2499 | 2548 | |
|
2500 | 2549 | def magic_cd(self, parameter_s=''): |
|
2501 | 2550 | """Change the current working directory. |
|
2502 | 2551 | |
|
2503 | 2552 | This command automatically maintains an internal list of directories |
|
2504 | 2553 | you visit during your IPython session, in the variable _dh. The |
|
2505 | 2554 | command %dhist shows this history nicely formatted. |
|
2506 | 2555 | |
|
2507 | 2556 | Usage: |
|
2508 | 2557 | |
|
2509 | 2558 | cd 'dir': changes to directory 'dir'. |
|
2510 | 2559 | |
|
2511 | 2560 | cd -: changes to the last visited directory. |
|
2512 | 2561 | |
|
2513 | 2562 | cd -<n>: changes to the n-th directory in the directory history. |
|
2514 | 2563 | |
|
2515 | 2564 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
2516 | 2565 | (note: cd <bookmark_name> is enough if there is no |
|
2517 | 2566 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
2518 | 2567 | |
|
2519 | 2568 | Options: |
|
2520 | 2569 | |
|
2521 | 2570 | -q: quiet. Do not print the working directory after the cd command is |
|
2522 | 2571 | executed. By default IPython's cd command does print this directory, |
|
2523 | 2572 | since the default prompts do not display path information. |
|
2524 | 2573 | |
|
2525 | 2574 | Note that !cd doesn't work for this purpose because the shell where |
|
2526 | 2575 | !command runs is immediately discarded after executing 'command'.""" |
|
2527 | 2576 | |
|
2528 | 2577 | parameter_s = parameter_s.strip() |
|
2529 | 2578 | #bkms = self.shell.persist.get("bookmarks",{}) |
|
2530 | 2579 | |
|
2531 | 2580 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
2532 | 2581 | # jump in directory history by number |
|
2533 | 2582 | if numcd: |
|
2534 | 2583 | nn = int(numcd.group(2)) |
|
2535 | 2584 | try: |
|
2536 | 2585 | ps = self.shell.user_ns['_dh'][nn] |
|
2537 | 2586 | except IndexError: |
|
2538 | 2587 | print 'The requested directory does not exist in history.' |
|
2539 | 2588 | return |
|
2540 | 2589 | else: |
|
2541 | 2590 | opts = {} |
|
2542 | 2591 | else: |
|
2543 | 2592 | #turn all non-space-escaping backslashes to slashes, |
|
2544 | 2593 | # for c:\windows\directory\names\ |
|
2545 | 2594 | parameter_s = re.sub(r'\\(?! )','/', parameter_s) |
|
2546 | 2595 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
2547 | 2596 | # jump to previous |
|
2548 | 2597 | if ps == '-': |
|
2549 | 2598 | try: |
|
2550 | 2599 | ps = self.shell.user_ns['_dh'][-2] |
|
2551 | 2600 | except IndexError: |
|
2552 | 2601 | print 'No previous directory to change to.' |
|
2553 | 2602 | return |
|
2554 | 2603 | # jump to bookmark if needed |
|
2555 | 2604 | else: |
|
2556 | 2605 | if not os.path.isdir(ps) or opts.has_key('b'): |
|
2557 | 2606 | bkms = self.db.get('bookmarks', {}) |
|
2558 | 2607 | |
|
2559 | 2608 | if bkms.has_key(ps): |
|
2560 | 2609 | target = bkms[ps] |
|
2561 | 2610 | print '(bookmark:%s) -> %s' % (ps,target) |
|
2562 | 2611 | ps = target |
|
2563 | 2612 | else: |
|
2564 | 2613 | if opts.has_key('b'): |
|
2565 | 2614 | error("Bookmark '%s' not found. " |
|
2566 | 2615 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
2567 | 2616 | return |
|
2568 | 2617 | |
|
2569 | 2618 | # at this point ps should point to the target dir |
|
2570 | 2619 | if ps: |
|
2571 | 2620 | try: |
|
2572 | 2621 | os.chdir(os.path.expanduser(ps)) |
|
2573 | 2622 | ttitle = ("IPy:" + ( |
|
2574 | 2623 | os.getcwd() == '/' and '/' or os.path.basename(os.getcwd()))) |
|
2575 | 2624 | platutils.set_term_title(ttitle) |
|
2576 | 2625 | except OSError: |
|
2577 | 2626 | print sys.exc_info()[1] |
|
2578 | 2627 | else: |
|
2579 | 2628 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2580 | 2629 | else: |
|
2581 | 2630 | os.chdir(self.shell.home_dir) |
|
2582 | 2631 | platutils.set_term_title("IPy:~") |
|
2583 | 2632 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2584 | 2633 | if not 'q' in opts: |
|
2585 | 2634 | print self.shell.user_ns['_dh'][-1] |
|
2586 | 2635 | |
|
2587 | 2636 | def magic_dhist(self, parameter_s=''): |
|
2588 | 2637 | """Print your history of visited directories. |
|
2589 | 2638 | |
|
2590 | 2639 | %dhist -> print full history\\ |
|
2591 | 2640 | %dhist n -> print last n entries only\\ |
|
2592 | 2641 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
2593 | 2642 | |
|
2594 | 2643 | This history is automatically maintained by the %cd command, and |
|
2595 | 2644 | always available as the global list variable _dh. You can use %cd -<n> |
|
2596 | 2645 | to go to directory number <n>.""" |
|
2597 | 2646 | |
|
2598 | 2647 | dh = self.shell.user_ns['_dh'] |
|
2599 | 2648 | if parameter_s: |
|
2600 | 2649 | try: |
|
2601 | 2650 | args = map(int,parameter_s.split()) |
|
2602 | 2651 | except: |
|
2603 | 2652 | self.arg_err(Magic.magic_dhist) |
|
2604 | 2653 | return |
|
2605 | 2654 | if len(args) == 1: |
|
2606 | 2655 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
2607 | 2656 | elif len(args) == 2: |
|
2608 | 2657 | ini,fin = args |
|
2609 | 2658 | else: |
|
2610 | 2659 | self.arg_err(Magic.magic_dhist) |
|
2611 | 2660 | return |
|
2612 | 2661 | else: |
|
2613 | 2662 | ini,fin = 0,len(dh) |
|
2614 | 2663 | nlprint(dh, |
|
2615 | 2664 | header = 'Directory history (kept in _dh)', |
|
2616 | 2665 | start=ini,stop=fin) |
|
2617 | 2666 | |
|
2618 | 2667 | def magic_env(self, parameter_s=''): |
|
2619 | 2668 | """List environment variables.""" |
|
2620 | 2669 | |
|
2621 | 2670 | return os.environ.data |
|
2622 | 2671 | |
|
2623 | 2672 | def magic_pushd(self, parameter_s=''): |
|
2624 | 2673 | """Place the current dir on stack and change directory. |
|
2625 | 2674 | |
|
2626 | 2675 | Usage:\\ |
|
2627 | 2676 | %pushd ['dirname'] |
|
2628 | 2677 | |
|
2629 | 2678 | %pushd with no arguments does a %pushd to your home directory. |
|
2630 | 2679 | """ |
|
2631 | 2680 | if parameter_s == '': parameter_s = '~' |
|
2632 | 2681 | dir_s = self.shell.dir_stack |
|
2633 | 2682 | if len(dir_s)>0 and os.path.expanduser(parameter_s) != \ |
|
2634 | 2683 | os.path.expanduser(self.shell.dir_stack[0]): |
|
2635 | 2684 | try: |
|
2636 | 2685 | self.magic_cd(parameter_s) |
|
2637 | 2686 | dir_s.insert(0,os.getcwd().replace(self.home_dir,'~')) |
|
2638 | 2687 | self.magic_dirs() |
|
2639 | 2688 | except: |
|
2640 | 2689 | print 'Invalid directory' |
|
2641 | 2690 | else: |
|
2642 | 2691 | print 'You are already there!' |
|
2643 | 2692 | |
|
2644 | 2693 | def magic_popd(self, parameter_s=''): |
|
2645 | 2694 | """Change to directory popped off the top of the stack. |
|
2646 | 2695 | """ |
|
2647 | 2696 | if len (self.shell.dir_stack) > 1: |
|
2648 | 2697 | self.shell.dir_stack.pop(0) |
|
2649 | 2698 | self.magic_cd(self.shell.dir_stack[0]) |
|
2650 | 2699 | print self.shell.dir_stack[0] |
|
2651 | 2700 | else: |
|
2652 | 2701 | print "You can't remove the starting directory from the stack:",\ |
|
2653 | 2702 | self.shell.dir_stack |
|
2654 | 2703 | |
|
2655 | 2704 | def magic_dirs(self, parameter_s=''): |
|
2656 | 2705 | """Return the current directory stack.""" |
|
2657 | 2706 | |
|
2658 | 2707 | return self.shell.dir_stack[:] |
|
2659 | 2708 | |
|
2660 | 2709 | def magic_sc(self, parameter_s=''): |
|
2661 | 2710 | """Shell capture - execute a shell command and capture its output. |
|
2662 | 2711 | |
|
2663 | 2712 | DEPRECATED. Suboptimal, retained for backwards compatibility. |
|
2664 | 2713 | |
|
2665 | 2714 | You should use the form 'var = !command' instead. Example: |
|
2666 | 2715 | |
|
2667 | 2716 | "%sc -l myfiles = ls ~" should now be written as |
|
2668 | 2717 | |
|
2669 | 2718 | "myfiles = !ls ~" |
|
2670 | 2719 | |
|
2671 | 2720 | myfiles.s, myfiles.l and myfiles.n still apply as documented |
|
2672 | 2721 | below. |
|
2673 | 2722 | |
|
2674 | 2723 | -- |
|
2675 | 2724 | %sc [options] varname=command |
|
2676 | 2725 | |
|
2677 | 2726 | IPython will run the given command using commands.getoutput(), and |
|
2678 | 2727 | will then update the user's interactive namespace with a variable |
|
2679 | 2728 | called varname, containing the value of the call. Your command can |
|
2680 | 2729 | contain shell wildcards, pipes, etc. |
|
2681 | 2730 | |
|
2682 | 2731 | The '=' sign in the syntax is mandatory, and the variable name you |
|
2683 | 2732 | supply must follow Python's standard conventions for valid names. |
|
2684 | 2733 | |
|
2685 | 2734 | (A special format without variable name exists for internal use) |
|
2686 | 2735 | |
|
2687 | 2736 | Options: |
|
2688 | 2737 | |
|
2689 | 2738 | -l: list output. Split the output on newlines into a list before |
|
2690 | 2739 | assigning it to the given variable. By default the output is stored |
|
2691 | 2740 | as a single string. |
|
2692 | 2741 | |
|
2693 | 2742 | -v: verbose. Print the contents of the variable. |
|
2694 | 2743 | |
|
2695 | 2744 | In most cases you should not need to split as a list, because the |
|
2696 | 2745 | returned value is a special type of string which can automatically |
|
2697 | 2746 | provide its contents either as a list (split on newlines) or as a |
|
2698 | 2747 | space-separated string. These are convenient, respectively, either |
|
2699 | 2748 | for sequential processing or to be passed to a shell command. |
|
2700 | 2749 | |
|
2701 | 2750 | For example: |
|
2702 | 2751 | |
|
2703 | 2752 | # Capture into variable a |
|
2704 | 2753 | In [9]: sc a=ls *py |
|
2705 | 2754 | |
|
2706 | 2755 | # a is a string with embedded newlines |
|
2707 | 2756 | In [10]: a |
|
2708 | 2757 | Out[10]: 'setup.py\nwin32_manual_post_install.py' |
|
2709 | 2758 | |
|
2710 | 2759 | # which can be seen as a list: |
|
2711 | 2760 | In [11]: a.l |
|
2712 | 2761 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] |
|
2713 | 2762 | |
|
2714 | 2763 | # or as a whitespace-separated string: |
|
2715 | 2764 | In [12]: a.s |
|
2716 | 2765 | Out[12]: 'setup.py win32_manual_post_install.py' |
|
2717 | 2766 | |
|
2718 | 2767 | # a.s is useful to pass as a single command line: |
|
2719 | 2768 | In [13]: !wc -l $a.s |
|
2720 | 2769 | 146 setup.py |
|
2721 | 2770 | 130 win32_manual_post_install.py |
|
2722 | 2771 | 276 total |
|
2723 | 2772 | |
|
2724 | 2773 | # while the list form is useful to loop over: |
|
2725 | 2774 | In [14]: for f in a.l: |
|
2726 | 2775 | ....: !wc -l $f |
|
2727 | 2776 | ....: |
|
2728 | 2777 | 146 setup.py |
|
2729 | 2778 | 130 win32_manual_post_install.py |
|
2730 | 2779 | |
|
2731 | 2780 | Similiarly, the lists returned by the -l option are also special, in |
|
2732 | 2781 | the sense that you can equally invoke the .s attribute on them to |
|
2733 | 2782 | automatically get a whitespace-separated string from their contents: |
|
2734 | 2783 | |
|
2735 | 2784 | In [1]: sc -l b=ls *py |
|
2736 | 2785 | |
|
2737 | 2786 | In [2]: b |
|
2738 | 2787 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] |
|
2739 | 2788 | |
|
2740 | 2789 | In [3]: b.s |
|
2741 | 2790 | Out[3]: 'setup.py win32_manual_post_install.py' |
|
2742 | 2791 | |
|
2743 | 2792 | In summary, both the lists and strings used for ouptut capture have |
|
2744 | 2793 | the following special attributes: |
|
2745 | 2794 | |
|
2746 | 2795 | .l (or .list) : value as list. |
|
2747 | 2796 | .n (or .nlstr): value as newline-separated string. |
|
2748 | 2797 | .s (or .spstr): value as space-separated string. |
|
2749 | 2798 | """ |
|
2750 | 2799 | |
|
2751 | 2800 | opts,args = self.parse_options(parameter_s,'lv') |
|
2752 | 2801 | # Try to get a variable name and command to run |
|
2753 | 2802 | try: |
|
2754 | 2803 | # the variable name must be obtained from the parse_options |
|
2755 | 2804 | # output, which uses shlex.split to strip options out. |
|
2756 | 2805 | var,_ = args.split('=',1) |
|
2757 | 2806 | var = var.strip() |
|
2758 | 2807 | # But the the command has to be extracted from the original input |
|
2759 | 2808 | # parameter_s, not on what parse_options returns, to avoid the |
|
2760 | 2809 | # quote stripping which shlex.split performs on it. |
|
2761 | 2810 | _,cmd = parameter_s.split('=',1) |
|
2762 | 2811 | except ValueError: |
|
2763 | 2812 | var,cmd = '','' |
|
2764 | 2813 | # If all looks ok, proceed |
|
2765 | 2814 | out,err = self.shell.getoutputerror(cmd) |
|
2766 | 2815 | if err: |
|
2767 | 2816 | print >> Term.cerr,err |
|
2768 | 2817 | if opts.has_key('l'): |
|
2769 | 2818 | out = SList(out.split('\n')) |
|
2770 | 2819 | else: |
|
2771 | 2820 | out = LSString(out) |
|
2772 | 2821 | if opts.has_key('v'): |
|
2773 | 2822 | print '%s ==\n%s' % (var,pformat(out)) |
|
2774 | 2823 | if var: |
|
2775 | 2824 | self.shell.user_ns.update({var:out}) |
|
2776 | 2825 | else: |
|
2777 | 2826 | return out |
|
2778 | 2827 | |
|
2779 | 2828 | def magic_sx(self, parameter_s=''): |
|
2780 | 2829 | """Shell execute - run a shell command and capture its output. |
|
2781 | 2830 | |
|
2782 | 2831 | %sx command |
|
2783 | 2832 | |
|
2784 | 2833 | IPython will run the given command using commands.getoutput(), and |
|
2785 | 2834 | return the result formatted as a list (split on '\\n'). Since the |
|
2786 | 2835 | output is _returned_, it will be stored in ipython's regular output |
|
2787 | 2836 | cache Out[N] and in the '_N' automatic variables. |
|
2788 | 2837 | |
|
2789 | 2838 | Notes: |
|
2790 | 2839 | |
|
2791 | 2840 | 1) If an input line begins with '!!', then %sx is automatically |
|
2792 | 2841 | invoked. That is, while: |
|
2793 | 2842 | !ls |
|
2794 | 2843 | causes ipython to simply issue system('ls'), typing |
|
2795 | 2844 | !!ls |
|
2796 | 2845 | is a shorthand equivalent to: |
|
2797 | 2846 | %sx ls |
|
2798 | 2847 | |
|
2799 | 2848 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
2800 | 2849 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
2801 | 2850 | to process line-oriented shell output via further python commands. |
|
2802 | 2851 | %sc is meant to provide much finer control, but requires more |
|
2803 | 2852 | typing. |
|
2804 | 2853 | |
|
2805 | 2854 | 3) Just like %sc -l, this is a list with special attributes: |
|
2806 | 2855 | |
|
2807 | 2856 | .l (or .list) : value as list. |
|
2808 | 2857 | .n (or .nlstr): value as newline-separated string. |
|
2809 | 2858 | .s (or .spstr): value as whitespace-separated string. |
|
2810 | 2859 | |
|
2811 | 2860 | This is very useful when trying to use such lists as arguments to |
|
2812 | 2861 | system commands.""" |
|
2813 | 2862 | |
|
2814 | 2863 | if parameter_s: |
|
2815 | 2864 | out,err = self.shell.getoutputerror(parameter_s) |
|
2816 | 2865 | if err: |
|
2817 | 2866 | print >> Term.cerr,err |
|
2818 | 2867 | return SList(out.split('\n')) |
|
2819 | 2868 | |
|
2820 | 2869 | def magic_bg(self, parameter_s=''): |
|
2821 | 2870 | """Run a job in the background, in a separate thread. |
|
2822 | 2871 | |
|
2823 | 2872 | For example, |
|
2824 | 2873 | |
|
2825 | 2874 | %bg myfunc(x,y,z=1) |
|
2826 | 2875 | |
|
2827 | 2876 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the |
|
2828 | 2877 | execution starts, a message will be printed indicating the job |
|
2829 | 2878 | number. If your job number is 5, you can use |
|
2830 | 2879 | |
|
2831 | 2880 | myvar = jobs.result(5) or myvar = jobs[5].result |
|
2832 | 2881 | |
|
2833 | 2882 | to assign this result to variable 'myvar'. |
|
2834 | 2883 | |
|
2835 | 2884 | IPython has a job manager, accessible via the 'jobs' object. You can |
|
2836 | 2885 | type jobs? to get more information about it, and use jobs.<TAB> to see |
|
2837 | 2886 | its attributes. All attributes not starting with an underscore are |
|
2838 | 2887 | meant for public use. |
|
2839 | 2888 | |
|
2840 | 2889 | In particular, look at the jobs.new() method, which is used to create |
|
2841 | 2890 | new jobs. This magic %bg function is just a convenience wrapper |
|
2842 | 2891 | around jobs.new(), for expression-based jobs. If you want to create a |
|
2843 | 2892 | new job with an explicit function object and arguments, you must call |
|
2844 | 2893 | jobs.new() directly. |
|
2845 | 2894 | |
|
2846 | 2895 | The jobs.new docstring also describes in detail several important |
|
2847 | 2896 | caveats associated with a thread-based model for background job |
|
2848 | 2897 | execution. Type jobs.new? for details. |
|
2849 | 2898 | |
|
2850 | 2899 | You can check the status of all jobs with jobs.status(). |
|
2851 | 2900 | |
|
2852 | 2901 | The jobs variable is set by IPython into the Python builtin namespace. |
|
2853 | 2902 | If you ever declare a variable named 'jobs', you will shadow this |
|
2854 | 2903 | name. You can either delete your global jobs variable to regain |
|
2855 | 2904 | access to the job manager, or make a new name and assign it manually |
|
2856 | 2905 | to the manager (stored in IPython's namespace). For example, to |
|
2857 | 2906 | assign the job manager to the Jobs name, use: |
|
2858 | 2907 | |
|
2859 | 2908 | Jobs = __builtins__.jobs""" |
|
2860 | 2909 | |
|
2861 | 2910 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2862 | 2911 | |
|
2863 | 2912 | |
|
2864 | 2913 | def magic_bookmark(self, parameter_s=''): |
|
2865 | 2914 | """Manage IPython's bookmark system. |
|
2866 | 2915 | |
|
2867 | 2916 | %bookmark <name> - set bookmark to current dir |
|
2868 | 2917 | %bookmark <name> <dir> - set bookmark to <dir> |
|
2869 | 2918 | %bookmark -l - list all bookmarks |
|
2870 | 2919 | %bookmark -d <name> - remove bookmark |
|
2871 | 2920 | %bookmark -r - remove all bookmarks |
|
2872 | 2921 | |
|
2873 | 2922 | You can later on access a bookmarked folder with: |
|
2874 | 2923 | %cd -b <name> |
|
2875 | 2924 | or simply '%cd <name>' if there is no directory called <name> AND |
|
2876 | 2925 | there is such a bookmark defined. |
|
2877 | 2926 | |
|
2878 | 2927 | Your bookmarks persist through IPython sessions, but they are |
|
2879 | 2928 | associated with each profile.""" |
|
2880 | 2929 | |
|
2881 | 2930 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
2882 | 2931 | if len(args) > 2: |
|
2883 | 2932 | error('You can only give at most two arguments') |
|
2884 | 2933 | return |
|
2885 | 2934 | |
|
2886 | 2935 | bkms = self.db.get('bookmarks',{}) |
|
2887 | 2936 | |
|
2888 | 2937 | if opts.has_key('d'): |
|
2889 | 2938 | try: |
|
2890 | 2939 | todel = args[0] |
|
2891 | 2940 | except IndexError: |
|
2892 | 2941 | error('You must provide a bookmark to delete') |
|
2893 | 2942 | else: |
|
2894 | 2943 | try: |
|
2895 | 2944 | del bkms[todel] |
|
2896 | 2945 | except: |
|
2897 | 2946 | error("Can't delete bookmark '%s'" % todel) |
|
2898 | 2947 | elif opts.has_key('r'): |
|
2899 | 2948 | bkms = {} |
|
2900 | 2949 | elif opts.has_key('l'): |
|
2901 | 2950 | bks = bkms.keys() |
|
2902 | 2951 | bks.sort() |
|
2903 | 2952 | if bks: |
|
2904 | 2953 | size = max(map(len,bks)) |
|
2905 | 2954 | else: |
|
2906 | 2955 | size = 0 |
|
2907 | 2956 | fmt = '%-'+str(size)+'s -> %s' |
|
2908 | 2957 | print 'Current bookmarks:' |
|
2909 | 2958 | for bk in bks: |
|
2910 | 2959 | print fmt % (bk,bkms[bk]) |
|
2911 | 2960 | else: |
|
2912 | 2961 | if not args: |
|
2913 | 2962 | error("You must specify the bookmark name") |
|
2914 | 2963 | elif len(args)==1: |
|
2915 | 2964 | bkms[args[0]] = os.getcwd() |
|
2916 | 2965 | elif len(args)==2: |
|
2917 | 2966 | bkms[args[0]] = args[1] |
|
2918 | 2967 | self.db['bookmarks'] = bkms |
|
2919 | 2968 | |
|
2920 | 2969 | def magic_pycat(self, parameter_s=''): |
|
2921 | 2970 | """Show a syntax-highlighted file through a pager. |
|
2922 | 2971 | |
|
2923 | 2972 | This magic is similar to the cat utility, but it will assume the file |
|
2924 | 2973 | to be Python source and will show it with syntax highlighting. """ |
|
2925 | 2974 | |
|
2926 | 2975 | try: |
|
2927 | 2976 | filename = get_py_filename(parameter_s) |
|
2928 | 2977 | cont = file_read(filename) |
|
2929 | 2978 | except IOError: |
|
2930 | 2979 | try: |
|
2931 | 2980 | cont = eval(parameter_s,self.user_ns) |
|
2932 | 2981 | except NameError: |
|
2933 | 2982 | cont = None |
|
2934 | 2983 | if cont is None: |
|
2935 | 2984 | print "Error: no such file or variable" |
|
2936 | 2985 | return |
|
2937 | 2986 | |
|
2938 | 2987 | page(self.shell.pycolorize(cont), |
|
2939 | 2988 | screen_lines=self.shell.rc.screen_length) |
|
2940 | 2989 | |
|
2941 | 2990 | def magic_cpaste(self, parameter_s=''): |
|
2942 | 2991 | """Allows you to paste & execute a pre-formatted code block from clipboard |
|
2943 | 2992 | |
|
2944 | 2993 | You must terminate the block with '--' (two minus-signs) alone on the |
|
2945 | 2994 | line. You can also provide your own sentinel with '%paste -s %%' ('%%' |
|
2946 | 2995 | is the new sentinel for this operation) |
|
2947 | 2996 | |
|
2948 | 2997 | The block is dedented prior to execution to enable execution of |
|
2949 | 2998 | method definitions. '>' characters at the beginning of a line is |
|
2950 | 2999 | ignored, to allow pasting directly from e-mails. The executed block |
|
2951 | 3000 | is also assigned to variable named 'pasted_block' for later editing |
|
2952 | 3001 | with '%edit pasted_block'. |
|
2953 | 3002 | |
|
2954 | 3003 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. |
|
2955 | 3004 | This assigns the pasted block to variable 'foo' as string, without |
|
2956 | 3005 | dedenting or executing it. |
|
2957 | 3006 | |
|
2958 | 3007 | Do not be alarmed by garbled output on Windows (it's a readline bug). |
|
2959 | 3008 | Just press enter and type -- (and press enter again) and the block |
|
2960 | 3009 | will be what was just pasted. |
|
2961 | 3010 | |
|
2962 | 3011 | IPython statements (magics, shell escapes) are not supported (yet). |
|
2963 | 3012 | """ |
|
2964 | 3013 | opts,args = self.parse_options(parameter_s,'s:',mode='string') |
|
2965 | 3014 | par = args.strip() |
|
2966 | 3015 | sentinel = opts.get('s','--') |
|
2967 | 3016 | |
|
2968 | 3017 | from IPython import iplib |
|
2969 | 3018 | lines = [] |
|
2970 | 3019 | print "Pasting code; enter '%s' alone on the line to stop." % sentinel |
|
2971 | 3020 | while 1: |
|
2972 | 3021 | l = iplib.raw_input_original(':') |
|
2973 | 3022 | if l ==sentinel: |
|
2974 | 3023 | break |
|
2975 | 3024 | lines.append(l.lstrip('>')) |
|
2976 | 3025 | block = "\n".join(lines) + '\n' |
|
2977 | 3026 | #print "block:\n",block |
|
2978 | 3027 | if not par: |
|
2979 | 3028 | b = textwrap.dedent(block) |
|
2980 | 3029 | exec b in self.user_ns |
|
2981 | 3030 | self.user_ns['pasted_block'] = b |
|
2982 | 3031 | else: |
|
2983 | 3032 | self.user_ns[par] = block |
|
2984 | 3033 | print "Block assigned to '%s'" % par |
|
2985 | 3034 | |
|
2986 | 3035 | def magic_quickref(self,arg): |
|
2987 | 3036 | """ Show a quick reference sheet """ |
|
2988 | 3037 | import IPython.usage |
|
2989 | 3038 | qr = IPython.usage.quick_reference + self.magic_magic('-brief') |
|
2990 | 3039 | |
|
2991 | 3040 | page(qr) |
|
2992 | 3041 | |
|
2993 | 3042 | def magic_upgrade(self,arg): |
|
2994 | 3043 | """ Upgrade your IPython installation |
|
2995 | 3044 | |
|
2996 | 3045 | This will copy the config files that don't yet exist in your |
|
2997 | 3046 | ipython dir from the system config dir. Use this after upgrading |
|
2998 | 3047 | IPython if you don't wish to delete your .ipython dir. |
|
2999 | 3048 | |
|
3000 | 3049 | Call with -nolegacy to get rid of ipythonrc* files (recommended for |
|
3001 | 3050 | new users) |
|
3002 | 3051 | |
|
3003 | 3052 | """ |
|
3004 | 3053 | ip = self.getapi() |
|
3005 | 3054 | ipinstallation = path(IPython.__file__).dirname() |
|
3006 | 3055 | upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py') |
|
3007 | 3056 | src_config = ipinstallation / 'UserConfig' |
|
3008 | 3057 | userdir = path(ip.options.ipythondir) |
|
3009 | 3058 | cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir) |
|
3010 | 3059 | print ">",cmd |
|
3011 | 3060 | shell(cmd) |
|
3012 | 3061 | if arg == '-nolegacy': |
|
3013 | 3062 | legacy = userdir.files('ipythonrc*') |
|
3014 | 3063 | print "Nuking legacy files:",legacy |
|
3015 | 3064 | |
|
3016 | 3065 | [p.remove() for p in legacy] |
|
3017 | 3066 | suffix = (sys.platform == 'win32' and '.ini' or '') |
|
3018 | 3067 | (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n') |
|
3019 | 3068 | |
|
3020 | 3069 | |
|
3021 | 3070 | # end Magic |
@@ -1,607 +1,616 b'' | |||
|
1 | 1 | # -*- Mode: Shell-Script -*- Not really, but shows comments correctly |
|
2 |
# $Id: ipythonrc 1 |
|
|
2 | # $Id: ipythonrc 1879 2006-11-04 00:34:34Z fptest $ | |
|
3 | 3 | |
|
4 | 4 | #*************************************************************************** |
|
5 | 5 | # |
|
6 | 6 | # Configuration file for IPython -- ipythonrc format |
|
7 | 7 | # |
|
8 | 8 | # The format of this file is simply one of 'key value' lines. |
|
9 | 9 | # Lines containing only whitespace at the beginning and then a # are ignored |
|
10 | 10 | # as comments. But comments can NOT be put on lines with data. |
|
11 | 11 | |
|
12 | 12 | # The meaning and use of each key are explained below. |
|
13 | 13 | |
|
14 | 14 | #--------------------------------------------------------------------------- |
|
15 | 15 | # Section: included files |
|
16 | 16 | |
|
17 | 17 | # Put one or more *config* files (with the syntax of this file) you want to |
|
18 | 18 | # include. For keys with a unique value the outermost file has precedence. For |
|
19 | 19 | # keys with multiple values, they all get assembled into a list which then |
|
20 | 20 | # gets loaded by IPython. |
|
21 | 21 | |
|
22 | 22 | # In this file, all lists of things should simply be space-separated. |
|
23 | 23 | |
|
24 | 24 | # This allows you to build hierarchies of files which recursively load |
|
25 | 25 | # lower-level services. If this is your main ~/.ipython/ipythonrc file, you |
|
26 | 26 | # should only keep here basic things you always want available. Then you can |
|
27 | 27 | # include it in every other special-purpose config file you create. |
|
28 | 28 | include |
|
29 | 29 | |
|
30 | 30 | #--------------------------------------------------------------------------- |
|
31 | 31 | # Section: startup setup |
|
32 | 32 | |
|
33 | 33 | # These are mostly things which parallel a command line option of the same |
|
34 | 34 | # name. |
|
35 | 35 | |
|
36 | 36 | # Keys in this section should only appear once. If any key from this section |
|
37 | 37 | # is encountered more than once, the last value remains, all earlier ones get |
|
38 | 38 | # discarded. |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | # Automatic calling of callable objects. If set to 1 or 2, callable objects |
|
42 | 42 | # are automatically called when invoked at the command line, even if you don't |
|
43 | 43 | # type parentheses. IPython adds the parentheses for you. For example: |
|
44 | 44 | |
|
45 | 45 | #In [1]: str 45 |
|
46 | 46 | #------> str(45) |
|
47 | 47 | #Out[1]: '45' |
|
48 | 48 | |
|
49 | 49 | # IPython reprints your line with '---->' indicating that it added |
|
50 | 50 | # parentheses. While this option is very convenient for interactive use, it |
|
51 | 51 | # may occasionally cause problems with objects which have side-effects if |
|
52 | 52 | # called unexpectedly. |
|
53 | 53 | |
|
54 | 54 | # The valid values for autocall are: |
|
55 | 55 | |
|
56 | 56 | # autocall 0 -> disabled (you can toggle it at runtime with the %autocall magic) |
|
57 | 57 | |
|
58 | 58 | # autocall 1 -> active, but do not apply if there are no arguments on the line. |
|
59 | 59 | |
|
60 | 60 | # In this mode, you get: |
|
61 | 61 | |
|
62 | 62 | #In [1]: callable |
|
63 | 63 | #Out[1]: <built-in function callable> |
|
64 | 64 | |
|
65 | 65 | #In [2]: callable 'hello' |
|
66 | 66 | #------> callable('hello') |
|
67 | 67 | #Out[2]: False |
|
68 | 68 | |
|
69 | 69 | # 2 -> Active always. Even if no arguments are present, the callable object |
|
70 | 70 | # is called: |
|
71 | 71 | |
|
72 | 72 | #In [4]: callable |
|
73 | 73 | #------> callable() |
|
74 | 74 | |
|
75 | 75 | # Note that even with autocall off, you can still use '/' at the start of a |
|
76 | 76 | # line to treat the first argument on the command line as a function and add |
|
77 | 77 | # parentheses to it: |
|
78 | 78 | |
|
79 | 79 | #In [8]: /str 43 |
|
80 | 80 | #------> str(43) |
|
81 | 81 | #Out[8]: '43' |
|
82 | 82 | |
|
83 | 83 | autocall 1 |
|
84 | 84 | |
|
85 | 85 | # Auto-edit syntax errors. When you use the %edit magic in ipython to edit |
|
86 | 86 | # source code (see the 'editor' variable below), it is possible that you save |
|
87 | 87 | # a file with syntax errors in it. If this variable is true, IPython will ask |
|
88 | 88 | # you whether to re-open the editor immediately to correct such an error. |
|
89 | 89 | |
|
90 | 90 | autoedit_syntax 0 |
|
91 | 91 | |
|
92 | 92 | # Auto-indent. IPython can recognize lines ending in ':' and indent the next |
|
93 | 93 | # line, while also un-indenting automatically after 'raise' or 'return'. |
|
94 | 94 | |
|
95 | 95 | # This feature uses the readline library, so it will honor your ~/.inputrc |
|
96 | 96 | # configuration (or whatever file your INPUTRC variable points to). Adding |
|
97 | 97 | # the following lines to your .inputrc file can make indent/unindenting more |
|
98 | 98 | # convenient (M-i indents, M-u unindents): |
|
99 | 99 | |
|
100 | 100 | # $if Python |
|
101 | 101 | # "\M-i": " " |
|
102 | 102 | # "\M-u": "\d\d\d\d" |
|
103 | 103 | # $endif |
|
104 | 104 | |
|
105 | 105 | # The feature is potentially a bit dangerous, because it can cause problems |
|
106 | 106 | # with pasting of indented code (the pasted code gets re-indented on each |
|
107 | 107 | # line). But it's a huge time-saver when working interactively. The magic |
|
108 | 108 | # function %autoindent allows you to toggle it on/off at runtime. |
|
109 | 109 | |
|
110 | 110 | autoindent 1 |
|
111 | 111 | |
|
112 | 112 | # Auto-magic. This gives you access to all the magic functions without having |
|
113 | 113 | # to prepend them with an % sign. If you define a variable with the same name |
|
114 | 114 | # as a magic function (say who=1), you will need to access the magic function |
|
115 | 115 | # with % (%who in this example). However, if later you delete your variable |
|
116 | 116 | # (del who), you'll recover the automagic calling form. |
|
117 | 117 | |
|
118 | 118 | # Considering that many magic functions provide a lot of shell-like |
|
119 | 119 | # functionality, automagic gives you something close to a full Python+system |
|
120 | 120 | # shell environment (and you can extend it further if you want). |
|
121 | 121 | |
|
122 | 122 | automagic 1 |
|
123 | 123 | |
|
124 | 124 | # Size of the output cache. After this many entries are stored, the cache will |
|
125 | 125 | # get flushed. Depending on the size of your intermediate calculations, you |
|
126 | 126 | # may have memory problems if you make it too big, since keeping things in the |
|
127 | 127 | # cache prevents Python from reclaiming the memory for old results. Experiment |
|
128 | 128 | # with a value that works well for you. |
|
129 | 129 | |
|
130 | 130 | # If you choose cache_size 0 IPython will revert to python's regular >>> |
|
131 | 131 | # unnumbered prompt. You will still have _, __ and ___ for your last three |
|
132 | 132 | # results, but that will be it. No dynamic _1, _2, etc. will be created. If |
|
133 | 133 | # you are running on a slow machine or with very limited memory, this may |
|
134 | 134 | # help. |
|
135 | 135 | |
|
136 | 136 | cache_size 1000 |
|
137 | 137 | |
|
138 | 138 | # Classic mode: Setting 'classic 1' you lose many of IPython niceties, |
|
139 | 139 | # but that's your choice! Classic 1 -> same as IPython -classic. |
|
140 | 140 | # Note that this is _not_ the normal python interpreter, it's simply |
|
141 | 141 | # IPython emulating most of the classic interpreter's behavior. |
|
142 | 142 | classic 0 |
|
143 | 143 | |
|
144 | 144 | # colors - Coloring option for prompts and traceback printouts. |
|
145 | 145 | |
|
146 | 146 | # Currently available schemes: NoColor, Linux, LightBG. |
|
147 | 147 | |
|
148 | 148 | # This option allows coloring the prompts and traceback printouts. This |
|
149 | 149 | # requires a terminal which can properly handle color escape sequences. If you |
|
150 | 150 | # are having problems with this, use the NoColor scheme (uses no color escapes |
|
151 | 151 | # at all). |
|
152 | 152 | |
|
153 | 153 | # The Linux option works well in linux console type environments: dark |
|
154 | 154 | # background with light fonts. |
|
155 | 155 | |
|
156 | 156 | # LightBG is similar to Linux but swaps dark/light colors to be more readable |
|
157 | 157 | # in light background terminals. |
|
158 | 158 | |
|
159 | 159 | # keep uncommented only the one you want: |
|
160 | 160 | colors Linux |
|
161 | 161 | #colors LightBG |
|
162 | 162 | #colors NoColor |
|
163 | 163 | |
|
164 | 164 | ######################## |
|
165 | 165 | # Note to Windows users |
|
166 | 166 | # |
|
167 | 167 | # Color and readline support is avaialble to Windows users via Gary Bishop's |
|
168 | 168 | # readline library. You can find Gary's tools at |
|
169 | 169 | # http://sourceforge.net/projects/uncpythontools. |
|
170 | 170 | # Note that his readline module requires in turn the ctypes library, available |
|
171 | 171 | # at http://starship.python.net/crew/theller/ctypes. |
|
172 | 172 | ######################## |
|
173 | 173 | |
|
174 | 174 | # color_info: IPython can display information about objects via a set of |
|
175 | 175 | # functions, and optionally can use colors for this, syntax highlighting |
|
176 | 176 | # source code and various other elements. This information is passed through a |
|
177 | 177 | # pager (it defaults to 'less' if $PAGER is not set). |
|
178 | 178 | |
|
179 | 179 | # If your pager has problems, try to setting it to properly handle escapes |
|
180 | 180 | # (see the less manpage for detail), or disable this option. The magic |
|
181 | 181 | # function %color_info allows you to toggle this interactively for testing. |
|
182 | 182 | |
|
183 | 183 | color_info 1 |
|
184 | 184 | |
|
185 | 185 | # confirm_exit: set to 1 if you want IPython to confirm when you try to exit |
|
186 | 186 | # with an EOF (Control-d in Unix, Control-Z/Enter in Windows). Note that using |
|
187 | 187 | # the magic functions %Exit or %Quit you can force a direct exit, bypassing |
|
188 | 188 | # any confirmation. |
|
189 | 189 | |
|
190 | 190 | confirm_exit 1 |
|
191 | 191 | |
|
192 | 192 | # Use deep_reload() as a substitute for reload() by default. deep_reload() is |
|
193 | 193 | # still available as dreload() and appears as a builtin. |
|
194 | 194 | |
|
195 | 195 | deep_reload 0 |
|
196 | 196 | |
|
197 | 197 | # Which editor to use with the %edit command. If you leave this at 0, IPython |
|
198 | 198 | # will honor your EDITOR environment variable. Since this editor is invoked on |
|
199 | 199 | # the fly by ipython and is meant for editing small code snippets, you may |
|
200 | 200 | # want to use a small, lightweight editor here. |
|
201 | 201 | |
|
202 | 202 | # For Emacs users, setting up your Emacs server properly as described in the |
|
203 | 203 | # manual is a good idea. An alternative is to use jed, a very light editor |
|
204 | 204 | # with much of the feel of Emacs (though not as powerful for heavy-duty work). |
|
205 | 205 | |
|
206 | 206 | editor 0 |
|
207 | 207 | |
|
208 | 208 | # log 1 -> same as ipython -log. This automatically logs to ./ipython.log |
|
209 | 209 | log 0 |
|
210 | 210 | |
|
211 | 211 | # Same as ipython -Logfile YourLogfileName. |
|
212 | 212 | # Don't use with log 1 (use one or the other) |
|
213 | 213 | logfile '' |
|
214 | 214 | |
|
215 | 215 | # banner 0 -> same as ipython -nobanner |
|
216 | 216 | banner 1 |
|
217 | 217 | |
|
218 | 218 | # messages 0 -> same as ipython -nomessages |
|
219 | 219 | messages 1 |
|
220 | 220 | |
|
221 | 221 | # Automatically call the pdb debugger after every uncaught exception. If you |
|
222 | 222 | # are used to debugging using pdb, this puts you automatically inside of it |
|
223 | 223 | # after any call (either in IPython or in code called by it) which triggers an |
|
224 | 224 | # exception which goes uncaught. |
|
225 | 225 | pdb 0 |
|
226 | 226 | |
|
227 | 227 | # Enable the pprint module for printing. pprint tends to give a more readable |
|
228 | 228 | # display (than print) for complex nested data structures. |
|
229 | 229 | pprint 1 |
|
230 | 230 | |
|
231 | 231 | # Prompt strings |
|
232 | 232 | |
|
233 | 233 | # Most bash-like escapes can be used to customize IPython's prompts, as well as |
|
234 | 234 | # a few additional ones which are IPython-specific. All valid prompt escapes |
|
235 | 235 | # are described in detail in the Customization section of the IPython HTML/PDF |
|
236 | 236 | # manual. |
|
237 | 237 | |
|
238 | 238 | # Use \# to represent the current prompt number, and quote them to protect |
|
239 | 239 | # spaces. |
|
240 | 240 | prompt_in1 'In [\#]: ' |
|
241 | 241 | |
|
242 | 242 | # \D is replaced by as many dots as there are digits in the |
|
243 | 243 | # current value of \#. |
|
244 | 244 | prompt_in2 ' .\D.: ' |
|
245 | 245 | |
|
246 | 246 | prompt_out 'Out[\#]: ' |
|
247 | 247 | |
|
248 | 248 | # Select whether to left-pad the output prompts to match the length of the |
|
249 | 249 | # input ones. This allows you for example to use a simple '>' as an output |
|
250 | 250 | # prompt, and yet have the output line up with the input. If set to false, |
|
251 | 251 | # the output prompts will be unpadded (flush left). |
|
252 | 252 | prompts_pad_left 1 |
|
253 | 253 | |
|
254 | 254 | # quick 1 -> same as ipython -quick |
|
255 | 255 | quick 0 |
|
256 | 256 | |
|
257 | 257 | # Use the readline library (1) or not (0). Most users will want this on, but |
|
258 | 258 | # if you experience strange problems with line management (mainly when using |
|
259 | 259 | # IPython inside Emacs buffers) you may try disabling it. Not having it on |
|
260 | 260 | # prevents you from getting command history with the arrow keys, searching and |
|
261 | 261 | # name completion using TAB. |
|
262 | 262 | |
|
263 | 263 | readline 1 |
|
264 | 264 | |
|
265 | 265 | # Screen Length: number of lines of your screen. This is used to control |
|
266 | 266 | # printing of very long strings. Strings longer than this number of lines will |
|
267 | 267 | # be paged with the less command instead of directly printed. |
|
268 | 268 | |
|
269 | 269 | # The default value for this is 0, which means IPython will auto-detect your |
|
270 | 270 | # screen size every time it needs to print. If for some reason this isn't |
|
271 | 271 | # working well (it needs curses support), specify it yourself. Otherwise don't |
|
272 | 272 | # change the default. |
|
273 | 273 | |
|
274 | 274 | screen_length 0 |
|
275 | 275 | |
|
276 | 276 | # Prompt separators for input and output. |
|
277 | 277 | # Use \n for newline explicitly, without quotes. |
|
278 | 278 | # Use 0 (like at the cmd line) to turn off a given separator. |
|
279 | 279 | |
|
280 | 280 | # The structure of prompt printing is: |
|
281 | 281 | # (SeparateIn)Input.... |
|
282 | 282 | # (SeparateOut)Output... |
|
283 | 283 | # (SeparateOut2), # that is, no newline is printed after Out2 |
|
284 | 284 | # By choosing these you can organize your output any way you want. |
|
285 | 285 | |
|
286 | 286 | separate_in \n |
|
287 | 287 | separate_out 0 |
|
288 | 288 | separate_out2 0 |
|
289 | 289 | |
|
290 | 290 | # 'nosep 1' is a shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 0'. |
|
291 | 291 | # Simply removes all input/output separators, overriding the choices above. |
|
292 | 292 | nosep 0 |
|
293 | 293 | |
|
294 | 294 | # Wildcard searches - IPython has a system for searching names using |
|
295 | 295 | # shell-like wildcards; type %psearch? for details. This variables sets |
|
296 | 296 | # whether by default such searches should be case sensitive or not. You can |
|
297 | 297 | # always override the default at the system command line or the IPython |
|
298 | 298 | # prompt. |
|
299 | 299 | |
|
300 | 300 | wildcards_case_sensitive 1 |
|
301 | 301 | |
|
302 | 302 | # Object information: at what level of detail to display the string form of an |
|
303 | 303 | # object. If set to 0, ipython will compute the string form of any object X, |
|
304 | 304 | # by calling str(X), when X? is typed. If set to 1, str(X) will only be |
|
305 | 305 | # computed when X?? is given, and if set to 2 or higher, it will never be |
|
306 | 306 | # computed (there is no X??? level of detail). This is mostly of use to |
|
307 | 307 | # people who frequently manipulate objects whose string representation is |
|
308 | 308 | # extremely expensive to compute. |
|
309 | 309 | |
|
310 | 310 | object_info_string_level 0 |
|
311 | 311 | |
|
312 | 312 | # xmode - Exception reporting mode. |
|
313 | 313 | |
|
314 | 314 | # Valid modes: Plain, Context and Verbose. |
|
315 | 315 | |
|
316 | 316 | # Plain: similar to python's normal traceback printing. |
|
317 | 317 | |
|
318 | 318 | # Context: prints 5 lines of context source code around each line in the |
|
319 | 319 | # traceback. |
|
320 | 320 | |
|
321 | 321 | # Verbose: similar to Context, but additionally prints the variables currently |
|
322 | 322 | # visible where the exception happened (shortening their strings if too |
|
323 | 323 | # long). This can potentially be very slow, if you happen to have a huge data |
|
324 | 324 | # structure whose string representation is complex to compute. Your computer |
|
325 | 325 | # may appear to freeze for a while with cpu usage at 100%. If this occurs, you |
|
326 | 326 | # can cancel the traceback with Ctrl-C (maybe hitting it more than once). |
|
327 | 327 | |
|
328 | 328 | #xmode Plain |
|
329 | 329 | xmode Context |
|
330 | 330 | #xmode Verbose |
|
331 | 331 | |
|
332 | 332 | # multi_line_specials: if true, allow magics, aliases and shell escapes (via |
|
333 | 333 | # !cmd) to be used in multi-line input (like for loops). For example, if you |
|
334 | 334 | # have this active, the following is valid in IPython: |
|
335 | 335 | # |
|
336 | 336 | #In [17]: for i in range(3): |
|
337 | 337 | # ....: mkdir $i |
|
338 | 338 | # ....: !touch $i/hello |
|
339 | 339 | # ....: ls -l $i |
|
340 | 340 | |
|
341 | 341 | multi_line_specials 1 |
|
342 | 342 | |
|
343 | ||
|
344 | # System calls: When IPython makes system calls (e.g. via special syntax like | |
|
345 | # !cmd or !!cmd, or magics like %sc or %sx), it can print the command it is | |
|
346 | # executing to standard output, prefixed by a header string. | |
|
347 | ||
|
348 | system_header "IPython system call: " | |
|
349 | ||
|
350 | system_verbose 1 | |
|
351 | ||
|
343 | 352 | # wxversion: request a specific wxPython version (used for -wthread) |
|
344 | 353 | |
|
345 | 354 | # Set this to the value of wxPython you want to use, but note that this |
|
346 | 355 | # feature requires you to have the wxversion Python module to work. If you |
|
347 | 356 | # don't have the wxversion module (try 'import wxversion' at the prompt to |
|
348 | 357 | # check) or simply want to leave the system to pick up the default, leave this |
|
349 | 358 | # variable at 0. |
|
350 | 359 | |
|
351 | 360 | wxversion 0 |
|
352 | 361 | |
|
353 | 362 | #--------------------------------------------------------------------------- |
|
354 | 363 | # Section: Readline configuration (readline is not available for MS-Windows) |
|
355 | 364 | |
|
356 | 365 | # This is done via the following options: |
|
357 | 366 | |
|
358 | 367 | # (i) readline_parse_and_bind: this option can appear as many times as you |
|
359 | 368 | # want, each time defining a string to be executed via a |
|
360 | 369 | # readline.parse_and_bind() command. The syntax for valid commands of this |
|
361 | 370 | # kind can be found by reading the documentation for the GNU readline library, |
|
362 | 371 | # as these commands are of the kind which readline accepts in its |
|
363 | 372 | # configuration file. |
|
364 | 373 | |
|
365 | 374 | # The TAB key can be used to complete names at the command line in one of two |
|
366 | 375 | # ways: 'complete' and 'menu-complete'. The difference is that 'complete' only |
|
367 | 376 | # completes as much as possible while 'menu-complete' cycles through all |
|
368 | 377 | # possible completions. Leave the one you prefer uncommented. |
|
369 | 378 | |
|
370 | 379 | readline_parse_and_bind tab: complete |
|
371 | 380 | #readline_parse_and_bind tab: menu-complete |
|
372 | 381 | |
|
373 | 382 | # This binds Control-l to printing the list of all possible completions when |
|
374 | 383 | # there is more than one (what 'complete' does when hitting TAB twice, or at |
|
375 | 384 | # the first TAB if show-all-if-ambiguous is on) |
|
376 | 385 | readline_parse_and_bind "\C-l": possible-completions |
|
377 | 386 | |
|
378 | 387 | # This forces readline to automatically print the above list when tab |
|
379 | 388 | # completion is set to 'complete'. You can still get this list manually by |
|
380 | 389 | # using the key bound to 'possible-completions' (Control-l by default) or by |
|
381 | 390 | # hitting TAB twice. Turning this on makes the printing happen at the first |
|
382 | 391 | # TAB. |
|
383 | 392 | readline_parse_and_bind set show-all-if-ambiguous on |
|
384 | 393 | |
|
385 | 394 | # If you have TAB set to complete names, you can rebind any key (Control-o by |
|
386 | 395 | # default) to insert a true TAB character. |
|
387 | 396 | readline_parse_and_bind "\C-o": tab-insert |
|
388 | 397 | |
|
389 | 398 | # These commands allow you to indent/unindent easily, with the 4-space |
|
390 | 399 | # convention of the Python coding standards. Since IPython's internal |
|
391 | 400 | # auto-indent system also uses 4 spaces, you should not change the number of |
|
392 | 401 | # spaces in the code below. |
|
393 | 402 | readline_parse_and_bind "\M-i": " " |
|
394 | 403 | readline_parse_and_bind "\M-o": "\d\d\d\d" |
|
395 | 404 | readline_parse_and_bind "\M-I": "\d\d\d\d" |
|
396 | 405 | |
|
397 | 406 | # Bindings for incremental searches in the history. These searches use the |
|
398 | 407 | # string typed so far on the command line and search anything in the previous |
|
399 | 408 | # input history containing them. |
|
400 | 409 | readline_parse_and_bind "\C-r": reverse-search-history |
|
401 | 410 | readline_parse_and_bind "\C-s": forward-search-history |
|
402 | 411 | |
|
403 | 412 | # Bindings for completing the current line in the history of previous |
|
404 | 413 | # commands. This allows you to recall any previous command by typing its first |
|
405 | 414 | # few letters and hitting Control-p, bypassing all intermediate commands which |
|
406 | 415 | # may be in the history (much faster than hitting up-arrow 50 times!) |
|
407 | 416 | readline_parse_and_bind "\C-p": history-search-backward |
|
408 | 417 | readline_parse_and_bind "\C-n": history-search-forward |
|
409 | 418 | |
|
410 | 419 | # I also like to have the same functionality on the plain arrow keys. If you'd |
|
411 | 420 | # rather have the arrows use all the history (and not just match what you've |
|
412 | 421 | # typed so far), comment out or delete the next two lines. |
|
413 | 422 | readline_parse_and_bind "\e[A": history-search-backward |
|
414 | 423 | readline_parse_and_bind "\e[B": history-search-forward |
|
415 | 424 | |
|
416 | 425 | # These are typically on by default under *nix, but not win32. |
|
417 | 426 | readline_parse_and_bind "\C-k": kill-line |
|
418 | 427 | readline_parse_and_bind "\C-u": unix-line-discard |
|
419 | 428 | |
|
420 | 429 | # (ii) readline_remove_delims: a string of characters to be removed from the |
|
421 | 430 | # default word-delimiters list used by readline, so that completions may be |
|
422 | 431 | # performed on strings which contain them. |
|
423 | 432 | |
|
424 | 433 | readline_remove_delims -/~ |
|
425 | 434 | |
|
426 | 435 | # (iii) readline_merge_completions: whether to merge the result of all |
|
427 | 436 | # possible completions or not. If true, IPython will complete filenames, |
|
428 | 437 | # python names and aliases and return all possible completions. If you set it |
|
429 | 438 | # to false, each completer is used at a time, and only if it doesn't return |
|
430 | 439 | # any completions is the next one used. |
|
431 | 440 | |
|
432 | 441 | # The default order is: [python_matches, file_matches, alias_matches] |
|
433 | 442 | |
|
434 | 443 | readline_merge_completions 1 |
|
435 | 444 | |
|
436 | 445 | # (iv) readline_omit__names: normally hitting <tab> after a '.' in a name |
|
437 | 446 | # will complete all attributes of an object, including all the special methods |
|
438 | 447 | # whose names start with single or double underscores (like __getitem__ or |
|
439 | 448 | # __class__). |
|
440 | 449 | |
|
441 | 450 | # This variable allows you to control this completion behavior: |
|
442 | 451 | |
|
443 | 452 | # readline_omit__names 1 -> completion will omit showing any names starting |
|
444 | 453 | # with two __, but it will still show names starting with one _. |
|
445 | 454 | |
|
446 | 455 | # readline_omit__names 2 -> completion will omit all names beginning with one |
|
447 | 456 | # _ (which obviously means filtering out the double __ ones). |
|
448 | 457 | |
|
449 | 458 | # Even when this option is set, you can still see those names by explicitly |
|
450 | 459 | # typing a _ after the period and hitting <tab>: 'name._<tab>' will always |
|
451 | 460 | # complete attribute names starting with '_'. |
|
452 | 461 | |
|
453 | 462 | # This option is off by default so that new users see all attributes of any |
|
454 | 463 | # objects they are dealing with. |
|
455 | 464 | |
|
456 | 465 | readline_omit__names 0 |
|
457 | 466 | |
|
458 | 467 | #--------------------------------------------------------------------------- |
|
459 | 468 | # Section: modules to be loaded with 'import ...' |
|
460 | 469 | |
|
461 | 470 | # List, separated by spaces, the names of the modules you want to import |
|
462 | 471 | |
|
463 | 472 | # Example: |
|
464 | 473 | # import_mod sys os |
|
465 | 474 | # will produce internally the statements |
|
466 | 475 | # import sys |
|
467 | 476 | # import os |
|
468 | 477 | |
|
469 | 478 | # Each import is executed in its own try/except block, so if one module |
|
470 | 479 | # fails to load the others will still be ok. |
|
471 | 480 | |
|
472 | 481 | import_mod |
|
473 | 482 | |
|
474 | 483 | #--------------------------------------------------------------------------- |
|
475 | 484 | # Section: modules to import some functions from: 'from ... import ...' |
|
476 | 485 | |
|
477 | 486 | # List, one per line, the modules for which you want only to import some |
|
478 | 487 | # functions. Give the module name first and then the name of functions to be |
|
479 | 488 | # imported from that module. |
|
480 | 489 | |
|
481 | 490 | # Example: |
|
482 | 491 | |
|
483 | 492 | # import_some IPython.genutils timing timings |
|
484 | 493 | # will produce internally the statement |
|
485 | 494 | # from IPython.genutils import timing, timings |
|
486 | 495 | |
|
487 | 496 | # timing() and timings() are two IPython utilities for timing the execution of |
|
488 | 497 | # your own functions, which you may find useful. Just commment out the above |
|
489 | 498 | # line if you want to test them. |
|
490 | 499 | |
|
491 | 500 | # If you have more than one modules_some line, each gets its own try/except |
|
492 | 501 | # block (like modules, see above). |
|
493 | 502 | |
|
494 | 503 | import_some |
|
495 | 504 | |
|
496 | 505 | #--------------------------------------------------------------------------- |
|
497 | 506 | # Section: modules to import all from : 'from ... import *' |
|
498 | 507 | |
|
499 | 508 | # List (same syntax as import_mod above) those modules for which you want to |
|
500 | 509 | # import all functions. Remember, this is a potentially dangerous thing to do, |
|
501 | 510 | # since it is very easy to overwrite names of things you need. Use with |
|
502 | 511 | # caution. |
|
503 | 512 | |
|
504 | 513 | # Example: |
|
505 | 514 | # import_all sys os |
|
506 | 515 | # will produce internally the statements |
|
507 | 516 | # from sys import * |
|
508 | 517 | # from os import * |
|
509 | 518 | |
|
510 | 519 | # As before, each will be called in a separate try/except block. |
|
511 | 520 | |
|
512 | 521 | import_all |
|
513 | 522 | |
|
514 | 523 | #--------------------------------------------------------------------------- |
|
515 | 524 | # Section: Python code to execute. |
|
516 | 525 | |
|
517 | 526 | # Put here code to be explicitly executed (keep it simple!) |
|
518 | 527 | # Put one line of python code per line. All whitespace is removed (this is a |
|
519 | 528 | # feature, not a bug), so don't get fancy building loops here. |
|
520 | 529 | # This is just for quick convenient creation of things you want available. |
|
521 | 530 | |
|
522 | 531 | # Example: |
|
523 | 532 | # execute x = 1 |
|
524 | 533 | # execute print 'hello world'; y = z = 'a' |
|
525 | 534 | # will produce internally |
|
526 | 535 | # x = 1 |
|
527 | 536 | # print 'hello world'; y = z = 'a' |
|
528 | 537 | # and each *line* (not each statement, we don't do python syntax parsing) is |
|
529 | 538 | # executed in its own try/except block. |
|
530 | 539 | |
|
531 | 540 | execute |
|
532 | 541 | |
|
533 | 542 | # Note for the adventurous: you can use this to define your own names for the |
|
534 | 543 | # magic functions, by playing some namespace tricks: |
|
535 | 544 | |
|
536 | 545 | # execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
537 | 546 | |
|
538 | 547 | # defines %pf as a new name for %profile. |
|
539 | 548 | |
|
540 | 549 | #--------------------------------------------------------------------------- |
|
541 | 550 | # Section: Pyhton files to load and execute. |
|
542 | 551 | |
|
543 | 552 | # Put here the full names of files you want executed with execfile(file). If |
|
544 | 553 | # you want complicated initialization, just write whatever you want in a |
|
545 | 554 | # regular python file and load it from here. |
|
546 | 555 | |
|
547 | 556 | # Filenames defined here (which *must* include the extension) are searched for |
|
548 | 557 | # through all of sys.path. Since IPython adds your .ipython directory to |
|
549 | 558 | # sys.path, they can also be placed in your .ipython dir and will be |
|
550 | 559 | # found. Otherwise (if you want to execute things not in .ipyton nor in |
|
551 | 560 | # sys.path) give a full path (you can use ~, it gets expanded) |
|
552 | 561 | |
|
553 | 562 | # Example: |
|
554 | 563 | # execfile file1.py ~/file2.py |
|
555 | 564 | # will generate |
|
556 | 565 | # execfile('file1.py') |
|
557 | 566 | # execfile('_path_to_your_home/file2.py') |
|
558 | 567 | |
|
559 | 568 | # As before, each file gets its own try/except block. |
|
560 | 569 | |
|
561 | 570 | execfile |
|
562 | 571 | |
|
563 | 572 | # If you are feeling adventurous, you can even add functionality to IPython |
|
564 | 573 | # through here. IPython works through a global variable called __ip which |
|
565 | 574 | # exists at the time when these files are read. If you know what you are doing |
|
566 | 575 | # (read the source) you can add functions to __ip in files loaded here. |
|
567 | 576 | |
|
568 | 577 | # The file example-magic.py contains a simple but correct example. Try it: |
|
569 | 578 | |
|
570 | 579 | # execfile example-magic.py |
|
571 | 580 | |
|
572 | 581 | # Look at the examples in IPython/iplib.py for more details on how these magic |
|
573 | 582 | # functions need to process their arguments. |
|
574 | 583 | |
|
575 | 584 | #--------------------------------------------------------------------------- |
|
576 | 585 | # Section: aliases for system shell commands |
|
577 | 586 | |
|
578 | 587 | # Here you can define your own names for system commands. The syntax is |
|
579 | 588 | # similar to that of the builtin %alias function: |
|
580 | 589 | |
|
581 | 590 | # alias alias_name command_string |
|
582 | 591 | |
|
583 | 592 | # The resulting aliases are auto-generated magic functions (hence usable as |
|
584 | 593 | # %alias_name) |
|
585 | 594 | |
|
586 | 595 | # For example: |
|
587 | 596 | |
|
588 | 597 | # alias myls ls -la |
|
589 | 598 | |
|
590 | 599 | # will define 'myls' as an alias for executing the system command 'ls -la'. |
|
591 | 600 | # This allows you to customize IPython's environment to have the same aliases |
|
592 | 601 | # you are accustomed to from your own shell. |
|
593 | 602 | |
|
594 | 603 | # You can also define aliases with parameters using %s specifiers (one per |
|
595 | 604 | # parameter): |
|
596 | 605 | |
|
597 | 606 | # alias parts echo first %s second %s |
|
598 | 607 | |
|
599 | 608 | # will give you in IPython: |
|
600 | 609 | # >>> %parts A B |
|
601 | 610 | # first A second B |
|
602 | 611 | |
|
603 | 612 | # Use one 'alias' statement per alias you wish to define. |
|
604 | 613 | |
|
605 | 614 | # alias |
|
606 | 615 | |
|
607 | 616 | #************************* end of file <ipythonrc> ************************ |
@@ -1,2489 +1,2515 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.3 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 187 |
|
|
9 | $Id: iplib.py 1879 2006-11-04 00:34:34Z fptest $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | 31 | from IPython import Release |
|
32 | 32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
33 | 33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
34 | 34 | __license__ = Release.license |
|
35 | 35 | __version__ = Release.version |
|
36 | 36 | |
|
37 | 37 | # Python standard modules |
|
38 | 38 | import __main__ |
|
39 | 39 | import __builtin__ |
|
40 | 40 | import StringIO |
|
41 | 41 | import bdb |
|
42 | 42 | import cPickle as pickle |
|
43 | 43 | import codeop |
|
44 | 44 | import exceptions |
|
45 | 45 | import glob |
|
46 | 46 | import inspect |
|
47 | 47 | import keyword |
|
48 | 48 | import new |
|
49 | 49 | import os |
|
50 | 50 | import pydoc |
|
51 | 51 | import re |
|
52 | 52 | import shutil |
|
53 | 53 | import string |
|
54 | 54 | import sys |
|
55 | 55 | import tempfile |
|
56 | 56 | import traceback |
|
57 | 57 | import types |
|
58 | 58 | import pickleshare |
|
59 | 59 | from sets import Set |
|
60 | 60 | from pprint import pprint, pformat |
|
61 | 61 | |
|
62 | 62 | # IPython's own modules |
|
63 | 63 | import IPython |
|
64 | 64 | from IPython import OInspect,PyColorize,ultraTB |
|
65 | 65 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
66 | 66 | from IPython.FakeModule import FakeModule |
|
67 | 67 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
68 | 68 | from IPython.Logger import Logger |
|
69 | 69 | from IPython.Magic import Magic |
|
70 | 70 | from IPython.Prompts import CachedOutput |
|
71 | 71 | from IPython.ipstruct import Struct |
|
72 | 72 | from IPython.background_jobs import BackgroundJobManager |
|
73 | 73 | from IPython.usage import cmd_line_usage,interactive_usage |
|
74 | 74 | from IPython.genutils import * |
|
75 | 75 | from IPython.strdispatch import StrDispatch |
|
76 | 76 | import IPython.ipapi |
|
77 | 77 | |
|
78 | 78 | # Globals |
|
79 | 79 | |
|
80 | 80 | # store the builtin raw_input globally, and use this always, in case user code |
|
81 | 81 | # overwrites it (like wx.py.PyShell does) |
|
82 | 82 | raw_input_original = raw_input |
|
83 | 83 | |
|
84 | 84 | # compiled regexps for autoindent management |
|
85 | 85 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | #**************************************************************************** |
|
89 | 89 | # Some utility function definitions |
|
90 | 90 | |
|
91 | 91 | ini_spaces_re = re.compile(r'^(\s+)') |
|
92 | 92 | |
|
93 | 93 | def num_ini_spaces(strng): |
|
94 | 94 | """Return the number of initial spaces in a string""" |
|
95 | 95 | |
|
96 | 96 | ini_spaces = ini_spaces_re.match(strng) |
|
97 | 97 | if ini_spaces: |
|
98 | 98 | return ini_spaces.end() |
|
99 | 99 | else: |
|
100 | 100 | return 0 |
|
101 | 101 | |
|
102 | 102 | def softspace(file, newvalue): |
|
103 | 103 | """Copied from code.py, to remove the dependency""" |
|
104 | 104 | |
|
105 | 105 | oldvalue = 0 |
|
106 | 106 | try: |
|
107 | 107 | oldvalue = file.softspace |
|
108 | 108 | except AttributeError: |
|
109 | 109 | pass |
|
110 | 110 | try: |
|
111 | 111 | file.softspace = newvalue |
|
112 | 112 | except (AttributeError, TypeError): |
|
113 | 113 | # "attribute-less object" or "read-only attributes" |
|
114 | 114 | pass |
|
115 | 115 | return oldvalue |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | #**************************************************************************** |
|
119 | 119 | # Local use exceptions |
|
120 | 120 | class SpaceInInput(exceptions.Exception): pass |
|
121 | 121 | |
|
122 | 122 | |
|
123 | 123 | #**************************************************************************** |
|
124 | 124 | # Local use classes |
|
125 | 125 | class Bunch: pass |
|
126 | 126 | |
|
127 | 127 | class Undefined: pass |
|
128 | 128 | |
|
129 | 129 | class Quitter(object): |
|
130 | 130 | """Simple class to handle exit, similar to Python 2.5's. |
|
131 | 131 | |
|
132 | 132 | It handles exiting in an ipython-safe manner, which the one in Python 2.5 |
|
133 | 133 | doesn't do (obviously, since it doesn't know about ipython).""" |
|
134 | 134 | |
|
135 | 135 | def __init__(self,shell,name): |
|
136 | 136 | self.shell = shell |
|
137 | 137 | self.name = name |
|
138 | 138 | |
|
139 | 139 | def __repr__(self): |
|
140 | 140 | return 'Type %s() to exit.' % self.name |
|
141 | 141 | __str__ = __repr__ |
|
142 | 142 | |
|
143 | 143 | def __call__(self): |
|
144 | 144 | self.shell.exit() |
|
145 | 145 | |
|
146 | 146 | class InputList(list): |
|
147 | 147 | """Class to store user input. |
|
148 | 148 | |
|
149 | 149 | It's basically a list, but slices return a string instead of a list, thus |
|
150 | 150 | allowing things like (assuming 'In' is an instance): |
|
151 | 151 | |
|
152 | 152 | exec In[4:7] |
|
153 | 153 | |
|
154 | 154 | or |
|
155 | 155 | |
|
156 | 156 | exec In[5:9] + In[14] + In[21:25]""" |
|
157 | 157 | |
|
158 | 158 | def __getslice__(self,i,j): |
|
159 | 159 | return ''.join(list.__getslice__(self,i,j)) |
|
160 | 160 | |
|
161 | 161 | class SyntaxTB(ultraTB.ListTB): |
|
162 | 162 | """Extension which holds some state: the last exception value""" |
|
163 | 163 | |
|
164 | 164 | def __init__(self,color_scheme = 'NoColor'): |
|
165 | 165 | ultraTB.ListTB.__init__(self,color_scheme) |
|
166 | 166 | self.last_syntax_error = None |
|
167 | 167 | |
|
168 | 168 | def __call__(self, etype, value, elist): |
|
169 | 169 | self.last_syntax_error = value |
|
170 | 170 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
171 | 171 | |
|
172 | 172 | def clear_err_state(self): |
|
173 | 173 | """Return the current error state and clear it""" |
|
174 | 174 | e = self.last_syntax_error |
|
175 | 175 | self.last_syntax_error = None |
|
176 | 176 | return e |
|
177 | 177 | |
|
178 | 178 | #**************************************************************************** |
|
179 | 179 | # Main IPython class |
|
180 | 180 | |
|
181 | 181 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
182 | 182 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
183 | 183 | # attributes and methods, but too much user code out there relies on the |
|
184 | 184 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
185 | 185 | # |
|
186 | 186 | # But at least now, all the pieces have been separated and we could, in |
|
187 | 187 | # principle, stop using the mixin. This will ease the transition to the |
|
188 | 188 | # chainsaw branch. |
|
189 | 189 | |
|
190 | 190 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
191 | 191 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
192 | 192 | # class, to prevent clashes. |
|
193 | 193 | |
|
194 | 194 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
195 | 195 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
196 | 196 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
197 | 197 | # 'self.value'] |
|
198 | 198 | |
|
199 | 199 | class InteractiveShell(object,Magic): |
|
200 | 200 | """An enhanced console for Python.""" |
|
201 | 201 | |
|
202 | 202 | # class attribute to indicate whether the class supports threads or not. |
|
203 | 203 | # Subclasses with thread support should override this as needed. |
|
204 | 204 | isthreaded = False |
|
205 | 205 | |
|
206 | 206 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
207 | 207 | user_ns = None,user_global_ns=None,banner2='', |
|
208 | 208 | custom_exceptions=((),None),embedded=False): |
|
209 | 209 | |
|
210 | 210 | # log system |
|
211 | 211 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
212 | 212 | |
|
213 | 213 | # some minimal strict typechecks. For some core data structures, I |
|
214 | 214 | # want actual basic python types, not just anything that looks like |
|
215 | 215 | # one. This is especially true for namespaces. |
|
216 | 216 | for ns in (user_ns,user_global_ns): |
|
217 | 217 | if ns is not None and type(ns) != types.DictType: |
|
218 | 218 | raise TypeError,'namespace must be a dictionary' |
|
219 | 219 | |
|
220 | 220 | # Job manager (for jobs run as background threads) |
|
221 | 221 | self.jobs = BackgroundJobManager() |
|
222 | 222 | |
|
223 | 223 | # Store the actual shell's name |
|
224 | 224 | self.name = name |
|
225 | 225 | |
|
226 | 226 | # We need to know whether the instance is meant for embedding, since |
|
227 | 227 | # global/local namespaces need to be handled differently in that case |
|
228 | 228 | self.embedded = embedded |
|
229 | 229 | |
|
230 | 230 | # command compiler |
|
231 | 231 | self.compile = codeop.CommandCompiler() |
|
232 | 232 | |
|
233 | 233 | # User input buffer |
|
234 | 234 | self.buffer = [] |
|
235 | 235 | |
|
236 | 236 | # Default name given in compilation of code |
|
237 | 237 | self.filename = '<ipython console>' |
|
238 | 238 | |
|
239 | 239 | # Install our own quitter instead of the builtins. For python2.3-2.4, |
|
240 | 240 | # this brings in behavior like 2.5, and for 2.5 it's identical. |
|
241 | 241 | __builtin__.exit = Quitter(self,'exit') |
|
242 | 242 | __builtin__.quit = Quitter(self,'quit') |
|
243 | 243 | |
|
244 | 244 | # Make an empty namespace, which extension writers can rely on both |
|
245 | 245 | # existing and NEVER being used by ipython itself. This gives them a |
|
246 | 246 | # convenient location for storing additional information and state |
|
247 | 247 | # their extensions may require, without fear of collisions with other |
|
248 | 248 | # ipython names that may develop later. |
|
249 | 249 | self.meta = Struct() |
|
250 | 250 | |
|
251 | 251 | # Create the namespace where the user will operate. user_ns is |
|
252 | 252 | # normally the only one used, and it is passed to the exec calls as |
|
253 | 253 | # the locals argument. But we do carry a user_global_ns namespace |
|
254 | 254 | # given as the exec 'globals' argument, This is useful in embedding |
|
255 | 255 | # situations where the ipython shell opens in a context where the |
|
256 | 256 | # distinction between locals and globals is meaningful. |
|
257 | 257 | |
|
258 | 258 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
259 | 259 | # level as a dict instead of a module. This is a manual fix, but I |
|
260 | 260 | # should really track down where the problem is coming from. Alex |
|
261 | 261 | # Schmolck reported this problem first. |
|
262 | 262 | |
|
263 | 263 | # A useful post by Alex Martelli on this topic: |
|
264 | 264 | # Re: inconsistent value from __builtins__ |
|
265 | 265 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
266 | 266 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
267 | 267 | # Gruppen: comp.lang.python |
|
268 | 268 | |
|
269 | 269 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
270 | 270 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
271 | 271 | # > <type 'dict'> |
|
272 | 272 | # > >>> print type(__builtins__) |
|
273 | 273 | # > <type 'module'> |
|
274 | 274 | # > Is this difference in return value intentional? |
|
275 | 275 | |
|
276 | 276 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
277 | 277 | # or a module, and it's been that way for a long time. Whether it's |
|
278 | 278 | # intentional (or sensible), I don't know. In any case, the idea is |
|
279 | 279 | # that if you need to access the built-in namespace directly, you |
|
280 | 280 | # should start with "import __builtin__" (note, no 's') which will |
|
281 | 281 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
282 | 282 | |
|
283 | 283 | # These routines return properly built dicts as needed by the rest of |
|
284 | 284 | # the code, and can also be used by extension writers to generate |
|
285 | 285 | # properly initialized namespaces. |
|
286 | 286 | user_ns = IPython.ipapi.make_user_ns(user_ns) |
|
287 | 287 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) |
|
288 | 288 | |
|
289 | 289 | # Assign namespaces |
|
290 | 290 | # This is the namespace where all normal user variables live |
|
291 | 291 | self.user_ns = user_ns |
|
292 | 292 | # Embedded instances require a separate namespace for globals. |
|
293 | 293 | # Normally this one is unused by non-embedded instances. |
|
294 | 294 | self.user_global_ns = user_global_ns |
|
295 | 295 | # A namespace to keep track of internal data structures to prevent |
|
296 | 296 | # them from cluttering user-visible stuff. Will be updated later |
|
297 | 297 | self.internal_ns = {} |
|
298 | 298 | |
|
299 | 299 | # Namespace of system aliases. Each entry in the alias |
|
300 | 300 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
301 | 301 | # of positional arguments of the alias. |
|
302 | 302 | self.alias_table = {} |
|
303 | 303 | |
|
304 | 304 | # A table holding all the namespaces IPython deals with, so that |
|
305 | 305 | # introspection facilities can search easily. |
|
306 | 306 | self.ns_table = {'user':user_ns, |
|
307 | 307 | 'user_global':user_global_ns, |
|
308 | 308 | 'alias':self.alias_table, |
|
309 | 309 | 'internal':self.internal_ns, |
|
310 | 310 | 'builtin':__builtin__.__dict__ |
|
311 | 311 | } |
|
312 | 312 | |
|
313 | 313 | # The user namespace MUST have a pointer to the shell itself. |
|
314 | 314 | self.user_ns[name] = self |
|
315 | 315 | |
|
316 | 316 | # We need to insert into sys.modules something that looks like a |
|
317 | 317 | # module but which accesses the IPython namespace, for shelve and |
|
318 | 318 | # pickle to work interactively. Normally they rely on getting |
|
319 | 319 | # everything out of __main__, but for embedding purposes each IPython |
|
320 | 320 | # instance has its own private namespace, so we can't go shoving |
|
321 | 321 | # everything into __main__. |
|
322 | 322 | |
|
323 | 323 | # note, however, that we should only do this for non-embedded |
|
324 | 324 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
325 | 325 | # namespace. Embedded instances, on the other hand, should not do |
|
326 | 326 | # this because they need to manage the user local/global namespaces |
|
327 | 327 | # only, but they live within a 'normal' __main__ (meaning, they |
|
328 | 328 | # shouldn't overtake the execution environment of the script they're |
|
329 | 329 | # embedded in). |
|
330 | 330 | |
|
331 | 331 | if not embedded: |
|
332 | 332 | try: |
|
333 | 333 | main_name = self.user_ns['__name__'] |
|
334 | 334 | except KeyError: |
|
335 | 335 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
336 | 336 | else: |
|
337 | 337 | #print "pickle hack in place" # dbg |
|
338 | 338 | #print 'main_name:',main_name # dbg |
|
339 | 339 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
340 | 340 | |
|
341 | 341 | # List of input with multi-line handling. |
|
342 | 342 | # Fill its zero entry, user counter starts at 1 |
|
343 | 343 | self.input_hist = InputList(['\n']) |
|
344 | 344 | # This one will hold the 'raw' input history, without any |
|
345 | 345 | # pre-processing. This will allow users to retrieve the input just as |
|
346 | 346 | # it was exactly typed in by the user, with %hist -r. |
|
347 | 347 | self.input_hist_raw = InputList(['\n']) |
|
348 | 348 | |
|
349 | 349 | # list of visited directories |
|
350 | 350 | try: |
|
351 | 351 | self.dir_hist = [os.getcwd()] |
|
352 | 352 | except IOError, e: |
|
353 | 353 | self.dir_hist = [] |
|
354 | 354 | |
|
355 | 355 | # dict of output history |
|
356 | 356 | self.output_hist = {} |
|
357 | 357 | |
|
358 | 358 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
359 | 359 | no_alias = {} |
|
360 | 360 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
361 | 361 | for key in keyword.kwlist + no_alias_magics: |
|
362 | 362 | no_alias[key] = 1 |
|
363 | 363 | no_alias.update(__builtin__.__dict__) |
|
364 | 364 | self.no_alias = no_alias |
|
365 | 365 | |
|
366 | 366 | # make global variables for user access to these |
|
367 | 367 | self.user_ns['_ih'] = self.input_hist |
|
368 | 368 | self.user_ns['_oh'] = self.output_hist |
|
369 | 369 | self.user_ns['_dh'] = self.dir_hist |
|
370 | 370 | |
|
371 | 371 | # user aliases to input and output histories |
|
372 | 372 | self.user_ns['In'] = self.input_hist |
|
373 | 373 | self.user_ns['Out'] = self.output_hist |
|
374 | 374 | |
|
375 | 375 | # Object variable to store code object waiting execution. This is |
|
376 | 376 | # used mainly by the multithreaded shells, but it can come in handy in |
|
377 | 377 | # other situations. No need to use a Queue here, since it's a single |
|
378 | 378 | # item which gets cleared once run. |
|
379 | 379 | self.code_to_run = None |
|
380 | 380 | |
|
381 | 381 | # escapes for automatic behavior on the command line |
|
382 | 382 | self.ESC_SHELL = '!' |
|
383 | 383 | self.ESC_HELP = '?' |
|
384 | 384 | self.ESC_MAGIC = '%' |
|
385 | 385 | self.ESC_QUOTE = ',' |
|
386 | 386 | self.ESC_QUOTE2 = ';' |
|
387 | 387 | self.ESC_PAREN = '/' |
|
388 | 388 | |
|
389 | 389 | # And their associated handlers |
|
390 | 390 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
391 | 391 | self.ESC_QUOTE : self.handle_auto, |
|
392 | 392 | self.ESC_QUOTE2 : self.handle_auto, |
|
393 | 393 | self.ESC_MAGIC : self.handle_magic, |
|
394 | 394 | self.ESC_HELP : self.handle_help, |
|
395 | 395 | self.ESC_SHELL : self.handle_shell_escape, |
|
396 | 396 | } |
|
397 | 397 | |
|
398 | 398 | # class initializations |
|
399 | 399 | Magic.__init__(self,self) |
|
400 | 400 | |
|
401 | 401 | # Python source parser/formatter for syntax highlighting |
|
402 | 402 | pyformat = PyColorize.Parser().format |
|
403 | 403 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
404 | 404 | |
|
405 | 405 | # hooks holds pointers used for user-side customizations |
|
406 | 406 | self.hooks = Struct() |
|
407 | 407 | |
|
408 | 408 | self.strdispatchers = {} |
|
409 | 409 | |
|
410 | 410 | # Set all default hooks, defined in the IPython.hooks module. |
|
411 | 411 | hooks = IPython.hooks |
|
412 | 412 | for hook_name in hooks.__all__: |
|
413 | 413 | # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority |
|
414 | 414 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
415 | 415 | #print "bound hook",hook_name |
|
416 | 416 | |
|
417 | 417 | # Flag to mark unconditional exit |
|
418 | 418 | self.exit_now = False |
|
419 | 419 | |
|
420 | 420 | self.usage_min = """\ |
|
421 | 421 | An enhanced console for Python. |
|
422 | 422 | Some of its features are: |
|
423 | 423 | - Readline support if the readline library is present. |
|
424 | 424 | - Tab completion in the local namespace. |
|
425 | 425 | - Logging of input, see command-line options. |
|
426 | 426 | - System shell escape via ! , eg !ls. |
|
427 | 427 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
428 | 428 | - Keeps track of locally defined variables via %who, %whos. |
|
429 | 429 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
430 | 430 | """ |
|
431 | 431 | if usage: self.usage = usage |
|
432 | 432 | else: self.usage = self.usage_min |
|
433 | 433 | |
|
434 | 434 | # Storage |
|
435 | 435 | self.rc = rc # This will hold all configuration information |
|
436 | 436 | self.pager = 'less' |
|
437 | 437 | # temporary files used for various purposes. Deleted at exit. |
|
438 | 438 | self.tempfiles = [] |
|
439 | 439 | |
|
440 | 440 | # Keep track of readline usage (later set by init_readline) |
|
441 | 441 | self.has_readline = False |
|
442 | 442 | |
|
443 | 443 | # template for logfile headers. It gets resolved at runtime by the |
|
444 | 444 | # logstart method. |
|
445 | 445 | self.loghead_tpl = \ |
|
446 | 446 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
447 | 447 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
448 | 448 | #log# opts = %s |
|
449 | 449 | #log# args = %s |
|
450 | 450 | #log# It is safe to make manual edits below here. |
|
451 | 451 | #log#----------------------------------------------------------------------- |
|
452 | 452 | """ |
|
453 | 453 | # for pushd/popd management |
|
454 | 454 | try: |
|
455 | 455 | self.home_dir = get_home_dir() |
|
456 | 456 | except HomeDirError,msg: |
|
457 | 457 | fatal(msg) |
|
458 | 458 | |
|
459 | 459 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
460 | 460 | |
|
461 | 461 | # Functions to call the underlying shell. |
|
462 | 462 | |
|
463 | 463 | # The first is similar to os.system, but it doesn't return a value, |
|
464 | 464 | # and it allows interpolation of variables in the user's namespace. |
|
465 | 465 | self.system = lambda cmd: \ |
|
466 | 466 | shell(self.var_expand(cmd,depth=2), |
|
467 |
header= |
|
|
467 | header=self.rc.system_header, | |
|
468 | 468 | verbose=self.rc.system_verbose) |
|
469 | ||
|
469 | 470 | # These are for getoutput and getoutputerror: |
|
470 | 471 | self.getoutput = lambda cmd: \ |
|
471 | 472 | getoutput(self.var_expand(cmd,depth=2), |
|
472 |
header= |
|
|
473 | header=self.rc.system_header, | |
|
473 | 474 | verbose=self.rc.system_verbose) |
|
475 | ||
|
474 | 476 | self.getoutputerror = lambda cmd: \ |
|
475 | 477 | getoutputerror(self.var_expand(cmd,depth=2), |
|
476 |
header= |
|
|
478 | header=self.rc.system_header, | |
|
477 | 479 | verbose=self.rc.system_verbose) |
|
478 | 480 | |
|
479 | 481 | # RegExp for splitting line contents into pre-char//first |
|
480 | 482 | # word-method//rest. For clarity, each group in on one line. |
|
481 | 483 | |
|
482 | 484 | # WARNING: update the regexp if the above escapes are changed, as they |
|
483 | 485 | # are hardwired in. |
|
484 | 486 | |
|
485 | 487 | # Don't get carried away with trying to make the autocalling catch too |
|
486 | 488 | # much: it's better to be conservative rather than to trigger hidden |
|
487 | 489 | # evals() somewhere and end up causing side effects. |
|
488 | 490 | |
|
489 | 491 | self.line_split = re.compile(r'^([\s*,;/])' |
|
490 | 492 | r'([\?\w\.]+\w*\s*)' |
|
491 | 493 | r'(\(?.*$)') |
|
492 | 494 | |
|
493 | 495 | # Original re, keep around for a while in case changes break something |
|
494 | 496 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
495 | 497 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
496 | 498 | # r'(\(?.*$)') |
|
497 | 499 | |
|
498 | 500 | # RegExp to identify potential function names |
|
499 | 501 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
500 | 502 | |
|
501 | 503 | # RegExp to exclude strings with this start from autocalling. In |
|
502 | 504 | # particular, all binary operators should be excluded, so that if foo |
|
503 | 505 | # is callable, foo OP bar doesn't become foo(OP bar), which is |
|
504 | 506 | # invalid. The characters '!=()' don't need to be checked for, as the |
|
505 | 507 | # _prefilter routine explicitely does so, to catch direct calls and |
|
506 | 508 | # rebindings of existing names. |
|
507 | 509 | |
|
508 | 510 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
509 | 511 | # it affects the rest of the group in square brackets. |
|
510 | 512 | self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]' |
|
511 | 513 | '|^is |^not |^in |^and |^or ') |
|
512 | 514 | |
|
513 | 515 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
514 | 516 | # (experimental). For this to work, the line_split regexp would need |
|
515 | 517 | # to be modified so it wouldn't break things at '['. That line is |
|
516 | 518 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
517 | 519 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
518 | 520 | |
|
519 | 521 | # keep track of where we started running (mainly for crash post-mortem) |
|
520 | 522 | self.starting_dir = os.getcwd() |
|
521 | 523 | |
|
522 | 524 | # Various switches which can be set |
|
523 | 525 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
524 | 526 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
525 | 527 | self.banner2 = banner2 |
|
526 | 528 | |
|
527 | 529 | # TraceBack handlers: |
|
528 | 530 | |
|
529 | 531 | # Syntax error handler. |
|
530 | 532 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
531 | 533 | |
|
532 | 534 | # The interactive one is initialized with an offset, meaning we always |
|
533 | 535 | # want to remove the topmost item in the traceback, which is our own |
|
534 | 536 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
535 | 537 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
536 | 538 | color_scheme='NoColor', |
|
537 | 539 | tb_offset = 1) |
|
538 | 540 | |
|
539 | 541 | # IPython itself shouldn't crash. This will produce a detailed |
|
540 | 542 | # post-mortem if it does. But we only install the crash handler for |
|
541 | 543 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
542 | 544 | # and lose the crash handler. This is because exceptions in the main |
|
543 | 545 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
544 | 546 | # and there's no point in printing crash dumps for every user exception. |
|
545 | 547 | if self.isthreaded: |
|
546 | 548 | ipCrashHandler = ultraTB.FormattedTB() |
|
547 | 549 | else: |
|
548 | 550 | from IPython import CrashHandler |
|
549 | 551 | ipCrashHandler = CrashHandler.IPythonCrashHandler(self) |
|
550 | 552 | self.set_crash_handler(ipCrashHandler) |
|
551 | 553 | |
|
552 | 554 | # and add any custom exception handlers the user may have specified |
|
553 | 555 | self.set_custom_exc(*custom_exceptions) |
|
554 | 556 | |
|
555 | 557 | # indentation management |
|
556 | 558 | self.autoindent = False |
|
557 | 559 | self.indent_current_nsp = 0 |
|
558 | 560 | |
|
559 | 561 | # Make some aliases automatically |
|
560 | 562 | # Prepare list of shell aliases to auto-define |
|
561 | 563 | if os.name == 'posix': |
|
562 | 564 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
563 | 565 | 'mv mv -i','rm rm -i','cp cp -i', |
|
564 | 566 | 'cat cat','less less','clear clear', |
|
565 | 567 | # a better ls |
|
566 | 568 | 'ls ls -F', |
|
567 | 569 | # long ls |
|
568 | 570 | 'll ls -lF') |
|
569 | 571 | # Extra ls aliases with color, which need special treatment on BSD |
|
570 | 572 | # variants |
|
571 | 573 | ls_extra = ( # color ls |
|
572 | 574 | 'lc ls -F -o --color', |
|
573 | 575 | # ls normal files only |
|
574 | 576 | 'lf ls -F -o --color %l | grep ^-', |
|
575 | 577 | # ls symbolic links |
|
576 | 578 | 'lk ls -F -o --color %l | grep ^l', |
|
577 | 579 | # directories or links to directories, |
|
578 | 580 | 'ldir ls -F -o --color %l | grep /$', |
|
579 | 581 | # things which are executable |
|
580 | 582 | 'lx ls -F -o --color %l | grep ^-..x', |
|
581 | 583 | ) |
|
582 | 584 | # The BSDs don't ship GNU ls, so they don't understand the |
|
583 | 585 | # --color switch out of the box |
|
584 | 586 | if 'bsd' in sys.platform: |
|
585 | 587 | ls_extra = ( # ls normal files only |
|
586 | 588 | 'lf ls -lF | grep ^-', |
|
587 | 589 | # ls symbolic links |
|
588 | 590 | 'lk ls -lF | grep ^l', |
|
589 | 591 | # directories or links to directories, |
|
590 | 592 | 'ldir ls -lF | grep /$', |
|
591 | 593 | # things which are executable |
|
592 | 594 | 'lx ls -lF | grep ^-..x', |
|
593 | 595 | ) |
|
594 | 596 | auto_alias = auto_alias + ls_extra |
|
595 | 597 | elif os.name in ['nt','dos']: |
|
596 | 598 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
597 | 599 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
598 | 600 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
599 | 601 | 'ren ren','cls cls','copy copy') |
|
600 | 602 | else: |
|
601 | 603 | auto_alias = () |
|
602 | 604 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
603 | 605 | # Call the actual (public) initializer |
|
604 | 606 | self.init_auto_alias() |
|
605 | 607 | |
|
606 | 608 | # Produce a public API instance |
|
607 | 609 | self.api = IPython.ipapi.IPApi(self) |
|
608 | 610 | |
|
609 | 611 | # track which builtins we add, so we can clean up later |
|
610 | 612 | self.builtins_added = {} |
|
611 | 613 | # This method will add the necessary builtins for operation, but |
|
612 | 614 | # tracking what it did via the builtins_added dict. |
|
613 | 615 | self.add_builtins() |
|
614 | 616 | |
|
615 | 617 | # end __init__ |
|
616 | 618 | |
|
617 | 619 | def var_expand(self,cmd,depth=0): |
|
618 | 620 | """Expand python variables in a string. |
|
619 | 621 | |
|
620 | 622 | The depth argument indicates how many frames above the caller should |
|
621 | 623 | be walked to look for the local namespace where to expand variables. |
|
622 | 624 | |
|
623 | 625 | The global namespace for expansion is always the user's interactive |
|
624 | 626 | namespace. |
|
625 | 627 | """ |
|
626 | 628 | |
|
627 | 629 | return str(ItplNS(cmd.replace('#','\#'), |
|
628 | 630 | self.user_ns, # globals |
|
629 | 631 | # Skip our own frame in searching for locals: |
|
630 | 632 | sys._getframe(depth+1).f_locals # locals |
|
631 | 633 | )) |
|
632 | 634 | |
|
633 | 635 | def pre_config_initialization(self): |
|
634 | 636 | """Pre-configuration init method |
|
635 | 637 | |
|
636 | 638 | This is called before the configuration files are processed to |
|
637 | 639 | prepare the services the config files might need. |
|
638 | 640 | |
|
639 | 641 | self.rc already has reasonable default values at this point. |
|
640 | 642 | """ |
|
641 | 643 | rc = self.rc |
|
642 | 644 | |
|
643 | 645 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
644 | 646 | |
|
645 | 647 | def post_config_initialization(self): |
|
646 | 648 | """Post configuration init method |
|
647 | 649 | |
|
648 | 650 | This is called after the configuration files have been processed to |
|
649 | 651 | 'finalize' the initialization.""" |
|
650 | 652 | |
|
651 | 653 | rc = self.rc |
|
652 | 654 | |
|
653 | 655 | # Object inspector |
|
654 | 656 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
655 | 657 | PyColorize.ANSICodeColors, |
|
656 | 658 | 'NoColor', |
|
657 | 659 | rc.object_info_string_level) |
|
658 | 660 | |
|
659 | 661 | # Load readline proper |
|
660 | 662 | if rc.readline: |
|
661 | 663 | self.init_readline() |
|
662 | 664 | |
|
663 | 665 | # local shortcut, this is used a LOT |
|
664 | 666 | self.log = self.logger.log |
|
665 | 667 | |
|
666 | 668 | # Initialize cache, set in/out prompts and printing system |
|
667 | 669 | self.outputcache = CachedOutput(self, |
|
668 | 670 | rc.cache_size, |
|
669 | 671 | rc.pprint, |
|
670 | 672 | input_sep = rc.separate_in, |
|
671 | 673 | output_sep = rc.separate_out, |
|
672 | 674 | output_sep2 = rc.separate_out2, |
|
673 | 675 | ps1 = rc.prompt_in1, |
|
674 | 676 | ps2 = rc.prompt_in2, |
|
675 | 677 | ps_out = rc.prompt_out, |
|
676 | 678 | pad_left = rc.prompts_pad_left) |
|
677 | 679 | |
|
678 | 680 | # user may have over-ridden the default print hook: |
|
679 | 681 | try: |
|
680 | 682 | self.outputcache.__class__.display = self.hooks.display |
|
681 | 683 | except AttributeError: |
|
682 | 684 | pass |
|
683 | 685 | |
|
684 | 686 | # I don't like assigning globally to sys, because it means when |
|
685 | 687 | # embedding instances, each embedded instance overrides the previous |
|
686 | 688 | # choice. But sys.displayhook seems to be called internally by exec, |
|
687 | 689 | # so I don't see a way around it. We first save the original and then |
|
688 | 690 | # overwrite it. |
|
689 | 691 | self.sys_displayhook = sys.displayhook |
|
690 | 692 | sys.displayhook = self.outputcache |
|
691 | 693 | |
|
692 | 694 | # Set user colors (don't do it in the constructor above so that it |
|
693 | 695 | # doesn't crash if colors option is invalid) |
|
694 | 696 | self.magic_colors(rc.colors) |
|
695 | 697 | |
|
696 | 698 | # Set calling of pdb on exceptions |
|
697 | 699 | self.call_pdb = rc.pdb |
|
698 | 700 | |
|
699 | 701 | # Load user aliases |
|
700 | 702 | for alias in rc.alias: |
|
701 | 703 | self.magic_alias(alias) |
|
702 | 704 | self.hooks.late_startup_hook() |
|
703 | 705 | |
|
704 | 706 | batchrun = False |
|
705 | 707 | for batchfile in [path(arg) for arg in self.rc.args |
|
706 | 708 | if arg.lower().endswith('.ipy')]: |
|
707 | 709 | if not batchfile.isfile(): |
|
708 | 710 | print "No such batch file:", batchfile |
|
709 | 711 | continue |
|
710 | 712 | self.api.runlines(batchfile.text()) |
|
711 | 713 | batchrun = True |
|
712 | 714 | if batchrun: |
|
713 | 715 | self.exit_now = True |
|
714 | 716 | |
|
715 | 717 | def add_builtins(self): |
|
716 | 718 | """Store ipython references into the builtin namespace. |
|
717 | 719 | |
|
718 | 720 | Some parts of ipython operate via builtins injected here, which hold a |
|
719 | 721 | reference to IPython itself.""" |
|
720 | 722 | |
|
721 | 723 | # TODO: deprecate all except _ip; 'jobs' should be installed |
|
722 | 724 | # by an extension and the rest are under _ip, ipalias is redundant |
|
723 | 725 | builtins_new = dict(__IPYTHON__ = self, |
|
724 | 726 | ip_set_hook = self.set_hook, |
|
725 | 727 | jobs = self.jobs, |
|
726 | 728 | ipmagic = self.ipmagic, |
|
727 | 729 | ipalias = self.ipalias, |
|
728 | 730 | ipsystem = self.ipsystem, |
|
731 | ipconfig = self.ipconfig, | |
|
729 | 732 | _ip = self.api |
|
730 | 733 | ) |
|
731 | 734 | for biname,bival in builtins_new.items(): |
|
732 | 735 | try: |
|
733 | 736 | # store the orignal value so we can restore it |
|
734 | 737 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
735 | 738 | except KeyError: |
|
736 | 739 | # or mark that it wasn't defined, and we'll just delete it at |
|
737 | 740 | # cleanup |
|
738 | 741 | self.builtins_added[biname] = Undefined |
|
739 | 742 | __builtin__.__dict__[biname] = bival |
|
740 | 743 | |
|
741 | 744 | # Keep in the builtins a flag for when IPython is active. We set it |
|
742 | 745 | # with setdefault so that multiple nested IPythons don't clobber one |
|
743 | 746 | # another. Each will increase its value by one upon being activated, |
|
744 | 747 | # which also gives us a way to determine the nesting level. |
|
745 | 748 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
746 | 749 | |
|
747 | 750 | def clean_builtins(self): |
|
748 | 751 | """Remove any builtins which might have been added by add_builtins, or |
|
749 | 752 | restore overwritten ones to their previous values.""" |
|
750 | 753 | for biname,bival in self.builtins_added.items(): |
|
751 | 754 | if bival is Undefined: |
|
752 | 755 | del __builtin__.__dict__[biname] |
|
753 | 756 | else: |
|
754 | 757 | __builtin__.__dict__[biname] = bival |
|
755 | 758 | self.builtins_added.clear() |
|
756 | 759 | |
|
757 | 760 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
758 | 761 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
759 | 762 | |
|
760 | 763 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
761 | 764 | adding your function to one of these hooks, you can modify IPython's |
|
762 | 765 | behavior to call at runtime your own routines.""" |
|
763 | 766 | |
|
764 | 767 | # At some point in the future, this should validate the hook before it |
|
765 | 768 | # accepts it. Probably at least check that the hook takes the number |
|
766 | 769 | # of args it's supposed to. |
|
767 | 770 | |
|
768 | 771 | f = new.instancemethod(hook,self,self.__class__) |
|
769 | 772 | |
|
770 | 773 | # check if the hook is for strdispatcher first |
|
771 | 774 | if str_key is not None: |
|
772 | 775 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
773 | 776 | sdp.add_s(str_key, f, priority ) |
|
774 | 777 | self.strdispatchers[name] = sdp |
|
775 | 778 | return |
|
776 | 779 | if re_key is not None: |
|
777 | 780 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
778 | 781 | sdp.add_re(re.compile(re_key), f, priority ) |
|
779 | 782 | self.strdispatchers[name] = sdp |
|
780 | 783 | return |
|
781 | 784 | |
|
782 | 785 | dp = getattr(self.hooks, name, None) |
|
783 | 786 | if name not in IPython.hooks.__all__: |
|
784 | 787 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ ) |
|
785 | 788 | if not dp: |
|
786 | 789 | dp = IPython.hooks.CommandChainDispatcher() |
|
787 | 790 | |
|
788 | 791 | try: |
|
789 | 792 | dp.add(f,priority) |
|
790 | 793 | except AttributeError: |
|
791 | 794 | # it was not commandchain, plain old func - replace |
|
792 | 795 | dp = f |
|
793 | 796 | |
|
794 | 797 | setattr(self.hooks,name, dp) |
|
795 | 798 | |
|
796 | 799 | |
|
797 | 800 | #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
798 | 801 | |
|
799 | 802 | def set_crash_handler(self,crashHandler): |
|
800 | 803 | """Set the IPython crash handler. |
|
801 | 804 | |
|
802 | 805 | This must be a callable with a signature suitable for use as |
|
803 | 806 | sys.excepthook.""" |
|
804 | 807 | |
|
805 | 808 | # Install the given crash handler as the Python exception hook |
|
806 | 809 | sys.excepthook = crashHandler |
|
807 | 810 | |
|
808 | 811 | # The instance will store a pointer to this, so that runtime code |
|
809 | 812 | # (such as magics) can access it. This is because during the |
|
810 | 813 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
811 | 814 | # frameworks). |
|
812 | 815 | self.sys_excepthook = sys.excepthook |
|
813 | 816 | |
|
814 | 817 | |
|
815 | 818 | def set_custom_exc(self,exc_tuple,handler): |
|
816 | 819 | """set_custom_exc(exc_tuple,handler) |
|
817 | 820 | |
|
818 | 821 | Set a custom exception handler, which will be called if any of the |
|
819 | 822 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
820 | 823 | runcode() method. |
|
821 | 824 | |
|
822 | 825 | Inputs: |
|
823 | 826 | |
|
824 | 827 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
825 | 828 | handler for. It is very important that you use a tuple, and NOT A |
|
826 | 829 | LIST here, because of the way Python's except statement works. If |
|
827 | 830 | you only want to trap a single exception, use a singleton tuple: |
|
828 | 831 | |
|
829 | 832 | exc_tuple == (MyCustomException,) |
|
830 | 833 | |
|
831 | 834 | - handler: this must be defined as a function with the following |
|
832 | 835 | basic interface: def my_handler(self,etype,value,tb). |
|
833 | 836 | |
|
834 | 837 | This will be made into an instance method (via new.instancemethod) |
|
835 | 838 | of IPython itself, and it will be called if any of the exceptions |
|
836 | 839 | listed in the exc_tuple are caught. If the handler is None, an |
|
837 | 840 | internal basic one is used, which just prints basic info. |
|
838 | 841 | |
|
839 | 842 | WARNING: by putting in your own exception handler into IPython's main |
|
840 | 843 | execution loop, you run a very good chance of nasty crashes. This |
|
841 | 844 | facility should only be used if you really know what you are doing.""" |
|
842 | 845 | |
|
843 | 846 | assert type(exc_tuple)==type(()) , \ |
|
844 | 847 | "The custom exceptions must be given AS A TUPLE." |
|
845 | 848 | |
|
846 | 849 | def dummy_handler(self,etype,value,tb): |
|
847 | 850 | print '*** Simple custom exception handler ***' |
|
848 | 851 | print 'Exception type :',etype |
|
849 | 852 | print 'Exception value:',value |
|
850 | 853 | print 'Traceback :',tb |
|
851 | 854 | print 'Source code :','\n'.join(self.buffer) |
|
852 | 855 | |
|
853 | 856 | if handler is None: handler = dummy_handler |
|
854 | 857 | |
|
855 | 858 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
856 | 859 | self.custom_exceptions = exc_tuple |
|
857 | 860 | |
|
858 | 861 | def set_custom_completer(self,completer,pos=0): |
|
859 | 862 | """set_custom_completer(completer,pos=0) |
|
860 | 863 | |
|
861 | 864 | Adds a new custom completer function. |
|
862 | 865 | |
|
863 | 866 | The position argument (defaults to 0) is the index in the completers |
|
864 | 867 | list where you want the completer to be inserted.""" |
|
865 | 868 | |
|
866 | 869 | newcomp = new.instancemethod(completer,self.Completer, |
|
867 | 870 | self.Completer.__class__) |
|
868 | 871 | self.Completer.matchers.insert(pos,newcomp) |
|
869 | 872 | |
|
870 | 873 | def _get_call_pdb(self): |
|
871 | 874 | return self._call_pdb |
|
872 | 875 | |
|
873 | 876 | def _set_call_pdb(self,val): |
|
874 | 877 | |
|
875 | 878 | if val not in (0,1,False,True): |
|
876 | 879 | raise ValueError,'new call_pdb value must be boolean' |
|
877 | 880 | |
|
878 | 881 | # store value in instance |
|
879 | 882 | self._call_pdb = val |
|
880 | 883 | |
|
881 | 884 | # notify the actual exception handlers |
|
882 | 885 | self.InteractiveTB.call_pdb = val |
|
883 | 886 | if self.isthreaded: |
|
884 | 887 | try: |
|
885 | 888 | self.sys_excepthook.call_pdb = val |
|
886 | 889 | except: |
|
887 | 890 | warn('Failed to activate pdb for threaded exception handler') |
|
888 | 891 | |
|
889 | 892 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
890 | 893 | 'Control auto-activation of pdb at exceptions') |
|
891 | 894 | |
|
892 | 895 | |
|
893 | 896 | # These special functions get installed in the builtin namespace, to |
|
894 | 897 | # provide programmatic (pure python) access to magics, aliases and system |
|
895 | 898 | # calls. This is important for logging, user scripting, and more. |
|
896 | 899 | |
|
897 | 900 | # We are basically exposing, via normal python functions, the three |
|
898 | 901 | # mechanisms in which ipython offers special call modes (magics for |
|
899 | 902 | # internal control, aliases for direct system access via pre-selected |
|
900 | 903 | # names, and !cmd for calling arbitrary system commands). |
|
901 | 904 | |
|
902 | 905 | def ipmagic(self,arg_s): |
|
903 | 906 | """Call a magic function by name. |
|
904 | 907 | |
|
905 | 908 | Input: a string containing the name of the magic function to call and any |
|
906 | 909 | additional arguments to be passed to the magic. |
|
907 | 910 | |
|
908 | 911 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
909 | 912 | prompt: |
|
910 | 913 | |
|
911 | 914 | In[1]: %name -opt foo bar |
|
912 | 915 | |
|
913 | 916 | To call a magic without arguments, simply use ipmagic('name'). |
|
914 | 917 | |
|
915 | 918 | This provides a proper Python function to call IPython's magics in any |
|
916 | 919 | valid Python code you can type at the interpreter, including loops and |
|
917 | 920 | compound statements. It is added by IPython to the Python builtin |
|
918 | 921 | namespace upon initialization.""" |
|
919 | 922 | |
|
920 | 923 | args = arg_s.split(' ',1) |
|
921 | 924 | magic_name = args[0] |
|
922 | 925 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
923 | 926 | |
|
924 | 927 | try: |
|
925 | 928 | magic_args = args[1] |
|
926 | 929 | except IndexError: |
|
927 | 930 | magic_args = '' |
|
928 | 931 | fn = getattr(self,'magic_'+magic_name,None) |
|
929 | 932 | if fn is None: |
|
930 | 933 | error("Magic function `%s` not found." % magic_name) |
|
931 | 934 | else: |
|
932 | 935 | magic_args = self.var_expand(magic_args,1) |
|
933 | 936 | return fn(magic_args) |
|
934 | 937 | |
|
935 | 938 | def ipalias(self,arg_s): |
|
936 | 939 | """Call an alias by name. |
|
937 | 940 | |
|
938 | 941 | Input: a string containing the name of the alias to call and any |
|
939 | 942 | additional arguments to be passed to the magic. |
|
940 | 943 | |
|
941 | 944 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
942 | 945 | prompt: |
|
943 | 946 | |
|
944 | 947 | In[1]: name -opt foo bar |
|
945 | 948 | |
|
946 | 949 | To call an alias without arguments, simply use ipalias('name'). |
|
947 | 950 | |
|
948 | 951 | This provides a proper Python function to call IPython's aliases in any |
|
949 | 952 | valid Python code you can type at the interpreter, including loops and |
|
950 | 953 | compound statements. It is added by IPython to the Python builtin |
|
951 | 954 | namespace upon initialization.""" |
|
952 | 955 | |
|
953 | 956 | args = arg_s.split(' ',1) |
|
954 | 957 | alias_name = args[0] |
|
955 | 958 | try: |
|
956 | 959 | alias_args = args[1] |
|
957 | 960 | except IndexError: |
|
958 | 961 | alias_args = '' |
|
959 | 962 | if alias_name in self.alias_table: |
|
960 | 963 | self.call_alias(alias_name,alias_args) |
|
961 | 964 | else: |
|
962 | 965 | error("Alias `%s` not found." % alias_name) |
|
963 | 966 | |
|
967 | def ipconfig(self,key=None,value=None): | |
|
968 | """Manipulate the IPython config. | |
|
969 | ||
|
970 | This provides a python interface to | |
|
971 | If called with no arguments, it prints the internal IPython config | |
|
972 | ||
|
973 | Optional arguments: | |
|
974 | ||
|
975 | - key(None): if given, what key of the rc structure to return. | |
|
976 | ||
|
977 | - value(None): if given, set the key to this value.""" | |
|
978 | ||
|
979 | if key is None: | |
|
980 | page('Current configuration structure:\n'+ | |
|
981 | pformat(self.rc.dict())) | |
|
982 | else: | |
|
983 | if value is None: | |
|
984 | print '%s -> %s' % (key,self.rc[key]) | |
|
985 | else: | |
|
986 | if key not in self.rc: | |
|
987 | raise KeyError(str(key)) | |
|
988 | self.rc[key] = value | |
|
989 | ||
|
964 | 990 | def ipsystem(self,arg_s): |
|
965 | 991 | """Make a system call, using IPython.""" |
|
966 | 992 | |
|
967 | 993 | self.system(arg_s) |
|
968 | 994 | |
|
969 | 995 | def complete(self,text): |
|
970 | 996 | """Return a sorted list of all possible completions on text. |
|
971 | 997 | |
|
972 | 998 | Inputs: |
|
973 | 999 | |
|
974 | 1000 | - text: a string of text to be completed on. |
|
975 | 1001 | |
|
976 | 1002 | This is a wrapper around the completion mechanism, similar to what |
|
977 | 1003 | readline does at the command line when the TAB key is hit. By |
|
978 | 1004 | exposing it as a method, it can be used by other non-readline |
|
979 | 1005 | environments (such as GUIs) for text completion. |
|
980 | 1006 | |
|
981 | 1007 | Simple usage example: |
|
982 | 1008 | |
|
983 | 1009 | In [1]: x = 'hello' |
|
984 | 1010 | |
|
985 | 1011 | In [2]: __IP.complete('x.l') |
|
986 | 1012 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
987 | 1013 | |
|
988 | 1014 | complete = self.Completer.complete |
|
989 | 1015 | state = 0 |
|
990 | 1016 | # use a dict so we get unique keys, since ipyhton's multiple |
|
991 | 1017 | # completers can return duplicates. |
|
992 | 1018 | comps = {} |
|
993 | 1019 | while True: |
|
994 | 1020 | newcomp = complete(text,state) |
|
995 | 1021 | if newcomp is None: |
|
996 | 1022 | break |
|
997 | 1023 | comps[newcomp] = 1 |
|
998 | 1024 | state += 1 |
|
999 | 1025 | outcomps = comps.keys() |
|
1000 | 1026 | outcomps.sort() |
|
1001 | 1027 | return outcomps |
|
1002 | 1028 | |
|
1003 | 1029 | def set_completer_frame(self, frame=None): |
|
1004 | 1030 | if frame: |
|
1005 | 1031 | self.Completer.namespace = frame.f_locals |
|
1006 | 1032 | self.Completer.global_namespace = frame.f_globals |
|
1007 | 1033 | else: |
|
1008 | 1034 | self.Completer.namespace = self.user_ns |
|
1009 | 1035 | self.Completer.global_namespace = self.user_global_ns |
|
1010 | 1036 | |
|
1011 | 1037 | def init_auto_alias(self): |
|
1012 | 1038 | """Define some aliases automatically. |
|
1013 | 1039 | |
|
1014 | 1040 | These are ALL parameter-less aliases""" |
|
1015 | 1041 | |
|
1016 | 1042 | for alias,cmd in self.auto_alias: |
|
1017 | 1043 | self.alias_table[alias] = (0,cmd) |
|
1018 | 1044 | |
|
1019 | 1045 | def alias_table_validate(self,verbose=0): |
|
1020 | 1046 | """Update information about the alias table. |
|
1021 | 1047 | |
|
1022 | 1048 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1023 | 1049 | |
|
1024 | 1050 | no_alias = self.no_alias |
|
1025 | 1051 | for k in self.alias_table.keys(): |
|
1026 | 1052 | if k in no_alias: |
|
1027 | 1053 | del self.alias_table[k] |
|
1028 | 1054 | if verbose: |
|
1029 | 1055 | print ("Deleting alias <%s>, it's a Python " |
|
1030 | 1056 | "keyword or builtin." % k) |
|
1031 | 1057 | |
|
1032 | 1058 | def set_autoindent(self,value=None): |
|
1033 | 1059 | """Set the autoindent flag, checking for readline support. |
|
1034 | 1060 | |
|
1035 | 1061 | If called with no arguments, it acts as a toggle.""" |
|
1036 | 1062 | |
|
1037 | 1063 | if not self.has_readline: |
|
1038 | 1064 | if os.name == 'posix': |
|
1039 | 1065 | warn("The auto-indent feature requires the readline library") |
|
1040 | 1066 | self.autoindent = 0 |
|
1041 | 1067 | return |
|
1042 | 1068 | if value is None: |
|
1043 | 1069 | self.autoindent = not self.autoindent |
|
1044 | 1070 | else: |
|
1045 | 1071 | self.autoindent = value |
|
1046 | 1072 | |
|
1047 | 1073 | def rc_set_toggle(self,rc_field,value=None): |
|
1048 | 1074 | """Set or toggle a field in IPython's rc config. structure. |
|
1049 | 1075 | |
|
1050 | 1076 | If called with no arguments, it acts as a toggle. |
|
1051 | 1077 | |
|
1052 | 1078 | If called with a non-existent field, the resulting AttributeError |
|
1053 | 1079 | exception will propagate out.""" |
|
1054 | 1080 | |
|
1055 | 1081 | rc_val = getattr(self.rc,rc_field) |
|
1056 | 1082 | if value is None: |
|
1057 | 1083 | value = not rc_val |
|
1058 | 1084 | setattr(self.rc,rc_field,value) |
|
1059 | 1085 | |
|
1060 | 1086 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1061 | 1087 | """Install the user configuration directory. |
|
1062 | 1088 | |
|
1063 | 1089 | Can be called when running for the first time or to upgrade the user's |
|
1064 | 1090 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1065 | 1091 | and 'upgrade'.""" |
|
1066 | 1092 | |
|
1067 | 1093 | def wait(): |
|
1068 | 1094 | try: |
|
1069 | 1095 | raw_input("Please press <RETURN> to start IPython.") |
|
1070 | 1096 | except EOFError: |
|
1071 | 1097 | print >> Term.cout |
|
1072 | 1098 | print '*'*70 |
|
1073 | 1099 | |
|
1074 | 1100 | cwd = os.getcwd() # remember where we started |
|
1075 | 1101 | glb = glob.glob |
|
1076 | 1102 | print '*'*70 |
|
1077 | 1103 | if mode == 'install': |
|
1078 | 1104 | print \ |
|
1079 | 1105 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1080 | 1106 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1081 | 1107 | else: |
|
1082 | 1108 | print 'I am going to upgrade your configuration in:' |
|
1083 | 1109 | |
|
1084 | 1110 | print ipythondir |
|
1085 | 1111 | |
|
1086 | 1112 | rcdirend = os.path.join('IPython','UserConfig') |
|
1087 | 1113 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1088 | 1114 | try: |
|
1089 | 1115 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1090 | 1116 | except IOError: |
|
1091 | 1117 | warning = """ |
|
1092 | 1118 | Installation error. IPython's directory was not found. |
|
1093 | 1119 | |
|
1094 | 1120 | Check the following: |
|
1095 | 1121 | |
|
1096 | 1122 | The ipython/IPython directory should be in a directory belonging to your |
|
1097 | 1123 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1098 | 1124 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1099 | 1125 | |
|
1100 | 1126 | IPython will proceed with builtin defaults. |
|
1101 | 1127 | """ |
|
1102 | 1128 | warn(warning) |
|
1103 | 1129 | wait() |
|
1104 | 1130 | return |
|
1105 | 1131 | |
|
1106 | 1132 | if mode == 'install': |
|
1107 | 1133 | try: |
|
1108 | 1134 | shutil.copytree(rcdir,ipythondir) |
|
1109 | 1135 | os.chdir(ipythondir) |
|
1110 | 1136 | rc_files = glb("ipythonrc*") |
|
1111 | 1137 | for rc_file in rc_files: |
|
1112 | 1138 | os.rename(rc_file,rc_file+rc_suffix) |
|
1113 | 1139 | except: |
|
1114 | 1140 | warning = """ |
|
1115 | 1141 | |
|
1116 | 1142 | There was a problem with the installation: |
|
1117 | 1143 | %s |
|
1118 | 1144 | Try to correct it or contact the developers if you think it's a bug. |
|
1119 | 1145 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1120 | 1146 | warn(warning) |
|
1121 | 1147 | wait() |
|
1122 | 1148 | return |
|
1123 | 1149 | |
|
1124 | 1150 | elif mode == 'upgrade': |
|
1125 | 1151 | try: |
|
1126 | 1152 | os.chdir(ipythondir) |
|
1127 | 1153 | except: |
|
1128 | 1154 | print """ |
|
1129 | 1155 | Can not upgrade: changing to directory %s failed. Details: |
|
1130 | 1156 | %s |
|
1131 | 1157 | """ % (ipythondir,sys.exc_info()[1]) |
|
1132 | 1158 | wait() |
|
1133 | 1159 | return |
|
1134 | 1160 | else: |
|
1135 | 1161 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1136 | 1162 | for new_full_path in sources: |
|
1137 | 1163 | new_filename = os.path.basename(new_full_path) |
|
1138 | 1164 | if new_filename.startswith('ipythonrc'): |
|
1139 | 1165 | new_filename = new_filename + rc_suffix |
|
1140 | 1166 | # The config directory should only contain files, skip any |
|
1141 | 1167 | # directories which may be there (like CVS) |
|
1142 | 1168 | if os.path.isdir(new_full_path): |
|
1143 | 1169 | continue |
|
1144 | 1170 | if os.path.exists(new_filename): |
|
1145 | 1171 | old_file = new_filename+'.old' |
|
1146 | 1172 | if os.path.exists(old_file): |
|
1147 | 1173 | os.remove(old_file) |
|
1148 | 1174 | os.rename(new_filename,old_file) |
|
1149 | 1175 | shutil.copy(new_full_path,new_filename) |
|
1150 | 1176 | else: |
|
1151 | 1177 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1152 | 1178 | |
|
1153 | 1179 | # Fix line-endings to those native to each platform in the config |
|
1154 | 1180 | # directory. |
|
1155 | 1181 | try: |
|
1156 | 1182 | os.chdir(ipythondir) |
|
1157 | 1183 | except: |
|
1158 | 1184 | print """ |
|
1159 | 1185 | Problem: changing to directory %s failed. |
|
1160 | 1186 | Details: |
|
1161 | 1187 | %s |
|
1162 | 1188 | |
|
1163 | 1189 | Some configuration files may have incorrect line endings. This should not |
|
1164 | 1190 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1165 | 1191 | wait() |
|
1166 | 1192 | else: |
|
1167 | 1193 | for fname in glb('ipythonrc*'): |
|
1168 | 1194 | try: |
|
1169 | 1195 | native_line_ends(fname,backup=0) |
|
1170 | 1196 | except IOError: |
|
1171 | 1197 | pass |
|
1172 | 1198 | |
|
1173 | 1199 | if mode == 'install': |
|
1174 | 1200 | print """ |
|
1175 | 1201 | Successful installation! |
|
1176 | 1202 | |
|
1177 | 1203 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1178 | 1204 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1179 | 1205 | distribution) to make sure that your system environment is properly configured |
|
1180 | 1206 | to take advantage of IPython's features. |
|
1181 | 1207 | |
|
1182 | 1208 | Important note: the configuration system has changed! The old system is |
|
1183 | 1209 | still in place, but its setting may be partly overridden by the settings in |
|
1184 | 1210 | "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file |
|
1185 | 1211 | if some of the new settings bother you. |
|
1186 | 1212 | |
|
1187 | 1213 | """ |
|
1188 | 1214 | else: |
|
1189 | 1215 | print """ |
|
1190 | 1216 | Successful upgrade! |
|
1191 | 1217 | |
|
1192 | 1218 | All files in your directory: |
|
1193 | 1219 | %(ipythondir)s |
|
1194 | 1220 | which would have been overwritten by the upgrade were backed up with a .old |
|
1195 | 1221 | extension. If you had made particular customizations in those files you may |
|
1196 | 1222 | want to merge them back into the new files.""" % locals() |
|
1197 | 1223 | wait() |
|
1198 | 1224 | os.chdir(cwd) |
|
1199 | 1225 | # end user_setup() |
|
1200 | 1226 | |
|
1201 | 1227 | def atexit_operations(self): |
|
1202 | 1228 | """This will be executed at the time of exit. |
|
1203 | 1229 | |
|
1204 | 1230 | Saving of persistent data should be performed here. """ |
|
1205 | 1231 | |
|
1206 | 1232 | #print '*** IPython exit cleanup ***' # dbg |
|
1207 | 1233 | # input history |
|
1208 | 1234 | self.savehist() |
|
1209 | 1235 | |
|
1210 | 1236 | # Cleanup all tempfiles left around |
|
1211 | 1237 | for tfile in self.tempfiles: |
|
1212 | 1238 | try: |
|
1213 | 1239 | os.unlink(tfile) |
|
1214 | 1240 | except OSError: |
|
1215 | 1241 | pass |
|
1216 | 1242 | |
|
1217 | 1243 | # save the "persistent data" catch-all dictionary |
|
1218 | 1244 | self.hooks.shutdown_hook() |
|
1219 | 1245 | |
|
1220 | 1246 | def savehist(self): |
|
1221 | 1247 | """Save input history to a file (via readline library).""" |
|
1222 | 1248 | try: |
|
1223 | 1249 | self.readline.write_history_file(self.histfile) |
|
1224 | 1250 | except: |
|
1225 | 1251 | print 'Unable to save IPython command history to file: ' + \ |
|
1226 | 1252 | `self.histfile` |
|
1227 | 1253 | |
|
1228 | 1254 | def history_saving_wrapper(self, func): |
|
1229 | 1255 | """ Wrap func for readline history saving |
|
1230 | 1256 | |
|
1231 | 1257 | Convert func into callable that saves & restores |
|
1232 | 1258 | history around the call """ |
|
1233 | 1259 | |
|
1234 | 1260 | if not self.has_readline: |
|
1235 | 1261 | return func |
|
1236 | 1262 | |
|
1237 | 1263 | def wrapper(): |
|
1238 | 1264 | self.savehist() |
|
1239 | 1265 | try: |
|
1240 | 1266 | func() |
|
1241 | 1267 | finally: |
|
1242 | 1268 | readline.read_history_file(self.histfile) |
|
1243 | 1269 | return wrapper |
|
1244 | 1270 | |
|
1245 | 1271 | |
|
1246 | 1272 | def pre_readline(self): |
|
1247 | 1273 | """readline hook to be used at the start of each line. |
|
1248 | 1274 | |
|
1249 | 1275 | Currently it handles auto-indent only.""" |
|
1250 | 1276 | |
|
1251 | 1277 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1252 | 1278 | self.readline.insert_text(self.indent_current_str()) |
|
1253 | 1279 | |
|
1254 | 1280 | def init_readline(self): |
|
1255 | 1281 | """Command history completion/saving/reloading.""" |
|
1256 | 1282 | |
|
1257 | 1283 | import IPython.rlineimpl as readline |
|
1258 | 1284 | if not readline.have_readline: |
|
1259 | 1285 | self.has_readline = 0 |
|
1260 | 1286 | self.readline = None |
|
1261 | 1287 | # no point in bugging windows users with this every time: |
|
1262 | 1288 | warn('Readline services not available on this platform.') |
|
1263 | 1289 | else: |
|
1264 | 1290 | sys.modules['readline'] = readline |
|
1265 | 1291 | import atexit |
|
1266 | 1292 | from IPython.completer import IPCompleter |
|
1267 | 1293 | self.Completer = IPCompleter(self, |
|
1268 | 1294 | self.user_ns, |
|
1269 | 1295 | self.user_global_ns, |
|
1270 | 1296 | self.rc.readline_omit__names, |
|
1271 | 1297 | self.alias_table) |
|
1272 | 1298 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1273 | 1299 | self.strdispatchers['complete_command'] = sdisp |
|
1274 | 1300 | self.Completer.custom_completers = sdisp |
|
1275 | 1301 | # Platform-specific configuration |
|
1276 | 1302 | if os.name == 'nt': |
|
1277 | 1303 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1278 | 1304 | else: |
|
1279 | 1305 | self.readline_startup_hook = readline.set_startup_hook |
|
1280 | 1306 | |
|
1281 | 1307 | # Load user's initrc file (readline config) |
|
1282 | 1308 | inputrc_name = os.environ.get('INPUTRC') |
|
1283 | 1309 | if inputrc_name is None: |
|
1284 | 1310 | home_dir = get_home_dir() |
|
1285 | 1311 | if home_dir is not None: |
|
1286 | 1312 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1287 | 1313 | if os.path.isfile(inputrc_name): |
|
1288 | 1314 | try: |
|
1289 | 1315 | readline.read_init_file(inputrc_name) |
|
1290 | 1316 | except: |
|
1291 | 1317 | warn('Problems reading readline initialization file <%s>' |
|
1292 | 1318 | % inputrc_name) |
|
1293 | 1319 | |
|
1294 | 1320 | self.has_readline = 1 |
|
1295 | 1321 | self.readline = readline |
|
1296 | 1322 | # save this in sys so embedded copies can restore it properly |
|
1297 | 1323 | sys.ipcompleter = self.Completer.complete |
|
1298 | 1324 | readline.set_completer(self.Completer.complete) |
|
1299 | 1325 | |
|
1300 | 1326 | # Configure readline according to user's prefs |
|
1301 | 1327 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1302 | 1328 | readline.parse_and_bind(rlcommand) |
|
1303 | 1329 | |
|
1304 | 1330 | # remove some chars from the delimiters list |
|
1305 | 1331 | delims = readline.get_completer_delims() |
|
1306 | 1332 | delims = delims.translate(string._idmap, |
|
1307 | 1333 | self.rc.readline_remove_delims) |
|
1308 | 1334 | readline.set_completer_delims(delims) |
|
1309 | 1335 | # otherwise we end up with a monster history after a while: |
|
1310 | 1336 | readline.set_history_length(1000) |
|
1311 | 1337 | try: |
|
1312 | 1338 | #print '*** Reading readline history' # dbg |
|
1313 | 1339 | readline.read_history_file(self.histfile) |
|
1314 | 1340 | except IOError: |
|
1315 | 1341 | pass # It doesn't exist yet. |
|
1316 | 1342 | |
|
1317 | 1343 | atexit.register(self.atexit_operations) |
|
1318 | 1344 | del atexit |
|
1319 | 1345 | |
|
1320 | 1346 | # Configure auto-indent for all platforms |
|
1321 | 1347 | self.set_autoindent(self.rc.autoindent) |
|
1322 | 1348 | |
|
1323 | 1349 | def ask_yes_no(self,prompt,default=True): |
|
1324 | 1350 | if self.rc.quiet: |
|
1325 | 1351 | return True |
|
1326 | 1352 | return ask_yes_no(prompt,default) |
|
1327 | 1353 | |
|
1328 | 1354 | def _should_recompile(self,e): |
|
1329 | 1355 | """Utility routine for edit_syntax_error""" |
|
1330 | 1356 | |
|
1331 | 1357 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1332 | 1358 | '<console>','<BackgroundJob compilation>', |
|
1333 | 1359 | None): |
|
1334 | 1360 | |
|
1335 | 1361 | return False |
|
1336 | 1362 | try: |
|
1337 | 1363 | if (self.rc.autoedit_syntax and |
|
1338 | 1364 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1339 | 1365 | '[Y/n] ','y')): |
|
1340 | 1366 | return False |
|
1341 | 1367 | except EOFError: |
|
1342 | 1368 | return False |
|
1343 | 1369 | |
|
1344 | 1370 | def int0(x): |
|
1345 | 1371 | try: |
|
1346 | 1372 | return int(x) |
|
1347 | 1373 | except TypeError: |
|
1348 | 1374 | return 0 |
|
1349 | 1375 | # always pass integer line and offset values to editor hook |
|
1350 | 1376 | self.hooks.fix_error_editor(e.filename, |
|
1351 | 1377 | int0(e.lineno),int0(e.offset),e.msg) |
|
1352 | 1378 | return True |
|
1353 | 1379 | |
|
1354 | 1380 | def edit_syntax_error(self): |
|
1355 | 1381 | """The bottom half of the syntax error handler called in the main loop. |
|
1356 | 1382 | |
|
1357 | 1383 | Loop until syntax error is fixed or user cancels. |
|
1358 | 1384 | """ |
|
1359 | 1385 | |
|
1360 | 1386 | while self.SyntaxTB.last_syntax_error: |
|
1361 | 1387 | # copy and clear last_syntax_error |
|
1362 | 1388 | err = self.SyntaxTB.clear_err_state() |
|
1363 | 1389 | if not self._should_recompile(err): |
|
1364 | 1390 | return |
|
1365 | 1391 | try: |
|
1366 | 1392 | # may set last_syntax_error again if a SyntaxError is raised |
|
1367 | 1393 | self.safe_execfile(err.filename,self.user_ns) |
|
1368 | 1394 | except: |
|
1369 | 1395 | self.showtraceback() |
|
1370 | 1396 | else: |
|
1371 | 1397 | try: |
|
1372 | 1398 | f = file(err.filename) |
|
1373 | 1399 | try: |
|
1374 | 1400 | sys.displayhook(f.read()) |
|
1375 | 1401 | finally: |
|
1376 | 1402 | f.close() |
|
1377 | 1403 | except: |
|
1378 | 1404 | self.showtraceback() |
|
1379 | 1405 | |
|
1380 | 1406 | def showsyntaxerror(self, filename=None): |
|
1381 | 1407 | """Display the syntax error that just occurred. |
|
1382 | 1408 | |
|
1383 | 1409 | This doesn't display a stack trace because there isn't one. |
|
1384 | 1410 | |
|
1385 | 1411 | If a filename is given, it is stuffed in the exception instead |
|
1386 | 1412 | of what was there before (because Python's parser always uses |
|
1387 | 1413 | "<string>" when reading from a string). |
|
1388 | 1414 | """ |
|
1389 | 1415 | etype, value, last_traceback = sys.exc_info() |
|
1390 | 1416 | |
|
1391 | 1417 | # See note about these variables in showtraceback() below |
|
1392 | 1418 | sys.last_type = etype |
|
1393 | 1419 | sys.last_value = value |
|
1394 | 1420 | sys.last_traceback = last_traceback |
|
1395 | 1421 | |
|
1396 | 1422 | if filename and etype is SyntaxError: |
|
1397 | 1423 | # Work hard to stuff the correct filename in the exception |
|
1398 | 1424 | try: |
|
1399 | 1425 | msg, (dummy_filename, lineno, offset, line) = value |
|
1400 | 1426 | except: |
|
1401 | 1427 | # Not the format we expect; leave it alone |
|
1402 | 1428 | pass |
|
1403 | 1429 | else: |
|
1404 | 1430 | # Stuff in the right filename |
|
1405 | 1431 | try: |
|
1406 | 1432 | # Assume SyntaxError is a class exception |
|
1407 | 1433 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1408 | 1434 | except: |
|
1409 | 1435 | # If that failed, assume SyntaxError is a string |
|
1410 | 1436 | value = msg, (filename, lineno, offset, line) |
|
1411 | 1437 | self.SyntaxTB(etype,value,[]) |
|
1412 | 1438 | |
|
1413 | 1439 | def debugger(self): |
|
1414 | 1440 | """Call the pydb/pdb debugger.""" |
|
1415 | 1441 | |
|
1416 | 1442 | if not self.rc.pdb: |
|
1417 | 1443 | return |
|
1418 | 1444 | have_pydb = False |
|
1419 | 1445 | if sys.version[:3] >= '2.5': |
|
1420 | 1446 | try: |
|
1421 | 1447 | from pydb import pm |
|
1422 | 1448 | have_pydb = True |
|
1423 | 1449 | except ImportError: |
|
1424 | 1450 | pass |
|
1425 | 1451 | if not have_pydb: |
|
1426 | 1452 | from pdb import pm |
|
1427 | 1453 | self.history_saving_wrapper(pm)() |
|
1428 | 1454 | |
|
1429 | 1455 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1430 | 1456 | """Display the exception that just occurred. |
|
1431 | 1457 | |
|
1432 | 1458 | If nothing is known about the exception, this is the method which |
|
1433 | 1459 | should be used throughout the code for presenting user tracebacks, |
|
1434 | 1460 | rather than directly invoking the InteractiveTB object. |
|
1435 | 1461 | |
|
1436 | 1462 | A specific showsyntaxerror() also exists, but this method can take |
|
1437 | 1463 | care of calling it if needed, so unless you are explicitly catching a |
|
1438 | 1464 | SyntaxError exception, don't try to analyze the stack manually and |
|
1439 | 1465 | simply call this method.""" |
|
1440 | 1466 | |
|
1441 | 1467 | # Though this won't be called by syntax errors in the input line, |
|
1442 | 1468 | # there may be SyntaxError cases whith imported code. |
|
1443 | 1469 | if exc_tuple is None: |
|
1444 | 1470 | etype, value, tb = sys.exc_info() |
|
1445 | 1471 | else: |
|
1446 | 1472 | etype, value, tb = exc_tuple |
|
1447 | 1473 | if etype is SyntaxError: |
|
1448 | 1474 | self.showsyntaxerror(filename) |
|
1449 | 1475 | else: |
|
1450 | 1476 | # WARNING: these variables are somewhat deprecated and not |
|
1451 | 1477 | # necessarily safe to use in a threaded environment, but tools |
|
1452 | 1478 | # like pdb depend on their existence, so let's set them. If we |
|
1453 | 1479 | # find problems in the field, we'll need to revisit their use. |
|
1454 | 1480 | sys.last_type = etype |
|
1455 | 1481 | sys.last_value = value |
|
1456 | 1482 | sys.last_traceback = tb |
|
1457 | 1483 | |
|
1458 | 1484 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1459 | 1485 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1460 | 1486 | # pdb mucks up readline, fix it back |
|
1461 | 1487 | self.readline.set_completer(self.Completer.complete) |
|
1462 | 1488 | |
|
1463 | 1489 | def mainloop(self,banner=None): |
|
1464 | 1490 | """Creates the local namespace and starts the mainloop. |
|
1465 | 1491 | |
|
1466 | 1492 | If an optional banner argument is given, it will override the |
|
1467 | 1493 | internally created default banner.""" |
|
1468 | 1494 | |
|
1469 | 1495 | if self.rc.c: # Emulate Python's -c option |
|
1470 | 1496 | self.exec_init_cmd() |
|
1471 | 1497 | if banner is None: |
|
1472 | 1498 | if not self.rc.banner: |
|
1473 | 1499 | banner = '' |
|
1474 | 1500 | # banner is string? Use it directly! |
|
1475 | 1501 | elif isinstance(self.rc.banner,basestring): |
|
1476 | 1502 | banner = self.rc.banner |
|
1477 | 1503 | else: |
|
1478 | 1504 | banner = self.BANNER+self.banner2 |
|
1479 | 1505 | |
|
1480 | 1506 | self.interact(banner) |
|
1481 | 1507 | |
|
1482 | 1508 | def exec_init_cmd(self): |
|
1483 | 1509 | """Execute a command given at the command line. |
|
1484 | 1510 | |
|
1485 | 1511 | This emulates Python's -c option.""" |
|
1486 | 1512 | |
|
1487 | 1513 | #sys.argv = ['-c'] |
|
1488 | 1514 | self.push(self.rc.c) |
|
1489 | 1515 | |
|
1490 | 1516 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1491 | 1517 | """Embeds IPython into a running python program. |
|
1492 | 1518 | |
|
1493 | 1519 | Input: |
|
1494 | 1520 | |
|
1495 | 1521 | - header: An optional header message can be specified. |
|
1496 | 1522 | |
|
1497 | 1523 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1498 | 1524 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1499 | 1525 | program variables become visible but user-specific configuration |
|
1500 | 1526 | remains possible. |
|
1501 | 1527 | |
|
1502 | 1528 | - stack_depth: specifies how many levels in the stack to go to |
|
1503 | 1529 | looking for namespaces (when local_ns and global_ns are None). This |
|
1504 | 1530 | allows an intermediate caller to make sure that this function gets |
|
1505 | 1531 | the namespace from the intended level in the stack. By default (0) |
|
1506 | 1532 | it will get its locals and globals from the immediate caller. |
|
1507 | 1533 | |
|
1508 | 1534 | Warning: it's possible to use this in a program which is being run by |
|
1509 | 1535 | IPython itself (via %run), but some funny things will happen (a few |
|
1510 | 1536 | globals get overwritten). In the future this will be cleaned up, as |
|
1511 | 1537 | there is no fundamental reason why it can't work perfectly.""" |
|
1512 | 1538 | |
|
1513 | 1539 | # Get locals and globals from caller |
|
1514 | 1540 | if local_ns is None or global_ns is None: |
|
1515 | 1541 | call_frame = sys._getframe(stack_depth).f_back |
|
1516 | 1542 | |
|
1517 | 1543 | if local_ns is None: |
|
1518 | 1544 | local_ns = call_frame.f_locals |
|
1519 | 1545 | if global_ns is None: |
|
1520 | 1546 | global_ns = call_frame.f_globals |
|
1521 | 1547 | |
|
1522 | 1548 | # Update namespaces and fire up interpreter |
|
1523 | 1549 | |
|
1524 | 1550 | # The global one is easy, we can just throw it in |
|
1525 | 1551 | self.user_global_ns = global_ns |
|
1526 | 1552 | |
|
1527 | 1553 | # but the user/local one is tricky: ipython needs it to store internal |
|
1528 | 1554 | # data, but we also need the locals. We'll copy locals in the user |
|
1529 | 1555 | # one, but will track what got copied so we can delete them at exit. |
|
1530 | 1556 | # This is so that a later embedded call doesn't see locals from a |
|
1531 | 1557 | # previous call (which most likely existed in a separate scope). |
|
1532 | 1558 | local_varnames = local_ns.keys() |
|
1533 | 1559 | self.user_ns.update(local_ns) |
|
1534 | 1560 | |
|
1535 | 1561 | # Patch for global embedding to make sure that things don't overwrite |
|
1536 | 1562 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1537 | 1563 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1538 | 1564 | if local_ns is None and global_ns is None: |
|
1539 | 1565 | self.user_global_ns.update(__main__.__dict__) |
|
1540 | 1566 | |
|
1541 | 1567 | # make sure the tab-completer has the correct frame information, so it |
|
1542 | 1568 | # actually completes using the frame's locals/globals |
|
1543 | 1569 | self.set_completer_frame() |
|
1544 | 1570 | |
|
1545 | 1571 | # before activating the interactive mode, we need to make sure that |
|
1546 | 1572 | # all names in the builtin namespace needed by ipython point to |
|
1547 | 1573 | # ourselves, and not to other instances. |
|
1548 | 1574 | self.add_builtins() |
|
1549 | 1575 | |
|
1550 | 1576 | self.interact(header) |
|
1551 | 1577 | |
|
1552 | 1578 | # now, purge out the user namespace from anything we might have added |
|
1553 | 1579 | # from the caller's local namespace |
|
1554 | 1580 | delvar = self.user_ns.pop |
|
1555 | 1581 | for var in local_varnames: |
|
1556 | 1582 | delvar(var,None) |
|
1557 | 1583 | # and clean builtins we may have overridden |
|
1558 | 1584 | self.clean_builtins() |
|
1559 | 1585 | |
|
1560 | 1586 | def interact(self, banner=None): |
|
1561 | 1587 | """Closely emulate the interactive Python console. |
|
1562 | 1588 | |
|
1563 | 1589 | The optional banner argument specify the banner to print |
|
1564 | 1590 | before the first interaction; by default it prints a banner |
|
1565 | 1591 | similar to the one printed by the real Python interpreter, |
|
1566 | 1592 | followed by the current class name in parentheses (so as not |
|
1567 | 1593 | to confuse this with the real interpreter -- since it's so |
|
1568 | 1594 | close!). |
|
1569 | 1595 | |
|
1570 | 1596 | """ |
|
1571 | 1597 | |
|
1572 | 1598 | if self.exit_now: |
|
1573 | 1599 | # batch run -> do not interact |
|
1574 | 1600 | return |
|
1575 | 1601 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1576 | 1602 | if banner is None: |
|
1577 | 1603 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1578 | 1604 | (sys.version, sys.platform, cprt, |
|
1579 | 1605 | self.__class__.__name__)) |
|
1580 | 1606 | else: |
|
1581 | 1607 | self.write(banner) |
|
1582 | 1608 | |
|
1583 | 1609 | more = 0 |
|
1584 | 1610 | |
|
1585 | 1611 | # Mark activity in the builtins |
|
1586 | 1612 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1587 | 1613 | |
|
1588 | 1614 | # exit_now is set by a call to %Exit or %Quit |
|
1589 | 1615 | while not self.exit_now: |
|
1590 | 1616 | if more: |
|
1591 | 1617 | prompt = self.hooks.generate_prompt(True) |
|
1592 | 1618 | if self.autoindent: |
|
1593 | 1619 | self.readline_startup_hook(self.pre_readline) |
|
1594 | 1620 | else: |
|
1595 | 1621 | prompt = self.hooks.generate_prompt(False) |
|
1596 | 1622 | try: |
|
1597 | 1623 | line = self.raw_input(prompt,more) |
|
1598 | 1624 | if self.exit_now: |
|
1599 | 1625 | # quick exit on sys.std[in|out] close |
|
1600 | 1626 | break |
|
1601 | 1627 | if self.autoindent: |
|
1602 | 1628 | self.readline_startup_hook(None) |
|
1603 | 1629 | except KeyboardInterrupt: |
|
1604 | 1630 | self.write('\nKeyboardInterrupt\n') |
|
1605 | 1631 | self.resetbuffer() |
|
1606 | 1632 | # keep cache in sync with the prompt counter: |
|
1607 | 1633 | self.outputcache.prompt_count -= 1 |
|
1608 | 1634 | |
|
1609 | 1635 | if self.autoindent: |
|
1610 | 1636 | self.indent_current_nsp = 0 |
|
1611 | 1637 | more = 0 |
|
1612 | 1638 | except EOFError: |
|
1613 | 1639 | if self.autoindent: |
|
1614 | 1640 | self.readline_startup_hook(None) |
|
1615 | 1641 | self.write('\n') |
|
1616 | 1642 | self.exit() |
|
1617 | 1643 | except bdb.BdbQuit: |
|
1618 | 1644 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1619 | 1645 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1620 | 1646 | 'for IPython to properly format this particular exception.\n' |
|
1621 | 1647 | 'IPython will resume normal operation.') |
|
1622 | 1648 | except: |
|
1623 | 1649 | # exceptions here are VERY RARE, but they can be triggered |
|
1624 | 1650 | # asynchronously by signal handlers, for example. |
|
1625 | 1651 | self.showtraceback() |
|
1626 | 1652 | else: |
|
1627 | 1653 | more = self.push(line) |
|
1628 | 1654 | if (self.SyntaxTB.last_syntax_error and |
|
1629 | 1655 | self.rc.autoedit_syntax): |
|
1630 | 1656 | self.edit_syntax_error() |
|
1631 | 1657 | |
|
1632 | 1658 | # We are off again... |
|
1633 | 1659 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1634 | 1660 | |
|
1635 | 1661 | def excepthook(self, etype, value, tb): |
|
1636 | 1662 | """One more defense for GUI apps that call sys.excepthook. |
|
1637 | 1663 | |
|
1638 | 1664 | GUI frameworks like wxPython trap exceptions and call |
|
1639 | 1665 | sys.excepthook themselves. I guess this is a feature that |
|
1640 | 1666 | enables them to keep running after exceptions that would |
|
1641 | 1667 | otherwise kill their mainloop. This is a bother for IPython |
|
1642 | 1668 | which excepts to catch all of the program exceptions with a try: |
|
1643 | 1669 | except: statement. |
|
1644 | 1670 | |
|
1645 | 1671 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1646 | 1672 | any app directly invokes sys.excepthook, it will look to the user like |
|
1647 | 1673 | IPython crashed. In order to work around this, we can disable the |
|
1648 | 1674 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1649 | 1675 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1650 | 1676 | call sys.excepthook will generate a regular-looking exception from |
|
1651 | 1677 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1652 | 1678 | crashes. |
|
1653 | 1679 | |
|
1654 | 1680 | This hook should be used sparingly, only in places which are not likely |
|
1655 | 1681 | to be true IPython errors. |
|
1656 | 1682 | """ |
|
1657 | 1683 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1658 | 1684 | |
|
1659 | 1685 | def expand_aliases(self,fn,rest): |
|
1660 | 1686 | """ Expand multiple levels of aliases: |
|
1661 | 1687 | |
|
1662 | 1688 | if: |
|
1663 | 1689 | |
|
1664 | 1690 | alias foo bar /tmp |
|
1665 | 1691 | alias baz foo |
|
1666 | 1692 | |
|
1667 | 1693 | then: |
|
1668 | 1694 | |
|
1669 | 1695 | baz huhhahhei -> bar /tmp huhhahhei |
|
1670 | 1696 | |
|
1671 | 1697 | """ |
|
1672 | 1698 | line = fn + " " + rest |
|
1673 | 1699 | |
|
1674 | 1700 | done = Set() |
|
1675 | 1701 | while 1: |
|
1676 | 1702 | pre,fn,rest = self.split_user_input(line) |
|
1677 | 1703 | if fn in self.alias_table: |
|
1678 | 1704 | if fn in done: |
|
1679 | 1705 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
1680 | 1706 | return "" |
|
1681 | 1707 | done.add(fn) |
|
1682 | 1708 | |
|
1683 | 1709 | l2 = self.transform_alias(fn,rest) |
|
1684 | 1710 | # dir -> dir |
|
1685 | 1711 | # print "alias",line, "->",l2 #dbg |
|
1686 | 1712 | if l2 == line: |
|
1687 | 1713 | break |
|
1688 | 1714 | # ls -> ls -F should not recurse forever |
|
1689 | 1715 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
1690 | 1716 | line = l2 |
|
1691 | 1717 | break |
|
1692 | 1718 | |
|
1693 | 1719 | line=l2 |
|
1694 | 1720 | |
|
1695 | 1721 | |
|
1696 | 1722 | # print "al expand to",line #dbg |
|
1697 | 1723 | else: |
|
1698 | 1724 | break |
|
1699 | 1725 | |
|
1700 | 1726 | return line |
|
1701 | 1727 | |
|
1702 | 1728 | def transform_alias(self, alias,rest=''): |
|
1703 | 1729 | """ Transform alias to system command string. |
|
1704 | 1730 | """ |
|
1705 | 1731 | nargs,cmd = self.alias_table[alias] |
|
1706 | 1732 | if ' ' in cmd and os.path.isfile(cmd): |
|
1707 | 1733 | cmd = '"%s"' % cmd |
|
1708 | 1734 | |
|
1709 | 1735 | # Expand the %l special to be the user's input line |
|
1710 | 1736 | if cmd.find('%l') >= 0: |
|
1711 | 1737 | cmd = cmd.replace('%l',rest) |
|
1712 | 1738 | rest = '' |
|
1713 | 1739 | if nargs==0: |
|
1714 | 1740 | # Simple, argument-less aliases |
|
1715 | 1741 | cmd = '%s %s' % (cmd,rest) |
|
1716 | 1742 | else: |
|
1717 | 1743 | # Handle aliases with positional arguments |
|
1718 | 1744 | args = rest.split(None,nargs) |
|
1719 | 1745 | if len(args)< nargs: |
|
1720 | 1746 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1721 | 1747 | (alias,nargs,len(args))) |
|
1722 | 1748 | return None |
|
1723 | 1749 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1724 | 1750 | # Now call the macro, evaluating in the user's namespace |
|
1725 | 1751 | #print 'new command: <%r>' % cmd # dbg |
|
1726 | 1752 | return cmd |
|
1727 | 1753 | |
|
1728 | 1754 | def call_alias(self,alias,rest=''): |
|
1729 | 1755 | """Call an alias given its name and the rest of the line. |
|
1730 | 1756 | |
|
1731 | 1757 | This is only used to provide backwards compatibility for users of |
|
1732 | 1758 | ipalias(), use of which is not recommended for anymore.""" |
|
1733 | 1759 | |
|
1734 | 1760 | # Now call the macro, evaluating in the user's namespace |
|
1735 | 1761 | cmd = self.transform_alias(alias, rest) |
|
1736 | 1762 | try: |
|
1737 | 1763 | self.system(cmd) |
|
1738 | 1764 | except: |
|
1739 | 1765 | self.showtraceback() |
|
1740 | 1766 | |
|
1741 | 1767 | def indent_current_str(self): |
|
1742 | 1768 | """return the current level of indentation as a string""" |
|
1743 | 1769 | return self.indent_current_nsp * ' ' |
|
1744 | 1770 | |
|
1745 | 1771 | def autoindent_update(self,line): |
|
1746 | 1772 | """Keep track of the indent level.""" |
|
1747 | 1773 | |
|
1748 | 1774 | #debugx('line') |
|
1749 | 1775 | #debugx('self.indent_current_nsp') |
|
1750 | 1776 | if self.autoindent: |
|
1751 | 1777 | if line: |
|
1752 | 1778 | inisp = num_ini_spaces(line) |
|
1753 | 1779 | if inisp < self.indent_current_nsp: |
|
1754 | 1780 | self.indent_current_nsp = inisp |
|
1755 | 1781 | |
|
1756 | 1782 | if line[-1] == ':': |
|
1757 | 1783 | self.indent_current_nsp += 4 |
|
1758 | 1784 | elif dedent_re.match(line): |
|
1759 | 1785 | self.indent_current_nsp -= 4 |
|
1760 | 1786 | else: |
|
1761 | 1787 | self.indent_current_nsp = 0 |
|
1762 | 1788 | |
|
1763 | 1789 | def runlines(self,lines): |
|
1764 | 1790 | """Run a string of one or more lines of source. |
|
1765 | 1791 | |
|
1766 | 1792 | This method is capable of running a string containing multiple source |
|
1767 | 1793 | lines, as if they had been entered at the IPython prompt. Since it |
|
1768 | 1794 | exposes IPython's processing machinery, the given strings can contain |
|
1769 | 1795 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1770 | 1796 | |
|
1771 | 1797 | # We must start with a clean buffer, in case this is run from an |
|
1772 | 1798 | # interactive IPython session (via a magic, for example). |
|
1773 | 1799 | self.resetbuffer() |
|
1774 | 1800 | lines = lines.split('\n') |
|
1775 | 1801 | more = 0 |
|
1776 | 1802 | for line in lines: |
|
1777 | 1803 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1778 | 1804 | # NOT skip even a blank line if we are in a code block (more is |
|
1779 | 1805 | # true) |
|
1780 | 1806 | if line or more: |
|
1781 | 1807 | more = self.push(self.prefilter(line,more)) |
|
1782 | 1808 | # IPython's runsource returns None if there was an error |
|
1783 | 1809 | # compiling the code. This allows us to stop processing right |
|
1784 | 1810 | # away, so the user gets the error message at the right place. |
|
1785 | 1811 | if more is None: |
|
1786 | 1812 | break |
|
1787 | 1813 | # final newline in case the input didn't have it, so that the code |
|
1788 | 1814 | # actually does get executed |
|
1789 | 1815 | if more: |
|
1790 | 1816 | self.push('\n') |
|
1791 | 1817 | |
|
1792 | 1818 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1793 | 1819 | """Compile and run some source in the interpreter. |
|
1794 | 1820 | |
|
1795 | 1821 | Arguments are as for compile_command(). |
|
1796 | 1822 | |
|
1797 | 1823 | One several things can happen: |
|
1798 | 1824 | |
|
1799 | 1825 | 1) The input is incorrect; compile_command() raised an |
|
1800 | 1826 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1801 | 1827 | will be printed by calling the showsyntaxerror() method. |
|
1802 | 1828 | |
|
1803 | 1829 | 2) The input is incomplete, and more input is required; |
|
1804 | 1830 | compile_command() returned None. Nothing happens. |
|
1805 | 1831 | |
|
1806 | 1832 | 3) The input is complete; compile_command() returned a code |
|
1807 | 1833 | object. The code is executed by calling self.runcode() (which |
|
1808 | 1834 | also handles run-time exceptions, except for SystemExit). |
|
1809 | 1835 | |
|
1810 | 1836 | The return value is: |
|
1811 | 1837 | |
|
1812 | 1838 | - True in case 2 |
|
1813 | 1839 | |
|
1814 | 1840 | - False in the other cases, unless an exception is raised, where |
|
1815 | 1841 | None is returned instead. This can be used by external callers to |
|
1816 | 1842 | know whether to continue feeding input or not. |
|
1817 | 1843 | |
|
1818 | 1844 | The return value can be used to decide whether to use sys.ps1 or |
|
1819 | 1845 | sys.ps2 to prompt the next line.""" |
|
1820 | 1846 | |
|
1821 | 1847 | try: |
|
1822 | 1848 | code = self.compile(source,filename,symbol) |
|
1823 | 1849 | except (OverflowError, SyntaxError, ValueError): |
|
1824 | 1850 | # Case 1 |
|
1825 | 1851 | self.showsyntaxerror(filename) |
|
1826 | 1852 | return None |
|
1827 | 1853 | |
|
1828 | 1854 | if code is None: |
|
1829 | 1855 | # Case 2 |
|
1830 | 1856 | return True |
|
1831 | 1857 | |
|
1832 | 1858 | # Case 3 |
|
1833 | 1859 | # We store the code object so that threaded shells and |
|
1834 | 1860 | # custom exception handlers can access all this info if needed. |
|
1835 | 1861 | # The source corresponding to this can be obtained from the |
|
1836 | 1862 | # buffer attribute as '\n'.join(self.buffer). |
|
1837 | 1863 | self.code_to_run = code |
|
1838 | 1864 | # now actually execute the code object |
|
1839 | 1865 | if self.runcode(code) == 0: |
|
1840 | 1866 | return False |
|
1841 | 1867 | else: |
|
1842 | 1868 | return None |
|
1843 | 1869 | |
|
1844 | 1870 | def runcode(self,code_obj): |
|
1845 | 1871 | """Execute a code object. |
|
1846 | 1872 | |
|
1847 | 1873 | When an exception occurs, self.showtraceback() is called to display a |
|
1848 | 1874 | traceback. |
|
1849 | 1875 | |
|
1850 | 1876 | Return value: a flag indicating whether the code to be run completed |
|
1851 | 1877 | successfully: |
|
1852 | 1878 | |
|
1853 | 1879 | - 0: successful execution. |
|
1854 | 1880 | - 1: an error occurred. |
|
1855 | 1881 | """ |
|
1856 | 1882 | |
|
1857 | 1883 | # Set our own excepthook in case the user code tries to call it |
|
1858 | 1884 | # directly, so that the IPython crash handler doesn't get triggered |
|
1859 | 1885 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1860 | 1886 | |
|
1861 | 1887 | # we save the original sys.excepthook in the instance, in case config |
|
1862 | 1888 | # code (such as magics) needs access to it. |
|
1863 | 1889 | self.sys_excepthook = old_excepthook |
|
1864 | 1890 | outflag = 1 # happens in more places, so it's easier as default |
|
1865 | 1891 | try: |
|
1866 | 1892 | try: |
|
1867 | 1893 | # Embedded instances require separate global/local namespaces |
|
1868 | 1894 | # so they can see both the surrounding (local) namespace and |
|
1869 | 1895 | # the module-level globals when called inside another function. |
|
1870 | 1896 | if self.embedded: |
|
1871 | 1897 | exec code_obj in self.user_global_ns, self.user_ns |
|
1872 | 1898 | # Normal (non-embedded) instances should only have a single |
|
1873 | 1899 | # namespace for user code execution, otherwise functions won't |
|
1874 | 1900 | # see interactive top-level globals. |
|
1875 | 1901 | else: |
|
1876 | 1902 | exec code_obj in self.user_ns |
|
1877 | 1903 | finally: |
|
1878 | 1904 | # Reset our crash handler in place |
|
1879 | 1905 | sys.excepthook = old_excepthook |
|
1880 | 1906 | except SystemExit: |
|
1881 | 1907 | self.resetbuffer() |
|
1882 | 1908 | self.showtraceback() |
|
1883 | 1909 | warn("Type %exit or %quit to exit IPython " |
|
1884 | 1910 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1885 | 1911 | except self.custom_exceptions: |
|
1886 | 1912 | etype,value,tb = sys.exc_info() |
|
1887 | 1913 | self.CustomTB(etype,value,tb) |
|
1888 | 1914 | except: |
|
1889 | 1915 | self.showtraceback() |
|
1890 | 1916 | else: |
|
1891 | 1917 | outflag = 0 |
|
1892 | 1918 | if softspace(sys.stdout, 0): |
|
1893 | 1919 | |
|
1894 | 1920 | # Flush out code object which has been run (and source) |
|
1895 | 1921 | self.code_to_run = None |
|
1896 | 1922 | return outflag |
|
1897 | 1923 | |
|
1898 | 1924 | def push(self, line): |
|
1899 | 1925 | """Push a line to the interpreter. |
|
1900 | 1926 | |
|
1901 | 1927 | The line should not have a trailing newline; it may have |
|
1902 | 1928 | internal newlines. The line is appended to a buffer and the |
|
1903 | 1929 | interpreter's runsource() method is called with the |
|
1904 | 1930 | concatenated contents of the buffer as source. If this |
|
1905 | 1931 | indicates that the command was executed or invalid, the buffer |
|
1906 | 1932 | is reset; otherwise, the command is incomplete, and the buffer |
|
1907 | 1933 | is left as it was after the line was appended. The return |
|
1908 | 1934 | value is 1 if more input is required, 0 if the line was dealt |
|
1909 | 1935 | with in some way (this is the same as runsource()). |
|
1910 | 1936 | """ |
|
1911 | 1937 | |
|
1912 | 1938 | # autoindent management should be done here, and not in the |
|
1913 | 1939 | # interactive loop, since that one is only seen by keyboard input. We |
|
1914 | 1940 | # need this done correctly even for code run via runlines (which uses |
|
1915 | 1941 | # push). |
|
1916 | 1942 | |
|
1917 | 1943 | #print 'push line: <%s>' % line # dbg |
|
1918 | 1944 | for subline in line.splitlines(): |
|
1919 | 1945 | self.autoindent_update(subline) |
|
1920 | 1946 | self.buffer.append(line) |
|
1921 | 1947 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1922 | 1948 | if not more: |
|
1923 | 1949 | self.resetbuffer() |
|
1924 | 1950 | return more |
|
1925 | 1951 | |
|
1926 | 1952 | def resetbuffer(self): |
|
1927 | 1953 | """Reset the input buffer.""" |
|
1928 | 1954 | self.buffer[:] = [] |
|
1929 | 1955 | |
|
1930 | 1956 | def raw_input(self,prompt='',continue_prompt=False): |
|
1931 | 1957 | """Write a prompt and read a line. |
|
1932 | 1958 | |
|
1933 | 1959 | The returned line does not include the trailing newline. |
|
1934 | 1960 | When the user enters the EOF key sequence, EOFError is raised. |
|
1935 | 1961 | |
|
1936 | 1962 | Optional inputs: |
|
1937 | 1963 | |
|
1938 | 1964 | - prompt(''): a string to be printed to prompt the user. |
|
1939 | 1965 | |
|
1940 | 1966 | - continue_prompt(False): whether this line is the first one or a |
|
1941 | 1967 | continuation in a sequence of inputs. |
|
1942 | 1968 | """ |
|
1943 | 1969 | |
|
1944 | 1970 | try: |
|
1945 | 1971 | line = raw_input_original(prompt) |
|
1946 | 1972 | except ValueError: |
|
1947 | 1973 | warn("\n********\nYou or a %run:ed script called sys.stdin.close() or sys.stdout.close()!\nExiting IPython!") |
|
1948 | 1974 | self.exit_now = True |
|
1949 | 1975 | return "" |
|
1950 | 1976 | |
|
1951 | 1977 | |
|
1952 | 1978 | # Try to be reasonably smart about not re-indenting pasted input more |
|
1953 | 1979 | # than necessary. We do this by trimming out the auto-indent initial |
|
1954 | 1980 | # spaces, if the user's actual input started itself with whitespace. |
|
1955 | 1981 | #debugx('self.buffer[-1]') |
|
1956 | 1982 | |
|
1957 | 1983 | if self.autoindent: |
|
1958 | 1984 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
1959 | 1985 | line = line[self.indent_current_nsp:] |
|
1960 | 1986 | self.indent_current_nsp = 0 |
|
1961 | 1987 | |
|
1962 | 1988 | # store the unfiltered input before the user has any chance to modify |
|
1963 | 1989 | # it. |
|
1964 | 1990 | if line.strip(): |
|
1965 | 1991 | if continue_prompt: |
|
1966 | 1992 | self.input_hist_raw[-1] += '%s\n' % line |
|
1967 | 1993 | if self.has_readline: # and some config option is set? |
|
1968 | 1994 | try: |
|
1969 | 1995 | histlen = self.readline.get_current_history_length() |
|
1970 | 1996 | newhist = self.input_hist_raw[-1].rstrip() |
|
1971 | 1997 | self.readline.remove_history_item(histlen-1) |
|
1972 | 1998 | self.readline.replace_history_item(histlen-2,newhist) |
|
1973 | 1999 | except AttributeError: |
|
1974 | 2000 | pass # re{move,place}_history_item are new in 2.4. |
|
1975 | 2001 | else: |
|
1976 | 2002 | self.input_hist_raw.append('%s\n' % line) |
|
1977 | 2003 | |
|
1978 | 2004 | try: |
|
1979 | 2005 | lineout = self.prefilter(line,continue_prompt) |
|
1980 | 2006 | except: |
|
1981 | 2007 | # blanket except, in case a user-defined prefilter crashes, so it |
|
1982 | 2008 | # can't take all of ipython with it. |
|
1983 | 2009 | self.showtraceback() |
|
1984 | 2010 | return '' |
|
1985 | 2011 | else: |
|
1986 | 2012 | return lineout |
|
1987 | 2013 | |
|
1988 | 2014 | def split_user_input(self,line): |
|
1989 | 2015 | """Split user input into pre-char, function part and rest.""" |
|
1990 | 2016 | |
|
1991 | 2017 | lsplit = self.line_split.match(line) |
|
1992 | 2018 | if lsplit is None: # no regexp match returns None |
|
1993 | 2019 | try: |
|
1994 | 2020 | iFun,theRest = line.split(None,1) |
|
1995 | 2021 | except ValueError: |
|
1996 | 2022 | iFun,theRest = line,'' |
|
1997 | 2023 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1998 | 2024 | else: |
|
1999 | 2025 | pre,iFun,theRest = lsplit.groups() |
|
2000 | 2026 | |
|
2001 | 2027 | #print 'line:<%s>' % line # dbg |
|
2002 | 2028 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
2003 | 2029 | return pre,iFun.strip(),theRest |
|
2004 | 2030 | |
|
2005 | 2031 | def _prefilter(self, line, continue_prompt): |
|
2006 | 2032 | """Calls different preprocessors, depending on the form of line.""" |
|
2007 | 2033 | |
|
2008 | 2034 | # All handlers *must* return a value, even if it's blank (''). |
|
2009 | 2035 | |
|
2010 | 2036 | # Lines are NOT logged here. Handlers should process the line as |
|
2011 | 2037 | # needed, update the cache AND log it (so that the input cache array |
|
2012 | 2038 | # stays synced). |
|
2013 | 2039 | |
|
2014 | 2040 | # This function is _very_ delicate, and since it's also the one which |
|
2015 | 2041 | # determines IPython's response to user input, it must be as efficient |
|
2016 | 2042 | # as possible. For this reason it has _many_ returns in it, trying |
|
2017 | 2043 | # always to exit as quickly as it can figure out what it needs to do. |
|
2018 | 2044 | |
|
2019 | 2045 | # This function is the main responsible for maintaining IPython's |
|
2020 | 2046 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
2021 | 2047 | # making changes to anything here. |
|
2022 | 2048 | |
|
2023 | 2049 | #..................................................................... |
|
2024 | 2050 | # Code begins |
|
2025 | 2051 | |
|
2026 | 2052 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2027 | 2053 | |
|
2028 | 2054 | # save the line away in case we crash, so the post-mortem handler can |
|
2029 | 2055 | # record it |
|
2030 | 2056 | self._last_input_line = line |
|
2031 | 2057 | |
|
2032 | 2058 | #print '***line: <%s>' % line # dbg |
|
2033 | 2059 | |
|
2034 | 2060 | # the input history needs to track even empty lines |
|
2035 | 2061 | stripped = line.strip() |
|
2036 | 2062 | |
|
2037 | 2063 | if not stripped: |
|
2038 | 2064 | if not continue_prompt: |
|
2039 | 2065 | self.outputcache.prompt_count -= 1 |
|
2040 | 2066 | return self.handle_normal(line,continue_prompt) |
|
2041 | 2067 | #return self.handle_normal('',continue_prompt) |
|
2042 | 2068 | |
|
2043 | 2069 | # print '***cont',continue_prompt # dbg |
|
2044 | 2070 | # special handlers are only allowed for single line statements |
|
2045 | 2071 | if continue_prompt and not self.rc.multi_line_specials: |
|
2046 | 2072 | return self.handle_normal(line,continue_prompt) |
|
2047 | 2073 | |
|
2048 | 2074 | |
|
2049 | 2075 | # For the rest, we need the structure of the input |
|
2050 | 2076 | pre,iFun,theRest = self.split_user_input(line) |
|
2051 | 2077 | |
|
2052 | 2078 | # See whether any pre-existing handler can take care of it |
|
2053 | 2079 | |
|
2054 | 2080 | rewritten = self.hooks.input_prefilter(stripped) |
|
2055 | 2081 | if rewritten != stripped: # ok, some prefilter did something |
|
2056 | 2082 | rewritten = pre + rewritten # add indentation |
|
2057 | 2083 | return self.handle_normal(rewritten) |
|
2058 | 2084 | |
|
2059 | 2085 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2060 | 2086 | |
|
2061 | 2087 | # First check for explicit escapes in the last/first character |
|
2062 | 2088 | handler = None |
|
2063 | 2089 | if line[-1] == self.ESC_HELP: |
|
2064 | 2090 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
2065 | 2091 | if handler is None: |
|
2066 | 2092 | # look at the first character of iFun, NOT of line, so we skip |
|
2067 | 2093 | # leading whitespace in multiline input |
|
2068 | 2094 | handler = self.esc_handlers.get(iFun[0:1]) |
|
2069 | 2095 | if handler is not None: |
|
2070 | 2096 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
2071 | 2097 | # Emacs ipython-mode tags certain input lines |
|
2072 | 2098 | if line.endswith('# PYTHON-MODE'): |
|
2073 | 2099 | return self.handle_emacs(line,continue_prompt) |
|
2074 | 2100 | |
|
2075 | 2101 | # Next, check if we can automatically execute this thing |
|
2076 | 2102 | |
|
2077 | 2103 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
2078 | 2104 | if continue_prompt and self.rc.multi_line_specials and \ |
|
2079 | 2105 | iFun.startswith(self.ESC_SHELL): |
|
2080 | 2106 | return self.handle_shell_escape(line,continue_prompt, |
|
2081 | 2107 | pre=pre,iFun=iFun, |
|
2082 | 2108 | theRest=theRest) |
|
2083 | 2109 | |
|
2084 | 2110 | # Let's try to find if the input line is a magic fn |
|
2085 | 2111 | oinfo = None |
|
2086 | 2112 | if hasattr(self,'magic_'+iFun): |
|
2087 | 2113 | # WARNING: _ofind uses getattr(), so it can consume generators and |
|
2088 | 2114 | # cause other side effects. |
|
2089 | 2115 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
2090 | 2116 | if oinfo['ismagic']: |
|
2091 | 2117 | # Be careful not to call magics when a variable assignment is |
|
2092 | 2118 | # being made (ls='hi', for example) |
|
2093 | 2119 | if self.rc.automagic and \ |
|
2094 | 2120 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
2095 | 2121 | (self.rc.multi_line_specials or not continue_prompt): |
|
2096 | 2122 | return self.handle_magic(line,continue_prompt, |
|
2097 | 2123 | pre,iFun,theRest) |
|
2098 | 2124 | else: |
|
2099 | 2125 | return self.handle_normal(line,continue_prompt) |
|
2100 | 2126 | |
|
2101 | 2127 | # If the rest of the line begins with an (in)equality, assginment or |
|
2102 | 2128 | # function call, we should not call _ofind but simply execute it. |
|
2103 | 2129 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
2104 | 2130 | # |
|
2105 | 2131 | # It also allows users to assign to either alias or magic names true |
|
2106 | 2132 | # python variables (the magic/alias systems always take second seat to |
|
2107 | 2133 | # true python code). |
|
2108 | 2134 | if theRest and theRest[0] in '!=()': |
|
2109 | 2135 | return self.handle_normal(line,continue_prompt) |
|
2110 | 2136 | |
|
2111 | 2137 | if oinfo is None: |
|
2112 | 2138 | # let's try to ensure that _oinfo is ONLY called when autocall is |
|
2113 | 2139 | # on. Since it has inevitable potential side effects, at least |
|
2114 | 2140 | # having autocall off should be a guarantee to the user that no |
|
2115 | 2141 | # weird things will happen. |
|
2116 | 2142 | |
|
2117 | 2143 | if self.rc.autocall: |
|
2118 | 2144 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
2119 | 2145 | else: |
|
2120 | 2146 | # in this case, all that's left is either an alias or |
|
2121 | 2147 | # processing the line normally. |
|
2122 | 2148 | if iFun in self.alias_table: |
|
2123 | 2149 | # if autocall is off, by not running _ofind we won't know |
|
2124 | 2150 | # whether the given name may also exist in one of the |
|
2125 | 2151 | # user's namespace. At this point, it's best to do a |
|
2126 | 2152 | # quick check just to be sure that we don't let aliases |
|
2127 | 2153 | # shadow variables. |
|
2128 | 2154 | head = iFun.split('.',1)[0] |
|
2129 | 2155 | if head in self.user_ns or head in self.internal_ns \ |
|
2130 | 2156 | or head in __builtin__.__dict__: |
|
2131 | 2157 | return self.handle_normal(line,continue_prompt) |
|
2132 | 2158 | else: |
|
2133 | 2159 | return self.handle_alias(line,continue_prompt, |
|
2134 | 2160 | pre,iFun,theRest) |
|
2135 | 2161 | |
|
2136 | 2162 | else: |
|
2137 | 2163 | return self.handle_normal(line,continue_prompt) |
|
2138 | 2164 | |
|
2139 | 2165 | if not oinfo['found']: |
|
2140 | 2166 | return self.handle_normal(line,continue_prompt) |
|
2141 | 2167 | else: |
|
2142 | 2168 | #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2143 | 2169 | if oinfo['isalias']: |
|
2144 | 2170 | return self.handle_alias(line,continue_prompt, |
|
2145 | 2171 | pre,iFun,theRest) |
|
2146 | 2172 | |
|
2147 | 2173 | if (self.rc.autocall |
|
2148 | 2174 | and |
|
2149 | 2175 | ( |
|
2150 | 2176 | #only consider exclusion re if not "," or ";" autoquoting |
|
2151 | 2177 | (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2 |
|
2152 | 2178 | or pre == self.ESC_PAREN) or |
|
2153 | 2179 | (not self.re_exclude_auto.match(theRest))) |
|
2154 | 2180 | and |
|
2155 | 2181 | self.re_fun_name.match(iFun) and |
|
2156 | 2182 | callable(oinfo['obj'])) : |
|
2157 | 2183 | #print 'going auto' # dbg |
|
2158 | 2184 | return self.handle_auto(line,continue_prompt, |
|
2159 | 2185 | pre,iFun,theRest,oinfo['obj']) |
|
2160 | 2186 | else: |
|
2161 | 2187 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
2162 | 2188 | return self.handle_normal(line,continue_prompt) |
|
2163 | 2189 | |
|
2164 | 2190 | # If we get here, we have a normal Python line. Log and return. |
|
2165 | 2191 | return self.handle_normal(line,continue_prompt) |
|
2166 | 2192 | |
|
2167 | 2193 | def _prefilter_dumb(self, line, continue_prompt): |
|
2168 | 2194 | """simple prefilter function, for debugging""" |
|
2169 | 2195 | return self.handle_normal(line,continue_prompt) |
|
2170 | 2196 | |
|
2171 | 2197 | |
|
2172 | 2198 | def multiline_prefilter(self, line, continue_prompt): |
|
2173 | 2199 | """ Run _prefilter for each line of input |
|
2174 | 2200 | |
|
2175 | 2201 | Covers cases where there are multiple lines in the user entry, |
|
2176 | 2202 | which is the case when the user goes back to a multiline history |
|
2177 | 2203 | entry and presses enter. |
|
2178 | 2204 | |
|
2179 | 2205 | """ |
|
2180 | 2206 | out = [] |
|
2181 | 2207 | for l in line.rstrip('\n').split('\n'): |
|
2182 | 2208 | out.append(self._prefilter(l, continue_prompt)) |
|
2183 | 2209 | return '\n'.join(out) |
|
2184 | 2210 | |
|
2185 | 2211 | # Set the default prefilter() function (this can be user-overridden) |
|
2186 | 2212 | prefilter = multiline_prefilter |
|
2187 | 2213 | |
|
2188 | 2214 | def handle_normal(self,line,continue_prompt=None, |
|
2189 | 2215 | pre=None,iFun=None,theRest=None): |
|
2190 | 2216 | """Handle normal input lines. Use as a template for handlers.""" |
|
2191 | 2217 | |
|
2192 | 2218 | # With autoindent on, we need some way to exit the input loop, and I |
|
2193 | 2219 | # don't want to force the user to have to backspace all the way to |
|
2194 | 2220 | # clear the line. The rule will be in this case, that either two |
|
2195 | 2221 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2196 | 2222 | # of a size different to the indent level, will exit the input loop. |
|
2197 | 2223 | |
|
2198 | 2224 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2199 | 2225 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2200 | 2226 | (self.buffer[-1]).isspace() )): |
|
2201 | 2227 | line = '' |
|
2202 | 2228 | |
|
2203 | 2229 | self.log(line,line,continue_prompt) |
|
2204 | 2230 | return line |
|
2205 | 2231 | |
|
2206 | 2232 | def handle_alias(self,line,continue_prompt=None, |
|
2207 | 2233 | pre=None,iFun=None,theRest=None): |
|
2208 | 2234 | """Handle alias input lines. """ |
|
2209 | 2235 | |
|
2210 | 2236 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2211 | 2237 | # aliases won't work in indented sections. |
|
2212 | 2238 | transformed = self.expand_aliases(iFun, theRest) |
|
2213 | 2239 | line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed )) |
|
2214 | 2240 | self.log(line,line_out,continue_prompt) |
|
2215 | 2241 | #print 'line out:',line_out # dbg |
|
2216 | 2242 | return line_out |
|
2217 | 2243 | |
|
2218 | 2244 | def handle_shell_escape(self, line, continue_prompt=None, |
|
2219 | 2245 | pre=None,iFun=None,theRest=None): |
|
2220 | 2246 | """Execute the line in a shell, empty return value""" |
|
2221 | 2247 | |
|
2222 | 2248 | #print 'line in :', `line` # dbg |
|
2223 | 2249 | # Example of a special handler. Others follow a similar pattern. |
|
2224 | 2250 | if line.lstrip().startswith('!!'): |
|
2225 | 2251 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
2226 | 2252 | # the actual command to be executed, so handle_magic can work |
|
2227 | 2253 | # correctly |
|
2228 | 2254 | theRest = '%s %s' % (iFun[2:],theRest) |
|
2229 | 2255 | iFun = 'sx' |
|
2230 | 2256 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC, |
|
2231 | 2257 | line.lstrip()[2:]), |
|
2232 | 2258 | continue_prompt,pre,iFun,theRest) |
|
2233 | 2259 | else: |
|
2234 | 2260 | cmd=line.lstrip().lstrip('!') |
|
2235 | 2261 | line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd)) |
|
2236 | 2262 | # update cache/log and return |
|
2237 | 2263 | self.log(line,line_out,continue_prompt) |
|
2238 | 2264 | return line_out |
|
2239 | 2265 | |
|
2240 | 2266 | def handle_magic(self, line, continue_prompt=None, |
|
2241 | 2267 | pre=None,iFun=None,theRest=None): |
|
2242 | 2268 | """Execute magic functions.""" |
|
2243 | 2269 | |
|
2244 | 2270 | |
|
2245 | 2271 | cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest)) |
|
2246 | 2272 | self.log(line,cmd,continue_prompt) |
|
2247 | 2273 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2248 | 2274 | return cmd |
|
2249 | 2275 | |
|
2250 | 2276 | def handle_auto(self, line, continue_prompt=None, |
|
2251 | 2277 | pre=None,iFun=None,theRest=None,obj=None): |
|
2252 | 2278 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2253 | 2279 | |
|
2254 | 2280 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2255 | 2281 | |
|
2256 | 2282 | # This should only be active for single-line input! |
|
2257 | 2283 | if continue_prompt: |
|
2258 | 2284 | self.log(line,line,continue_prompt) |
|
2259 | 2285 | return line |
|
2260 | 2286 | |
|
2261 | 2287 | auto_rewrite = True |
|
2262 | 2288 | |
|
2263 | 2289 | if pre == self.ESC_QUOTE: |
|
2264 | 2290 | # Auto-quote splitting on whitespace |
|
2265 | 2291 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2266 | 2292 | elif pre == self.ESC_QUOTE2: |
|
2267 | 2293 | # Auto-quote whole string |
|
2268 | 2294 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2269 | 2295 | elif pre == self.ESC_PAREN: |
|
2270 | 2296 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2271 | 2297 | else: |
|
2272 | 2298 | # Auto-paren. |
|
2273 | 2299 | # We only apply it to argument-less calls if the autocall |
|
2274 | 2300 | # parameter is set to 2. We only need to check that autocall is < |
|
2275 | 2301 | # 2, since this function isn't called unless it's at least 1. |
|
2276 | 2302 | if not theRest and (self.rc.autocall < 2): |
|
2277 | 2303 | newcmd = '%s %s' % (iFun,theRest) |
|
2278 | 2304 | auto_rewrite = False |
|
2279 | 2305 | else: |
|
2280 | 2306 | if theRest.startswith('['): |
|
2281 | 2307 | if hasattr(obj,'__getitem__'): |
|
2282 | 2308 | # Don't autocall in this case: item access for an object |
|
2283 | 2309 | # which is BOTH callable and implements __getitem__. |
|
2284 | 2310 | newcmd = '%s %s' % (iFun,theRest) |
|
2285 | 2311 | auto_rewrite = False |
|
2286 | 2312 | else: |
|
2287 | 2313 | # if the object doesn't support [] access, go ahead and |
|
2288 | 2314 | # autocall |
|
2289 | 2315 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2290 | 2316 | elif theRest.endswith(';'): |
|
2291 | 2317 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2292 | 2318 | else: |
|
2293 | 2319 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2294 | 2320 | |
|
2295 | 2321 | if auto_rewrite: |
|
2296 | 2322 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2297 | 2323 | # log what is now valid Python, not the actual user input (without the |
|
2298 | 2324 | # final newline) |
|
2299 | 2325 | self.log(line,newcmd,continue_prompt) |
|
2300 | 2326 | return newcmd |
|
2301 | 2327 | |
|
2302 | 2328 | def handle_help(self, line, continue_prompt=None, |
|
2303 | 2329 | pre=None,iFun=None,theRest=None): |
|
2304 | 2330 | """Try to get some help for the object. |
|
2305 | 2331 | |
|
2306 | 2332 | obj? or ?obj -> basic information. |
|
2307 | 2333 | obj?? or ??obj -> more details. |
|
2308 | 2334 | """ |
|
2309 | 2335 | |
|
2310 | 2336 | # We need to make sure that we don't process lines which would be |
|
2311 | 2337 | # otherwise valid python, such as "x=1 # what?" |
|
2312 | 2338 | try: |
|
2313 | 2339 | codeop.compile_command(line) |
|
2314 | 2340 | except SyntaxError: |
|
2315 | 2341 | # We should only handle as help stuff which is NOT valid syntax |
|
2316 | 2342 | if line[0]==self.ESC_HELP: |
|
2317 | 2343 | line = line[1:] |
|
2318 | 2344 | elif line[-1]==self.ESC_HELP: |
|
2319 | 2345 | line = line[:-1] |
|
2320 | 2346 | self.log(line,'#?'+line,continue_prompt) |
|
2321 | 2347 | if line: |
|
2322 | 2348 | self.magic_pinfo(line) |
|
2323 | 2349 | else: |
|
2324 | 2350 | page(self.usage,screen_lines=self.rc.screen_length) |
|
2325 | 2351 | return '' # Empty string is needed here! |
|
2326 | 2352 | except: |
|
2327 | 2353 | # Pass any other exceptions through to the normal handler |
|
2328 | 2354 | return self.handle_normal(line,continue_prompt) |
|
2329 | 2355 | else: |
|
2330 | 2356 | # If the code compiles ok, we should handle it normally |
|
2331 | 2357 | return self.handle_normal(line,continue_prompt) |
|
2332 | 2358 | |
|
2333 | 2359 | def getapi(self): |
|
2334 | 2360 | """ Get an IPApi object for this shell instance |
|
2335 | 2361 | |
|
2336 | 2362 | Getting an IPApi object is always preferable to accessing the shell |
|
2337 | 2363 | directly, but this holds true especially for extensions. |
|
2338 | 2364 | |
|
2339 | 2365 | It should always be possible to implement an extension with IPApi |
|
2340 | 2366 | alone. If not, contact maintainer to request an addition. |
|
2341 | 2367 | |
|
2342 | 2368 | """ |
|
2343 | 2369 | return self.api |
|
2344 | 2370 | |
|
2345 | 2371 | def handle_emacs(self,line,continue_prompt=None, |
|
2346 | 2372 | pre=None,iFun=None,theRest=None): |
|
2347 | 2373 | """Handle input lines marked by python-mode.""" |
|
2348 | 2374 | |
|
2349 | 2375 | # Currently, nothing is done. Later more functionality can be added |
|
2350 | 2376 | # here if needed. |
|
2351 | 2377 | |
|
2352 | 2378 | # The input cache shouldn't be updated |
|
2353 | 2379 | |
|
2354 | 2380 | return line |
|
2355 | 2381 | |
|
2356 | 2382 | def mktempfile(self,data=None): |
|
2357 | 2383 | """Make a new tempfile and return its filename. |
|
2358 | 2384 | |
|
2359 | 2385 | This makes a call to tempfile.mktemp, but it registers the created |
|
2360 | 2386 | filename internally so ipython cleans it up at exit time. |
|
2361 | 2387 | |
|
2362 | 2388 | Optional inputs: |
|
2363 | 2389 | |
|
2364 | 2390 | - data(None): if data is given, it gets written out to the temp file |
|
2365 | 2391 | immediately, and the file is closed again.""" |
|
2366 | 2392 | |
|
2367 | 2393 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2368 | 2394 | self.tempfiles.append(filename) |
|
2369 | 2395 | |
|
2370 | 2396 | if data: |
|
2371 | 2397 | tmp_file = open(filename,'w') |
|
2372 | 2398 | tmp_file.write(data) |
|
2373 | 2399 | tmp_file.close() |
|
2374 | 2400 | return filename |
|
2375 | 2401 | |
|
2376 | 2402 | def write(self,data): |
|
2377 | 2403 | """Write a string to the default output""" |
|
2378 | 2404 | Term.cout.write(data) |
|
2379 | 2405 | |
|
2380 | 2406 | def write_err(self,data): |
|
2381 | 2407 | """Write a string to the default error output""" |
|
2382 | 2408 | Term.cerr.write(data) |
|
2383 | 2409 | |
|
2384 | 2410 | def exit(self): |
|
2385 | 2411 | """Handle interactive exit. |
|
2386 | 2412 | |
|
2387 | 2413 | This method sets the exit_now attribute.""" |
|
2388 | 2414 | |
|
2389 | 2415 | if self.rc.confirm_exit: |
|
2390 | 2416 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2391 | 2417 | self.exit_now = True |
|
2392 | 2418 | else: |
|
2393 | 2419 | self.exit_now = True |
|
2394 | 2420 | |
|
2395 | 2421 | def safe_execfile(self,fname,*where,**kw): |
|
2396 | 2422 | fname = os.path.expanduser(fname) |
|
2397 | 2423 | |
|
2398 | 2424 | try: |
|
2399 | 2425 | xfile = open(fname) |
|
2400 | 2426 | except: |
|
2401 | 2427 | print >> Term.cerr, \ |
|
2402 | 2428 | 'Could not open file <%s> for safe execution.' % fname |
|
2403 | 2429 | return None |
|
2404 | 2430 | |
|
2405 | 2431 | kw.setdefault('islog',0) |
|
2406 | 2432 | kw.setdefault('quiet',1) |
|
2407 | 2433 | kw.setdefault('exit_ignore',0) |
|
2408 | 2434 | first = xfile.readline() |
|
2409 | 2435 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2410 | 2436 | xfile.close() |
|
2411 | 2437 | # line by line execution |
|
2412 | 2438 | if first.startswith(loghead) or kw['islog']: |
|
2413 | 2439 | print 'Loading log file <%s> one line at a time...' % fname |
|
2414 | 2440 | if kw['quiet']: |
|
2415 | 2441 | stdout_save = sys.stdout |
|
2416 | 2442 | sys.stdout = StringIO.StringIO() |
|
2417 | 2443 | try: |
|
2418 | 2444 | globs,locs = where[0:2] |
|
2419 | 2445 | except: |
|
2420 | 2446 | try: |
|
2421 | 2447 | globs = locs = where[0] |
|
2422 | 2448 | except: |
|
2423 | 2449 | globs = locs = globals() |
|
2424 | 2450 | badblocks = [] |
|
2425 | 2451 | |
|
2426 | 2452 | # we also need to identify indented blocks of code when replaying |
|
2427 | 2453 | # logs and put them together before passing them to an exec |
|
2428 | 2454 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2429 | 2455 | # file. It's easiest if we swallow the whole thing in memory |
|
2430 | 2456 | # first, and manually walk through the lines list moving the |
|
2431 | 2457 | # counter ourselves. |
|
2432 | 2458 | indent_re = re.compile('\s+\S') |
|
2433 | 2459 | xfile = open(fname) |
|
2434 | 2460 | filelines = xfile.readlines() |
|
2435 | 2461 | xfile.close() |
|
2436 | 2462 | nlines = len(filelines) |
|
2437 | 2463 | lnum = 0 |
|
2438 | 2464 | while lnum < nlines: |
|
2439 | 2465 | line = filelines[lnum] |
|
2440 | 2466 | lnum += 1 |
|
2441 | 2467 | # don't re-insert logger status info into cache |
|
2442 | 2468 | if line.startswith('#log#'): |
|
2443 | 2469 | continue |
|
2444 | 2470 | else: |
|
2445 | 2471 | # build a block of code (maybe a single line) for execution |
|
2446 | 2472 | block = line |
|
2447 | 2473 | try: |
|
2448 | 2474 | next = filelines[lnum] # lnum has already incremented |
|
2449 | 2475 | except: |
|
2450 | 2476 | next = None |
|
2451 | 2477 | while next and indent_re.match(next): |
|
2452 | 2478 | block += next |
|
2453 | 2479 | lnum += 1 |
|
2454 | 2480 | try: |
|
2455 | 2481 | next = filelines[lnum] |
|
2456 | 2482 | except: |
|
2457 | 2483 | next = None |
|
2458 | 2484 | # now execute the block of one or more lines |
|
2459 | 2485 | try: |
|
2460 | 2486 | exec block in globs,locs |
|
2461 | 2487 | except SystemExit: |
|
2462 | 2488 | pass |
|
2463 | 2489 | except: |
|
2464 | 2490 | badblocks.append(block.rstrip()) |
|
2465 | 2491 | if kw['quiet']: # restore stdout |
|
2466 | 2492 | sys.stdout.close() |
|
2467 | 2493 | sys.stdout = stdout_save |
|
2468 | 2494 | print 'Finished replaying log file <%s>' % fname |
|
2469 | 2495 | if badblocks: |
|
2470 | 2496 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2471 | 2497 | '<%s> reported errors:' % fname) |
|
2472 | 2498 | |
|
2473 | 2499 | for badline in badblocks: |
|
2474 | 2500 | print >> sys.stderr, badline |
|
2475 | 2501 | else: # regular file execution |
|
2476 | 2502 | try: |
|
2477 | 2503 | execfile(fname,*where) |
|
2478 | 2504 | except SyntaxError: |
|
2479 | 2505 | self.showsyntaxerror() |
|
2480 | 2506 | warn('Failure executing file: <%s>' % fname) |
|
2481 | 2507 | except SystemExit,status: |
|
2482 | 2508 | if not kw['exit_ignore']: |
|
2483 | 2509 | self.showtraceback() |
|
2484 | 2510 | warn('Failure executing file: <%s>' % fname) |
|
2485 | 2511 | except: |
|
2486 | 2512 | self.showtraceback() |
|
2487 | 2513 | warn('Failure executing file: <%s>' % fname) |
|
2488 | 2514 | |
|
2489 | 2515 | #************************* end of file <iplib.py> ***************************** |
@@ -1,754 +1,755 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or better. |
|
6 | 6 | |
|
7 | 7 | This file contains the main make_IPython() starter function. |
|
8 | 8 | |
|
9 |
$Id: ipmaker.py 1 |
|
|
9 | $Id: ipmaker.py 1879 2006-11-04 00:34:34Z fptest $""" | |
|
10 | 10 | |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #***************************************************************************** |
|
17 | 17 | |
|
18 | 18 | from IPython import Release |
|
19 | 19 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | __version__ = Release.version |
|
22 | 22 | |
|
23 | 23 | credits._Printer__data = """ |
|
24 | 24 | Python: %s |
|
25 | 25 | |
|
26 | 26 | IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users. |
|
27 | 27 | See http://ipython.scipy.org for more information.""" \ |
|
28 | 28 | % credits._Printer__data |
|
29 | 29 | |
|
30 | 30 | copyright._Printer__data += """ |
|
31 | 31 | |
|
32 | 32 | Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray. |
|
33 | 33 | All Rights Reserved.""" |
|
34 | 34 | |
|
35 | 35 | #**************************************************************************** |
|
36 | 36 | # Required modules |
|
37 | 37 | |
|
38 | 38 | # From the standard library |
|
39 | 39 | import __main__ |
|
40 | 40 | import __builtin__ |
|
41 | 41 | import os |
|
42 | 42 | import re |
|
43 | 43 | import sys |
|
44 | 44 | import types |
|
45 | 45 | from pprint import pprint,pformat |
|
46 | 46 | |
|
47 | 47 | # Our own |
|
48 | 48 | from IPython import DPyGetOpt |
|
49 | 49 | from IPython.ipstruct import Struct |
|
50 | 50 | from IPython.OutputTrap import OutputTrap |
|
51 | 51 | from IPython.ConfigLoader import ConfigLoader |
|
52 | 52 | from IPython.iplib import InteractiveShell |
|
53 | 53 | from IPython.usage import cmd_line_usage,interactive_usage |
|
54 | 54 | from IPython.genutils import * |
|
55 | 55 | |
|
56 | 56 | #----------------------------------------------------------------------------- |
|
57 | 57 | def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1, |
|
58 | 58 | rc_override=None,shell_class=InteractiveShell, |
|
59 | 59 | embedded=False,**kw): |
|
60 | 60 | """This is a dump of IPython into a single function. |
|
61 | 61 | |
|
62 | 62 | Later it will have to be broken up in a sensible manner. |
|
63 | 63 | |
|
64 | 64 | Arguments: |
|
65 | 65 | |
|
66 | 66 | - argv: a list similar to sys.argv[1:]. It should NOT contain the desired |
|
67 | 67 | script name, b/c DPyGetOpt strips the first argument only for the real |
|
68 | 68 | sys.argv. |
|
69 | 69 | |
|
70 | 70 | - user_ns: a dict to be used as the user's namespace.""" |
|
71 | 71 | |
|
72 | 72 | #---------------------------------------------------------------------- |
|
73 | 73 | # Defaults and initialization |
|
74 | 74 | |
|
75 | 75 | # For developer debugging, deactivates crash handler and uses pdb. |
|
76 | 76 | DEVDEBUG = False |
|
77 | 77 | |
|
78 | 78 | if argv is None: |
|
79 | 79 | argv = sys.argv |
|
80 | 80 | |
|
81 | 81 | # __IP is the main global that lives throughout and represents the whole |
|
82 | 82 | # application. If the user redefines it, all bets are off as to what |
|
83 | 83 | # happens. |
|
84 | 84 | |
|
85 | 85 | # __IP is the name of he global which the caller will have accessible as |
|
86 | 86 | # __IP.name. We set its name via the first parameter passed to |
|
87 | 87 | # InteractiveShell: |
|
88 | 88 | |
|
89 | 89 | IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns, |
|
90 | 90 | embedded=embedded,**kw) |
|
91 | 91 | |
|
92 | 92 | # Put 'help' in the user namespace |
|
93 | 93 | from site import _Helper |
|
94 | 94 | IP.user_ns['help'] = _Helper() |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | if DEVDEBUG: |
|
98 | 98 | # For developer debugging only (global flag) |
|
99 | 99 | from IPython import ultraTB |
|
100 | 100 | sys.excepthook = ultraTB.VerboseTB(call_pdb=1) |
|
101 | 101 | |
|
102 | 102 | IP.BANNER_PARTS = ['Python %s\n' |
|
103 | 103 | 'Type "copyright", "credits" or "license" ' |
|
104 | 104 | 'for more information.\n' |
|
105 | 105 | % (sys.version.split('\n')[0],), |
|
106 | 106 | "IPython %s -- An enhanced Interactive Python." |
|
107 | 107 | % (__version__,), |
|
108 | 108 | """? -> Introduction to IPython's features. |
|
109 | 109 | %magic -> Information about IPython's 'magic' % functions. |
|
110 | 110 | help -> Python's own help system. |
|
111 | 111 | object? -> Details about 'object'. ?object also works, ?? prints more. |
|
112 | 112 | """ ] |
|
113 | 113 | |
|
114 | 114 | IP.usage = interactive_usage |
|
115 | 115 | |
|
116 | 116 | # Platform-dependent suffix and directory names. We use _ipython instead |
|
117 | 117 | # of .ipython under win32 b/c there's software that breaks with .named |
|
118 | 118 | # directories on that platform. |
|
119 | 119 | if os.name == 'posix': |
|
120 | 120 | rc_suffix = '' |
|
121 | 121 | ipdir_def = '.ipython' |
|
122 | 122 | else: |
|
123 | 123 | rc_suffix = '.ini' |
|
124 | 124 | ipdir_def = '_ipython' |
|
125 | 125 | |
|
126 | 126 | # default directory for configuration |
|
127 | 127 | ipythondir_def = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
128 | 128 | os.path.join(IP.home_dir,ipdir_def))) |
|
129 | 129 | |
|
130 | 130 | sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran |
|
131 | 131 | |
|
132 | 132 | # we need the directory where IPython itself is installed |
|
133 | 133 | import IPython |
|
134 | 134 | IPython_dir = os.path.dirname(IPython.__file__) |
|
135 | 135 | del IPython |
|
136 | 136 | |
|
137 | 137 | #------------------------------------------------------------------------- |
|
138 | 138 | # Command line handling |
|
139 | 139 | |
|
140 | 140 | # Valid command line options (uses DPyGetOpt syntax, like Perl's |
|
141 | 141 | # GetOpt::Long) |
|
142 | 142 | |
|
143 | 143 | # Any key not listed here gets deleted even if in the file (like session |
|
144 | 144 | # or profile). That's deliberate, to maintain the rc namespace clean. |
|
145 | 145 | |
|
146 | 146 | # Each set of options appears twice: under _conv only the names are |
|
147 | 147 | # listed, indicating which type they must be converted to when reading the |
|
148 | 148 | # ipythonrc file. And under DPyGetOpt they are listed with the regular |
|
149 | 149 | # DPyGetOpt syntax (=s,=i,:f,etc). |
|
150 | 150 | |
|
151 | 151 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
152 | 152 | cmdline_opts = ('autocall=i autoindent! automagic! banner! cache_size|cs=i ' |
|
153 | 153 | 'c=s classic|cl color_info! colors=s confirm_exit! ' |
|
154 | 154 | 'debug! deep_reload! editor=s log|l messages! nosep ' |
|
155 | 155 | 'object_info_string_level=i pdb! ' |
|
156 | 156 | 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s ' |
|
157 | 157 | 'quick screen_length|sl=i prompts_pad_left=i ' |
|
158 | 158 | 'logfile|lf=s logplay|lp=s profile|p=s ' |
|
159 | 159 | 'readline! readline_merge_completions! ' |
|
160 | 160 | 'readline_omit__names! ' |
|
161 | 161 | 'rcfile=s separate_in|si=s separate_out|so=s ' |
|
162 | 162 | 'separate_out2|so2=s xmode=s wildcards_case_sensitive! ' |
|
163 | 163 | 'magic_docstrings system_verbose! ' |
|
164 | 164 | 'multi_line_specials! ' |
|
165 | 165 | 'wxversion=s ' |
|
166 | 166 | 'autoedit_syntax!') |
|
167 | 167 | |
|
168 | 168 | # Options that can *only* appear at the cmd line (not in rcfiles). |
|
169 | 169 | |
|
170 | 170 | # The "ignore" option is a kludge so that Emacs buffers don't crash, since |
|
171 | 171 | # the 'C-c !' command in emacs automatically appends a -i option at the end. |
|
172 | 172 | cmdline_only = ('help ignore|i ipythondir=s Version upgrade ' |
|
173 | 173 | 'gthread! qthread! q4thread! wthread! pylab! tk!') |
|
174 | 174 | |
|
175 | 175 | # Build the actual name list to be used by DPyGetOpt |
|
176 | 176 | opts_names = qw(cmdline_opts) + qw(cmdline_only) |
|
177 | 177 | |
|
178 | 178 | # Set sensible command line defaults. |
|
179 | 179 | # This should have everything from cmdline_opts and cmdline_only |
|
180 | 180 | opts_def = Struct(autocall = 1, |
|
181 | 181 | autoedit_syntax = 0, |
|
182 | 182 | autoindent = 0, |
|
183 | 183 | automagic = 1, |
|
184 | 184 | banner = 1, |
|
185 | 185 | cache_size = 1000, |
|
186 | 186 | c = '', |
|
187 | 187 | classic = 0, |
|
188 | 188 | colors = 'NoColor', |
|
189 | 189 | color_info = 0, |
|
190 | 190 | confirm_exit = 1, |
|
191 | 191 | debug = 0, |
|
192 | 192 | deep_reload = 0, |
|
193 | 193 | editor = '0', |
|
194 | 194 | help = 0, |
|
195 | 195 | ignore = 0, |
|
196 | 196 | ipythondir = ipythondir_def, |
|
197 | 197 | log = 0, |
|
198 | 198 | logfile = '', |
|
199 | 199 | logplay = '', |
|
200 | 200 | multi_line_specials = 1, |
|
201 | 201 | messages = 1, |
|
202 | 202 | object_info_string_level = 0, |
|
203 | 203 | nosep = 0, |
|
204 | 204 | pdb = 0, |
|
205 | 205 | pprint = 0, |
|
206 | 206 | profile = '', |
|
207 | 207 | prompt_in1 = 'In [\\#]: ', |
|
208 | 208 | prompt_in2 = ' .\\D.: ', |
|
209 | 209 | prompt_out = 'Out[\\#]: ', |
|
210 | 210 | prompts_pad_left = 1, |
|
211 | 211 | quiet = 0, |
|
212 | 212 | quick = 0, |
|
213 | 213 | readline = 1, |
|
214 | 214 | readline_merge_completions = 1, |
|
215 | 215 | readline_omit__names = 0, |
|
216 | 216 | rcfile = 'ipythonrc' + rc_suffix, |
|
217 | 217 | screen_length = 0, |
|
218 | 218 | separate_in = '\n', |
|
219 | 219 | separate_out = '\n', |
|
220 | 220 | separate_out2 = '', |
|
221 | system_header = 'IPython system call: ', | |
|
221 | 222 | system_verbose = 0, |
|
222 | 223 | gthread = 0, |
|
223 | 224 | qthread = 0, |
|
224 | 225 | q4thread = 0, |
|
225 | 226 | wthread = 0, |
|
226 | 227 | pylab = 0, |
|
227 | 228 | tk = 0, |
|
228 | 229 | upgrade = 0, |
|
229 | 230 | Version = 0, |
|
230 | 231 | xmode = 'Verbose', |
|
231 | 232 | wildcards_case_sensitive = 1, |
|
232 | 233 | wxversion = '0', |
|
233 | 234 | magic_docstrings = 0, # undocumented, for doc generation |
|
234 | 235 | ) |
|
235 | 236 | |
|
236 | 237 | # Things that will *only* appear in rcfiles (not at the command line). |
|
237 | 238 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
238 | 239 | rcfile_opts = { qwflat: 'include import_mod import_all execfile ', |
|
239 | 240 | qw_lol: 'import_some ', |
|
240 | 241 | # for things with embedded whitespace: |
|
241 | 242 | list_strings:'execute alias readline_parse_and_bind ', |
|
242 | 243 | # Regular strings need no conversion: |
|
243 | 244 | None:'readline_remove_delims ', |
|
244 | 245 | } |
|
245 | 246 | # Default values for these |
|
246 | 247 | rc_def = Struct(include = [], |
|
247 | 248 | import_mod = [], |
|
248 | 249 | import_all = [], |
|
249 | 250 | import_some = [[]], |
|
250 | 251 | execute = [], |
|
251 | 252 | execfile = [], |
|
252 | 253 | alias = [], |
|
253 | 254 | readline_parse_and_bind = [], |
|
254 | 255 | readline_remove_delims = '', |
|
255 | 256 | ) |
|
256 | 257 | |
|
257 | 258 | # Build the type conversion dictionary from the above tables: |
|
258 | 259 | typeconv = rcfile_opts.copy() |
|
259 | 260 | typeconv.update(optstr2types(cmdline_opts)) |
|
260 | 261 | |
|
261 | 262 | # FIXME: the None key appears in both, put that back together by hand. Ugly! |
|
262 | 263 | typeconv[None] += ' ' + rcfile_opts[None] |
|
263 | 264 | |
|
264 | 265 | # Remove quotes at ends of all strings (used to protect spaces) |
|
265 | 266 | typeconv[unquote_ends] = typeconv[None] |
|
266 | 267 | del typeconv[None] |
|
267 | 268 | |
|
268 | 269 | # Build the list we'll use to make all config decisions with defaults: |
|
269 | 270 | opts_all = opts_def.copy() |
|
270 | 271 | opts_all.update(rc_def) |
|
271 | 272 | |
|
272 | 273 | # Build conflict resolver for recursive loading of config files: |
|
273 | 274 | # - preserve means the outermost file maintains the value, it is not |
|
274 | 275 | # overwritten if an included file has the same key. |
|
275 | 276 | # - add_flip applies + to the two values, so it better make sense to add |
|
276 | 277 | # those types of keys. But it flips them first so that things loaded |
|
277 | 278 | # deeper in the inclusion chain have lower precedence. |
|
278 | 279 | conflict = {'preserve': ' '.join([ typeconv[int], |
|
279 | 280 | typeconv[unquote_ends] ]), |
|
280 | 281 | 'add_flip': ' '.join([ typeconv[qwflat], |
|
281 | 282 | typeconv[qw_lol], |
|
282 | 283 | typeconv[list_strings] ]) |
|
283 | 284 | } |
|
284 | 285 | |
|
285 | 286 | # Now actually process the command line |
|
286 | 287 | getopt = DPyGetOpt.DPyGetOpt() |
|
287 | 288 | getopt.setIgnoreCase(0) |
|
288 | 289 | |
|
289 | 290 | getopt.parseConfiguration(opts_names) |
|
290 | 291 | |
|
291 | 292 | try: |
|
292 | 293 | getopt.processArguments(argv) |
|
293 | 294 | except: |
|
294 | 295 | print cmd_line_usage |
|
295 | 296 | warn('\nError in Arguments: ' + `sys.exc_value`) |
|
296 | 297 | sys.exit(1) |
|
297 | 298 | |
|
298 | 299 | # convert the options dict to a struct for much lighter syntax later |
|
299 | 300 | opts = Struct(getopt.optionValues) |
|
300 | 301 | args = getopt.freeValues |
|
301 | 302 | |
|
302 | 303 | # this is the struct (which has default values at this point) with which |
|
303 | 304 | # we make all decisions: |
|
304 | 305 | opts_all.update(opts) |
|
305 | 306 | |
|
306 | 307 | # Options that force an immediate exit |
|
307 | 308 | if opts_all.help: |
|
308 | 309 | page(cmd_line_usage) |
|
309 | 310 | sys.exit() |
|
310 | 311 | |
|
311 | 312 | if opts_all.Version: |
|
312 | 313 | print __version__ |
|
313 | 314 | sys.exit() |
|
314 | 315 | |
|
315 | 316 | if opts_all.magic_docstrings: |
|
316 | 317 | IP.magic_magic('-latex') |
|
317 | 318 | sys.exit() |
|
318 | 319 | |
|
319 | 320 | # add personal ipythondir to sys.path so that users can put things in |
|
320 | 321 | # there for customization |
|
321 | 322 | sys.path.append(os.path.abspath(opts_all.ipythondir)) |
|
322 | 323 | |
|
323 | 324 | # Create user config directory if it doesn't exist. This must be done |
|
324 | 325 | # *after* getting the cmd line options. |
|
325 | 326 | if not os.path.isdir(opts_all.ipythondir): |
|
326 | 327 | IP.user_setup(opts_all.ipythondir,rc_suffix,'install') |
|
327 | 328 | |
|
328 | 329 | # upgrade user config files while preserving a copy of the originals |
|
329 | 330 | if opts_all.upgrade: |
|
330 | 331 | IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade') |
|
331 | 332 | |
|
332 | 333 | # check mutually exclusive options in the *original* command line |
|
333 | 334 | mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'), |
|
334 | 335 | qw('classic profile'),qw('classic rcfile')]) |
|
335 | 336 | |
|
336 | 337 | #--------------------------------------------------------------------------- |
|
337 | 338 | # Log replay |
|
338 | 339 | |
|
339 | 340 | # if -logplay, we need to 'become' the other session. That basically means |
|
340 | 341 | # replacing the current command line environment with that of the old |
|
341 | 342 | # session and moving on. |
|
342 | 343 | |
|
343 | 344 | # this is needed so that later we know we're in session reload mode, as |
|
344 | 345 | # opts_all will get overwritten: |
|
345 | 346 | load_logplay = 0 |
|
346 | 347 | |
|
347 | 348 | if opts_all.logplay: |
|
348 | 349 | load_logplay = opts_all.logplay |
|
349 | 350 | opts_debug_save = opts_all.debug |
|
350 | 351 | try: |
|
351 | 352 | logplay = open(opts_all.logplay) |
|
352 | 353 | except IOError: |
|
353 | 354 | if opts_all.debug: IP.InteractiveTB() |
|
354 | 355 | warn('Could not open logplay file '+`opts_all.logplay`) |
|
355 | 356 | # restore state as if nothing had happened and move on, but make |
|
356 | 357 | # sure that later we don't try to actually load the session file |
|
357 | 358 | logplay = None |
|
358 | 359 | load_logplay = 0 |
|
359 | 360 | del opts_all.logplay |
|
360 | 361 | else: |
|
361 | 362 | try: |
|
362 | 363 | logplay.readline() |
|
363 | 364 | logplay.readline(); |
|
364 | 365 | # this reloads that session's command line |
|
365 | 366 | cmd = logplay.readline()[6:] |
|
366 | 367 | exec cmd |
|
367 | 368 | # restore the true debug flag given so that the process of |
|
368 | 369 | # session loading itself can be monitored. |
|
369 | 370 | opts.debug = opts_debug_save |
|
370 | 371 | # save the logplay flag so later we don't overwrite the log |
|
371 | 372 | opts.logplay = load_logplay |
|
372 | 373 | # now we must update our own structure with defaults |
|
373 | 374 | opts_all.update(opts) |
|
374 | 375 | # now load args |
|
375 | 376 | cmd = logplay.readline()[6:] |
|
376 | 377 | exec cmd |
|
377 | 378 | logplay.close() |
|
378 | 379 | except: |
|
379 | 380 | logplay.close() |
|
380 | 381 | if opts_all.debug: IP.InteractiveTB() |
|
381 | 382 | warn("Logplay file lacking full configuration information.\n" |
|
382 | 383 | "I'll try to read it, but some things may not work.") |
|
383 | 384 | |
|
384 | 385 | #------------------------------------------------------------------------- |
|
385 | 386 | # set up output traps: catch all output from files, being run, modules |
|
386 | 387 | # loaded, etc. Then give it to the user in a clean form at the end. |
|
387 | 388 | |
|
388 | 389 | msg_out = 'Output messages. ' |
|
389 | 390 | msg_err = 'Error messages. ' |
|
390 | 391 | msg_sep = '\n' |
|
391 | 392 | msg = Struct(config = OutputTrap('Configuration Loader',msg_out, |
|
392 | 393 | msg_err,msg_sep,debug, |
|
393 | 394 | quiet_out=1), |
|
394 | 395 | user_exec = OutputTrap('User File Execution',msg_out, |
|
395 | 396 | msg_err,msg_sep,debug), |
|
396 | 397 | logplay = OutputTrap('Log Loader',msg_out, |
|
397 | 398 | msg_err,msg_sep,debug), |
|
398 | 399 | summary = '' |
|
399 | 400 | ) |
|
400 | 401 | |
|
401 | 402 | #------------------------------------------------------------------------- |
|
402 | 403 | # Process user ipythonrc-type configuration files |
|
403 | 404 | |
|
404 | 405 | # turn on output trapping and log to msg.config |
|
405 | 406 | # remember that with debug on, trapping is actually disabled |
|
406 | 407 | msg.config.trap_all() |
|
407 | 408 | |
|
408 | 409 | # look for rcfile in current or default directory |
|
409 | 410 | try: |
|
410 | 411 | opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir) |
|
411 | 412 | except IOError: |
|
412 | 413 | if opts_all.debug: IP.InteractiveTB() |
|
413 | 414 | warn('Configuration file %s not found. Ignoring request.' |
|
414 | 415 | % (opts_all.rcfile) ) |
|
415 | 416 | |
|
416 | 417 | # 'profiles' are a shorthand notation for config filenames |
|
417 | 418 | if opts_all.profile: |
|
418 | 419 | |
|
419 | 420 | try: |
|
420 | 421 | opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile |
|
421 | 422 | + rc_suffix, |
|
422 | 423 | opts_all.ipythondir) |
|
423 | 424 | except IOError: |
|
424 | 425 | if opts_all.debug: IP.InteractiveTB() |
|
425 | 426 | opts.profile = '' # remove profile from options if invalid |
|
426 | 427 | # We won't warn anymore, primary method is ipy_profile_PROFNAME |
|
427 | 428 | # which does trigger a warning. |
|
428 | 429 | |
|
429 | 430 | # load the config file |
|
430 | 431 | rcfiledata = None |
|
431 | 432 | if opts_all.quick: |
|
432 | 433 | print 'Launching IPython in quick mode. No config file read.' |
|
433 | 434 | elif opts_all.classic: |
|
434 | 435 | print 'Launching IPython in classic mode. No config file read.' |
|
435 | 436 | elif opts_all.rcfile: |
|
436 | 437 | try: |
|
437 | 438 | cfg_loader = ConfigLoader(conflict) |
|
438 | 439 | rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv, |
|
439 | 440 | 'include',opts_all.ipythondir, |
|
440 | 441 | purge = 1, |
|
441 | 442 | unique = conflict['preserve']) |
|
442 | 443 | except: |
|
443 | 444 | IP.InteractiveTB() |
|
444 | 445 | warn('Problems loading configuration file '+ |
|
445 | 446 | `opts_all.rcfile`+ |
|
446 | 447 | '\nStarting with default -bare bones- configuration.') |
|
447 | 448 | else: |
|
448 | 449 | warn('No valid configuration file found in either currrent directory\n'+ |
|
449 | 450 | 'or in the IPython config. directory: '+`opts_all.ipythondir`+ |
|
450 | 451 | '\nProceeding with internal defaults.') |
|
451 | 452 | |
|
452 | 453 | #------------------------------------------------------------------------ |
|
453 | 454 | # Set exception handlers in mode requested by user. |
|
454 | 455 | otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode |
|
455 | 456 | IP.magic_xmode(opts_all.xmode) |
|
456 | 457 | otrap.release_out() |
|
457 | 458 | |
|
458 | 459 | #------------------------------------------------------------------------ |
|
459 | 460 | # Execute user config |
|
460 | 461 | |
|
461 | 462 | # Create a valid config structure with the right precedence order: |
|
462 | 463 | # defaults < rcfile < command line. This needs to be in the instance, so |
|
463 | 464 | # that method calls below that rely on it find it. |
|
464 | 465 | IP.rc = rc_def.copy() |
|
465 | 466 | |
|
466 | 467 | # Work with a local alias inside this routine to avoid unnecessary |
|
467 | 468 | # attribute lookups. |
|
468 | 469 | IP_rc = IP.rc |
|
469 | 470 | |
|
470 | 471 | IP_rc.update(opts_def) |
|
471 | 472 | if rcfiledata: |
|
472 | 473 | # now we can update |
|
473 | 474 | IP_rc.update(rcfiledata) |
|
474 | 475 | IP_rc.update(opts) |
|
475 | 476 | IP_rc.update(rc_override) |
|
476 | 477 | |
|
477 | 478 | # Store the original cmd line for reference: |
|
478 | 479 | IP_rc.opts = opts |
|
479 | 480 | IP_rc.args = args |
|
480 | 481 | |
|
481 | 482 | # create a *runtime* Struct like rc for holding parameters which may be |
|
482 | 483 | # created and/or modified by runtime user extensions. |
|
483 | 484 | IP.runtime_rc = Struct() |
|
484 | 485 | |
|
485 | 486 | # from this point on, all config should be handled through IP_rc, |
|
486 | 487 | # opts* shouldn't be used anymore. |
|
487 | 488 | |
|
488 | 489 | |
|
489 | 490 | # update IP_rc with some special things that need manual |
|
490 | 491 | # tweaks. Basically options which affect other options. I guess this |
|
491 | 492 | # should just be written so that options are fully orthogonal and we |
|
492 | 493 | # wouldn't worry about this stuff! |
|
493 | 494 | |
|
494 | 495 | if IP_rc.classic: |
|
495 | 496 | IP_rc.quick = 1 |
|
496 | 497 | IP_rc.cache_size = 0 |
|
497 | 498 | IP_rc.pprint = 0 |
|
498 | 499 | IP_rc.prompt_in1 = '>>> ' |
|
499 | 500 | IP_rc.prompt_in2 = '... ' |
|
500 | 501 | IP_rc.prompt_out = '' |
|
501 | 502 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
502 | 503 | IP_rc.colors = 'NoColor' |
|
503 | 504 | IP_rc.xmode = 'Plain' |
|
504 | 505 | |
|
505 | 506 | IP.pre_config_initialization() |
|
506 | 507 | # configure readline |
|
507 | 508 | # Define the history file for saving commands in between sessions |
|
508 | 509 | if IP_rc.profile: |
|
509 | 510 | histfname = 'history-%s' % IP_rc.profile |
|
510 | 511 | else: |
|
511 | 512 | histfname = 'history' |
|
512 | 513 | IP.histfile = os.path.join(opts_all.ipythondir,histfname) |
|
513 | 514 | |
|
514 | 515 | # update exception handlers with rc file status |
|
515 | 516 | otrap.trap_out() # I don't want these messages ever. |
|
516 | 517 | IP.magic_xmode(IP_rc.xmode) |
|
517 | 518 | otrap.release_out() |
|
518 | 519 | |
|
519 | 520 | # activate logging if requested and not reloading a log |
|
520 | 521 | if IP_rc.logplay: |
|
521 | 522 | IP.magic_logstart(IP_rc.logplay + ' append') |
|
522 | 523 | elif IP_rc.logfile: |
|
523 | 524 | IP.magic_logstart(IP_rc.logfile) |
|
524 | 525 | elif IP_rc.log: |
|
525 | 526 | IP.magic_logstart() |
|
526 | 527 | |
|
527 | 528 | # find user editor so that it we don't have to look it up constantly |
|
528 | 529 | if IP_rc.editor.strip()=='0': |
|
529 | 530 | try: |
|
530 | 531 | ed = os.environ['EDITOR'] |
|
531 | 532 | except KeyError: |
|
532 | 533 | if os.name == 'posix': |
|
533 | 534 | ed = 'vi' # the only one guaranteed to be there! |
|
534 | 535 | else: |
|
535 | 536 | ed = 'notepad' # same in Windows! |
|
536 | 537 | IP_rc.editor = ed |
|
537 | 538 | |
|
538 | 539 | # Keep track of whether this is an embedded instance or not (useful for |
|
539 | 540 | # post-mortems). |
|
540 | 541 | IP_rc.embedded = IP.embedded |
|
541 | 542 | |
|
542 | 543 | # Recursive reload |
|
543 | 544 | try: |
|
544 | 545 | from IPython import deep_reload |
|
545 | 546 | if IP_rc.deep_reload: |
|
546 | 547 | __builtin__.reload = deep_reload.reload |
|
547 | 548 | else: |
|
548 | 549 | __builtin__.dreload = deep_reload.reload |
|
549 | 550 | del deep_reload |
|
550 | 551 | except ImportError: |
|
551 | 552 | pass |
|
552 | 553 | |
|
553 | 554 | # Save the current state of our namespace so that the interactive shell |
|
554 | 555 | # can later know which variables have been created by us from config files |
|
555 | 556 | # and loading. This way, loading a file (in any way) is treated just like |
|
556 | 557 | # defining things on the command line, and %who works as expected. |
|
557 | 558 | |
|
558 | 559 | # DON'T do anything that affects the namespace beyond this point! |
|
559 | 560 | IP.internal_ns.update(__main__.__dict__) |
|
560 | 561 | |
|
561 | 562 | #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who |
|
562 | 563 | |
|
563 | 564 | # Now run through the different sections of the users's config |
|
564 | 565 | if IP_rc.debug: |
|
565 | 566 | print 'Trying to execute the following configuration structure:' |
|
566 | 567 | print '(Things listed first are deeper in the inclusion tree and get' |
|
567 | 568 | print 'loaded first).\n' |
|
568 | 569 | pprint(IP_rc.__dict__) |
|
569 | 570 | |
|
570 | 571 | for mod in IP_rc.import_mod: |
|
571 | 572 | try: |
|
572 | 573 | exec 'import '+mod in IP.user_ns |
|
573 | 574 | except : |
|
574 | 575 | IP.InteractiveTB() |
|
575 | 576 | import_fail_info(mod) |
|
576 | 577 | |
|
577 | 578 | for mod_fn in IP_rc.import_some: |
|
578 | 579 | if mod_fn == []: break |
|
579 | 580 | mod,fn = mod_fn[0],','.join(mod_fn[1:]) |
|
580 | 581 | try: |
|
581 | 582 | exec 'from '+mod+' import '+fn in IP.user_ns |
|
582 | 583 | except : |
|
583 | 584 | IP.InteractiveTB() |
|
584 | 585 | import_fail_info(mod,fn) |
|
585 | 586 | |
|
586 | 587 | for mod in IP_rc.import_all: |
|
587 | 588 | try: |
|
588 | 589 | exec 'from '+mod+' import *' in IP.user_ns |
|
589 | 590 | except : |
|
590 | 591 | IP.InteractiveTB() |
|
591 | 592 | import_fail_info(mod) |
|
592 | 593 | |
|
593 | 594 | for code in IP_rc.execute: |
|
594 | 595 | try: |
|
595 | 596 | exec code in IP.user_ns |
|
596 | 597 | except: |
|
597 | 598 | IP.InteractiveTB() |
|
598 | 599 | warn('Failure executing code: ' + `code`) |
|
599 | 600 | |
|
600 | 601 | # Execute the files the user wants in ipythonrc |
|
601 | 602 | for file in IP_rc.execfile: |
|
602 | 603 | try: |
|
603 | 604 | file = filefind(file,sys.path+[IPython_dir]) |
|
604 | 605 | except IOError: |
|
605 | 606 | warn(itpl('File $file not found. Skipping it.')) |
|
606 | 607 | else: |
|
607 | 608 | IP.safe_execfile(os.path.expanduser(file),IP.user_ns) |
|
608 | 609 | |
|
609 | 610 | # finally, try importing ipy_*_conf for final configuration |
|
610 | 611 | try: |
|
611 | 612 | import ipy_system_conf |
|
612 | 613 | except ImportError: |
|
613 | 614 | if opts_all.debug: IP.InteractiveTB() |
|
614 | 615 | warn("Could not import 'ipy_system_conf'") |
|
615 | 616 | except: |
|
616 | 617 | IP.InteractiveTB() |
|
617 | 618 | import_fail_info('ipy_system_conf') |
|
618 | 619 | |
|
619 | 620 | if opts_all.profile: |
|
620 | 621 | profmodname = 'ipy_profile_' + opts_all.profile |
|
621 | 622 | try: |
|
622 | 623 | __import__(profmodname) |
|
623 | 624 | except ImportError: |
|
624 | 625 | # only warn if ipythonrc-PROFNAME didn't exist |
|
625 | 626 | if opts.profile =='': |
|
626 | 627 | warn("Could not start with profile '%s'!\n" |
|
627 | 628 | "('%s/%s.py' does not exist? run '%%upgrade')" % |
|
628 | 629 | (opts_all.profile, opts_all.ipythondir, profmodname) ) |
|
629 | 630 | except: |
|
630 | 631 | print "Error importing",profmodname |
|
631 | 632 | IP.InteractiveTB() |
|
632 | 633 | import_fail_info(profmodname) |
|
633 | 634 | |
|
634 | 635 | try: |
|
635 | 636 | import ipy_user_conf |
|
636 | 637 | except ImportError: |
|
637 | 638 | if opts_all.debug: IP.InteractiveTB() |
|
638 | 639 | warn("Could not import user config!\n " |
|
639 | 640 | "('%s/ipy_user_conf.py' does not exist? Please run '%%upgrade')\n" |
|
640 | 641 | % opts_all.ipythondir) |
|
641 | 642 | except: |
|
642 | 643 | print "Error importing ipy_user_conf" |
|
643 | 644 | IP.InteractiveTB() |
|
644 | 645 | import_fail_info("ipy_user_conf") |
|
645 | 646 | |
|
646 | 647 | # release stdout and stderr and save config log into a global summary |
|
647 | 648 | msg.config.release_all() |
|
648 | 649 | if IP_rc.messages: |
|
649 | 650 | msg.summary += msg.config.summary_all() |
|
650 | 651 | |
|
651 | 652 | #------------------------------------------------------------------------ |
|
652 | 653 | # Setup interactive session |
|
653 | 654 | |
|
654 | 655 | # Now we should be fully configured. We can then execute files or load |
|
655 | 656 | # things only needed for interactive use. Then we'll open the shell. |
|
656 | 657 | |
|
657 | 658 | # Take a snapshot of the user namespace before opening the shell. That way |
|
658 | 659 | # we'll be able to identify which things were interactively defined and |
|
659 | 660 | # which were defined through config files. |
|
660 | 661 | IP.user_config_ns = IP.user_ns.copy() |
|
661 | 662 | |
|
662 | 663 | # Force reading a file as if it were a session log. Slower but safer. |
|
663 | 664 | if load_logplay: |
|
664 | 665 | print 'Replaying log...' |
|
665 | 666 | try: |
|
666 | 667 | if IP_rc.debug: |
|
667 | 668 | logplay_quiet = 0 |
|
668 | 669 | else: |
|
669 | 670 | logplay_quiet = 1 |
|
670 | 671 | |
|
671 | 672 | msg.logplay.trap_all() |
|
672 | 673 | IP.safe_execfile(load_logplay,IP.user_ns, |
|
673 | 674 | islog = 1, quiet = logplay_quiet) |
|
674 | 675 | msg.logplay.release_all() |
|
675 | 676 | if IP_rc.messages: |
|
676 | 677 | msg.summary += msg.logplay.summary_all() |
|
677 | 678 | except: |
|
678 | 679 | warn('Problems replaying logfile %s.' % load_logplay) |
|
679 | 680 | IP.InteractiveTB() |
|
680 | 681 | |
|
681 | 682 | # Load remaining files in command line |
|
682 | 683 | msg.user_exec.trap_all() |
|
683 | 684 | |
|
684 | 685 | # Do NOT execute files named in the command line as scripts to be loaded |
|
685 | 686 | # by embedded instances. Doing so has the potential for an infinite |
|
686 | 687 | # recursion if there are exceptions thrown in the process. |
|
687 | 688 | |
|
688 | 689 | # XXX FIXME: the execution of user files should be moved out to after |
|
689 | 690 | # ipython is fully initialized, just as if they were run via %run at the |
|
690 | 691 | # ipython prompt. This would also give them the benefit of ipython's |
|
691 | 692 | # nice tracebacks. |
|
692 | 693 | |
|
693 | 694 | if (not embedded and IP_rc.args and |
|
694 | 695 | not IP_rc.args[0].lower().endswith('.ipy')): |
|
695 | 696 | name_save = IP.user_ns['__name__'] |
|
696 | 697 | IP.user_ns['__name__'] = '__main__' |
|
697 | 698 | # Set our own excepthook in case the user code tries to call it |
|
698 | 699 | # directly. This prevents triggering the IPython crash handler. |
|
699 | 700 | old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook |
|
700 | 701 | |
|
701 | 702 | save_argv = sys.argv[1:] # save it for later restoring |
|
702 | 703 | |
|
703 | 704 | sys.argv = args |
|
704 | 705 | |
|
705 | 706 | try: |
|
706 | 707 | IP.safe_execfile(args[0], IP.user_ns) |
|
707 | 708 | finally: |
|
708 | 709 | # Reset our crash handler in place |
|
709 | 710 | sys.excepthook = old_excepthook |
|
710 | 711 | sys.argv[:] = save_argv |
|
711 | 712 | IP.user_ns['__name__'] = name_save |
|
712 | 713 | |
|
713 | 714 | msg.user_exec.release_all() |
|
714 | 715 | |
|
715 | 716 | if IP_rc.messages: |
|
716 | 717 | msg.summary += msg.user_exec.summary_all() |
|
717 | 718 | |
|
718 | 719 | # since we can't specify a null string on the cmd line, 0 is the equivalent: |
|
719 | 720 | if IP_rc.nosep: |
|
720 | 721 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
721 | 722 | if IP_rc.separate_in == '0': IP_rc.separate_in = '' |
|
722 | 723 | if IP_rc.separate_out == '0': IP_rc.separate_out = '' |
|
723 | 724 | if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = '' |
|
724 | 725 | IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n') |
|
725 | 726 | IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n') |
|
726 | 727 | IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n') |
|
727 | 728 | |
|
728 | 729 | # Determine how many lines at the bottom of the screen are needed for |
|
729 | 730 | # showing prompts, so we can know wheter long strings are to be printed or |
|
730 | 731 | # paged: |
|
731 | 732 | num_lines_bot = IP_rc.separate_in.count('\n')+1 |
|
732 | 733 | IP_rc.screen_length = IP_rc.screen_length - num_lines_bot |
|
733 | 734 | |
|
734 | 735 | # configure startup banner |
|
735 | 736 | if IP_rc.c: # regular python doesn't print the banner with -c |
|
736 | 737 | IP_rc.banner = 0 |
|
737 | 738 | if IP_rc.banner: |
|
738 | 739 | BANN_P = IP.BANNER_PARTS |
|
739 | 740 | else: |
|
740 | 741 | BANN_P = [] |
|
741 | 742 | |
|
742 | 743 | if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile) |
|
743 | 744 | |
|
744 | 745 | # add message log (possibly empty) |
|
745 | 746 | if msg.summary: BANN_P.append(msg.summary) |
|
746 | 747 | # Final banner is a string |
|
747 | 748 | IP.BANNER = '\n'.join(BANN_P) |
|
748 | 749 | |
|
749 | 750 | # Finalize the IPython instance. This assumes the rc structure is fully |
|
750 | 751 | # in place. |
|
751 | 752 | IP.post_config_initialization() |
|
752 | 753 | |
|
753 | 754 | return IP |
|
754 | 755 | #************************ end of file <ipmaker.py> ************************** |
@@ -1,305 +1,314 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """Module for interactively running scripts. |
|
3 | 3 | |
|
4 | 4 | This module implements classes for interactively running scripts written for |
|
5 | 5 | any system with a prompt which can be matched by a regexp suitable for |
|
6 | 6 | pexpect. It can be used to run as if they had been typed up interactively, an |
|
7 | 7 | arbitrary series of commands for the target system. |
|
8 | 8 | |
|
9 | 9 | The module includes classes ready for IPython (with the default prompts), |
|
10 | 10 | plain Python and SAGE, but making a new one is trivial. To see how to use it, |
|
11 | 11 | simply run the module as a script: |
|
12 | 12 | |
|
13 | 13 | ./irunner.py --help |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | This is an extension of Ken Schutte <kschutte-AT-csail.mit.edu>'s script |
|
17 | 17 | contributed on the ipython-user list: |
|
18 | 18 | |
|
19 | 19 | http://scipy.net/pipermail/ipython-user/2006-May/001705.html |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | NOTES: |
|
23 | 23 | |
|
24 | 24 | - This module requires pexpect, available in most linux distros, or which can |
|
25 | 25 | be downloaded from |
|
26 | 26 | |
|
27 | 27 | http://pexpect.sourceforge.net |
|
28 | 28 | |
|
29 | 29 | - Because pexpect only works under Unix or Windows-Cygwin, this has the same |
|
30 | 30 | limitations. This means that it will NOT work under native windows Python. |
|
31 | 31 | """ |
|
32 | 32 | |
|
33 | 33 | # Stdlib imports |
|
34 | 34 | import optparse |
|
35 | 35 | import os |
|
36 | 36 | import sys |
|
37 | 37 | |
|
38 | 38 | # Third-party modules. |
|
39 | 39 | import pexpect |
|
40 | 40 | |
|
41 | 41 | # Global usage strings, to avoid indentation issues when typing it below. |
|
42 | 42 | USAGE = """ |
|
43 | 43 | Interactive script runner, type: %s |
|
44 | 44 | |
|
45 | 45 | runner [opts] script_name |
|
46 | 46 | """ |
|
47 | 47 | |
|
48 | 48 | # The generic runner class |
|
49 | 49 | class InteractiveRunner(object): |
|
50 | 50 | """Class to run a sequence of commands through an interactive program.""" |
|
51 | 51 | |
|
52 | 52 | def __init__(self,program,prompts,args=None): |
|
53 | 53 | """Construct a runner. |
|
54 | 54 | |
|
55 | 55 | Inputs: |
|
56 | 56 | |
|
57 | 57 | - program: command to execute the given program. |
|
58 | 58 | |
|
59 | 59 | - prompts: a list of patterns to match as valid prompts, in the |
|
60 | 60 | format used by pexpect. This basically means that it can be either |
|
61 | 61 | a string (to be compiled as a regular expression) or a list of such |
|
62 | 62 | (it must be a true list, as pexpect does type checks). |
|
63 | 63 | |
|
64 | 64 | If more than one prompt is given, the first is treated as the main |
|
65 | 65 | program prompt and the others as 'continuation' prompts, like |
|
66 | 66 | python's. This means that blank lines in the input source are |
|
67 | 67 | ommitted when the first prompt is matched, but are NOT ommitted when |
|
68 | 68 | the continuation one matches, since this is how python signals the |
|
69 | 69 | end of multiline input interactively. |
|
70 | 70 | |
|
71 | 71 | Optional inputs: |
|
72 | 72 | |
|
73 | 73 | - args(None): optional list of strings to pass as arguments to the |
|
74 | 74 | child program. |
|
75 | 75 | |
|
76 | 76 | Public members not parameterized in the constructor: |
|
77 | 77 | |
|
78 | 78 | - delaybeforesend(0): Newer versions of pexpect have a delay before |
|
79 | 79 | sending each new input. For our purposes here, it's typically best |
|
80 | 80 | to just set this to zero, but if you encounter reliability problems |
|
81 | 81 | or want an interactive run to pause briefly at each prompt, just |
|
82 | 82 | increase this value (it is measured in seconds). Note that this |
|
83 | 83 | variable is not honored at all by older versions of pexpect. |
|
84 | 84 | """ |
|
85 | 85 | |
|
86 | 86 | self.program = program |
|
87 | 87 | self.prompts = prompts |
|
88 | 88 | if args is None: args = [] |
|
89 | 89 | self.args = args |
|
90 | 90 | # Other public members which we don't make as parameters, but which |
|
91 | 91 | # users may occasionally want to tweak |
|
92 | 92 | self.delaybeforesend = 0 |
|
93 | 93 | |
|
94 | 94 | def run_file(self,fname,interact=False): |
|
95 | 95 | """Run the given file interactively. |
|
96 | 96 | |
|
97 | 97 | Inputs: |
|
98 | 98 | |
|
99 | 99 | -fname: name of the file to execute. |
|
100 | 100 | |
|
101 | 101 | See the run_source docstring for the meaning of the optional |
|
102 | 102 | arguments.""" |
|
103 | 103 | |
|
104 | 104 | fobj = open(fname,'r') |
|
105 | 105 | try: |
|
106 | 106 | self.run_source(fobj,interact) |
|
107 | 107 | finally: |
|
108 | 108 | fobj.close() |
|
109 | 109 | |
|
110 | 110 | def run_source(self,source,interact=False): |
|
111 | 111 | """Run the given source code interactively. |
|
112 | 112 | |
|
113 | 113 | Inputs: |
|
114 | 114 | |
|
115 | 115 | - source: a string of code to be executed, or an open file object we |
|
116 | 116 | can iterate over. |
|
117 | 117 | |
|
118 | 118 | Optional inputs: |
|
119 | 119 | |
|
120 | 120 | - interact(False): if true, start to interact with the running |
|
121 | 121 | program at the end of the script. Otherwise, just exit. |
|
122 | 122 | """ |
|
123 | 123 | |
|
124 | 124 | # if the source is a string, chop it up in lines so we can iterate |
|
125 | 125 | # over it just as if it were an open file. |
|
126 | 126 | if not isinstance(source,file): |
|
127 | 127 | source = source.splitlines(True) |
|
128 | 128 | |
|
129 | 129 | # grab the true write method of stdout, in case anything later |
|
130 | 130 | # reassigns sys.stdout, so that we really are writing to the true |
|
131 | 131 | # stdout and not to something else. We also normalize all strings we |
|
132 | 132 | # write to use the native OS line separators. |
|
133 | 133 | linesep = os.linesep |
|
134 | 134 | stdwrite = sys.stdout.write |
|
135 | 135 | write = lambda s: stdwrite(s.replace('\r\n',linesep)) |
|
136 | 136 | |
|
137 | 137 | c = pexpect.spawn(self.program,self.args,timeout=None) |
|
138 | 138 | c.delaybeforesend = self.delaybeforesend |
|
139 | 139 | |
|
140 | 140 | # pexpect hard-codes the terminal size as (24,80) (rows,columns). |
|
141 | 141 | # This causes problems because any line longer than 80 characters gets |
|
142 | 142 | # completely overwrapped on the printed outptut (even though |
|
143 | 143 | # internally the code runs fine). We reset this to 99 rows X 200 |
|
144 | 144 | # columns (arbitrarily chosen), which should avoid problems in all |
|
145 | 145 | # reasonable cases. |
|
146 | 146 | c.setwinsize(99,200) |
|
147 | 147 | |
|
148 | 148 | prompts = c.compile_pattern_list(self.prompts) |
|
149 | 149 | |
|
150 | 150 | prompt_idx = c.expect_list(prompts) |
|
151 | 151 | # Flag whether the script ends normally or not, to know whether we can |
|
152 | 152 | # do anything further with the underlying process. |
|
153 | 153 | end_normal = True |
|
154 | 154 | for cmd in source: |
|
155 | 155 | # skip blank lines for all matches to the 'main' prompt, while the |
|
156 | 156 | # secondary prompts do not |
|
157 | 157 | if prompt_idx==0 and \ |
|
158 | 158 | (cmd.isspace() or cmd.lstrip().startswith('#')): |
|
159 | 159 | print cmd, |
|
160 | 160 | continue |
|
161 | 161 | |
|
162 | 162 | write(c.after) |
|
163 | 163 | c.send(cmd) |
|
164 | 164 | try: |
|
165 | 165 | prompt_idx = c.expect_list(prompts) |
|
166 | 166 | except pexpect.EOF: |
|
167 | 167 | # this will happen if the child dies unexpectedly |
|
168 | 168 | write(c.before) |
|
169 | 169 | end_normal = False |
|
170 | 170 | break |
|
171 | 171 | write(c.before) |
|
172 | 172 | |
|
173 | 173 | if end_normal: |
|
174 | 174 | if interact: |
|
175 | 175 | c.send('\n') |
|
176 | 176 | print '<< Starting interactive mode >>', |
|
177 | 177 | try: |
|
178 | 178 | c.interact() |
|
179 | 179 | except OSError: |
|
180 | 180 | # This is what fires when the child stops. Simply print a |
|
181 | 181 | # newline so the system prompt is aligned. The extra |
|
182 | 182 | # space is there to make sure it gets printed, otherwise |
|
183 | 183 | # OS buffering sometimes just suppresses it. |
|
184 | 184 | write(' \n') |
|
185 | 185 | sys.stdout.flush() |
|
186 | 186 | else: |
|
187 | 187 | c.close() |
|
188 | 188 | else: |
|
189 | 189 | if interact: |
|
190 | 190 | e="Further interaction is not possible: child process is dead." |
|
191 | 191 | print >> sys.stderr, e |
|
192 | 192 | |
|
193 | 193 | def main(self,argv=None): |
|
194 | 194 | """Run as a command-line script.""" |
|
195 | 195 | |
|
196 | 196 | parser = optparse.OptionParser(usage=USAGE % self.__class__.__name__) |
|
197 | 197 | newopt = parser.add_option |
|
198 | 198 | newopt('-i','--interact',action='store_true',default=False, |
|
199 | 199 | help='Interact with the program after the script is run.') |
|
200 | 200 | |
|
201 | 201 | opts,args = parser.parse_args(argv) |
|
202 | 202 | |
|
203 | 203 | if len(args) != 1: |
|
204 | 204 | print >> sys.stderr,"You must supply exactly one file to run." |
|
205 | 205 | sys.exit(1) |
|
206 | 206 | |
|
207 | 207 | self.run_file(args[0],opts.interact) |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | # Specific runners for particular programs |
|
211 | 211 | class IPythonRunner(InteractiveRunner): |
|
212 | 212 | """Interactive IPython runner. |
|
213 | 213 | |
|
214 | 214 | This initalizes IPython in 'nocolor' mode for simplicity. This lets us |
|
215 | 215 | avoid having to write a regexp that matches ANSI sequences, though pexpect |
|
216 | 216 | does support them. If anyone contributes patches for ANSI color support, |
|
217 | 217 | they will be welcome. |
|
218 | 218 | |
|
219 | 219 | It also sets the prompts manually, since the prompt regexps for |
|
220 | 220 | pexpect need to be matched to the actual prompts, so user-customized |
|
221 | 221 | prompts would break this. |
|
222 | 222 | """ |
|
223 | 223 | |
|
224 | 224 | def __init__(self,program = 'ipython',args=None): |
|
225 | 225 | """New runner, optionally passing the ipython command to use.""" |
|
226 | 226 | |
|
227 | 227 | args0 = ['-colors','NoColor', |
|
228 | 228 | '-pi1','In [\\#]: ', |
|
229 | 229 | '-pi2',' .\\D.: '] |
|
230 | 230 | if args is None: args = args0 |
|
231 | 231 | else: args = args0 + args |
|
232 | 232 | prompts = [r'In \[\d+\]: ',r' \.*: '] |
|
233 | 233 | InteractiveRunner.__init__(self,program,prompts,args) |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | class PythonRunner(InteractiveRunner): |
|
237 | 237 | """Interactive Python runner.""" |
|
238 | 238 | |
|
239 | 239 | def __init__(self,program='python',args=None): |
|
240 | 240 | """New runner, optionally passing the python command to use.""" |
|
241 | 241 | |
|
242 | 242 | prompts = [r'>>> ',r'\.\.\. '] |
|
243 | 243 | InteractiveRunner.__init__(self,program,prompts,args) |
|
244 | 244 | |
|
245 | 245 | |
|
246 | 246 | class SAGERunner(InteractiveRunner): |
|
247 | 247 | """Interactive SAGE runner. |
|
248 | 248 | |
|
249 | 249 | WARNING: this runner only works if you manually configure your SAGE copy |
|
250 | 250 | to use 'colors NoColor' in the ipythonrc config file, since currently the |
|
251 | 251 | prompt matching regexp does not identify color sequences.""" |
|
252 | 252 | |
|
253 | 253 | def __init__(self,program='sage',args=None): |
|
254 | 254 | """New runner, optionally passing the sage command to use.""" |
|
255 | 255 | |
|
256 | 256 | prompts = ['sage: ',r'\s*\.\.\. '] |
|
257 | 257 | InteractiveRunner.__init__(self,program,prompts,args) |
|
258 | 258 | |
|
259 | 259 | # Global usage string, to avoid indentation issues if typed in a function def. |
|
260 | 260 | MAIN_USAGE = """ |
|
261 | 261 | %prog [options] file_to_run |
|
262 | 262 | |
|
263 | 263 | This is an interface to the various interactive runners available in this |
|
264 | 264 | module. If you want to pass specific options to one of the runners, you need |
|
265 | 265 | to first terminate the main options with a '--', and then provide the runner's |
|
266 | 266 | options. For example: |
|
267 | 267 | |
|
268 | 268 | irunner.py --python -- --help |
|
269 | 269 | |
|
270 | 270 | will pass --help to the python runner. Similarly, |
|
271 | 271 | |
|
272 | 272 | irunner.py --ipython -- --interact script.ipy |
|
273 | 273 | |
|
274 | 274 | will run the script.ipy file under the IPython runner, and then will start to |
|
275 | 275 | interact with IPython at the end of the script (instead of exiting). |
|
276 | 276 | |
|
277 | 277 | The already implemented runners are listed below; adding one for a new program |
|
278 | 278 | is a trivial task, see the source for examples. |
|
279 | 279 | |
|
280 | 280 | WARNING: the SAGE runner only works if you manually configure your SAGE copy |
|
281 | 281 | to use 'colors NoColor' in the ipythonrc config file, since currently the |
|
282 | 282 | prompt matching regexp does not identify color sequences. |
|
283 | 283 | """ |
|
284 | 284 | |
|
285 | 285 | def main(): |
|
286 | 286 | """Run as a command-line script.""" |
|
287 | 287 | |
|
288 | 288 | parser = optparse.OptionParser(usage=MAIN_USAGE) |
|
289 | 289 | newopt = parser.add_option |
|
290 | 290 | parser.set_defaults(mode='ipython') |
|
291 | 291 | newopt('--ipython',action='store_const',dest='mode',const='ipython', |
|
292 | 292 | help='IPython interactive runner (default).') |
|
293 | 293 | newopt('--python',action='store_const',dest='mode',const='python', |
|
294 | 294 | help='Python interactive runner.') |
|
295 | 295 | newopt('--sage',action='store_const',dest='mode',const='sage', |
|
296 | 296 | help='SAGE interactive runner.') |
|
297 | 297 | |
|
298 | 298 | opts,args = parser.parse_args() |
|
299 | 299 | runners = dict(ipython=IPythonRunner, |
|
300 | 300 | python=PythonRunner, |
|
301 | 301 | sage=SAGERunner) |
|
302 | runners[opts.mode]().main(args) | |
|
302 | ||
|
303 | try: | |
|
304 | ext = os.path.splitext(args[0]) | |
|
305 | except IndexError: | |
|
306 | ext = '' | |
|
307 | modes = {'.ipy':'ipython', | |
|
308 | '.py':'python', | |
|
309 | '.sage':'sage'} | |
|
310 | mode = modes.get(ext,opts.mode) | |
|
311 | runners[mode]().main(args) | |
|
303 | 312 | |
|
304 | 313 | if __name__ == '__main__': |
|
305 | 314 | main() |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now