extension.py
63 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r6967 | """Implementation of magic functions for the extension machinery. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2012 The IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
# Our own packages | ||||
Bradley M. Froehle
|
r8278 | from IPython.core.error import UsageError | ||
Fernando Perez
|
r6973 | from IPython.core.magic import Magics, magics_class, line_magic | ||
Fernando Perez
|
r6967 | |||
#----------------------------------------------------------------------------- | ||||
# Magic implementation classes | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r6973 | @magics_class | ||
Fernando Perez
|
r6967 | class ExtensionMagics(Magics): | ||
"""Magics to manage the IPython extensions system.""" | ||||
@line_magic | ||||
def load_ext(self, module_str): | ||||
"""Load an IPython extension by its module name.""" | ||||
Bradley M. Froehle
|
r8278 | if not module_str: | ||
raise UsageError('Missing module name.') | ||||
Thomas Kluyver
|
r8586 | res = self.shell.extension_manager.load_extension(module_str) | ||
if res == 'already loaded': | ||||
Thomas Kluyver
|
r13348 | print("The %s extension is already loaded. To reload it, use:" % module_str) | ||
print(" %reload_ext", module_str) | ||||
Thomas Kluyver
|
r8586 | elif res == 'no load function': | ||
Thomas Kluyver
|
r13348 | print("The %s module is not an IPython extension." % module_str) | ||
Fernando Perez
|
r6967 | |||
@line_magic | ||||
def unload_ext(self, module_str): | ||||
Thomas Kluyver
|
r8549 | """Unload an IPython extension by its module name. | ||
Not all extensions can be unloaded, only those which define an | ||||
``unload_ipython_extension`` function. | ||||
""" | ||||
Bradley M. Froehle
|
r8278 | if not module_str: | ||
raise UsageError('Missing module name.') | ||||
Thomas Kluyver
|
r8549 | |||
res = self.shell.extension_manager.unload_extension(module_str) | ||||
if res == 'no unload function': | ||||
Thomas Kluyver
|
r13348 | print("The %s extension doesn't define how to unload it." % module_str) | ||
Thomas Kluyver
|
r8549 | elif res == "not loaded": | ||
Thomas Kluyver
|
r13348 | print("The %s extension is not loaded." % module_str) | ||
Fernando Perez
|
r6967 | |||
@line_magic | ||||
def reload_ext(self, module_str): | ||||
"""Reload an IPython extension by its module name.""" | ||||
Bradley M. Froehle
|
r8278 | if not module_str: | ||
raise UsageError('Missing module name.') | ||||
Fernando Perez
|
r6967 | self.shell.extension_manager.reload_extension(module_str) | ||