##// END OF EJS Templates
Create core.magics.auto according to new API.
Fernando Perez -
Show More
@@ -0,0 +1,128 b''
1 """Implementation of execution-related magic functions.
2 """
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
5 #
6 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
15 # Our own packages
16 from IPython.core.magic import Bunch, Magics, register_magics, line_magic
17 from IPython.testing.skipdoctest import skip_doctest
18 from IPython.utils.warn import error
19
20 #-----------------------------------------------------------------------------
21 # Magic implementation classes
22 #-----------------------------------------------------------------------------
23
24 @register_magics
25 class AutoMagics(Magics):
26 """Magics that control various autoX behaviors."""
27
28 def __init__(self, shell):
29 super(AutoMagics, self).__init__(shell)
30 # namespace for holding state we may need
31 self._magic_state = Bunch()
32
33 @line_magic
34 def automagic(self, parameter_s=''):
35 """Make magic functions callable without having to type the initial %.
36
37 Without argumentsl toggles on/off (when off, you must call it as
38 %automagic, of course). With arguments it sets the value, and you can
39 use any of (case insensitive):
40
41 - on, 1, True: to activate
42
43 - off, 0, False: to deactivate.
44
45 Note that magic functions have lowest priority, so if there's a
46 variable whose name collides with that of a magic fn, automagic won't
47 work for that function (you get the variable instead). However, if you
48 delete the variable (del var), the previously shadowed magic function
49 becomes visible to automagic again."""
50
51 arg = parameter_s.lower()
52 mman = self.shell.magics_manager
53 if arg in ('on', '1', 'true'):
54 val = True
55 elif arg in ('off', '0', 'false'):
56 val = False
57 else:
58 val = not mman.auto_magic
59 mman.auto_magic = val
60 print '\n' + self.shell.magics_manager.auto_status()
61
62 @skip_doctest
63 @line_magic
64 def autocall(self, parameter_s=''):
65 """Make functions callable without having to type parentheses.
66
67 Usage:
68
69 %autocall [mode]
70
71 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
72 value is toggled on and off (remembering the previous state).
73
74 In more detail, these values mean:
75
76 0 -> fully disabled
77
78 1 -> active, but do not apply if there are no arguments on the line.
79
80 In this mode, you get::
81
82 In [1]: callable
83 Out[1]: <built-in function callable>
84
85 In [2]: callable 'hello'
86 ------> callable('hello')
87 Out[2]: False
88
89 2 -> Active always. Even if no arguments are present, the callable
90 object is called::
91
92 In [2]: float
93 ------> float()
94 Out[2]: 0.0
95
96 Note that even with autocall off, you can still use '/' at the start of
97 a line to treat the first argument on the command line as a function
98 and add parentheses to it::
99
100 In [8]: /str 43
101 ------> str(43)
102 Out[8]: '43'
103
104 # all-random (note for auto-testing)
105 """
106
107 if parameter_s:
108 arg = int(parameter_s)
109 else:
110 arg = 'toggle'
111
112 if not arg in (0, 1, 2,'toggle'):
113 error('Valid modes: (0->Off, 1->Smart, 2->Full')
114 return
115
116 if arg in (0, 1, 2):
117 self.shell.autocall = arg
118 else: # toggle
119 if self.shell.autocall:
120 self._magic_state.autocall_save = self.shell.autocall
121 self.shell.autocall = 0
122 else:
123 try:
124 self.shell.autocall = self._magic_state.autocall_save
125 except AttributeError:
126 self.shell.autocall = self._magic_state.autocall_save = 1
127
128 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
@@ -2005,7 +2005,7 b' class InteractiveShell(SingletonConfigurable):'
2005 2005 self.register_magic_function = self.magics_manager.register_function
2006 2006 self.define_magic = self.magics_manager.define_magic
2007 2007
2008 self.register_magics(mf.AutoMagics, m.BasicMagics, m.CodeMagics,
2008 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2009 2009 m.ConfigMagics, mf.DeprecatedMagics, m.ExecutionMagics,
2010 2010 mf.ExtensionsMagics, m.HistoryMagics, mf.LoggingMagics,
2011 2011 m.NamespaceMagics, mf.OSMagics, mf.PylabMagics )
@@ -67,113 +67,6 b' from IPython.utils.warn import warn, error'
67 67 #-----------------------------------------------------------------------------
68 68
69 69 @register_magics
70 class AutoMagics(Magics):
71 """Magics that control various autoX behaviors."""
72
73 def __init__(self, shell):
74 super(AutoMagics, self).__init__(shell)
75 # namespace for holding state we may need
76 self._magic_state = Bunch()
77
78 @line_magic
79 def automagic(self, parameter_s=''):
80 """Make magic functions callable without having to type the initial %.
81
82 Without argumentsl toggles on/off (when off, you must call it as
83 %automagic, of course). With arguments it sets the value, and you can
84 use any of (case insensitive):
85
86 - on, 1, True: to activate
87
88 - off, 0, False: to deactivate.
89
90 Note that magic functions have lowest priority, so if there's a
91 variable whose name collides with that of a magic fn, automagic won't
92 work for that function (you get the variable instead). However, if you
93 delete the variable (del var), the previously shadowed magic function
94 becomes visible to automagic again."""
95
96 arg = parameter_s.lower()
97 mman = self.shell.magics_manager
98 if arg in ('on', '1', 'true'):
99 val = True
100 elif arg in ('off', '0', 'false'):
101 val = False
102 else:
103 val = not mman.auto_magic
104 mman.auto_magic = val
105 print '\n' + self.shell.magics_manager.auto_status()
106
107 @skip_doctest
108 @line_magic
109 def autocall(self, parameter_s=''):
110 """Make functions callable without having to type parentheses.
111
112 Usage:
113
114 %autocall [mode]
115
116 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
117 value is toggled on and off (remembering the previous state).
118
119 In more detail, these values mean:
120
121 0 -> fully disabled
122
123 1 -> active, but do not apply if there are no arguments on the line.
124
125 In this mode, you get::
126
127 In [1]: callable
128 Out[1]: <built-in function callable>
129
130 In [2]: callable 'hello'
131 ------> callable('hello')
132 Out[2]: False
133
134 2 -> Active always. Even if no arguments are present, the callable
135 object is called::
136
137 In [2]: float
138 ------> float()
139 Out[2]: 0.0
140
141 Note that even with autocall off, you can still use '/' at the start of
142 a line to treat the first argument on the command line as a function
143 and add parentheses to it::
144
145 In [8]: /str 43
146 ------> str(43)
147 Out[8]: '43'
148
149 # all-random (note for auto-testing)
150 """
151
152 if parameter_s:
153 arg = int(parameter_s)
154 else:
155 arg = 'toggle'
156
157 if not arg in (0, 1, 2,'toggle'):
158 error('Valid modes: (0->Off, 1->Smart, 2->Full')
159 return
160
161 if arg in (0, 1, 2):
162 self.shell.autocall = arg
163 else: # toggle
164 if self.shell.autocall:
165 self._magic_state.autocall_save = self.shell.autocall
166 self.shell.autocall = 0
167 else:
168 try:
169 self.shell.autocall = self._magic_state.autocall_save
170 except AttributeError:
171 self.shell.autocall = self._magic_state.autocall_save = 1
172
173 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
174
175
176 @register_magics
177 70 class OSMagics(Magics):
178 71 """Magics to interact with the underlying OS (shell-type functionality).
179 72 """
@@ -13,6 +13,7 b''
13 13 #-----------------------------------------------------------------------------
14 14
15 15 from IPython.core.magic import Magics, register_magics
16 from .auto import AutoMagics
16 17 from .basic import BasicMagics
17 18 from .code import CodeMagics, MacroToEdit
18 19 from .config import ConfigMagics
General Comments 0
You need to be logged in to leave comments. Login now