| 1 |
"""Implementation of magic functions for the extension machinery. |
| 2 |
""" |
| 3 |
#----------------------------------------------------------------------------- |
| 4 |
# Copyright (c) 2012 The IPython Development Team. |
| 5 |
# |
| 6 |
# Distributed under the terms of the Modified BSD License. |
| 7 |
# |
| 8 |
# The full license is in the file COPYING.txt, distributed with this software. |
| 9 |
#----------------------------------------------------------------------------- |
| 10 |
|
| 11 |
#----------------------------------------------------------------------------- |
| 12 |
# Imports |
| 13 |
#----------------------------------------------------------------------------- |
| 14 |
|
| 15 |
|
| 16 |
# Our own packages |
| 17 |
from IPython.core.error import UsageError |
| 18 |
from IPython.core.magic import Magics, magics_class, line_magic |
| 19 |
|
| 20 |
#----------------------------------------------------------------------------- |
| 21 |
# Magic implementation classes |
| 22 |
#----------------------------------------------------------------------------- |
| 23 |
|
| 24 |
@magics_class |
| 25 |
class ExtensionMagics(Magics): |
| 26 |
"""Magics to manage the IPython extensions system.""" |
| 27 |
|
| 28 |
@line_magic |
| 29 |
def load_ext(self, module_str): |
| 30 |
"""Load an IPython extension by its module name.""" |
| 31 |
if not module_str: |
| 32 |
raise UsageError('Missing module name.') |
| 33 |
res = self.shell.extension_manager.load_extension(module_str) |
| 34 |
|
| 35 |
if res == 'already loaded': |
| 36 |
print("The %s extension is already loaded. To reload it, use:" % module_str) |
| 37 |
print(" %reload_ext", module_str) |
| 38 |
elif res == 'no load function': |
| 39 |
print("The %s module is not an IPython extension." % module_str) |
| 40 |
|
| 41 |
@line_magic |
| 42 |
def unload_ext(self, module_str): |
| 43 |
"""Unload an IPython extension by its module name. |
| 44 |
|
| 45 |
Not all extensions can be unloaded, only those which define an |
| 46 |
``unload_ipython_extension`` function. |
| 47 |
""" |
| 48 |
if not module_str: |
| 49 |
raise UsageError('Missing module name.') |
| 50 |
|
| 51 |
res = self.shell.extension_manager.unload_extension(module_str) |
| 52 |
|
| 53 |
if res == 'no unload function': |
| 54 |
print("The %s extension doesn't define how to unload it." % module_str) |
| 55 |
elif res == "not loaded": |
| 56 |
print("The %s extension is not loaded." % module_str) |
| 57 |
|
| 58 |
@line_magic |
| 59 |
def reload_ext(self, module_str): |
| 60 |
"""Reload an IPython extension by its module name.""" |
| 61 |
if not module_str: |
| 62 |
raise UsageError('Missing module name.') |
| 63 |
self.shell.extension_manager.reload_extension(module_str) |