##// END OF EJS Templates
Documentation update
nfgf -
Show More
@@ -1,757 +1,757 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2008 The IPython Development Team
8 # Copyright (C) 2008 The IPython Development Team
9
9
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 import os
14 import os
15 import re
15 import re
16 import sys
16 import sys
17 from getopt import getopt, GetoptError
17 from getopt import getopt, GetoptError
18
18
19 from traitlets.config.configurable import Configurable
19 from traitlets.config.configurable import Configurable
20 from . import oinspect
20 from . import oinspect
21 from .error import UsageError
21 from .error import UsageError
22 from .inputtransformer2 import ESC_MAGIC, ESC_MAGIC2
22 from .inputtransformer2 import ESC_MAGIC, ESC_MAGIC2
23 from ..utils.ipstruct import Struct
23 from ..utils.ipstruct import Struct
24 from ..utils.process import arg_split
24 from ..utils.process import arg_split
25 from ..utils.text import dedent
25 from ..utils.text import dedent
26 from traitlets import Bool, Dict, Instance, observe
26 from traitlets import Bool, Dict, Instance, observe
27 from logging import error
27 from logging import error
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Globals
30 # Globals
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 # A dict we'll use for each class that has magics, used as temporary storage to
33 # A dict we'll use for each class that has magics, used as temporary storage to
34 # pass information between the @line/cell_magic method decorators and the
34 # pass information between the @line/cell_magic method decorators and the
35 # @magics_class class decorator, because the method decorators have no
35 # @magics_class class decorator, because the method decorators have no
36 # access to the class when they run. See for more details:
36 # access to the class when they run. See for more details:
37 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
37 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
38
38
39 magics = dict(line={}, cell={})
39 magics = dict(line={}, cell={})
40
40
41 magic_kinds = ('line', 'cell')
41 magic_kinds = ('line', 'cell')
42 magic_spec = ('line', 'cell', 'line_cell')
42 magic_spec = ('line', 'cell', 'line_cell')
43 magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2)
43 magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2)
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Utility classes and functions
46 # Utility classes and functions
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49 class Bunch: pass
49 class Bunch: pass
50
50
51
51
52 def on_off(tag):
52 def on_off(tag):
53 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
53 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
54 return ['OFF','ON'][tag]
54 return ['OFF','ON'][tag]
55
55
56
56
57 def compress_dhist(dh):
57 def compress_dhist(dh):
58 """Compress a directory history into a new one with at most 20 entries.
58 """Compress a directory history into a new one with at most 20 entries.
59
59
60 Return a new list made from the first and last 10 elements of dhist after
60 Return a new list made from the first and last 10 elements of dhist after
61 removal of duplicates.
61 removal of duplicates.
62 """
62 """
63 head, tail = dh[:-10], dh[-10:]
63 head, tail = dh[:-10], dh[-10:]
64
64
65 newhead = []
65 newhead = []
66 done = set()
66 done = set()
67 for h in head:
67 for h in head:
68 if h in done:
68 if h in done:
69 continue
69 continue
70 newhead.append(h)
70 newhead.append(h)
71 done.add(h)
71 done.add(h)
72
72
73 return newhead + tail
73 return newhead + tail
74
74
75
75
76 def needs_local_scope(func):
76 def needs_local_scope(func):
77 """Decorator to mark magic functions which need to local scope to run."""
77 """Decorator to mark magic functions which need to local scope to run."""
78 func.needs_local_scope = True
78 func.needs_local_scope = True
79 return func
79 return func
80
80
81 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
82 # Class and method decorators for registering magics
82 # Class and method decorators for registering magics
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84
84
85 def magics_class(cls):
85 def magics_class(cls):
86 """Class decorator for all subclasses of the main Magics class.
86 """Class decorator for all subclasses of the main Magics class.
87
87
88 Any class that subclasses Magics *must* also apply this decorator, to
88 Any class that subclasses Magics *must* also apply this decorator, to
89 ensure that all the methods that have been decorated as line/cell magics
89 ensure that all the methods that have been decorated as line/cell magics
90 get correctly registered in the class instance. This is necessary because
90 get correctly registered in the class instance. This is necessary because
91 when method decorators run, the class does not exist yet, so they
91 when method decorators run, the class does not exist yet, so they
92 temporarily store their information into a module global. Application of
92 temporarily store their information into a module global. Application of
93 this class decorator copies that global data to the class instance and
93 this class decorator copies that global data to the class instance and
94 clears the global.
94 clears the global.
95
95
96 Obviously, this mechanism is not thread-safe, which means that the
96 Obviously, this mechanism is not thread-safe, which means that the
97 *creation* of subclasses of Magic should only be done in a single-thread
97 *creation* of subclasses of Magic should only be done in a single-thread
98 context. Instantiation of the classes has no restrictions. Given that
98 context. Instantiation of the classes has no restrictions. Given that
99 these classes are typically created at IPython startup time and before user
99 these classes are typically created at IPython startup time and before user
100 application code becomes active, in practice this should not pose any
100 application code becomes active, in practice this should not pose any
101 problems.
101 problems.
102 """
102 """
103 cls.registered = True
103 cls.registered = True
104 cls.magics = dict(line = magics['line'],
104 cls.magics = dict(line = magics['line'],
105 cell = magics['cell'])
105 cell = magics['cell'])
106 magics['line'] = {}
106 magics['line'] = {}
107 magics['cell'] = {}
107 magics['cell'] = {}
108 return cls
108 return cls
109
109
110
110
111 def record_magic(dct, magic_kind, magic_name, func):
111 def record_magic(dct, magic_kind, magic_name, func):
112 """Utility function to store a function as a magic of a specific kind.
112 """Utility function to store a function as a magic of a specific kind.
113
113
114 Parameters
114 Parameters
115 ----------
115 ----------
116 dct : dict
116 dct : dict
117 A dictionary with 'line' and 'cell' subdicts.
117 A dictionary with 'line' and 'cell' subdicts.
118 magic_kind : str
118 magic_kind : str
119 Kind of magic to be stored.
119 Kind of magic to be stored.
120 magic_name : str
120 magic_name : str
121 Key to store the magic as.
121 Key to store the magic as.
122 func : function
122 func : function
123 Callable object to store.
123 Callable object to store.
124 """
124 """
125 if magic_kind == 'line_cell':
125 if magic_kind == 'line_cell':
126 dct['line'][magic_name] = dct['cell'][magic_name] = func
126 dct['line'][magic_name] = dct['cell'][magic_name] = func
127 else:
127 else:
128 dct[magic_kind][magic_name] = func
128 dct[magic_kind][magic_name] = func
129
129
130
130
131 def validate_type(magic_kind):
131 def validate_type(magic_kind):
132 """Ensure that the given magic_kind is valid.
132 """Ensure that the given magic_kind is valid.
133
133
134 Check that the given magic_kind is one of the accepted spec types (stored
134 Check that the given magic_kind is one of the accepted spec types (stored
135 in the global `magic_spec`), raise ValueError otherwise.
135 in the global `magic_spec`), raise ValueError otherwise.
136 """
136 """
137 if magic_kind not in magic_spec:
137 if magic_kind not in magic_spec:
138 raise ValueError('magic_kind must be one of %s, %s given' %
138 raise ValueError('magic_kind must be one of %s, %s given' %
139 magic_kinds, magic_kind)
139 magic_kinds, magic_kind)
140
140
141
141
142 # The docstrings for the decorator below will be fairly similar for the two
142 # The docstrings for the decorator below will be fairly similar for the two
143 # types (method and function), so we generate them here once and reuse the
143 # types (method and function), so we generate them here once and reuse the
144 # templates below.
144 # templates below.
145 _docstring_template = \
145 _docstring_template = \
146 """Decorate the given {0} as {1} magic.
146 """Decorate the given {0} as {1} magic.
147
147
148 The decorator can be used with or without arguments, as follows.
148 The decorator can be used with or without arguments, as follows.
149
149
150 i) without arguments: it will create a {1} magic named as the {0} being
150 i) without arguments: it will create a {1} magic named as the {0} being
151 decorated::
151 decorated::
152
152
153 @deco
153 @deco
154 def foo(...)
154 def foo(...)
155
155
156 will create a {1} magic named `foo`.
156 will create a {1} magic named `foo`.
157
157
158 ii) with one string argument: which will be used as the actual name of the
158 ii) with one string argument: which will be used as the actual name of the
159 resulting magic::
159 resulting magic::
160
160
161 @deco('bar')
161 @deco('bar')
162 def foo(...)
162 def foo(...)
163
163
164 will create a {1} magic named `bar`.
164 will create a {1} magic named `bar`.
165
165
166 To register a class magic use ``Interactiveshell.register_magic(class or instance)``.
166 To register a class magic use ``Interactiveshell.register_magic(class or instance)``.
167 """
167 """
168
168
169 # These two are decorator factories. While they are conceptually very similar,
169 # These two are decorator factories. While they are conceptually very similar,
170 # there are enough differences in the details that it's simpler to have them
170 # there are enough differences in the details that it's simpler to have them
171 # written as completely standalone functions rather than trying to share code
171 # written as completely standalone functions rather than trying to share code
172 # and make a single one with convoluted logic.
172 # and make a single one with convoluted logic.
173
173
174 def _method_magic_marker(magic_kind):
174 def _method_magic_marker(magic_kind):
175 """Decorator factory for methods in Magics subclasses.
175 """Decorator factory for methods in Magics subclasses.
176 """
176 """
177
177
178 validate_type(magic_kind)
178 validate_type(magic_kind)
179
179
180 # This is a closure to capture the magic_kind. We could also use a class,
180 # This is a closure to capture the magic_kind. We could also use a class,
181 # but it's overkill for just that one bit of state.
181 # but it's overkill for just that one bit of state.
182 def magic_deco(arg):
182 def magic_deco(arg):
183 if callable(arg):
183 if callable(arg):
184 # "Naked" decorator call (just @foo, no args)
184 # "Naked" decorator call (just @foo, no args)
185 func = arg
185 func = arg
186 name = func.__name__
186 name = func.__name__
187 retval = arg
187 retval = arg
188 record_magic(magics, magic_kind, name, name)
188 record_magic(magics, magic_kind, name, name)
189 elif isinstance(arg, str):
189 elif isinstance(arg, str):
190 # Decorator called with arguments (@foo('bar'))
190 # Decorator called with arguments (@foo('bar'))
191 name = arg
191 name = arg
192 def mark(func, *a, **kw):
192 def mark(func, *a, **kw):
193 record_magic(magics, magic_kind, name, func.__name__)
193 record_magic(magics, magic_kind, name, func.__name__)
194 return func
194 return func
195 retval = mark
195 retval = mark
196 else:
196 else:
197 raise TypeError("Decorator can only be called with "
197 raise TypeError("Decorator can only be called with "
198 "string or function")
198 "string or function")
199 return retval
199 return retval
200
200
201 # Ensure the resulting decorator has a usable docstring
201 # Ensure the resulting decorator has a usable docstring
202 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
202 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
203 return magic_deco
203 return magic_deco
204
204
205
205
206 def _function_magic_marker(magic_kind):
206 def _function_magic_marker(magic_kind):
207 """Decorator factory for standalone functions.
207 """Decorator factory for standalone functions.
208 """
208 """
209 validate_type(magic_kind)
209 validate_type(magic_kind)
210
210
211 # This is a closure to capture the magic_kind. We could also use a class,
211 # This is a closure to capture the magic_kind. We could also use a class,
212 # but it's overkill for just that one bit of state.
212 # but it's overkill for just that one bit of state.
213 def magic_deco(arg):
213 def magic_deco(arg):
214 # Find get_ipython() in the caller's namespace
214 # Find get_ipython() in the caller's namespace
215 caller = sys._getframe(1)
215 caller = sys._getframe(1)
216 for ns in ['f_locals', 'f_globals', 'f_builtins']:
216 for ns in ['f_locals', 'f_globals', 'f_builtins']:
217 get_ipython = getattr(caller, ns).get('get_ipython')
217 get_ipython = getattr(caller, ns).get('get_ipython')
218 if get_ipython is not None:
218 if get_ipython is not None:
219 break
219 break
220 else:
220 else:
221 raise NameError('Decorator can only run in context where '
221 raise NameError('Decorator can only run in context where '
222 '`get_ipython` exists')
222 '`get_ipython` exists')
223
223
224 ip = get_ipython()
224 ip = get_ipython()
225
225
226 if callable(arg):
226 if callable(arg):
227 # "Naked" decorator call (just @foo, no args)
227 # "Naked" decorator call (just @foo, no args)
228 func = arg
228 func = arg
229 name = func.__name__
229 name = func.__name__
230 ip.register_magic_function(func, magic_kind, name)
230 ip.register_magic_function(func, magic_kind, name)
231 retval = arg
231 retval = arg
232 elif isinstance(arg, str):
232 elif isinstance(arg, str):
233 # Decorator called with arguments (@foo('bar'))
233 # Decorator called with arguments (@foo('bar'))
234 name = arg
234 name = arg
235 def mark(func, *a, **kw):
235 def mark(func, *a, **kw):
236 ip.register_magic_function(func, magic_kind, name)
236 ip.register_magic_function(func, magic_kind, name)
237 return func
237 return func
238 retval = mark
238 retval = mark
239 else:
239 else:
240 raise TypeError("Decorator can only be called with "
240 raise TypeError("Decorator can only be called with "
241 "string or function")
241 "string or function")
242 return retval
242 return retval
243
243
244 # Ensure the resulting decorator has a usable docstring
244 # Ensure the resulting decorator has a usable docstring
245 ds = _docstring_template.format('function', magic_kind)
245 ds = _docstring_template.format('function', magic_kind)
246
246
247 ds += dedent("""
247 ds += dedent("""
248 Note: this decorator can only be used in a context where IPython is already
248 Note: this decorator can only be used in a context where IPython is already
249 active, so that the `get_ipython()` call succeeds. You can therefore use
249 active, so that the `get_ipython()` call succeeds. You can therefore use
250 it in your startup files loaded after IPython initializes, but *not* in the
250 it in your startup files loaded after IPython initializes, but *not* in the
251 IPython configuration file itself, which is executed before IPython is
251 IPython configuration file itself, which is executed before IPython is
252 fully up and running. Any file located in the `startup` subdirectory of
252 fully up and running. Any file located in the `startup` subdirectory of
253 your configuration profile will be OK in this sense.
253 your configuration profile will be OK in this sense.
254 """)
254 """)
255
255
256 magic_deco.__doc__ = ds
256 magic_deco.__doc__ = ds
257 return magic_deco
257 return magic_deco
258
258
259
259
260 MAGIC_NO_VAR_EXPAND_ATTR = "_ipython_magic_no_var_expand"
260 MAGIC_NO_VAR_EXPAND_ATTR = "_ipython_magic_no_var_expand"
261 MAGIC_OUTPUT_CAN_BE_SILENCED = "_ipython_magic_output_can_be_silenced"
261 MAGIC_OUTPUT_CAN_BE_SILENCED = "_ipython_magic_output_can_be_silenced"
262
262
263
263
264 def no_var_expand(magic_func):
264 def no_var_expand(magic_func):
265 """Mark a magic function as not needing variable expansion
265 """Mark a magic function as not needing variable expansion
266
266
267 By default, IPython interprets `{a}` or `$a` in the line passed to magics
267 By default, IPython interprets `{a}` or `$a` in the line passed to magics
268 as variables that should be interpolated from the interactive namespace
268 as variables that should be interpolated from the interactive namespace
269 before passing the line to the magic function.
269 before passing the line to the magic function.
270 This is not always desirable, e.g. when the magic executes Python code
270 This is not always desirable, e.g. when the magic executes Python code
271 (%timeit, %time, etc.).
271 (%timeit, %time, etc.).
272 Decorate magics with `@no_var_expand` to opt-out of variable expansion.
272 Decorate magics with `@no_var_expand` to opt-out of variable expansion.
273
273
274 .. versionadded:: 7.3
274 .. versionadded:: 7.3
275 """
275 """
276 setattr(magic_func, MAGIC_NO_VAR_EXPAND_ATTR, True)
276 setattr(magic_func, MAGIC_NO_VAR_EXPAND_ATTR, True)
277 return magic_func
277 return magic_func
278
278
279
279
280 def output_can_be_silenced(magic_func):
280 def output_can_be_silenced(magic_func):
281 """Mark a magic function so its output may be silenced.
281 """Mark a magic function so its output may be silenced.
282
282
283 The output is silenced if the Python expression used as a parameter of
283 The output is silenced if the Python code used as a parameter of
284 the magic ends in a semicolon, not counting a Python comment that can
284 the magic ends in a semicolon, not counting a Python comment that can
285 follow it.
285 follow it.
286 """
286 """
287 setattr(magic_func, MAGIC_OUTPUT_CAN_BE_SILENCED, True)
287 setattr(magic_func, MAGIC_OUTPUT_CAN_BE_SILENCED, True)
288 return magic_func
288 return magic_func
289
289
290 # Create the actual decorators for public use
290 # Create the actual decorators for public use
291
291
292 # These three are used to decorate methods in class definitions
292 # These three are used to decorate methods in class definitions
293 line_magic = _method_magic_marker('line')
293 line_magic = _method_magic_marker('line')
294 cell_magic = _method_magic_marker('cell')
294 cell_magic = _method_magic_marker('cell')
295 line_cell_magic = _method_magic_marker('line_cell')
295 line_cell_magic = _method_magic_marker('line_cell')
296
296
297 # These three decorate standalone functions and perform the decoration
297 # These three decorate standalone functions and perform the decoration
298 # immediately. They can only run where get_ipython() works
298 # immediately. They can only run where get_ipython() works
299 register_line_magic = _function_magic_marker('line')
299 register_line_magic = _function_magic_marker('line')
300 register_cell_magic = _function_magic_marker('cell')
300 register_cell_magic = _function_magic_marker('cell')
301 register_line_cell_magic = _function_magic_marker('line_cell')
301 register_line_cell_magic = _function_magic_marker('line_cell')
302
302
303 #-----------------------------------------------------------------------------
303 #-----------------------------------------------------------------------------
304 # Core Magic classes
304 # Core Magic classes
305 #-----------------------------------------------------------------------------
305 #-----------------------------------------------------------------------------
306
306
307 class MagicsManager(Configurable):
307 class MagicsManager(Configurable):
308 """Object that handles all magic-related functionality for IPython.
308 """Object that handles all magic-related functionality for IPython.
309 """
309 """
310 # Non-configurable class attributes
310 # Non-configurable class attributes
311
311
312 # A two-level dict, first keyed by magic type, then by magic function, and
312 # A two-level dict, first keyed by magic type, then by magic function, and
313 # holding the actual callable object as value. This is the dict used for
313 # holding the actual callable object as value. This is the dict used for
314 # magic function dispatch
314 # magic function dispatch
315 magics = Dict()
315 magics = Dict()
316 lazy_magics = Dict(
316 lazy_magics = Dict(
317 help="""
317 help="""
318 Mapping from magic names to modules to load.
318 Mapping from magic names to modules to load.
319
319
320 This can be used in IPython/IPykernel configuration to declare lazy magics
320 This can be used in IPython/IPykernel configuration to declare lazy magics
321 that will only be imported/registered on first use.
321 that will only be imported/registered on first use.
322
322
323 For example::
323 For example::
324
324
325 c.MagicsManager.lazy_magics = {
325 c.MagicsManager.lazy_magics = {
326 "my_magic": "slow.to.import",
326 "my_magic": "slow.to.import",
327 "my_other_magic": "also.slow",
327 "my_other_magic": "also.slow",
328 }
328 }
329
329
330 On first invocation of `%my_magic`, `%%my_magic`, `%%my_other_magic` or
330 On first invocation of `%my_magic`, `%%my_magic`, `%%my_other_magic` or
331 `%%my_other_magic`, the corresponding module will be loaded as an ipython
331 `%%my_other_magic`, the corresponding module will be loaded as an ipython
332 extensions as if you had previously done `%load_ext ipython`.
332 extensions as if you had previously done `%load_ext ipython`.
333
333
334 Magics names should be without percent(s) as magics can be both cell
334 Magics names should be without percent(s) as magics can be both cell
335 and line magics.
335 and line magics.
336
336
337 Lazy loading happen relatively late in execution process, and
337 Lazy loading happen relatively late in execution process, and
338 complex extensions that manipulate Python/IPython internal state or global state
338 complex extensions that manipulate Python/IPython internal state or global state
339 might not support lazy loading.
339 might not support lazy loading.
340 """
340 """
341 ).tag(
341 ).tag(
342 config=True,
342 config=True,
343 )
343 )
344
344
345 # A registry of the original objects that we've been given holding magics.
345 # A registry of the original objects that we've been given holding magics.
346 registry = Dict()
346 registry = Dict()
347
347
348 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
348 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
349
349
350 auto_magic = Bool(True, help=
350 auto_magic = Bool(True, help=
351 "Automatically call line magics without requiring explicit % prefix"
351 "Automatically call line magics without requiring explicit % prefix"
352 ).tag(config=True)
352 ).tag(config=True)
353 @observe('auto_magic')
353 @observe('auto_magic')
354 def _auto_magic_changed(self, change):
354 def _auto_magic_changed(self, change):
355 self.shell.automagic = change['new']
355 self.shell.automagic = change['new']
356
356
357 _auto_status = [
357 _auto_status = [
358 'Automagic is OFF, % prefix IS needed for line magics.',
358 'Automagic is OFF, % prefix IS needed for line magics.',
359 'Automagic is ON, % prefix IS NOT needed for line magics.']
359 'Automagic is ON, % prefix IS NOT needed for line magics.']
360
360
361 user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True)
361 user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True)
362
362
363 def __init__(self, shell=None, config=None, user_magics=None, **traits):
363 def __init__(self, shell=None, config=None, user_magics=None, **traits):
364
364
365 super(MagicsManager, self).__init__(shell=shell, config=config,
365 super(MagicsManager, self).__init__(shell=shell, config=config,
366 user_magics=user_magics, **traits)
366 user_magics=user_magics, **traits)
367 self.magics = dict(line={}, cell={})
367 self.magics = dict(line={}, cell={})
368 # Let's add the user_magics to the registry for uniformity, so *all*
368 # Let's add the user_magics to the registry for uniformity, so *all*
369 # registered magic containers can be found there.
369 # registered magic containers can be found there.
370 self.registry[user_magics.__class__.__name__] = user_magics
370 self.registry[user_magics.__class__.__name__] = user_magics
371
371
372 def auto_status(self):
372 def auto_status(self):
373 """Return descriptive string with automagic status."""
373 """Return descriptive string with automagic status."""
374 return self._auto_status[self.auto_magic]
374 return self._auto_status[self.auto_magic]
375
375
376 def lsmagic(self):
376 def lsmagic(self):
377 """Return a dict of currently available magic functions.
377 """Return a dict of currently available magic functions.
378
378
379 The return dict has the keys 'line' and 'cell', corresponding to the
379 The return dict has the keys 'line' and 'cell', corresponding to the
380 two types of magics we support. Each value is a list of names.
380 two types of magics we support. Each value is a list of names.
381 """
381 """
382 return self.magics
382 return self.magics
383
383
384 def lsmagic_docs(self, brief=False, missing=''):
384 def lsmagic_docs(self, brief=False, missing=''):
385 """Return dict of documentation of magic functions.
385 """Return dict of documentation of magic functions.
386
386
387 The return dict has the keys 'line' and 'cell', corresponding to the
387 The return dict has the keys 'line' and 'cell', corresponding to the
388 two types of magics we support. Each value is a dict keyed by magic
388 two types of magics we support. Each value is a dict keyed by magic
389 name whose value is the function docstring. If a docstring is
389 name whose value is the function docstring. If a docstring is
390 unavailable, the value of `missing` is used instead.
390 unavailable, the value of `missing` is used instead.
391
391
392 If brief is True, only the first line of each docstring will be returned.
392 If brief is True, only the first line of each docstring will be returned.
393 """
393 """
394 docs = {}
394 docs = {}
395 for m_type in self.magics:
395 for m_type in self.magics:
396 m_docs = {}
396 m_docs = {}
397 for m_name, m_func in self.magics[m_type].items():
397 for m_name, m_func in self.magics[m_type].items():
398 if m_func.__doc__:
398 if m_func.__doc__:
399 if brief:
399 if brief:
400 m_docs[m_name] = m_func.__doc__.split('\n', 1)[0]
400 m_docs[m_name] = m_func.__doc__.split('\n', 1)[0]
401 else:
401 else:
402 m_docs[m_name] = m_func.__doc__.rstrip()
402 m_docs[m_name] = m_func.__doc__.rstrip()
403 else:
403 else:
404 m_docs[m_name] = missing
404 m_docs[m_name] = missing
405 docs[m_type] = m_docs
405 docs[m_type] = m_docs
406 return docs
406 return docs
407
407
408 def register_lazy(self, name: str, fully_qualified_name: str):
408 def register_lazy(self, name: str, fully_qualified_name: str):
409 """
409 """
410 Lazily register a magic via an extension.
410 Lazily register a magic via an extension.
411
411
412
412
413 Parameters
413 Parameters
414 ----------
414 ----------
415 name : str
415 name : str
416 Name of the magic you wish to register.
416 Name of the magic you wish to register.
417 fully_qualified_name :
417 fully_qualified_name :
418 Fully qualified name of the module/submodule that should be loaded
418 Fully qualified name of the module/submodule that should be loaded
419 as an extensions when the magic is first called.
419 as an extensions when the magic is first called.
420 It is assumed that loading this extensions will register the given
420 It is assumed that loading this extensions will register the given
421 magic.
421 magic.
422 """
422 """
423
423
424 self.lazy_magics[name] = fully_qualified_name
424 self.lazy_magics[name] = fully_qualified_name
425
425
426 def register(self, *magic_objects):
426 def register(self, *magic_objects):
427 """Register one or more instances of Magics.
427 """Register one or more instances of Magics.
428
428
429 Take one or more classes or instances of classes that subclass the main
429 Take one or more classes or instances of classes that subclass the main
430 `core.Magic` class, and register them with IPython to use the magic
430 `core.Magic` class, and register them with IPython to use the magic
431 functions they provide. The registration process will then ensure that
431 functions they provide. The registration process will then ensure that
432 any methods that have decorated to provide line and/or cell magics will
432 any methods that have decorated to provide line and/or cell magics will
433 be recognized with the `%x`/`%%x` syntax as a line/cell magic
433 be recognized with the `%x`/`%%x` syntax as a line/cell magic
434 respectively.
434 respectively.
435
435
436 If classes are given, they will be instantiated with the default
436 If classes are given, they will be instantiated with the default
437 constructor. If your classes need a custom constructor, you should
437 constructor. If your classes need a custom constructor, you should
438 instanitate them first and pass the instance.
438 instanitate them first and pass the instance.
439
439
440 The provided arguments can be an arbitrary mix of classes and instances.
440 The provided arguments can be an arbitrary mix of classes and instances.
441
441
442 Parameters
442 Parameters
443 ----------
443 ----------
444 *magic_objects : one or more classes or instances
444 *magic_objects : one or more classes or instances
445 """
445 """
446 # Start by validating them to ensure they have all had their magic
446 # Start by validating them to ensure they have all had their magic
447 # methods registered at the instance level
447 # methods registered at the instance level
448 for m in magic_objects:
448 for m in magic_objects:
449 if not m.registered:
449 if not m.registered:
450 raise ValueError("Class of magics %r was constructed without "
450 raise ValueError("Class of magics %r was constructed without "
451 "the @register_magics class decorator")
451 "the @register_magics class decorator")
452 if isinstance(m, type):
452 if isinstance(m, type):
453 # If we're given an uninstantiated class
453 # If we're given an uninstantiated class
454 m = m(shell=self.shell)
454 m = m(shell=self.shell)
455
455
456 # Now that we have an instance, we can register it and update the
456 # Now that we have an instance, we can register it and update the
457 # table of callables
457 # table of callables
458 self.registry[m.__class__.__name__] = m
458 self.registry[m.__class__.__name__] = m
459 for mtype in magic_kinds:
459 for mtype in magic_kinds:
460 self.magics[mtype].update(m.magics[mtype])
460 self.magics[mtype].update(m.magics[mtype])
461
461
462 def register_function(self, func, magic_kind='line', magic_name=None):
462 def register_function(self, func, magic_kind='line', magic_name=None):
463 """Expose a standalone function as magic function for IPython.
463 """Expose a standalone function as magic function for IPython.
464
464
465 This will create an IPython magic (line, cell or both) from a
465 This will create an IPython magic (line, cell or both) from a
466 standalone function. The functions should have the following
466 standalone function. The functions should have the following
467 signatures:
467 signatures:
468
468
469 * For line magics: `def f(line)`
469 * For line magics: `def f(line)`
470 * For cell magics: `def f(line, cell)`
470 * For cell magics: `def f(line, cell)`
471 * For a function that does both: `def f(line, cell=None)`
471 * For a function that does both: `def f(line, cell=None)`
472
472
473 In the latter case, the function will be called with `cell==None` when
473 In the latter case, the function will be called with `cell==None` when
474 invoked as `%f`, and with cell as a string when invoked as `%%f`.
474 invoked as `%f`, and with cell as a string when invoked as `%%f`.
475
475
476 Parameters
476 Parameters
477 ----------
477 ----------
478 func : callable
478 func : callable
479 Function to be registered as a magic.
479 Function to be registered as a magic.
480 magic_kind : str
480 magic_kind : str
481 Kind of magic, one of 'line', 'cell' or 'line_cell'
481 Kind of magic, one of 'line', 'cell' or 'line_cell'
482 magic_name : optional str
482 magic_name : optional str
483 If given, the name the magic will have in the IPython namespace. By
483 If given, the name the magic will have in the IPython namespace. By
484 default, the name of the function itself is used.
484 default, the name of the function itself is used.
485 """
485 """
486
486
487 # Create the new method in the user_magics and register it in the
487 # Create the new method in the user_magics and register it in the
488 # global table
488 # global table
489 validate_type(magic_kind)
489 validate_type(magic_kind)
490 magic_name = func.__name__ if magic_name is None else magic_name
490 magic_name = func.__name__ if magic_name is None else magic_name
491 setattr(self.user_magics, magic_name, func)
491 setattr(self.user_magics, magic_name, func)
492 record_magic(self.magics, magic_kind, magic_name, func)
492 record_magic(self.magics, magic_kind, magic_name, func)
493
493
494 def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None):
494 def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None):
495 """Register an alias to a magic function.
495 """Register an alias to a magic function.
496
496
497 The alias is an instance of :class:`MagicAlias`, which holds the
497 The alias is an instance of :class:`MagicAlias`, which holds the
498 name and kind of the magic it should call. Binding is done at
498 name and kind of the magic it should call. Binding is done at
499 call time, so if the underlying magic function is changed the alias
499 call time, so if the underlying magic function is changed the alias
500 will call the new function.
500 will call the new function.
501
501
502 Parameters
502 Parameters
503 ----------
503 ----------
504 alias_name : str
504 alias_name : str
505 The name of the magic to be registered.
505 The name of the magic to be registered.
506 magic_name : str
506 magic_name : str
507 The name of an existing magic.
507 The name of an existing magic.
508 magic_kind : str
508 magic_kind : str
509 Kind of magic, one of 'line' or 'cell'
509 Kind of magic, one of 'line' or 'cell'
510 """
510 """
511
511
512 # `validate_type` is too permissive, as it allows 'line_cell'
512 # `validate_type` is too permissive, as it allows 'line_cell'
513 # which we do not handle.
513 # which we do not handle.
514 if magic_kind not in magic_kinds:
514 if magic_kind not in magic_kinds:
515 raise ValueError('magic_kind must be one of %s, %s given' %
515 raise ValueError('magic_kind must be one of %s, %s given' %
516 magic_kinds, magic_kind)
516 magic_kinds, magic_kind)
517
517
518 alias = MagicAlias(self.shell, magic_name, magic_kind, magic_params)
518 alias = MagicAlias(self.shell, magic_name, magic_kind, magic_params)
519 setattr(self.user_magics, alias_name, alias)
519 setattr(self.user_magics, alias_name, alias)
520 record_magic(self.magics, magic_kind, alias_name, alias)
520 record_magic(self.magics, magic_kind, alias_name, alias)
521
521
522 # Key base class that provides the central functionality for magics.
522 # Key base class that provides the central functionality for magics.
523
523
524
524
525 class Magics(Configurable):
525 class Magics(Configurable):
526 """Base class for implementing magic functions.
526 """Base class for implementing magic functions.
527
527
528 Shell functions which can be reached as %function_name. All magic
528 Shell functions which can be reached as %function_name. All magic
529 functions should accept a string, which they can parse for their own
529 functions should accept a string, which they can parse for their own
530 needs. This can make some functions easier to type, eg `%cd ../`
530 needs. This can make some functions easier to type, eg `%cd ../`
531 vs. `%cd("../")`
531 vs. `%cd("../")`
532
532
533 Classes providing magic functions need to subclass this class, and they
533 Classes providing magic functions need to subclass this class, and they
534 MUST:
534 MUST:
535
535
536 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
536 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
537 individual methods as magic functions, AND
537 individual methods as magic functions, AND
538
538
539 - Use the class decorator `@magics_class` to ensure that the magic
539 - Use the class decorator `@magics_class` to ensure that the magic
540 methods are properly registered at the instance level upon instance
540 methods are properly registered at the instance level upon instance
541 initialization.
541 initialization.
542
542
543 See :mod:`magic_functions` for examples of actual implementation classes.
543 See :mod:`magic_functions` for examples of actual implementation classes.
544 """
544 """
545 # Dict holding all command-line options for each magic.
545 # Dict holding all command-line options for each magic.
546 options_table = None
546 options_table = None
547 # Dict for the mapping of magic names to methods, set by class decorator
547 # Dict for the mapping of magic names to methods, set by class decorator
548 magics = None
548 magics = None
549 # Flag to check that the class decorator was properly applied
549 # Flag to check that the class decorator was properly applied
550 registered = False
550 registered = False
551 # Instance of IPython shell
551 # Instance of IPython shell
552 shell = None
552 shell = None
553
553
554 def __init__(self, shell=None, **kwargs):
554 def __init__(self, shell=None, **kwargs):
555 if not(self.__class__.registered):
555 if not(self.__class__.registered):
556 raise ValueError('Magics subclass without registration - '
556 raise ValueError('Magics subclass without registration - '
557 'did you forget to apply @magics_class?')
557 'did you forget to apply @magics_class?')
558 if shell is not None:
558 if shell is not None:
559 if hasattr(shell, 'configurables'):
559 if hasattr(shell, 'configurables'):
560 shell.configurables.append(self)
560 shell.configurables.append(self)
561 if hasattr(shell, 'config'):
561 if hasattr(shell, 'config'):
562 kwargs.setdefault('parent', shell)
562 kwargs.setdefault('parent', shell)
563
563
564 self.shell = shell
564 self.shell = shell
565 self.options_table = {}
565 self.options_table = {}
566 # The method decorators are run when the instance doesn't exist yet, so
566 # The method decorators are run when the instance doesn't exist yet, so
567 # they can only record the names of the methods they are supposed to
567 # they can only record the names of the methods they are supposed to
568 # grab. Only now, that the instance exists, can we create the proper
568 # grab. Only now, that the instance exists, can we create the proper
569 # mapping to bound methods. So we read the info off the original names
569 # mapping to bound methods. So we read the info off the original names
570 # table and replace each method name by the actual bound method.
570 # table and replace each method name by the actual bound method.
571 # But we mustn't clobber the *class* mapping, in case of multiple instances.
571 # But we mustn't clobber the *class* mapping, in case of multiple instances.
572 class_magics = self.magics
572 class_magics = self.magics
573 self.magics = {}
573 self.magics = {}
574 for mtype in magic_kinds:
574 for mtype in magic_kinds:
575 tab = self.magics[mtype] = {}
575 tab = self.magics[mtype] = {}
576 cls_tab = class_magics[mtype]
576 cls_tab = class_magics[mtype]
577 for magic_name, meth_name in cls_tab.items():
577 for magic_name, meth_name in cls_tab.items():
578 if isinstance(meth_name, str):
578 if isinstance(meth_name, str):
579 # it's a method name, grab it
579 # it's a method name, grab it
580 tab[magic_name] = getattr(self, meth_name)
580 tab[magic_name] = getattr(self, meth_name)
581 else:
581 else:
582 # it's the real thing
582 # it's the real thing
583 tab[magic_name] = meth_name
583 tab[magic_name] = meth_name
584 # Configurable **needs** to be initiated at the end or the config
584 # Configurable **needs** to be initiated at the end or the config
585 # magics get screwed up.
585 # magics get screwed up.
586 super(Magics, self).__init__(**kwargs)
586 super(Magics, self).__init__(**kwargs)
587
587
588 def arg_err(self,func):
588 def arg_err(self,func):
589 """Print docstring if incorrect arguments were passed"""
589 """Print docstring if incorrect arguments were passed"""
590 print('Error in arguments:')
590 print('Error in arguments:')
591 print(oinspect.getdoc(func))
591 print(oinspect.getdoc(func))
592
592
593 def format_latex(self, strng):
593 def format_latex(self, strng):
594 """Format a string for latex inclusion."""
594 """Format a string for latex inclusion."""
595
595
596 # Characters that need to be escaped for latex:
596 # Characters that need to be escaped for latex:
597 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
597 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
598 # Magic command names as headers:
598 # Magic command names as headers:
599 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
599 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
600 re.MULTILINE)
600 re.MULTILINE)
601 # Magic commands
601 # Magic commands
602 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
602 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
603 re.MULTILINE)
603 re.MULTILINE)
604 # Paragraph continue
604 # Paragraph continue
605 par_re = re.compile(r'\\$',re.MULTILINE)
605 par_re = re.compile(r'\\$',re.MULTILINE)
606
606
607 # The "\n" symbol
607 # The "\n" symbol
608 newline_re = re.compile(r'\\n')
608 newline_re = re.compile(r'\\n')
609
609
610 # Now build the string for output:
610 # Now build the string for output:
611 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
611 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
612 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
612 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
613 strng)
613 strng)
614 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
614 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
615 strng = par_re.sub(r'\\\\',strng)
615 strng = par_re.sub(r'\\\\',strng)
616 strng = escape_re.sub(r'\\\1',strng)
616 strng = escape_re.sub(r'\\\1',strng)
617 strng = newline_re.sub(r'\\textbackslash{}n',strng)
617 strng = newline_re.sub(r'\\textbackslash{}n',strng)
618 return strng
618 return strng
619
619
620 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
620 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
621 """Parse options passed to an argument string.
621 """Parse options passed to an argument string.
622
622
623 The interface is similar to that of :func:`getopt.getopt`, but it
623 The interface is similar to that of :func:`getopt.getopt`, but it
624 returns a :class:`~IPython.utils.struct.Struct` with the options as keys
624 returns a :class:`~IPython.utils.struct.Struct` with the options as keys
625 and the stripped argument string still as a string.
625 and the stripped argument string still as a string.
626
626
627 arg_str is quoted as a true sys.argv vector by using shlex.split.
627 arg_str is quoted as a true sys.argv vector by using shlex.split.
628 This allows us to easily expand variables, glob files, quote
628 This allows us to easily expand variables, glob files, quote
629 arguments, etc.
629 arguments, etc.
630
630
631 Parameters
631 Parameters
632 ----------
632 ----------
633 arg_str : str
633 arg_str : str
634 The arguments to parse.
634 The arguments to parse.
635 opt_str : str
635 opt_str : str
636 The options specification.
636 The options specification.
637 mode : str, default 'string'
637 mode : str, default 'string'
638 If given as 'list', the argument string is returned as a list (split
638 If given as 'list', the argument string is returned as a list (split
639 on whitespace) instead of a string.
639 on whitespace) instead of a string.
640 list_all : bool, default False
640 list_all : bool, default False
641 Put all option values in lists. Normally only options
641 Put all option values in lists. Normally only options
642 appearing more than once are put in a list.
642 appearing more than once are put in a list.
643 posix : bool, default True
643 posix : bool, default True
644 Whether to split the input line in POSIX mode or not, as per the
644 Whether to split the input line in POSIX mode or not, as per the
645 conventions outlined in the :mod:`shlex` module from the standard
645 conventions outlined in the :mod:`shlex` module from the standard
646 library.
646 library.
647 """
647 """
648
648
649 # inject default options at the beginning of the input line
649 # inject default options at the beginning of the input line
650 caller = sys._getframe(1).f_code.co_name
650 caller = sys._getframe(1).f_code.co_name
651 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
651 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
652
652
653 mode = kw.get('mode','string')
653 mode = kw.get('mode','string')
654 if mode not in ['string','list']:
654 if mode not in ['string','list']:
655 raise ValueError('incorrect mode given: %s' % mode)
655 raise ValueError('incorrect mode given: %s' % mode)
656 # Get options
656 # Get options
657 list_all = kw.get('list_all',0)
657 list_all = kw.get('list_all',0)
658 posix = kw.get('posix', os.name == 'posix')
658 posix = kw.get('posix', os.name == 'posix')
659 strict = kw.get('strict', True)
659 strict = kw.get('strict', True)
660
660
661 preserve_non_opts = kw.get("preserve_non_opts", False)
661 preserve_non_opts = kw.get("preserve_non_opts", False)
662 remainder_arg_str = arg_str
662 remainder_arg_str = arg_str
663
663
664 # Check if we have more than one argument to warrant extra processing:
664 # Check if we have more than one argument to warrant extra processing:
665 odict = {} # Dictionary with options
665 odict = {} # Dictionary with options
666 args = arg_str.split()
666 args = arg_str.split()
667 if len(args) >= 1:
667 if len(args) >= 1:
668 # If the list of inputs only has 0 or 1 thing in it, there's no
668 # If the list of inputs only has 0 or 1 thing in it, there's no
669 # need to look for options
669 # need to look for options
670 argv = arg_split(arg_str, posix, strict)
670 argv = arg_split(arg_str, posix, strict)
671 # Do regular option processing
671 # Do regular option processing
672 try:
672 try:
673 opts,args = getopt(argv, opt_str, long_opts)
673 opts,args = getopt(argv, opt_str, long_opts)
674 except GetoptError as e:
674 except GetoptError as e:
675 raise UsageError(
675 raise UsageError(
676 '%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts))
676 '%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts))
677 ) from e
677 ) from e
678 for o, a in opts:
678 for o, a in opts:
679 if mode == "string" and preserve_non_opts:
679 if mode == "string" and preserve_non_opts:
680 # remove option-parts from the original args-string and preserve remaining-part.
680 # remove option-parts from the original args-string and preserve remaining-part.
681 # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are
681 # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are
682 # returned in the original order.
682 # returned in the original order.
683 remainder_arg_str = remainder_arg_str.replace(o, "", 1).replace(
683 remainder_arg_str = remainder_arg_str.replace(o, "", 1).replace(
684 a, "", 1
684 a, "", 1
685 )
685 )
686 if o.startswith("--"):
686 if o.startswith("--"):
687 o = o[2:]
687 o = o[2:]
688 else:
688 else:
689 o = o[1:]
689 o = o[1:]
690 try:
690 try:
691 odict[o].append(a)
691 odict[o].append(a)
692 except AttributeError:
692 except AttributeError:
693 odict[o] = [odict[o],a]
693 odict[o] = [odict[o],a]
694 except KeyError:
694 except KeyError:
695 if list_all:
695 if list_all:
696 odict[o] = [a]
696 odict[o] = [a]
697 else:
697 else:
698 odict[o] = a
698 odict[o] = a
699
699
700 # Prepare opts,args for return
700 # Prepare opts,args for return
701 opts = Struct(odict)
701 opts = Struct(odict)
702 if mode == 'string':
702 if mode == 'string':
703 if preserve_non_opts:
703 if preserve_non_opts:
704 args = remainder_arg_str.lstrip()
704 args = remainder_arg_str.lstrip()
705 else:
705 else:
706 args = " ".join(args)
706 args = " ".join(args)
707
707
708 return opts,args
708 return opts,args
709
709
710 def default_option(self, fn, optstr):
710 def default_option(self, fn, optstr):
711 """Make an entry in the options_table for fn, with value optstr"""
711 """Make an entry in the options_table for fn, with value optstr"""
712
712
713 if fn not in self.lsmagic():
713 if fn not in self.lsmagic():
714 error("%s is not a magic function" % fn)
714 error("%s is not a magic function" % fn)
715 self.options_table[fn] = optstr
715 self.options_table[fn] = optstr
716
716
717
717
718 class MagicAlias(object):
718 class MagicAlias(object):
719 """An alias to another magic function.
719 """An alias to another magic function.
720
720
721 An alias is determined by its magic name and magic kind. Lookup
721 An alias is determined by its magic name and magic kind. Lookup
722 is done at call time, so if the underlying magic changes the alias
722 is done at call time, so if the underlying magic changes the alias
723 will call the new function.
723 will call the new function.
724
724
725 Use the :meth:`MagicsManager.register_alias` method or the
725 Use the :meth:`MagicsManager.register_alias` method or the
726 `%alias_magic` magic function to create and register a new alias.
726 `%alias_magic` magic function to create and register a new alias.
727 """
727 """
728 def __init__(self, shell, magic_name, magic_kind, magic_params=None):
728 def __init__(self, shell, magic_name, magic_kind, magic_params=None):
729 self.shell = shell
729 self.shell = shell
730 self.magic_name = magic_name
730 self.magic_name = magic_name
731 self.magic_params = magic_params
731 self.magic_params = magic_params
732 self.magic_kind = magic_kind
732 self.magic_kind = magic_kind
733
733
734 self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
734 self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
735 self.__doc__ = "Alias for `%s`." % self.pretty_target
735 self.__doc__ = "Alias for `%s`." % self.pretty_target
736
736
737 self._in_call = False
737 self._in_call = False
738
738
739 def __call__(self, *args, **kwargs):
739 def __call__(self, *args, **kwargs):
740 """Call the magic alias."""
740 """Call the magic alias."""
741 fn = self.shell.find_magic(self.magic_name, self.magic_kind)
741 fn = self.shell.find_magic(self.magic_name, self.magic_kind)
742 if fn is None:
742 if fn is None:
743 raise UsageError("Magic `%s` not found." % self.pretty_target)
743 raise UsageError("Magic `%s` not found." % self.pretty_target)
744
744
745 # Protect against infinite recursion.
745 # Protect against infinite recursion.
746 if self._in_call:
746 if self._in_call:
747 raise UsageError("Infinite recursion detected; "
747 raise UsageError("Infinite recursion detected; "
748 "magic aliases cannot call themselves.")
748 "magic aliases cannot call themselves.")
749 self._in_call = True
749 self._in_call = True
750 try:
750 try:
751 if self.magic_params:
751 if self.magic_params:
752 args_list = list(args)
752 args_list = list(args)
753 args_list[0] = self.magic_params + " " + args[0]
753 args_list[0] = self.magic_params + " " + args[0]
754 args = tuple(args_list)
754 args = tuple(args_list)
755 return fn(*args, **kwargs)
755 return fn(*args, **kwargs)
756 finally:
756 finally:
757 self._in_call = False
757 self._in_call = False
@@ -1,199 +1,212 b''
1 .. _defining_magics:
1 .. _defining_magics:
2
2
3 Defining custom magics
3 Defining custom magics
4 ======================
4 ======================
5
5
6 There are two main ways to define your own magic functions: from standalone
6 There are two main ways to define your own magic functions: from standalone
7 functions and by inheriting from a base class provided by IPython:
7 functions and by inheriting from a base class provided by IPython:
8 :class:`IPython.core.magic.Magics`. Below we show code you can place in a file
8 :class:`IPython.core.magic.Magics`. Below we show code you can place in a file
9 that you load from your configuration, such as any file in the ``startup``
9 that you load from your configuration, such as any file in the ``startup``
10 subdirectory of your default IPython profile.
10 subdirectory of your default IPython profile.
11
11
12 First, let us see the simplest case. The following shows how to create a line
12 First, let us see the simplest case. The following shows how to create a line
13 magic, a cell one and one that works in both modes, using just plain functions:
13 magic, a cell one and one that works in both modes, using just plain functions:
14
14
15 .. sourcecode:: python
15 .. sourcecode:: python
16
16
17 from IPython.core.magic import (register_line_magic, register_cell_magic,
17 from IPython.core.magic import (register_line_magic, register_cell_magic,
18 register_line_cell_magic)
18 register_line_cell_magic)
19
19
20 @register_line_magic
20 @register_line_magic
21 def lmagic(line):
21 def lmagic(line):
22 "my line magic"
22 "my line magic"
23 return line
23 return line
24
24
25 @register_cell_magic
25 @register_cell_magic
26 def cmagic(line, cell):
26 def cmagic(line, cell):
27 "my cell magic"
27 "my cell magic"
28 return line, cell
28 return line, cell
29
29
30 @register_line_cell_magic
30 @register_line_cell_magic
31 def lcmagic(line, cell=None):
31 def lcmagic(line, cell=None):
32 "Magic that works both as %lcmagic and as %%lcmagic"
32 "Magic that works both as %lcmagic and as %%lcmagic"
33 if cell is None:
33 if cell is None:
34 print("Called as line magic")
34 print("Called as line magic")
35 return line
35 return line
36 else:
36 else:
37 print("Called as cell magic")
37 print("Called as cell magic")
38 return line, cell
38 return line, cell
39
39
40 # In an interactive session, we need to delete these to avoid
40 # In an interactive session, we need to delete these to avoid
41 # name conflicts for automagic to work on line magics.
41 # name conflicts for automagic to work on line magics.
42 del lmagic, lcmagic
42 del lmagic, lcmagic
43
43
44
44
45 You can also create magics of all three kinds by inheriting from the
45 You can also create magics of all three kinds by inheriting from the
46 :class:`IPython.core.magic.Magics` class. This lets you create magics that can
46 :class:`IPython.core.magic.Magics` class. This lets you create magics that can
47 potentially hold state in between calls, and that have full access to the main
47 potentially hold state in between calls, and that have full access to the main
48 IPython object:
48 IPython object:
49
49
50 .. sourcecode:: python
50 .. sourcecode:: python
51
51
52 # This code can be put in any Python module, it does not require IPython
52 # This code can be put in any Python module, it does not require IPython
53 # itself to be running already. It only creates the magics subclass but
53 # itself to be running already. It only creates the magics subclass but
54 # doesn't instantiate it yet.
54 # doesn't instantiate it yet.
55 from __future__ import print_function
55 from __future__ import print_function
56 from IPython.core.magic import (Magics, magics_class, line_magic,
56 from IPython.core.magic import (Magics, magics_class, line_magic,
57 cell_magic, line_cell_magic)
57 cell_magic, line_cell_magic)
58
58
59 # The class MUST call this class decorator at creation time
59 # The class MUST call this class decorator at creation time
60 @magics_class
60 @magics_class
61 class MyMagics(Magics):
61 class MyMagics(Magics):
62
62
63 @line_magic
63 @line_magic
64 def lmagic(self, line):
64 def lmagic(self, line):
65 "my line magic"
65 "my line magic"
66 print("Full access to the main IPython object:", self.shell)
66 print("Full access to the main IPython object:", self.shell)
67 print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
67 print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
68 return line
68 return line
69
69
70 @cell_magic
70 @cell_magic
71 def cmagic(self, line, cell):
71 def cmagic(self, line, cell):
72 "my cell magic"
72 "my cell magic"
73 return line, cell
73 return line, cell
74
74
75 @line_cell_magic
75 @line_cell_magic
76 def lcmagic(self, line, cell=None):
76 def lcmagic(self, line, cell=None):
77 "Magic that works both as %lcmagic and as %%lcmagic"
77 "Magic that works both as %lcmagic and as %%lcmagic"
78 if cell is None:
78 if cell is None:
79 print("Called as line magic")
79 print("Called as line magic")
80 return line
80 return line
81 else:
81 else:
82 print("Called as cell magic")
82 print("Called as cell magic")
83 return line, cell
83 return line, cell
84
84
85
85
86 # In order to actually use these magics, you must register them with a
86 # In order to actually use these magics, you must register them with a
87 # running IPython.
87 # running IPython.
88
88
89 def load_ipython_extension(ipython):
89 def load_ipython_extension(ipython):
90 """
90 """
91 Any module file that define a function named `load_ipython_extension`
91 Any module file that define a function named `load_ipython_extension`
92 can be loaded via `%load_ext module.path` or be configured to be
92 can be loaded via `%load_ext module.path` or be configured to be
93 autoloaded by IPython at startup time.
93 autoloaded by IPython at startup time.
94 """
94 """
95 # You can register the class itself without instantiating it. IPython will
95 # You can register the class itself without instantiating it. IPython will
96 # call the default constructor on it.
96 # call the default constructor on it.
97 ipython.register_magics(MyMagics)
97 ipython.register_magics(MyMagics)
98
98
99 If you want to create a class with a different constructor that holds
99 If you want to create a class with a different constructor that holds
100 additional state, then you should always call the parent constructor and
100 additional state, then you should always call the parent constructor and
101 instantiate the class yourself before registration:
101 instantiate the class yourself before registration:
102
102
103 .. sourcecode:: python
103 .. sourcecode:: python
104
104
105 @magics_class
105 @magics_class
106 class StatefulMagics(Magics):
106 class StatefulMagics(Magics):
107 "Magics that hold additional state"
107 "Magics that hold additional state"
108
108
109 def __init__(self, shell, data):
109 def __init__(self, shell, data):
110 # You must call the parent constructor
110 # You must call the parent constructor
111 super(StatefulMagics, self).__init__(shell)
111 super(StatefulMagics, self).__init__(shell)
112 self.data = data
112 self.data = data
113
113
114 # etc...
114 # etc...
115
115
116 def load_ipython_extension(ipython):
116 def load_ipython_extension(ipython):
117 """
117 """
118 Any module file that define a function named `load_ipython_extension`
118 Any module file that define a function named `load_ipython_extension`
119 can be loaded via `%load_ext module.path` or be configured to be
119 can be loaded via `%load_ext module.path` or be configured to be
120 autoloaded by IPython at startup time.
120 autoloaded by IPython at startup time.
121 """
121 """
122 # This class must then be registered with a manually created instance,
122 # This class must then be registered with a manually created instance,
123 # since its constructor has different arguments from the default:
123 # since its constructor has different arguments from the default:
124 magics = StatefulMagics(ipython, some_data)
124 magics = StatefulMagics(ipython, some_data)
125 ipython.register_magics(magics)
125 ipython.register_magics(magics)
126
126
127
127
128 .. note::
128 .. note::
129
129
130 In early IPython versions 0.12 and before the line magics were
130 In early IPython versions 0.12 and before the line magics were
131 created using a :func:`define_magic` API function. This API has been
131 created using a :func:`define_magic` API function. This API has been
132 replaced with the above in IPython 0.13 and then completely removed
132 replaced with the above in IPython 0.13 and then completely removed
133 in IPython 5. Maintainers of IPython extensions that still use the
133 in IPython 5. Maintainers of IPython extensions that still use the
134 :func:`define_magic` function are advised to adjust their code
134 :func:`define_magic` function are advised to adjust their code
135 for the current API.
135 for the current API.
136
136
137
137
138 Accessing user namespace and local scope
138 Accessing user namespace and local scope
139 ========================================
139 ========================================
140
140
141 When creating line magics, you may need to access surrounding scope to get user
141 When creating line magics, you may need to access surrounding scope to get user
142 variables (e.g when called inside functions). IPython provide the
142 variables (e.g when called inside functions). IPython provides the
143 ``@needs_local_scope`` decorator that can be imported from
143 ``@needs_local_scope`` decorator that can be imported from
144 ``IPython.core.magics``. When decorated with ``@needs_local_scope`` a magic will
144 ``IPython.core.magics``. When decorated with ``@needs_local_scope`` a magic will
145 be passed ``local_ns`` as an argument. As a convenience ``@needs_local_scope``
145 be passed ``local_ns`` as an argument. As a convenience ``@needs_local_scope``
146 can also be applied to cell magics even if cell magics cannot appear at local
146 can also be applied to cell magics even if cell magics cannot appear at local
147 scope context.
147 scope context.
148
148
149 Silencing the magic output
150 ==========================
151
152 Sometimes it may be useful to define a magic that can be silenced the same way
153 that non-magic expressions can, i.e., by appending a semicolon at the end of the Python
154 code to be executed. That can be achieved by decorating the magic function with
155 the decorator ``@output_can_be_silenced`` that can be imported from
156 ``IPython.core.magics``. When this decorator is used, IPython will parse the Python
157 code used by the magic and, if the last token is a ``;``, the output created by the
158 magic will not show up on the screen. If you want to see an example of this decorator
159 in action, take a look on the ``time`` magic defined in
160 ``IPython.core.magics.execution.py``.
161
149 Complete Example
162 Complete Example
150 ================
163 ================
151
164
152 Here is a full example of a magic package. You can distribute magics using
165 Here is a full example of a magic package. You can distribute magics using
153 setuptools, distutils, or any other distribution tools like `flit
166 setuptools, distutils, or any other distribution tools like `flit
154 <https://flit.readthedocs.io>`_ for pure Python packages.
167 <https://flit.readthedocs.io>`_ for pure Python packages.
155
168
156 When distributing magics as part of a package, recommended best practice is to
169 When distributing magics as part of a package, recommended best practice is to
157 execute the registration inside the `load_ipython_extension` as demonstrated in
170 execute the registration inside the `load_ipython_extension` as demonstrated in
158 the example below, instead of directly in the module (as in the initial example
171 the example below, instead of directly in the module (as in the initial example
159 with the ``@register_*`` decorators). This means a user will need to explicitly
172 with the ``@register_*`` decorators). This means a user will need to explicitly
160 choose to load your magic with ``%load_ext``. instead implicitly getting it when
173 choose to load your magic with ``%load_ext``. instead implicitly getting it when
161 importing the module. This is particularly relevant if loading your magic has
174 importing the module. This is particularly relevant if loading your magic has
162 side effects, if it is slow to load, or if it might override another magic with
175 side effects, if it is slow to load, or if it might override another magic with
163 the same name.
176 the same name.
164
177
165 .. sourcecode:: bash
178 .. sourcecode:: bash
166
179
167 .
180 .
168 β”œβ”€β”€ example_magic
181 β”œβ”€β”€ example_magic
169 β”‚Β Β  β”œβ”€β”€ __init__.py
182 β”‚Β Β  β”œβ”€β”€ __init__.py
170 β”‚Β Β  └── abracadabra.py
183 β”‚Β Β  └── abracadabra.py
171 └── setup.py
184 └── setup.py
172
185
173 .. sourcecode:: bash
186 .. sourcecode:: bash
174
187
175 $ cat example_magic/__init__.py
188 $ cat example_magic/__init__.py
176 """An example magic"""
189 """An example magic"""
177 __version__ = '0.0.1'
190 __version__ = '0.0.1'
178
191
179 from .abracadabra import Abracadabra
192 from .abracadabra import Abracadabra
180
193
181 def load_ipython_extension(ipython):
194 def load_ipython_extension(ipython):
182 ipython.register_magics(Abracadabra)
195 ipython.register_magics(Abracadabra)
183
196
184 .. sourcecode:: bash
197 .. sourcecode:: bash
185
198
186 $ cat example_magic/abracadabra.py
199 $ cat example_magic/abracadabra.py
187 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
200 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
188
201
189 @magics_class
202 @magics_class
190 class Abracadabra(Magics):
203 class Abracadabra(Magics):
191
204
192 @line_magic
205 @line_magic
193 def abra(self, line):
206 def abra(self, line):
194 return line
207 return line
195
208
196 @cell_magic
209 @cell_magic
197 def cadabra(self, line, cell):
210 def cadabra(self, line, cell):
198 return line, cell
211 return line, cell
199
212
General Comments 0
You need to be logged in to leave comments. Login now