extension.py 2.5 KB

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