##// END OF EJS Templates
Fix more failures due to direct shell access in magics.
Fernando Perez -
Show More
@@ -1,982 +1,982 b''
1 1 """ History related magics and functionality """
2 2 #-----------------------------------------------------------------------------
3 3 # Copyright (C) 2010-2011 The IPython Development Team.
4 4 #
5 5 # Distributed under the terms of the BSD License.
6 6 #
7 7 # The full license is in the file COPYING.txt, distributed with this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # Stdlib imports
16 16 import atexit
17 17 import datetime
18 18 from io import open as io_open
19 19 import os
20 20 import re
21 21 try:
22 22 import sqlite3
23 23 except ImportError:
24 24 sqlite3 = None
25 25 import threading
26 26
27 27 # Our own packages
28 28 from IPython.core.error import StdinNotImplementedError
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.external.decorator import decorator
31 31 from IPython.testing.skipdoctest import skip_doctest
32 32 from IPython.utils import io
33 33 from IPython.utils.path import locate_profile
34 34 from IPython.utils.traitlets import Bool, Dict, Instance, Integer, List, Unicode
35 35 from IPython.utils.warn import warn
36 36
37 37 #-----------------------------------------------------------------------------
38 38 # Classes and functions
39 39 #-----------------------------------------------------------------------------
40 40
41 41 class DummyDB(object):
42 42 """Dummy DB that will act as a black hole for history.
43 43
44 44 Only used in the absence of sqlite"""
45 45 def execute(*args, **kwargs):
46 46 return []
47 47
48 48 def commit(self, *args, **kwargs):
49 49 pass
50 50
51 51 def __enter__(self, *args, **kwargs):
52 52 pass
53 53
54 54 def __exit__(self, *args, **kwargs):
55 55 pass
56 56
57 57
58 58 @decorator
59 59 def needs_sqlite(f,*a,**kw):
60 60 """return an empty list in the absence of sqlite"""
61 61 if sqlite3 is None:
62 62 return []
63 63 else:
64 64 return f(*a,**kw)
65 65
66 66
67 67 class HistoryAccessor(Configurable):
68 68 """Access the history database without adding to it.
69 69
70 70 This is intended for use by standalone history tools. IPython shells use
71 71 HistoryManager, below, which is a subclass of this."""
72 72
73 73 # String holding the path to the history file
74 74 hist_file = Unicode(config=True,
75 75 help="""Path to file to use for SQLite history database.
76 76
77 77 By default, IPython will put the history database in the IPython profile
78 78 directory. If you would rather share one history among profiles,
79 79 you ca set this value in each, so that they are consistent.
80 80
81 81 Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts.
82 82 If you see IPython hanging, try setting this to something on a local disk,
83 83 e.g::
84 84
85 85 ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
86 86
87 87 """)
88 88
89 89 # The SQLite database
90 90 if sqlite3:
91 91 db = Instance(sqlite3.Connection)
92 92 else:
93 93 db = Instance(DummyDB)
94 94
95 95 def __init__(self, profile='default', hist_file=u'', config=None, **traits):
96 96 """Create a new history accessor.
97 97
98 98 Parameters
99 99 ----------
100 100 profile : str
101 101 The name of the profile from which to open history.
102 102 hist_file : str
103 103 Path to an SQLite history database stored by IPython. If specified,
104 104 hist_file overrides profile.
105 105 config :
106 106 Config object. hist_file can also be set through this.
107 107 """
108 108 # We need a pointer back to the shell for various tasks.
109 109 super(HistoryAccessor, self).__init__(config=config, **traits)
110 110 # defer setting hist_file from kwarg until after init,
111 111 # otherwise the default kwarg value would clobber any value
112 112 # set by config
113 113 if hist_file:
114 114 self.hist_file = hist_file
115 115
116 116 if self.hist_file == u'':
117 117 # No one has set the hist_file, yet.
118 118 self.hist_file = self._get_hist_file_name(profile)
119 119
120 120 if sqlite3 is None:
121 121 warn("IPython History requires SQLite, your history will not be saved\n")
122 122 self.db = DummyDB()
123 123 return
124 124
125 125 try:
126 126 self.init_db()
127 127 except sqlite3.DatabaseError:
128 128 if os.path.isfile(self.hist_file):
129 129 # Try to move the file out of the way
130 130 base,ext = os.path.splitext(self.hist_file)
131 131 newpath = base + '-corrupt' + ext
132 132 os.rename(self.hist_file, newpath)
133 133 print("ERROR! History file wasn't a valid SQLite database.",
134 134 "It was moved to %s" % newpath, "and a new file created.")
135 135 self.init_db()
136 136 else:
137 137 # The hist_file is probably :memory: or something else.
138 138 raise
139 139
140 140 def _get_hist_file_name(self, profile='default'):
141 141 """Find the history file for the given profile name.
142 142
143 143 This is overridden by the HistoryManager subclass, to use the shell's
144 144 active profile.
145 145
146 146 Parameters
147 147 ----------
148 148 profile : str
149 149 The name of a profile which has a history file.
150 150 """
151 151 return os.path.join(locate_profile(profile), 'history.sqlite')
152 152
153 153 def init_db(self):
154 154 """Connect to the database, and create tables if necessary."""
155 155 # use detect_types so that timestamps return datetime objects
156 156 self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
157 157 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
158 158 primary key autoincrement, start timestamp,
159 159 end timestamp, num_cmds integer, remark text)""")
160 160 self.db.execute("""CREATE TABLE IF NOT EXISTS history
161 161 (session integer, line integer, source text, source_raw text,
162 162 PRIMARY KEY (session, line))""")
163 163 # Output history is optional, but ensure the table's there so it can be
164 164 # enabled later.
165 165 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
166 166 (session integer, line integer, output text,
167 167 PRIMARY KEY (session, line))""")
168 168 self.db.commit()
169 169
170 170 def writeout_cache(self):
171 171 """Overridden by HistoryManager to dump the cache before certain
172 172 database lookups."""
173 173 pass
174 174
175 175 ## -------------------------------
176 176 ## Methods for retrieving history:
177 177 ## -------------------------------
178 178 def _run_sql(self, sql, params, raw=True, output=False):
179 179 """Prepares and runs an SQL query for the history database.
180 180
181 181 Parameters
182 182 ----------
183 183 sql : str
184 184 Any filtering expressions to go after SELECT ... FROM ...
185 185 params : tuple
186 186 Parameters passed to the SQL query (to replace "?")
187 187 raw, output : bool
188 188 See :meth:`get_range`
189 189
190 190 Returns
191 191 -------
192 192 Tuples as :meth:`get_range`
193 193 """
194 194 toget = 'source_raw' if raw else 'source'
195 195 sqlfrom = "history"
196 196 if output:
197 197 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
198 198 toget = "history.%s, output_history.output" % toget
199 199 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
200 200 (toget, sqlfrom) + sql, params)
201 201 if output: # Regroup into 3-tuples, and parse JSON
202 202 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
203 203 return cur
204 204
205 205 @needs_sqlite
206 206 def get_session_info(self, session=0):
207 207 """get info about a session
208 208
209 209 Parameters
210 210 ----------
211 211
212 212 session : int
213 213 Session number to retrieve. The current session is 0, and negative
214 214 numbers count back from current session, so -1 is previous session.
215 215
216 216 Returns
217 217 -------
218 218
219 219 (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode])
220 220
221 221 Sessions that are running or did not exit cleanly will have `end=None`
222 222 and `num_cmds=None`.
223 223
224 224 """
225 225
226 226 if session <= 0:
227 227 session += self.session_number
228 228
229 229 query = "SELECT * from sessions where session == ?"
230 230 return self.db.execute(query, (session,)).fetchone()
231 231
232 232 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
233 233 """Get the last n lines from the history database.
234 234
235 235 Parameters
236 236 ----------
237 237 n : int
238 238 The number of lines to get
239 239 raw, output : bool
240 240 See :meth:`get_range`
241 241 include_latest : bool
242 242 If False (default), n+1 lines are fetched, and the latest one
243 243 is discarded. This is intended to be used where the function
244 244 is called by a user command, which it should not return.
245 245
246 246 Returns
247 247 -------
248 248 Tuples as :meth:`get_range`
249 249 """
250 250 self.writeout_cache()
251 251 if not include_latest:
252 252 n += 1
253 253 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
254 254 (n,), raw=raw, output=output)
255 255 if not include_latest:
256 256 return reversed(list(cur)[1:])
257 257 return reversed(list(cur))
258 258
259 259 def search(self, pattern="*", raw=True, search_raw=True,
260 260 output=False):
261 261 """Search the database using unix glob-style matching (wildcards
262 262 * and ?).
263 263
264 264 Parameters
265 265 ----------
266 266 pattern : str
267 267 The wildcarded pattern to match when searching
268 268 search_raw : bool
269 269 If True, search the raw input, otherwise, the parsed input
270 270 raw, output : bool
271 271 See :meth:`get_range`
272 272
273 273 Returns
274 274 -------
275 275 Tuples as :meth:`get_range`
276 276 """
277 277 tosearch = "source_raw" if search_raw else "source"
278 278 if output:
279 279 tosearch = "history." + tosearch
280 280 self.writeout_cache()
281 281 return self._run_sql("WHERE %s GLOB ?" % tosearch, (pattern,),
282 282 raw=raw, output=output)
283 283
284 284 def get_range(self, session, start=1, stop=None, raw=True,output=False):
285 285 """Retrieve input by session.
286 286
287 287 Parameters
288 288 ----------
289 289 session : int
290 290 Session number to retrieve.
291 291 start : int
292 292 First line to retrieve.
293 293 stop : int
294 294 End of line range (excluded from output itself). If None, retrieve
295 295 to the end of the session.
296 296 raw : bool
297 297 If True, return untranslated input
298 298 output : bool
299 299 If True, attempt to include output. This will be 'real' Python
300 300 objects for the current session, or text reprs from previous
301 301 sessions if db_log_output was enabled at the time. Where no output
302 302 is found, None is used.
303 303
304 304 Returns
305 305 -------
306 306 An iterator over the desired lines. Each line is a 3-tuple, either
307 307 (session, line, input) if output is False, or
308 308 (session, line, (input, output)) if output is True.
309 309 """
310 310 if stop:
311 311 lineclause = "line >= ? AND line < ?"
312 312 params = (session, start, stop)
313 313 else:
314 314 lineclause = "line>=?"
315 315 params = (session, start)
316 316
317 317 return self._run_sql("WHERE session==? AND %s""" % lineclause,
318 318 params, raw=raw, output=output)
319 319
320 320 def get_range_by_str(self, rangestr, raw=True, output=False):
321 321 """Get lines of history from a string of ranges, as used by magic
322 322 commands %hist, %save, %macro, etc.
323 323
324 324 Parameters
325 325 ----------
326 326 rangestr : str
327 327 A string specifying ranges, e.g. "5 ~2/1-4". See
328 328 :func:`magic_history` for full details.
329 329 raw, output : bool
330 330 As :meth:`get_range`
331 331
332 332 Returns
333 333 -------
334 334 Tuples as :meth:`get_range`
335 335 """
336 336 for sess, s, e in extract_hist_ranges(rangestr):
337 337 for line in self.get_range(sess, s, e, raw=raw, output=output):
338 338 yield line
339 339
340 340
341 341 class HistoryManager(HistoryAccessor):
342 342 """A class to organize all history-related functionality in one place.
343 343 """
344 344 # Public interface
345 345
346 346 # An instance of the IPython shell we are attached to
347 347 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
348 348 # Lists to hold processed and raw history. These start with a blank entry
349 349 # so that we can index them starting from 1
350 350 input_hist_parsed = List([""])
351 351 input_hist_raw = List([""])
352 352 # A list of directories visited during session
353 353 dir_hist = List()
354 354 def _dir_hist_default(self):
355 355 try:
356 356 return [os.getcwdu()]
357 357 except OSError:
358 358 return []
359 359
360 360 # A dict of output history, keyed with ints from the shell's
361 361 # execution count.
362 362 output_hist = Dict()
363 363 # The text/plain repr of outputs.
364 364 output_hist_reprs = Dict()
365 365
366 366 # The number of the current session in the history database
367 367 session_number = Integer()
368 368 # Should we log output to the database? (default no)
369 369 db_log_output = Bool(False, config=True)
370 370 # Write to database every x commands (higher values save disk access & power)
371 371 # Values of 1 or less effectively disable caching.
372 372 db_cache_size = Integer(0, config=True)
373 373 # The input and output caches
374 374 db_input_cache = List()
375 375 db_output_cache = List()
376 376
377 377 # History saving in separate thread
378 378 save_thread = Instance('IPython.core.history.HistorySavingThread')
379 379 try: # Event is a function returning an instance of _Event...
380 380 save_flag = Instance(threading._Event)
381 381 except AttributeError: # ...until Python 3.3, when it's a class.
382 382 save_flag = Instance(threading.Event)
383 383
384 384 # Private interface
385 385 # Variables used to store the three last inputs from the user. On each new
386 386 # history update, we populate the user's namespace with these, shifted as
387 387 # necessary.
388 388 _i00 = Unicode(u'')
389 389 _i = Unicode(u'')
390 390 _ii = Unicode(u'')
391 391 _iii = Unicode(u'')
392 392
393 393 # A regex matching all forms of the exit command, so that we don't store
394 394 # them in the history (it's annoying to rewind the first entry and land on
395 395 # an exit call).
396 396 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
397 397
398 398 def __init__(self, shell=None, config=None, **traits):
399 399 """Create a new history manager associated with a shell instance.
400 400 """
401 401 # We need a pointer back to the shell for various tasks.
402 402 super(HistoryManager, self).__init__(shell=shell, config=config,
403 403 **traits)
404 404 self.save_flag = threading.Event()
405 405 self.db_input_cache_lock = threading.Lock()
406 406 self.db_output_cache_lock = threading.Lock()
407 407 if self.hist_file != ':memory:':
408 408 self.save_thread = HistorySavingThread(self)
409 409 self.save_thread.start()
410 410
411 411 self.new_session()
412 412
413 413 def _get_hist_file_name(self, profile=None):
414 414 """Get default history file name based on the Shell's profile.
415 415
416 416 The profile parameter is ignored, but must exist for compatibility with
417 417 the parent class."""
418 418 profile_dir = self.shell.profile_dir.location
419 419 return os.path.join(profile_dir, 'history.sqlite')
420 420
421 421 @needs_sqlite
422 422 def new_session(self, conn=None):
423 423 """Get a new session number."""
424 424 if conn is None:
425 425 conn = self.db
426 426
427 427 with conn:
428 428 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
429 429 NULL, "") """, (datetime.datetime.now(),))
430 430 self.session_number = cur.lastrowid
431 431
432 432 def end_session(self):
433 433 """Close the database session, filling in the end time and line count."""
434 434 self.writeout_cache()
435 435 with self.db:
436 436 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
437 437 session==?""", (datetime.datetime.now(),
438 438 len(self.input_hist_parsed)-1, self.session_number))
439 439 self.session_number = 0
440 440
441 441 def name_session(self, name):
442 442 """Give the current session a name in the history database."""
443 443 with self.db:
444 444 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
445 445 (name, self.session_number))
446 446
447 447 def reset(self, new_session=True):
448 448 """Clear the session history, releasing all object references, and
449 449 optionally open a new session."""
450 450 self.output_hist.clear()
451 451 # The directory history can't be completely empty
452 452 self.dir_hist[:] = [os.getcwdu()]
453 453
454 454 if new_session:
455 455 if self.session_number:
456 456 self.end_session()
457 457 self.input_hist_parsed[:] = [""]
458 458 self.input_hist_raw[:] = [""]
459 459 self.new_session()
460 460
461 461 # ------------------------------
462 462 # Methods for retrieving history
463 463 # ------------------------------
464 464 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
465 465 """Get input and output history from the current session. Called by
466 466 get_range, and takes similar parameters."""
467 467 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
468 468
469 469 n = len(input_hist)
470 470 if start < 0:
471 471 start += n
472 472 if not stop or (stop > n):
473 473 stop = n
474 474 elif stop < 0:
475 475 stop += n
476 476
477 477 for i in range(start, stop):
478 478 if output:
479 479 line = (input_hist[i], self.output_hist_reprs.get(i))
480 480 else:
481 481 line = input_hist[i]
482 482 yield (0, i, line)
483 483
484 484 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
485 485 """Retrieve input by session.
486 486
487 487 Parameters
488 488 ----------
489 489 session : int
490 490 Session number to retrieve. The current session is 0, and negative
491 491 numbers count back from current session, so -1 is previous session.
492 492 start : int
493 493 First line to retrieve.
494 494 stop : int
495 495 End of line range (excluded from output itself). If None, retrieve
496 496 to the end of the session.
497 497 raw : bool
498 498 If True, return untranslated input
499 499 output : bool
500 500 If True, attempt to include output. This will be 'real' Python
501 501 objects for the current session, or text reprs from previous
502 502 sessions if db_log_output was enabled at the time. Where no output
503 503 is found, None is used.
504 504
505 505 Returns
506 506 -------
507 507 An iterator over the desired lines. Each line is a 3-tuple, either
508 508 (session, line, input) if output is False, or
509 509 (session, line, (input, output)) if output is True.
510 510 """
511 511 if session <= 0:
512 512 session += self.session_number
513 513 if session==self.session_number: # Current session
514 514 return self._get_range_session(start, stop, raw, output)
515 515 return super(HistoryManager, self).get_range(session, start, stop, raw, output)
516 516
517 517 ## ----------------------------
518 518 ## Methods for storing history:
519 519 ## ----------------------------
520 520 def store_inputs(self, line_num, source, source_raw=None):
521 521 """Store source and raw input in history and create input cache
522 522 variables _i*.
523 523
524 524 Parameters
525 525 ----------
526 526 line_num : int
527 527 The prompt number of this input.
528 528
529 529 source : str
530 530 Python input.
531 531
532 532 source_raw : str, optional
533 533 If given, this is the raw input without any IPython transformations
534 534 applied to it. If not given, ``source`` is used.
535 535 """
536 536 if source_raw is None:
537 537 source_raw = source
538 538 source = source.rstrip('\n')
539 539 source_raw = source_raw.rstrip('\n')
540 540
541 541 # do not store exit/quit commands
542 542 if self._exit_re.match(source_raw.strip()):
543 543 return
544 544
545 545 self.input_hist_parsed.append(source)
546 546 self.input_hist_raw.append(source_raw)
547 547
548 548 with self.db_input_cache_lock:
549 549 self.db_input_cache.append((line_num, source, source_raw))
550 550 # Trigger to flush cache and write to DB.
551 551 if len(self.db_input_cache) >= self.db_cache_size:
552 552 self.save_flag.set()
553 553
554 554 # update the auto _i variables
555 555 self._iii = self._ii
556 556 self._ii = self._i
557 557 self._i = self._i00
558 558 self._i00 = source_raw
559 559
560 560 # hackish access to user namespace to create _i1,_i2... dynamically
561 561 new_i = '_i%s' % line_num
562 562 to_main = {'_i': self._i,
563 563 '_ii': self._ii,
564 564 '_iii': self._iii,
565 565 new_i : self._i00 }
566 566
567 567 self.shell.push(to_main, interactive=False)
568 568
569 569 def store_output(self, line_num):
570 570 """If database output logging is enabled, this saves all the
571 571 outputs from the indicated prompt number to the database. It's
572 572 called by run_cell after code has been executed.
573 573
574 574 Parameters
575 575 ----------
576 576 line_num : int
577 577 The line number from which to save outputs
578 578 """
579 579 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
580 580 return
581 581 output = self.output_hist_reprs[line_num]
582 582
583 583 with self.db_output_cache_lock:
584 584 self.db_output_cache.append((line_num, output))
585 585 if self.db_cache_size <= 1:
586 586 self.save_flag.set()
587 587
588 588 def _writeout_input_cache(self, conn):
589 589 with conn:
590 590 for line in self.db_input_cache:
591 591 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
592 592 (self.session_number,)+line)
593 593
594 594 def _writeout_output_cache(self, conn):
595 595 with conn:
596 596 for line in self.db_output_cache:
597 597 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
598 598 (self.session_number,)+line)
599 599
600 600 @needs_sqlite
601 601 def writeout_cache(self, conn=None):
602 602 """Write any entries in the cache to the database."""
603 603 if conn is None:
604 604 conn = self.db
605 605
606 606 with self.db_input_cache_lock:
607 607 try:
608 608 self._writeout_input_cache(conn)
609 609 except sqlite3.IntegrityError:
610 610 self.new_session(conn)
611 611 print("ERROR! Session/line number was not unique in",
612 612 "database. History logging moved to new session",
613 613 self.session_number)
614 614 try: # Try writing to the new session. If this fails, don't recurse
615 615 self._writeout_input_cache(conn)
616 616 except sqlite3.IntegrityError:
617 617 pass
618 618 finally:
619 619 self.db_input_cache = []
620 620
621 621 with self.db_output_cache_lock:
622 622 try:
623 623 self._writeout_output_cache(conn)
624 624 except sqlite3.IntegrityError:
625 625 print("!! Session/line number for output was not unique",
626 626 "in database. Output will not be stored.")
627 627 finally:
628 628 self.db_output_cache = []
629 629
630 630
631 631 class HistorySavingThread(threading.Thread):
632 632 """This thread takes care of writing history to the database, so that
633 633 the UI isn't held up while that happens.
634 634
635 635 It waits for the HistoryManager's save_flag to be set, then writes out
636 636 the history cache. The main thread is responsible for setting the flag when
637 637 the cache size reaches a defined threshold."""
638 638 daemon = True
639 639 stop_now = False
640 640 def __init__(self, history_manager):
641 641 super(HistorySavingThread, self).__init__()
642 642 self.history_manager = history_manager
643 643 atexit.register(self.stop)
644 644
645 645 @needs_sqlite
646 646 def run(self):
647 647 # We need a separate db connection per thread:
648 648 try:
649 649 self.db = sqlite3.connect(self.history_manager.hist_file)
650 650 while True:
651 651 self.history_manager.save_flag.wait()
652 652 if self.stop_now:
653 653 return
654 654 self.history_manager.save_flag.clear()
655 655 self.history_manager.writeout_cache(self.db)
656 656 except Exception as e:
657 657 print(("The history saving thread hit an unexpected error (%s)."
658 658 "History will not be written to the database.") % repr(e))
659 659
660 660 def stop(self):
661 661 """This can be called from the main thread to safely stop this thread.
662 662
663 663 Note that it does not attempt to write out remaining history before
664 664 exiting. That should be done by calling the HistoryManager's
665 665 end_session method."""
666 666 self.stop_now = True
667 667 self.history_manager.save_flag.set()
668 668 self.join()
669 669
670 670
671 671 # To match, e.g. ~5/8-~2/3
672 672 range_re = re.compile(r"""
673 673 ((?P<startsess>~?\d+)/)?
674 674 (?P<start>\d+) # Only the start line num is compulsory
675 675 ((?P<sep>[\-:])
676 676 ((?P<endsess>~?\d+)/)?
677 677 (?P<end>\d+))?
678 678 $""", re.VERBOSE)
679 679
680 680
681 681 def extract_hist_ranges(ranges_str):
682 682 """Turn a string of history ranges into 3-tuples of (session, start, stop).
683 683
684 684 Examples
685 685 --------
686 686 list(extract_input_ranges("~8/5-~7/4 2"))
687 687 [(-8, 5, None), (-7, 1, 4), (0, 2, 3)]
688 688 """
689 689 for range_str in ranges_str.split():
690 690 rmatch = range_re.match(range_str)
691 691 if not rmatch:
692 692 continue
693 693 start = int(rmatch.group("start"))
694 694 end = rmatch.group("end")
695 695 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
696 696 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
697 697 end += 1
698 698 startsess = rmatch.group("startsess") or "0"
699 699 endsess = rmatch.group("endsess") or startsess
700 700 startsess = int(startsess.replace("~","-"))
701 701 endsess = int(endsess.replace("~","-"))
702 702 assert endsess >= startsess
703 703
704 704 if endsess == startsess:
705 705 yield (startsess, start, end)
706 706 continue
707 707 # Multiple sessions in one range:
708 708 yield (startsess, start, None)
709 709 for sess in range(startsess+1, endsess):
710 710 yield (sess, 1, None)
711 711 yield (endsess, 1, end)
712 712
713 713
714 714 def _format_lineno(session, line):
715 715 """Helper function to format line numbers properly."""
716 716 if session == 0:
717 717 return str(line)
718 718 return "%s#%s" % (session, line)
719 719
720 720
721 721 @skip_doctest
722 722 def magic_history(self, parameter_s = ''):
723 723 """Print input history (_i<n> variables), with most recent last.
724 724
725 725 %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
726 726
727 727 By default, input history is printed without line numbers so it can be
728 728 directly pasted into an editor. Use -n to show them.
729 729
730 730 By default, all input history from the current session is displayed.
731 731 Ranges of history can be indicated using the syntax:
732 732 4 : Line 4, current session
733 733 4-6 : Lines 4-6, current session
734 734 243/1-5: Lines 1-5, session 243
735 735 ~2/7 : Line 7, session 2 before current
736 736 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
737 737 of 6 sessions ago.
738 738 Multiple ranges can be entered, separated by spaces
739 739
740 740 The same syntax is used by %macro, %save, %edit, %rerun
741 741
742 742 Options:
743 743
744 744 -n: print line numbers for each input.
745 745 This feature is only available if numbered prompts are in use.
746 746
747 747 -o: also print outputs for each input.
748 748
749 749 -p: print classic '>>>' python prompts before each input. This is useful
750 750 for making documentation, and in conjunction with -o, for producing
751 751 doctest-ready output.
752 752
753 753 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
754 754
755 755 -t: print the 'translated' history, as IPython understands it. IPython
756 756 filters your input and converts it all into valid Python source before
757 757 executing it (things like magics or aliases are turned into function
758 758 calls, for example). With this option, you'll see the native history
759 759 instead of the user-entered version: '%cd /' will be seen as
760 760 'get_ipython().magic("%cd /")' instead of '%cd /'.
761 761
762 762 -g: treat the arg as a pattern to grep for in (full) history.
763 763 This includes the saved history (almost all commands ever written).
764 764 Use '%hist -g' to show full saved history (may be very long).
765 765
766 766 -l: get the last n lines from all sessions. Specify n as a single arg, or
767 767 the default is the last 10 lines.
768 768
769 769 -f FILENAME: instead of printing the output to the screen, redirect it to
770 770 the given file. The file is always overwritten, though *when it can*,
771 771 IPython asks for confirmation first. In particular, running the command
772 772 "history -f FILENAME" from the IPython Notebook interface will replace
773 773 FILENAME even if it already exists *without* confirmation.
774 774
775 775 Examples
776 776 --------
777 777 ::
778 778
779 779 In [6]: %hist -n 4-6
780 780 4:a = 12
781 781 5:print a**2
782 782 6:%hist -n 4-6
783 783
784 784 """
785 785
786 786 if not self.shell.displayhook.do_full_cache:
787 787 print('This feature is only available if numbered prompts are in use.')
788 788 return
789 789 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
790 790
791 791 # For brevity
792 792 history_manager = self.shell.history_manager
793 793
794 794 def _format_lineno(session, line):
795 795 """Helper function to format line numbers properly."""
796 796 if session in (0, history_manager.session_number):
797 797 return str(line)
798 798 return "%s/%s" % (session, line)
799 799
800 800 # Check if output to specific file was requested.
801 801 try:
802 802 outfname = opts['f']
803 803 except KeyError:
804 804 outfile = io.stdout # default
805 805 # We don't want to close stdout at the end!
806 806 close_at_end = False
807 807 else:
808 808 if os.path.exists(outfname):
809 809 try:
810 810 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
811 811 except StdinNotImplementedError:
812 812 ans = True
813 813 if not ans:
814 814 print('Aborting.')
815 815 return
816 816 print("Overwriting file.")
817 817 outfile = io_open(outfname, 'w', encoding='utf-8')
818 818 close_at_end = True
819 819
820 820 print_nums = 'n' in opts
821 821 get_output = 'o' in opts
822 822 pyprompts = 'p' in opts
823 823 # Raw history is the default
824 824 raw = not('t' in opts)
825 825
826 826 default_length = 40
827 827 pattern = None
828 828
829 829 if 'g' in opts: # Glob search
830 830 pattern = "*" + args + "*" if args else "*"
831 831 hist = history_manager.search(pattern, raw=raw, output=get_output)
832 832 print_nums = True
833 833 elif 'l' in opts: # Get 'tail'
834 834 try:
835 835 n = int(args)
836 836 except ValueError, IndexError:
837 837 n = 10
838 838 hist = history_manager.get_tail(n, raw=raw, output=get_output)
839 839 else:
840 840 if args: # Get history by ranges
841 841 hist = history_manager.get_range_by_str(args, raw, get_output)
842 842 else: # Just get history for the current session
843 843 hist = history_manager.get_range(raw=raw, output=get_output)
844 844
845 845 # We could be displaying the entire history, so let's not try to pull it
846 846 # into a list in memory. Anything that needs more space will just misalign.
847 847 width = 4
848 848
849 849 for session, lineno, inline in hist:
850 850 # Print user history with tabs expanded to 4 spaces. The GUI clients
851 851 # use hard tabs for easier usability in auto-indented code, but we want
852 852 # to produce PEP-8 compliant history for safe pasting into an editor.
853 853 if get_output:
854 854 inline, output = inline
855 855 inline = inline.expandtabs(4).rstrip()
856 856
857 857 multiline = "\n" in inline
858 858 line_sep = '\n' if multiline else ' '
859 859 if print_nums:
860 860 print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width),
861 861 line_sep), file=outfile, end=u'')
862 862 if pyprompts:
863 863 print(u">>> ", end=u"", file=outfile)
864 864 if multiline:
865 865 inline = "\n... ".join(inline.splitlines()) + "\n..."
866 866 print(inline, file=outfile)
867 867 if get_output and output:
868 868 print(output, file=outfile)
869 869
870 870 if close_at_end:
871 871 outfile.close()
872 872
873 873
874 874 def magic_rep(self, arg):
875 875 r"""Repeat a command, or get command to input line for editing.
876 876
877 877 %recall and %rep are equivalent.
878 878
879 879 - %recall (no arguments):
880 880
881 881 Place a string version of last computation result (stored in the special '_'
882 882 variable) to the next input prompt. Allows you to create elaborate command
883 883 lines without using copy-paste::
884 884
885 885 In[1]: l = ["hei", "vaan"]
886 886 In[2]: "".join(l)
887 887 Out[2]: heivaan
888 888 In[3]: %rep
889 889 In[4]: heivaan_ <== cursor blinking
890 890
891 891 %recall 45
892 892
893 893 Place history line 45 on the next input prompt. Use %hist to find
894 894 out the number.
895 895
896 896 %recall 1-4
897 897
898 898 Combine the specified lines into one cell, and place it on the next
899 899 input prompt. See %history for the slice syntax.
900 900
901 901 %recall foo+bar
902 902
903 903 If foo+bar can be evaluated in the user namespace, the result is
904 904 placed at the next input prompt. Otherwise, the history is searched
905 905 for lines which contain that substring, and the most recent one is
906 906 placed at the next input prompt.
907 907 """
908 908 if not arg: # Last output
909 909 self.shell.set_next_input(str(self.shell.user_ns["_"]))
910 910 return
911 911 # Get history range
912 histlines = self.history_manager.get_range_by_str(arg)
912 histlines = self.shell.history_manager.get_range_by_str(arg)
913 913 cmd = "\n".join(x[2] for x in histlines)
914 914 if cmd:
915 915 self.shell.set_next_input(cmd.rstrip())
916 916 return
917 917
918 918 try: # Variable in user namespace
919 919 cmd = str(eval(arg, self.shell.user_ns))
920 920 except Exception: # Search for term in history
921 histlines = self.history_manager.search("*"+arg+"*")
921 histlines = self.shell.history_manager.search("*"+arg+"*")
922 922 for h in reversed([x[2] for x in histlines]):
923 923 if 'rep' in h:
924 924 continue
925 925 self.shell.set_next_input(h.rstrip())
926 926 return
927 927 else:
928 928 self.shell.set_next_input(cmd.rstrip())
929 929 print("Couldn't evaluate or find in history:", arg)
930 930
931 931
932 932 def magic_rerun(self, parameter_s=''):
933 933 """Re-run previous input
934 934
935 935 By default, you can specify ranges of input history to be repeated
936 936 (as with %history). With no arguments, it will repeat the last line.
937 937
938 938 Options:
939 939
940 940 -l <n> : Repeat the last n lines of input, not including the
941 941 current command.
942 942
943 943 -g foo : Repeat the most recent line which contains foo
944 944 """
945 945 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
946 946 if "l" in opts: # Last n lines
947 947 n = int(opts['l'])
948 hist = self.history_manager.get_tail(n)
948 hist = self.shell.history_manager.get_tail(n)
949 949 elif "g" in opts: # Search
950 950 p = "*"+opts['g']+"*"
951 hist = list(self.history_manager.search(p))
951 hist = list(self.shell.history_manager.search(p))
952 952 for l in reversed(hist):
953 953 if "rerun" not in l[2]:
954 954 hist = [l] # The last match which isn't a %rerun
955 955 break
956 956 else:
957 957 hist = [] # No matches except %rerun
958 958 elif args: # Specify history ranges
959 hist = self.history_manager.get_range_by_str(args)
959 hist = self.shell.history_manager.get_range_by_str(args)
960 960 else: # Last line
961 hist = self.history_manager.get_tail(1)
961 hist = self.shell.history_manager.get_tail(1)
962 962 hist = [x[2] for x in hist]
963 963 if not hist:
964 964 print("No lines in history match specification")
965 965 return
966 966 histlines = "\n".join(hist)
967 967 print("=== Executing: ===")
968 968 print(histlines)
969 969 print("=== Output: ===")
970 self.run_cell("\n".join(hist), store_history=False)
970 self.shell.run_cell("\n".join(hist), store_history=False)
971 971
972 972
973 973 def init_ipython(ip):
974 974 ip.define_magic("rep", magic_rep)
975 975 ip.define_magic("recall", magic_rep)
976 976 ip.define_magic("rerun", magic_rerun)
977 977 ip.define_magic("hist", magic_history) # Alternative name
978 978 ip.define_magic("history", magic_history)
979 979
980 980 # XXX - ipy_completers are in quarantine, need to be updated to new apis
981 981 #import ipy_completers
982 982 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,3843 +1,3844 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008-2011 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import __builtin__ as builtin_mod
19 19 import __future__
20 20 import bdb
21 21 import inspect
22 22 import io
23 23 import json
24 24 import os
25 25 import sys
26 26 import re
27 27 import time
28 28 import gc
29 29 from StringIO import StringIO
30 30 from getopt import getopt,GetoptError
31 31 from pprint import pformat
32 32 from urllib2 import urlopen
33 33
34 34 # cProfile was added in Python2.5
35 35 try:
36 36 import cProfile as profile
37 37 import pstats
38 38 except ImportError:
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 from IPython.core import debugger, oinspect
46 46 from IPython.core.error import TryNext
47 47 from IPython.core.error import UsageError
48 48 from IPython.core.error import StdinNotImplementedError
49 49 from IPython.core.macro import Macro
50 50 from IPython.core import magic_arguments, page
51 51 from IPython.core.prefilter import ESC_MAGIC
52 52 from IPython.testing.skipdoctest import skip_doctest
53 53 from IPython.utils import py3compat
54 54 from IPython.utils.encoding import DEFAULT_ENCODING
55 55 from IPython.utils.io import file_read, nlprint
56 56 from IPython.utils.module_paths import find_mod
57 57 from IPython.utils.path import get_py_filename, unquote_filename
58 58 from IPython.utils.process import arg_split, abbrev_cwd
59 59 from IPython.utils.terminal import set_term_title
60 60 from IPython.utils.text import format_screen
61 61 from IPython.utils.timing import clock, clock2
62 62 from IPython.utils.warn import warn, error
63 63 from IPython.utils.ipstruct import Struct
64 64 from IPython.config.application import Application
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Utility functions
68 68 #-----------------------------------------------------------------------------
69 69
70 70 def on_off(tag):
71 71 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
72 72 return ['OFF','ON'][tag]
73 73
74 74 class Bunch: pass
75 75
76 76 def compress_dhist(dh):
77 77 head, tail = dh[:-10], dh[-10:]
78 78
79 79 newhead = []
80 80 done = set()
81 81 for h in head:
82 82 if h in done:
83 83 continue
84 84 newhead.append(h)
85 85 done.add(h)
86 86
87 87 return newhead + tail
88 88
89 89 def needs_local_scope(func):
90 90 """Decorator to mark magic functions which need to local scope to run."""
91 91 func.needs_local_scope = True
92 92 return func
93 93
94 94
95 95 # Used for exception handling in magic_edit
96 96 class MacroToEdit(ValueError): pass
97 97
98 98 #***************************************************************************
99 99 # Main class implementing Magic functionality
100 100
101 101 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
102 102 # on construction of the main InteractiveShell object. Something odd is going
103 103 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
104 104 # eventually this needs to be clarified.
105 105 # BG: This is because InteractiveShell inherits from this, but is itself a
106 106 # Configurable. This messes up the MRO in some way. The fix is that we need to
107 107 # make Magic a configurable that InteractiveShell does not subclass.
108 108
109 109 class Magic(object):
110 110 """Magic functions for InteractiveShell.
111 111
112 112 Shell functions which can be reached as %function_name. All magic
113 113 functions should accept a string, which they can parse for their own
114 114 needs. This can make some functions easier to type, eg `%cd ../`
115 115 vs. `%cd("../")`
116 116
117 117 ALL definitions MUST begin with the prefix magic_. The user won't need it
118 118 at the command line, but it is is needed in the definition. """
119 119
120 120 # class globals
121 121 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
122 122 'Automagic is ON, % prefix NOT needed for magic functions.']
123 123
124 124
125 125 configurables = None
126 126
127 127 default_runner = None
128 128 #......................................................................
129 129 # some utility functions
130 130
131 131 def __init__(self, shell):
132 132
133 133 self.options_table = {}
134 134 if profile is None:
135 135 self.magic_prun = self.profile_missing_notice
136 136 self.shell = shell
137 137 if self.configurables is None:
138 138 self.configurables = []
139 139
140 140 # namespace for holding state we may need
141 141 self._magic_state = Bunch()
142 142
143 143 def profile_missing_notice(self, *args, **kwargs):
144 144 error("""\
145 145 The profile module could not be found. It has been removed from the standard
146 146 python packages because of its non-free license. To use profiling, install the
147 147 python-profiler package from non-free.""")
148 148
149 149 def default_option(self,fn,optstr):
150 150 """Make an entry in the options_table for fn, with value optstr"""
151 151
152 152 if fn not in self.lsmagic():
153 153 error("%s is not a magic function" % fn)
154 154 self.options_table[fn] = optstr
155 155
156 156 def lsmagic(self):
157 157 """Return a list of currently available magic functions.
158 158
159 159 Gives a list of the bare names after mangling (['ls','cd', ...], not
160 160 ['magic_ls','magic_cd',...]"""
161 161
162 162 # FIXME. This needs a cleanup, in the way the magics list is built.
163 163
164 164 # magics in class definition
165 165 class_magic = lambda fn: fn.startswith('magic_') and \
166 166 callable(Magic.__dict__[fn])
167 167 # in instance namespace (run-time user additions)
168 168 inst_magic = lambda fn: fn.startswith('magic_') and \
169 169 callable(self.__dict__[fn])
170 170 # and bound magics by user (so they can access self):
171 171 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
172 172 callable(self.__class__.__dict__[fn])
173 173 magics = filter(class_magic,Magic.__dict__.keys()) + \
174 filter(inst_magic,self.__dict__.keys()) + \
175 filter(inst_bound_magic,self.__class__.__dict__.keys())
174 filter(inst_magic, self.__dict__.keys()) + \
175 filter(inst_bound_magic, self.__class__.__dict__.keys())
176 176 out = []
177 177 for fn in set(magics):
178 178 out.append(fn.replace('magic_','',1))
179 179 out.sort()
180 180 return out
181 181
182 182 def extract_input_lines(self, range_str, raw=False):
183 183 """Return as a string a set of input history slices.
184 184
185 185 Parameters
186 186 ----------
187 187 range_str : string
188 188 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
189 189 since this function is for use by magic functions which get their
190 190 arguments as strings. The number before the / is the session
191 191 number: ~n goes n back from the current session.
192 192
193 193 Optional Parameters:
194 194 - raw(False): by default, the processed input is used. If this is
195 195 true, the raw input history is used instead.
196 196
197 197 Note that slices can be called with two notations:
198 198
199 199 N:M -> standard python form, means including items N...(M-1).
200 200
201 201 N-M -> include items N..M (closed endpoint)."""
202 202 lines = self.shell.history_manager.\
203 203 get_range_by_str(range_str, raw=raw)
204 204 return "\n".join(x for _, _, x in lines)
205 205
206 206 def arg_err(self,func):
207 207 """Print docstring if incorrect arguments were passed"""
208 208 print 'Error in arguments:'
209 209 print oinspect.getdoc(func)
210 210
211 211 def format_latex(self,strng):
212 212 """Format a string for latex inclusion."""
213 213
214 214 # Characters that need to be escaped for latex:
215 215 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
216 216 # Magic command names as headers:
217 217 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
218 218 re.MULTILINE)
219 219 # Magic commands
220 220 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
221 221 re.MULTILINE)
222 222 # Paragraph continue
223 223 par_re = re.compile(r'\\$',re.MULTILINE)
224 224
225 225 # The "\n" symbol
226 226 newline_re = re.compile(r'\\n')
227 227
228 228 # Now build the string for output:
229 229 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
230 230 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
231 231 strng)
232 232 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
233 233 strng = par_re.sub(r'\\\\',strng)
234 234 strng = escape_re.sub(r'\\\1',strng)
235 235 strng = newline_re.sub(r'\\textbackslash{}n',strng)
236 236 return strng
237 237
238 238 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
239 239 """Parse options passed to an argument string.
240 240
241 241 The interface is similar to that of getopt(), but it returns back a
242 242 Struct with the options as keys and the stripped argument string still
243 243 as a string.
244 244
245 245 arg_str is quoted as a true sys.argv vector by using shlex.split.
246 246 This allows us to easily expand variables, glob files, quote
247 247 arguments, etc.
248 248
249 249 Options:
250 250 -mode: default 'string'. If given as 'list', the argument string is
251 251 returned as a list (split on whitespace) instead of a string.
252 252
253 253 -list_all: put all option values in lists. Normally only options
254 254 appearing more than once are put in a list.
255 255
256 256 -posix (True): whether to split the input line in POSIX mode or not,
257 257 as per the conventions outlined in the shlex module from the
258 258 standard library."""
259 259
260 260 # inject default options at the beginning of the input line
261 261 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
262 262 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
263 263
264 264 mode = kw.get('mode','string')
265 265 if mode not in ['string','list']:
266 266 raise ValueError,'incorrect mode given: %s' % mode
267 267 # Get options
268 268 list_all = kw.get('list_all',0)
269 269 posix = kw.get('posix', os.name == 'posix')
270 270 strict = kw.get('strict', True)
271 271
272 272 # Check if we have more than one argument to warrant extra processing:
273 273 odict = {} # Dictionary with options
274 274 args = arg_str.split()
275 275 if len(args) >= 1:
276 276 # If the list of inputs only has 0 or 1 thing in it, there's no
277 277 # need to look for options
278 278 argv = arg_split(arg_str, posix, strict)
279 279 # Do regular option processing
280 280 try:
281 281 opts,args = getopt(argv,opt_str,*long_opts)
282 282 except GetoptError,e:
283 283 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
284 284 " ".join(long_opts)))
285 285 for o,a in opts:
286 286 if o.startswith('--'):
287 287 o = o[2:]
288 288 else:
289 289 o = o[1:]
290 290 try:
291 291 odict[o].append(a)
292 292 except AttributeError:
293 293 odict[o] = [odict[o],a]
294 294 except KeyError:
295 295 if list_all:
296 296 odict[o] = [a]
297 297 else:
298 298 odict[o] = a
299 299
300 300 # Prepare opts,args for return
301 301 opts = Struct(odict)
302 302 if mode == 'string':
303 303 args = ' '.join(args)
304 304
305 305 return opts,args
306 306
307 307 #......................................................................
308 308 # And now the actual magic functions
309 309
310 310 # Functions for IPython shell work (vars,funcs, config, etc)
311 311 def magic_lsmagic(self, parameter_s = ''):
312 312 """List currently available magic functions."""
313 313 mesc = ESC_MAGIC
314 314 print 'Available magic functions:\n'+mesc+\
315 315 (' '+mesc).join(self.lsmagic())
316 316 print '\n' + Magic.auto_status[self.shell.automagic]
317 317 return None
318 318
319 319 def magic_magic(self, parameter_s = ''):
320 320 """Print information about the magic function system.
321 321
322 322 Supported formats: -latex, -brief, -rest
323 323 """
324 324
325 325 mode = ''
326 326 try:
327 327 if parameter_s.split()[0] == '-latex':
328 328 mode = 'latex'
329 329 if parameter_s.split()[0] == '-brief':
330 330 mode = 'brief'
331 331 if parameter_s.split()[0] == '-rest':
332 332 mode = 'rest'
333 333 rest_docs = []
334 334 except:
335 335 pass
336 336
337 337 magic_docs = []
338 338 for fname in self.lsmagic():
339 339 mname = 'magic_' + fname
340 for space in (Magic,self,self.__class__):
340 for space in (Magic, self, self.__class__):
341 341 try:
342 342 fn = space.__dict__[mname]
343 343 except KeyError:
344 344 pass
345 345 else:
346 346 break
347 347 if mode == 'brief':
348 348 # only first line
349 349 if fn.__doc__:
350 350 fndoc = fn.__doc__.split('\n',1)[0]
351 351 else:
352 352 fndoc = 'No documentation'
353 353 else:
354 354 if fn.__doc__:
355 355 fndoc = fn.__doc__.rstrip()
356 356 else:
357 357 fndoc = 'No documentation'
358 358
359 359
360 360 if mode == 'rest':
361 361 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
362 362 fname,fndoc))
363 363
364 364 else:
365 365 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
366 366 fname,fndoc))
367 367
368 368 magic_docs = ''.join(magic_docs)
369 369
370 370 if mode == 'rest':
371 371 return "".join(rest_docs)
372 372
373 373 if mode == 'latex':
374 374 print self.format_latex(magic_docs)
375 375 return
376 376 else:
377 377 magic_docs = format_screen(magic_docs)
378 378 if mode == 'brief':
379 379 return magic_docs
380 380
381 381 outmsg = """
382 382 IPython's 'magic' functions
383 383 ===========================
384 384
385 385 The magic function system provides a series of functions which allow you to
386 386 control the behavior of IPython itself, plus a lot of system-type
387 387 features. All these functions are prefixed with a % character, but parameters
388 388 are given without parentheses or quotes.
389 389
390 390 NOTE: If you have 'automagic' enabled (via the command line option or with the
391 391 %automagic function), you don't need to type in the % explicitly. By default,
392 392 IPython ships with automagic on, so you should only rarely need the % escape.
393 393
394 394 Example: typing '%cd mydir' (without the quotes) changes you working directory
395 395 to 'mydir', if it exists.
396 396
397 397 For a list of the available magic functions, use %lsmagic. For a description
398 398 of any of them, type %magic_name?, e.g. '%cd?'.
399 399
400 400 Currently the magic system has the following functions:\n"""
401 401
402 402 mesc = ESC_MAGIC
403 403 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
404 404 "\n\n%s%s\n\n%s" % (outmsg,
405 405 magic_docs,mesc,mesc,
406 406 (' '+mesc).join(self.lsmagic()),
407 407 Magic.auto_status[self.shell.automagic] ) )
408 408 page.page(outmsg)
409 409
410 410 def magic_automagic(self, parameter_s = ''):
411 411 """Make magic functions callable without having to type the initial %.
412 412
413 413 Without argumentsl toggles on/off (when off, you must call it as
414 414 %automagic, of course). With arguments it sets the value, and you can
415 415 use any of (case insensitive):
416 416
417 417 - on,1,True: to activate
418 418
419 419 - off,0,False: to deactivate.
420 420
421 421 Note that magic functions have lowest priority, so if there's a
422 422 variable whose name collides with that of a magic fn, automagic won't
423 423 work for that function (you get the variable instead). However, if you
424 424 delete the variable (del var), the previously shadowed magic function
425 425 becomes visible to automagic again."""
426 426
427 427 arg = parameter_s.lower()
428 428 if parameter_s in ('on','1','true'):
429 429 self.shell.automagic = True
430 430 elif parameter_s in ('off','0','false'):
431 431 self.shell.automagic = False
432 432 else:
433 433 self.shell.automagic = not self.shell.automagic
434 434 print '\n' + Magic.auto_status[self.shell.automagic]
435 435
436 436 @skip_doctest
437 437 def magic_autocall(self, parameter_s = ''):
438 438 """Make functions callable without having to type parentheses.
439 439
440 440 Usage:
441 441
442 442 %autocall [mode]
443 443
444 444 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
445 445 value is toggled on and off (remembering the previous state).
446 446
447 447 In more detail, these values mean:
448 448
449 449 0 -> fully disabled
450 450
451 451 1 -> active, but do not apply if there are no arguments on the line.
452 452
453 453 In this mode, you get::
454 454
455 455 In [1]: callable
456 456 Out[1]: <built-in function callable>
457 457
458 458 In [2]: callable 'hello'
459 459 ------> callable('hello')
460 460 Out[2]: False
461 461
462 462 2 -> Active always. Even if no arguments are present, the callable
463 463 object is called::
464 464
465 465 In [2]: float
466 466 ------> float()
467 467 Out[2]: 0.0
468 468
469 469 Note that even with autocall off, you can still use '/' at the start of
470 470 a line to treat the first argument on the command line as a function
471 471 and add parentheses to it::
472 472
473 473 In [8]: /str 43
474 474 ------> str(43)
475 475 Out[8]: '43'
476 476
477 477 # all-random (note for auto-testing)
478 478 """
479 479
480 480 if parameter_s:
481 481 arg = int(parameter_s)
482 482 else:
483 483 arg = 'toggle'
484 484
485 485 if not arg in (0,1,2,'toggle'):
486 486 error('Valid modes: (0->Off, 1->Smart, 2->Full')
487 487 return
488 488
489 489 if arg in (0,1,2):
490 490 self.shell.autocall = arg
491 491 else: # toggle
492 492 if self.shell.autocall:
493 493 self._magic_state.autocall_save = self.shell.autocall
494 494 self.shell.autocall = 0
495 495 else:
496 496 try:
497 497 self.shell.autocall = self._magic_state.autocall_save
498 498 except AttributeError:
499 499 self.shell.autocall = self._magic_state.autocall_save = 1
500 500
501 501 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
502 502
503 503
504 504 def magic_page(self, parameter_s=''):
505 505 """Pretty print the object and display it through a pager.
506 506
507 507 %page [options] OBJECT
508 508
509 509 If no object is given, use _ (last output).
510 510
511 511 Options:
512 512
513 513 -r: page str(object), don't pretty-print it."""
514 514
515 515 # After a function contributed by Olivier Aubert, slightly modified.
516 516
517 517 # Process options/args
518 518 opts,args = self.parse_options(parameter_s,'r')
519 519 raw = 'r' in opts
520 520
521 521 oname = args and args or '_'
522 522 info = self._ofind(oname)
523 523 if info['found']:
524 524 txt = (raw and str or pformat)( info['obj'] )
525 525 page.page(txt)
526 526 else:
527 527 print 'Object `%s` not found' % oname
528 528
529 529 def magic_profile(self, parameter_s=''):
530 530 """Print your currently active IPython profile."""
531 531 from IPython.core.application import BaseIPythonApplication
532 532 if BaseIPythonApplication.initialized():
533 533 print BaseIPythonApplication.instance().profile
534 534 else:
535 535 error("profile is an application-level value, but you don't appear to be in an IPython application")
536 536
537 537 def magic_pinfo(self, parameter_s='', namespaces=None):
538 538 """Provide detailed information about an object.
539 539
540 540 '%pinfo object' is just a synonym for object? or ?object."""
541 541
542 542 #print 'pinfo par: <%s>' % parameter_s # dbg
543 543
544 544
545 545 # detail_level: 0 -> obj? , 1 -> obj??
546 546 detail_level = 0
547 547 # We need to detect if we got called as 'pinfo pinfo foo', which can
548 548 # happen if the user types 'pinfo foo?' at the cmd line.
549 549 pinfo,qmark1,oname,qmark2 = \
550 550 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
551 551 if pinfo or qmark1 or qmark2:
552 552 detail_level = 1
553 553 if "*" in oname:
554 554 self.magic_psearch(oname)
555 555 else:
556 556 self.shell._inspect('pinfo', oname, detail_level=detail_level,
557 557 namespaces=namespaces)
558 558
559 559 def magic_pinfo2(self, parameter_s='', namespaces=None):
560 560 """Provide extra detailed information about an object.
561 561
562 562 '%pinfo2 object' is just a synonym for object?? or ??object."""
563 563 self.shell._inspect('pinfo', parameter_s, detail_level=1,
564 564 namespaces=namespaces)
565 565
566 566 @skip_doctest
567 567 def magic_pdef(self, parameter_s='', namespaces=None):
568 568 """Print the definition header for any callable object.
569 569
570 570 If the object is a class, print the constructor information.
571 571
572 572 Examples
573 573 --------
574 574 ::
575 575
576 576 In [3]: %pdef urllib.urlopen
577 577 urllib.urlopen(url, data=None, proxies=None)
578 578 """
579 579 self._inspect('pdef',parameter_s, namespaces)
580 580
581 581 def magic_pdoc(self, parameter_s='', namespaces=None):
582 582 """Print the docstring for an object.
583 583
584 584 If the given object is a class, it will print both the class and the
585 585 constructor docstrings."""
586 586 self._inspect('pdoc',parameter_s, namespaces)
587 587
588 588 def magic_psource(self, parameter_s='', namespaces=None):
589 589 """Print (or run through pager) the source code for an object."""
590 590 self._inspect('psource',parameter_s, namespaces)
591 591
592 592 def magic_pfile(self, parameter_s=''):
593 593 """Print (or run through pager) the file where an object is defined.
594 594
595 595 The file opens at the line where the object definition begins. IPython
596 596 will honor the environment variable PAGER if set, and otherwise will
597 597 do its best to print the file in a convenient form.
598 598
599 599 If the given argument is not an object currently defined, IPython will
600 600 try to interpret it as a filename (automatically adding a .py extension
601 601 if needed). You can thus use %pfile as a syntax highlighting code
602 602 viewer."""
603 603
604 604 # first interpret argument as an object name
605 605 out = self._inspect('pfile',parameter_s)
606 606 # if not, try the input as a filename
607 607 if out == 'not found':
608 608 try:
609 609 filename = get_py_filename(parameter_s)
610 610 except IOError,msg:
611 611 print msg
612 612 return
613 613 page.page(self.shell.inspector.format(open(filename).read()))
614 614
615 615 def magic_psearch(self, parameter_s=''):
616 616 """Search for object in namespaces by wildcard.
617 617
618 618 %psearch [options] PATTERN [OBJECT TYPE]
619 619
620 620 Note: ? can be used as a synonym for %psearch, at the beginning or at
621 621 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
622 622 rest of the command line must be unchanged (options come first), so
623 623 for example the following forms are equivalent
624 624
625 625 %psearch -i a* function
626 626 -i a* function?
627 627 ?-i a* function
628 628
629 629 Arguments:
630 630
631 631 PATTERN
632 632
633 633 where PATTERN is a string containing * as a wildcard similar to its
634 634 use in a shell. The pattern is matched in all namespaces on the
635 635 search path. By default objects starting with a single _ are not
636 636 matched, many IPython generated objects have a single
637 637 underscore. The default is case insensitive matching. Matching is
638 638 also done on the attributes of objects and not only on the objects
639 639 in a module.
640 640
641 641 [OBJECT TYPE]
642 642
643 643 Is the name of a python type from the types module. The name is
644 644 given in lowercase without the ending type, ex. StringType is
645 645 written string. By adding a type here only objects matching the
646 646 given type are matched. Using all here makes the pattern match all
647 647 types (this is the default).
648 648
649 649 Options:
650 650
651 651 -a: makes the pattern match even objects whose names start with a
652 652 single underscore. These names are normally omitted from the
653 653 search.
654 654
655 655 -i/-c: make the pattern case insensitive/sensitive. If neither of
656 656 these options are given, the default is read from your configuration
657 657 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
658 658 If this option is not specified in your configuration file, IPython's
659 659 internal default is to do a case sensitive search.
660 660
661 661 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
662 662 specify can be searched in any of the following namespaces:
663 663 'builtin', 'user', 'user_global','internal', 'alias', where
664 664 'builtin' and 'user' are the search defaults. Note that you should
665 665 not use quotes when specifying namespaces.
666 666
667 667 'Builtin' contains the python module builtin, 'user' contains all
668 668 user data, 'alias' only contain the shell aliases and no python
669 669 objects, 'internal' contains objects used by IPython. The
670 670 'user_global' namespace is only used by embedded IPython instances,
671 671 and it contains module-level globals. You can add namespaces to the
672 672 search with -s or exclude them with -e (these options can be given
673 673 more than once).
674 674
675 675 Examples
676 676 --------
677 677 ::
678 678
679 679 %psearch a* -> objects beginning with an a
680 680 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
681 681 %psearch a* function -> all functions beginning with an a
682 682 %psearch re.e* -> objects beginning with an e in module re
683 683 %psearch r*.e* -> objects that start with e in modules starting in r
684 684 %psearch r*.* string -> all strings in modules beginning with r
685 685
686 686 Case sensitive search::
687 687
688 688 %psearch -c a* list all object beginning with lower case a
689 689
690 690 Show objects beginning with a single _::
691 691
692 692 %psearch -a _* list objects beginning with a single underscore"""
693 693 try:
694 694 parameter_s.encode('ascii')
695 695 except UnicodeEncodeError:
696 696 print 'Python identifiers can only contain ascii characters.'
697 697 return
698 698
699 699 # default namespaces to be searched
700 700 def_search = ['user_local', 'user_global', 'builtin']
701 701
702 702 # Process options/args
703 703 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
704 704 opt = opts.get
705 705 shell = self.shell
706 706 psearch = shell.inspector.psearch
707 707
708 708 # select case options
709 709 if opts.has_key('i'):
710 710 ignore_case = True
711 711 elif opts.has_key('c'):
712 712 ignore_case = False
713 713 else:
714 714 ignore_case = not shell.wildcards_case_sensitive
715 715
716 716 # Build list of namespaces to search from user options
717 717 def_search.extend(opt('s',[]))
718 718 ns_exclude = ns_exclude=opt('e',[])
719 719 ns_search = [nm for nm in def_search if nm not in ns_exclude]
720 720
721 721 # Call the actual search
722 722 try:
723 723 psearch(args,shell.ns_table,ns_search,
724 724 show_all=opt('a'),ignore_case=ignore_case)
725 725 except:
726 726 shell.showtraceback()
727 727
728 728 @skip_doctest
729 729 def magic_who_ls(self, parameter_s=''):
730 730 """Return a sorted list of all interactive variables.
731 731
732 732 If arguments are given, only variables of types matching these
733 733 arguments are returned.
734 734
735 735 Examples
736 736 --------
737 737
738 738 Define two variables and list them with who_ls::
739 739
740 740 In [1]: alpha = 123
741 741
742 742 In [2]: beta = 'test'
743 743
744 744 In [3]: %who_ls
745 745 Out[3]: ['alpha', 'beta']
746 746
747 747 In [4]: %who_ls int
748 748 Out[4]: ['alpha']
749 749
750 750 In [5]: %who_ls str
751 751 Out[5]: ['beta']
752 752 """
753 753
754 754 user_ns = self.shell.user_ns
755 755 user_ns_hidden = self.shell.user_ns_hidden
756 756 out = [ i for i in user_ns
757 757 if not i.startswith('_') \
758 758 and not i in user_ns_hidden ]
759 759
760 760 typelist = parameter_s.split()
761 761 if typelist:
762 762 typeset = set(typelist)
763 763 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
764 764
765 765 out.sort()
766 766 return out
767 767
768 768 @skip_doctest
769 769 def magic_who(self, parameter_s=''):
770 770 """Print all interactive variables, with some minimal formatting.
771 771
772 772 If any arguments are given, only variables whose type matches one of
773 773 these are printed. For example::
774 774
775 775 %who function str
776 776
777 777 will only list functions and strings, excluding all other types of
778 778 variables. To find the proper type names, simply use type(var) at a
779 779 command line to see how python prints type names. For example:
780 780
781 781 ::
782 782
783 783 In [1]: type('hello')\\
784 784 Out[1]: <type 'str'>
785 785
786 786 indicates that the type name for strings is 'str'.
787 787
788 788 ``%who`` always excludes executed names loaded through your configuration
789 789 file and things which are internal to IPython.
790 790
791 791 This is deliberate, as typically you may load many modules and the
792 792 purpose of %who is to show you only what you've manually defined.
793 793
794 794 Examples
795 795 --------
796 796
797 797 Define two variables and list them with who::
798 798
799 799 In [1]: alpha = 123
800 800
801 801 In [2]: beta = 'test'
802 802
803 803 In [3]: %who
804 804 alpha beta
805 805
806 806 In [4]: %who int
807 807 alpha
808 808
809 809 In [5]: %who str
810 810 beta
811 811 """
812 812
813 813 varlist = self.magic_who_ls(parameter_s)
814 814 if not varlist:
815 815 if parameter_s:
816 816 print 'No variables match your requested type.'
817 817 else:
818 818 print 'Interactive namespace is empty.'
819 819 return
820 820
821 821 # if we have variables, move on...
822 822 count = 0
823 823 for i in varlist:
824 824 print i+'\t',
825 825 count += 1
826 826 if count > 8:
827 827 count = 0
828 828 print
829 829 print
830 830
831 831 @skip_doctest
832 832 def magic_whos(self, parameter_s=''):
833 833 """Like %who, but gives some extra information about each variable.
834 834
835 835 The same type filtering of %who can be applied here.
836 836
837 837 For all variables, the type is printed. Additionally it prints:
838 838
839 839 - For {},[],(): their length.
840 840
841 841 - For numpy arrays, a summary with shape, number of
842 842 elements, typecode and size in memory.
843 843
844 844 - Everything else: a string representation, snipping their middle if
845 845 too long.
846 846
847 847 Examples
848 848 --------
849 849
850 850 Define two variables and list them with whos::
851 851
852 852 In [1]: alpha = 123
853 853
854 854 In [2]: beta = 'test'
855 855
856 856 In [3]: %whos
857 857 Variable Type Data/Info
858 858 --------------------------------
859 859 alpha int 123
860 860 beta str test
861 861 """
862 862
863 863 varnames = self.magic_who_ls(parameter_s)
864 864 if not varnames:
865 865 if parameter_s:
866 866 print 'No variables match your requested type.'
867 867 else:
868 868 print 'Interactive namespace is empty.'
869 869 return
870 870
871 871 # if we have variables, move on...
872 872
873 873 # for these types, show len() instead of data:
874 874 seq_types = ['dict', 'list', 'tuple']
875 875
876 876 # for numpy arrays, display summary info
877 877 ndarray_type = None
878 878 if 'numpy' in sys.modules:
879 879 try:
880 880 from numpy import ndarray
881 881 except ImportError:
882 882 pass
883 883 else:
884 884 ndarray_type = ndarray.__name__
885 885
886 886 # Find all variable names and types so we can figure out column sizes
887 887 def get_vars(i):
888 888 return self.shell.user_ns[i]
889 889
890 890 # some types are well known and can be shorter
891 891 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
892 892 def type_name(v):
893 893 tn = type(v).__name__
894 894 return abbrevs.get(tn,tn)
895 895
896 896 varlist = map(get_vars,varnames)
897 897
898 898 typelist = []
899 899 for vv in varlist:
900 900 tt = type_name(vv)
901 901
902 902 if tt=='instance':
903 903 typelist.append( abbrevs.get(str(vv.__class__),
904 904 str(vv.__class__)))
905 905 else:
906 906 typelist.append(tt)
907 907
908 908 # column labels and # of spaces as separator
909 909 varlabel = 'Variable'
910 910 typelabel = 'Type'
911 911 datalabel = 'Data/Info'
912 912 colsep = 3
913 913 # variable format strings
914 914 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
915 915 aformat = "%s: %s elems, type `%s`, %s bytes"
916 916 # find the size of the columns to format the output nicely
917 917 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
918 918 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
919 919 # table header
920 920 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
921 921 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
922 922 # and the table itself
923 923 kb = 1024
924 924 Mb = 1048576 # kb**2
925 925 for vname,var,vtype in zip(varnames,varlist,typelist):
926 926 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
927 927 if vtype in seq_types:
928 928 print "n="+str(len(var))
929 929 elif vtype == ndarray_type:
930 930 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
931 931 if vtype==ndarray_type:
932 932 # numpy
933 933 vsize = var.size
934 934 vbytes = vsize*var.itemsize
935 935 vdtype = var.dtype
936 936
937 937 if vbytes < 100000:
938 938 print aformat % (vshape,vsize,vdtype,vbytes)
939 939 else:
940 940 print aformat % (vshape,vsize,vdtype,vbytes),
941 941 if vbytes < Mb:
942 942 print '(%s kb)' % (vbytes/kb,)
943 943 else:
944 944 print '(%s Mb)' % (vbytes/Mb,)
945 945 else:
946 946 try:
947 947 vstr = str(var)
948 948 except UnicodeEncodeError:
949 949 vstr = unicode(var).encode(DEFAULT_ENCODING,
950 950 'backslashreplace')
951 951 except:
952 952 vstr = "<object with id %d (str() failed)>" % id(var)
953 953 vstr = vstr.replace('\n','\\n')
954 954 if len(vstr) < 50:
955 955 print vstr
956 956 else:
957 957 print vstr[:25] + "<...>" + vstr[-25:]
958 958
959 959 def magic_reset(self, parameter_s=''):
960 960 """Resets the namespace by removing all names defined by the user, if
961 961 called without arguments, or by removing some types of objects, such
962 962 as everything currently in IPython's In[] and Out[] containers (see
963 963 the parameters for details).
964 964
965 965 Parameters
966 966 ----------
967 967 -f : force reset without asking for confirmation.
968 968
969 969 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
970 970 References to objects may be kept. By default (without this option),
971 971 we do a 'hard' reset, giving you a new session and removing all
972 972 references to objects from the current session.
973 973
974 974 in : reset input history
975 975
976 976 out : reset output history
977 977
978 978 dhist : reset directory history
979 979
980 980 array : reset only variables that are NumPy arrays
981 981
982 982 See Also
983 983 --------
984 984 magic_reset_selective : invoked as ``%reset_selective``
985 985
986 986 Examples
987 987 --------
988 988 ::
989 989
990 990 In [6]: a = 1
991 991
992 992 In [7]: a
993 993 Out[7]: 1
994 994
995 995 In [8]: 'a' in _ip.user_ns
996 996 Out[8]: True
997 997
998 998 In [9]: %reset -f
999 999
1000 1000 In [1]: 'a' in _ip.user_ns
1001 1001 Out[1]: False
1002 1002
1003 1003 In [2]: %reset -f in
1004 1004 Flushing input history
1005 1005
1006 1006 In [3]: %reset -f dhist in
1007 1007 Flushing directory history
1008 1008 Flushing input history
1009 1009
1010 1010 Notes
1011 1011 -----
1012 1012 Calling this magic from clients that do not implement standard input,
1013 1013 such as the ipython notebook interface, will reset the namespace
1014 1014 without confirmation.
1015 1015 """
1016 1016 opts, args = self.parse_options(parameter_s,'sf', mode='list')
1017 1017 if 'f' in opts:
1018 1018 ans = True
1019 1019 else:
1020 1020 try:
1021 1021 ans = self.shell.ask_yes_no(
1022 1022 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", default='n')
1023 1023 except StdinNotImplementedError:
1024 1024 ans = True
1025 1025 if not ans:
1026 1026 print 'Nothing done.'
1027 1027 return
1028 1028
1029 1029 if 's' in opts: # Soft reset
1030 1030 user_ns = self.shell.user_ns
1031 1031 for i in self.magic_who_ls():
1032 1032 del(user_ns[i])
1033 1033 elif len(args) == 0: # Hard reset
1034 1034 self.shell.reset(new_session = False)
1035 1035
1036 1036 # reset in/out/dhist/array: previously extensinions/clearcmd.py
1037 1037 ip = self.shell
1038 user_ns = self.user_ns # local lookup, heavily used
1038 user_ns = self.shell.user_ns # local lookup, heavily used
1039 1039
1040 1040 for target in args:
1041 1041 target = target.lower() # make matches case insensitive
1042 1042 if target == 'out':
1043 1043 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
1044 self.displayhook.flush()
1044 self.shell.displayhook.flush()
1045 1045
1046 1046 elif target == 'in':
1047 1047 print "Flushing input history"
1048 pc = self.displayhook.prompt_count + 1
1048 pc = self.shell.displayhook.prompt_count + 1
1049 1049 for n in range(1, pc):
1050 1050 key = '_i'+repr(n)
1051 1051 user_ns.pop(key,None)
1052 1052 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
1053 1053 hm = ip.history_manager
1054 1054 # don't delete these, as %save and %macro depending on the length
1055 1055 # of these lists to be preserved
1056 1056 hm.input_hist_parsed[:] = [''] * pc
1057 1057 hm.input_hist_raw[:] = [''] * pc
1058 1058 # hm has internal machinery for _i,_ii,_iii, clear it out
1059 1059 hm._i = hm._ii = hm._iii = hm._i00 = u''
1060 1060
1061 1061 elif target == 'array':
1062 1062 # Support cleaning up numpy arrays
1063 1063 try:
1064 1064 from numpy import ndarray
1065 1065 # This must be done with items and not iteritems because we're
1066 1066 # going to modify the dict in-place.
1067 1067 for x,val in user_ns.items():
1068 1068 if isinstance(val,ndarray):
1069 1069 del user_ns[x]
1070 1070 except ImportError:
1071 1071 print "reset array only works if Numpy is available."
1072 1072
1073 1073 elif target == 'dhist':
1074 1074 print "Flushing directory history"
1075 1075 del user_ns['_dh'][:]
1076 1076
1077 1077 else:
1078 1078 print "Don't know how to reset ",
1079 1079 print target + ", please run `%reset?` for details"
1080 1080
1081 1081 gc.collect()
1082 1082
1083 1083 def magic_reset_selective(self, parameter_s=''):
1084 1084 """Resets the namespace by removing names defined by the user.
1085 1085
1086 1086 Input/Output history are left around in case you need them.
1087 1087
1088 1088 %reset_selective [-f] regex
1089 1089
1090 1090 No action is taken if regex is not included
1091 1091
1092 1092 Options
1093 1093 -f : force reset without asking for confirmation.
1094 1094
1095 1095 See Also
1096 1096 --------
1097 1097 magic_reset : invoked as ``%reset``
1098 1098
1099 1099 Examples
1100 1100 --------
1101 1101
1102 1102 We first fully reset the namespace so your output looks identical to
1103 1103 this example for pedagogical reasons; in practice you do not need a
1104 1104 full reset::
1105 1105
1106 1106 In [1]: %reset -f
1107 1107
1108 1108 Now, with a clean namespace we can make a few variables and use
1109 1109 ``%reset_selective`` to only delete names that match our regexp::
1110 1110
1111 1111 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1112 1112
1113 1113 In [3]: who_ls
1114 1114 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1115 1115
1116 1116 In [4]: %reset_selective -f b[2-3]m
1117 1117
1118 1118 In [5]: who_ls
1119 1119 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1120 1120
1121 1121 In [6]: %reset_selective -f d
1122 1122
1123 1123 In [7]: who_ls
1124 1124 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1125 1125
1126 1126 In [8]: %reset_selective -f c
1127 1127
1128 1128 In [9]: who_ls
1129 1129 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1130 1130
1131 1131 In [10]: %reset_selective -f b
1132 1132
1133 1133 In [11]: who_ls
1134 1134 Out[11]: ['a']
1135 1135
1136 1136 Notes
1137 1137 -----
1138 1138 Calling this magic from clients that do not implement standard input,
1139 1139 such as the ipython notebook interface, will reset the namespace
1140 1140 without confirmation.
1141 1141 """
1142 1142
1143 1143 opts, regex = self.parse_options(parameter_s,'f')
1144 1144
1145 1145 if opts.has_key('f'):
1146 1146 ans = True
1147 1147 else:
1148 1148 try:
1149 1149 ans = self.shell.ask_yes_no(
1150 1150 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
1151 1151 default='n')
1152 1152 except StdinNotImplementedError:
1153 1153 ans = True
1154 1154 if not ans:
1155 1155 print 'Nothing done.'
1156 1156 return
1157 1157 user_ns = self.shell.user_ns
1158 1158 if not regex:
1159 1159 print 'No regex pattern specified. Nothing done.'
1160 1160 return
1161 1161 else:
1162 1162 try:
1163 1163 m = re.compile(regex)
1164 1164 except TypeError:
1165 1165 raise TypeError('regex must be a string or compiled pattern')
1166 1166 for i in self.magic_who_ls():
1167 1167 if m.search(i):
1168 1168 del(user_ns[i])
1169 1169
1170 1170 def magic_xdel(self, parameter_s=''):
1171 1171 """Delete a variable, trying to clear it from anywhere that
1172 1172 IPython's machinery has references to it. By default, this uses
1173 1173 the identity of the named object in the user namespace to remove
1174 1174 references held under other names. The object is also removed
1175 1175 from the output history.
1176 1176
1177 1177 Options
1178 1178 -n : Delete the specified name from all namespaces, without
1179 1179 checking their identity.
1180 1180 """
1181 1181 opts, varname = self.parse_options(parameter_s,'n')
1182 1182 try:
1183 1183 self.shell.del_var(varname, ('n' in opts))
1184 1184 except (NameError, ValueError) as e:
1185 1185 print type(e).__name__ +": "+ str(e)
1186 1186
1187 1187 def magic_logstart(self,parameter_s=''):
1188 1188 """Start logging anywhere in a session.
1189 1189
1190 1190 %logstart [-o|-r|-t] [log_name [log_mode]]
1191 1191
1192 1192 If no name is given, it defaults to a file named 'ipython_log.py' in your
1193 1193 current directory, in 'rotate' mode (see below).
1194 1194
1195 1195 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1196 1196 history up to that point and then continues logging.
1197 1197
1198 1198 %logstart takes a second optional parameter: logging mode. This can be one
1199 1199 of (note that the modes are given unquoted):\\
1200 1200 append: well, that says it.\\
1201 1201 backup: rename (if exists) to name~ and start name.\\
1202 1202 global: single logfile in your home dir, appended to.\\
1203 1203 over : overwrite existing log.\\
1204 1204 rotate: create rotating logs name.1~, name.2~, etc.
1205 1205
1206 1206 Options:
1207 1207
1208 1208 -o: log also IPython's output. In this mode, all commands which
1209 1209 generate an Out[NN] prompt are recorded to the logfile, right after
1210 1210 their corresponding input line. The output lines are always
1211 1211 prepended with a '#[Out]# ' marker, so that the log remains valid
1212 1212 Python code.
1213 1213
1214 1214 Since this marker is always the same, filtering only the output from
1215 1215 a log is very easy, using for example a simple awk call::
1216 1216
1217 1217 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1218 1218
1219 1219 -r: log 'raw' input. Normally, IPython's logs contain the processed
1220 1220 input, so that user lines are logged in their final form, converted
1221 1221 into valid Python. For example, %Exit is logged as
1222 1222 _ip.magic("Exit"). If the -r flag is given, all input is logged
1223 1223 exactly as typed, with no transformations applied.
1224 1224
1225 1225 -t: put timestamps before each input line logged (these are put in
1226 1226 comments)."""
1227 1227
1228 1228 opts,par = self.parse_options(parameter_s,'ort')
1229 1229 log_output = 'o' in opts
1230 1230 log_raw_input = 'r' in opts
1231 1231 timestamp = 't' in opts
1232 1232
1233 1233 logger = self.shell.logger
1234 1234
1235 1235 # if no args are given, the defaults set in the logger constructor by
1236 1236 # ipython remain valid
1237 1237 if par:
1238 1238 try:
1239 1239 logfname,logmode = par.split()
1240 1240 except:
1241 1241 logfname = par
1242 1242 logmode = 'backup'
1243 1243 else:
1244 1244 logfname = logger.logfname
1245 1245 logmode = logger.logmode
1246 1246 # put logfname into rc struct as if it had been called on the command
1247 1247 # line, so it ends up saved in the log header Save it in case we need
1248 1248 # to restore it...
1249 1249 old_logfile = self.shell.logfile
1250 1250 if logfname:
1251 1251 logfname = os.path.expanduser(logfname)
1252 1252 self.shell.logfile = logfname
1253 1253
1254 1254 loghead = '# IPython log file\n\n'
1255 1255 try:
1256 1256 started = logger.logstart(logfname,loghead,logmode,
1257 1257 log_output,timestamp,log_raw_input)
1258 1258 except:
1259 1259 self.shell.logfile = old_logfile
1260 1260 warn("Couldn't start log: %s" % sys.exc_info()[1])
1261 1261 else:
1262 1262 # log input history up to this point, optionally interleaving
1263 1263 # output if requested
1264 1264
1265 1265 if timestamp:
1266 1266 # disable timestamping for the previous history, since we've
1267 1267 # lost those already (no time machine here).
1268 1268 logger.timestamp = False
1269 1269
1270 1270 if log_raw_input:
1271 1271 input_hist = self.shell.history_manager.input_hist_raw
1272 1272 else:
1273 1273 input_hist = self.shell.history_manager.input_hist_parsed
1274 1274
1275 1275 if log_output:
1276 1276 log_write = logger.log_write
1277 1277 output_hist = self.shell.history_manager.output_hist
1278 1278 for n in range(1,len(input_hist)-1):
1279 1279 log_write(input_hist[n].rstrip() + '\n')
1280 1280 if n in output_hist:
1281 1281 log_write(repr(output_hist[n]),'output')
1282 1282 else:
1283 1283 logger.log_write('\n'.join(input_hist[1:]))
1284 1284 logger.log_write('\n')
1285 1285 if timestamp:
1286 1286 # re-enable timestamping
1287 1287 logger.timestamp = True
1288 1288
1289 1289 print ('Activating auto-logging. '
1290 1290 'Current session state plus future input saved.')
1291 1291 logger.logstate()
1292 1292
1293 1293 def magic_logstop(self,parameter_s=''):
1294 1294 """Fully stop logging and close log file.
1295 1295
1296 1296 In order to start logging again, a new %logstart call needs to be made,
1297 1297 possibly (though not necessarily) with a new filename, mode and other
1298 1298 options."""
1299 1299 self.logger.logstop()
1300 1300
1301 1301 def magic_logoff(self,parameter_s=''):
1302 1302 """Temporarily stop logging.
1303 1303
1304 1304 You must have previously started logging."""
1305 1305 self.shell.logger.switch_log(0)
1306 1306
1307 1307 def magic_logon(self,parameter_s=''):
1308 1308 """Restart logging.
1309 1309
1310 1310 This function is for restarting logging which you've temporarily
1311 1311 stopped with %logoff. For starting logging for the first time, you
1312 1312 must use the %logstart function, which allows you to specify an
1313 1313 optional log filename."""
1314 1314
1315 1315 self.shell.logger.switch_log(1)
1316 1316
1317 1317 def magic_logstate(self,parameter_s=''):
1318 1318 """Print the status of the logging system."""
1319 1319
1320 1320 self.shell.logger.logstate()
1321 1321
1322 1322 def magic_pdb(self, parameter_s=''):
1323 1323 """Control the automatic calling of the pdb interactive debugger.
1324 1324
1325 1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1326 1326 argument it works as a toggle.
1327 1327
1328 1328 When an exception is triggered, IPython can optionally call the
1329 1329 interactive pdb debugger after the traceback printout. %pdb toggles
1330 1330 this feature on and off.
1331 1331
1332 1332 The initial state of this feature is set in your configuration
1333 1333 file (the option is ``InteractiveShell.pdb``).
1334 1334
1335 1335 If you want to just activate the debugger AFTER an exception has fired,
1336 1336 without having to type '%pdb on' and rerunning your code, you can use
1337 1337 the %debug magic."""
1338 1338
1339 1339 par = parameter_s.strip().lower()
1340 1340
1341 1341 if par:
1342 1342 try:
1343 1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1344 1344 except KeyError:
1345 1345 print ('Incorrect argument. Use on/1, off/0, '
1346 1346 'or nothing for a toggle.')
1347 1347 return
1348 1348 else:
1349 1349 # toggle
1350 1350 new_pdb = not self.shell.call_pdb
1351 1351
1352 1352 # set on the shell
1353 1353 self.shell.call_pdb = new_pdb
1354 1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1355 1355
1356 1356 def magic_debug(self, parameter_s=''):
1357 1357 """Activate the interactive debugger in post-mortem mode.
1358 1358
1359 1359 If an exception has just occurred, this lets you inspect its stack
1360 1360 frames interactively. Note that this will always work only on the last
1361 1361 traceback that occurred, so you must call this quickly after an
1362 1362 exception that you wish to inspect has fired, because if another one
1363 1363 occurs, it clobbers the previous one.
1364 1364
1365 1365 If you want IPython to automatically do this on every exception, see
1366 1366 the %pdb magic for more details.
1367 1367 """
1368 1368 self.shell.debugger(force=True)
1369 1369
1370 1370 @skip_doctest
1371 1371 def magic_prun(self, parameter_s ='',user_mode=1,
1372 1372 opts=None,arg_lst=None,prog_ns=None):
1373 1373
1374 1374 """Run a statement through the python code profiler.
1375 1375
1376 1376 Usage:
1377 1377 %prun [options] statement
1378 1378
1379 1379 The given statement (which doesn't require quote marks) is run via the
1380 1380 python profiler in a manner similar to the profile.run() function.
1381 1381 Namespaces are internally managed to work correctly; profile.run
1382 1382 cannot be used in IPython because it makes certain assumptions about
1383 1383 namespaces which do not hold under IPython.
1384 1384
1385 1385 Options:
1386 1386
1387 1387 -l <limit>: you can place restrictions on what or how much of the
1388 1388 profile gets printed. The limit value can be:
1389 1389
1390 1390 * A string: only information for function names containing this string
1391 1391 is printed.
1392 1392
1393 1393 * An integer: only these many lines are printed.
1394 1394
1395 1395 * A float (between 0 and 1): this fraction of the report is printed
1396 1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1397 1397
1398 1398 You can combine several limits with repeated use of the option. For
1399 1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1400 1400 information about class constructors.
1401 1401
1402 1402 -r: return the pstats.Stats object generated by the profiling. This
1403 1403 object has all the information about the profile in it, and you can
1404 1404 later use it for further analysis or in other functions.
1405 1405
1406 1406 -s <key>: sort profile by given key. You can provide more than one key
1407 1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1408 1408 default sorting key is 'time'.
1409 1409
1410 1410 The following is copied verbatim from the profile documentation
1411 1411 referenced below:
1412 1412
1413 1413 When more than one key is provided, additional keys are used as
1414 1414 secondary criteria when the there is equality in all keys selected
1415 1415 before them.
1416 1416
1417 1417 Abbreviations can be used for any key names, as long as the
1418 1418 abbreviation is unambiguous. The following are the keys currently
1419 1419 defined:
1420 1420
1421 1421 Valid Arg Meaning
1422 1422 "calls" call count
1423 1423 "cumulative" cumulative time
1424 1424 "file" file name
1425 1425 "module" file name
1426 1426 "pcalls" primitive call count
1427 1427 "line" line number
1428 1428 "name" function name
1429 1429 "nfl" name/file/line
1430 1430 "stdname" standard name
1431 1431 "time" internal time
1432 1432
1433 1433 Note that all sorts on statistics are in descending order (placing
1434 1434 most time consuming items first), where as name, file, and line number
1435 1435 searches are in ascending order (i.e., alphabetical). The subtle
1436 1436 distinction between "nfl" and "stdname" is that the standard name is a
1437 1437 sort of the name as printed, which means that the embedded line
1438 1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1439 1439 would (if the file names were the same) appear in the string order
1440 1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1441 1441 line numbers. In fact, sort_stats("nfl") is the same as
1442 1442 sort_stats("name", "file", "line").
1443 1443
1444 1444 -T <filename>: save profile results as shown on screen to a text
1445 1445 file. The profile is still shown on screen.
1446 1446
1447 1447 -D <filename>: save (via dump_stats) profile statistics to given
1448 1448 filename. This data is in a format understood by the pstats module, and
1449 1449 is generated by a call to the dump_stats() method of profile
1450 1450 objects. The profile is still shown on screen.
1451 1451
1452 1452 -q: suppress output to the pager. Best used with -T and/or -D above.
1453 1453
1454 1454 If you want to run complete programs under the profiler's control, use
1455 1455 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1456 1456 contains profiler specific options as described here.
1457 1457
1458 1458 You can read the complete documentation for the profile module with::
1459 1459
1460 1460 In [1]: import profile; profile.help()
1461 1461 """
1462 1462
1463 1463 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1464 1464
1465 1465 if user_mode: # regular user call
1466 1466 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
1467 1467 list_all=1, posix=False)
1468 1468 namespace = self.shell.user_ns
1469 1469 else: # called to run a program by %run -p
1470 1470 try:
1471 1471 filename = get_py_filename(arg_lst[0])
1472 1472 except IOError as e:
1473 1473 try:
1474 1474 msg = str(e)
1475 1475 except UnicodeError:
1476 1476 msg = e.message
1477 1477 error(msg)
1478 1478 return
1479 1479
1480 1480 arg_str = 'execfile(filename,prog_ns)'
1481 1481 namespace = {
1482 1482 'execfile': self.shell.safe_execfile,
1483 1483 'prog_ns': prog_ns,
1484 1484 'filename': filename
1485 1485 }
1486 1486
1487 1487 opts.merge(opts_def)
1488 1488
1489 1489 prof = profile.Profile()
1490 1490 try:
1491 1491 prof = prof.runctx(arg_str,namespace,namespace)
1492 1492 sys_exit = ''
1493 1493 except SystemExit:
1494 1494 sys_exit = """*** SystemExit exception caught in code being profiled."""
1495 1495
1496 1496 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1497 1497
1498 1498 lims = opts.l
1499 1499 if lims:
1500 1500 lims = [] # rebuild lims with ints/floats/strings
1501 1501 for lim in opts.l:
1502 1502 try:
1503 1503 lims.append(int(lim))
1504 1504 except ValueError:
1505 1505 try:
1506 1506 lims.append(float(lim))
1507 1507 except ValueError:
1508 1508 lims.append(lim)
1509 1509
1510 1510 # Trap output.
1511 1511 stdout_trap = StringIO()
1512 1512
1513 1513 if hasattr(stats,'stream'):
1514 1514 # In newer versions of python, the stats object has a 'stream'
1515 1515 # attribute to write into.
1516 1516 stats.stream = stdout_trap
1517 1517 stats.print_stats(*lims)
1518 1518 else:
1519 1519 # For older versions, we manually redirect stdout during printing
1520 1520 sys_stdout = sys.stdout
1521 1521 try:
1522 1522 sys.stdout = stdout_trap
1523 1523 stats.print_stats(*lims)
1524 1524 finally:
1525 1525 sys.stdout = sys_stdout
1526 1526
1527 1527 output = stdout_trap.getvalue()
1528 1528 output = output.rstrip()
1529 1529
1530 1530 if 'q' not in opts:
1531 1531 page.page(output)
1532 1532 print sys_exit,
1533 1533
1534 1534 dump_file = opts.D[0]
1535 1535 text_file = opts.T[0]
1536 1536 if dump_file:
1537 1537 dump_file = unquote_filename(dump_file)
1538 1538 prof.dump_stats(dump_file)
1539 1539 print '\n*** Profile stats marshalled to file',\
1540 1540 `dump_file`+'.',sys_exit
1541 1541 if text_file:
1542 1542 text_file = unquote_filename(text_file)
1543 1543 pfile = open(text_file,'w')
1544 1544 pfile.write(output)
1545 1545 pfile.close()
1546 1546 print '\n*** Profile printout saved to text file',\
1547 1547 `text_file`+'.',sys_exit
1548 1548
1549 1549 if opts.has_key('r'):
1550 1550 return stats
1551 1551 else:
1552 1552 return None
1553 1553
1554 1554 @skip_doctest
1555 1555 def magic_run(self, parameter_s ='', runner=None,
1556 1556 file_finder=get_py_filename):
1557 1557 """Run the named file inside IPython as a program.
1558 1558
1559 1559 Usage:\\
1560 1560 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1561 1561
1562 1562 Parameters after the filename are passed as command-line arguments to
1563 1563 the program (put in sys.argv). Then, control returns to IPython's
1564 1564 prompt.
1565 1565
1566 1566 This is similar to running at a system prompt:\\
1567 1567 $ python file args\\
1568 1568 but with the advantage of giving you IPython's tracebacks, and of
1569 1569 loading all variables into your interactive namespace for further use
1570 1570 (unless -p is used, see below).
1571 1571
1572 1572 The file is executed in a namespace initially consisting only of
1573 1573 __name__=='__main__' and sys.argv constructed as indicated. It thus
1574 1574 sees its environment as if it were being run as a stand-alone program
1575 1575 (except for sharing global objects such as previously imported
1576 1576 modules). But after execution, the IPython interactive namespace gets
1577 1577 updated with all variables defined in the program (except for __name__
1578 1578 and sys.argv). This allows for very convenient loading of code for
1579 1579 interactive work, while giving each program a 'clean sheet' to run in.
1580 1580
1581 1581 Options:
1582 1582
1583 1583 -n: __name__ is NOT set to '__main__', but to the running file's name
1584 1584 without extension (as python does under import). This allows running
1585 1585 scripts and reloading the definitions in them without calling code
1586 1586 protected by an ' if __name__ == "__main__" ' clause.
1587 1587
1588 1588 -i: run the file in IPython's namespace instead of an empty one. This
1589 1589 is useful if you are experimenting with code written in a text editor
1590 1590 which depends on variables defined interactively.
1591 1591
1592 1592 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1593 1593 being run. This is particularly useful if IPython is being used to
1594 1594 run unittests, which always exit with a sys.exit() call. In such
1595 1595 cases you are interested in the output of the test results, not in
1596 1596 seeing a traceback of the unittest module.
1597 1597
1598 1598 -t: print timing information at the end of the run. IPython will give
1599 1599 you an estimated CPU time consumption for your script, which under
1600 1600 Unix uses the resource module to avoid the wraparound problems of
1601 1601 time.clock(). Under Unix, an estimate of time spent on system tasks
1602 1602 is also given (for Windows platforms this is reported as 0.0).
1603 1603
1604 1604 If -t is given, an additional -N<N> option can be given, where <N>
1605 1605 must be an integer indicating how many times you want the script to
1606 1606 run. The final timing report will include total and per run results.
1607 1607
1608 1608 For example (testing the script uniq_stable.py)::
1609 1609
1610 1610 In [1]: run -t uniq_stable
1611 1611
1612 1612 IPython CPU timings (estimated):\\
1613 1613 User : 0.19597 s.\\
1614 1614 System: 0.0 s.\\
1615 1615
1616 1616 In [2]: run -t -N5 uniq_stable
1617 1617
1618 1618 IPython CPU timings (estimated):\\
1619 1619 Total runs performed: 5\\
1620 1620 Times : Total Per run\\
1621 1621 User : 0.910862 s, 0.1821724 s.\\
1622 1622 System: 0.0 s, 0.0 s.
1623 1623
1624 1624 -d: run your program under the control of pdb, the Python debugger.
1625 1625 This allows you to execute your program step by step, watch variables,
1626 1626 etc. Internally, what IPython does is similar to calling:
1627 1627
1628 1628 pdb.run('execfile("YOURFILENAME")')
1629 1629
1630 1630 with a breakpoint set on line 1 of your file. You can change the line
1631 1631 number for this automatic breakpoint to be <N> by using the -bN option
1632 1632 (where N must be an integer). For example::
1633 1633
1634 1634 %run -d -b40 myscript
1635 1635
1636 1636 will set the first breakpoint at line 40 in myscript.py. Note that
1637 1637 the first breakpoint must be set on a line which actually does
1638 1638 something (not a comment or docstring) for it to stop execution.
1639 1639
1640 1640 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1641 1641 first enter 'c' (without quotes) to start execution up to the first
1642 1642 breakpoint.
1643 1643
1644 1644 Entering 'help' gives information about the use of the debugger. You
1645 1645 can easily see pdb's full documentation with "import pdb;pdb.help()"
1646 1646 at a prompt.
1647 1647
1648 1648 -p: run program under the control of the Python profiler module (which
1649 1649 prints a detailed report of execution times, function calls, etc).
1650 1650
1651 1651 You can pass other options after -p which affect the behavior of the
1652 1652 profiler itself. See the docs for %prun for details.
1653 1653
1654 1654 In this mode, the program's variables do NOT propagate back to the
1655 1655 IPython interactive namespace (because they remain in the namespace
1656 1656 where the profiler executes them).
1657 1657
1658 1658 Internally this triggers a call to %prun, see its documentation for
1659 1659 details on the options available specifically for profiling.
1660 1660
1661 1661 There is one special usage for which the text above doesn't apply:
1662 1662 if the filename ends with .ipy, the file is run as ipython script,
1663 1663 just as if the commands were written on IPython prompt.
1664 1664
1665 1665 -m: specify module name to load instead of script path. Similar to
1666 1666 the -m option for the python interpreter. Use this option last if you
1667 1667 want to combine with other %run options. Unlike the python interpreter
1668 1668 only source modules are allowed no .pyc or .pyo files.
1669 1669 For example::
1670 1670
1671 1671 %run -m example
1672 1672
1673 1673 will run the example module.
1674 1674
1675 1675 """
1676 1676
1677 1677 # get arguments and set sys.argv for program to be run.
1678 1678 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
1679 1679 mode='list', list_all=1)
1680 1680 if "m" in opts:
1681 1681 modulename = opts["m"][0]
1682 1682 modpath = find_mod(modulename)
1683 1683 if modpath is None:
1684 1684 warn('%r is not a valid modulename on sys.path'%modulename)
1685 1685 return
1686 1686 arg_lst = [modpath] + arg_lst
1687 1687 try:
1688 1688 filename = file_finder(arg_lst[0])
1689 1689 except IndexError:
1690 1690 warn('you must provide at least a filename.')
1691 1691 print '\n%run:\n', oinspect.getdoc(self.magic_run)
1692 1692 return
1693 1693 except IOError as e:
1694 1694 try:
1695 1695 msg = str(e)
1696 1696 except UnicodeError:
1697 1697 msg = e.message
1698 1698 error(msg)
1699 1699 return
1700 1700
1701 1701 if filename.lower().endswith('.ipy'):
1702 1702 self.shell.safe_execfile_ipy(filename)
1703 1703 return
1704 1704
1705 1705 # Control the response to exit() calls made by the script being run
1706 1706 exit_ignore = 'e' in opts
1707 1707
1708 1708 # Make sure that the running script gets a proper sys.argv as if it
1709 1709 # were run from a system shell.
1710 1710 save_argv = sys.argv # save it for later restoring
1711 1711
1712 1712 # simulate shell expansion on arguments, at least tilde expansion
1713 1713 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
1714 1714
1715 1715 sys.argv = [filename] + args # put in the proper filename
1716 1716 # protect sys.argv from potential unicode strings on Python 2:
1717 1717 if not py3compat.PY3:
1718 1718 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
1719 1719
1720 1720 if 'i' in opts:
1721 1721 # Run in user's interactive namespace
1722 1722 prog_ns = self.shell.user_ns
1723 1723 __name__save = self.shell.user_ns['__name__']
1724 1724 prog_ns['__name__'] = '__main__'
1725 1725 main_mod = self.shell.new_main_mod(prog_ns)
1726 1726 else:
1727 1727 # Run in a fresh, empty namespace
1728 1728 if 'n' in opts:
1729 1729 name = os.path.splitext(os.path.basename(filename))[0]
1730 1730 else:
1731 1731 name = '__main__'
1732 1732
1733 1733 main_mod = self.shell.new_main_mod()
1734 1734 prog_ns = main_mod.__dict__
1735 1735 prog_ns['__name__'] = name
1736 1736
1737 1737 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1738 1738 # set the __file__ global in the script's namespace
1739 1739 prog_ns['__file__'] = filename
1740 1740
1741 1741 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1742 1742 # that, if we overwrite __main__, we replace it at the end
1743 1743 main_mod_name = prog_ns['__name__']
1744 1744
1745 1745 if main_mod_name == '__main__':
1746 1746 restore_main = sys.modules['__main__']
1747 1747 else:
1748 1748 restore_main = False
1749 1749
1750 1750 # This needs to be undone at the end to prevent holding references to
1751 1751 # every single object ever created.
1752 1752 sys.modules[main_mod_name] = main_mod
1753 1753
1754 1754 try:
1755 1755 stats = None
1756 1756 with self.shell.readline_no_record:
1757 1757 if 'p' in opts:
1758 1758 stats = self.magic_prun('', 0, opts, arg_lst, prog_ns)
1759 1759 else:
1760 1760 if 'd' in opts:
1761 1761 deb = debugger.Pdb(self.shell.colors)
1762 1762 # reset Breakpoint state, which is moronically kept
1763 1763 # in a class
1764 1764 bdb.Breakpoint.next = 1
1765 1765 bdb.Breakpoint.bplist = {}
1766 1766 bdb.Breakpoint.bpbynumber = [None]
1767 1767 # Set an initial breakpoint to stop execution
1768 1768 maxtries = 10
1769 1769 bp = int(opts.get('b', [1])[0])
1770 1770 checkline = deb.checkline(filename, bp)
1771 1771 if not checkline:
1772 1772 for bp in range(bp + 1, bp + maxtries + 1):
1773 1773 if deb.checkline(filename, bp):
1774 1774 break
1775 1775 else:
1776 1776 msg = ("\nI failed to find a valid line to set "
1777 1777 "a breakpoint\n"
1778 1778 "after trying up to line: %s.\n"
1779 1779 "Please set a valid breakpoint manually "
1780 1780 "with the -b option." % bp)
1781 1781 error(msg)
1782 1782 return
1783 1783 # if we find a good linenumber, set the breakpoint
1784 1784 deb.do_break('%s:%s' % (filename, bp))
1785 1785 # Start file run
1786 1786 print "NOTE: Enter 'c' at the",
1787 1787 print "%s prompt to start your script." % deb.prompt
1788 1788 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
1789 1789 try:
1790 1790 deb.run('execfile("%s", prog_ns)' % filename, ns)
1791 1791
1792 1792 except:
1793 1793 etype, value, tb = sys.exc_info()
1794 1794 # Skip three frames in the traceback: the %run one,
1795 1795 # one inside bdb.py, and the command-line typed by the
1796 1796 # user (run by exec in pdb itself).
1797 1797 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
1798 1798 else:
1799 1799 if runner is None:
1800 1800 runner = self.default_runner
1801 1801 if runner is None:
1802 1802 runner = self.shell.safe_execfile
1803 1803 if 't' in opts:
1804 1804 # timed execution
1805 1805 try:
1806 1806 nruns = int(opts['N'][0])
1807 1807 if nruns < 1:
1808 1808 error('Number of runs must be >=1')
1809 1809 return
1810 1810 except (KeyError):
1811 1811 nruns = 1
1812 1812 twall0 = time.time()
1813 1813 if nruns == 1:
1814 1814 t0 = clock2()
1815 1815 runner(filename, prog_ns, prog_ns,
1816 1816 exit_ignore=exit_ignore)
1817 1817 t1 = clock2()
1818 1818 t_usr = t1[0] - t0[0]
1819 1819 t_sys = t1[1] - t0[1]
1820 1820 print "\nIPython CPU timings (estimated):"
1821 1821 print " User : %10.2f s." % t_usr
1822 1822 print " System : %10.2f s." % t_sys
1823 1823 else:
1824 1824 runs = range(nruns)
1825 1825 t0 = clock2()
1826 1826 for nr in runs:
1827 1827 runner(filename, prog_ns, prog_ns,
1828 1828 exit_ignore=exit_ignore)
1829 1829 t1 = clock2()
1830 1830 t_usr = t1[0] - t0[0]
1831 1831 t_sys = t1[1] - t0[1]
1832 1832 print "\nIPython CPU timings (estimated):"
1833 1833 print "Total runs performed:", nruns
1834 1834 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
1835 1835 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
1836 1836 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
1837 1837 twall1 = time.time()
1838 1838 print "Wall time: %10.2f s." % (twall1 - twall0)
1839 1839
1840 1840 else:
1841 1841 # regular execution
1842 1842 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
1843 1843
1844 1844 if 'i' in opts:
1845 1845 self.shell.user_ns['__name__'] = __name__save
1846 1846 else:
1847 1847 # The shell MUST hold a reference to prog_ns so after %run
1848 1848 # exits, the python deletion mechanism doesn't zero it out
1849 1849 # (leaving dangling references).
1850 1850 self.shell.cache_main_mod(prog_ns, filename)
1851 1851 # update IPython interactive namespace
1852 1852
1853 1853 # Some forms of read errors on the file may mean the
1854 1854 # __name__ key was never set; using pop we don't have to
1855 1855 # worry about a possible KeyError.
1856 1856 prog_ns.pop('__name__', None)
1857 1857
1858 1858 self.shell.user_ns.update(prog_ns)
1859 1859 finally:
1860 1860 # It's a bit of a mystery why, but __builtins__ can change from
1861 1861 # being a module to becoming a dict missing some key data after
1862 1862 # %run. As best I can see, this is NOT something IPython is doing
1863 1863 # at all, and similar problems have been reported before:
1864 1864 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1865 1865 # Since this seems to be done by the interpreter itself, the best
1866 1866 # we can do is to at least restore __builtins__ for the user on
1867 1867 # exit.
1868 1868 self.shell.user_ns['__builtins__'] = builtin_mod
1869 1869
1870 1870 # Ensure key global structures are restored
1871 1871 sys.argv = save_argv
1872 1872 if restore_main:
1873 1873 sys.modules['__main__'] = restore_main
1874 1874 else:
1875 1875 # Remove from sys.modules the reference to main_mod we'd
1876 1876 # added. Otherwise it will trap references to objects
1877 1877 # contained therein.
1878 1878 del sys.modules[main_mod_name]
1879 1879
1880 1880 return stats
1881 1881
1882 1882 @skip_doctest
1883 1883 def magic_timeit(self, parameter_s =''):
1884 1884 """Time execution of a Python statement or expression
1885 1885
1886 1886 Usage:\\
1887 1887 %timeit [-n<N> -r<R> [-t|-c]] statement
1888 1888
1889 1889 Time execution of a Python statement or expression using the timeit
1890 1890 module.
1891 1891
1892 1892 Options:
1893 1893 -n<N>: execute the given statement <N> times in a loop. If this value
1894 1894 is not given, a fitting value is chosen.
1895 1895
1896 1896 -r<R>: repeat the loop iteration <R> times and take the best result.
1897 1897 Default: 3
1898 1898
1899 1899 -t: use time.time to measure the time, which is the default on Unix.
1900 1900 This function measures wall time.
1901 1901
1902 1902 -c: use time.clock to measure the time, which is the default on
1903 1903 Windows and measures wall time. On Unix, resource.getrusage is used
1904 1904 instead and returns the CPU user time.
1905 1905
1906 1906 -p<P>: use a precision of <P> digits to display the timing result.
1907 1907 Default: 3
1908 1908
1909 1909
1910 1910 Examples
1911 1911 --------
1912 1912 ::
1913 1913
1914 1914 In [1]: %timeit pass
1915 1915 10000000 loops, best of 3: 53.3 ns per loop
1916 1916
1917 1917 In [2]: u = None
1918 1918
1919 1919 In [3]: %timeit u is None
1920 1920 10000000 loops, best of 3: 184 ns per loop
1921 1921
1922 1922 In [4]: %timeit -r 4 u == None
1923 1923 1000000 loops, best of 4: 242 ns per loop
1924 1924
1925 1925 In [5]: import time
1926 1926
1927 1927 In [6]: %timeit -n1 time.sleep(2)
1928 1928 1 loops, best of 3: 2 s per loop
1929 1929
1930 1930
1931 1931 The times reported by %timeit will be slightly higher than those
1932 1932 reported by the timeit.py script when variables are accessed. This is
1933 1933 due to the fact that %timeit executes the statement in the namespace
1934 1934 of the shell, compared with timeit.py, which uses a single setup
1935 1935 statement to import function or create variables. Generally, the bias
1936 1936 does not matter as long as results from timeit.py are not mixed with
1937 1937 those from %timeit."""
1938 1938
1939 1939 import timeit
1940 1940 import math
1941 1941
1942 1942 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1943 1943 # certain terminals. Until we figure out a robust way of
1944 1944 # auto-detecting if the terminal can deal with it, use plain 'us' for
1945 1945 # microseconds. I am really NOT happy about disabling the proper
1946 1946 # 'micro' prefix, but crashing is worse... If anyone knows what the
1947 1947 # right solution for this is, I'm all ears...
1948 1948 #
1949 1949 # Note: using
1950 1950 #
1951 1951 # s = u'\xb5'
1952 1952 # s.encode(sys.getdefaultencoding())
1953 1953 #
1954 1954 # is not sufficient, as I've seen terminals where that fails but
1955 1955 # print s
1956 1956 #
1957 1957 # succeeds
1958 1958 #
1959 1959 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1960 1960
1961 1961 #units = [u"s", u"ms",u'\xb5',"ns"]
1962 1962 units = [u"s", u"ms",u'us',"ns"]
1963 1963
1964 1964 scaling = [1, 1e3, 1e6, 1e9]
1965 1965
1966 1966 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1967 1967 posix=False, strict=False)
1968 1968 if stmt == "":
1969 1969 return
1970 1970 timefunc = timeit.default_timer
1971 1971 number = int(getattr(opts, "n", 0))
1972 1972 repeat = int(getattr(opts, "r", timeit.default_repeat))
1973 1973 precision = int(getattr(opts, "p", 3))
1974 1974 if hasattr(opts, "t"):
1975 1975 timefunc = time.time
1976 1976 if hasattr(opts, "c"):
1977 1977 timefunc = clock
1978 1978
1979 1979 timer = timeit.Timer(timer=timefunc)
1980 1980 # this code has tight coupling to the inner workings of timeit.Timer,
1981 1981 # but is there a better way to achieve that the code stmt has access
1982 1982 # to the shell namespace?
1983 1983
1984 1984 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1985 1985 'setup': "pass"}
1986 1986 # Track compilation time so it can be reported if too long
1987 1987 # Minimum time above which compilation time will be reported
1988 1988 tc_min = 0.1
1989 1989
1990 1990 t0 = clock()
1991 1991 code = compile(src, "<magic-timeit>", "exec")
1992 1992 tc = clock()-t0
1993 1993
1994 1994 ns = {}
1995 1995 exec code in self.shell.user_ns, ns
1996 1996 timer.inner = ns["inner"]
1997 1997
1998 1998 if number == 0:
1999 1999 # determine number so that 0.2 <= total time < 2.0
2000 2000 number = 1
2001 2001 for i in range(1, 10):
2002 2002 if timer.timeit(number) >= 0.2:
2003 2003 break
2004 2004 number *= 10
2005 2005
2006 2006 best = min(timer.repeat(repeat, number)) / number
2007 2007
2008 2008 if best > 0.0 and best < 1000.0:
2009 2009 order = min(-int(math.floor(math.log10(best)) // 3), 3)
2010 2010 elif best >= 1000.0:
2011 2011 order = 0
2012 2012 else:
2013 2013 order = 3
2014 2014 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
2015 2015 precision,
2016 2016 best * scaling[order],
2017 2017 units[order])
2018 2018 if tc > tc_min:
2019 2019 print "Compiler time: %.2f s" % tc
2020 2020
2021 2021 @skip_doctest
2022 2022 @needs_local_scope
2023 2023 def magic_time(self,parameter_s, user_locals):
2024 2024 """Time execution of a Python statement or expression.
2025 2025
2026 2026 The CPU and wall clock times are printed, and the value of the
2027 2027 expression (if any) is returned. Note that under Win32, system time
2028 2028 is always reported as 0, since it can not be measured.
2029 2029
2030 2030 This function provides very basic timing functionality. In Python
2031 2031 2.3, the timeit module offers more control and sophistication, so this
2032 2032 could be rewritten to use it (patches welcome).
2033 2033
2034 2034 Examples
2035 2035 --------
2036 2036 ::
2037 2037
2038 2038 In [1]: time 2**128
2039 2039 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2040 2040 Wall time: 0.00
2041 2041 Out[1]: 340282366920938463463374607431768211456L
2042 2042
2043 2043 In [2]: n = 1000000
2044 2044
2045 2045 In [3]: time sum(range(n))
2046 2046 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
2047 2047 Wall time: 1.37
2048 2048 Out[3]: 499999500000L
2049 2049
2050 2050 In [4]: time print 'hello world'
2051 2051 hello world
2052 2052 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2053 2053 Wall time: 0.00
2054 2054
2055 2055 Note that the time needed by Python to compile the given expression
2056 2056 will be reported if it is more than 0.1s. In this example, the
2057 2057 actual exponentiation is done by Python at compilation time, so while
2058 2058 the expression can take a noticeable amount of time to compute, that
2059 2059 time is purely due to the compilation:
2060 2060
2061 2061 In [5]: time 3**9999;
2062 2062 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2063 2063 Wall time: 0.00 s
2064 2064
2065 2065 In [6]: time 3**999999;
2066 2066 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2067 2067 Wall time: 0.00 s
2068 2068 Compiler : 0.78 s
2069 2069 """
2070 2070
2071 2071 # fail immediately if the given expression can't be compiled
2072 2072
2073 2073 expr = self.shell.prefilter(parameter_s,False)
2074 2074
2075 2075 # Minimum time above which compilation time will be reported
2076 2076 tc_min = 0.1
2077 2077
2078 2078 try:
2079 2079 mode = 'eval'
2080 2080 t0 = clock()
2081 2081 code = compile(expr,'<timed eval>',mode)
2082 2082 tc = clock()-t0
2083 2083 except SyntaxError:
2084 2084 mode = 'exec'
2085 2085 t0 = clock()
2086 2086 code = compile(expr,'<timed exec>',mode)
2087 2087 tc = clock()-t0
2088 2088 # skew measurement as little as possible
2089 2089 glob = self.shell.user_ns
2090 2090 wtime = time.time
2091 2091 # time execution
2092 2092 wall_st = wtime()
2093 2093 if mode=='eval':
2094 2094 st = clock2()
2095 2095 out = eval(code, glob, user_locals)
2096 2096 end = clock2()
2097 2097 else:
2098 2098 st = clock2()
2099 2099 exec code in glob, user_locals
2100 2100 end = clock2()
2101 2101 out = None
2102 2102 wall_end = wtime()
2103 2103 # Compute actual times and report
2104 2104 wall_time = wall_end-wall_st
2105 2105 cpu_user = end[0]-st[0]
2106 2106 cpu_sys = end[1]-st[1]
2107 2107 cpu_tot = cpu_user+cpu_sys
2108 2108 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2109 2109 (cpu_user,cpu_sys,cpu_tot)
2110 2110 print "Wall time: %.2f s" % wall_time
2111 2111 if tc > tc_min:
2112 2112 print "Compiler : %.2f s" % tc
2113 2113 return out
2114 2114
2115 2115 @skip_doctest
2116 2116 def magic_macro(self,parameter_s = ''):
2117 2117 """Define a macro for future re-execution. It accepts ranges of history,
2118 2118 filenames or string objects.
2119 2119
2120 2120 Usage:\\
2121 2121 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2122 2122
2123 2123 Options:
2124 2124
2125 2125 -r: use 'raw' input. By default, the 'processed' history is used,
2126 2126 so that magics are loaded in their transformed version to valid
2127 2127 Python. If this option is given, the raw input as typed as the
2128 2128 command line is used instead.
2129 2129
2130 2130 This will define a global variable called `name` which is a string
2131 2131 made of joining the slices and lines you specify (n1,n2,... numbers
2132 2132 above) from your input history into a single string. This variable
2133 2133 acts like an automatic function which re-executes those lines as if
2134 2134 you had typed them. You just type 'name' at the prompt and the code
2135 2135 executes.
2136 2136
2137 2137 The syntax for indicating input ranges is described in %history.
2138 2138
2139 2139 Note: as a 'hidden' feature, you can also use traditional python slice
2140 2140 notation, where N:M means numbers N through M-1.
2141 2141
2142 2142 For example, if your history contains (%hist prints it)::
2143 2143
2144 2144 44: x=1
2145 2145 45: y=3
2146 2146 46: z=x+y
2147 2147 47: print x
2148 2148 48: a=5
2149 2149 49: print 'x',x,'y',y
2150 2150
2151 2151 you can create a macro with lines 44 through 47 (included) and line 49
2152 2152 called my_macro with::
2153 2153
2154 2154 In [55]: %macro my_macro 44-47 49
2155 2155
2156 2156 Now, typing `my_macro` (without quotes) will re-execute all this code
2157 2157 in one pass.
2158 2158
2159 2159 You don't need to give the line-numbers in order, and any given line
2160 2160 number can appear multiple times. You can assemble macros with any
2161 2161 lines from your input history in any order.
2162 2162
2163 2163 The macro is a simple object which holds its value in an attribute,
2164 2164 but IPython's display system checks for macros and executes them as
2165 2165 code instead of printing them when you type their name.
2166 2166
2167 2167 You can view a macro's contents by explicitly printing it with::
2168 2168
2169 2169 print macro_name
2170 2170
2171 2171 """
2172 2172 opts,args = self.parse_options(parameter_s,'r',mode='list')
2173 2173 if not args: # List existing macros
2174 2174 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
2175 2175 isinstance(v, Macro))
2176 2176 if len(args) == 1:
2177 2177 raise UsageError(
2178 2178 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2179 2179 name, codefrom = args[0], " ".join(args[1:])
2180 2180
2181 2181 #print 'rng',ranges # dbg
2182 2182 try:
2183 2183 lines = self.shell.find_user_code(codefrom, 'r' in opts)
2184 2184 except (ValueError, TypeError) as e:
2185 2185 print e.args[0]
2186 2186 return
2187 2187 macro = Macro(lines)
2188 2188 self.shell.define_macro(name, macro)
2189 2189 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2190 2190 print '=== Macro contents: ==='
2191 2191 print macro,
2192 2192
2193 2193 def magic_save(self,parameter_s = ''):
2194 2194 """Save a set of lines or a macro to a given filename.
2195 2195
2196 2196 Usage:\\
2197 2197 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2198 2198
2199 2199 Options:
2200 2200
2201 2201 -r: use 'raw' input. By default, the 'processed' history is used,
2202 2202 so that magics are loaded in their transformed version to valid
2203 2203 Python. If this option is given, the raw input as typed as the
2204 2204 command line is used instead.
2205 2205
2206 2206 This function uses the same syntax as %history for input ranges,
2207 2207 then saves the lines to the filename you specify.
2208 2208
2209 2209 It adds a '.py' extension to the file if you don't do so yourself, and
2210 2210 it asks for confirmation before overwriting existing files."""
2211 2211
2212 2212 opts,args = self.parse_options(parameter_s,'r',mode='list')
2213 2213 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
2214 2214 if not fname.endswith('.py'):
2215 2215 fname += '.py'
2216 2216 if os.path.isfile(fname):
2217 2217 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
2218 2218 if not overwrite :
2219 2219 print 'Operation cancelled.'
2220 2220 return
2221 2221 try:
2222 2222 cmds = self.shell.find_user_code(codefrom, 'r' in opts)
2223 2223 except (TypeError, ValueError) as e:
2224 2224 print e.args[0]
2225 2225 return
2226 2226 with io.open(fname,'w', encoding="utf-8") as f:
2227 2227 f.write(u"# coding: utf-8\n")
2228 2228 f.write(py3compat.cast_unicode(cmds))
2229 2229 print 'The following commands were written to file `%s`:' % fname
2230 2230 print cmds
2231 2231
2232 2232 def magic_pastebin(self, parameter_s = ''):
2233 2233 """Upload code to Github's Gist paste bin, returning the URL.
2234 2234
2235 2235 Usage:\\
2236 2236 %pastebin [-d "Custom description"] 1-7
2237 2237
2238 2238 The argument can be an input history range, a filename, or the name of a
2239 2239 string or macro.
2240 2240
2241 2241 Options:
2242 2242
2243 2243 -d: Pass a custom description for the gist. The default will say
2244 2244 "Pasted from IPython".
2245 2245 """
2246 2246 opts, args = self.parse_options(parameter_s, 'd:')
2247 2247
2248 2248 try:
2249 2249 code = self.shell.find_user_code(args)
2250 2250 except (ValueError, TypeError) as e:
2251 2251 print e.args[0]
2252 2252 return
2253 2253
2254 2254 post_data = json.dumps({
2255 2255 "description": opts.get('d', "Pasted from IPython"),
2256 2256 "public": True,
2257 2257 "files": {
2258 2258 "file1.py": {
2259 2259 "content": code
2260 2260 }
2261 2261 }
2262 2262 }).encode('utf-8')
2263 2263
2264 2264 response = urlopen("https://api.github.com/gists", post_data)
2265 2265 response_data = json.loads(response.read().decode('utf-8'))
2266 2266 return response_data['html_url']
2267 2267
2268 2268 def magic_loadpy(self, arg_s):
2269 2269 """Alias of `%load`
2270 2270
2271 2271 `%loadpy` has gained some flexibility and droped the requirement of a `.py`
2272 2272 extension. So it has been renamed simply into %load. You can look at
2273 2273 `%load`'s docstring for more info.
2274 2274 """
2275 2275 self.magic_load(arg_s)
2276 2276
2277 2277 def magic_load(self, arg_s):
2278 2278 """Load code into the current frontend.
2279 2279
2280 2280 Usage:\\
2281 2281 %load [options] source
2282 2282
2283 2283 where source can be a filename, URL, input history range or macro
2284 2284
2285 2285 Options:
2286 2286 --------
2287 2287 -y : Don't ask confirmation for loading source above 200 000 characters.
2288 2288
2289 2289 This magic command can either take a local filename, a URL, an history
2290 2290 range (see %history) or a macro as argument, it will prompt for
2291 2291 confirmation before loading source with more than 200 000 characters, unless
2292 2292 -y flag is passed or if the frontend does not support raw_input::
2293 2293
2294 2294 %load myscript.py
2295 2295 %load 7-27
2296 2296 %load myMacro
2297 2297 %load http://www.example.com/myscript.py
2298 2298 """
2299 2299 opts,args = self.parse_options(arg_s,'y')
2300 2300
2301 2301 contents = self.shell.find_user_code(args)
2302 2302 l = len(contents)
2303 2303
2304 2304 # 200 000 is ~ 2500 full 80 caracter lines
2305 2305 # so in average, more than 5000 lines
2306 2306 if l > 200000 and 'y' not in opts:
2307 2307 try:
2308 2308 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
2309 2309 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
2310 2310 except StdinNotImplementedError:
2311 2311 #asume yes if raw input not implemented
2312 2312 ans = True
2313 2313
2314 2314 if ans is False :
2315 2315 print 'Operation cancelled.'
2316 2316 return
2317 2317
2318 2318 self.set_next_input(contents)
2319 2319
2320 2320 def _find_edit_target(self, args, opts, last_call):
2321 2321 """Utility method used by magic_edit to find what to edit."""
2322 2322
2323 2323 def make_filename(arg):
2324 2324 "Make a filename from the given args"
2325 2325 arg = unquote_filename(arg)
2326 2326 try:
2327 2327 filename = get_py_filename(arg)
2328 2328 except IOError:
2329 2329 # If it ends with .py but doesn't already exist, assume we want
2330 2330 # a new file.
2331 2331 if arg.endswith('.py'):
2332 2332 filename = arg
2333 2333 else:
2334 2334 filename = None
2335 2335 return filename
2336 2336
2337 2337 # Set a few locals from the options for convenience:
2338 2338 opts_prev = 'p' in opts
2339 2339 opts_raw = 'r' in opts
2340 2340
2341 2341 # custom exceptions
2342 2342 class DataIsObject(Exception): pass
2343 2343
2344 2344 # Default line number value
2345 2345 lineno = opts.get('n',None)
2346 2346
2347 2347 if opts_prev:
2348 2348 args = '_%s' % last_call[0]
2349 2349 if not self.shell.user_ns.has_key(args):
2350 2350 args = last_call[1]
2351 2351
2352 2352 # use last_call to remember the state of the previous call, but don't
2353 2353 # let it be clobbered by successive '-p' calls.
2354 2354 try:
2355 2355 last_call[0] = self.shell.displayhook.prompt_count
2356 2356 if not opts_prev:
2357 2357 last_call[1] = args
2358 2358 except:
2359 2359 pass
2360 2360
2361 2361 # by default this is done with temp files, except when the given
2362 2362 # arg is a filename
2363 2363 use_temp = True
2364 2364
2365 2365 data = ''
2366 2366
2367 2367 # First, see if the arguments should be a filename.
2368 2368 filename = make_filename(args)
2369 2369 if filename:
2370 2370 use_temp = False
2371 2371 elif args:
2372 2372 # Mode where user specifies ranges of lines, like in %macro.
2373 2373 data = self.extract_input_lines(args, opts_raw)
2374 2374 if not data:
2375 2375 try:
2376 2376 # Load the parameter given as a variable. If not a string,
2377 2377 # process it as an object instead (below)
2378 2378
2379 2379 #print '*** args',args,'type',type(args) # dbg
2380 2380 data = eval(args, self.shell.user_ns)
2381 2381 if not isinstance(data, basestring):
2382 2382 raise DataIsObject
2383 2383
2384 2384 except (NameError,SyntaxError):
2385 2385 # given argument is not a variable, try as a filename
2386 2386 filename = make_filename(args)
2387 2387 if filename is None:
2388 2388 warn("Argument given (%s) can't be found as a variable "
2389 2389 "or as a filename." % args)
2390 2390 return
2391 2391 use_temp = False
2392 2392
2393 2393 except DataIsObject:
2394 2394 # macros have a special edit function
2395 2395 if isinstance(data, Macro):
2396 2396 raise MacroToEdit(data)
2397 2397
2398 2398 # For objects, try to edit the file where they are defined
2399 2399 try:
2400 2400 filename = inspect.getabsfile(data)
2401 2401 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2402 2402 # class created by %edit? Try to find source
2403 2403 # by looking for method definitions instead, the
2404 2404 # __module__ in those classes is FakeModule.
2405 2405 attrs = [getattr(data, aname) for aname in dir(data)]
2406 2406 for attr in attrs:
2407 2407 if not inspect.ismethod(attr):
2408 2408 continue
2409 2409 filename = inspect.getabsfile(attr)
2410 2410 if filename and 'fakemodule' not in filename.lower():
2411 2411 # change the attribute to be the edit target instead
2412 2412 data = attr
2413 2413 break
2414 2414
2415 2415 datafile = 1
2416 2416 except TypeError:
2417 2417 filename = make_filename(args)
2418 2418 datafile = 1
2419 2419 warn('Could not find file where `%s` is defined.\n'
2420 2420 'Opening a file named `%s`' % (args,filename))
2421 2421 # Now, make sure we can actually read the source (if it was in
2422 2422 # a temp file it's gone by now).
2423 2423 if datafile:
2424 2424 try:
2425 2425 if lineno is None:
2426 2426 lineno = inspect.getsourcelines(data)[1]
2427 2427 except IOError:
2428 2428 filename = make_filename(args)
2429 2429 if filename is None:
2430 2430 warn('The file `%s` where `%s` was defined cannot '
2431 2431 'be read.' % (filename,data))
2432 2432 return
2433 2433 use_temp = False
2434 2434
2435 2435 if use_temp:
2436 2436 filename = self.shell.mktempfile(data)
2437 2437 print 'IPython will make a temporary file named:',filename
2438 2438
2439 2439 return filename, lineno, use_temp
2440 2440
2441 2441 def _edit_macro(self,mname,macro):
2442 2442 """open an editor with the macro data in a file"""
2443 2443 filename = self.shell.mktempfile(macro.value)
2444 2444 self.shell.hooks.editor(filename)
2445 2445
2446 2446 # and make a new macro object, to replace the old one
2447 2447 mfile = open(filename)
2448 2448 mvalue = mfile.read()
2449 2449 mfile.close()
2450 2450 self.shell.user_ns[mname] = Macro(mvalue)
2451 2451
2452 2452 def magic_ed(self,parameter_s=''):
2453 2453 """Alias to %edit."""
2454 2454 return self.magic_edit(parameter_s)
2455 2455
2456 2456 @skip_doctest
2457 2457 def magic_edit(self,parameter_s='',last_call=['','']):
2458 2458 """Bring up an editor and execute the resulting code.
2459 2459
2460 2460 Usage:
2461 2461 %edit [options] [args]
2462 2462
2463 2463 %edit runs IPython's editor hook. The default version of this hook is
2464 2464 set to call the editor specified by your $EDITOR environment variable.
2465 2465 If this isn't found, it will default to vi under Linux/Unix and to
2466 2466 notepad under Windows. See the end of this docstring for how to change
2467 2467 the editor hook.
2468 2468
2469 2469 You can also set the value of this editor via the
2470 2470 ``TerminalInteractiveShell.editor`` option in your configuration file.
2471 2471 This is useful if you wish to use a different editor from your typical
2472 2472 default with IPython (and for Windows users who typically don't set
2473 2473 environment variables).
2474 2474
2475 2475 This command allows you to conveniently edit multi-line code right in
2476 2476 your IPython session.
2477 2477
2478 2478 If called without arguments, %edit opens up an empty editor with a
2479 2479 temporary file and will execute the contents of this file when you
2480 2480 close it (don't forget to save it!).
2481 2481
2482 2482
2483 2483 Options:
2484 2484
2485 2485 -n <number>: open the editor at a specified line number. By default,
2486 2486 the IPython editor hook uses the unix syntax 'editor +N filename', but
2487 2487 you can configure this by providing your own modified hook if your
2488 2488 favorite editor supports line-number specifications with a different
2489 2489 syntax.
2490 2490
2491 2491 -p: this will call the editor with the same data as the previous time
2492 2492 it was used, regardless of how long ago (in your current session) it
2493 2493 was.
2494 2494
2495 2495 -r: use 'raw' input. This option only applies to input taken from the
2496 2496 user's history. By default, the 'processed' history is used, so that
2497 2497 magics are loaded in their transformed version to valid Python. If
2498 2498 this option is given, the raw input as typed as the command line is
2499 2499 used instead. When you exit the editor, it will be executed by
2500 2500 IPython's own processor.
2501 2501
2502 2502 -x: do not execute the edited code immediately upon exit. This is
2503 2503 mainly useful if you are editing programs which need to be called with
2504 2504 command line arguments, which you can then do using %run.
2505 2505
2506 2506
2507 2507 Arguments:
2508 2508
2509 2509 If arguments are given, the following possibilities exist:
2510 2510
2511 2511 - If the argument is a filename, IPython will load that into the
2512 2512 editor. It will execute its contents with execfile() when you exit,
2513 2513 loading any code in the file into your interactive namespace.
2514 2514
2515 2515 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
2516 2516 The syntax is the same as in the %history magic.
2517 2517
2518 2518 - If the argument is a string variable, its contents are loaded
2519 2519 into the editor. You can thus edit any string which contains
2520 2520 python code (including the result of previous edits).
2521 2521
2522 2522 - If the argument is the name of an object (other than a string),
2523 2523 IPython will try to locate the file where it was defined and open the
2524 2524 editor at the point where it is defined. You can use `%edit function`
2525 2525 to load an editor exactly at the point where 'function' is defined,
2526 2526 edit it and have the file be executed automatically.
2527 2527
2528 2528 - If the object is a macro (see %macro for details), this opens up your
2529 2529 specified editor with a temporary file containing the macro's data.
2530 2530 Upon exit, the macro is reloaded with the contents of the file.
2531 2531
2532 2532 Note: opening at an exact line is only supported under Unix, and some
2533 2533 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2534 2534 '+NUMBER' parameter necessary for this feature. Good editors like
2535 2535 (X)Emacs, vi, jed, pico and joe all do.
2536 2536
2537 2537 After executing your code, %edit will return as output the code you
2538 2538 typed in the editor (except when it was an existing file). This way
2539 2539 you can reload the code in further invocations of %edit as a variable,
2540 2540 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2541 2541 the output.
2542 2542
2543 2543 Note that %edit is also available through the alias %ed.
2544 2544
2545 2545 This is an example of creating a simple function inside the editor and
2546 2546 then modifying it. First, start up the editor::
2547 2547
2548 2548 In [1]: ed
2549 2549 Editing... done. Executing edited code...
2550 2550 Out[1]: 'def foo():\\n print "foo() was defined in an editing
2551 2551 session"\\n'
2552 2552
2553 2553 We can then call the function foo()::
2554 2554
2555 2555 In [2]: foo()
2556 2556 foo() was defined in an editing session
2557 2557
2558 2558 Now we edit foo. IPython automatically loads the editor with the
2559 2559 (temporary) file where foo() was previously defined::
2560 2560
2561 2561 In [3]: ed foo
2562 2562 Editing... done. Executing edited code...
2563 2563
2564 2564 And if we call foo() again we get the modified version::
2565 2565
2566 2566 In [4]: foo()
2567 2567 foo() has now been changed!
2568 2568
2569 2569 Here is an example of how to edit a code snippet successive
2570 2570 times. First we call the editor::
2571 2571
2572 2572 In [5]: ed
2573 2573 Editing... done. Executing edited code...
2574 2574 hello
2575 2575 Out[5]: "print 'hello'\\n"
2576 2576
2577 2577 Now we call it again with the previous output (stored in _)::
2578 2578
2579 2579 In [6]: ed _
2580 2580 Editing... done. Executing edited code...
2581 2581 hello world
2582 2582 Out[6]: "print 'hello world'\\n"
2583 2583
2584 2584 Now we call it with the output #8 (stored in _8, also as Out[8])::
2585 2585
2586 2586 In [7]: ed _8
2587 2587 Editing... done. Executing edited code...
2588 2588 hello again
2589 2589 Out[7]: "print 'hello again'\\n"
2590 2590
2591 2591
2592 2592 Changing the default editor hook:
2593 2593
2594 2594 If you wish to write your own editor hook, you can put it in a
2595 2595 configuration file which you load at startup time. The default hook
2596 2596 is defined in the IPython.core.hooks module, and you can use that as a
2597 2597 starting example for further modifications. That file also has
2598 2598 general instructions on how to set a new hook for use once you've
2599 2599 defined it."""
2600 2600 opts,args = self.parse_options(parameter_s,'prxn:')
2601 2601
2602 2602 try:
2603 2603 filename, lineno, is_temp = self._find_edit_target(args, opts, last_call)
2604 2604 except MacroToEdit as e:
2605 2605 self._edit_macro(args, e.args[0])
2606 2606 return
2607 2607
2608 2608 # do actual editing here
2609 2609 print 'Editing...',
2610 2610 sys.stdout.flush()
2611 2611 try:
2612 2612 # Quote filenames that may have spaces in them
2613 2613 if ' ' in filename:
2614 2614 filename = "'%s'" % filename
2615 2615 self.shell.hooks.editor(filename,lineno)
2616 2616 except TryNext:
2617 2617 warn('Could not open editor')
2618 2618 return
2619 2619
2620 2620 # XXX TODO: should this be generalized for all string vars?
2621 2621 # For now, this is special-cased to blocks created by cpaste
2622 2622 if args.strip() == 'pasted_block':
2623 2623 self.shell.user_ns['pasted_block'] = file_read(filename)
2624 2624
2625 2625 if 'x' in opts: # -x prevents actual execution
2626 2626 print
2627 2627 else:
2628 2628 print 'done. Executing edited code...'
2629 2629 if 'r' in opts: # Untranslated IPython code
2630 2630 self.shell.run_cell(file_read(filename),
2631 2631 store_history=False)
2632 2632 else:
2633 self.shell.safe_execfile(filename,self.shell.user_ns,
2633 self.shell.safe_execfile(filename, self.shell.user_ns,
2634 2634 self.shell.user_ns)
2635 2635
2636 2636 if is_temp:
2637 2637 try:
2638 2638 return open(filename).read()
2639 2639 except IOError,msg:
2640 2640 if msg.filename == filename:
2641 2641 warn('File not found. Did you forget to save?')
2642 2642 return
2643 2643 else:
2644 2644 self.shell.showtraceback()
2645 2645
2646 2646 def magic_xmode(self,parameter_s = ''):
2647 2647 """Switch modes for the exception handlers.
2648 2648
2649 2649 Valid modes: Plain, Context and Verbose.
2650 2650
2651 2651 If called without arguments, acts as a toggle."""
2652 2652
2653 2653 def xmode_switch_err(name):
2654 2654 warn('Error changing %s exception modes.\n%s' %
2655 2655 (name,sys.exc_info()[1]))
2656 2656
2657 2657 shell = self.shell
2658 2658 new_mode = parameter_s.strip().capitalize()
2659 2659 try:
2660 2660 shell.InteractiveTB.set_mode(mode=new_mode)
2661 2661 print 'Exception reporting mode:',shell.InteractiveTB.mode
2662 2662 except:
2663 2663 xmode_switch_err('user')
2664 2664
2665 2665 def magic_colors(self,parameter_s = ''):
2666 2666 """Switch color scheme for prompts, info system and exception handlers.
2667 2667
2668 2668 Currently implemented schemes: NoColor, Linux, LightBG.
2669 2669
2670 2670 Color scheme names are not case-sensitive.
2671 2671
2672 2672 Examples
2673 2673 --------
2674 2674 To get a plain black and white terminal::
2675 2675
2676 2676 %colors nocolor
2677 2677 """
2678 2678
2679 2679 def color_switch_err(name):
2680 2680 warn('Error changing %s color schemes.\n%s' %
2681 2681 (name,sys.exc_info()[1]))
2682 2682
2683 2683
2684 2684 new_scheme = parameter_s.strip()
2685 2685 if not new_scheme:
2686 2686 raise UsageError(
2687 2687 "%colors: you must specify a color scheme. See '%colors?'")
2688 2688 return
2689 2689 # local shortcut
2690 2690 shell = self.shell
2691 2691
2692 2692 import IPython.utils.rlineimpl as readline
2693 2693
2694 2694 if not shell.colors_force and \
2695 2695 not readline.have_readline and sys.platform == "win32":
2696 2696 msg = """\
2697 2697 Proper color support under MS Windows requires the pyreadline library.
2698 2698 You can find it at:
2699 2699 http://ipython.org/pyreadline.html
2700 2700 Gary's readline needs the ctypes module, from:
2701 2701 http://starship.python.net/crew/theller/ctypes
2702 2702 (Note that ctypes is already part of Python versions 2.5 and newer).
2703 2703
2704 2704 Defaulting color scheme to 'NoColor'"""
2705 2705 new_scheme = 'NoColor'
2706 2706 warn(msg)
2707 2707
2708 2708 # readline option is 0
2709 2709 if not shell.colors_force and not shell.has_readline:
2710 2710 new_scheme = 'NoColor'
2711 2711
2712 2712 # Set prompt colors
2713 2713 try:
2714 2714 shell.prompt_manager.color_scheme = new_scheme
2715 2715 except:
2716 2716 color_switch_err('prompt')
2717 2717 else:
2718 2718 shell.colors = \
2719 2719 shell.prompt_manager.color_scheme_table.active_scheme_name
2720 2720 # Set exception colors
2721 2721 try:
2722 2722 shell.InteractiveTB.set_colors(scheme = new_scheme)
2723 2723 shell.SyntaxTB.set_colors(scheme = new_scheme)
2724 2724 except:
2725 2725 color_switch_err('exception')
2726 2726
2727 2727 # Set info (for 'object?') colors
2728 2728 if shell.color_info:
2729 2729 try:
2730 2730 shell.inspector.set_active_scheme(new_scheme)
2731 2731 except:
2732 2732 color_switch_err('object inspector')
2733 2733 else:
2734 2734 shell.inspector.set_active_scheme('NoColor')
2735 2735
2736 2736 def magic_pprint(self, parameter_s=''):
2737 2737 """Toggle pretty printing on/off."""
2738 2738 ptformatter = self.shell.display_formatter.formatters['text/plain']
2739 2739 ptformatter.pprint = bool(1 - ptformatter.pprint)
2740 2740 print 'Pretty printing has been turned', \
2741 2741 ['OFF','ON'][ptformatter.pprint]
2742 2742
2743 2743 #......................................................................
2744 2744 # Functions to implement unix shell-type things
2745 2745
2746 2746 @skip_doctest
2747 2747 def magic_alias(self, parameter_s = ''):
2748 2748 """Define an alias for a system command.
2749 2749
2750 2750 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2751 2751
2752 2752 Then, typing 'alias_name params' will execute the system command 'cmd
2753 2753 params' (from your underlying operating system).
2754 2754
2755 2755 Aliases have lower precedence than magic functions and Python normal
2756 2756 variables, so if 'foo' is both a Python variable and an alias, the
2757 2757 alias can not be executed until 'del foo' removes the Python variable.
2758 2758
2759 2759 You can use the %l specifier in an alias definition to represent the
2760 2760 whole line when the alias is called. For example::
2761 2761
2762 2762 In [2]: alias bracket echo "Input in brackets: <%l>"
2763 2763 In [3]: bracket hello world
2764 2764 Input in brackets: <hello world>
2765 2765
2766 2766 You can also define aliases with parameters using %s specifiers (one
2767 2767 per parameter)::
2768 2768
2769 2769 In [1]: alias parts echo first %s second %s
2770 2770 In [2]: %parts A B
2771 2771 first A second B
2772 2772 In [3]: %parts A
2773 2773 Incorrect number of arguments: 2 expected.
2774 2774 parts is an alias to: 'echo first %s second %s'
2775 2775
2776 2776 Note that %l and %s are mutually exclusive. You can only use one or
2777 2777 the other in your aliases.
2778 2778
2779 2779 Aliases expand Python variables just like system calls using ! or !!
2780 2780 do: all expressions prefixed with '$' get expanded. For details of
2781 2781 the semantic rules, see PEP-215:
2782 2782 http://www.python.org/peps/pep-0215.html. This is the library used by
2783 2783 IPython for variable expansion. If you want to access a true shell
2784 2784 variable, an extra $ is necessary to prevent its expansion by
2785 2785 IPython::
2786 2786
2787 2787 In [6]: alias show echo
2788 2788 In [7]: PATH='A Python string'
2789 2789 In [8]: show $PATH
2790 2790 A Python string
2791 2791 In [9]: show $$PATH
2792 2792 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2793 2793
2794 2794 You can use the alias facility to acess all of $PATH. See the %rehash
2795 2795 and %rehashx functions, which automatically create aliases for the
2796 2796 contents of your $PATH.
2797 2797
2798 2798 If called with no parameters, %alias prints the current alias table."""
2799 2799
2800 2800 par = parameter_s.strip()
2801 2801 if not par:
2802 2802 stored = self.shell.db.get('stored_aliases', {} )
2803 2803 aliases = sorted(self.shell.alias_manager.aliases)
2804 2804 # for k, v in stored:
2805 2805 # atab.append(k, v[0])
2806 2806
2807 2807 print "Total number of aliases:", len(aliases)
2808 2808 sys.stdout.flush()
2809 2809 return aliases
2810 2810
2811 2811 # Now try to define a new one
2812 2812 try:
2813 2813 alias,cmd = par.split(None, 1)
2814 2814 except:
2815 2815 print oinspect.getdoc(self.magic_alias)
2816 2816 else:
2817 2817 self.shell.alias_manager.soft_define_alias(alias, cmd)
2818 2818 # end magic_alias
2819 2819
2820 2820 def magic_unalias(self, parameter_s = ''):
2821 2821 """Remove an alias"""
2822 2822
2823 2823 aname = parameter_s.strip()
2824 2824 self.shell.alias_manager.undefine_alias(aname)
2825 2825 stored = self.shell.db.get('stored_aliases', {} )
2826 2826 if aname in stored:
2827 2827 print "Removing %stored alias",aname
2828 2828 del stored[aname]
2829 2829 self.shell.db['stored_aliases'] = stored
2830 2830
2831 2831 def magic_rehashx(self, parameter_s = ''):
2832 2832 """Update the alias table with all executable files in $PATH.
2833 2833
2834 2834 This version explicitly checks that every entry in $PATH is a file
2835 2835 with execute access (os.X_OK), so it is much slower than %rehash.
2836 2836
2837 2837 Under Windows, it checks executability as a match against a
2838 2838 '|'-separated string of extensions, stored in the IPython config
2839 2839 variable win_exec_ext. This defaults to 'exe|com|bat'.
2840 2840
2841 2841 This function also resets the root module cache of module completer,
2842 2842 used on slow filesystems.
2843 2843 """
2844 2844 from IPython.core.alias import InvalidAliasError
2845 2845
2846 2846 # for the benefit of module completer in ipy_completers.py
2847 2847 del self.shell.db['rootmodules']
2848 2848
2849 2849 path = [os.path.abspath(os.path.expanduser(p)) for p in
2850 2850 os.environ.get('PATH','').split(os.pathsep)]
2851 2851 path = filter(os.path.isdir,path)
2852 2852
2853 2853 syscmdlist = []
2854 2854 # Now define isexec in a cross platform manner.
2855 2855 if os.name == 'posix':
2856 2856 isexec = lambda fname:os.path.isfile(fname) and \
2857 2857 os.access(fname,os.X_OK)
2858 2858 else:
2859 2859 try:
2860 2860 winext = os.environ['pathext'].replace(';','|').replace('.','')
2861 2861 except KeyError:
2862 2862 winext = 'exe|com|bat|py'
2863 2863 if 'py' not in winext:
2864 2864 winext += '|py'
2865 2865 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2866 2866 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2867 2867 savedir = os.getcwdu()
2868 2868
2869 2869 # Now walk the paths looking for executables to alias.
2870 2870 try:
2871 2871 # write the whole loop for posix/Windows so we don't have an if in
2872 2872 # the innermost part
2873 2873 if os.name == 'posix':
2874 2874 for pdir in path:
2875 2875 os.chdir(pdir)
2876 2876 for ff in os.listdir(pdir):
2877 2877 if isexec(ff):
2878 2878 try:
2879 2879 # Removes dots from the name since ipython
2880 2880 # will assume names with dots to be python.
2881 2881 self.shell.alias_manager.define_alias(
2882 2882 ff.replace('.',''), ff)
2883 2883 except InvalidAliasError:
2884 2884 pass
2885 2885 else:
2886 2886 syscmdlist.append(ff)
2887 2887 else:
2888 2888 no_alias = self.shell.alias_manager.no_alias
2889 2889 for pdir in path:
2890 2890 os.chdir(pdir)
2891 2891 for ff in os.listdir(pdir):
2892 2892 base, ext = os.path.splitext(ff)
2893 2893 if isexec(ff) and base.lower() not in no_alias:
2894 2894 if ext.lower() == '.exe':
2895 2895 ff = base
2896 2896 try:
2897 2897 # Removes dots from the name since ipython
2898 2898 # will assume names with dots to be python.
2899 2899 self.shell.alias_manager.define_alias(
2900 2900 base.lower().replace('.',''), ff)
2901 2901 except InvalidAliasError:
2902 2902 pass
2903 2903 syscmdlist.append(ff)
2904 2904 self.shell.db['syscmdlist'] = syscmdlist
2905 2905 finally:
2906 2906 os.chdir(savedir)
2907 2907
2908 2908 @skip_doctest
2909 2909 def magic_pwd(self, parameter_s = ''):
2910 2910 """Return the current working directory path.
2911 2911
2912 2912 Examples
2913 2913 --------
2914 2914 ::
2915 2915
2916 2916 In [9]: pwd
2917 2917 Out[9]: '/home/tsuser/sprint/ipython'
2918 2918 """
2919 2919 return os.getcwdu()
2920 2920
2921 2921 @skip_doctest
2922 2922 def magic_cd(self, parameter_s=''):
2923 2923 """Change the current working directory.
2924 2924
2925 2925 This command automatically maintains an internal list of directories
2926 2926 you visit during your IPython session, in the variable _dh. The
2927 2927 command %dhist shows this history nicely formatted. You can also
2928 2928 do 'cd -<tab>' to see directory history conveniently.
2929 2929
2930 2930 Usage:
2931 2931
2932 2932 cd 'dir': changes to directory 'dir'.
2933 2933
2934 2934 cd -: changes to the last visited directory.
2935 2935
2936 2936 cd -<n>: changes to the n-th directory in the directory history.
2937 2937
2938 2938 cd --foo: change to directory that matches 'foo' in history
2939 2939
2940 2940 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2941 2941 (note: cd <bookmark_name> is enough if there is no
2942 2942 directory <bookmark_name>, but a bookmark with the name exists.)
2943 2943 'cd -b <tab>' allows you to tab-complete bookmark names.
2944 2944
2945 2945 Options:
2946 2946
2947 2947 -q: quiet. Do not print the working directory after the cd command is
2948 2948 executed. By default IPython's cd command does print this directory,
2949 2949 since the default prompts do not display path information.
2950 2950
2951 2951 Note that !cd doesn't work for this purpose because the shell where
2952 2952 !command runs is immediately discarded after executing 'command'.
2953 2953
2954 2954 Examples
2955 2955 --------
2956 2956 ::
2957 2957
2958 2958 In [10]: cd parent/child
2959 2959 /home/tsuser/parent/child
2960 2960 """
2961 2961
2962 2962 parameter_s = parameter_s.strip()
2963 2963 #bkms = self.shell.persist.get("bookmarks",{})
2964 2964
2965 2965 oldcwd = os.getcwdu()
2966 2966 numcd = re.match(r'(-)(\d+)$',parameter_s)
2967 2967 # jump in directory history by number
2968 2968 if numcd:
2969 2969 nn = int(numcd.group(2))
2970 2970 try:
2971 2971 ps = self.shell.user_ns['_dh'][nn]
2972 2972 except IndexError:
2973 2973 print 'The requested directory does not exist in history.'
2974 2974 return
2975 2975 else:
2976 2976 opts = {}
2977 2977 elif parameter_s.startswith('--'):
2978 2978 ps = None
2979 2979 fallback = None
2980 2980 pat = parameter_s[2:]
2981 2981 dh = self.shell.user_ns['_dh']
2982 2982 # first search only by basename (last component)
2983 2983 for ent in reversed(dh):
2984 2984 if pat in os.path.basename(ent) and os.path.isdir(ent):
2985 2985 ps = ent
2986 2986 break
2987 2987
2988 2988 if fallback is None and pat in ent and os.path.isdir(ent):
2989 2989 fallback = ent
2990 2990
2991 2991 # if we have no last part match, pick the first full path match
2992 2992 if ps is None:
2993 2993 ps = fallback
2994 2994
2995 2995 if ps is None:
2996 2996 print "No matching entry in directory history"
2997 2997 return
2998 2998 else:
2999 2999 opts = {}
3000 3000
3001 3001
3002 3002 else:
3003 3003 #turn all non-space-escaping backslashes to slashes,
3004 3004 # for c:\windows\directory\names\
3005 3005 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
3006 3006 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
3007 3007 # jump to previous
3008 3008 if ps == '-':
3009 3009 try:
3010 3010 ps = self.shell.user_ns['_dh'][-2]
3011 3011 except IndexError:
3012 3012 raise UsageError('%cd -: No previous directory to change to.')
3013 3013 # jump to bookmark if needed
3014 3014 else:
3015 3015 if not os.path.isdir(ps) or opts.has_key('b'):
3016 3016 bkms = self.shell.db.get('bookmarks', {})
3017 3017
3018 3018 if bkms.has_key(ps):
3019 3019 target = bkms[ps]
3020 3020 print '(bookmark:%s) -> %s' % (ps,target)
3021 3021 ps = target
3022 3022 else:
3023 3023 if opts.has_key('b'):
3024 3024 raise UsageError("Bookmark '%s' not found. "
3025 3025 "Use '%%bookmark -l' to see your bookmarks." % ps)
3026 3026
3027 3027 # strip extra quotes on Windows, because os.chdir doesn't like them
3028 3028 ps = unquote_filename(ps)
3029 3029 # at this point ps should point to the target dir
3030 3030 if ps:
3031 3031 try:
3032 3032 os.chdir(os.path.expanduser(ps))
3033 3033 if hasattr(self.shell, 'term_title') and self.shell.term_title:
3034 3034 set_term_title('IPython: ' + abbrev_cwd())
3035 3035 except OSError:
3036 3036 print sys.exc_info()[1]
3037 3037 else:
3038 3038 cwd = os.getcwdu()
3039 3039 dhist = self.shell.user_ns['_dh']
3040 3040 if oldcwd != cwd:
3041 3041 dhist.append(cwd)
3042 3042 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
3043 3043
3044 3044 else:
3045 3045 os.chdir(self.shell.home_dir)
3046 3046 if hasattr(self.shell, 'term_title') and self.shell.term_title:
3047 3047 set_term_title('IPython: ' + '~')
3048 3048 cwd = os.getcwdu()
3049 3049 dhist = self.shell.user_ns['_dh']
3050 3050
3051 3051 if oldcwd != cwd:
3052 3052 dhist.append(cwd)
3053 3053 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
3054 3054 if not 'q' in opts and self.shell.user_ns['_dh']:
3055 3055 print self.shell.user_ns['_dh'][-1]
3056 3056
3057 3057
3058 3058 def magic_env(self, parameter_s=''):
3059 3059 """List environment variables."""
3060 3060
3061 3061 return dict(os.environ)
3062 3062
3063 3063 def magic_pushd(self, parameter_s=''):
3064 3064 """Place the current dir on stack and change directory.
3065 3065
3066 3066 Usage:\\
3067 3067 %pushd ['dirname']
3068 3068 """
3069 3069
3070 3070 dir_s = self.shell.dir_stack
3071 3071 tgt = os.path.expanduser(unquote_filename(parameter_s))
3072 3072 cwd = os.getcwdu().replace(self.home_dir,'~')
3073 3073 if tgt:
3074 3074 self.magic_cd(parameter_s)
3075 3075 dir_s.insert(0,cwd)
3076 3076 return self.magic_dirs()
3077 3077
3078 3078 def magic_popd(self, parameter_s=''):
3079 3079 """Change to directory popped off the top of the stack.
3080 3080 """
3081 3081 if not self.shell.dir_stack:
3082 3082 raise UsageError("%popd on empty stack")
3083 3083 top = self.shell.dir_stack.pop(0)
3084 3084 self.magic_cd(top)
3085 3085 print "popd ->",top
3086 3086
3087 3087 def magic_dirs(self, parameter_s=''):
3088 3088 """Return the current directory stack."""
3089 3089
3090 3090 return self.shell.dir_stack
3091 3091
3092 3092 def magic_dhist(self, parameter_s=''):
3093 3093 """Print your history of visited directories.
3094 3094
3095 3095 %dhist -> print full history\\
3096 3096 %dhist n -> print last n entries only\\
3097 3097 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
3098 3098
3099 3099 This history is automatically maintained by the %cd command, and
3100 3100 always available as the global list variable _dh. You can use %cd -<n>
3101 3101 to go to directory number <n>.
3102 3102
3103 3103 Note that most of time, you should view directory history by entering
3104 3104 cd -<TAB>.
3105 3105
3106 3106 """
3107 3107
3108 3108 dh = self.shell.user_ns['_dh']
3109 3109 if parameter_s:
3110 3110 try:
3111 3111 args = map(int,parameter_s.split())
3112 3112 except:
3113 3113 self.arg_err(Magic.magic_dhist)
3114 3114 return
3115 3115 if len(args) == 1:
3116 3116 ini,fin = max(len(dh)-(args[0]),0),len(dh)
3117 3117 elif len(args) == 2:
3118 3118 ini,fin = args
3119 3119 else:
3120 3120 self.arg_err(Magic.magic_dhist)
3121 3121 return
3122 3122 else:
3123 3123 ini,fin = 0,len(dh)
3124 3124 nlprint(dh,
3125 3125 header = 'Directory history (kept in _dh)',
3126 3126 start=ini,stop=fin)
3127 3127
3128 3128 @skip_doctest
3129 3129 def magic_sc(self, parameter_s=''):
3130 3130 """Shell capture - execute a shell command and capture its output.
3131 3131
3132 3132 DEPRECATED. Suboptimal, retained for backwards compatibility.
3133 3133
3134 3134 You should use the form 'var = !command' instead. Example:
3135 3135
3136 3136 "%sc -l myfiles = ls ~" should now be written as
3137 3137
3138 3138 "myfiles = !ls ~"
3139 3139
3140 3140 myfiles.s, myfiles.l and myfiles.n still apply as documented
3141 3141 below.
3142 3142
3143 3143 --
3144 3144 %sc [options] varname=command
3145 3145
3146 3146 IPython will run the given command using commands.getoutput(), and
3147 3147 will then update the user's interactive namespace with a variable
3148 3148 called varname, containing the value of the call. Your command can
3149 3149 contain shell wildcards, pipes, etc.
3150 3150
3151 3151 The '=' sign in the syntax is mandatory, and the variable name you
3152 3152 supply must follow Python's standard conventions for valid names.
3153 3153
3154 3154 (A special format without variable name exists for internal use)
3155 3155
3156 3156 Options:
3157 3157
3158 3158 -l: list output. Split the output on newlines into a list before
3159 3159 assigning it to the given variable. By default the output is stored
3160 3160 as a single string.
3161 3161
3162 3162 -v: verbose. Print the contents of the variable.
3163 3163
3164 3164 In most cases you should not need to split as a list, because the
3165 3165 returned value is a special type of string which can automatically
3166 3166 provide its contents either as a list (split on newlines) or as a
3167 3167 space-separated string. These are convenient, respectively, either
3168 3168 for sequential processing or to be passed to a shell command.
3169 3169
3170 3170 For example::
3171 3171
3172 3172 # Capture into variable a
3173 3173 In [1]: sc a=ls *py
3174 3174
3175 3175 # a is a string with embedded newlines
3176 3176 In [2]: a
3177 3177 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3178 3178
3179 3179 # which can be seen as a list:
3180 3180 In [3]: a.l
3181 3181 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3182 3182
3183 3183 # or as a whitespace-separated string:
3184 3184 In [4]: a.s
3185 3185 Out[4]: 'setup.py win32_manual_post_install.py'
3186 3186
3187 3187 # a.s is useful to pass as a single command line:
3188 3188 In [5]: !wc -l $a.s
3189 3189 146 setup.py
3190 3190 130 win32_manual_post_install.py
3191 3191 276 total
3192 3192
3193 3193 # while the list form is useful to loop over:
3194 3194 In [6]: for f in a.l:
3195 3195 ...: !wc -l $f
3196 3196 ...:
3197 3197 146 setup.py
3198 3198 130 win32_manual_post_install.py
3199 3199
3200 3200 Similarly, the lists returned by the -l option are also special, in
3201 3201 the sense that you can equally invoke the .s attribute on them to
3202 3202 automatically get a whitespace-separated string from their contents::
3203 3203
3204 3204 In [7]: sc -l b=ls *py
3205 3205
3206 3206 In [8]: b
3207 3207 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3208 3208
3209 3209 In [9]: b.s
3210 3210 Out[9]: 'setup.py win32_manual_post_install.py'
3211 3211
3212 3212 In summary, both the lists and strings used for output capture have
3213 3213 the following special attributes::
3214 3214
3215 3215 .l (or .list) : value as list.
3216 3216 .n (or .nlstr): value as newline-separated string.
3217 3217 .s (or .spstr): value as space-separated string.
3218 3218 """
3219 3219
3220 3220 opts,args = self.parse_options(parameter_s,'lv')
3221 3221 # Try to get a variable name and command to run
3222 3222 try:
3223 3223 # the variable name must be obtained from the parse_options
3224 3224 # output, which uses shlex.split to strip options out.
3225 3225 var,_ = args.split('=',1)
3226 3226 var = var.strip()
3227 3227 # But the command has to be extracted from the original input
3228 3228 # parameter_s, not on what parse_options returns, to avoid the
3229 3229 # quote stripping which shlex.split performs on it.
3230 3230 _,cmd = parameter_s.split('=',1)
3231 3231 except ValueError:
3232 3232 var,cmd = '',''
3233 3233 # If all looks ok, proceed
3234 3234 split = 'l' in opts
3235 3235 out = self.shell.getoutput(cmd, split=split)
3236 3236 if opts.has_key('v'):
3237 3237 print '%s ==\n%s' % (var,pformat(out))
3238 3238 if var:
3239 3239 self.shell.user_ns.update({var:out})
3240 3240 else:
3241 3241 return out
3242 3242
3243 3243 def magic_sx(self, parameter_s=''):
3244 3244 """Shell execute - run a shell command and capture its output.
3245 3245
3246 3246 %sx command
3247 3247
3248 3248 IPython will run the given command using commands.getoutput(), and
3249 3249 return the result formatted as a list (split on '\\n'). Since the
3250 3250 output is _returned_, it will be stored in ipython's regular output
3251 3251 cache Out[N] and in the '_N' automatic variables.
3252 3252
3253 3253 Notes:
3254 3254
3255 3255 1) If an input line begins with '!!', then %sx is automatically
3256 3256 invoked. That is, while::
3257 3257
3258 3258 !ls
3259 3259
3260 3260 causes ipython to simply issue system('ls'), typing::
3261 3261
3262 3262 !!ls
3263 3263
3264 3264 is a shorthand equivalent to::
3265 3265
3266 3266 %sx ls
3267 3267
3268 3268 2) %sx differs from %sc in that %sx automatically splits into a list,
3269 3269 like '%sc -l'. The reason for this is to make it as easy as possible
3270 3270 to process line-oriented shell output via further python commands.
3271 3271 %sc is meant to provide much finer control, but requires more
3272 3272 typing.
3273 3273
3274 3274 3) Just like %sc -l, this is a list with special attributes:
3275 3275 ::
3276 3276
3277 3277 .l (or .list) : value as list.
3278 3278 .n (or .nlstr): value as newline-separated string.
3279 3279 .s (or .spstr): value as whitespace-separated string.
3280 3280
3281 3281 This is very useful when trying to use such lists as arguments to
3282 3282 system commands."""
3283 3283
3284 3284 if parameter_s:
3285 3285 return self.shell.getoutput(parameter_s)
3286 3286
3287 3287
3288 3288 def magic_bookmark(self, parameter_s=''):
3289 3289 """Manage IPython's bookmark system.
3290 3290
3291 3291 %bookmark <name> - set bookmark to current dir
3292 3292 %bookmark <name> <dir> - set bookmark to <dir>
3293 3293 %bookmark -l - list all bookmarks
3294 3294 %bookmark -d <name> - remove bookmark
3295 3295 %bookmark -r - remove all bookmarks
3296 3296
3297 3297 You can later on access a bookmarked folder with::
3298 3298
3299 3299 %cd -b <name>
3300 3300
3301 3301 or simply '%cd <name>' if there is no directory called <name> AND
3302 3302 there is such a bookmark defined.
3303 3303
3304 3304 Your bookmarks persist through IPython sessions, but they are
3305 3305 associated with each profile."""
3306 3306
3307 3307 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3308 3308 if len(args) > 2:
3309 3309 raise UsageError("%bookmark: too many arguments")
3310 3310
3311 3311 bkms = self.shell.db.get('bookmarks',{})
3312 3312
3313 3313 if opts.has_key('d'):
3314 3314 try:
3315 3315 todel = args[0]
3316 3316 except IndexError:
3317 3317 raise UsageError(
3318 3318 "%bookmark -d: must provide a bookmark to delete")
3319 3319 else:
3320 3320 try:
3321 3321 del bkms[todel]
3322 3322 except KeyError:
3323 3323 raise UsageError(
3324 3324 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3325 3325
3326 3326 elif opts.has_key('r'):
3327 3327 bkms = {}
3328 3328 elif opts.has_key('l'):
3329 3329 bks = bkms.keys()
3330 3330 bks.sort()
3331 3331 if bks:
3332 3332 size = max(map(len,bks))
3333 3333 else:
3334 3334 size = 0
3335 3335 fmt = '%-'+str(size)+'s -> %s'
3336 3336 print 'Current bookmarks:'
3337 3337 for bk in bks:
3338 3338 print fmt % (bk,bkms[bk])
3339 3339 else:
3340 3340 if not args:
3341 3341 raise UsageError("%bookmark: You must specify the bookmark name")
3342 3342 elif len(args)==1:
3343 3343 bkms[args[0]] = os.getcwdu()
3344 3344 elif len(args)==2:
3345 3345 bkms[args[0]] = args[1]
3346 3346 self.shell.db['bookmarks'] = bkms
3347 3347
3348 3348
3349 3349 def magic_pycat(self, parameter_s=''):
3350 3350 """Show a syntax-highlighted file through a pager.
3351 3351
3352 3352 This magic is similar to the cat utility, but it will assume the file
3353 3353 to be Python source and will show it with syntax highlighting.
3354 3354
3355 3355 This magic command can either take a local filename, an url,
3356 3356 an history range (see %history) or a macro as argument ::
3357 3357
3358 3358 %pycat myscript.py
3359 3359 %pycat 7-27
3360 3360 %pycat myMacro
3361 3361 %pycat http://www.example.com/myscript.py
3362 3362 """
3363 3363
3364 3364 try :
3365 3365 cont = self.shell.find_user_code(parameter_s)
3366 3366 except ValueError, IOError:
3367 3367 print "Error: no such file, variable, URL, history range or macro"
3368 3368 return
3369 3369
3370 3370 page.page(self.shell.pycolorize(cont))
3371 3371
3372 3372 def magic_quickref(self,arg):
3373 3373 """ Show a quick reference sheet """
3374 3374 import IPython.core.usage
3375 3375 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3376 3376
3377 3377 page.page(qr)
3378 3378
3379 3379 def magic_doctest_mode(self,parameter_s=''):
3380 3380 """Toggle doctest mode on and off.
3381 3381
3382 3382 This mode is intended to make IPython behave as much as possible like a
3383 3383 plain Python shell, from the perspective of how its prompts, exceptions
3384 3384 and output look. This makes it easy to copy and paste parts of a
3385 3385 session into doctests. It does so by:
3386 3386
3387 3387 - Changing the prompts to the classic ``>>>`` ones.
3388 3388 - Changing the exception reporting mode to 'Plain'.
3389 3389 - Disabling pretty-printing of output.
3390 3390
3391 3391 Note that IPython also supports the pasting of code snippets that have
3392 3392 leading '>>>' and '...' prompts in them. This means that you can paste
3393 3393 doctests from files or docstrings (even if they have leading
3394 3394 whitespace), and the code will execute correctly. You can then use
3395 3395 '%history -t' to see the translated history; this will give you the
3396 3396 input after removal of all the leading prompts and whitespace, which
3397 3397 can be pasted back into an editor.
3398 3398
3399 3399 With these features, you can switch into this mode easily whenever you
3400 3400 need to do testing and changes to doctests, without having to leave
3401 3401 your existing IPython session.
3402 3402 """
3403 3403
3404 3404 from IPython.utils.ipstruct import Struct
3405 3405
3406 3406 # Shorthands
3407 3407 shell = self.shell
3408 3408 pm = shell.prompt_manager
3409 3409 meta = shell.meta
3410 3410 disp_formatter = self.shell.display_formatter
3411 3411 ptformatter = disp_formatter.formatters['text/plain']
3412 3412 # dstore is a data store kept in the instance metadata bag to track any
3413 3413 # changes we make, so we can undo them later.
3414 3414 dstore = meta.setdefault('doctest_mode',Struct())
3415 3415 save_dstore = dstore.setdefault
3416 3416
3417 3417 # save a few values we'll need to recover later
3418 3418 mode = save_dstore('mode',False)
3419 3419 save_dstore('rc_pprint',ptformatter.pprint)
3420 3420 save_dstore('xmode',shell.InteractiveTB.mode)
3421 3421 save_dstore('rc_separate_out',shell.separate_out)
3422 3422 save_dstore('rc_separate_out2',shell.separate_out2)
3423 3423 save_dstore('rc_prompts_pad_left',pm.justify)
3424 3424 save_dstore('rc_separate_in',shell.separate_in)
3425 3425 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
3426 3426 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
3427 3427
3428 3428 if mode == False:
3429 3429 # turn on
3430 3430 pm.in_template = '>>> '
3431 3431 pm.in2_template = '... '
3432 3432 pm.out_template = ''
3433 3433
3434 3434 # Prompt separators like plain python
3435 3435 shell.separate_in = ''
3436 3436 shell.separate_out = ''
3437 3437 shell.separate_out2 = ''
3438 3438
3439 3439 pm.justify = False
3440 3440
3441 3441 ptformatter.pprint = False
3442 3442 disp_formatter.plain_text_only = True
3443 3443
3444 3444 shell.magic_xmode('Plain')
3445 3445 else:
3446 3446 # turn off
3447 3447 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
3448 3448
3449 3449 shell.separate_in = dstore.rc_separate_in
3450 3450
3451 3451 shell.separate_out = dstore.rc_separate_out
3452 3452 shell.separate_out2 = dstore.rc_separate_out2
3453 3453
3454 3454 pm.justify = dstore.rc_prompts_pad_left
3455 3455
3456 3456 ptformatter.pprint = dstore.rc_pprint
3457 3457 disp_formatter.plain_text_only = dstore.rc_plain_text_only
3458 3458
3459 3459 shell.magic_xmode(dstore.xmode)
3460 3460
3461 3461 # Store new mode and inform
3462 3462 dstore.mode = bool(1-int(mode))
3463 3463 mode_label = ['OFF','ON'][dstore.mode]
3464 3464 print 'Doctest mode is:', mode_label
3465 3465
3466 3466 def magic_gui(self, parameter_s=''):
3467 3467 """Enable or disable IPython GUI event loop integration.
3468 3468
3469 3469 %gui [GUINAME]
3470 3470
3471 3471 This magic replaces IPython's threaded shells that were activated
3472 3472 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3473 3473 can now be enabled at runtime and keyboard
3474 3474 interrupts should work without any problems. The following toolkits
3475 3475 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
3476 3476
3477 3477 %gui wx # enable wxPython event loop integration
3478 3478 %gui qt4|qt # enable PyQt4 event loop integration
3479 3479 %gui gtk # enable PyGTK event loop integration
3480 3480 %gui gtk3 # enable Gtk3 event loop integration
3481 3481 %gui tk # enable Tk event loop integration
3482 3482 %gui OSX # enable Cocoa event loop integration
3483 3483 # (requires %matplotlib 1.1)
3484 3484 %gui # disable all event loop integration
3485 3485
3486 3486 WARNING: after any of these has been called you can simply create
3487 3487 an application object, but DO NOT start the event loop yourself, as
3488 3488 we have already handled that.
3489 3489 """
3490 3490 opts, arg = self.parse_options(parameter_s, '')
3491 3491 if arg=='': arg = None
3492 3492 try:
3493 3493 return self.enable_gui(arg)
3494 3494 except Exception as e:
3495 3495 # print simple error message, rather than traceback if we can't
3496 3496 # hook up the GUI
3497 3497 error(str(e))
3498 3498
3499 3499 def magic_install_ext(self, parameter_s):
3500 3500 """Download and install an extension from a URL, e.g.::
3501 3501
3502 3502 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
3503 3503
3504 3504 The URL should point to an importable Python module - either a .py file
3505 3505 or a .zip file.
3506 3506
3507 3507 Parameters:
3508 3508
3509 3509 -n filename : Specify a name for the file, rather than taking it from
3510 3510 the URL.
3511 3511 """
3512 3512 opts, args = self.parse_options(parameter_s, 'n:')
3513 3513 try:
3514 3514 filename = self.extension_manager.install_extension(args, opts.get('n'))
3515 3515 except ValueError as e:
3516 3516 print e
3517 3517 return
3518 3518
3519 3519 filename = os.path.basename(filename)
3520 3520 print "Installed %s. To use it, type:" % filename
3521 3521 print " %%load_ext %s" % os.path.splitext(filename)[0]
3522 3522
3523 3523
3524 3524 def magic_load_ext(self, module_str):
3525 3525 """Load an IPython extension by its module name."""
3526 3526 return self.extension_manager.load_extension(module_str)
3527 3527
3528 3528 def magic_unload_ext(self, module_str):
3529 3529 """Unload an IPython extension by its module name."""
3530 3530 self.extension_manager.unload_extension(module_str)
3531 3531
3532 3532 def magic_reload_ext(self, module_str):
3533 3533 """Reload an IPython extension by its module name."""
3534 3534 self.extension_manager.reload_extension(module_str)
3535 3535
3536 3536 def magic_install_profiles(self, s):
3537 3537 """%install_profiles has been deprecated."""
3538 3538 print '\n'.join([
3539 3539 "%install_profiles has been deprecated.",
3540 3540 "Use `ipython profile list` to view available profiles.",
3541 3541 "Requesting a profile with `ipython profile create <name>`",
3542 3542 "or `ipython --profile=<name>` will start with the bundled",
3543 3543 "profile of that name if it exists."
3544 3544 ])
3545 3545
3546 3546 def magic_install_default_config(self, s):
3547 3547 """%install_default_config has been deprecated."""
3548 3548 print '\n'.join([
3549 3549 "%install_default_config has been deprecated.",
3550 3550 "Use `ipython profile create <name>` to initialize a profile",
3551 3551 "with the default config files.",
3552 3552 "Add `--reset` to overwrite already existing config files with defaults."
3553 3553 ])
3554 3554
3555 3555 @skip_doctest
3556 3556 def magic_pylab(self, s):
3557 3557 """Load numpy and matplotlib to work interactively.
3558 3558
3559 3559 %pylab [GUINAME]
3560 3560
3561 3561 This function lets you activate pylab (matplotlib, numpy and
3562 3562 interactive support) at any point during an IPython session.
3563 3563
3564 3564 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3565 3565 pylab and mlab, as well as all names from numpy and pylab.
3566 3566
3567 3567 If you are using the inline matplotlib backend for embedded figures,
3568 3568 you can adjust its behavior via the %config magic::
3569 3569
3570 3570 # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
3571 3571 In [1]: %config InlineBackend.figure_format = 'svg'
3572 3572
3573 3573 # change the behavior of closing all figures at the end of each
3574 3574 # execution (cell), or allowing reuse of active figures across
3575 3575 # cells:
3576 3576 In [2]: %config InlineBackend.close_figures = False
3577 3577
3578 3578 Parameters
3579 3579 ----------
3580 3580 guiname : optional
3581 3581 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
3582 3582 'osx' or 'tk'). If given, the corresponding Matplotlib backend is
3583 3583 used, otherwise matplotlib's default (which you can override in your
3584 3584 matplotlib config file) is used.
3585 3585
3586 3586 Examples
3587 3587 --------
3588 3588 In this case, where the MPL default is TkAgg::
3589 3589
3590 3590 In [2]: %pylab
3591 3591
3592 3592 Welcome to pylab, a matplotlib-based Python environment.
3593 3593 Backend in use: TkAgg
3594 3594 For more information, type 'help(pylab)'.
3595 3595
3596 3596 But you can explicitly request a different backend::
3597 3597
3598 3598 In [3]: %pylab qt
3599 3599
3600 3600 Welcome to pylab, a matplotlib-based Python environment.
3601 3601 Backend in use: Qt4Agg
3602 3602 For more information, type 'help(pylab)'.
3603 3603 """
3604 3604
3605 3605 if Application.initialized():
3606 3606 app = Application.instance()
3607 3607 try:
3608 3608 import_all_status = app.pylab_import_all
3609 3609 except AttributeError:
3610 3610 import_all_status = True
3611 3611 else:
3612 3612 import_all_status = True
3613 3613
3614 3614 self.shell.enable_pylab(s, import_all=import_all_status)
3615 3615
3616 3616 def magic_tb(self, s):
3617 3617 """Print the last traceback with the currently active exception mode.
3618 3618
3619 3619 See %xmode for changing exception reporting modes."""
3620 3620 self.shell.showtraceback()
3621 3621
3622 3622 @skip_doctest
3623 3623 def magic_precision(self, s=''):
3624 3624 """Set floating point precision for pretty printing.
3625 3625
3626 3626 Can set either integer precision or a format string.
3627 3627
3628 3628 If numpy has been imported and precision is an int,
3629 3629 numpy display precision will also be set, via ``numpy.set_printoptions``.
3630 3630
3631 3631 If no argument is given, defaults will be restored.
3632 3632
3633 3633 Examples
3634 3634 --------
3635 3635 ::
3636 3636
3637 3637 In [1]: from math import pi
3638 3638
3639 3639 In [2]: %precision 3
3640 3640 Out[2]: u'%.3f'
3641 3641
3642 3642 In [3]: pi
3643 3643 Out[3]: 3.142
3644 3644
3645 3645 In [4]: %precision %i
3646 3646 Out[4]: u'%i'
3647 3647
3648 3648 In [5]: pi
3649 3649 Out[5]: 3
3650 3650
3651 3651 In [6]: %precision %e
3652 3652 Out[6]: u'%e'
3653 3653
3654 3654 In [7]: pi**10
3655 3655 Out[7]: 9.364805e+04
3656 3656
3657 3657 In [8]: %precision
3658 3658 Out[8]: u'%r'
3659 3659
3660 3660 In [9]: pi**10
3661 3661 Out[9]: 93648.047476082982
3662 3662
3663 3663 """
3664 3664
3665 3665 ptformatter = self.shell.display_formatter.formatters['text/plain']
3666 3666 ptformatter.float_precision = s
3667 3667 return ptformatter.float_format
3668 3668
3669 3669
3670 3670 @magic_arguments.magic_arguments()
3671 3671 @magic_arguments.argument(
3672 3672 '-e', '--export', action='store_true', default=False,
3673 3673 help='Export IPython history as a notebook. The filename argument '
3674 3674 'is used to specify the notebook name and format. For example '
3675 3675 'a filename of notebook.ipynb will result in a notebook name '
3676 3676 'of "notebook" and a format of "xml". Likewise using a ".json" '
3677 3677 'or ".py" file extension will write the notebook in the json '
3678 3678 'or py formats.'
3679 3679 )
3680 3680 @magic_arguments.argument(
3681 3681 '-f', '--format',
3682 3682 help='Convert an existing IPython notebook to a new format. This option '
3683 3683 'specifies the new format and can have the values: xml, json, py. '
3684 3684 'The target filename is chosen automatically based on the new '
3685 3685 'format. The filename argument gives the name of the source file.'
3686 3686 )
3687 3687 @magic_arguments.argument(
3688 3688 'filename', type=unicode,
3689 3689 help='Notebook name or filename'
3690 3690 )
3691 3691 def magic_notebook(self, s):
3692 3692 """Export and convert IPython notebooks.
3693 3693
3694 3694 This function can export the current IPython history to a notebook file
3695 3695 or can convert an existing notebook file into a different format. For
3696 3696 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
3697 3697 To export the history to "foo.py" do "%notebook -e foo.py". To convert
3698 3698 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
3699 3699 formats include (json/ipynb, py).
3700 3700 """
3701 3701 args = magic_arguments.parse_argstring(self.magic_notebook, s)
3702 3702
3703 3703 from IPython.nbformat import current
3704 3704 args.filename = unquote_filename(args.filename)
3705 3705 if args.export:
3706 3706 fname, name, format = current.parse_filename(args.filename)
3707 3707 cells = []
3708 3708 hist = list(self.history_manager.get_range())
3709 3709 for session, prompt_number, input in hist[:-1]:
3710 3710 cells.append(current.new_code_cell(prompt_number=prompt_number, input=input))
3711 3711 worksheet = current.new_worksheet(cells=cells)
3712 3712 nb = current.new_notebook(name=name,worksheets=[worksheet])
3713 3713 with io.open(fname, 'w', encoding='utf-8') as f:
3714 3714 current.write(nb, f, format);
3715 3715 elif args.format is not None:
3716 3716 old_fname, old_name, old_format = current.parse_filename(args.filename)
3717 3717 new_format = args.format
3718 3718 if new_format == u'xml':
3719 3719 raise ValueError('Notebooks cannot be written as xml.')
3720 3720 elif new_format == u'ipynb' or new_format == u'json':
3721 3721 new_fname = old_name + u'.ipynb'
3722 3722 new_format = u'json'
3723 3723 elif new_format == u'py':
3724 3724 new_fname = old_name + u'.py'
3725 3725 else:
3726 3726 raise ValueError('Invalid notebook format: %s' % new_format)
3727 3727 with io.open(old_fname, 'r', encoding='utf-8') as f:
3728 3728 nb = current.read(f, old_format)
3729 3729 with io.open(new_fname, 'w', encoding='utf-8') as f:
3730 3730 current.write(nb, f, new_format)
3731 3731
3732 3732 def magic_config(self, s):
3733 3733 """configure IPython
3734 3734
3735 3735 %config Class[.trait=value]
3736 3736
3737 3737 This magic exposes most of the IPython config system. Any
3738 3738 Configurable class should be able to be configured with the simple
3739 3739 line::
3740 3740
3741 3741 %config Class.trait=value
3742 3742
3743 3743 Where `value` will be resolved in the user's namespace, if it is an
3744 3744 expression or variable name.
3745 3745
3746 3746 Examples
3747 3747 --------
3748 3748
3749 3749 To see what classes are available for config, pass no arguments::
3750 3750
3751 3751 In [1]: %config
3752 3752 Available objects for config:
3753 3753 TerminalInteractiveShell
3754 3754 HistoryManager
3755 3755 PrefilterManager
3756 3756 AliasManager
3757 3757 IPCompleter
3758 3758 PromptManager
3759 3759 DisplayFormatter
3760 3760
3761 3761 To view what is configurable on a given class, just pass the class
3762 3762 name::
3763 3763
3764 3764 In [2]: %config IPCompleter
3765 3765 IPCompleter options
3766 3766 -----------------
3767 3767 IPCompleter.omit__names=<Enum>
3768 3768 Current: 2
3769 3769 Choices: (0, 1, 2)
3770 3770 Instruct the completer to omit private method names
3771 3771 Specifically, when completing on ``object.<tab>``.
3772 3772 When 2 [default]: all names that start with '_' will be excluded.
3773 3773 When 1: all 'magic' names (``__foo__``) will be excluded.
3774 3774 When 0: nothing will be excluded.
3775 3775 IPCompleter.merge_completions=<CBool>
3776 3776 Current: True
3777 3777 Whether to merge completion results into a single list
3778 3778 If False, only the completion results from the first non-empty completer
3779 3779 will be returned.
3780 3780 IPCompleter.limit_to__all__=<CBool>
3781 3781 Current: False
3782 3782 Instruct the completer to use __all__ for the completion
3783 3783 Specifically, when completing on ``object.<tab>``.
3784 3784 When True: only those names in obj.__all__ will be included.
3785 3785 When False [default]: the __all__ attribute is ignored
3786 3786 IPCompleter.greedy=<CBool>
3787 3787 Current: False
3788 3788 Activate greedy completion
3789 3789 This will enable completion on elements of lists, results of function calls,
3790 3790 etc., but can be unsafe because the code is actually evaluated on TAB.
3791 3791
3792 3792 but the real use is in setting values::
3793 3793
3794 3794 In [3]: %config IPCompleter.greedy = True
3795 3795
3796 3796 and these values are read from the user_ns if they are variables::
3797 3797
3798 3798 In [4]: feeling_greedy=False
3799 3799
3800 3800 In [5]: %config IPCompleter.greedy = feeling_greedy
3801 3801
3802 3802 """
3803 3803 from IPython.config.loader import Config
3804 3804 # some IPython objects are Configurable, but do not yet have
3805 3805 # any configurable traits. Exclude them from the effects of
3806 3806 # this magic, as their presence is just noise:
3807 configurables = [ c for c in self.configurables if c.__class__.class_traits(config=True) ]
3807 configurables = [ c for c in self.shell.configurables
3808 if c.__class__.class_traits(config=True) ]
3808 3809 classnames = [ c.__class__.__name__ for c in configurables ]
3809 3810
3810 3811 line = s.strip()
3811 3812 if not line:
3812 3813 # print available configurable names
3813 3814 print "Available objects for config:"
3814 3815 for name in classnames:
3815 3816 print " ", name
3816 3817 return
3817 3818 elif line in classnames:
3818 3819 # `%config TerminalInteractiveShell` will print trait info for
3819 3820 # TerminalInteractiveShell
3820 3821 c = configurables[classnames.index(line)]
3821 3822 cls = c.__class__
3822 3823 help = cls.class_get_help(c)
3823 3824 # strip leading '--' from cl-args:
3824 3825 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
3825 3826 print help
3826 3827 return
3827 3828 elif '=' not in line:
3828 3829 raise UsageError("Invalid config statement: %r, should be Class.trait = value" % line)
3829 3830
3830 3831
3831 3832 # otherwise, assume we are setting configurables.
3832 3833 # leave quotes on args when splitting, because we want
3833 3834 # unquoted args to eval in user_ns
3834 3835 cfg = Config()
3835 exec "cfg."+line in locals(), self.user_ns
3836 exec "cfg."+line in locals(), self.shell.user_ns
3836 3837
3837 3838 for configurable in configurables:
3838 3839 try:
3839 3840 configurable.update_config(cfg)
3840 3841 except Exception as e:
3841 3842 error(e)
3842 3843
3843 3844 # end Magic
@@ -1,957 +1,957 b''
1 1 # encoding: utf-8
2 2 """
3 3 Prefiltering components.
4 4
5 5 Prefilters transform user input before it is exec'd by Python. These
6 6 transforms are used to implement additional syntax such as !ls and %magic.
7 7
8 8 Authors:
9 9
10 10 * Brian Granger
11 11 * Fernando Perez
12 12 * Dan Milstein
13 13 * Ville Vainio
14 14 """
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Copyright (C) 2008-2011 The IPython Development Team
18 18 #
19 19 # Distributed under the terms of the BSD License. The full license is in
20 20 # the file COPYING, distributed as part of this software.
21 21 #-----------------------------------------------------------------------------
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Imports
25 25 #-----------------------------------------------------------------------------
26 26
27 27 import __builtin__
28 28 import codeop
29 29 import re
30 30
31 31 from IPython.core.alias import AliasManager
32 32 from IPython.core.autocall import IPyAutocall
33 33 from IPython.config.configurable import Configurable
34 34 from IPython.core.macro import Macro
35 35 from IPython.core.splitinput import split_user_input, LineInfo
36 36 from IPython.core import page
37 37
38 38 from IPython.utils.traitlets import (
39 39 List, Integer, Any, Unicode, CBool, Bool, Instance, CRegExp
40 40 )
41 41 from IPython.utils.autoattr import auto_attr
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Global utilities, errors and constants
45 45 #-----------------------------------------------------------------------------
46 46
47 47 # Warning, these cannot be changed unless various regular expressions
48 48 # are updated in a number of places. Not great, but at least we told you.
49 49 ESC_SHELL = '!'
50 50 ESC_SH_CAP = '!!'
51 51 ESC_HELP = '?'
52 52 ESC_MAGIC = '%'
53 53 ESC_QUOTE = ','
54 54 ESC_QUOTE2 = ';'
55 55 ESC_PAREN = '/'
56 56
57 57
58 58 class PrefilterError(Exception):
59 59 pass
60 60
61 61
62 62 # RegExp to identify potential function names
63 63 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
64 64
65 65 # RegExp to exclude strings with this start from autocalling. In
66 66 # particular, all binary operators should be excluded, so that if foo is
67 67 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
68 68 # characters '!=()' don't need to be checked for, as the checkPythonChars
69 69 # routine explicitely does so, to catch direct calls and rebindings of
70 70 # existing names.
71 71
72 72 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
73 73 # it affects the rest of the group in square brackets.
74 74 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
75 75 r'|^is |^not |^in |^and |^or ')
76 76
77 77 # try to catch also methods for stuff in lists/tuples/dicts: off
78 78 # (experimental). For this to work, the line_split regexp would need
79 79 # to be modified so it wouldn't break things at '['. That line is
80 80 # nasty enough that I shouldn't change it until I can test it _well_.
81 81 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
82 82
83 83
84 84 # Handler Check Utilities
85 85 def is_shadowed(identifier, ip):
86 86 """Is the given identifier defined in one of the namespaces which shadow
87 87 the alias and magic namespaces? Note that an identifier is different
88 88 than ifun, because it can not contain a '.' character."""
89 89 # This is much safer than calling ofind, which can change state
90 90 return (identifier in ip.user_ns \
91 91 or identifier in ip.user_global_ns \
92 92 or identifier in ip.ns_table['builtin'])
93 93
94 94
95 95 #-----------------------------------------------------------------------------
96 96 # Main Prefilter manager
97 97 #-----------------------------------------------------------------------------
98 98
99 99
100 100 class PrefilterManager(Configurable):
101 101 """Main prefilter component.
102 102
103 103 The IPython prefilter is run on all user input before it is run. The
104 104 prefilter consumes lines of input and produces transformed lines of
105 105 input.
106 106
107 107 The iplementation consists of two phases:
108 108
109 109 1. Transformers
110 110 2. Checkers and handlers
111 111
112 112 Over time, we plan on deprecating the checkers and handlers and doing
113 113 everything in the transformers.
114 114
115 115 The transformers are instances of :class:`PrefilterTransformer` and have
116 116 a single method :meth:`transform` that takes a line and returns a
117 117 transformed line. The transformation can be accomplished using any
118 118 tool, but our current ones use regular expressions for speed. We also
119 119 ship :mod:`pyparsing` in :mod:`IPython.external` for use in transformers.
120 120
121 121 After all the transformers have been run, the line is fed to the checkers,
122 122 which are instances of :class:`PrefilterChecker`. The line is passed to
123 123 the :meth:`check` method, which either returns `None` or a
124 124 :class:`PrefilterHandler` instance. If `None` is returned, the other
125 125 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
126 126 the line is passed to the :meth:`handle` method of the returned
127 127 handler and no further checkers are tried.
128 128
129 129 Both transformers and checkers have a `priority` attribute, that determines
130 130 the order in which they are called. Smaller priorities are tried first.
131 131
132 132 Both transformers and checkers also have `enabled` attribute, which is
133 133 a boolean that determines if the instance is used.
134 134
135 135 Users or developers can change the priority or enabled attribute of
136 136 transformers or checkers, but they must call the :meth:`sort_checkers`
137 137 or :meth:`sort_transformers` method after changing the priority.
138 138 """
139 139
140 140 multi_line_specials = CBool(True, config=True)
141 141 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
142 142
143 143 def __init__(self, shell=None, config=None):
144 144 super(PrefilterManager, self).__init__(shell=shell, config=config)
145 145 self.shell = shell
146 146 self.init_transformers()
147 147 self.init_handlers()
148 148 self.init_checkers()
149 149
150 150 #-------------------------------------------------------------------------
151 151 # API for managing transformers
152 152 #-------------------------------------------------------------------------
153 153
154 154 def init_transformers(self):
155 155 """Create the default transformers."""
156 156 self._transformers = []
157 157 for transformer_cls in _default_transformers:
158 158 transformer_cls(
159 159 shell=self.shell, prefilter_manager=self, config=self.config
160 160 )
161 161
162 162 def sort_transformers(self):
163 163 """Sort the transformers by priority.
164 164
165 165 This must be called after the priority of a transformer is changed.
166 166 The :meth:`register_transformer` method calls this automatically.
167 167 """
168 168 self._transformers.sort(key=lambda x: x.priority)
169 169
170 170 @property
171 171 def transformers(self):
172 172 """Return a list of checkers, sorted by priority."""
173 173 return self._transformers
174 174
175 175 def register_transformer(self, transformer):
176 176 """Register a transformer instance."""
177 177 if transformer not in self._transformers:
178 178 self._transformers.append(transformer)
179 179 self.sort_transformers()
180 180
181 181 def unregister_transformer(self, transformer):
182 182 """Unregister a transformer instance."""
183 183 if transformer in self._transformers:
184 184 self._transformers.remove(transformer)
185 185
186 186 #-------------------------------------------------------------------------
187 187 # API for managing checkers
188 188 #-------------------------------------------------------------------------
189 189
190 190 def init_checkers(self):
191 191 """Create the default checkers."""
192 192 self._checkers = []
193 193 for checker in _default_checkers:
194 194 checker(
195 195 shell=self.shell, prefilter_manager=self, config=self.config
196 196 )
197 197
198 198 def sort_checkers(self):
199 199 """Sort the checkers by priority.
200 200
201 201 This must be called after the priority of a checker is changed.
202 202 The :meth:`register_checker` method calls this automatically.
203 203 """
204 204 self._checkers.sort(key=lambda x: x.priority)
205 205
206 206 @property
207 207 def checkers(self):
208 208 """Return a list of checkers, sorted by priority."""
209 209 return self._checkers
210 210
211 211 def register_checker(self, checker):
212 212 """Register a checker instance."""
213 213 if checker not in self._checkers:
214 214 self._checkers.append(checker)
215 215 self.sort_checkers()
216 216
217 217 def unregister_checker(self, checker):
218 218 """Unregister a checker instance."""
219 219 if checker in self._checkers:
220 220 self._checkers.remove(checker)
221 221
222 222 #-------------------------------------------------------------------------
223 223 # API for managing checkers
224 224 #-------------------------------------------------------------------------
225 225
226 226 def init_handlers(self):
227 227 """Create the default handlers."""
228 228 self._handlers = {}
229 229 self._esc_handlers = {}
230 230 for handler in _default_handlers:
231 231 handler(
232 232 shell=self.shell, prefilter_manager=self, config=self.config
233 233 )
234 234
235 235 @property
236 236 def handlers(self):
237 237 """Return a dict of all the handlers."""
238 238 return self._handlers
239 239
240 240 def register_handler(self, name, handler, esc_strings):
241 241 """Register a handler instance by name with esc_strings."""
242 242 self._handlers[name] = handler
243 243 for esc_str in esc_strings:
244 244 self._esc_handlers[esc_str] = handler
245 245
246 246 def unregister_handler(self, name, handler, esc_strings):
247 247 """Unregister a handler instance by name with esc_strings."""
248 248 try:
249 249 del self._handlers[name]
250 250 except KeyError:
251 251 pass
252 252 for esc_str in esc_strings:
253 253 h = self._esc_handlers.get(esc_str)
254 254 if h is handler:
255 255 del self._esc_handlers[esc_str]
256 256
257 257 def get_handler_by_name(self, name):
258 258 """Get a handler by its name."""
259 259 return self._handlers.get(name)
260 260
261 261 def get_handler_by_esc(self, esc_str):
262 262 """Get a handler by its escape string."""
263 263 return self._esc_handlers.get(esc_str)
264 264
265 265 #-------------------------------------------------------------------------
266 266 # Main prefiltering API
267 267 #-------------------------------------------------------------------------
268 268
269 269 def prefilter_line_info(self, line_info):
270 270 """Prefilter a line that has been converted to a LineInfo object.
271 271
272 272 This implements the checker/handler part of the prefilter pipe.
273 273 """
274 274 # print "prefilter_line_info: ", line_info
275 275 handler = self.find_handler(line_info)
276 276 return handler.handle(line_info)
277 277
278 278 def find_handler(self, line_info):
279 279 """Find a handler for the line_info by trying checkers."""
280 280 for checker in self.checkers:
281 281 if checker.enabled:
282 282 handler = checker.check(line_info)
283 283 if handler:
284 284 return handler
285 285 return self.get_handler_by_name('normal')
286 286
287 287 def transform_line(self, line, continue_prompt):
288 288 """Calls the enabled transformers in order of increasing priority."""
289 289 for transformer in self.transformers:
290 290 if transformer.enabled:
291 291 line = transformer.transform(line, continue_prompt)
292 292 return line
293 293
294 294 def prefilter_line(self, line, continue_prompt=False):
295 295 """Prefilter a single input line as text.
296 296
297 297 This method prefilters a single line of text by calling the
298 298 transformers and then the checkers/handlers.
299 299 """
300 300
301 301 # print "prefilter_line: ", line, continue_prompt
302 302 # All handlers *must* return a value, even if it's blank ('').
303 303
304 304 # save the line away in case we crash, so the post-mortem handler can
305 305 # record it
306 306 self.shell._last_input_line = line
307 307
308 308 if not line:
309 309 # Return immediately on purely empty lines, so that if the user
310 310 # previously typed some whitespace that started a continuation
311 311 # prompt, he can break out of that loop with just an empty line.
312 312 # This is how the default python prompt works.
313 313 return ''
314 314
315 315 # At this point, we invoke our transformers.
316 316 if not continue_prompt or (continue_prompt and self.multi_line_specials):
317 317 line = self.transform_line(line, continue_prompt)
318 318
319 319 # Now we compute line_info for the checkers and handlers
320 320 line_info = LineInfo(line, continue_prompt)
321 321
322 322 # the input history needs to track even empty lines
323 323 stripped = line.strip()
324 324
325 325 normal_handler = self.get_handler_by_name('normal')
326 326 if not stripped:
327 327 if not continue_prompt:
328 328 self.shell.displayhook.prompt_count -= 1
329 329
330 330 return normal_handler.handle(line_info)
331 331
332 332 # special handlers are only allowed for single line statements
333 333 if continue_prompt and not self.multi_line_specials:
334 334 return normal_handler.handle(line_info)
335 335
336 336 prefiltered = self.prefilter_line_info(line_info)
337 337 # print "prefiltered line: %r" % prefiltered
338 338 return prefiltered
339 339
340 340 def prefilter_lines(self, lines, continue_prompt=False):
341 341 """Prefilter multiple input lines of text.
342 342
343 343 This is the main entry point for prefiltering multiple lines of
344 344 input. This simply calls :meth:`prefilter_line` for each line of
345 345 input.
346 346
347 347 This covers cases where there are multiple lines in the user entry,
348 348 which is the case when the user goes back to a multiline history
349 349 entry and presses enter.
350 350 """
351 351 llines = lines.rstrip('\n').split('\n')
352 352 # We can get multiple lines in one shot, where multiline input 'blends'
353 353 # into one line, in cases like recalling from the readline history
354 354 # buffer. We need to make sure that in such cases, we correctly
355 355 # communicate downstream which line is first and which are continuation
356 356 # ones.
357 357 if len(llines) > 1:
358 358 out = '\n'.join([self.prefilter_line(line, lnum>0)
359 359 for lnum, line in enumerate(llines) ])
360 360 else:
361 361 out = self.prefilter_line(llines[0], continue_prompt)
362 362
363 363 return out
364 364
365 365 #-----------------------------------------------------------------------------
366 366 # Prefilter transformers
367 367 #-----------------------------------------------------------------------------
368 368
369 369
370 370 class PrefilterTransformer(Configurable):
371 371 """Transform a line of user input."""
372 372
373 373 priority = Integer(100, config=True)
374 374 # Transformers don't currently use shell or prefilter_manager, but as we
375 375 # move away from checkers and handlers, they will need them.
376 376 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
377 377 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
378 378 enabled = Bool(True, config=True)
379 379
380 380 def __init__(self, shell=None, prefilter_manager=None, config=None):
381 381 super(PrefilterTransformer, self).__init__(
382 382 shell=shell, prefilter_manager=prefilter_manager, config=config
383 383 )
384 384 self.prefilter_manager.register_transformer(self)
385 385
386 386 def transform(self, line, continue_prompt):
387 387 """Transform a line, returning the new one."""
388 388 return None
389 389
390 390 def __repr__(self):
391 391 return "<%s(priority=%r, enabled=%r)>" % (
392 392 self.__class__.__name__, self.priority, self.enabled)
393 393
394 394
395 395 _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
396 396 r'\s*=\s*!(?P<cmd>.*)')
397 397
398 398
399 399 class AssignSystemTransformer(PrefilterTransformer):
400 400 """Handle the `files = !ls` syntax."""
401 401
402 402 priority = Integer(100, config=True)
403 403
404 404 def transform(self, line, continue_prompt):
405 405 m = _assign_system_re.match(line)
406 406 if m is not None:
407 407 cmd = m.group('cmd')
408 408 lhs = m.group('lhs')
409 409 expr = "sc =%s" % cmd
410 410 new_line = '%s = get_ipython().magic(%r)' % (lhs, expr)
411 411 return new_line
412 412 return line
413 413
414 414
415 415 _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
416 416 r'\s*=\s*%(?P<cmd>.*)')
417 417
418 418 class AssignMagicTransformer(PrefilterTransformer):
419 419 """Handle the `a = %who` syntax."""
420 420
421 421 priority = Integer(200, config=True)
422 422
423 423 def transform(self, line, continue_prompt):
424 424 m = _assign_magic_re.match(line)
425 425 if m is not None:
426 426 cmd = m.group('cmd')
427 427 lhs = m.group('lhs')
428 428 new_line = '%s = get_ipython().magic(%r)' % (lhs, cmd)
429 429 return new_line
430 430 return line
431 431
432 432
433 433 _classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
434 434
435 435 class PyPromptTransformer(PrefilterTransformer):
436 436 """Handle inputs that start with '>>> ' syntax."""
437 437
438 438 priority = Integer(50, config=True)
439 439
440 440 def transform(self, line, continue_prompt):
441 441
442 442 if not line or line.isspace() or line.strip() == '...':
443 443 # This allows us to recognize multiple input prompts separated by
444 444 # blank lines and pasted in a single chunk, very common when
445 445 # pasting doctests or long tutorial passages.
446 446 return ''
447 447 m = _classic_prompt_re.match(line)
448 448 if m:
449 449 return line[len(m.group(0)):]
450 450 else:
451 451 return line
452 452
453 453
454 454 _ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
455 455
456 456 class IPyPromptTransformer(PrefilterTransformer):
457 457 """Handle inputs that start classic IPython prompt syntax."""
458 458
459 459 priority = Integer(50, config=True)
460 460
461 461 def transform(self, line, continue_prompt):
462 462
463 463 if not line or line.isspace() or line.strip() == '...':
464 464 # This allows us to recognize multiple input prompts separated by
465 465 # blank lines and pasted in a single chunk, very common when
466 466 # pasting doctests or long tutorial passages.
467 467 return ''
468 468 m = _ipy_prompt_re.match(line)
469 469 if m:
470 470 return line[len(m.group(0)):]
471 471 else:
472 472 return line
473 473
474 474 #-----------------------------------------------------------------------------
475 475 # Prefilter checkers
476 476 #-----------------------------------------------------------------------------
477 477
478 478
479 479 class PrefilterChecker(Configurable):
480 480 """Inspect an input line and return a handler for that line."""
481 481
482 482 priority = Integer(100, config=True)
483 483 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
484 484 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
485 485 enabled = Bool(True, config=True)
486 486
487 487 def __init__(self, shell=None, prefilter_manager=None, config=None):
488 488 super(PrefilterChecker, self).__init__(
489 489 shell=shell, prefilter_manager=prefilter_manager, config=config
490 490 )
491 491 self.prefilter_manager.register_checker(self)
492 492
493 493 def check(self, line_info):
494 494 """Inspect line_info and return a handler instance or None."""
495 495 return None
496 496
497 497 def __repr__(self):
498 498 return "<%s(priority=%r, enabled=%r)>" % (
499 499 self.__class__.__name__, self.priority, self.enabled)
500 500
501 501
502 502 class EmacsChecker(PrefilterChecker):
503 503
504 504 priority = Integer(100, config=True)
505 505 enabled = Bool(False, config=True)
506 506
507 507 def check(self, line_info):
508 508 "Emacs ipython-mode tags certain input lines."
509 509 if line_info.line.endswith('# PYTHON-MODE'):
510 510 return self.prefilter_manager.get_handler_by_name('emacs')
511 511 else:
512 512 return None
513 513
514 514
515 515 class ShellEscapeChecker(PrefilterChecker):
516 516
517 517 priority = Integer(200, config=True)
518 518
519 519 def check(self, line_info):
520 520 if line_info.line.lstrip().startswith(ESC_SHELL):
521 521 return self.prefilter_manager.get_handler_by_name('shell')
522 522
523 523
524 524 class MacroChecker(PrefilterChecker):
525 525
526 526 priority = Integer(250, config=True)
527 527
528 528 def check(self, line_info):
529 529 obj = self.shell.user_ns.get(line_info.ifun)
530 530 if isinstance(obj, Macro):
531 531 return self.prefilter_manager.get_handler_by_name('macro')
532 532 else:
533 533 return None
534 534
535 535
536 536 class IPyAutocallChecker(PrefilterChecker):
537 537
538 538 priority = Integer(300, config=True)
539 539
540 540 def check(self, line_info):
541 541 "Instances of IPyAutocall in user_ns get autocalled immediately"
542 542 obj = self.shell.user_ns.get(line_info.ifun, None)
543 543 if isinstance(obj, IPyAutocall):
544 544 obj.set_ip(self.shell)
545 545 return self.prefilter_manager.get_handler_by_name('auto')
546 546 else:
547 547 return None
548 548
549 549
550 550 class MultiLineMagicChecker(PrefilterChecker):
551 551
552 552 priority = Integer(400, config=True)
553 553
554 554 def check(self, line_info):
555 555 "Allow ! and !! in multi-line statements if multi_line_specials is on"
556 556 # Note that this one of the only places we check the first character of
557 557 # ifun and *not* the pre_char. Also note that the below test matches
558 558 # both ! and !!.
559 559 if line_info.continue_prompt \
560 560 and self.prefilter_manager.multi_line_specials:
561 561 if line_info.esc == ESC_MAGIC:
562 562 return self.prefilter_manager.get_handler_by_name('magic')
563 563 else:
564 564 return None
565 565
566 566
567 567 class EscCharsChecker(PrefilterChecker):
568 568
569 569 priority = Integer(500, config=True)
570 570
571 571 def check(self, line_info):
572 572 """Check for escape character and return either a handler to handle it,
573 573 or None if there is no escape char."""
574 574 if line_info.line[-1] == ESC_HELP \
575 575 and line_info.esc != ESC_SHELL \
576 576 and line_info.esc != ESC_SH_CAP:
577 577 # the ? can be at the end, but *not* for either kind of shell escape,
578 578 # because a ? can be a vaild final char in a shell cmd
579 579 return self.prefilter_manager.get_handler_by_name('help')
580 580 else:
581 581 if line_info.pre:
582 582 return None
583 583 # This returns None like it should if no handler exists
584 584 return self.prefilter_manager.get_handler_by_esc(line_info.esc)
585 585
586 586
587 587 class AssignmentChecker(PrefilterChecker):
588 588
589 589 priority = Integer(600, config=True)
590 590
591 591 def check(self, line_info):
592 592 """Check to see if user is assigning to a var for the first time, in
593 593 which case we want to avoid any sort of automagic / autocall games.
594 594
595 595 This allows users to assign to either alias or magic names true python
596 596 variables (the magic/alias systems always take second seat to true
597 597 python code). E.g. ls='hi', or ls,that=1,2"""
598 598 if line_info.the_rest:
599 599 if line_info.the_rest[0] in '=,':
600 600 return self.prefilter_manager.get_handler_by_name('normal')
601 601 else:
602 602 return None
603 603
604 604
605 605 class AutoMagicChecker(PrefilterChecker):
606 606
607 607 priority = Integer(700, config=True)
608 608
609 609 def check(self, line_info):
610 610 """If the ifun is magic, and automagic is on, run it. Note: normal,
611 611 non-auto magic would already have been triggered via '%' in
612 612 check_esc_chars. This just checks for automagic. Also, before
613 613 triggering the magic handler, make sure that there is nothing in the
614 614 user namespace which could shadow it."""
615 615 if not self.shell.automagic or not self.shell.find_magic(line_info.ifun):
616 616 return None
617 617
618 618 # We have a likely magic method. Make sure we should actually call it.
619 619 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
620 620 return None
621 621
622 622 head = line_info.ifun.split('.',1)[0]
623 623 if is_shadowed(head, self.shell):
624 624 return None
625 625
626 626 return self.prefilter_manager.get_handler_by_name('magic')
627 627
628 628
629 629 class AliasChecker(PrefilterChecker):
630 630
631 631 priority = Integer(800, config=True)
632 632
633 633 def check(self, line_info):
634 634 "Check if the initital identifier on the line is an alias."
635 635 # Note: aliases can not contain '.'
636 636 head = line_info.ifun.split('.',1)[0]
637 637 if line_info.ifun not in self.shell.alias_manager \
638 638 or head not in self.shell.alias_manager \
639 639 or is_shadowed(head, self.shell):
640 640 return None
641 641
642 642 return self.prefilter_manager.get_handler_by_name('alias')
643 643
644 644
645 645 class PythonOpsChecker(PrefilterChecker):
646 646
647 647 priority = Integer(900, config=True)
648 648
649 649 def check(self, line_info):
650 650 """If the 'rest' of the line begins with a function call or pretty much
651 651 any python operator, we should simply execute the line (regardless of
652 652 whether or not there's a possible autocall expansion). This avoids
653 653 spurious (and very confusing) geattr() accesses."""
654 654 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
655 655 return self.prefilter_manager.get_handler_by_name('normal')
656 656 else:
657 657 return None
658 658
659 659
660 660 class AutocallChecker(PrefilterChecker):
661 661
662 662 priority = Integer(1000, config=True)
663 663
664 664 function_name_regexp = CRegExp(re_fun_name, config=True,
665 665 help="RegExp to identify potential function names.")
666 666 exclude_regexp = CRegExp(re_exclude_auto, config=True,
667 667 help="RegExp to exclude strings with this start from autocalling.")
668 668
669 669 def check(self, line_info):
670 670 "Check if the initial word/function is callable and autocall is on."
671 671 if not self.shell.autocall:
672 672 return None
673 673
674 674 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
675 675 if not oinfo['found']:
676 676 return None
677 677
678 678 if callable(oinfo['obj']) \
679 679 and (not self.exclude_regexp.match(line_info.the_rest)) \
680 680 and self.function_name_regexp.match(line_info.ifun):
681 681 return self.prefilter_manager.get_handler_by_name('auto')
682 682 else:
683 683 return None
684 684
685 685
686 686 #-----------------------------------------------------------------------------
687 687 # Prefilter handlers
688 688 #-----------------------------------------------------------------------------
689 689
690 690
691 691 class PrefilterHandler(Configurable):
692 692
693 693 handler_name = Unicode('normal')
694 694 esc_strings = List([])
695 695 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
696 696 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
697 697
698 698 def __init__(self, shell=None, prefilter_manager=None, config=None):
699 699 super(PrefilterHandler, self).__init__(
700 700 shell=shell, prefilter_manager=prefilter_manager, config=config
701 701 )
702 702 self.prefilter_manager.register_handler(
703 703 self.handler_name,
704 704 self,
705 705 self.esc_strings
706 706 )
707 707
708 708 def handle(self, line_info):
709 709 # print "normal: ", line_info
710 710 """Handle normal input lines. Use as a template for handlers."""
711 711
712 712 # With autoindent on, we need some way to exit the input loop, and I
713 713 # don't want to force the user to have to backspace all the way to
714 714 # clear the line. The rule will be in this case, that either two
715 715 # lines of pure whitespace in a row, or a line of pure whitespace but
716 716 # of a size different to the indent level, will exit the input loop.
717 717 line = line_info.line
718 718 continue_prompt = line_info.continue_prompt
719 719
720 720 if (continue_prompt and
721 721 self.shell.autoindent and
722 722 line.isspace() and
723 723 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2):
724 724 line = ''
725 725
726 726 return line
727 727
728 728 def __str__(self):
729 729 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
730 730
731 731
732 732 class AliasHandler(PrefilterHandler):
733 733
734 734 handler_name = Unicode('alias')
735 735
736 736 def handle(self, line_info):
737 737 """Handle alias input lines. """
738 738 transformed = self.shell.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
739 739 # pre is needed, because it carries the leading whitespace. Otherwise
740 740 # aliases won't work in indented sections.
741 741 line_out = '%sget_ipython().system(%r)' % (line_info.pre_whitespace, transformed)
742 742
743 743 return line_out
744 744
745 745
746 746 class ShellEscapeHandler(PrefilterHandler):
747 747
748 748 handler_name = Unicode('shell')
749 749 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
750 750
751 751 def handle(self, line_info):
752 752 """Execute the line in a shell, empty return value"""
753 753 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
754 754
755 755 line = line_info.line
756 756 if line.lstrip().startswith(ESC_SH_CAP):
757 757 # rewrite LineInfo's line, ifun and the_rest to properly hold the
758 758 # call to %sx and the actual command to be executed, so
759 759 # handle_magic can work correctly. Note that this works even if
760 760 # the line is indented, so it handles multi_line_specials
761 761 # properly.
762 762 new_rest = line.lstrip()[2:]
763 763 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
764 764 line_info.ifun = 'sx'
765 765 line_info.the_rest = new_rest
766 766 return magic_handler.handle(line_info)
767 767 else:
768 768 cmd = line.lstrip().lstrip(ESC_SHELL)
769 769 line_out = '%sget_ipython().system(%r)' % (line_info.pre_whitespace, cmd)
770 770 return line_out
771 771
772 772
773 773 class MacroHandler(PrefilterHandler):
774 774 handler_name = Unicode("macro")
775 775
776 776 def handle(self, line_info):
777 777 obj = self.shell.user_ns.get(line_info.ifun)
778 778 pre_space = line_info.pre_whitespace
779 779 line_sep = "\n" + pre_space
780 780 return pre_space + line_sep.join(obj.value.splitlines())
781 781
782 782
783 783 class MagicHandler(PrefilterHandler):
784 784
785 785 handler_name = Unicode('magic')
786 786 esc_strings = List([ESC_MAGIC])
787 787
788 788 def handle(self, line_info):
789 789 """Execute magic functions."""
790 790 ifun = line_info.ifun
791 791 the_rest = line_info.the_rest
792 792 cmd = '%sget_ipython().magic(%r)' % (line_info.pre_whitespace,
793 793 (ifun + " " + the_rest))
794 794 return cmd
795 795
796 796
797 797 class AutoHandler(PrefilterHandler):
798 798
799 799 handler_name = Unicode('auto')
800 800 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
801 801
802 802 def handle(self, line_info):
803 803 """Handle lines which can be auto-executed, quoting if requested."""
804 804 line = line_info.line
805 805 ifun = line_info.ifun
806 806 the_rest = line_info.the_rest
807 807 pre = line_info.pre
808 808 esc = line_info.esc
809 809 continue_prompt = line_info.continue_prompt
810 obj = line_info.ofind(self)['obj']
810 obj = line_info.ofind(self.shell)['obj']
811 811 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
812 812
813 813 # This should only be active for single-line input!
814 814 if continue_prompt:
815 815 return line
816 816
817 817 force_auto = isinstance(obj, IPyAutocall)
818 818
819 819 # User objects sometimes raise exceptions on attribute access other
820 820 # than AttributeError (we've seen it in the past), so it's safest to be
821 821 # ultra-conservative here and catch all.
822 822 try:
823 823 auto_rewrite = obj.rewrite
824 824 except Exception:
825 825 auto_rewrite = True
826 826
827 827 if esc == ESC_QUOTE:
828 828 # Auto-quote splitting on whitespace
829 829 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
830 830 elif esc == ESC_QUOTE2:
831 831 # Auto-quote whole string
832 832 newcmd = '%s("%s")' % (ifun,the_rest)
833 833 elif esc == ESC_PAREN:
834 834 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
835 835 else:
836 836 # Auto-paren.
837 837 if force_auto:
838 838 # Don't rewrite if it is already a call.
839 839 do_rewrite = not the_rest.startswith('(')
840 840 else:
841 841 if not the_rest:
842 842 # We only apply it to argument-less calls if the autocall
843 843 # parameter is set to 2.
844 844 do_rewrite = (self.shell.autocall >= 2)
845 845 elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
846 846 # Don't autocall in this case: item access for an object
847 847 # which is BOTH callable and implements __getitem__.
848 848 do_rewrite = False
849 849 else:
850 850 do_rewrite = True
851 851
852 852 # Figure out the rewritten command
853 853 if do_rewrite:
854 854 if the_rest.endswith(';'):
855 855 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
856 856 else:
857 857 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
858 858 else:
859 859 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
860 860 return normal_handler.handle(line_info)
861 861
862 862 # Display the rewritten call
863 863 if auto_rewrite:
864 864 self.shell.auto_rewrite_input(newcmd)
865 865
866 866 return newcmd
867 867
868 868
869 869 class HelpHandler(PrefilterHandler):
870 870
871 871 handler_name = Unicode('help')
872 872 esc_strings = List([ESC_HELP])
873 873
874 874 def handle(self, line_info):
875 875 """Try to get some help for the object.
876 876
877 877 obj? or ?obj -> basic information.
878 878 obj?? or ??obj -> more details.
879 879 """
880 880 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
881 881 line = line_info.line
882 882 # We need to make sure that we don't process lines which would be
883 883 # otherwise valid python, such as "x=1 # what?"
884 884 try:
885 885 codeop.compile_command(line)
886 886 except SyntaxError:
887 887 # We should only handle as help stuff which is NOT valid syntax
888 888 if line[0]==ESC_HELP:
889 889 line = line[1:]
890 890 elif line[-1]==ESC_HELP:
891 891 line = line[:-1]
892 892 if line:
893 893 #print 'line:<%r>' % line # dbg
894 894 self.shell.magic('pinfo %s' % line_info.ifun)
895 895 else:
896 896 self.shell.show_usage()
897 897 return '' # Empty string is needed here!
898 898 except:
899 899 raise
900 900 # Pass any other exceptions through to the normal handler
901 901 return normal_handler.handle(line_info)
902 902 else:
903 903 # If the code compiles ok, we should handle it normally
904 904 return normal_handler.handle(line_info)
905 905
906 906
907 907 class EmacsHandler(PrefilterHandler):
908 908
909 909 handler_name = Unicode('emacs')
910 910 esc_strings = List([])
911 911
912 912 def handle(self, line_info):
913 913 """Handle input lines marked by python-mode."""
914 914
915 915 # Currently, nothing is done. Later more functionality can be added
916 916 # here if needed.
917 917
918 918 # The input cache shouldn't be updated
919 919 return line_info.line
920 920
921 921
922 922 #-----------------------------------------------------------------------------
923 923 # Defaults
924 924 #-----------------------------------------------------------------------------
925 925
926 926
927 927 _default_transformers = [
928 928 AssignSystemTransformer,
929 929 AssignMagicTransformer,
930 930 PyPromptTransformer,
931 931 IPyPromptTransformer,
932 932 ]
933 933
934 934 _default_checkers = [
935 935 EmacsChecker,
936 936 ShellEscapeChecker,
937 937 MacroChecker,
938 938 IPyAutocallChecker,
939 939 MultiLineMagicChecker,
940 940 EscCharsChecker,
941 941 AssignmentChecker,
942 942 AutoMagicChecker,
943 943 AliasChecker,
944 944 PythonOpsChecker,
945 945 AutocallChecker
946 946 ]
947 947
948 948 _default_handlers = [
949 949 PrefilterHandler,
950 950 AliasHandler,
951 951 ShellEscapeHandler,
952 952 MacroHandler,
953 953 MagicHandler,
954 954 AutoHandler,
955 955 HelpHandler,
956 956 EmacsHandler
957 957 ]
@@ -1,140 +1,142 b''
1 1 # encoding: utf-8
2 2 """
3 3 Simple utility for splitting user input. This is used by both inputsplitter and
4 4 prefilter.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2011 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import re
24 24 import sys
25 25
26 26 from IPython.utils import py3compat
27 27 from IPython.utils.encoding import get_stream_enc
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Main function
31 31 #-----------------------------------------------------------------------------
32 32
33 33 # RegExp for splitting line contents into pre-char//first word-method//rest.
34 34 # For clarity, each group in on one line.
35 35
36 36 # WARNING: update the regexp if the escapes in interactiveshell are changed, as
37 37 # they are hardwired in.
38 38
39 39 # Although it's not solely driven by the regex, note that:
40 40 # ,;/% only trigger if they are the first character on the line
41 41 # ! and !! trigger if they are first char(s) *or* follow an indent
42 42 # ? triggers as first or last char.
43 43
44 44 line_split = re.compile("""
45 45 ^(\s*) # any leading space
46 46 ([,;/%]|!!?|\?\??)? # escape character or characters
47 47 \s*(%?[\w\.\*]*) # function/method, possibly with leading %
48 48 # to correctly treat things like '?%magic'
49 49 (.*?$|$) # rest of line
50 50 """, re.VERBOSE)
51 51
52
52 53 def split_user_input(line, pattern=None):
53 54 """Split user input into initial whitespace, escape character, function part
54 55 and the rest.
55 56 """
56 57 # We need to ensure that the rest of this routine deals only with unicode
57 58 encoding = get_stream_enc(sys.stdin, 'utf-8')
58 59 line = py3compat.cast_unicode(line, encoding)
59 60
60 61 if pattern is None:
61 62 pattern = line_split
62 63 match = pattern.match(line)
63 64 if not match:
64 65 # print "match failed for line '%s'" % line
65 66 try:
66 67 ifun, the_rest = line.split(None,1)
67 68 except ValueError:
68 69 # print "split failed for line '%s'" % line
69 70 ifun, the_rest = line, u''
70 71 pre = re.match('^(\s*)(.*)',line).groups()[0]
71 72 esc = ""
72 73 else:
73 74 pre, esc, ifun, the_rest = match.groups()
74 75
75 76 #print 'line:<%s>' % line # dbg
76 77 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
77 78 return pre, esc or '', ifun.strip(), the_rest.lstrip()
78 79
80
79 81 class LineInfo(object):
80 82 """A single line of input and associated info.
81 83
82 84 Includes the following as properties:
83 85
84 86 line
85 87 The original, raw line
86 88
87 89 continue_prompt
88 90 Is this line a continuation in a sequence of multiline input?
89 91
90 92 pre
91 93 Any leading whitespace.
92 94
93 95 esc
94 96 The escape character(s) in pre or the empty string if there isn't one.
95 97 Note that '!!' and '??' are possible values for esc. Otherwise it will
96 98 always be a single character.
97 99
98 100 ifun
99 101 The 'function part', which is basically the maximal initial sequence
100 102 of valid python identifiers and the '.' character. This is what is
101 103 checked for alias and magic transformations, used for auto-calling,
102 104 etc. In contrast to Python identifiers, it may start with "%" and contain
103 105 "*".
104 106
105 107 the_rest
106 108 Everything else on the line.
107 109 """
108 110 def __init__(self, line, continue_prompt=False):
109 111 self.line = line
110 112 self.continue_prompt = continue_prompt
111 113 self.pre, self.esc, self.ifun, self.the_rest = split_user_input(line)
112 114
113 115 self.pre_char = self.pre.strip()
114 116 if self.pre_char:
115 117 self.pre_whitespace = '' # No whitespace allowd before esc chars
116 118 else:
117 119 self.pre_whitespace = self.pre
118 120
119 121 self._oinfo = None
120 122
121 123 def ofind(self, ip):
122 124 """Do a full, attribute-walking lookup of the ifun in the various
123 125 namespaces for the given IPython InteractiveShell instance.
124 126
125 Return a dict with keys: found,obj,ospace,ismagic
127 Return a dict with keys: {found, obj, ospace, ismagic}
126 128
127 129 Note: can cause state changes because of calling getattr, but should
128 130 only be run if autocall is on and if the line hasn't matched any
129 131 other, less dangerous handlers.
130 132
131 133 Does cache the results of the call, so can be called multiple times
132 134 without worrying about *further* damaging state.
133 135 """
134 136 if not self._oinfo:
135 137 # ip.shell._ofind is actually on the Magic class!
136 138 self._oinfo = ip._ofind(self.ifun)
137 139 return self._oinfo
138 140
139 141 def __str__(self):
140 142 return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest)
General Comments 0
You need to be logged in to leave comments. Login now