##// END OF EJS Templates
Complete documenting magic module, as per review....
Fernando Perez -
Show More
@@ -1,527 +1,575 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 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Stdlib
17 # Stdlib
18 import os
18 import os
19 import re
19 import re
20 import sys
20 import sys
21 import types
21 import types
22 from getopt import getopt, GetoptError
22 from getopt import getopt, GetoptError
23
23
24 # Our own
24 # Our own
25 from IPython.config.configurable import Configurable
25 from IPython.config.configurable import Configurable
26 from IPython.core import oinspect
26 from IPython.core import oinspect
27 from IPython.core.error import UsageError
27 from IPython.core.error import UsageError
28 from IPython.core.prefilter import ESC_MAGIC
28 from IPython.core.prefilter import ESC_MAGIC
29 from IPython.external.decorator import decorator
29 from IPython.external.decorator import decorator
30 from IPython.utils.ipstruct import Struct
30 from IPython.utils.ipstruct import Struct
31 from IPython.utils.process import arg_split
31 from IPython.utils.process import arg_split
32 from IPython.utils.text import dedent
32 from IPython.utils.text import dedent
33 from IPython.utils.traitlets import Bool, Dict, Instance
33 from IPython.utils.traitlets import Bool, Dict, Instance
34 from IPython.utils.warn import error, warn
34 from IPython.utils.warn import error, warn
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 # Globals
37 # Globals
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 # A dict we'll use for each class that has magics, used as temporary storage to
40 # A dict we'll use for each class that has magics, used as temporary storage to
41 # pass information between the @line/cell_magic method decorators and the
41 # pass information between the @line/cell_magic method decorators and the
42 # @magics_class class decorator, because the method decorators have no
42 # @magics_class class decorator, because the method decorators have no
43 # access to the class when they run. See for more details:
43 # access to the class when they run. See for more details:
44 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
44 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
45
45
46 magics = dict(line={}, cell={})
46 magics = dict(line={}, cell={})
47
47
48 magic_kinds = ('line', 'cell')
48 magic_kinds = ('line', 'cell')
49 magic_spec = ('line', 'cell', 'line_cell')
49 magic_spec = ('line', 'cell', 'line_cell')
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Utility classes and functions
52 # Utility classes and functions
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54
54
55 class Bunch: pass
55 class Bunch: pass
56
56
57
57
58 def on_off(tag):
58 def on_off(tag):
59 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
59 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
60 return ['OFF','ON'][tag]
60 return ['OFF','ON'][tag]
61
61
62
62
63 def compress_dhist(dh):
63 def compress_dhist(dh):
64 """Compress a directory history into a new one with at most 20 entries.
64 """Compress a directory history into a new one with at most 20 entries.
65
65
66 Return a new list made from the first and last 10 elements of dhist after
66 Return a new list made from the first and last 10 elements of dhist after
67 removal of duplicates.
67 removal of duplicates.
68 """
68 """
69 head, tail = dh[:-10], dh[-10:]
69 head, tail = dh[:-10], dh[-10:]
70
70
71 newhead = []
71 newhead = []
72 done = set()
72 done = set()
73 for h in head:
73 for h in head:
74 if h in done:
74 if h in done:
75 continue
75 continue
76 newhead.append(h)
76 newhead.append(h)
77 done.add(h)
77 done.add(h)
78
78
79 return newhead + tail
79 return newhead + tail
80
80
81
81
82 def needs_local_scope(func):
82 def needs_local_scope(func):
83 """Decorator to mark magic functions which need to local scope to run."""
83 """Decorator to mark magic functions which need to local scope to run."""
84 func.needs_local_scope = True
84 func.needs_local_scope = True
85 return func
85 return func
86
86
87 #-----------------------------------------------------------------------------
87 #-----------------------------------------------------------------------------
88 # Class and method decorators for registering magics
88 # Class and method decorators for registering magics
89 #-----------------------------------------------------------------------------
89 #-----------------------------------------------------------------------------
90
90
91 def magics_class(cls):
91 def magics_class(cls):
92 """Class decorator for all subclasses of the main Magics class.
92 """Class decorator for all subclasses of the main Magics class.
93
93
94 Any class that subclasses Magics *must* also apply this decorator, to
94 Any class that subclasses Magics *must* also apply this decorator, to
95 ensure that all the methods that have been decorated as line/cell magics
95 ensure that all the methods that have been decorated as line/cell magics
96 get correctly registered in the class instance. This is necessary because
96 get correctly registered in the class instance. This is necessary because
97 when method decorators run, the class does not exist yet, so they
97 when method decorators run, the class does not exist yet, so they
98 temporarily store their information into a module global. Application of
98 temporarily store their information into a module global. Application of
99 this class decorator copies that global data to the class instance and
99 this class decorator copies that global data to the class instance and
100 clears the global.
100 clears the global.
101
101
102 Obviously, this mechanism is not thread-safe, which means that the
102 Obviously, this mechanism is not thread-safe, which means that the
103 *creation* of subclasses of Magic should only be done in a single-thread
103 *creation* of subclasses of Magic should only be done in a single-thread
104 context. Instantiation of the classes has no restrictions. Given that
104 context. Instantiation of the classes has no restrictions. Given that
105 these classes are typically created at IPython startup time and before user
105 these classes are typically created at IPython startup time and before user
106 application code becomes active, in practice this should not pose any
106 application code becomes active, in practice this should not pose any
107 problems.
107 problems.
108 """
108 """
109 cls.registered = True
109 cls.registered = True
110 cls.magics = dict(line = magics['line'],
110 cls.magics = dict(line = magics['line'],
111 cell = magics['cell'])
111 cell = magics['cell'])
112 magics['line'] = {}
112 magics['line'] = {}
113 magics['cell'] = {}
113 magics['cell'] = {}
114 return cls
114 return cls
115
115
116
116
117 def record_magic(dct, magic_kind, magic_name, func):
117 def record_magic(dct, magic_kind, magic_name, func):
118 """Utility function to store a function as a magic of a specific kind.
118 """Utility function to store a function as a magic of a specific kind.
119
119
120 Parameters
120 Parameters
121 ----------
121 ----------
122 dct : dict
122 dct : dict
123 A dictionary with 'line' and 'cell' subdicts.
123 A dictionary with 'line' and 'cell' subdicts.
124
124
125 magic_kind : str
125 magic_kind : str
126 Kind of magic to be stored.
126 Kind of magic to be stored.
127
127
128 magic_name : str
128 magic_name : str
129 Key to store the magic as.
129 Key to store the magic as.
130
130
131 func : function
131 func : function
132 Callable object to store.
132 Callable object to store.
133 """
133 """
134 if magic_kind == 'line_cell':
134 if magic_kind == 'line_cell':
135 dct['line'][magic_name] = dct['cell'][magic_name] = func
135 dct['line'][magic_name] = dct['cell'][magic_name] = func
136 else:
136 else:
137 dct[magic_kind][magic_name] = func
137 dct[magic_kind][magic_name] = func
138
138
139
139
140 def validate_type(magic_kind):
140 def validate_type(magic_kind):
141 """Ensure that the given magic_kind is valid.
141 """Ensure that the given magic_kind is valid.
142
142
143 Check that the given magic_kind is one of the accepted spec types (stored
143 Check that the given magic_kind is one of the accepted spec types (stored
144 in the global `magic_spec`), raise ValueError otherwise.
144 in the global `magic_spec`), raise ValueError otherwise.
145 """
145 """
146 if magic_kind not in magic_spec:
146 if magic_kind not in magic_spec:
147 raise ValueError('magic_kind must be one of %s, %s given' %
147 raise ValueError('magic_kind must be one of %s, %s given' %
148 magic_kinds, magic_kind)
148 magic_kinds, magic_kind)
149
149
150
150
151 # The docstrings for the decorator below will be fairly similar for the two
151 # The docstrings for the decorator below will be fairly similar for the two
152 # types (method and function), so we generate them here once and reuse the
152 # types (method and function), so we generate them here once and reuse the
153 # templates below.
153 # templates below.
154 _docstring_template = \
154 _docstring_template = \
155 """Decorate the given {0} as {1} magic.
155 """Decorate the given {0} as {1} magic.
156
156
157 The decorator can be used with or without arguments, as follows.
157 The decorator can be used with or without arguments, as follows.
158
158
159 i) without arguments: it will create a {1} magic named as the {0} being
159 i) without arguments: it will create a {1} magic named as the {0} being
160 decorated::
160 decorated::
161
161
162 @deco
162 @deco
163 def foo(...)
163 def foo(...)
164
164
165 will create a {1} magic named `foo`.
165 will create a {1} magic named `foo`.
166
166
167 ii) with one string argument: which will be used as the actual name of the
167 ii) with one string argument: which will be used as the actual name of the
168 resulting magic::
168 resulting magic::
169
169
170 @deco('bar')
170 @deco('bar')
171 def foo(...)
171 def foo(...)
172
172
173 will create a {1} magic named `bar`.
173 will create a {1} magic named `bar`.
174 """
174 """
175
175
176 # These two are decorator factories. While they are conceptually very similar,
176 # These two are decorator factories. While they are conceptually very similar,
177 # there are enough differences in the details that it's simpler to have them
177 # there are enough differences in the details that it's simpler to have them
178 # written as completely standalone functions rather than trying to share code
178 # written as completely standalone functions rather than trying to share code
179 # and make a single one with convoluted logic.
179 # and make a single one with convoluted logic.
180
180
181 def _method_magic_marker(magic_kind):
181 def _method_magic_marker(magic_kind):
182 """Decorator factory for methods in Magics subclasses.
182 """Decorator factory for methods in Magics subclasses.
183 """
183 """
184
184
185 validate_type(magic_kind)
185 validate_type(magic_kind)
186
186
187 # This is a closure to capture the magic_kind. We could also use a class,
187 # This is a closure to capture the magic_kind. We could also use a class,
188 # but it's overkill for just that one bit of state.
188 # but it's overkill for just that one bit of state.
189 def magic_deco(arg):
189 def magic_deco(arg):
190 call = lambda f, *a, **k: f(*a, **k)
190 call = lambda f, *a, **k: f(*a, **k)
191
191
192 if callable(arg):
192 if callable(arg):
193 # "Naked" decorator call (just @foo, no args)
193 # "Naked" decorator call (just @foo, no args)
194 func = arg
194 func = arg
195 name = func.func_name
195 name = func.func_name
196 retval = decorator(call, func)
196 retval = decorator(call, func)
197 record_magic(magics, magic_kind, name, name)
197 record_magic(magics, magic_kind, name, name)
198 elif isinstance(arg, basestring):
198 elif isinstance(arg, basestring):
199 # Decorator called with arguments (@foo('bar'))
199 # Decorator called with arguments (@foo('bar'))
200 name = arg
200 name = arg
201 def mark(func, *a, **kw):
201 def mark(func, *a, **kw):
202 record_magic(magics, magic_kind, name, func.func_name)
202 record_magic(magics, magic_kind, name, func.func_name)
203 return decorator(call, func)
203 return decorator(call, func)
204 retval = mark
204 retval = mark
205 else:
205 else:
206 raise ValueError("Decorator can only be called with "
206 raise ValueError("Decorator can only be called with "
207 "string or function")
207 "string or function")
208 return retval
208 return retval
209
209
210 # Ensure the resulting decorator has a usable docstring
210 # Ensure the resulting decorator has a usable docstring
211 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
211 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
212 return magic_deco
212 return magic_deco
213
213
214
214
215 def _function_magic_marker(magic_kind):
215 def _function_magic_marker(magic_kind):
216 """Decorator factory for standalone functions.
216 """Decorator factory for standalone functions.
217 """
217 """
218 validate_type(magic_kind)
218 validate_type(magic_kind)
219
219
220 # This is a closure to capture the magic_kind. We could also use a class,
220 # This is a closure to capture the magic_kind. We could also use a class,
221 # but it's overkill for just that one bit of state.
221 # but it's overkill for just that one bit of state.
222 def magic_deco(arg):
222 def magic_deco(arg):
223 call = lambda f, *a, **k: f(*a, **k)
223 call = lambda f, *a, **k: f(*a, **k)
224
224
225 # Find get_ipython() in the caller's namespace
225 # Find get_ipython() in the caller's namespace
226 caller = sys._getframe(1)
226 caller = sys._getframe(1)
227 for ns in ['f_locals', 'f_globals', 'f_builtins']:
227 for ns in ['f_locals', 'f_globals', 'f_builtins']:
228 get_ipython = getattr(caller, ns).get('get_ipython')
228 get_ipython = getattr(caller, ns).get('get_ipython')
229 if get_ipython is not None:
229 if get_ipython is not None:
230 break
230 break
231 else:
231 else:
232 raise('Decorator can only run in context where `get_ipython` exists')
232 raise('Decorator can only run in context where `get_ipython` exists')
233
233
234 ip = get_ipython()
234 ip = get_ipython()
235
235
236 if callable(arg):
236 if callable(arg):
237 # "Naked" decorator call (just @foo, no args)
237 # "Naked" decorator call (just @foo, no args)
238 func = arg
238 func = arg
239 name = func.func_name
239 name = func.func_name
240 ip.register_magic_function(func, magic_kind, name)
240 ip.register_magic_function(func, magic_kind, name)
241 retval = decorator(call, func)
241 retval = decorator(call, func)
242 elif isinstance(arg, basestring):
242 elif isinstance(arg, basestring):
243 # Decorator called with arguments (@foo('bar'))
243 # Decorator called with arguments (@foo('bar'))
244 name = arg
244 name = arg
245 def mark(func, *a, **kw):
245 def mark(func, *a, **kw):
246 ip.register_magic_function(func, magic_kind, name)
246 ip.register_magic_function(func, magic_kind, name)
247 return decorator(call, func)
247 return decorator(call, func)
248 retval = mark
248 retval = mark
249 else:
249 else:
250 raise ValueError("Decorator can only be called with "
250 raise ValueError("Decorator can only be called with "
251 "string or function")
251 "string or function")
252 return retval
252 return retval
253
253
254 # Ensure the resulting decorator has a usable docstring
254 # Ensure the resulting decorator has a usable docstring
255 ds = _docstring_template.format('function', magic_kind)
255 ds = _docstring_template.format('function', magic_kind)
256
256
257 ds += dedent("""
257 ds += dedent("""
258 Note: this decorator can only be used in a context where IPython is already
258 Note: this decorator can only be used in a context where IPython is already
259 active, so that the `get_ipython()` call succeeds. You can therefore use
259 active, so that the `get_ipython()` call succeeds. You can therefore use
260 it in your startup files loaded after IPython initializes, but *not* in the
260 it in your startup files loaded after IPython initializes, but *not* in the
261 IPython configuration file itself, which is executed before IPython is
261 IPython configuration file itself, which is executed before IPython is
262 fully up and running. Any file located in the `startup` subdirectory of
262 fully up and running. Any file located in the `startup` subdirectory of
263 your configuration profile will be OK in this sense.
263 your configuration profile will be OK in this sense.
264 """)
264 """)
265
265
266 magic_deco.__doc__ = ds
266 magic_deco.__doc__ = ds
267 return magic_deco
267 return magic_deco
268
268
269
269
270 # Create the actual decorators for public use
270 # Create the actual decorators for public use
271
271
272 # These three are used to decorate methods in class definitions
272 # These three are used to decorate methods in class definitions
273 line_magic = _method_magic_marker('line')
273 line_magic = _method_magic_marker('line')
274 cell_magic = _method_magic_marker('cell')
274 cell_magic = _method_magic_marker('cell')
275 line_cell_magic = _method_magic_marker('line_cell')
275 line_cell_magic = _method_magic_marker('line_cell')
276
276
277 # These three decorate standalone functions and perform the decoration
277 # These three decorate standalone functions and perform the decoration
278 # immediately. They can only run where get_ipython() works
278 # immediately. They can only run where get_ipython() works
279 register_line_magic = _function_magic_marker('line')
279 register_line_magic = _function_magic_marker('line')
280 register_cell_magic = _function_magic_marker('cell')
280 register_cell_magic = _function_magic_marker('cell')
281 register_line_cell_magic = _function_magic_marker('line_cell')
281 register_line_cell_magic = _function_magic_marker('line_cell')
282
282
283 #-----------------------------------------------------------------------------
283 #-----------------------------------------------------------------------------
284 # Core Magic classes
284 # Core Magic classes
285 #-----------------------------------------------------------------------------
285 #-----------------------------------------------------------------------------
286
286
287 class MagicsManager(Configurable):
287 class MagicsManager(Configurable):
288 """Object that handles all magic-related functionality for IPython.
288 """Object that handles all magic-related functionality for IPython.
289 """
289 """
290 # Non-configurable class attributes
290 # Non-configurable class attributes
291
291
292 # A two-level dict, first keyed by magic type, then by magic function, and
292 # A two-level dict, first keyed by magic type, then by magic function, and
293 # holding the actual callable object as value. This is the dict used for
293 # holding the actual callable object as value. This is the dict used for
294 # magic function dispatch
294 # magic function dispatch
295 magics = Dict
295 magics = Dict
296
296
297 # A registry of the original objects that we've been given holding magics.
297 # A registry of the original objects that we've been given holding magics.
298 registry = Dict
298 registry = Dict
299
299
300 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
300 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
301
301
302 auto_magic = Bool
302 auto_magic = Bool(True, config=True, help=
303
303 "Automatically call line magics without requiring explicit % prefix")
304
304 _auto_status = [
305 _auto_status = [
305 'Automagic is OFF, % prefix IS needed for line magics.',
306 'Automagic is OFF, % prefix IS needed for line magics.',
306 'Automagic is ON, % prefix IS NOT needed for line magics.']
307 'Automagic is ON, % prefix IS NOT needed for line magics.']
307
308
308 user_magics = Instance('IPython.core.magics.UserMagics')
309 user_magics = Instance('IPython.core.magics.UserMagics')
309
310
310 def __init__(self, shell=None, config=None, user_magics=None, **traits):
311 def __init__(self, shell=None, config=None, user_magics=None, **traits):
311
312
312 super(MagicsManager, self).__init__(shell=shell, config=config,
313 super(MagicsManager, self).__init__(shell=shell, config=config,
313 user_magics=user_magics, **traits)
314 user_magics=user_magics, **traits)
314 self.magics = dict(line={}, cell={})
315 self.magics = dict(line={}, cell={})
315 # Let's add the user_magics to the registry for uniformity, so *all*
316 # Let's add the user_magics to the registry for uniformity, so *all*
316 # registered magic containers can be found there.
317 # registered magic containers can be found there.
317 self.registry[user_magics.__class__.__name__] = user_magics
318 self.registry[user_magics.__class__.__name__] = user_magics
318
319
319 def auto_status(self):
320 def auto_status(self):
320 """Return descriptive string with automagic status."""
321 """Return descriptive string with automagic status."""
321 return self._auto_status[self.auto_magic]
322 return self._auto_status[self.auto_magic]
322
323
323 def lsmagic(self):
324 def lsmagic(self):
324 """Return a dict of currently available magic functions.
325 """Return a dict of currently available magic functions.
325
326
326 The return dict has the keys 'line' and 'cell', corresponding to the
327 The return dict has the keys 'line' and 'cell', corresponding to the
327 two types of magics we support. Each value is a list of names.
328 two types of magics we support. Each value is a list of names.
328 """
329 """
329 return self.magics
330 return self.magics
330
331
331 def register(self, *magic_objects):
332 def register(self, *magic_objects):
332 """Register one or more instances of Magics.
333 """Register one or more instances of Magics.
334
335 Take one or more classes or instances of classes that subclass the main
336 `core.Magic` class, and register them with IPython to use the magic
337 functions they provide. The registration process will then ensure that
338 any methods that have decorated to provide line and/or cell magics will
339 be recognized with the `%x`/`%%x` syntax as a line/cell magic
340 respectively.
341
342 If classes are given, they will be instantiated with the default
343 constructor. If your classes need a custom constructor, you should
344 instanitate them first and pass the instance.
345
346 The provided arguments can be an arbitrary mix of classes and instances.
347
348 Parameters
349 ----------
350 magic_objects : one or more classes or instances
333 """
351 """
334 # Start by validating them to ensure they have all had their magic
352 # Start by validating them to ensure they have all had their magic
335 # methods registered at the instance level
353 # methods registered at the instance level
336 for m in magic_objects:
354 for m in magic_objects:
337 if not m.registered:
355 if not m.registered:
338 raise ValueError("Class of magics %r was constructed without "
356 raise ValueError("Class of magics %r was constructed without "
339 "the @register_macics class decorator")
357 "the @register_macics class decorator")
340 if type(m) is type:
358 if type(m) is type:
341 # If we're given an uninstantiated class
359 # If we're given an uninstantiated class
342 m = m(self.shell)
360 m = m(self.shell)
343
361
344 # Now that we have an instance, we can register it and update the
362 # Now that we have an instance, we can register it and update the
345 # table of callables
363 # table of callables
346 self.registry[m.__class__.__name__] = m
364 self.registry[m.__class__.__name__] = m
347 for mtype in magic_kinds:
365 for mtype in magic_kinds:
348 self.magics[mtype].update(m.magics[mtype])
366 self.magics[mtype].update(m.magics[mtype])
349
367
350 def register_function(self, func, magic_kind='line', magic_name=None):
368 def register_function(self, func, magic_kind='line', magic_name=None):
351 """Expose a standalone function as magic function for ipython.
369 """Expose a standalone function as magic function for IPython.
370
371 This will create an IPython magic (line, cell or both) from a
372 standalone function. The functions should have the following
373 signatures:
374
375 * For line magics: `def f(line)`
376 * For cell magics: `def f(line, cell)`
377 * For a function that does both: `def f(line, cell=None)`
378
379 In the latter case, the function will be called with `cell==None` when
380 invoked as `%f`, and with cell as a string when invoked as `%%f`.
381
382 Parameters
383 ----------
384 func : callable
385 Function to be registered as a magic.
386
387 magic_kind : str
388 Kind of magic, one of 'line', 'cell' or 'line_cell'
389
390 magic_name : optional str
391 If given, the name the magic will have in the IPython namespace. By
392 default, the name of the function itself is used.
352 """
393 """
353
394
354 # Create the new method in the user_magics and register it in the
395 # Create the new method in the user_magics and register it in the
355 # global table
396 # global table
356 validate_type(magic_kind)
397 validate_type(magic_kind)
357 magic_name = func.func_name if magic_name is None else magic_name
398 magic_name = func.func_name if magic_name is None else magic_name
358 setattr(self.user_magics, magic_name, func)
399 setattr(self.user_magics, magic_name, func)
359 record_magic(self.magics, magic_kind, magic_name, func)
400 record_magic(self.magics, magic_kind, magic_name, func)
360
401
361 def define_magic(self, name, func):
402 def define_magic(self, name, func):
362 """Support for deprecated API.
403 """[Deprecated] Expose own function as magic function for IPython.
404
405 Example::
406
407 def foo_impl(self, parameter_s=''):
408 'My very own magic!. (Use docstrings, IPython reads them).'
409 print 'Magic function. Passed parameter is between < >:'
410 print '<%s>' % parameter_s
411 print 'The self object is:', self
363
412
364 This method exists only to support the old-style definition of magics.
413 ip.define_magic('foo',foo_impl)
365 It will eventually be removed. Deliberately not documented further.
366 """
414 """
367 meth = types.MethodType(func, self.user_magics)
415 meth = types.MethodType(func, self.user_magics)
368 setattr(self.user_magics, name, meth)
416 setattr(self.user_magics, name, meth)
369 record_magic(self.magics, 'line', name, meth)
417 record_magic(self.magics, 'line', name, meth)
370
418
371 # Key base class that provides the central functionality for magics.
419 # Key base class that provides the central functionality for magics.
372
420
373 class Magics(object):
421 class Magics(object):
374 """Base class for implementing magic functions.
422 """Base class for implementing magic functions.
375
423
376 Shell functions which can be reached as %function_name. All magic
424 Shell functions which can be reached as %function_name. All magic
377 functions should accept a string, which they can parse for their own
425 functions should accept a string, which they can parse for their own
378 needs. This can make some functions easier to type, eg `%cd ../`
426 needs. This can make some functions easier to type, eg `%cd ../`
379 vs. `%cd("../")`
427 vs. `%cd("../")`
380
428
381 Classes providing magic functions need to subclass this class, and they
429 Classes providing magic functions need to subclass this class, and they
382 MUST:
430 MUST:
383
431
384 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
432 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
385 individual methods as magic functions, AND
433 individual methods as magic functions, AND
386
434
387 - Use the class decorator `@magics_class` to ensure that the magic
435 - Use the class decorator `@magics_class` to ensure that the magic
388 methods are properly registered at the instance level upon instance
436 methods are properly registered at the instance level upon instance
389 initialization.
437 initialization.
390
438
391 See :mod:`magic_functions` for examples of actual implementation classes.
439 See :mod:`magic_functions` for examples of actual implementation classes.
392 """
440 """
393 # Dict holding all command-line options for each magic.
441 # Dict holding all command-line options for each magic.
394 options_table = None
442 options_table = None
395 # Dict for the mapping of magic names to methods, set by class decorator
443 # Dict for the mapping of magic names to methods, set by class decorator
396 magics = None
444 magics = None
397 # Flag to check that the class decorator was properly applied
445 # Flag to check that the class decorator was properly applied
398 registered = False
446 registered = False
399 # Instance of IPython shell
447 # Instance of IPython shell
400 shell = None
448 shell = None
401
449
402 def __init__(self, shell):
450 def __init__(self, shell):
403 if not(self.__class__.registered):
451 if not(self.__class__.registered):
404 raise ValueError('Magics subclass without registration - '
452 raise ValueError('Magics subclass without registration - '
405 'did you forget to apply @magics_class?')
453 'did you forget to apply @magics_class?')
406 self.shell = shell
454 self.shell = shell
407 self.options_table = {}
455 self.options_table = {}
408 # The method decorators are run when the instance doesn't exist yet, so
456 # The method decorators are run when the instance doesn't exist yet, so
409 # they can only record the names of the methods they are supposed to
457 # they can only record the names of the methods they are supposed to
410 # grab. Only now, that the instance exists, can we create the proper
458 # grab. Only now, that the instance exists, can we create the proper
411 # mapping to bound methods. So we read the info off the original names
459 # mapping to bound methods. So we read the info off the original names
412 # table and replace each method name by the actual bound method.
460 # table and replace each method name by the actual bound method.
413 for mtype in magic_kinds:
461 for mtype in magic_kinds:
414 tab = self.magics[mtype]
462 tab = self.magics[mtype]
415 # must explicitly use keys, as we're mutating this puppy
463 # must explicitly use keys, as we're mutating this puppy
416 for magic_name in tab.keys():
464 for magic_name in tab.keys():
417 meth_name = tab[magic_name]
465 meth_name = tab[magic_name]
418 if isinstance(meth_name, basestring):
466 if isinstance(meth_name, basestring):
419 tab[magic_name] = getattr(self, meth_name)
467 tab[magic_name] = getattr(self, meth_name)
420
468
421 def arg_err(self,func):
469 def arg_err(self,func):
422 """Print docstring if incorrect arguments were passed"""
470 """Print docstring if incorrect arguments were passed"""
423 print 'Error in arguments:'
471 print 'Error in arguments:'
424 print oinspect.getdoc(func)
472 print oinspect.getdoc(func)
425
473
426 def format_latex(self, strng):
474 def format_latex(self, strng):
427 """Format a string for latex inclusion."""
475 """Format a string for latex inclusion."""
428
476
429 # Characters that need to be escaped for latex:
477 # Characters that need to be escaped for latex:
430 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
478 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
431 # Magic command names as headers:
479 # Magic command names as headers:
432 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
480 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
433 re.MULTILINE)
481 re.MULTILINE)
434 # Magic commands
482 # Magic commands
435 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
483 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
436 re.MULTILINE)
484 re.MULTILINE)
437 # Paragraph continue
485 # Paragraph continue
438 par_re = re.compile(r'\\$',re.MULTILINE)
486 par_re = re.compile(r'\\$',re.MULTILINE)
439
487
440 # The "\n" symbol
488 # The "\n" symbol
441 newline_re = re.compile(r'\\n')
489 newline_re = re.compile(r'\\n')
442
490
443 # Now build the string for output:
491 # Now build the string for output:
444 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
492 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
445 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
493 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
446 strng)
494 strng)
447 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
495 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
448 strng = par_re.sub(r'\\\\',strng)
496 strng = par_re.sub(r'\\\\',strng)
449 strng = escape_re.sub(r'\\\1',strng)
497 strng = escape_re.sub(r'\\\1',strng)
450 strng = newline_re.sub(r'\\textbackslash{}n',strng)
498 strng = newline_re.sub(r'\\textbackslash{}n',strng)
451 return strng
499 return strng
452
500
453 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
501 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
454 """Parse options passed to an argument string.
502 """Parse options passed to an argument string.
455
503
456 The interface is similar to that of getopt(), but it returns back a
504 The interface is similar to that of getopt(), but it returns back a
457 Struct with the options as keys and the stripped argument string still
505 Struct with the options as keys and the stripped argument string still
458 as a string.
506 as a string.
459
507
460 arg_str is quoted as a true sys.argv vector by using shlex.split.
508 arg_str is quoted as a true sys.argv vector by using shlex.split.
461 This allows us to easily expand variables, glob files, quote
509 This allows us to easily expand variables, glob files, quote
462 arguments, etc.
510 arguments, etc.
463
511
464 Options:
512 Options:
465 -mode: default 'string'. If given as 'list', the argument string is
513 -mode: default 'string'. If given as 'list', the argument string is
466 returned as a list (split on whitespace) instead of a string.
514 returned as a list (split on whitespace) instead of a string.
467
515
468 -list_all: put all option values in lists. Normally only options
516 -list_all: put all option values in lists. Normally only options
469 appearing more than once are put in a list.
517 appearing more than once are put in a list.
470
518
471 -posix (True): whether to split the input line in POSIX mode or not,
519 -posix (True): whether to split the input line in POSIX mode or not,
472 as per the conventions outlined in the shlex module from the
520 as per the conventions outlined in the shlex module from the
473 standard library."""
521 standard library."""
474
522
475 # inject default options at the beginning of the input line
523 # inject default options at the beginning of the input line
476 caller = sys._getframe(1).f_code.co_name
524 caller = sys._getframe(1).f_code.co_name
477 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
525 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
478
526
479 mode = kw.get('mode','string')
527 mode = kw.get('mode','string')
480 if mode not in ['string','list']:
528 if mode not in ['string','list']:
481 raise ValueError,'incorrect mode given: %s' % mode
529 raise ValueError,'incorrect mode given: %s' % mode
482 # Get options
530 # Get options
483 list_all = kw.get('list_all',0)
531 list_all = kw.get('list_all',0)
484 posix = kw.get('posix', os.name == 'posix')
532 posix = kw.get('posix', os.name == 'posix')
485 strict = kw.get('strict', True)
533 strict = kw.get('strict', True)
486
534
487 # Check if we have more than one argument to warrant extra processing:
535 # Check if we have more than one argument to warrant extra processing:
488 odict = {} # Dictionary with options
536 odict = {} # Dictionary with options
489 args = arg_str.split()
537 args = arg_str.split()
490 if len(args) >= 1:
538 if len(args) >= 1:
491 # If the list of inputs only has 0 or 1 thing in it, there's no
539 # If the list of inputs only has 0 or 1 thing in it, there's no
492 # need to look for options
540 # need to look for options
493 argv = arg_split(arg_str, posix, strict)
541 argv = arg_split(arg_str, posix, strict)
494 # Do regular option processing
542 # Do regular option processing
495 try:
543 try:
496 opts,args = getopt(argv,opt_str,*long_opts)
544 opts,args = getopt(argv,opt_str,*long_opts)
497 except GetoptError,e:
545 except GetoptError,e:
498 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
546 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
499 " ".join(long_opts)))
547 " ".join(long_opts)))
500 for o,a in opts:
548 for o,a in opts:
501 if o.startswith('--'):
549 if o.startswith('--'):
502 o = o[2:]
550 o = o[2:]
503 else:
551 else:
504 o = o[1:]
552 o = o[1:]
505 try:
553 try:
506 odict[o].append(a)
554 odict[o].append(a)
507 except AttributeError:
555 except AttributeError:
508 odict[o] = [odict[o],a]
556 odict[o] = [odict[o],a]
509 except KeyError:
557 except KeyError:
510 if list_all:
558 if list_all:
511 odict[o] = [a]
559 odict[o] = [a]
512 else:
560 else:
513 odict[o] = a
561 odict[o] = a
514
562
515 # Prepare opts,args for return
563 # Prepare opts,args for return
516 opts = Struct(odict)
564 opts = Struct(odict)
517 if mode == 'string':
565 if mode == 'string':
518 args = ' '.join(args)
566 args = ' '.join(args)
519
567
520 return opts,args
568 return opts,args
521
569
522 def default_option(self, fn, optstr):
570 def default_option(self, fn, optstr):
523 """Make an entry in the options_table for fn, with value optstr"""
571 """Make an entry in the options_table for fn, with value optstr"""
524
572
525 if fn not in self.lsmagic():
573 if fn not in self.lsmagic():
526 error("%s is not a magic function" % fn)
574 error("%s is not a magic function" % fn)
527 self.options_table[fn] = optstr
575 self.options_table[fn] = optstr
General Comments 0
You need to be logged in to leave comments. Login now