Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/core/magics/extension.py: 36%
28 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
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#-----------------------------------------------------------------------------
11#-----------------------------------------------------------------------------
12# Imports
13#-----------------------------------------------------------------------------
16# Our own packages
17from IPython.core.error import UsageError
18from IPython.core.magic import Magics, magics_class, line_magic
20#-----------------------------------------------------------------------------
21# Magic implementation classes
22#-----------------------------------------------------------------------------
24@magics_class
25class ExtensionMagics(Magics):
26 """Magics to manage the IPython extensions system."""
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)
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)
41 @line_magic
42 def unload_ext(self, module_str):
43 """Unload an IPython extension by its module name.
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.')
51 res = self.shell.extension_manager.unload_extension(module_str)
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)
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)