|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +""" |
| 4 | +An embedded IPython shell. |
| 5 | +
|
| 6 | +Authors: |
| 7 | +
|
| 8 | +* Brian Granger |
| 9 | +* Fernando Perez |
| 10 | +
|
| 11 | +Notes |
| 12 | +----- |
| 13 | +""" |
| 14 | + |
| 15 | +#----------------------------------------------------------------------------- |
| 16 | +# Copyright (C) 2008-2009 The IPython Development Team |
| 17 | +# |
| 18 | +# Distributed under the terms of the BSD License. The full license is in |
| 19 | +# the file COPYING, distributed as part of this software. |
| 20 | +#----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +#----------------------------------------------------------------------------- |
| 23 | +# Imports |
| 24 | +#----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +import sys |
| 27 | + |
| 28 | +from IPython.core import ultratb |
| 29 | +from IPython.core.iplib import InteractiveShell |
| 30 | + |
| 31 | +from IPython.utils.traitlets import Bool, Str |
| 32 | +from IPython.utils.genutils import ask_yes_no |
| 33 | + |
| 34 | +#----------------------------------------------------------------------------- |
| 35 | +# Classes and functions |
| 36 | +#----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +# This is an additional magic that is exposed in embedded shells. |
| 39 | +def kill_embedded(self,parameter_s=''): |
| 40 | + """%kill_embedded : deactivate for good the current embedded IPython. |
| 41 | +
|
| 42 | + This function (after asking for confirmation) sets an internal flag so that |
| 43 | + an embedded IPython will never activate again. This is useful to |
| 44 | + permanently disable a shell that is being called inside a loop: once you've |
| 45 | + figured out what you needed from it, you may then kill it and the program |
| 46 | + will then continue to run without the interactive shell interfering again. |
| 47 | + """ |
| 48 | + |
| 49 | + kill = ask_yes_no("Are you sure you want to kill this embedded instance " |
| 50 | + "(y/n)? [y/N] ",'n') |
| 51 | + if kill: |
| 52 | + self.embedded_active = False |
| 53 | + print "This embedded IPython will not reactivate anymore once you exit." |
| 54 | + |
| 55 | + |
| 56 | +class InteractiveShellEmbed(InteractiveShell): |
| 57 | + |
| 58 | + dummy_mode = Bool(False) |
| 59 | + exit_msg = Str('') |
| 60 | + |
| 61 | + def __init__(self, parent=None, config=None, usage=None, |
| 62 | + user_ns=None, user_global_ns=None, |
| 63 | + banner1='', banner2='', |
| 64 | + custom_exceptions=((),None), exit_msg=''): |
| 65 | + |
| 66 | + # First we need to save the state of sys.displayhook and |
| 67 | + # sys.ipcompleter so we can restore it when we are done. |
| 68 | + self.save_sys_displayhook() |
| 69 | + self.save_sys_ipcompleter() |
| 70 | + |
| 71 | + super(InteractiveShellEmbed,self).__init__( |
| 72 | + parent=parent, config=config, usage=usage, |
| 73 | + user_ns=user_ns, user_global_ns=user_global_ns, |
| 74 | + banner1=banner1, banner2=banner2, |
| 75 | + custom_exceptions=custom_exceptions, embedded=True) |
| 76 | + |
| 77 | + self.save_sys_displayhook_embed() |
| 78 | + self.exit_msg = exit_msg |
| 79 | + self.define_magic("kill_embedded", kill_embedded) |
| 80 | + |
| 81 | + # don't use the ipython crash handler so that user exceptions aren't |
| 82 | + # trapped |
| 83 | + sys.excepthook = ultratb.FormattedTB(color_scheme = self.colors, |
| 84 | + mode = self.xmode, |
| 85 | + call_pdb = self.pdb) |
| 86 | + |
| 87 | + self.restore_sys_displayhook() |
| 88 | + self.restore_sys_ipcompleter() |
| 89 | + |
| 90 | + def save_sys_displayhook(self): |
| 91 | + # sys.displayhook is a global, we need to save the user's original |
| 92 | + # Don't rely on __displayhook__, as the user may have changed that. |
| 93 | + self.sys_displayhook_orig = sys.displayhook |
| 94 | + |
| 95 | + def save_sys_ipcompleter(self): |
| 96 | + """Save readline completer status.""" |
| 97 | + try: |
| 98 | + #print 'Save completer',sys.ipcompleter # dbg |
| 99 | + self.sys_ipcompleter_orig = sys.ipcompleter |
| 100 | + except: |
| 101 | + pass # not nested with IPython |
| 102 | + |
| 103 | + def restore_sys_displayhook(self): |
| 104 | + sys.displayhook = self.sys_displayhook_orig |
| 105 | + |
| 106 | + def restore_sys_ipcompleter(self): |
| 107 | + """Restores the readline completer which was in place. |
| 108 | +
|
| 109 | + This allows embedded IPython within IPython not to disrupt the |
| 110 | + parent's completion. |
| 111 | + """ |
| 112 | + try: |
| 113 | + self.readline.set_completer(self.sys_ipcompleter_orig) |
| 114 | + sys.ipcompleter = self.sys_ipcompleter_orig |
| 115 | + except: |
| 116 | + pass |
| 117 | + |
| 118 | + def save_sys_displayhook_embed(self): |
| 119 | + self.sys_displayhook_embed = sys.displayhook |
| 120 | + |
| 121 | + def restore_sys_displayhook_embed(self): |
| 122 | + sys.displayhook = self.sys_displayhook_embed |
| 123 | + |
| 124 | + def __call__(self, header='', local_ns=None, global_ns=None, dummy=None): |
| 125 | + """Activate the interactive interpreter. |
| 126 | +
|
| 127 | + __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
| 128 | + the interpreter shell with the given local and global namespaces, and |
| 129 | + optionally print a header string at startup. |
| 130 | +
|
| 131 | + The shell can be globally activated/deactivated using the |
| 132 | + set/get_dummy_mode methods. This allows you to turn off a shell used |
| 133 | + for debugging globally. |
| 134 | +
|
| 135 | + However, *each* time you call the shell you can override the current |
| 136 | + state of dummy_mode with the optional keyword parameter 'dummy'. For |
| 137 | + example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
| 138 | + can still have a specific call work by making it as IPShell(dummy=0). |
| 139 | +
|
| 140 | + The optional keyword parameter dummy controls whether the call |
| 141 | + actually does anything. |
| 142 | + """ |
| 143 | + |
| 144 | + # If the user has turned it off, go away |
| 145 | + if not self.embedded_active: |
| 146 | + return |
| 147 | + |
| 148 | + # Normal exits from interactive mode set this flag, so the shell can't |
| 149 | + # re-enter (it checks this variable at the start of interactive mode). |
| 150 | + self.exit_now = False |
| 151 | + |
| 152 | + # Allow the dummy parameter to override the global __dummy_mode |
| 153 | + if dummy or (dummy != 0 and self.dummy_mode): |
| 154 | + return |
| 155 | + |
| 156 | + self.restore_sys_displayhook_embed() |
| 157 | + |
| 158 | + if self.has_readline: |
| 159 | + self.set_completer() |
| 160 | + |
| 161 | + if self.banner and header: |
| 162 | + format = '%s\n%s\n' |
| 163 | + else: |
| 164 | + format = '%s%s\n' |
| 165 | + banner = format % (self.banner,header) |
| 166 | + |
| 167 | + # Call the embedding code with a stack depth of 1 so it can skip over |
| 168 | + # our call and get the original caller's namespaces. |
| 169 | + self.embed_mainloop(banner, local_ns, global_ns, stack_depth=1) |
| 170 | + |
| 171 | + if self.exit_msg: |
| 172 | + print self.exit_msg |
| 173 | + |
| 174 | + # Restore global systems (display, completion) |
| 175 | + self.restore_sys_displayhook() |
| 176 | + self.restore_sys_ipcompleter() |
0 commit comments