##// END OF EJS Templates
Update embedded shell to new magics API.
Fernando Perez -
Show More
@@ -23,16 +23,13 b' Notes'
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 from __future__ import with_statement
25 from __future__ import with_statement
26 import __main__
27
26
28 import sys
27 import sys
29 try:
28 from contextlib import nested
30 from contextlib import nested
31 except:
32 from IPython.utils.nested_context import nested
33 import warnings
29 import warnings
34
30
35 from IPython.core import ultratb
31 from IPython.core import ultratb
32 from IPython.core.magic import Magics, register_magics, line_magic
36 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
37 from IPython.frontend.terminal.ipapp import load_default_config
34 from IPython.frontend.terminal.ipapp import load_default_config
38
35
@@ -45,21 +42,27 b' from IPython.utils.io import ask_yes_no'
45 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
46
43
47 # This is an additional magic that is exposed in embedded shells.
44 # This is an additional magic that is exposed in embedded shells.
48 def kill_embedded(self,parameter_s=''):
45 @register_magics
49 """%kill_embedded : deactivate for good the current embedded IPython.
46 class EmbeddedMagics(Magics):
50
47
51 This function (after asking for confirmation) sets an internal flag so that
48 @line_magic
52 an embedded IPython will never activate again. This is useful to
49 def kill_embedded(self, parameter_s=''):
53 permanently disable a shell that is being called inside a loop: once you've
50 """%kill_embedded : deactivate for good the current embedded IPython.
54 figured out what you needed from it, you may then kill it and the program
51
55 will then continue to run without the interactive shell interfering again.
52 This function (after asking for confirmation) sets an internal flag so
56 """
53 that an embedded IPython will never activate again. This is useful to
54 permanently disable a shell that is being called inside a loop: once
55 you've figured out what you needed from it, you may then kill it and
56 the program will then continue to run without the interactive shell
57 interfering again.
58 """
57
59
58 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
60 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
59 "(y/n)? [y/N] ",'n')
61 "(y/n)? [y/N] ",'n')
60 if kill:
62 if kill:
61 self.embedded_active = False
63 self.shell.embedded_active = False
62 print "This embedded IPython will not reactivate anymore once you exit."
64 print ("This embedded IPython will not reactivate anymore "
65 "once you exit.")
63
66
64
67
65 class InteractiveShellEmbed(TerminalInteractiveShell):
68 class InteractiveShellEmbed(TerminalInteractiveShell):
@@ -89,7 +92,6 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
89 )
92 )
90
93
91 self.exit_msg = exit_msg
94 self.exit_msg = exit_msg
92 self.define_magic("kill_embedded", kill_embedded)
93
95
94 # don't use the ipython crash handler so that user exceptions aren't
96 # don't use the ipython crash handler so that user exceptions aren't
95 # trapped
97 # trapped
@@ -100,6 +102,10 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
100 def init_sys_modules(self):
102 def init_sys_modules(self):
101 pass
103 pass
102
104
105 def init_magics(self):
106 super(InteractiveShellEmbed, self).init_magics()
107 self.register_magics(EmbeddedMagics)
108
103 def __call__(self, header='', local_ns=None, module=None, dummy=None,
109 def __call__(self, header='', local_ns=None, module=None, dummy=None,
104 stack_depth=1, global_ns=None):
110 stack_depth=1, global_ns=None):
105 """Activate the interactive interpreter.
111 """Activate the interactive interpreter.
General Comments 0
You need to be logged in to leave comments. Login now